feat(fin,ops): Facturación y Cobranza (Diag. 4), bitácora de embarque (Diag. 3) y subida a MinIO
- 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>
This commit is contained in:
@@ -37,8 +37,9 @@ import api.v1.modules.crm.quotes.models # noqa: E402,F401
|
||||
import api.v1.modules.crm.service_requests.models # noqa: E402,F401
|
||||
import api.v1.modules.crm.suppliers.models # noqa: E402,F401
|
||||
import api.v1.modules.ops.shipments.models # noqa: E402,F401
|
||||
import api.v1.modules.fin.invoices.models # noqa: E402,F401
|
||||
|
||||
_SCHEMA_MAP = {"crm": None, "core": None, "ops": None}
|
||||
_SCHEMA_MAP = {"crm": None, "core": None, "ops": None, "fin": None}
|
||||
|
||||
# Tabla mínima core.tenants para resolver la FK tenant_id de las tablas crm.
|
||||
# En CI (PostgreSQL) la tabla real la crea la migración inicial del core.
|
||||
|
||||
52
backend/tests/test_invoices.py
Normal file
52
backend/tests/test_invoices.py
Normal file
@@ -0,0 +1,52 @@
|
||||
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
|
||||
34
backend/tests/test_shipment_events.py
Normal file
34
backend/tests/test_shipment_events.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from api.v1.modules.ops.shipments import service
|
||||
from api.v1.modules.ops.shipments.dto import ShipmentCreate
|
||||
|
||||
T, C = 1, 1
|
||||
|
||||
|
||||
def test_seed_import_milestones(db):
|
||||
s = service.create_shipment(db, ShipmentCreate(reference="IMP-1", operation_type="importacion"), T, C)
|
||||
events = service.seed_default_milestones(db, s.id, T, C)
|
||||
titles = [e.event_type for e in events]
|
||||
assert "aviso_llegada" in titles
|
||||
assert "despacho_importacion" in titles
|
||||
assert "entrega" in titles
|
||||
# idempotente: no duplica
|
||||
again = service.seed_default_milestones(db, s.id, T, C)
|
||||
assert len(again) == len(events)
|
||||
|
||||
|
||||
def test_seed_export_milestones_and_complete(db):
|
||||
s = service.create_shipment(db, ShipmentCreate(reference="EXP-1", operation_type="exportacion"), T, C)
|
||||
events = service.seed_default_milestones(db, s.id, T, C)
|
||||
assert any(e.event_type == "embarque" for e in events)
|
||||
done = service.complete_shipment_event(db, events[0].id, T, C)
|
||||
assert done.status == "completado" and done.actual_date is not None
|
||||
|
||||
|
||||
def test_seed_requires_operation_type(db):
|
||||
s = service.create_shipment(db, ShipmentCreate(reference="X-1"), T, C) # sin operation_type
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.seed_default_milestones(db, s.id, T, C)
|
||||
assert exc.value.status_code == 422
|
||||
Reference in New Issue
Block a user