Cierra los huecos de la auditoría contra "SOFTWARE PARA AGENTES DE CARGA": - ops (Diag. 2/3): bitácora con puntos de decisión (kind=decision) y ciclo de corrección (parent_event_id/attempt) para ¿Cut Off? y ¿despacho autorizado? (R-E-05/13, R-I-06). Reprogramación de salida (previous_etd, R-E-06). Hitos operativos completos export/import. Cierre operativo con costos finales (close_shipment, R-E-22). - fin (Diag. 4): facturación con gate por cierre operativo y sin duplicar (R-F-01), costos de operación arrastrados (ops_cost_total, R-F-02), envío con PDF generado y guardado en MinIO (send_invoice + pdf.py sin dependencias, R-F-05) y revisión del cliente (en_revision_cliente + aprobación, R-F-06). - crm (Diag. 1): opportunity_id enlaza embudo→RFQ (R-C-02), contacto como etapa (first_contact_at, R-C-04), re-cotización (clone_quote + reopen, R-C-12). - transversal: catálogo de Incoterms y participantes/actores incl. autoridad aduanera (R-T-01/10), enforcement de permisos por carril (RBAC) con roles sembrados y dependencias dev-safe (R-T-07). - Migración d5e6f7a8b9c0 con downgrade. Seed extendido. 70 tests (12 nuevos). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
163 lines
5.5 KiB
Python
163 lines
5.5 KiB
Python
from fastapi import APIRouter, Depends, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user
|
|
|
|
from . import service
|
|
from .dto import (
|
|
QuoteCreate,
|
|
QuoteItemCreate,
|
|
QuoteItemResponse,
|
|
QuoteItemUpdate,
|
|
QuoteResponse,
|
|
QuoteUpdate,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _user_id(current_user: dict) -> str | None:
|
|
return current_user.get("sub") or current_user.get("id")
|
|
|
|
|
|
@router.get("/quotes", response_model=list[QuoteResponse])
|
|
def list_quotes(
|
|
company_id: int = Query(..., description="Company ID"),
|
|
search: str | None = Query(None),
|
|
quote_status: str | None = Query(None, alias="status"),
|
|
account_id: int | None = Query(None),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
tenant_id = current_user["tenant_id"]
|
|
return service.get_quotes(db, tenant_id, company_id, search, quote_status, account_id)
|
|
|
|
|
|
@router.get("/quotes/{quote_id}", response_model=QuoteResponse)
|
|
def get_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
tenant_id = current_user["tenant_id"]
|
|
return service.get_quote(db, quote_id, tenant_id, company_id)
|
|
|
|
|
|
@router.post("/quotes", response_model=QuoteResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_quote(
|
|
payload: QuoteCreate,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
tenant_id = current_user["tenant_id"]
|
|
return service.create_quote(db, payload, tenant_id, company_id, _user_id(current_user))
|
|
|
|
|
|
@router.patch("/quotes/{quote_id}", response_model=QuoteResponse)
|
|
def update_quote(
|
|
quote_id: int,
|
|
payload: QuoteUpdate,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
tenant_id = current_user["tenant_id"]
|
|
return service.update_quote(db, quote_id, payload, tenant_id, company_id, _user_id(current_user))
|
|
|
|
|
|
@router.patch("/quotes/{quote_id}/send", response_model=QuoteResponse)
|
|
def send_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.send_quote(db, quote_id, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.patch("/quotes/{quote_id}/accept", response_model=QuoteResponse)
|
|
def accept_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.accept_quote(db, quote_id, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.patch("/quotes/{quote_id}/reject", response_model=QuoteResponse)
|
|
def reject_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.reject_quote(db, quote_id, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.post("/quotes/{quote_id}/clone", response_model=QuoteResponse, status_code=status.HTTP_201_CREATED)
|
|
def clone_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Clona la cotización como borrador para re-cotizar (R-C-12)."""
|
|
return service.clone_quote(db, quote_id, current_user["tenant_id"], company_id, _user_id(current_user))
|
|
|
|
|
|
@router.delete("/quotes/{quote_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_quote(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
service.delete_quote(db, quote_id, current_user["tenant_id"], company_id)
|
|
|
|
|
|
# ----- Conceptos de la cotización -----
|
|
|
|
@router.get("/quotes/{quote_id}/items", response_model=list[QuoteItemResponse])
|
|
def list_quote_items(
|
|
quote_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.get_quote_items(db, quote_id, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.post("/quote-items", response_model=QuoteItemResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_quote_item(
|
|
payload: QuoteItemCreate,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.create_quote_item(db, payload, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.patch("/quote-items/{item_id}", response_model=QuoteItemResponse)
|
|
def update_quote_item(
|
|
item_id: int,
|
|
payload: QuoteItemUpdate,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
return service.update_quote_item(db, item_id, payload, current_user["tenant_id"], company_id)
|
|
|
|
|
|
@router.delete("/quote-items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_quote_item(
|
|
item_id: int,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
service.delete_quote_item(db, item_id, current_user["tenant_id"], company_id)
|