Private
Public Access
ahora usa un datatable
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
442140e412
commit
77b27df6dd
2 files changed
+58
-72
No files matched your search
@@ -1,26 +1,18 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Button,
|
||||
DataTable,
|
||||
InlineNotification,
|
||||
Modal,
|
||||
NumberInput,
|
||||
Select,
|
||||
SelectItem,
|
||||
TextInput
|
||||
} from 'carbon-components-svelte';
|
||||
import { Add, TrashCan } from 'carbon-icons-svelte';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { registrarDevolucion } from '$lib/hooks/stock/registrarDevolucion';
|
||||
import { obtenerFacturas } from '$lib/hooks/facturas/obtenerFacturas';
|
||||
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
|
||||
import type { Almacen, FacturaResumen, Repuesto } from '../../types';
|
||||
|
||||
interface ItemDevolucion {
|
||||
repuestoId: number;
|
||||
cantidad: number;
|
||||
precioUnitario: number;
|
||||
}
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
@@ -40,11 +32,39 @@
|
||||
let facturaId = $state<number>(0);
|
||||
let almacenId = $state<number>(0);
|
||||
let motivo = $state('');
|
||||
let items = $state<ItemDevolucion[]>([]);
|
||||
|
||||
function itemVacio(): ItemDevolucion {
|
||||
return { repuestoId: 0, cantidad: 0, precioUnitario: 0 };
|
||||
}
|
||||
// Los ítems salen siempre de la factura elegida y no se pueden modificar.
|
||||
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(() => {
|
||||
if (!open) return;
|
||||
@@ -52,7 +72,6 @@
|
||||
facturaId = 0;
|
||||
almacenId = almacenes[0]?.id ?? 0;
|
||||
motivo = '';
|
||||
items = [itemVacio()];
|
||||
error = '';
|
||||
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() {
|
||||
onClose?.();
|
||||
}
|
||||
@@ -80,14 +107,12 @@
|
||||
enviando = true;
|
||||
error = '';
|
||||
|
||||
const itemsValidos = items.filter((i) => i.repuestoId > 0 && i.cantidad > 0);
|
||||
|
||||
if (!facturaId) {
|
||||
error = 'Seleccioná la factura de origen.';
|
||||
} else if (!motivo.trim()) {
|
||||
error = 'Ingresá el motivo de la devolución.';
|
||||
} else if (itemsValidos.length === 0) {
|
||||
error = 'Agregá al menos un ítem con repuesto y cantidad.';
|
||||
} else if (itemsDevolucion.length === 0) {
|
||||
error = 'La factura seleccionada no tiene ítems para devolver.';
|
||||
}
|
||||
|
||||
if (error === '') {
|
||||
@@ -95,11 +120,7 @@
|
||||
facturaId,
|
||||
almacenId: almacenId || undefined,
|
||||
motivo: motivo.trim(),
|
||||
items: itemsValidos.map((i) => ({
|
||||
repuestoId: i.repuestoId,
|
||||
cantidad: i.cantidad,
|
||||
precioUnitario: i.precioUnitario
|
||||
}))
|
||||
items: itemsDevolucion
|
||||
});
|
||||
if (typeof res === 'string') {
|
||||
error = res;
|
||||
@@ -136,7 +157,11 @@
|
||||
{/if}
|
||||
|
||||
<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" />
|
||||
{#each facturas as f (f.ID)}
|
||||
<SelectItem
|
||||
@@ -158,54 +183,14 @@
|
||||
<TextInput labelText="Motivo" bind:value={motivo} maxlength={60} />
|
||||
</div>
|
||||
|
||||
<div class="mt-6 flex items-center justify-between">
|
||||
<p class="text-base font-semibold">Ítems devueltos</p>
|
||||
<Button kind="ghost" size="small" icon={Add} onclick={() => (items = [...items, itemVacio()])}
|
||||
>Agregar ítem</Button
|
||||
>
|
||||
</div>
|
||||
<p class="mt-6 text-base font-semibold">Ítems devueltos</p>
|
||||
|
||||
<div class="mt-3 flex flex-col gap-4">
|
||||
{#each items as item, i (i)}
|
||||
<div class="rounded border border-gray-700 p-3">
|
||||
<div class="flex items-start justify-between gap-2">
|
||||
<p class="text-sm font-semibold">Ítem {i + 1}</p>
|
||||
{#if items.length > 1}
|
||||
<Button
|
||||
kind="ghost"
|
||||
size="small"
|
||||
icon={TrashCan}
|
||||
onclick={() => (items = items.filter((_, idx) => idx !== i))}>Quitar</Button
|
||||
>
|
||||
{/if}
|
||||
</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 class="mt-3">
|
||||
{#if facturaSeleccionada}
|
||||
<DataTable size="short" {headers} rows={filas} />
|
||||
{:else}
|
||||
<p class="text-sm text-gray-400">Seleccioná una factura para ver los ítems a devolver.</p>
|
||||
{/if}
|
||||
</div>
|
||||
</Modal>
|
||||
</form>
|
||||
Vendored
+2
-1
@@ -372,7 +372,8 @@ export interface FacturaResumen {
|
||||
numero?: string;
|
||||
total?: number;
|
||||
cliente?: Cliente;
|
||||
detalles?: Array<{
|
||||
detalles?: Array<{
|
||||
repuestoId: number;
|
||||
productoId: number;
|
||||
cantidad: number;
|
||||
precioUnitario: number;
|
||||
|
||||
Reference in new issue
Block a user