- 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>
79 lines
4.1 KiB
Python
79 lines
4.1 KiB
Python
from datetime import date, datetime
|
|
|
|
from sqlalchemy import Date, DateTime, ForeignKey, Integer, Numeric, 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 Invoice(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Factura (Diagrama 4). Integra los costos de la operación para cobro al cliente."""
|
|
|
|
__tablename__ = "invoices"
|
|
__table_args__ = {"schema": "fin"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
reference: Mapped[str | None] = mapped_column(String(40), nullable=True, index=True) # folio
|
|
shipment_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("ops.shipments.id"), nullable=True, index=True
|
|
)
|
|
quote_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.quotes.id"), nullable=True
|
|
)
|
|
account_id: Mapped[int | None] = mapped_column(
|
|
Integer, ForeignKey("crm.accounts.id"), nullable=True, index=True
|
|
)
|
|
currency: Mapped[str] = mapped_column(String(3), nullable=False, server_default=text("'MXN'"))
|
|
# borrador | emitida | enviada | pagada | cancelada
|
|
status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'borrador'"), index=True)
|
|
issue_date: Mapped[date | None] = mapped_column(Date, nullable=True)
|
|
due_date: Mapped[date | None] = mapped_column(Date, nullable=True)
|
|
subtotal: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
tax_rate: Mapped[float] = mapped_column(Numeric(5, 2), nullable=False, server_default=text("0")) # % IVA
|
|
tax_amount: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
total: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
paid_amount: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
balance: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
bank_info: Mapped[str | None] = mapped_column(Text, nullable=True) # datos bancarios
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
paid_at: Mapped[datetime | None] = mapped_column(DateTime, 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 InvoiceItem(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Concepto de una factura (transporte, flete, despacho, gastos en destino, otros)."""
|
|
|
|
__tablename__ = "invoice_items"
|
|
__table_args__ = {"schema": "fin"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
invoice_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("fin.invoices.id"), nullable=False, index=True
|
|
)
|
|
concept: Mapped[str] = mapped_column(String(60), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
quantity: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False, server_default=text("1"))
|
|
unit_amount: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False, server_default=text("0"))
|
|
|
|
|
|
class Payment(Base, TenantScopedMixin, TimestampMixin):
|
|
"""Pago (cobranza) aplicado a una factura."""
|
|
|
|
__tablename__ = "payments"
|
|
__table_args__ = {"schema": "fin"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
invoice_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("fin.invoices.id"), nullable=False, index=True
|
|
)
|
|
amount: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False)
|
|
payment_date: Mapped[date | None] = mapped_column(Date, nullable=True)
|
|
# transferencia | efectivo | cheque | tarjeta | otro
|
|
method: Mapped[str | None] = mapped_column(String(40), nullable=True)
|
|
reference: Mapped[str | None] = mapped_column(String(120), nullable=True)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|