- 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>
53 lines
2.8 KiB
Python
53 lines
2.8 KiB
Python
from decimal import Decimal
|
|
|
|
from api.v1.modules.crm.accounts import service as accounts_service
|
|
from api.v1.modules.crm.accounts.dto import AccountCreate
|
|
from api.v1.modules.crm.quotes import service as quotes_service
|
|
from api.v1.modules.crm.quotes.dto import QuoteCreate, QuoteItemCreate
|
|
from api.v1.modules.fin.invoices import service
|
|
from api.v1.modules.fin.invoices.dto import InvoiceCreate, InvoiceItemCreate, PaymentCreate
|
|
from api.v1.modules.ops.shipments import service as shipments_service
|
|
from api.v1.modules.ops.shipments.dto import ShipmentCreate
|
|
|
|
T, C = 1, 1
|
|
|
|
|
|
def test_invoice_totals_with_tax(db):
|
|
inv = service.create_invoice(db, InvoiceCreate(reference="F-001", currency="MXN", tax_rate=Decimal("16")), T, C)
|
|
service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="flete_internacional", quantity=1, unit_amount=1000), T, C)
|
|
service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="despacho_aduanal", quantity=1, unit_amount=500), T, C)
|
|
inv = service.get_invoice(db, inv.id, T, C)
|
|
assert float(inv.subtotal) == 1500.0
|
|
assert float(inv.tax_amount) == 240.0 # 16% de 1500
|
|
assert float(inv.total) == 1740.0
|
|
assert float(inv.balance) == 1740.0
|
|
|
|
|
|
def test_payment_marks_paid(db):
|
|
inv = service.create_invoice(db, InvoiceCreate(reference="F-002", tax_rate=Decimal("0")), T, C)
|
|
service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="otros", quantity=1, unit_amount=1000), T, C)
|
|
service.emit_invoice(db, inv.id, T, C)
|
|
service.create_payment(db, PaymentCreate(invoice_id=inv.id, amount=Decimal("400"), method="transferencia"), T, C)
|
|
inv = service.get_invoice(db, inv.id, T, C)
|
|
assert float(inv.paid_amount) == 400.0 and float(inv.balance) == 600.0
|
|
assert inv.status == "emitida"
|
|
service.create_payment(db, PaymentCreate(invoice_id=inv.id, amount=Decimal("600")), T, C)
|
|
inv = service.get_invoice(db, inv.id, T, C)
|
|
assert float(inv.balance) == 0.0 and inv.status == "pagada" and inv.paid_at is not None
|
|
|
|
|
|
def test_generate_from_shipment_copies_quote_items(db):
|
|
acc = accounts_service.create_account(db, AccountCreate(name="Cliente"), T, C)
|
|
quote = quotes_service.create_quote(db, QuoteCreate(reference="COT-9", account_id=acc.id, currency="USD"), T, C)
|
|
quotes_service.create_quote_item(db, QuoteItemCreate(quote_id=quote.id, concept="flete_internacional", quantity=1, unit_cost=1000, unit_sale=1500), T, C)
|
|
quotes_service.accept_quote(db, quote.id, T, C)
|
|
shipment = shipments_service.create_shipment_from_quote(db, quote.id, T, C)
|
|
|
|
inv = service.generate_from_shipment(db, shipment.id, T, C)
|
|
assert inv.shipment_id == shipment.id
|
|
assert inv.account_id == acc.id
|
|
assert inv.currency == "USD"
|
|
items = service.get_items(db, inv.id, T, C)
|
|
assert len(items) == 1 and float(items[0].unit_amount) == 1500.0
|
|
assert float(inv.subtotal) == 1500.0
|