fin.invoices gana tipo de comprobante, forma y método de pago y CP de
expedición; fin.invoice_items gana concepto de catálogo y las claves ProdServ,
unidad y objeto de impuesto. Todas nullable: las facturas ya emitidas no las
tienen y siguen funcionando igual (listado, detalle, PDF, envío).
La columna de texto libre invoice_items.concept se conserva obligatoria porque
la consume el PDF actual; al capturar por catálogo, el service hereda ahí la
descripción del concepto cuando el cliente no la envía.
Nueva tabla fin.invoice_item_taxes para el detalle de impuestos trasladados y
retenidos por partida. No interviene en el cálculo de subtotal/IVA/total, que
sigue saliendo de invoices.tax_rate.
Incluye la migración e6f7a8b9c0d1 (crea el schema sat, siembra los catálogos con
sync_catalogs y monta las tablas e índices nuevos) y registra los permisos
fin.concept.* y fin.settings.{view,edit}.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
142 lines
4.4 KiB
Python
142 lines
4.4 KiB
Python
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, computed_field
|
|
|
|
|
|
class InvoiceClientReviewInput(BaseModel):
|
|
"""Resultado de la revisión de la factura por el cliente (R-F-06)."""
|
|
approved: bool
|
|
notes: str | None = None
|
|
|
|
|
|
class InvoiceItemSatFields(BaseModel):
|
|
"""Claves fiscales de la partida. Opcionales: las facturas previas no las tienen."""
|
|
|
|
concept_id: int | None = None
|
|
product_service_id: int | None = None
|
|
unit_of_measure_id: int | None = None
|
|
tax_object_id: int | None = None
|
|
|
|
|
|
class InvoiceItemBase(InvoiceItemSatFields):
|
|
concept: str = Field(..., max_length=60)
|
|
description: str | None = Field(None, max_length=255)
|
|
quantity: Decimal = Field(Decimal(1), ge=0, max_digits=12, decimal_places=2)
|
|
unit_amount: Decimal = Field(Decimal(0), ge=0, max_digits=14, decimal_places=2)
|
|
|
|
|
|
class InvoiceItemCreate(InvoiceItemBase):
|
|
invoice_id: int
|
|
# Opcional solo si viene concept_id: el service copia la descripción del concepto.
|
|
concept: str | None = Field(None, max_length=60)
|
|
|
|
|
|
class InvoiceItemUpdate(InvoiceItemSatFields):
|
|
concept: str | None = Field(None, max_length=60)
|
|
description: str | None = Field(None, max_length=255)
|
|
quantity: Decimal | None = Field(None, ge=0, max_digits=12, decimal_places=2)
|
|
unit_amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
|
|
|
|
|
|
class InvoiceItemResponse(InvoiceItemBase):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
invoice_id: int
|
|
tenant_id: int
|
|
company_id: int
|
|
|
|
@computed_field
|
|
@property
|
|
def line_total(self) -> Decimal:
|
|
return (self.quantity or Decimal(0)) * (self.unit_amount or Decimal(0))
|
|
|
|
|
|
class PaymentBase(BaseModel):
|
|
amount: Decimal = Field(..., gt=0, max_digits=14, decimal_places=2)
|
|
payment_date: date | None = None
|
|
method: str | None = Field(None, max_length=40)
|
|
reference: str | None = Field(None, max_length=120)
|
|
notes: str | None = None
|
|
|
|
|
|
class PaymentCreate(PaymentBase):
|
|
invoice_id: int
|
|
|
|
|
|
class PaymentResponse(PaymentBase):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
invoice_id: int
|
|
tenant_id: int
|
|
company_id: int
|
|
created_at: datetime
|
|
|
|
|
|
class InvoiceBase(BaseModel):
|
|
reference: str | None = Field(None, max_length=40)
|
|
shipment_id: int | None = None
|
|
quote_id: int | None = None
|
|
account_id: int | None = None
|
|
currency: str = Field("MXN", max_length=3)
|
|
issue_date: date | None = None
|
|
due_date: date | None = None
|
|
tax_rate: Decimal = Field(Decimal(0), ge=0, le=100, max_digits=5, decimal_places=2)
|
|
bank_info: str | None = None
|
|
notes: str | None = None
|
|
owner_user_id: str | None = Field(None, max_length=64)
|
|
# ----- Claves fiscales del CFDI (opcionales mientras no se timbre) -----
|
|
voucher_type_id: int | None = None
|
|
payment_form_id: int | None = None
|
|
payment_method_id: int | None = None
|
|
expedition_zip_code: str | None = Field(None, max_length=5)
|
|
|
|
|
|
class InvoiceCreate(InvoiceBase):
|
|
pass
|
|
|
|
|
|
class InvoiceUpdate(BaseModel):
|
|
reference: str | None = Field(None, max_length=40)
|
|
shipment_id: int | None = None
|
|
quote_id: int | None = None
|
|
account_id: int | None = None
|
|
currency: str | None = Field(None, max_length=3)
|
|
issue_date: date | None = None
|
|
due_date: date | None = None
|
|
tax_rate: Decimal | None = Field(None, ge=0, le=100, max_digits=5, decimal_places=2)
|
|
bank_info: str | None = None
|
|
notes: str | None = None
|
|
owner_user_id: str | None = Field(None, max_length=64)
|
|
voucher_type_id: int | None = None
|
|
payment_form_id: int | None = None
|
|
payment_method_id: int | None = None
|
|
expedition_zip_code: str | None = Field(None, max_length=5)
|
|
|
|
|
|
class InvoiceResponse(InvoiceBase):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
status: str
|
|
subtotal: Decimal
|
|
tax_amount: Decimal
|
|
total: Decimal
|
|
paid_amount: Decimal
|
|
balance: Decimal
|
|
ops_cost_total: Decimal | None = None
|
|
sent_at: datetime | None = None
|
|
paid_at: datetime | None = None
|
|
pdf_file_key: str | None = None
|
|
client_reviewed_at: datetime | None = None
|
|
client_approved: bool | None = None
|
|
review_notes: str | None = None
|
|
created_by: str | None = None
|
|
updated_by: str | None = None
|
|
tenant_id: int
|
|
company_id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|