feat: update pedimento dates model, add PATCH method to API, and enhance edit forms

This commit is contained in:
2025-12-05 17:27:19 -06:00
parent 6eae26acf3
commit 0cb89ceeec
9 changed files with 310 additions and 277 deletions

View File

@@ -31,7 +31,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: Optional[datetime] = Field(None, description="End date")
class PedimentoDatesUpdate(BaseModel):

View File

@@ -49,7 +49,7 @@ class PedimentoDates(Base, TenantScopedMixin, TimestampMixin):
pedimento_id: Mapped[int] = mapped_column(Integer, nullable=False)
entry_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
pedimento_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)
extraction_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
@@ -58,7 +58,7 @@ class PedimentoDates(Base, TenantScopedMixin, TimestampMixin):
original_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
start_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
end_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
capture_date: Mapped[datetime] = mapped_column(DateTime)
capture_time: Mapped[datetime_time] = mapped_column(Time)
pedimento: Mapped["Pedimentos"] = relationship(

View File

@@ -106,7 +106,7 @@ class Pedimentos(Base, TenantScopedMixin, TimestampMixin):
client_id: Mapped[int] = mapped_column(Integer)
operation_type: Mapped[int] = mapped_column(Integer)
pedimento_type: Mapped[int] = mapped_column(Integer)
pedimento_code = mapped_column(String(2))
pedimento_code: Mapped[str] = mapped_column(String(2))
regime: Mapped[str] = mapped_column(String(3))
status: Mapped[str] = mapped_column(String(30))
usd_value: Mapped[Optional[Decimal]] = mapped_column(Numeric(17, 6))

View File

@@ -8,6 +8,7 @@ from typing import Any, Dict, List, Optional
from sqlalchemy import desc
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.orm import selectinload
from datetime import datetime
from ..dtos.pedimentos import PedimentosCreate, PedimentosUpdate
@@ -75,18 +76,20 @@ class PedimentosService:
Returns:
Tuple of (list of pedimentos, total count)
"""
query = db.query(Pedimentos).filter(Pedimentos.tenant_id == tenant_id, Pedimentos.company_id == company_id)
query = db.query(Pedimentos).filter(
Pedimentos.tenant_id == tenant_id, Pedimentos.company_id == company_id)
if filters:
if filters.get("status"):
query = query.filter(Pedimentos.status == filters["status"])
if filters.get("client_id"):
query = query.filter(Pedimentos.client_id == filters["client_id"])
query = query.filter(
Pedimentos.client_id == filters["client_id"])
if filters.get("year"):
query = query.filter(Pedimentos.year == filters["year"])
total = query.count()
# Eager load all relationships for the response schema
items = (
query.options(
@@ -134,10 +137,10 @@ class PedimentosService:
query = db.query(Pedimentos).filter(
Pedimentos.id == pedimento_id, Pedimentos.tenant_id == tenant_id, Pedimentos.company_id == company_id
)
if company_id is not None:
query = query.filter(Pedimentos.company_id == company_id)
# Eager load all relationships for the response schema
query = query.options(
selectinload(Pedimentos.pedimento_dates),
@@ -157,7 +160,7 @@ class PedimentosService:
selectinload(Pedimentos.pedimento_config_update_rectification),
selectinload(Pedimentos.pedimento_config_updates),
)
return query.first()
@staticmethod
@@ -196,7 +199,7 @@ class PedimentosService:
'pedimento_config_update_rectification': pedimento_data.pedimento_config_update_rectification,
'pedimento_config_updates': pedimento_data.pedimento_config_updates,
}
# Crear pedimento principal (excluyendo relaciones)
pedimento_dict = pedimento_data.model_dump(exclude={
'pedimento_dates', 'pedimento_decrementables', 'pedimento_incrementables',
@@ -207,7 +210,7 @@ class PedimentosService:
'pedimento_config_parameters', 'pedimento_config_surcharges',
'pedimento_config_update_rectification', 'pedimento_config_updates'
})
pedimento = Pedimentos(**pedimento_dict)
pedimento.tenant_id = tenant_id
pedimento.company_id = company_id
@@ -216,36 +219,59 @@ class PedimentosService:
db.flush() # Flush para obtener el ID sin commit
# Helper function para crear objetos relacionados
def create_related(model_class, data):
if data:
obj_dict = data.model_dump()
def create_related(model_class, data, extra_fields=None):
if data or extra_fields:
# Inicializar obj_dict desde data si existe, sino como dict vacío
obj_dict = data.model_dump() if data else {}
# Agregar campos extra si se proporcionan
if extra_fields:
obj_dict.update(extra_fields)
obj = model_class(**obj_dict)
obj.pedimento_id = pedimento.id
obj.tenant_id = tenant_id
obj.company_id = company_id
db.add(obj)
db.add(obj)
create_related(PedimentoDates, related_data['pedimento_dates'])
create_related(PedimentoDecrementables, related_data['pedimento_decrementables'])
create_related(PedimentoIncrementables, related_data['pedimento_incrementables'])
# Crear PedimentoDates con capture_time automático
create_related(
PedimentoDates,
related_data['pedimento_dates'],
extra_fields={'capture_time': datetime.now().time()}
)
create_related(PedimentoDecrementables,
related_data['pedimento_decrementables'])
create_related(PedimentoIncrementables,
related_data['pedimento_incrementables'])
create_related(PedimentoIndexes, related_data['pedimento_indexes'])
create_related(PedimentoValidation, related_data['pedimento_validation'])
create_related(PedimentoCustomsOffices, related_data['pedimento_customs_offices'])
create_related(PedimentoPayments, related_data['pedimento_payments'])
create_related(PedimentoRectificationDestination, related_data['pedimento_rectification_destination'])
create_related(PedimentoRectificationOrigin, related_data['pedimento_rectification_origin'])
create_related(PedimentoTransportMeans, related_data['pedimento_transport_means'])
create_related(PedimentoConfigAdditional, related_data['pedimento_config_additional'])
create_related(PedimentoConfigCalculations, related_data['pedimento_config_calculations'])
create_related(PedimentoConfigParameters, related_data['pedimento_config_parameters'])
create_related(PedimentoConfigSurcharges, related_data['pedimento_config_surcharges'])
create_related(PedimentoConfigUpdateRectification, related_data['pedimento_config_update_rectification'])
create_related(PedimentoConfigUpdates, related_data['pedimento_config_updates'])
create_related(PedimentoValidation,
related_data['pedimento_validation'])
create_related(PedimentoCustomsOffices,
related_data['pedimento_customs_offices'])
create_related(PedimentoPayments,
related_data['pedimento_payments'])
create_related(PedimentoRectificationDestination,
related_data['pedimento_rectification_destination'])
create_related(PedimentoRectificationOrigin,
related_data['pedimento_rectification_origin'])
create_related(PedimentoTransportMeans,
related_data['pedimento_transport_means'])
create_related(PedimentoConfigAdditional,
related_data['pedimento_config_additional'])
create_related(PedimentoConfigCalculations,
related_data['pedimento_config_calculations'])
create_related(PedimentoConfigParameters,
related_data['pedimento_config_parameters'])
create_related(PedimentoConfigSurcharges,
related_data['pedimento_config_surcharges'])
create_related(PedimentoConfigUpdateRectification,
related_data['pedimento_config_update_rectification'])
create_related(PedimentoConfigUpdates,
related_data['pedimento_config_updates'])
db.commit()
db.refresh(pedimento)
return pedimento
except Exception as e:
db.rollback()
logger.error(f"Error creating pedimento with related data: {e}")
@@ -268,7 +294,8 @@ class PedimentosService:
Returns:
Updated pedimento or None if not found
"""
pedimento = PedimentosService.get_by_id(db, pedimento_id, tenant_id, company_id)
pedimento = PedimentosService.get_by_id(
db, pedimento_id, tenant_id, company_id)
if not pedimento:
return None
@@ -283,7 +310,7 @@ class PedimentosService:
'pedimento_config_parameters', 'pedimento_config_surcharges',
'pedimento_config_update_rectification', 'pedimento_config_updates'
})
for field, value in update_data.items():
setattr(pedimento, field, value)
@@ -293,15 +320,16 @@ class PedimentosService:
def update_or_create_related(service_class, model_class, data_attr):
# Obtener datos del payload completo (no solo exclude_unset)
full_data = pedimento_data.model_dump()
if data_attr not in full_data:
return
data = full_data[data_attr]
if not data:
return
existing = service_class.get_by_pedimento_id(db, pedimento_id, tenant_id)
existing = service_class.get_by_pedimento_id(
db, pedimento_id, tenant_id)
if existing:
# Actualizar existente
for field, value in data.items():
@@ -313,30 +341,46 @@ class PedimentosService:
obj.pedimento_id = pedimento_id
obj.tenant_id = tenant_id
obj.company_id = company_id
db.add(obj)
db.add(obj)
# Actualizar o crear tablas relacionadas
update_or_create_related(PedimentoDatesService, PedimentoDates, 'pedimento_dates')
update_or_create_related(PedimentoDecrementablesService, PedimentoDecrementables, 'pedimento_decrementables')
update_or_create_related(PedimentoIncrementablesService, PedimentoIncrementables, 'pedimento_incrementables')
update_or_create_related(PedimentoIndexesService, PedimentoIndexes, 'pedimento_indexes')
update_or_create_related(PedimentoValidationService, PedimentoValidation, 'pedimento_validation')
update_or_create_related(PedimentoCustomsOfficesService, PedimentoCustomsOffices, 'pedimento_customs_offices')
update_or_create_related(PedimentoPaymentsService, PedimentoPayments, 'pedimento_payments')
update_or_create_related(PedimentoRectificationDestinationService, PedimentoRectificationDestination, 'pedimento_rectification_destination')
update_or_create_related(PedimentoRectificationOriginService, PedimentoRectificationOrigin, 'pedimento_rectification_origin')
update_or_create_related(PedimentoTransportMeansService, PedimentoTransportMeans, 'pedimento_transport_means')
update_or_create_related(PedimentoConfigAdditionalService, PedimentoConfigAdditional, 'pedimento_config_additional')
update_or_create_related(PedimentoConfigCalculationsService, PedimentoConfigCalculations, 'pedimento_config_calculations')
update_or_create_related(PedimentoConfigParametersService, PedimentoConfigParameters, 'pedimento_config_parameters')
update_or_create_related(PedimentoConfigSurchargesService, PedimentoConfigSurcharges, 'pedimento_config_surcharges')
update_or_create_related(PedimentoConfigUpdateRectificationService, PedimentoConfigUpdateRectification, 'pedimento_config_update_rectification')
update_or_create_related(PedimentoConfigUpdatesService, PedimentoConfigUpdates, 'pedimento_config_updates')
update_or_create_related(
PedimentoDatesService, PedimentoDates, 'pedimento_dates')
update_or_create_related(
PedimentoDecrementablesService, PedimentoDecrementables, 'pedimento_decrementables')
update_or_create_related(
PedimentoIncrementablesService, PedimentoIncrementables, 'pedimento_incrementables')
update_or_create_related(
PedimentoIndexesService, PedimentoIndexes, 'pedimento_indexes')
update_or_create_related(
PedimentoValidationService, PedimentoValidation, 'pedimento_validation')
update_or_create_related(
PedimentoCustomsOfficesService, PedimentoCustomsOffices, 'pedimento_customs_offices')
update_or_create_related(
PedimentoPaymentsService, PedimentoPayments, 'pedimento_payments')
update_or_create_related(PedimentoRectificationDestinationService,
PedimentoRectificationDestination, 'pedimento_rectification_destination')
update_or_create_related(PedimentoRectificationOriginService,
PedimentoRectificationOrigin, 'pedimento_rectification_origin')
update_or_create_related(
PedimentoTransportMeansService, PedimentoTransportMeans, 'pedimento_transport_means')
update_or_create_related(PedimentoConfigAdditionalService,
PedimentoConfigAdditional, 'pedimento_config_additional')
update_or_create_related(PedimentoConfigCalculationsService,
PedimentoConfigCalculations, 'pedimento_config_calculations')
update_or_create_related(PedimentoConfigParametersService,
PedimentoConfigParameters, 'pedimento_config_parameters')
update_or_create_related(PedimentoConfigSurchargesService,
PedimentoConfigSurcharges, 'pedimento_config_surcharges')
update_or_create_related(PedimentoConfigUpdateRectificationService,
PedimentoConfigUpdateRectification, 'pedimento_config_update_rectification')
update_or_create_related(
PedimentoConfigUpdatesService, PedimentoConfigUpdates, 'pedimento_config_updates')
db.commit()
db.refresh(pedimento)
return pedimento
except Exception as e:
db.rollback()
logger.error(f"Error updating pedimento with related data: {e}")
@@ -356,7 +400,8 @@ class PedimentosService:
Returns:
True if deleted, False if not found
"""
pedimento = PedimentosService.get_by_id(db, pedimento_id, tenant_id, company_id)
pedimento = PedimentosService.get_by_id(
db, pedimento_id, tenant_id, company_id)
if not pedimento:
return False