- schema fin: fin.invoices + fin.invoice_items + fin.payments; totales con IVA, estados borrador→emitida→enviada→pagada, cobranza (pagos) y saldo automático - generar-factura-desde-embarque (toma conceptos de venta de la cotización) - ops.shipment_events: bitácora/hitos del embarque con secuencia por defecto según operación (importación/exportación) — cubre Diagrama 3 - subida de documentos a MinIO: POST /crm/uploads (multipart) + URL firmada - migración c4d5e6f7a8b9, routers/permisos (fin), seed del flujo hasta factura - 58 tests pytest en verde Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
94 lines
5.1 KiB
Python
94 lines
5.1 KiB
Python
from datetime import date, datetime
|
|
|
|
from sqlalchemy import Date, DateTime, ForeignKey, Integer, String, Text, text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
|
|
from core.database import Base
|
|
|
|
|
|
class Shipment(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Operación / Embarque (Diagrama 2). Se crea al liberar una cotización aceptada."""
|
|
|
|
__tablename__ = "shipments"
|
|
__table_args__ = {"schema": "ops"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
reference: Mapped[str | None] = mapped_column(String(40), nullable=True, index=True) # folio de embarque
|
|
quote_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.quotes.id"), nullable=True, index=True
|
|
)
|
|
service_request_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.service_requests.id"), nullable=True
|
|
)
|
|
account_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.accounts.id"), nullable=True, index=True
|
|
)
|
|
operation_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
|
transport_mode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
|
service_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
|
incoterm: Mapped[str | None] = mapped_column(String(10), nullable=True)
|
|
origin: Mapped[str | None] = mapped_column(String(160), nullable=True)
|
|
destination: Mapped[str | None] = mapped_column(String(160), nullable=True)
|
|
# abierta | booking | en_transito | arribado | entregada | cerrada | cancelada
|
|
status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'abierta'"), index=True)
|
|
booking_number: Mapped[str | None] = mapped_column(String(60), nullable=True)
|
|
carrier_supplier_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.suppliers.id"), nullable=True
|
|
) # naviera / aerolínea / transportista
|
|
customs_agent_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.suppliers.id"), nullable=True
|
|
) # agente aduanal
|
|
destination_agent_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.suppliers.id"), nullable=True
|
|
) # agente en destino
|
|
cutoff_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # Cut Off
|
|
etd: Mapped[date | None] = mapped_column(Date, nullable=True) # salida estimada
|
|
eta: Mapped[date | None] = mapped_column(Date, nullable=True) # llegada estimada
|
|
vessel_flight: Mapped[str | None] = mapped_column(String(120), nullable=True) # buque / vuelo
|
|
container_number: Mapped[str | None] = mapped_column(String(60), nullable=True)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
owner_user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
|
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
updated_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
|
|
|
|
class ShipmentEvent(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Hito / bitácora del embarque (Diagramas 2 y 3). Timeline de la operación."""
|
|
|
|
__tablename__ = "shipment_events"
|
|
__table_args__ = {"schema": "ops"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
shipment_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("ops.shipments.id"), nullable=False, index=True
|
|
)
|
|
event_type: Mapped[str | None] = mapped_column(String(60), nullable=True) # clave del hito
|
|
title: Mapped[str] = mapped_column(String(160), nullable=False)
|
|
# pendiente | completado | omitido
|
|
status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'pendiente'"))
|
|
position: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text("0"))
|
|
planned_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
actual_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
|
|
class ShipmentDocument(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Documento de transporte del embarque (Master/House: MBL, HBL, MAWB, HAWB, CMR, etc.)."""
|
|
|
|
__tablename__ = "shipment_documents"
|
|
__table_args__ = {"schema": "ops"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
shipment_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("ops.shipments.id"), nullable=False, index=True
|
|
)
|
|
doc_kind: Mapped[str] = mapped_column(String(10), nullable=False, server_default=text("'otro'")) # master|house|otro
|
|
# MBL | HBL | MAWB | HAWB | CMR | factura_comercial | packing_list | carta_encomienda | carta_garantia | otro
|
|
doc_type: Mapped[str] = mapped_column(String(30), nullable=False)
|
|
number: Mapped[str | None] = mapped_column(String(80), nullable=True)
|
|
issue_date: Mapped[date | None] = mapped_column(Date, nullable=True)
|
|
file_url: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
|
file_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|