diff --git a/frontend/src/lib/components/dashboard/pedimentos/edit/general-tab-form.svelte b/frontend/src/lib/components/dashboard/pedimentos/edit/general-tab-form.svelte index d8ae6fa6..e9d55baa 100644 --- a/frontend/src/lib/components/dashboard/pedimentos/edit/general-tab-form.svelte +++ b/frontend/src/lib/components/dashboard/pedimentos/edit/general-tab-form.svelte @@ -320,6 +320,7 @@ exchange_rate: pedimento?.exchange_rate ?? null, // Date fields - Cargamos convirtiendo a local para el usuario entry_date: loadServerDate(datesData?.entry_date) || currentDate, + start_date: loadServerDate(datesData?.start_date) || null, end_date: loadServerDate(datesData?.end_date) || end_date, payment_date: loadServerDate(datesData?.payment_date) || payment_date, extraction_date: loadServerDate(datesData?.entry_date) || null, @@ -367,46 +368,63 @@ return locale === 'en' ? mode.transport_en : mode.transport_es; } + let refreshTrigger = $state(0); + + /** + * Obtiene la fecha que determina el tipo de cambio + * Regla: Siempre usar fecha de inicio, sin importar el método de transporte. + */ function getEffectiveExchangeDate(): string | null { - const entryMethod = getTransportByCode(formData?.pedimento_transport_means?.entry_exit); - const paymentCode = (entryMethod?.payment_date_code || 'E').toUpperCase(); - if (paymentCode === 'P') { - return formData?.payment_date || null; - } - return formData?.entry_date || null; + return formData?.start_date || null; } function getEffectiveDateLabel(): string { - const entryMethod = getTransportByCode(formData?.pedimento_transport_means?.entry_exit); - const paymentCode = (entryMethod?.payment_date_code || 'E').toUpperCase(); - return paymentCode === 'P' ? 'fecha de pago' : 'fecha de entrada'; + return 'Inicio'; } + let lastTrigger = 0; + // Obtener automáticamente el tipo de cambio cuando cambie la fecha efectiva $effect(() => { const effectiveDate = getEffectiveExchangeDate(); const companyId = companyStore.activeCompany?.id; + + // Acceder a refreshTrigger para reactividad + const currentTrigger = refreshTrigger; // Solo ejecutar si los valores clave cambiaron if ( formData && effectiveDate && companyId && - (effectiveDate !== lastFetchedDate || companyId !== lastCompanyId) + (effectiveDate !== lastFetchedDate || companyId !== lastCompanyId || currentTrigger !== lastTrigger) ) { lastFetchedDate = effectiveDate; lastCompanyId = companyId; + lastTrigger = currentTrigger; + + console.log(`[TIPO CAMBIO] Obteniendo para ${getEffectiveDateLabel()}:`, effectiveDate); getExchangeRateByDate(effectiveDate, companyId) .then((usdRate) => { if (usdRate && formData) { + console.log('[TIPO CAMBIO] Encontrado:', usdRate.value); formData.exchange_rate = usdRate.value; } else { console.warn('⚠️ [TIPO CAMBIO] No encontrado para fecha:', effectiveDate); + if (formData) { + formData.exchange_rate = null; + // Mostrar el diálogo automáticamente si no existe el tipo de cambio + missingExchangeRateDate = effectiveDate; + showExchangeRateDialog = true; + } } }) .catch((err) => { console.error('❌ [TIPO CAMBIO] Error:', err); + if (formData) { + formData.exchange_rate = null; + } }); } }); @@ -610,9 +628,7 @@ class="cursor-not-allowed bg-muted" />
- Tipo de fecha para TC: {getEffectiveDateLabel() === 'fecha de pago'
- ? 'FECHA PAGO'
- : 'FECHA ENTRADA'}
+ Tipo de fecha para TC: FECHA {getEffectiveDateLabel().toUpperCase()}
{#if formData.pedimento_type === 'consolidated' && getEffectiveExchangeDate() > getCurrentLocalDate()}
(Opcional por fecha futura en consolidado)
@@ -997,6 +1013,12 @@
{#if activeSection === 'fechas'}