From 58222e9ca9f9243dd97036831b70cf5655830a3f Mon Sep 17 00:00:00 2001 From: hreyes Date: Tue, 24 Mar 2026 09:16:05 -0600 Subject: [PATCH] fix/pedimentos-edit-datos-borrados --- .../code_pedimento_regimens/seed.py | 2 +- .../dashboard/invoices/edit/[id]/+page.svelte | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/backend/api/v1/modules/public/reference_data/code_pedimento_regimens/seed.py b/backend/api/v1/modules/public/reference_data/code_pedimento_regimens/seed.py index 0537b53e..76e5e1e9 100644 --- a/backend/api/v1/modules/public/reference_data/code_pedimento_regimens/seed.py +++ b/backend/api/v1/modules/public/reference_data/code_pedimento_regimens/seed.py @@ -33,7 +33,7 @@ seed = [ ("H1", "EXD", "E"), ("H8", "EXD", "E"), ("I1", "EXD", "E"), - ("J1", "EXD", "E"), + # ("J1", "EXD", "E"), # No existe en la tabla de pedimento_codes ("J2", "EXD", "E"), ("K1", "EXD", "E"), ("K2", "EXD", "E"), diff --git a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte index af17d17f..a12dfe38 100644 --- a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte +++ b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte @@ -486,9 +486,44 @@ return list.filter((p: { operation_type?: string }) => p.operation_type === opType); }); + let lastInheritancePedimentoId: number | null = null; + let hasInitializedInheritanceState = false; + // Al seleccionar pedimento, autollenar Aduana y Clave de régimen (document_type) // Fuente de verdad: pedimento.customs_office (aduana) y pedimento.regime (régimen) $effect(() => { + const pid = InvoiceTopFieldsFormData?.pedimento_id; + const currentPedimentoId = + typeof pid === 'string' + ? (Number.parseInt(pid, 10) || null) + : (pid ?? null); + + // Edición: solo reaplicar en transición real a nuevo pedimento. + // No sobrescribir/borrar al abrir la pantalla ni en cambios no relacionados. + if (!data.isCreate) { + if (!hasInitializedInheritanceState) { + lastInheritancePedimentoId = currentPedimentoId; + hasInitializedInheritanceState = true; + return; + } + + const pedimentoChanged = lastInheritancePedimentoId !== currentPedimentoId; + const hasPedimentoNow = !!currentPedimentoId; + const shouldReapplyFromPedimento = pedimentoChanged && hasPedimentoNow; + + lastInheritancePedimentoId = currentPedimentoId; + + if (!shouldReapplyFromPedimento || !generalFormData) return; + + const ped = (data.pedimentos || []).find((p: any) => p?.id === currentPedimentoId); + if (!ped) return; + + // En edición, cuando hay transición válida, pedimento manda (incluyendo vacío). + generalFormData.aduana = ped.customs_office || ''; + generalFormData.document_type = ped.regime || ''; + return; + } + // Si se marca pedimento pendiente, el componente ya limpia pedimento/remesa; // aquí limpiamos también los campos que dependen del pedimento. if (InvoiceTopFieldsFormData?.is_pedimento_pending) { @@ -499,9 +534,8 @@ return; } - const pid = InvoiceTopFieldsFormData?.pedimento_id; if (!pid || !generalFormData) return; - const id = typeof pid === 'string' ? parseInt(pid, 10) : pid; + const id = typeof pid === 'string' ? Number.parseInt(pid, 10) : pid; if (!id || Number.isNaN(id)) return; const ped = (filteredPedimentos || []).find((p: any) => p?.id === id);