diff --git a/backend/api/v1/modules/a76/pedmientos/dtos/pedimento_dates.py b/backend/api/v1/modules/a76/pedmientos/dtos/pedimento_dates.py index a5bf4118..cb6fcbe7 100644 --- a/backend/api/v1/modules/a76/pedmientos/dtos/pedimento_dates.py +++ b/backend/api/v1/modules/a76/pedmientos/dtos/pedimento_dates.py @@ -7,7 +7,7 @@ from pydantic import BaseModel, ConfigDict, Field class PedimentoDatesBase(BaseModel): """Base schema for Pedimento Dates""" - entry_date: Optional[datetime] = Field(None, description="Entry date") + entry_date: datetime = Field(..., description="Entry date") pedimento_date: Optional[datetime] = Field(None, description="Pedimento date") payment_date: datetime = Field(..., description="Payment date") rectification_payment_date: Optional[datetime] = Field( @@ -18,7 +18,7 @@ class PedimentoDatesBase(BaseModel): eucan_date: Optional[datetime] = Field(None, description="EUCAN date") original_date: Optional[datetime] = Field(None, description="Original date") start_date: Optional[datetime] = Field(None, description="Start date") - end_date: Optional[datetime] = Field(None, description="End date") + end_date: datetime = Field(..., description="End date") class PedimentoDatesCreate(BaseModel): @@ -33,7 +33,7 @@ class PedimentoDatesCreate(BaseModel): eucan_date: Optional[datetime] = Field(None, description="EUCAN date") original_date: Optional[datetime] = Field(None, description="Original date") start_date: Optional[datetime] = Field(None, description="Start date") - end_date: Optional[datetime] = Field(None, description="End date") + end_date: datetime = Field(..., description="End date") class PedimentoDatesUpdate(BaseModel): diff --git a/backend/api/v1/modules/a76/pedmientos/models/pedimento_dates.py b/backend/api/v1/modules/a76/pedmientos/models/pedimento_dates.py index fe8a5d0a..b69a5297 100644 --- a/backend/api/v1/modules/a76/pedmientos/models/pedimento_dates.py +++ b/backend/api/v1/modules/a76/pedmientos/models/pedimento_dates.py @@ -42,7 +42,7 @@ class PedimentoDates(Base, TenantScopedMixin, TimestampMixin): id: Mapped[int] = mapped_column(Integer) pedimento_id: Mapped[int] = mapped_column(Integer, nullable=False) - entry_date: Mapped[Optional[datetime]] = mapped_column(DateTime) + entry_date: Mapped[datetime] = mapped_column(DateTime) pedimento_date: Mapped[Optional[datetime]] = mapped_column(DateTime) payment_date: Mapped[datetime] = mapped_column(DateTime) rectification_payment_date: Mapped[Optional[datetime]] = mapped_column(DateTime) @@ -51,7 +51,7 @@ class PedimentoDates(Base, TenantScopedMixin, TimestampMixin): eucan_date: Mapped[Optional[datetime]] = mapped_column(DateTime) original_date: Mapped[Optional[datetime]] = mapped_column(DateTime) start_date: Mapped[Optional[datetime]] = mapped_column(DateTime) - end_date: Mapped[Optional[datetime]] = mapped_column(DateTime) + end_date: Mapped[datetime] = mapped_column(DateTime) capture_time: Mapped[datetime_time] = mapped_column(Time) 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 3bcb3573..9d5576dc 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 @@ -13,6 +13,14 @@ import type { ClientProvider } from '$lib/api/dashboard/a76/clients-providers'; import type { CodePedimentoRegimen } from '$lib/api/dashboard/refrence_data/code_pedimento_regimens'; import IdentificadoresTabForm from './identifiers-tab-form.svelte'; + import { Calendar, Clock } from 'lucide-svelte'; + import { + loadServerDate, + addDaysLocal, + getCurrentLocalYear, + getCurrentLocalDate, + getCurrentLocalTime + } from '$lib/date-utils'; let { pedimento, @@ -204,40 +212,44 @@ } }); - // Obtener el año actual (últimos 2 dígitos) - const now = new Date(); - const currentYear = String(now.getFullYear()).slice(-2); - const currentDate = now.toISOString().substring(0, 10); // YYYY-MM-DD - const currentTime = now.toTimeString().substring(0, 5); // HH:MM + // --- Inicialización --- + const currentYear = getCurrentLocalYear(); + const currentDate = getCurrentLocalDate(); + const currentTime = getCurrentLocalTime(); + + const end_date = addDaysLocal(currentDate, 4); + const payment_date = addDaysLocal(end_date, 2); + // Inicializar formData con los valores del pedimento (o vacío si es null) if (!formData) { const datesData = pedimento?.pedimento_dates; formData = { - year: pedimento?.year || currentYear, - customs_office: pedimento?.customs_office || '', - license: pedimento?.license || '', - pedimento_number: pedimento?.pedimento_number || '', - client_id: pedimento?.client_id ?? null, - operation_type: pedimento?.operation_type ?? null, - pedimento_type: pedimento?.pedimento_type || '', - pedimento_code: pedimento?.pedimento_code || '', - regime: pedimento?.regime || '', - status: pedimento?.status || '', - usd_value: pedimento?.usd_value ?? null, - paid_price: pedimento?.paid_price ?? null, - gross_weight: pedimento?.gross_weight ?? null, + year: pedimento?.year || currentYear, + customs_office: pedimento?.customs_office || '', + license: pedimento?.license || '', + pedimento_number: pedimento?.pedimento_number || '', + client_id: pedimento?.client_id ?? null, + operation_type: pedimento?.operation_type ?? null, + pedimento_type: pedimento?.pedimento_type || 'consolidated', + pedimento_code: pedimento?.pedimento_code || '', + regime: pedimento?.regime || '', + status: pedimento?.status || 'MODIFICABLE', + usd_value: pedimento?.usd_value ?? null, + paid_price: pedimento?.paid_price ?? null, + gross_weight: pedimento?.gross_weight ?? null, exchange_rate: pedimento?.exchange_rate ?? null, - // Date fields - entry_date: datesData?.entry_date ? datesData.entry_date.substring(0, 10) : '', - pedimento_date: datesData?.pedimento_date ? datesData.pedimento_date.substring(0, 10) : '', - extraction_date: datesData?.extraction_date ? datesData.extraction_date.substring(0, 10) : '', - rectification_payment_date: datesData?.rectification_payment_date ? datesData.rectification_payment_date.substring(0, 10) : '', - original_date: datesData?.original_date ? datesData.original_date.substring(0, 10) : '', - payment_date: datesData?.payment_date ? datesData.payment_date.substring(0, 10) : '', - // Campos de captura automática - fecha_captura: currentDate, - hora_captura: currentTime + // Date fields - Cargamos convirtiendo a local para el usuario + entry_date: loadServerDate(datesData?.entry_date) || currentDate, + end_date: loadServerDate(datesData?.end_date) || end_date, + payment_date: loadServerDate(datesData?.payment_date) || payment_date, + pedimento_date: loadServerDate(datesData?.pedimento_date), + extraction_date: loadServerDate(datesData?.extraction_date), + rectification_payment_date: loadServerDate(datesData?.rectification_payment_date), + original_date: loadServerDate(datesData?.original_date), + // Campos de captura automática (Local) + fecha_captura: currentDate, + hora_captura: currentTime }; } @@ -265,24 +277,6 @@ } }); - const statusOptions = [ - { value: 'MODIFICABLE', label: 'Modificable' }, - { value: 'ESPERA FIRMA PREVIO', label: 'Espera de firma previo' }, - { value: 'ESPERA VALIDACION', label: 'Espera validacion' }, - { value: 'VALIDADO', label: 'Validado' }, - { value: 'ESPERA BORRAR FIRMA PREVIO', label: 'Espera borrar firma previo' }, - { value: 'ESPERA BORRAR FIRMA VALIDACION', label: 'Espera borrar firma validacion ' }, - { value: 'ESPERA PAGO', label: 'Espera pago' }, - { value: 'CON FIRMA DE PREVIO', label: 'Con firma de previo' }, - { value: 'F DE PREVIO BORRADA', label: 'Forma de previo borrada' }, - { value: 'F VALIDACION BORRADA', label: 'Forma validacion borrada' }, - { value: 'PAGADO', label: 'Pagado' }, - { value: 'DESISTIO', label: 'Desistio' }, - { value: 'ESPERA CARTA CUPO', label: 'Espera carta cupo' }, - { value: 'CARTA CUPO', label: 'Carta cupo' }, - { value: 'ESPERA CANCELAR CARTA CUPO', label: 'Espera cancelar carta cupo' } - ]; - const pedimentoTypeOptions = [ { value: 'automobile', label: 'Automóvil' }, { value: 'complementary', label: 'Complementario' }, @@ -301,10 +295,27 @@ - Información General - - Edita los datos principales del pedimento - +
+
+ Información General + + Edita los datos principales del pedimento + +
+ + +
+
+ + {formData.fecha_captura} +
+
+
+ + {formData.hora_captura} +
+
+
@@ -457,7 +468,7 @@
- + -
- -
- - formData.status = v ?? ''} - > - - {statusOptions.find(o => o.value === formData.status)?.label || 'Seleccionar...'} - - - {#each statusOptions as option} - - {/each} - - -
-
@@ -654,33 +645,7 @@ bind:value={formData.es} placeholder="0" /> -
- - -
- - -
- - -
- - -
+
@@ -751,6 +716,28 @@ /> + + {#if formData.pedimento_type === 'consolidated'} +
+ + +
+ {/if} + + +
+ + +
+
@@ -771,15 +758,17 @@ />
- -
- - -
+ {#if formData.pedimento_code === 'R1'} + +
+ + +
+ {/if}
@@ -789,17 +778,7 @@ type="date" bind:value={formData.original_date} /> -
- - -
- - -
+ {:else if activeSection === 'incrementables'} diff --git a/frontend/src/lib/date-utils.ts b/frontend/src/lib/date-utils.ts new file mode 100644 index 00000000..20991a30 --- /dev/null +++ b/frontend/src/lib/date-utils.ts @@ -0,0 +1,75 @@ +import { + getLocalTimeZone, + parseDate, + parseTime, + toCalendarDateTime, + fromDate, + now, + today +} from '@internationalized/date'; + +const localTimeZone = getLocalTimeZone(); + +/** + * Convierte fecha local (string input YYYY-MM-DD) + hora (HH:MM) a UTC ISO string. + * Útil para enviar datos al backend que espera timestamps absolutos. + */ +export function prepareDateForBackend(dateStr: string, timeStr: string = '00:00'): string | null { + if (!dateStr) return null; + try { + const date = parseDate(dateStr); + const time = parseTime(timeStr); + // Combinar fecha y hora usando la zona horaria local + const dateTime = toCalendarDateTime(date, time); + const zonedDateTime = dateTime.toDate(localTimeZone); + return zonedDateTime.toISOString(); + } catch (e) { + console.error('Error parsing date:', e); + return null; + } +} + +/** + * Convertir fechas que vienen del servidor (ISO UTC) a fecha local del calendario (YYYY-MM-DD string). + * Útil para poblar inputs type="date". + */ +export function loadServerDate(isoStr: string | null | undefined): string { + if (!isoStr) return ''; + try { + // Si incluye T es ISO completo, convertir a zona local + if (isoStr.includes('T')) { + const dateObj = new Date(isoStr); + return fromDate(dateObj, localTimeZone).toString(); + } + // Si no, asumir fecha plana YYYY-MM-DD + return isoStr.substring(0, 10); + } catch { + return isoStr || ''; + } +} + +/** + * Sumar días de forma segura con calendario y devolver string YYYY-MM-DD. + */ +export function addDaysLocal(dateStr: string, days: number): string { + if (!dateStr) return ''; + try { + return parseDate(dateStr).add({ days }).toString(); + } catch { + return ''; + } +} + +export function getCurrentLocalYear(): string { + const nowZoned = now(localTimeZone); + return String(nowZoned.year).slice(-2); +} + +export function getCurrentLocalDate(): string { + return today(localTimeZone).toString(); +} + +export function getCurrentLocalTime(): string { + const nowZoned = now(localTimeZone); + return `${String(nowZoned.hour).padStart(2, '0')}:${String(nowZoned.minute).padStart(2, '0')}`; +} diff --git a/frontend/src/routes/dashboard/pedimentos/edit/[id]/+page.svelte b/frontend/src/routes/dashboard/pedimentos/edit/[id]/+page.svelte index b1a168f2..570e619c 100644 --- a/frontend/src/routes/dashboard/pedimentos/edit/[id]/+page.svelte +++ b/frontend/src/routes/dashboard/pedimentos/edit/[id]/+page.svelte @@ -230,7 +230,7 @@ if (generalFormData) { const hasDatesValue = generalFormData.entry_date || generalFormData.pedimento_date || generalFormData.extraction_date || generalFormData.rectification_payment_date || - generalFormData.original_date || generalFormData.payment_date; + generalFormData.original_date || generalFormData.payment_date || generalFormData.end_date; if (hasDatesValue) { payload.pedimento_dates = { entry_date: generalFormData.entry_date || null, @@ -238,7 +238,8 @@ payment_date: generalFormData.payment_date || null, rectification_payment_date: generalFormData.rectification_payment_date || null, extraction_date: generalFormData.extraction_date || null, - original_date: generalFormData.original_date || null + original_date: generalFormData.original_date || null, + end_date: generalFormData.end_date || null }; } }