fix: ya no se puede cambiar el precio desde la ui

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-11-28 16:49:17 -03:00
1 parent 837fd8fa44
commit 0160ba90d4
1 file changed
+66 -26
+66 -26
View File
@@ -35,38 +35,38 @@
interface Item { interface Item {
repuestoId: number; repuestoId: number;
cantidad: number; cantidad: number;
precioUnitario: number;
} }
let items = $state<Item[]>([{ repuestoId: 0, cantidad: 1, precioUnitario: 0 }]); let items = $state<Item[]>([{ repuestoId: 0, cantidad: 1 }]);
let pagoImporte = $state<number>(0); let pagoImporte = $state<number>(0);
let pagoFormaId = $state<number>(0); let pagoFormaId = $state<number>(0);
let registrarPago = $state(false); let registrarPago = $state(false);
function agregarItem() { function agregarItem() {
items = [...items, { repuestoId: 0, cantidad: 1, precioUnitario: 0 }]; items = [...items, { repuestoId: 0, cantidad: 1 }];
} }
function quitarItem(i: number) { function quitarItem(i: number) {
items = items.filter((_, idx) => idx !== i); items = items.filter((_, idx) => idx !== i);
if (items.length === 0) items = [{ repuestoId: 0, cantidad: 1, precioUnitario: 0 }]; if (items.length === 0) items = [{ repuestoId: 0, cantidad: 1 }];
} }
// El precio siempre sale del catálogo; el usuario sólo elige el repuesto
// y la cantidad.
function precioRepuesto(id: number): number { function precioRepuesto(id: number): number {
const r = repuestos.find((x) => x.ID === id); const r = repuestos.find((x) => x.ID === id);
return r ? r.precio : 0; return r ? r.precio : 0;
} }
$effect(() => { function formatMonto(monto: number): string {
for (const it of items) { return monto.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' });
if (it.repuestoId > 0 && it.precioUnitario === 0) {
it.precioUnitario = precioRepuesto(it.repuestoId);
} }
}
});
let subtotal = $derived( let subtotal = $derived(
items.reduce((acc, it) => acc + (it.cantidad > 0 ? it.cantidad * (it.precioUnitario || 0) : 0), 0) items.reduce(
(acc, it) => acc + (it.cantidad > 0 ? it.cantidad * precioRepuesto(it.repuestoId) : 0),
0
)
); );
let descuentoTotal = $derived.by(() => { let descuentoTotal = $derived.by(() => {
@@ -85,10 +85,22 @@
enviando = true; enviando = true;
error = ''; error = '';
if (!clienteId) { error = 'Seleccioná un cliente.'; enviando = false; return; } if (!clienteId) {
if (!almacenId) { error = 'Seleccioná un almacén.'; enviando = false; return; } error = 'Seleccioná un cliente.';
enviando = false;
return;
}
if (!almacenId) {
error = 'Seleccioná un almacén.';
enviando = false;
return;
}
const itemsValidos = items.filter((i) => i.repuestoId > 0 && i.cantidad > 0); const itemsValidos = items.filter((i) => i.repuestoId > 0 && i.cantidad > 0);
if (itemsValidos.length === 0) { error = 'Agregá al menos un ítem válido.'; enviando = false; return; } if (itemsValidos.length === 0) {
error = 'Agregá al menos un ítem válido.';
enviando = false;
return;
}
const payload = { const payload = {
clienteId, clienteId,
@@ -96,10 +108,13 @@
items: itemsValidos.map((i) => ({ items: itemsValidos.map((i) => ({
repuestoId: i.repuestoId, repuestoId: i.repuestoId,
cantidad: i.cantidad, cantidad: i.cantidad,
precioUnitario: i.precioUnitario || precioRepuesto(i.repuestoId) precioUnitario: precioRepuesto(i.repuestoId)
})), })),
descuentoId: descuentoId || undefined, descuentoId: descuentoId || undefined,
pagos: registrarPago && pagoImporte > 0 ? [{ formaDePagoId: pagoFormaId, importe: pagoImporte }] : undefined, pagos:
registrarPago && pagoImporte > 0
? [{ formaDePagoId: pagoFormaId, importe: pagoImporte }]
: undefined,
enviarEmail, enviarEmail,
observacion: observacion.trim() || undefined observacion: observacion.trim() || undefined
}; };
@@ -140,7 +155,9 @@
{#if clienteId} {#if clienteId}
{@const c = clientes.find((x) => x.ID === clienteId)} {@const c = clientes.find((x) => x.ID === clienteId)}
{#if c} {#if c}
<p class="mt-2 text-sm text-gray-400">Email: {c.email ?? '-'} · CUIT: {c.cuit ?? '-'}</p> <p class="mt-2 text-sm text-gray-400">
Email: {c.email ?? '-'} · CUIT: {c.cuit ?? '-'}
</p>
{/if} {/if}
{/if} {/if}
</div> </div>
@@ -150,7 +167,10 @@
<Select labelText="Almacén de origen" bind:selected={almacenId}> <Select labelText="Almacén de origen" bind:selected={almacenId}>
<SelectItem value={0} text="Seleccioná un almacén" /> <SelectItem value={0} text="Seleccioná un almacén" />
{#each almacenes as a (a.id)} {#each almacenes as a (a.id)}
<SelectItem value={a.id} text={a.ubicacion ? `${a.codigo} · ${a.ubicacion}` : a.codigo} /> <SelectItem
value={a.id}
text={a.ubicacion ? `${a.codigo} · ${a.ubicacion}` : a.codigo}
/>
{/each} {/each}
</Select> </Select>
</div> </div>
@@ -160,7 +180,10 @@
<Select labelText="Descuento" bind:selected={descuentoId}> <Select labelText="Descuento" bind:selected={descuentoId}>
<SelectItem value={0} text="Sin descuento" /> <SelectItem value={0} text="Sin descuento" />
{#each descuentos as d (d.ID)} {#each descuentos as d (d.ID)}
<SelectItem value={d.ID} text={`${d.Nombre} (${d.Tipo === 'PORCENTAJE' ? d.Valor + '%' : '$' + d.Valor})`} /> <SelectItem
value={d.ID}
text={`${d.Nombre} (${d.Tipo === 'PORCENTAJE' ? d.Valor + '%' : '$' + d.Valor})`}
/>
{/each} {/each}
</Select> </Select>
</div> </div>
@@ -197,13 +220,22 @@
<NumberInput labelText="Cantidad" bind:value={it.cantidad} min={0} step={1} /> <NumberInput labelText="Cantidad" bind:value={it.cantidad} min={0} step={1} />
</div> </div>
<div class="col-span-3"> <div class="col-span-3">
<NumberInput labelText="Precio Unit." bind:value={it.precioUnitario} min={0} step={0.01} /> <TextInput
labelText="Precio Unit. (catálogo)"
value={formatMonto(precioRepuesto(it.repuestoId))}
readonly
/>
</div> </div>
<div class="col-span-1 text-right text-sm"> <div class="col-span-1 text-right text-sm">
{(it.cantidad * (it.precioUnitario || 0)).toLocaleString('es-AR', { style: 'currency', currency: 'ARS' })} {formatMonto(it.cantidad * precioRepuesto(it.repuestoId))}
</div> </div>
<div class="col-span-1"> <div class="col-span-1">
<Button kind="danger-ghost" size="small" icon={TrashCan} onclick={() => quitarItem(i)} /> <Button
kind="danger-ghost"
size="small"
icon={TrashCan}
onclick={() => quitarItem(i)}
/>
</div> </div>
</div> </div>
{/each} {/each}
@@ -213,10 +245,18 @@
<div class="rounded border p-4"> <div class="rounded border p-4">
<h2 class="mb-3 text-lg font-semibold">Totales</h2> <h2 class="mb-3 text-lg font-semibold">Totales</h2>
<div class="space-y-1 text-sm"> <div class="space-y-1 text-sm">
<div class="flex justify-between"><span>Subtotal</span><span>{subtotal.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' })}</span></div> <div class="flex justify-between">
<div class="flex justify-between"><span>Descuento</span><span>{descuentoTotal.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' })}</span></div> <span>Subtotal</span><span>{formatMonto(subtotal)}</span>
<div class="flex justify-between"><span>IVA (21%)</span><span>{impuestos.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' })}</span></div> </div>
<div class="flex justify-between text-lg font-bold"><span>Total</span><span>{total.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' })}</span></div> <div class="flex justify-between">
<span>Descuento</span><span>{formatMonto(descuentoTotal)}</span>
</div>
<div class="flex justify-between">
<span>IVA (21%)</span><span>{formatMonto(impuestos)}</span>
</div>
<div class="flex justify-between text-lg font-bold">
<span>Total</span><span>{formatMonto(total)}</span>
</div>
</div> </div>
</div> </div>