Implement document reconciliation v2
This commit is contained in:
@@ -95,20 +95,10 @@ def _commercial_document_operation_fallbacks(opportunity_id: str) -> Dict[tuple,
|
||||
can show “Fatura ○” although a current invoice is visibly associated.
|
||||
"""
|
||||
try:
|
||||
with engine.begin() as conn:
|
||||
rows = conn.execute(text("""
|
||||
SELECT document_kind, document_number, external_id, external_url, status, payload, updated_at
|
||||
FROM commercial_documents
|
||||
WHERE opportunity_id = CAST(:opportunity_id AS UUID)
|
||||
AND COALESCE(is_active, TRUE) IS TRUE
|
||||
AND COALESCE(role, 'current') IN ('current','accepted','historical','history')
|
||||
AND document_kind IN ('quotation','quote','proforma','invoice')
|
||||
ORDER BY
|
||||
CASE WHEN COALESCE(role, 'current') IN ('current','accepted') THEN 0 ELSE 1 END,
|
||||
CASE WHEN document_kind = 'invoice' THEN 0 ELSE 1 END,
|
||||
COALESCE(updated_at, now()) DESC
|
||||
LIMIT 10
|
||||
"""), {"opportunity_id": opportunity_id}).mappings().all()
|
||||
from app.document_reconciliation_service import resolve_document_links, select_valid_primary
|
||||
resolved = resolve_document_links(opportunity_id)
|
||||
rows = [row for kind in ("invoice", "quotation", "quote", "proforma")
|
||||
if (row := select_valid_primary(resolved, kind)) is not None]
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user