From 5b4dabb72a2f4d902470e0aa3b1a40b741aaa627 Mon Sep 17 00:00:00 2001 From: hreyes Date: Wed, 11 Mar 2026 10:40:41 -0600 Subject: [PATCH] feature/filtro-pedimentos-tipo-invoices --- .../invoices/edit/invoice-top-fields.svelte | 23 +++++++++++++++++++ .../dashboard/invoices/edit/[id]/+page.svelte | 13 ++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/components/dashboard/invoices/edit/invoice-top-fields.svelte b/frontend/src/lib/components/dashboard/invoices/edit/invoice-top-fields.svelte index 98edefbd..3e548b20 100644 --- a/frontend/src/lib/components/dashboard/invoices/edit/invoice-top-fields.svelte +++ b/frontend/src/lib/components/dashboard/invoices/edit/invoice-top-fields.svelte @@ -31,6 +31,11 @@ const selectedPedimento = pedimentos.find((p) => p.id === parseInt(pedimentoId)); if (!selectedPedimento) return; + // Si el tipo de operación está vacío, llenarlo con el del pedimento + if (selectedPedimento.operation_type && !formData.operation_type) { + formData.operation_type = selectedPedimento.operation_type; + } + // Actualizar los campos del pedimento en formData formData.fecha_pedimento_del = selectedPedimento.pedimento_dates?.start_date || ''; formData.fecha_pedimento_al = selectedPedimento.pedimento_dates?.end_date || ''; @@ -121,6 +126,24 @@ } } }); + + // Limpiar pedimento seleccionado si ya no está en la lista filtrada (p. ej. al cambiar tipo de factura) + $effect(() => { + const pid = formData?.pedimento_id; + const list = pedimentos || []; + if (pid == null || pid === '') return; + const id = typeof pid === 'string' ? parseInt(pid, 10) : pid; + if (Number.isNaN(id)) return; + const found = list.some((p) => p.id === id); + if (!found) { + formData.pedimento_id = null; + formData.pedimento = ''; + formData.fecha_pedimento_del = ''; + formData.fecha_pedimento_al = ''; + formData.clave_pedimento = ''; + formData.regimen_pedimento = ''; + } + }); diff --git a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte index 9374f6c0..d58e7afb 100644 --- a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte +++ b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte @@ -472,6 +472,17 @@ return 'bg-muted text-muted-foreground border-border'; }); + // Pedimentos filtrados por tipo de operación: vacío → todos; imp/exp → solo los que coinciden + let filteredPedimentos = $derived.by(() => { + const list = data.pedimentos || []; + const opType = + InvoiceTopFieldsFormData?.operation_type || + data.invoice?.operation_type || + data.filters?.operation_type; + if (!opType || opType === '') return list; + return list.filter((p: { operation_type?: string }) => p.operation_type === opType); + }); + async function checkExchangeRate(date: string): Promise { if (!date || !companyStore?.activeCompany?.id) return true; @@ -859,7 +870,7 @@ invoice={data.invoice} bind:formData={InvoiceTopFieldsFormData} invoiceTypes={data.invoiceTypes || []} - pedimentos={data.pedimentos || []} + pedimentos={filteredPedimentos} defaultOperationType={data.filters?.operation_type ?? undefined} defaultInvoiceType={data.filters?.invoice_type ?? undefined} {invoiceType}