feat(crm,ops): proceso comercial (solicitudes/RFQ, tarifas, cotizaciones) + Operaciones (embarques)
Diagrama 1 (CRM comercial) y Diagrama 2 (Operaciones) del spec de agente de carga: - crm.service_requests (RFQ) + crm.rate_requests (solicitud de tarifas a proveedores) - crm.quotes + crm.quote_items: conceptos costo/venta/margen, totales automáticos, estados borrador→enviada→aceptada/rechazada - schema ops: ops.shipments (booking, Cut Off, ETD/ETA, naviera/agente aduanal/destino) y ops.shipment_documents (MBL/HBL, MAWB/HAWB, CMR…) - liberar-a-operaciones: crea el embarque desde la cotización aceptada - migración b3c4d5e6f7a8, routers/permisos, seed del flujo completo - 52 tests pytest en verde Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -33,9 +33,12 @@ import api.v1.modules.crm.documents.models # noqa: E402,F401
|
||||
import api.v1.modules.crm.leads.models # noqa: E402,F401
|
||||
import api.v1.modules.crm.opportunities.models # noqa: E402,F401
|
||||
import api.v1.modules.crm.pipelines.models # noqa: E402,F401
|
||||
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
|
||||
|
||||
_SCHEMA_MAP = {"crm": None, "core": None}
|
||||
_SCHEMA_MAP = {"crm": None, "core": None, "ops": 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.
|
||||
|
||||
46
backend/tests/test_quotes.py
Normal file
46
backend/tests/test_quotes.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from api.v1.modules.crm.quotes import service
|
||||
from api.v1.modules.crm.quotes.dto import QuoteCreate, QuoteItemCreate, QuoteItemUpdate
|
||||
from api.v1.modules.crm.service_requests import service as sr_service
|
||||
from api.v1.modules.crm.service_requests.dto import ServiceRequestCreate
|
||||
|
||||
T, C = 1, 1
|
||||
|
||||
|
||||
def test_quote_totals_recompute_on_items(db):
|
||||
q = service.create_quote(db, QuoteCreate(reference="COT-001", currency="USD"), T, C)
|
||||
service.create_quote_item(
|
||||
db, QuoteItemCreate(quote_id=q.id, concept="flete_internacional", quantity=2, unit_cost=100, unit_sale=150), T, C
|
||||
)
|
||||
service.create_quote_item(
|
||||
db, QuoteItemCreate(quote_id=q.id, concept="despacho_aduanal", quantity=1, unit_cost=50, unit_sale=90), T, C
|
||||
)
|
||||
q = service.get_quote(db, q.id, T, C)
|
||||
assert float(q.total_cost) == 250.0 # 2*100 + 1*50
|
||||
assert float(q.total_sale) == 390.0 # 2*150 + 1*90
|
||||
|
||||
|
||||
def test_quote_totals_update_and_delete_item(db):
|
||||
q = service.create_quote(db, QuoteCreate(reference="COT-002"), T, C)
|
||||
item = service.create_quote_item(
|
||||
db, QuoteItemCreate(quote_id=q.id, concept="otros", quantity=1, unit_cost=100, unit_sale=200), T, C
|
||||
)
|
||||
service.update_quote_item(db, item.id, QuoteItemUpdate(unit_sale=Decimal("300")), T, C)
|
||||
q = service.get_quote(db, q.id, T, C)
|
||||
assert float(q.total_sale) == 300.0
|
||||
service.delete_quote_item(db, item.id, T, C)
|
||||
q = service.get_quote(db, q.id, T, C)
|
||||
assert float(q.total_sale) == 0.0
|
||||
|
||||
|
||||
def test_accept_quote_updates_service_request(db):
|
||||
sr = sr_service.create_service_request(db, ServiceRequestCreate(operation_type="exportacion"), T, C)
|
||||
q = service.create_quote(db, QuoteCreate(reference="COT-003", service_request_id=sr.id), T, C)
|
||||
service.send_quote(db, q.id, T, C)
|
||||
accepted = service.accept_quote(db, q.id, T, C)
|
||||
assert accepted.status == "aceptada"
|
||||
assert accepted.accepted_at is not None
|
||||
# la solicitud asociada queda aceptada
|
||||
sr = sr_service.get_service_request(db, sr.id, T, C)
|
||||
assert sr.status == "aceptada"
|
||||
66
backend/tests/test_service_requests.py
Normal file
66
backend/tests/test_service_requests.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
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.service_requests import service
|
||||
from api.v1.modules.crm.service_requests.dto import (
|
||||
RateRequestCreate,
|
||||
ServiceRequestCreate,
|
||||
ServiceRequestUpdate,
|
||||
)
|
||||
|
||||
T, C = 1, 1
|
||||
|
||||
|
||||
def test_create_service_request(db):
|
||||
acc = accounts_service.create_account(db, AccountCreate(name="Cliente"), T, C)
|
||||
sr = service.create_service_request(
|
||||
db,
|
||||
ServiceRequestCreate(
|
||||
account_id=acc.id, operation_type="exportacion", transport_mode="maritimo",
|
||||
service_type="puerta_puerta", origin="Manzanillo", destination="Long Beach",
|
||||
incoterm="FOB", load_type="FCL",
|
||||
),
|
||||
T, C, user_id="dev",
|
||||
)
|
||||
assert sr.id is not None
|
||||
assert sr.status == "nueva"
|
||||
assert sr.created_by == "dev"
|
||||
|
||||
|
||||
def test_service_request_rejects_unknown_account(db):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.create_service_request(db, ServiceRequestCreate(account_id=999, operation_type="importacion"), T, C)
|
||||
assert exc.value.status_code == 422
|
||||
|
||||
|
||||
def test_filter_by_operation_and_status(db):
|
||||
service.create_service_request(db, ServiceRequestCreate(operation_type="exportacion"), T, C)
|
||||
service.create_service_request(db, ServiceRequestCreate(operation_type="importacion"), T, C)
|
||||
exp = service.get_service_requests(db, T, C, operation_type="exportacion")
|
||||
assert len(exp) == 1 and exp[0].operation_type == "exportacion"
|
||||
|
||||
|
||||
def test_rate_request_requires_service_request(db):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.create_rate_request(
|
||||
db, RateRequestCreate(service_request_id=999, concept="flete_internacional"), T, C
|
||||
)
|
||||
assert exc.value.status_code == 404
|
||||
|
||||
|
||||
def test_rate_request_ok_and_listed(db):
|
||||
sr = service.create_service_request(db, ServiceRequestCreate(operation_type="exportacion"), T, C)
|
||||
service.create_rate_request(
|
||||
db, RateRequestCreate(service_request_id=sr.id, concept="flete_internacional", rate_amount=1200, currency="USD"),
|
||||
T, C,
|
||||
)
|
||||
rates = service.get_rate_requests(db, T, C, service_request_id=sr.id)
|
||||
assert len(rates) == 1 and rates[0].concept == "flete_internacional"
|
||||
|
||||
|
||||
def test_update_service_request_status(db):
|
||||
sr = service.create_service_request(db, ServiceRequestCreate(operation_type="exportacion"), T, C)
|
||||
upd = service.update_service_request(db, sr.id, ServiceRequestUpdate(status="en_analisis"), T, C)
|
||||
assert upd.status == "en_analisis"
|
||||
60
backend/tests/test_shipments.py
Normal file
60
backend/tests/test_shipments.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
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
|
||||
from api.v1.modules.crm.service_requests import service as sr_service
|
||||
from api.v1.modules.crm.service_requests.dto import ServiceRequestCreate
|
||||
from api.v1.modules.ops.shipments import service
|
||||
from api.v1.modules.ops.shipments.dto import ShipmentCreate, ShipmentDocumentCreate
|
||||
|
||||
T, C = 1, 1
|
||||
|
||||
|
||||
def test_release_requires_accepted_quote(db):
|
||||
q = quotes_service.create_quote(db, QuoteCreate(reference="COT-A"), T, C)
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.create_shipment_from_quote(db, q.id, T, C)
|
||||
assert exc.value.status_code == 422 # aún no aceptada
|
||||
|
||||
|
||||
def test_release_from_accepted_quote_copies_data(db):
|
||||
acc = accounts_service.create_account(db, AccountCreate(name="Cliente"), T, C)
|
||||
sr = sr_service.create_service_request(
|
||||
db,
|
||||
ServiceRequestCreate(account_id=acc.id, operation_type="exportacion", transport_mode="maritimo",
|
||||
origin="Veracruz", destination="Rotterdam"),
|
||||
T, C,
|
||||
)
|
||||
q = quotes_service.create_quote(db, QuoteCreate(reference="COT-B", service_request_id=sr.id, account_id=acc.id), T, C)
|
||||
quotes_service.accept_quote(db, q.id, T, C)
|
||||
|
||||
shipment = service.create_shipment_from_quote(db, q.id, T, C, user_id="dev")
|
||||
assert shipment.quote_id == q.id
|
||||
assert shipment.account_id == acc.id
|
||||
assert shipment.operation_type == "exportacion"
|
||||
assert shipment.transport_mode == "maritimo"
|
||||
assert shipment.origin == "Veracruz"
|
||||
assert shipment.status == "abierta"
|
||||
# la solicitud queda liberada
|
||||
sr = sr_service.get_service_request(db, sr.id, T, C)
|
||||
assert sr.status == "liberada"
|
||||
|
||||
|
||||
def test_shipment_crud_and_documents(db):
|
||||
shipment = service.create_shipment(db, ShipmentCreate(reference="EMB-001", status="abierta", origin="MX"), T, C)
|
||||
assert shipment.id is not None
|
||||
doc = service.create_shipment_document(
|
||||
db, ShipmentDocumentCreate(shipment_id=shipment.id, doc_kind="master", doc_type="MBL", number="MBL123"), T, C
|
||||
)
|
||||
assert doc.doc_type == "MBL"
|
||||
docs = service.get_shipment_documents(db, T, C, shipment_id=shipment.id)
|
||||
assert len(docs) == 1
|
||||
|
||||
|
||||
def test_shipment_rejects_unknown_quote(db):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.create_shipment(db, ShipmentCreate(quote_id=999), T, C)
|
||||
assert exc.value.status_code == 422
|
||||
Reference in New Issue
Block a user