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 @@
}}
/>