From b76bd71265c2c4d5e5686aaf42f58b79d0031ead Mon Sep 17 00:00:00 2001 From: Kevin_Ramirez Date: Mon, 9 Feb 2026 11:30:08 -0600 Subject: [PATCH 1/2] Modulo de tipo de cambio inteligente --- .../exchange_rate/exchange-rate-guard.svelte | 97 +++-- .../invoices/edit/invoice-top-fields.svelte | 376 +++++++++--------- .../dashboard/invoices/edit/[id]/+page.svelte | 34 +- 3 files changed, 272 insertions(+), 235 deletions(-) diff --git a/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte b/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte index 8f113fc1..4f703c02 100644 --- a/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte +++ b/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte @@ -1,59 +1,56 @@ - + 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 e3549f05..eec4e626 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 @@ -1,196 +1,214 @@ -
-
- - { - formData.operation_type = v; - }} - > - - - {formData.operation_type - ? (formData.operation_type === 'exp' ? 'Exp' : 'Imp') - : '...'} - - - - Exportación - Importación - - -
-
- - { - formData.invoice_type = v ?? ''; - }} - > - - - {formData.invoice_type - ? `${formData.invoice_type}` - : '...'} - - - - {#each invoiceTypes as type} - - {type.key} - {type.description} - - {/each} - - -
+
+
+ + { + formData.operation_type = v; + }} + > + + + {formData.operation_type ? (formData.operation_type === 'exp' ? 'Exp' : 'Imp') : '...'} + + + + Exportación + Importación + + +
+
+ + { + formData.invoice_type = v ?? ''; + }} + > + + + {formData.invoice_type ? `${formData.invoice_type}` : '...'} + + + + {#each invoiceTypes as type} + + {type.key} - {type.description} + + {/each} + + +
-
- - { - formData.is_pedimento_pending = checked; - }} - /> -
-
- - { - formData.pedimento_id = v ? parseInt(v) : null; - if (v) { - handlePedimentoChange(v); - } - }} - > - - - {formData.pedimento || 'Selecciona pedimento...'} - - - - {#each pedimentos as pedimento} - - {pedimento.customs_office?.slice(0, 2)}-{pedimento.license}-{pedimento.pedimento_number} - - {/each} - - -
+
+ + { + formData.is_pedimento_pending = checked; + }} + /> +
+
+ + { + formData.pedimento_id = v ? parseInt(v) : null; + if (v) { + handlePedimentoChange(v); + } + }} + > + + + {formData.pedimento || 'Selecciona pedimento...'} + + + + {#each pedimentos as pedimento} + + {pedimento.customs_office?.slice(0, 2)}-{pedimento.license}-{pedimento.pedimento_number} + + {/each} + + +
-
- - -
+
+ + +
-
- - -
+
+ + +
-
- - -
+
+ + +
-
- - -
+
+ + +
diff --git a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte index a28a8c3f..c59dd75e 100644 --- a/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte +++ b/frontend/src/routes/dashboard/invoices/edit/[id]/+page.svelte @@ -109,6 +109,9 @@ data.invoice?.financials?.exchange_rate ?? null ); + // Guardar la fecha inicial para detectar cambios manuales + let initialInvoiceDate = data.invoice?.invoice_date || ''; + let showExchangeRateDialog = $state(false); let missingExchangeRateDate = $state(''); @@ -149,13 +152,32 @@ // Efecto para actualizar el tipo de cambio cuando cambia la fecha de factura $effect(() => { - if (mounted && companyStore?.activeCompany?.id && InvoiceTopFieldsFormData?.invoice_date) { - getExchangeRateByDate(InvoiceTopFieldsFormData.invoice_date, companyStore.activeCompany.id) + const currentDate = InvoiceTopFieldsFormData?.invoice_date; + + // Solo proceder si: + // 1. Tenemos datos cargados y compañía activa + // 2. Es una factura nueva (isCreate) + // 3. O la fecha es distinta a la original (el usuario la cambió) + // 4. O no tenemos ningún tipo de cambio todavía + + const shouldFetch = + mounted && + companyStore?.activeCompany?.id && + currentDate && + (data.isCreate || currentDate !== initialInvoiceDate || !calculatedExchangeRate); + + if (shouldFetch) { + getExchangeRateByDate(currentDate, companyStore.activeCompany.id) .then((rate) => { if (rate) { calculatedExchangeRate = rate.value; } else { - calculatedExchangeRate = 0; + // Si no hay en catálogo, pero YA teníamos uno en la factura (y no cambió fecha), NO poner 0 + if (currentDate === initialInvoiceDate && data.invoice?.financials?.exchange_rate) { + calculatedExchangeRate = data.invoice.financials.exchange_rate; + } else { + calculatedExchangeRate = 0; + } } }) .catch((err) => console.error('Error auto-updating exchange rate:', err)); @@ -293,7 +315,7 @@ : null} Nueva Factura {#if invoiceTypeInfo} - + - {invoiceTypeInfo.description} {/if} @@ -420,9 +442,9 @@ }} />
-
+
From 99d81e66b96abec27209d41f0b98bf02be3a1f6d Mon Sep 17 00:00:00 2001 From: Kevin_Ramirez Date: Mon, 9 Feb 2026 13:39:03 -0600 Subject: [PATCH 2/2] Tipo de cambio actualizado --- .../exchange_rate/create-edit-dialog.svelte | 570 ++++---- .../exchange_rate/exchange-rate-guard.svelte | 38 +- .../invoices/edit/general-tab-form.svelte | 1197 +++++++++-------- frontend/src/lib/stores/ui.svelte.ts | 17 + frontend/src/routes/dashboard/+layout.svelte | 20 +- .../dashboard/invoices/edit/[id]/+page.svelte | 75 +- 6 files changed, 1042 insertions(+), 875 deletions(-) create mode 100644 frontend/src/lib/stores/ui.svelte.ts diff --git a/frontend/src/lib/components/dashboard/exchange_rate/create-edit-dialog.svelte b/frontend/src/lib/components/dashboard/exchange_rate/create-edit-dialog.svelte index f85eec38..babfe641 100644 --- a/frontend/src/lib/components/dashboard/exchange_rate/create-edit-dialog.svelte +++ b/frontend/src/lib/components/dashboard/exchange_rate/create-edit-dialog.svelte @@ -1,287 +1,327 @@ - - - - -
- - {#if isMissingRateContext} -
-
- -
-
-

- {title} -

-

- Para continuar con el guardado, es necesario registrar el tipo de cambio oficial para esta fecha. -

-
-
- {:else} - - - - {title} - - - {/if} + + -
{ e.preventDefault(); handleSubmit(); }} class="p-6 pt-4 space-y-6"> - {#if error} -
- - {error} -
- {/if} + +
+ {#if isMissingRateContext} +
+
+ +
+
+

+ {title} +

+

+ Para continuar con el guardado, es necesario registrar el tipo de cambio oficial + para esta fecha. +

+
+
+ {:else} + + + + {title} + + + {/if} -
-
- -
- - {#if isMissingRateContext} -
- Requerida -
- {/if} -
-
+ { + e.preventDefault(); + handleSubmit(); + }} + class="space-y-6 p-6 pt-4" + > + {#if error} +
+ + {error} +
+ {/if} -
-
- - -
-
-
$
- -
-

- Ej. 24.1234 -

-
-
+
+
+ +
+ + {#if isMissingRateContext} +
+ Requerida +
+ {/if} +
+
- - - - - - - -
- - +
+
+ + +
+
+
+ $ +
+ +
+

Ej. 24.1234

+
+
+ + + + + + + + +
+
+
- - - ¿Estás seguro? - - Se {isEdit ? 'actualizará' : 'creará'} el tipo de cambio con valor {formData.value} para el día {formData.date}. - - - - showConfirmation = false}>Cancelar - Confirmar - - - \ No newline at end of file + + + ¿Estás seguro? + + Se {isEdit ? 'actualizará' : 'creará'} el tipo de cambio con valor {formData.value} para el día + {formData.date}. + + + + (showConfirmation = false)}>Cancelar + Confirmar + + + diff --git a/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte b/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte index 4f703c02..c2c1796d 100644 --- a/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte +++ b/frontend/src/lib/components/dashboard/exchange_rate/exchange-rate-guard.svelte @@ -1,10 +1,16 @@ - + diff --git a/frontend/src/lib/components/dashboard/invoices/edit/general-tab-form.svelte b/frontend/src/lib/components/dashboard/invoices/edit/general-tab-form.svelte index f9c18642..1166b59a 100644 --- a/frontend/src/lib/components/dashboard/invoices/edit/general-tab-form.svelte +++ b/frontend/src/lib/components/dashboard/invoices/edit/general-tab-form.svelte @@ -1,4 +1,4 @@ -