feat: Enhance Pedimentos CRUD operations with related data handling
- Updated PedimentosService to create and update related tables for Pedimentos. - Added company_id filtering in queries for Pedimentos. - Improved error handling and logging during creation and update processes. - Refactored router tags for consistency and clarity. - Adjusted API endpoints for customs brokers to include company_id in requests. - Enhanced company selection logic in various components to prioritize active company. - Implemented event listeners for company changes to reload data dynamically. - Updated frontend components to handle loading states and errors more effectively. - Ensured all relevant routes and API calls are aligned with the new company context.
This commit is contained in:
@@ -3,24 +3,65 @@ from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class CustomsBrokerDTO(BaseModel):
|
||||
type: Optional[str]
|
||||
class CustomsBrokerBaseDTO(BaseModel):
|
||||
"""Base fields for CustomsBroker"""
|
||||
type: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
address: Optional[str] = None
|
||||
postal_code: Optional[str] = None
|
||||
city: Optional[str] = None
|
||||
state: Optional[str] = None
|
||||
phone: Optional[str] = None
|
||||
fax: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
country: Optional[str] = None
|
||||
tax_id: Optional[str] = None
|
||||
personal_id: Optional[str] = None
|
||||
position: Optional[str] = None
|
||||
license: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
contact: Optional[str] = None
|
||||
|
||||
|
||||
class CustomsBrokerCreateDTO(CustomsBrokerBaseDTO):
|
||||
"""Schema for creating a new CustomsBroker"""
|
||||
broker_key: str
|
||||
name: Optional[str]
|
||||
address: Optional[str]
|
||||
postal_code: Optional[str]
|
||||
city: Optional[str]
|
||||
state: Optional[str]
|
||||
phone: Optional[str]
|
||||
fax: Optional[str]
|
||||
email: Optional[str]
|
||||
country: Optional[str]
|
||||
tax_id: Optional[str]
|
||||
personal_id: Optional[str]
|
||||
position: Optional[str]
|
||||
license: Optional[str]
|
||||
company: Optional[str]
|
||||
contact: Optional[str]
|
||||
|
||||
|
||||
class CustomsBrokerUpdateDTO(CustomsBrokerBaseDTO):
|
||||
"""Schema for updating an existing CustomsBroker"""
|
||||
pass
|
||||
|
||||
|
||||
class CustomsBrokerResponseDTO(CustomsBrokerBaseDTO):
|
||||
"""Schema for CustomsBroker response"""
|
||||
broker_key: str
|
||||
tenant_id: int
|
||||
company_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
# Legacy DTO for backwards compatibility (if needed elsewhere)
|
||||
class CustomsBrokerDTO(BaseModel):
|
||||
type: Optional[str] = None
|
||||
broker_key: str
|
||||
name: Optional[str] = None
|
||||
address: Optional[str] = None
|
||||
postal_code: Optional[str] = None
|
||||
city: Optional[str] = None
|
||||
state: Optional[str] = None
|
||||
phone: Optional[str] = None
|
||||
fax: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
country: Optional[str] = None
|
||||
tax_id: Optional[str] = None
|
||||
personal_id: Optional[str] = None
|
||||
position: Optional[str] = None
|
||||
license: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
contact: Optional[str] = None
|
||||
tenant_id: str
|
||||
company_id: str
|
||||
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
from api.v1.common.base_models import TenantScopedMixin
|
||||
from core.database import Base
|
||||
from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint, Integer, String
|
||||
from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint, Integer, String, UniqueConstraint, PrimaryKeyConstraint
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
|
||||
class CustomsBroker(Base, TenantScopedMixin):
|
||||
__tablename__ = "customs_brokers"
|
||||
__table_args__ = (
|
||||
PrimaryKeyConstraint("id", name="customs_brokers_pkey"),
|
||||
ForeignKeyConstraint(
|
||||
["tenant_id"], ["a76.tenants.id"], name="fk_customs_brokers_tenants"
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["company_id"], ["a76.company.id"], name="fk_customs_brokers_company"
|
||||
),
|
||||
UniqueConstraint("broker_key", "tenant_id", "company_id", name="uq_broker_key_tenant_company"),
|
||||
{"schema": "a76"},
|
||||
)
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
type = Column(String(9), nullable=True)
|
||||
broker_key = Column(String(5), primary_key=True, nullable=False)
|
||||
broker_key = Column(String(5), nullable=False)
|
||||
name = Column(String(80), nullable=True)
|
||||
address = Column(String(1500), nullable=True)
|
||||
postal_code = Column(String(15), nullable=True)
|
||||
@@ -44,10 +47,11 @@ class CustomsBroker(Base, TenantScopedMixin):
|
||||
|
||||
class CustomsBrokerVU(Base):
|
||||
__tablename__ = "customs_brokers_vu"
|
||||
__table_args__ = {"schema": "a76"}
|
||||
|
||||
broker_key = Column(
|
||||
String(5),
|
||||
ForeignKey("a76.customs_brokers.broker_key", ondelete="CASCADE"),
|
||||
customs_broker_id = Column(
|
||||
Integer,
|
||||
ForeignKey("a76.customs_brokers.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
)
|
||||
certificate_path = Column(String(1499), nullable=True)
|
||||
@@ -75,10 +79,11 @@ class CustomsBrokerVU(Base):
|
||||
|
||||
class CustomsBrokerPersonnel(Base):
|
||||
__tablename__ = "customs_brokers_personnel"
|
||||
__table_args__ = {"schema": "a76"}
|
||||
|
||||
broker_key = Column(
|
||||
String(5),
|
||||
ForeignKey("a76.customs_brokers.broker_key", ondelete="CASCADE"),
|
||||
customs_broker_id = Column(
|
||||
Integer,
|
||||
ForeignKey("a76.customs_brokers.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
)
|
||||
line = Column(Integer, primary_key=True, nullable=False)
|
||||
|
||||
@@ -1,43 +1,54 @@
|
||||
from typing import Dict, Any
|
||||
from api.v1.common.tenant_crud_routes import TenantCRUDRoutes
|
||||
from core.database import get_core_db
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.security import get_current_user, validate_access_to_resource
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from . import dto, services
|
||||
|
||||
# Create main router
|
||||
router = APIRouter()
|
||||
|
||||
# Create CRUD routes for CustomsBroker using TenantCRUDRoutes
|
||||
customs_broker_crud = TenantCRUDRoutes(
|
||||
service=services.CustomsBrokerService,
|
||||
create_schema=dto.CustomsBrokerCreateDTO,
|
||||
update_schema=dto.CustomsBrokerUpdateDTO,
|
||||
response_schema=dto.CustomsBrokerResponseDTO,
|
||||
prefix="/customs-brokers", # No prefix since it's already in the parent router
|
||||
tags=[],
|
||||
resource_name="Customs Broker",
|
||||
id_name="broker_key",
|
||||
id_type=str,
|
||||
enable_list=True, # Enable list endpoint with pagination
|
||||
)
|
||||
|
||||
@router.get("/customs-broker/{broker_key}", response_model=dto.CustomsBrokerDTO)
|
||||
def get_customs_broker(broker_key: str, db: Session = Depends(get_core_db)):
|
||||
broker = services.CustomsBrokerService.get_by_broker_key(db, broker_key)
|
||||
if not broker:
|
||||
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
||||
return broker
|
||||
# Include the CRUD routes
|
||||
router.include_router(customs_broker_crud.router)
|
||||
|
||||
|
||||
@router.post("/customs-broker", response_model=dto.CustomsBrokerDTO)
|
||||
def create_customs_broker(
|
||||
broker_data: dto.CustomsBrokerDTO, db: Session = Depends(get_core_db)
|
||||
):
|
||||
return services.CustomsBrokerService.create_customs_broker(db, broker_data)
|
||||
|
||||
|
||||
@router.delete("/customs-broker/{broker_key}", response_model=dto.CustomsBrokerDTO)
|
||||
def delete_customs_broker(broker_key: str, db: Session = Depends(get_core_db)):
|
||||
broker = services.CustomsBrokerService.delete_customs_broker(db, broker_key)
|
||||
if not broker:
|
||||
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
||||
return broker
|
||||
|
||||
# Additional routes for child resources (CustomsBrokerVU and CustomsBrokerPersonnel)
|
||||
# These remain as manual routes since they have different patterns
|
||||
|
||||
@router.put(
|
||||
"/customs-broker-vu/{broker_key}", response_model=dto.CustomsBrokerVUCreateDTO
|
||||
"/customs-broker-vu/{broker_key}",
|
||||
response_model=dto.CustomsBrokerVUCreateDTO,
|
||||
)
|
||||
def update_customs_broker_vu(
|
||||
broker_key: str,
|
||||
vu_data: dto.CustomsBrokerVUCreateDTO,
|
||||
company_id: int = Query(..., description="Company ID"),
|
||||
db: Session = Depends(get_core_db),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user),
|
||||
):
|
||||
tenant_id = validate_access_to_resource(db, company_id, current_user)
|
||||
|
||||
# Verify the broker exists and belongs to the tenant/company
|
||||
broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
||||
if not broker:
|
||||
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
||||
|
||||
updated_vu = services.CustomsBrokerVUService.update_vu(db, broker_key, vu_data)
|
||||
if not updated_vu:
|
||||
raise HTTPException(status_code=404, detail="Customs Broker VU not found")
|
||||
@@ -52,8 +63,17 @@ def update_customs_broker_personnel(
|
||||
broker_key: str,
|
||||
line: int,
|
||||
personnel_data: dto.CustomsBrokerPersonnelDTO,
|
||||
company_id: int = Query(..., description="Company ID"),
|
||||
db: Session = Depends(get_core_db),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user),
|
||||
):
|
||||
tenant_id = validate_access_to_resource(db, company_id, current_user)
|
||||
|
||||
# Verify the broker exists and belongs to the tenant/company
|
||||
broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
||||
if not broker:
|
||||
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
||||
|
||||
updated_personnel = services.CustomsBrokerPersonnelService.update_personnel(
|
||||
db, broker_key, line, personnel_data
|
||||
)
|
||||
@@ -62,3 +82,4 @@ def update_customs_broker_personnel(
|
||||
status_code=404, detail="Customs Broker Personnel not found"
|
||||
)
|
||||
return updated_personnel
|
||||
|
||||
|
||||
@@ -5,28 +5,70 @@ from . import dto, models
|
||||
|
||||
class CustomsBrokerService:
|
||||
@staticmethod
|
||||
def get_by_broker_key(db: Session, broker_key: str):
|
||||
def get_by_id(db: Session, broker_key: str, tenant_id: int, company_id: int):
|
||||
"""Get a customs broker by broker_key with tenant/company validation"""
|
||||
return (
|
||||
db.query(models.CustomsBroker)
|
||||
.filter(models.CustomsBroker.broker_key == broker_key)
|
||||
.filter(
|
||||
models.CustomsBroker.broker_key == broker_key,
|
||||
models.CustomsBroker.tenant_id == tenant_id,
|
||||
models.CustomsBroker.company_id == company_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create_customs_broker(db: Session, broker_data: dto.CustomsBrokerDTO):
|
||||
new_broker = models.CustomsBroker(**broker_data.dict())
|
||||
def get_all(
|
||||
db: Session,
|
||||
tenant_id: int,
|
||||
company_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
filters: dict = None,
|
||||
):
|
||||
"""Get all customs brokers for a tenant/company with pagination"""
|
||||
query = db.query(models.CustomsBroker).filter(
|
||||
models.CustomsBroker.tenant_id == tenant_id,
|
||||
models.CustomsBroker.company_id == company_id,
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
items = query.offset(skip).limit(limit).all()
|
||||
return items, total
|
||||
|
||||
@staticmethod
|
||||
def create(db: Session, broker_data: dto.CustomsBrokerCreateDTO, tenant_id: int, company_id: int):
|
||||
"""Create a new customs broker"""
|
||||
broker_dict = broker_data.model_dump()
|
||||
broker_dict["tenant_id"] = tenant_id
|
||||
broker_dict["company_id"] = company_id
|
||||
|
||||
new_broker = models.CustomsBroker(**broker_dict)
|
||||
db.add(new_broker)
|
||||
db.commit()
|
||||
db.refresh(new_broker)
|
||||
return new_broker
|
||||
|
||||
@staticmethod
|
||||
def delete_customs_broker(db: Session, broker_key: str):
|
||||
broker = CustomsBrokerService.get_by_broker_key(db, broker_key)
|
||||
def update(db: Session, broker_key: str, tenant_id: int, broker_data: dto.CustomsBrokerUpdateDTO, company_id: int):
|
||||
"""Update an existing customs broker"""
|
||||
broker = CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
||||
if broker:
|
||||
for key, value in broker_data.model_dump(exclude_unset=True).items():
|
||||
setattr(broker, key, value)
|
||||
db.commit()
|
||||
db.refresh(broker)
|
||||
return broker
|
||||
|
||||
@staticmethod
|
||||
def delete(db: Session, broker_key: str, tenant_id: int, company_id: int):
|
||||
"""Delete a customs broker"""
|
||||
broker = CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
||||
if broker:
|
||||
db.delete(broker)
|
||||
db.commit()
|
||||
return broker
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class CustomsBrokerVUService:
|
||||
|
||||
@@ -5,6 +5,23 @@ from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from .pedimento_config_additional import PedimentoConfigAdditionalCreate
|
||||
from .pedimento_config_calculations import PedimentoConfigCalculationsCreate
|
||||
from .pedimento_config_parameters import PedimentoConfigParametersCreate
|
||||
from .pedimento_config_surcharges import PedimentoConfigSurchargesCreate
|
||||
from .pedimento_config_update_rectification import PedimentoConfigUpdateRectificationCreate
|
||||
from .pedimento_config_updates import PedimentoConfigUpdatesCreate
|
||||
from .pedimento_customs_offices import PedimentoCustomsOfficesCreate
|
||||
from .pedimento_dates import PedimentoDatesCreate
|
||||
from .pedimento_decrementables import PedimentoDecrementablesCreate
|
||||
from .pedimento_incrementables import PedimentoIncrementablesCreate
|
||||
from .pedimento_indexes import PedimentoIndexesCreate
|
||||
from .pedimento_payments import PedimentoPaymentsCreate
|
||||
from .pedimento_rectification_destination import PedimentoRectificationDestinationCreate
|
||||
from .pedimento_rectification_origin import PedimentoRectificationOriginCreate
|
||||
from .pedimento_transport_means import PedimentoTransportMeansCreate
|
||||
from .pedimento_validation import PedimentoValidationCreate
|
||||
|
||||
|
||||
class OperationType(IntEnum):
|
||||
EXPORTACION = 1
|
||||
@@ -35,6 +52,22 @@ class PedimentosBase(BaseModel):
|
||||
gross_weight: Optional[Decimal] = Field(None, description="Gross weight")
|
||||
exchange_rate: Optional[Decimal] = Field(None, description="Exchange rate")
|
||||
|
||||
pedimento_dates: Optional[PedimentoDatesCreate] = None
|
||||
pedimento_decrementables: Optional[PedimentoDecrementablesCreate] = None
|
||||
pedimento_incrementables: Optional[PedimentoIncrementablesCreate] = None
|
||||
pedimento_indexes: Optional[PedimentoIndexesCreate] = None
|
||||
pedimento_validation: Optional[PedimentoValidationCreate] = None
|
||||
pedimento_customs_offices: Optional[PedimentoCustomsOfficesCreate] = None
|
||||
pedimento_payments: Optional[PedimentoPaymentsCreate] = None
|
||||
pedimento_rectification_destination: Optional[PedimentoRectificationDestinationCreate] = None
|
||||
pedimento_rectification_origin: Optional[PedimentoRectificationOriginCreate] = None
|
||||
pedimento_transport_means: Optional[PedimentoTransportMeansCreate] = None
|
||||
pedimento_config_additional: Optional[PedimentoConfigAdditionalCreate] = None
|
||||
pedimento_config_calculations: Optional[PedimentoConfigCalculationsCreate] = None
|
||||
pedimento_config_parameters: Optional[PedimentoConfigParametersCreate] = None
|
||||
pedimento_config_surcharges: Optional[PedimentoConfigSurchargesCreate] = None
|
||||
pedimento_config_update_rectification: Optional[PedimentoConfigUpdateRectificationCreate] = None
|
||||
pedimento_config_updates: Optional[PedimentoConfigUpdatesCreate] = None
|
||||
|
||||
class PedimentosCreate(PedimentosBase):
|
||||
"""Schema for creating a new Pedimento"""
|
||||
@@ -48,8 +81,7 @@ class PedimentosCreate(PedimentosBase):
|
||||
operation_type: int = Field(..., description="Operation type")
|
||||
pedimento_type: int = Field(..., description="Pedimento type")
|
||||
regime: str = Field(..., max_length=3, description="Regime")
|
||||
status: str = Field(..., max_length=30, description="Status")
|
||||
|
||||
status: str = Field(..., max_length=30, description="Status")
|
||||
|
||||
class PedimentosUpdate(BaseModel):
|
||||
"""Schema for updating a Pedimento"""
|
||||
|
||||
@@ -2,13 +2,51 @@
|
||||
Service layer for Pedimentos CRUD operations
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from sqlalchemy import desc
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..dtos.pedimentos import PedimentosCreate, PedimentosUpdate
|
||||
|
||||
from .pedimento_config_additional import PedimentoConfigAdditionalService
|
||||
from .pedimento_config_calculations import PedimentoConfigCalculationsService
|
||||
from .pedimento_config_parameters import PedimentoConfigParametersService
|
||||
from .pedimento_config_surcharges import PedimentoConfigSurchargesService
|
||||
from .pedimento_config_update_rectification import PedimentoConfigUpdateRectificationService
|
||||
from .pedimento_config_updates import PedimentoConfigUpdatesService
|
||||
from .pedimento_customs_offices import PedimentoCustomsOfficesService
|
||||
from .pedimento_dates import PedimentoDatesService
|
||||
from .pedimento_decrementables import PedimentoDecrementablesService
|
||||
from .pedimento_incrementables import PedimentoIncrementablesService
|
||||
from .pedimento_indexes import PedimentoIndexesService
|
||||
from .pedimento_payments import PedimentoPaymentsService
|
||||
from .pedimento_rectification_destination import PedimentoRectificationDestinationService
|
||||
from .pedimento_rectification_origin import PedimentoRectificationOriginService
|
||||
from .pedimento_transport_means import PedimentoTransportMeansService
|
||||
from .pedimento_validation import PedimentoValidationService
|
||||
|
||||
# Crear tablas relacionadas si existen datos
|
||||
from ..models.pedimentos import Pedimentos
|
||||
from ..models.pedimento_dates import PedimentoDates
|
||||
from ..models.pedimento_decrementables import PedimentoDecrementables
|
||||
from ..models.pedimento_incrementables import PedimentoIncrementables
|
||||
from ..models.pedimento_indexes import PedimentoIndexes
|
||||
from ..models.pedimento_validation import PedimentoValidation
|
||||
from ..models.pedimento_customs_offices import PedimentoCustomsOffices
|
||||
from ..models.pedimento_payments import PedimentoPayments
|
||||
from ..models.pedimento_rectification_destination import PedimentoRectificationDestination
|
||||
from ..models.pedimento_rectification_origin import PedimentoRectificationOrigin
|
||||
from ..models.pedimento_transport_means import PedimentoTransportMeans
|
||||
from ..models.pedimento_config_additional import PedimentoConfigAdditional
|
||||
from ..models.pedimento_config_calculations import PedimentoConfigCalculations
|
||||
from ..models.pedimento_config_parameters import PedimentoConfigParameters
|
||||
from ..models.pedimento_config_surcharges import PedimentoConfigSurcharges
|
||||
from ..models.pedimento_config_update_rectification import PedimentoConfigUpdateRectification
|
||||
from ..models.pedimento_config_updates import PedimentoConfigUpdates
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PedimentosService:
|
||||
@@ -36,7 +74,7 @@ class PedimentosService:
|
||||
Returns:
|
||||
Tuple of (list of pedimentos, total count)
|
||||
"""
|
||||
query = db.query(Pedimentos).filter(Pedimentos.tenant_id == tenant_id)
|
||||
query = db.query(Pedimentos).filter(Pedimentos.tenant_id == tenant_id, Pedimentos.company_id == company_id)
|
||||
|
||||
if filters:
|
||||
if filters.get("status"):
|
||||
@@ -70,7 +108,7 @@ class PedimentosService:
|
||||
Pedimento or None if not found
|
||||
"""
|
||||
query = db.query(Pedimentos).filter(
|
||||
Pedimentos.id == pedimento_id, Pedimentos.tenant_id == tenant_id
|
||||
Pedimentos.id == pedimento_id, Pedimentos.tenant_id == tenant_id, Pedimentos.company_id == company_id
|
||||
)
|
||||
|
||||
if company_id is not None:
|
||||
@@ -83,7 +121,7 @@ class PedimentosService:
|
||||
db: Session, pedimento_data: PedimentosCreate, tenant_id: int, company_id: int
|
||||
) -> Pedimentos:
|
||||
"""
|
||||
Create a new pedimento
|
||||
Create a new pedimento with related tables
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
@@ -94,21 +132,87 @@ class PedimentosService:
|
||||
Returns:
|
||||
Created pedimento
|
||||
"""
|
||||
pedimento = Pedimentos(**pedimento_data.model_dump())
|
||||
pedimento.tenant_id = tenant_id
|
||||
pedimento.company_id = company_id
|
||||
try:
|
||||
# Extraer datos de tablas relacionadas
|
||||
related_data = {
|
||||
'pedimento_dates': pedimento_data.pedimento_dates,
|
||||
'pedimento_decrementables': pedimento_data.pedimento_decrementables,
|
||||
'pedimento_incrementables': pedimento_data.pedimento_incrementables,
|
||||
'pedimento_indexes': pedimento_data.pedimento_indexes,
|
||||
'pedimento_validation': pedimento_data.pedimento_validation,
|
||||
'pedimento_customs_offices': pedimento_data.pedimento_customs_offices,
|
||||
'pedimento_payments': pedimento_data.pedimento_payments,
|
||||
'pedimento_rectification_destination': pedimento_data.pedimento_rectification_destination,
|
||||
'pedimento_rectification_origin': pedimento_data.pedimento_rectification_origin,
|
||||
'pedimento_transport_means': pedimento_data.pedimento_transport_means,
|
||||
'pedimento_config_additional': pedimento_data.pedimento_config_additional,
|
||||
'pedimento_config_calculations': pedimento_data.pedimento_config_calculations,
|
||||
'pedimento_config_parameters': pedimento_data.pedimento_config_parameters,
|
||||
'pedimento_config_surcharges': pedimento_data.pedimento_config_surcharges,
|
||||
'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',
|
||||
'pedimento_indexes', 'pedimento_validation', 'pedimento_customs_offices',
|
||||
'pedimento_payments', 'pedimento_rectification_destination',
|
||||
'pedimento_rectification_origin', 'pedimento_transport_means',
|
||||
'pedimento_config_additional', 'pedimento_config_calculations',
|
||||
'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
|
||||
|
||||
db.add(pedimento)
|
||||
db.commit()
|
||||
db.refresh(pedimento)
|
||||
return pedimento
|
||||
db.add(pedimento)
|
||||
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()
|
||||
obj = model_class(**obj_dict)
|
||||
obj.pedimento_id = pedimento.id
|
||||
obj.tenant_id = tenant_id
|
||||
obj.company_id = company_id
|
||||
db.add(obj)
|
||||
|
||||
create_related(PedimentoDates, related_data['pedimento_dates'])
|
||||
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'])
|
||||
|
||||
db.commit()
|
||||
db.refresh(pedimento)
|
||||
return pedimento
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error creating pedimento with related data: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def update(
|
||||
db: Session, pedimento_id: int, tenant_id: int, pedimento_data: PedimentosUpdate, company_id: int = None
|
||||
) -> Optional[Pedimentos]:
|
||||
"""
|
||||
Update a pedimento
|
||||
Update a pedimento and its related tables
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
@@ -124,13 +228,73 @@ class PedimentosService:
|
||||
if not pedimento:
|
||||
return None
|
||||
|
||||
update_data = pedimento_data.model_dump(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(pedimento, field, value)
|
||||
try:
|
||||
# Actualizar campos principales del pedimento
|
||||
update_data = pedimento_data.model_dump(exclude_unset=True, exclude={
|
||||
'pedimento_dates', 'pedimento_decrementables', 'pedimento_incrementables',
|
||||
'pedimento_indexes', 'pedimento_validation', 'pedimento_customs_offices',
|
||||
'pedimento_payments', 'pedimento_rectification_destination',
|
||||
'pedimento_rectification_origin', 'pedimento_transport_means',
|
||||
'pedimento_config_additional', 'pedimento_config_calculations',
|
||||
'pedimento_config_parameters', 'pedimento_config_surcharges',
|
||||
'pedimento_config_update_rectification', 'pedimento_config_updates'
|
||||
})
|
||||
|
||||
for field, value in update_data.items():
|
||||
setattr(pedimento, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(pedimento)
|
||||
return pedimento
|
||||
db.flush()
|
||||
|
||||
# Helper function para actualizar o crear objetos relacionados
|
||||
def update_or_create_related(service_class, model_class, data_attr):
|
||||
if not hasattr(pedimento_data, data_attr):
|
||||
return
|
||||
|
||||
data = getattr(pedimento_data, data_attr)
|
||||
if not data:
|
||||
return
|
||||
|
||||
existing = service_class.get_by_pedimento_id(db, pedimento_id, tenant_id)
|
||||
if existing:
|
||||
# Actualizar existente
|
||||
update_dict = data.model_dump(exclude_unset=True)
|
||||
for field, value in update_dict.items():
|
||||
setattr(existing, field, value)
|
||||
else:
|
||||
# Crear nuevo
|
||||
obj_dict = data.model_dump()
|
||||
obj = model_class(**obj_dict)
|
||||
obj.pedimento_id = pedimento_id
|
||||
obj.tenant_id = tenant_id
|
||||
obj.company_id = company_id
|
||||
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')
|
||||
|
||||
db.commit()
|
||||
db.refresh(pedimento)
|
||||
return pedimento
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error updating pedimento with related data: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def delete(db: Session, pedimento_id: int, tenant_id: int, company_id: int = None) -> bool:
|
||||
@@ -153,5 +317,3 @@ class PedimentosService:
|
||||
db.delete(pedimento)
|
||||
db.commit()
|
||||
return True
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
@@ -38,27 +38,27 @@ router.include_router(user_tenant_router, prefix="/a76", tags=["a76 / user-tenan
|
||||
router.include_router(licenses_router, prefix="/a76", tags=["a76 / licenses"])
|
||||
router.include_router(pedimentos_router, prefix="/a76")
|
||||
router.include_router(
|
||||
client_and_provider_router, prefix="/a76", tags=["a76 / clients and providers"]
|
||||
client_and_provider_router, prefix="/a76", tags=["a76 / clients_and_providers"]
|
||||
)
|
||||
router.include_router(company_router, prefix="/a76", tags=["a76 / company"])
|
||||
router.include_router(classes_router, prefix="/a76", tags=["a76 / classes"])
|
||||
router.include_router(parts_router, prefix="/a76", tags=["a76 / parts"])
|
||||
router.include_router(
|
||||
permission_rule_oct_router, prefix="/a76", tags=["a76 / PermissionRuleOct"]
|
||||
permission_rule_oct_router, prefix="/a76", tags=["a76 / permission_rule_oct"]
|
||||
)
|
||||
router.include_router(package_router, prefix="/a76", tags=["a76 / Package"])
|
||||
router.include_router(seal_router, prefix="/a76", tags=["a76 / Seal"])
|
||||
router.include_router(package_router, prefix="/a76", tags=["a76 / package"])
|
||||
router.include_router(seal_router, prefix="/a76", tags=["a76 / seal"])
|
||||
router.include_router(
|
||||
fraction_rule_octave_router, prefix="/a76", tags=["a76 / FractionRuleOctave"]
|
||||
fraction_rule_octave_router, prefix="/a76", tags=["a76 / fraction_rule_octave"]
|
||||
)
|
||||
router.include_router(
|
||||
country_rule_oct_router, prefix="/a76", tags=["a76 / CountryRuleOct"]
|
||||
country_rule_oct_router, prefix="/a76", tags=["a76 / country_rule_oct"]
|
||||
)
|
||||
router.include_router(exchange_rate_router, prefix="/a76", tags=["a76 / ExchangeRate"])
|
||||
router.include_router(trailers_router, prefix="/a76", tags=["a76 / Trailers"])
|
||||
router.include_router(exchange_rate_router, prefix="/a76", tags=["a76 / exchange_rate"])
|
||||
router.include_router(trailers_router, prefix="/a76", tags=["a76 / trailers"])
|
||||
router.include_router(
|
||||
customs_broker_router, prefix="/a76", tags=["a76 / CustomsBroker"]
|
||||
customs_broker_router, prefix="/a76", tags=["a76 / customs_broker"]
|
||||
)
|
||||
router.include_router(drivers_router, prefix="/a76", tags=["a76 / Drivers"])
|
||||
router.include_router(transporters_router, prefix="/a76", tags=["a76 / Transporters"])
|
||||
router.include_router(vehicles_router, prefix="/a76", tags=["a76 / Vehicles"])
|
||||
router.include_router(drivers_router, prefix="/a76", tags=["a76 / drivers"])
|
||||
router.include_router(transporters_router, prefix="/a76", tags=["a76 / transporters"])
|
||||
router.include_router(vehicles_router, prefix="/a76", tags=["a76 / vehicles"])
|
||||
|
||||
Reference in New Issue
Block a user