diff --git a/front/src/routes/facturas/nueva/+page.svelte b/front/src/routes/facturas/nueva/+page.svelte index 5aa60ec..5a6c217 100644 --- a/front/src/routes/facturas/nueva/+page.svelte +++ b/front/src/routes/facturas/nueva/+page.svelte @@ -3,16 +3,16 @@ import { Button, InlineNotification, - Modal, NumberInput, Select, SelectItem, TextInput, DataTable } from 'carbon-components-svelte'; - import { Add, TrashCan, ArrowLeft } from 'carbon-icons-svelte'; + import { Add, TrashCan, ArrowLeft, Search } from 'carbon-icons-svelte'; import { goto, invalidate } from '$app/navigation'; import { crearFactura } from '$lib/hooks/facturas/crearFactura'; + import ModalBuscarCliente from './ModalBuscarCliente.svelte'; import type { Cliente, Repuesto, Almacen } from '../../../types'; let { data } = $props(); @@ -20,6 +20,10 @@ let error = $state(''); let enviando = $state(false); + // Aviso de stock bajo: solo se muestra cuando hay datos reales que lo justifiquen. + let hayStockBajo = $state(false); + let cantidadStockBajo = $state(0); + let clientes = $derived(Array.isArray(data.clientes) ? data.clientes : []); let repuestos = $derived(Array.isArray(data.repuestos) ? data.repuestos : []); let almacenes = $derived(Array.isArray(data.almacenes) ? data.almacenes : []); @@ -33,26 +37,39 @@ let observacion = $state(''); interface Item { + id: number; repuestoId: number; cantidad: number; } - let items = $state([{ repuestoId: 0, cantidad: 1 }]); + let nextItemId = $state(1); + let items = $state([{ id: 0, repuestoId: 0, cantidad: 1 }]); let pagoImporte = $state(0); let pagoFormaId = $state(0); let registrarPago = $state(false); + let modalClienteAbierto = $state(false); + + let clienteSeleccionado = $derived(clientes.find((c) => c.ID === clienteId)); + + const itemHeaders = [ + { key: 'producto', value: 'Producto', empty: false }, + { key: 'cantidad', value: 'Cantidad', empty: false }, + { key: 'precioUnitario', value: 'Precio Unit.', empty: false }, + { key: 'subtotal', value: 'Subtotal', empty: false }, + { key: 'acciones', value: '', empty: false } + ]; + function agregarItem() { - items = [...items, { repuestoId: 0, cantidad: 1 }]; + items = [...items, { id: nextItemId++, repuestoId: 0, cantidad: 1 }]; } - function quitarItem(i: number) { - items = items.filter((_, idx) => idx !== i); - if (items.length === 0) items = [{ repuestoId: 0, cantidad: 1 }]; + function quitarItem(id: number) { + items = items.filter((it) => it.id !== id); + if (items.length === 0) items = [{ id: 0, repuestoId: 0, cantidad: 1 }]; + nextItemId = Math.max(...items.map((it) => it.id)) + 1; } - // El precio siempre sale del catálogo; el usuario sólo elige el repuesto - // y la cantidad. function precioRepuesto(id: number): number { const r = repuestos.find((x) => x.ID === id); return r ? r.precio : 0; @@ -80,11 +97,24 @@ let impuestos = $derived(baseImponible * 0.21); let total = $derived(baseImponible + impuestos); - async function enviar() { + function onSeleccionarCliente(id: number) { + clienteId = id; + modalClienteAbierto = false; + } + + function clienteDefault() { + const d = clientes.find((c) => c.ID === 40); + if (d) clienteId = d.ID; + else if (clientes.length > 0) clienteId = clientes[0].ID; + } + + async function enviar(conPago = false) { if (enviando) return; enviando = true; error = ''; + if (conPago) registrarPago = true; + if (!clienteId) { error = 'Seleccioná un cliente.'; enviando = false; @@ -101,6 +131,11 @@ enviando = false; return; } + if (registrarPago && pagoImporte <= 0) { + error = 'Ingresá el importe del pago.'; + enviando = false; + return; + } const payload = { clienteId, @@ -142,148 +177,164 @@ {/if} -
-
-
-

Datos del Cliente

- - {#if clienteId} - {@const c = clientes.find((x) => x.ID === clienteId)} - {#if c} -

- Email: {c.email ?? '-'} · CUIT: {c.cuit ?? '-'} -

- {/if} - {/if} +
+ + {#if hayStockBajo} +
+ ⚠ Aviso: {cantidadStockBajo} producto(s) con stock por debajo del umbral mínimo
+ {/if} -
-

Almacén

- -
- -
-

Descuento

- -
- -
- -
- +
+
+ CUIT + +
-
-
-
-

Ítems de la Venta

- -
-
- {#each items as it, i (i)} -
-
- -
-
- -
-
- -
-
- {formatMonto(it.cantidad * precioRepuesto(it.repuestoId))} -
-
-
-
- {/each} -
-
+ +
+

Almacén

+ +
-
-

Totales

-
-
- Subtotal{formatMonto(subtotal)} -
-
- Descuento{formatMonto(descuentoTotal)} -
-
- IVA (21%){formatMonto(impuestos)} -
-
- Total{formatMonto(total)} + +
+
+

Ítems de la Venta

+ +
+
+ + + {#if cell.key === 'producto'} + + {:else if cell.key === 'cantidad'} + + {:else if cell.key === 'precioUnitario'} + + {:else if cell.key === 'subtotal'} + + {:else if cell.key === 'acciones'} +
+
+ + +
+
+
+ Descuento + +
+
+
+
Subtotal{formatMonto(subtotal)}
+
Descuento{formatMonto(descuentoTotal)}
+
IVA (21%){formatMonto(impuestos)}
+
Total{formatMonto(total)}
+
-
- - {#if registrarPago} -
- - -
- {/if} + +
+ +
+
+
-
- - -
+ +
+ + {#if registrarPago} +
+ + +
+ {/if} +
+ +
+ + +
+ + + diff --git a/front/src/routes/facturas/nueva/ModalBuscarCliente.svelte b/front/src/routes/facturas/nueva/ModalBuscarCliente.svelte new file mode 100644 index 0000000..234af66 --- /dev/null +++ b/front/src/routes/facturas/nueva/ModalBuscarCliente.svelte @@ -0,0 +1,61 @@ + + + +
+

Buscá un cliente por nombre, CUIT o email.

+ +
+ {#if clientesFiltrados.length > 0} + + + {#if cell.key === 'acciones'} + + {:else} + {cell.value} + {/if} + + + {:else} +

No se encontraron clientes.

+ {/if} +
+

Condición: para enviar el comprobante por email, el cliente debe estar validado y con email registrado.

+
+