ahora usa un datatable

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-12-01 19:37:44 -03:00
1 parent 442140e412
commit 77b27df6dd
2 files changed
+56 -70

No files matched your search

@@ -1,26 +1,18 @@
<script lang="ts"> <script lang="ts">
import { import {
Button, DataTable,
InlineNotification, InlineNotification,
Modal, Modal,
NumberInput,
Select, Select,
SelectItem, SelectItem,
TextInput TextInput
} from 'carbon-components-svelte'; } from 'carbon-components-svelte';
import { Add, TrashCan } from 'carbon-icons-svelte';
import { invalidate } from '$app/navigation'; import { invalidate } from '$app/navigation';
import { registrarDevolucion } from '$lib/hooks/stock/registrarDevolucion'; import { registrarDevolucion } from '$lib/hooks/stock/registrarDevolucion';
import { obtenerFacturas } from '$lib/hooks/facturas/obtenerFacturas'; import { obtenerFacturas } from '$lib/hooks/facturas/obtenerFacturas';
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos'; import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
import type { Almacen, FacturaResumen, Repuesto } from '../../types'; import type { Almacen, FacturaResumen, Repuesto } from '../../types';
interface ItemDevolucion {
repuestoId: number;
cantidad: number;
precioUnitario: number;
}
let { let {
open = $bindable(), open = $bindable(),
onClose, onClose,
@@ -40,11 +32,39 @@
let facturaId = $state<number>(0); let facturaId = $state<number>(0);
let almacenId = $state<number>(0); let almacenId = $state<number>(0);
let motivo = $state(''); let motivo = $state('');
let items = $state<ItemDevolucion[]>([]);
function itemVacio(): ItemDevolucion { // Los ítems salen siempre de la factura elegida y no se pueden modificar.
return { repuestoId: 0, cantidad: 0, precioUnitario: 0 }; const facturaSeleccionada = $derived(facturas.find((f) => f.ID === facturaId));
} const itemsDevolucion = $derived(
(facturaSeleccionada?.detalles ?? []).map((d) => ({
repuestoId: d.repuestoId,
cantidad: d.cantidad,
precioUnitario: d.precioUnitario
}))
);
const formatoMoneda = new Intl.NumberFormat('es-AR', { style: 'currency', currency: 'ARS' });
const headers = [
{ key: 'repuesto', value: 'Repuesto' },
{ key: 'cantidad', value: 'Cantidad' },
{
key: 'precioUnitario',
value: 'Precio unitario',
display: (v: number) => formatoMoneda.format(v)
},
{ key: 'subtotal', value: 'Subtotal', display: (v: number) => formatoMoneda.format(v) }
];
const filas = $derived(
itemsDevolucion.map((i, idx) => ({
id: idx,
repuesto: repuestos.find((r) => r.ID === i.repuestoId)?.nombre ?? `Repuesto ${i.repuestoId}`,
cantidad: i.cantidad,
precioUnitario: i.precioUnitario,
subtotal: i.cantidad * i.precioUnitario
}))
);
$effect(() => { $effect(() => {
if (!open) return; if (!open) return;
@@ -52,7 +72,6 @@
facturaId = 0; facturaId = 0;
almacenId = almacenes[0]?.id ?? 0; almacenId = almacenes[0]?.id ?? 0;
motivo = ''; motivo = '';
items = [itemVacio()];
error = ''; error = '';
enviando = false; enviando = false;
@@ -71,6 +90,14 @@
}; };
}); });
// Al elegir una factura, el almacén de destino se autocompleta con el de la factura.
function alSeleccionarFactura(id: number) {
const factura = facturas.find((f) => f.ID === id);
if (factura?.almacenId && almacenes.some((a) => a.id === factura.almacenId)) {
almacenId = factura.almacenId;
}
}
function handleClose() { function handleClose() {
onClose?.(); onClose?.();
} }
@@ -80,14 +107,12 @@
enviando = true; enviando = true;
error = ''; error = '';
const itemsValidos = items.filter((i) => i.repuestoId > 0 && i.cantidad > 0);
if (!facturaId) { if (!facturaId) {
error = 'Seleccioná la factura de origen.'; error = 'Seleccioná la factura de origen.';
} else if (!motivo.trim()) { } else if (!motivo.trim()) {
error = 'Ingresá el motivo de la devolución.'; error = 'Ingresá el motivo de la devolución.';
} else if (itemsValidos.length === 0) { } else if (itemsDevolucion.length === 0) {
error = 'Agregá al menos un ítem con repuesto y cantidad.'; error = 'La factura seleccionada no tiene ítems para devolver.';
} }
if (error === '') { if (error === '') {
@@ -95,11 +120,7 @@
facturaId, facturaId,
almacenId: almacenId || undefined, almacenId: almacenId || undefined,
motivo: motivo.trim(), motivo: motivo.trim(),
items: itemsValidos.map((i) => ({ items: itemsDevolucion
repuestoId: i.repuestoId,
cantidad: i.cantidad,
precioUnitario: i.precioUnitario
}))
}); });
if (typeof res === 'string') { if (typeof res === 'string') {
error = res; error = res;
@@ -136,7 +157,11 @@
{/if} {/if}
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2"> <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
<Select labelText="Factura" bind:selected={facturaId}> <Select
labelText="Factura"
bind:selected={facturaId}
on:change={(e) => alSeleccionarFactura(Number(e.currentTarget.value))}
>
<SelectItem value={0} text="Seleccioná una factura" /> <SelectItem value={0} text="Seleccioná una factura" />
{#each facturas as f (f.ID)} {#each facturas as f (f.ID)}
<SelectItem <SelectItem
@@ -158,54 +183,14 @@
<TextInput labelText="Motivo" bind:value={motivo} maxlength={60} /> <TextInput labelText="Motivo" bind:value={motivo} maxlength={60} />
</div> </div>
<div class="mt-6 flex items-center justify-between"> <p class="mt-6 text-base font-semibold">Ítems devueltos</p>
<p class="text-base font-semibold">Ítems devueltos</p>
<Button kind="ghost" size="small" icon={Add} onclick={() => (items = [...items, itemVacio()])}
>Agregar ítem</Button
>
</div>
<div class="mt-3 flex flex-col gap-4"> <div class="mt-3">
{#each items as item, i (i)} {#if facturaSeleccionada}
<div class="rounded border border-gray-700 p-3"> <DataTable size="short" {headers} rows={filas} />
<div class="flex items-start justify-between gap-2"> {:else}
<p class="text-sm font-semibold">Ítem {i + 1}</p> <p class="text-sm text-gray-400">Seleccioná una factura para ver los ítems a devolver.</p>
{#if items.length > 1}
<Button
kind="ghost"
size="small"
icon={TrashCan}
onclick={() => (items = items.filter((_, idx) => idx !== i))}>Quitar</Button
>
{/if} {/if}
</div> </div>
<div class="mt-2 grid grid-cols-1 gap-3 sm:grid-cols-3">
<Select labelText="Repuesto" bind:selected={item.repuestoId}>
<SelectItem value={0} text="Seleccioná un repuesto" />
{#each repuestos as r (r.ID)}
<SelectItem value={r.ID} text={r.nombre} />
{/each}
</Select>
<NumberInput
labelText="Cantidad"
bind:value={item.cantidad}
min={0}
step={1}
allowEmpty
/>
<NumberInput
labelText="Precio unitario"
bind:value={item.precioUnitario}
min={0}
step={0.01}
allowEmpty
/>
</div>
</div>
{/each}
</div>
</Modal> </Modal>
</form> </form>
+1
View File
@@ -373,6 +373,7 @@ export interface FacturaResumen {
total?: number; total?: number;
cliente?: Cliente; cliente?: Cliente;
detalles?: Array<{ detalles?: Array<{
repuestoId: number;
productoId: number; productoId: number;
cantidad: number; cantidad: number;
precioUnitario: number; precioUnitario: number;