Implement document reconciliation v2
This commit is contained in:
@@ -209,6 +209,8 @@ def find_open_opportunities_by_document_references(refs: List[str]) -> List[Dict
|
||||
if not refs:
|
||||
return []
|
||||
with engine.begin() as conn:
|
||||
from app.document_reconciliation_service import prepare_effective_document_links
|
||||
prepare_effective_document_links(conn)
|
||||
rows = conn.execute(text("""
|
||||
WITH refs AS (
|
||||
SELECT unnest(CAST(:refs AS TEXT[])) AS ref
|
||||
@@ -218,11 +220,11 @@ def find_open_opportunities_by_document_references(refs: List[str]) -> List[Dict
|
||||
'commercial_document_reference'::text AS link_reason,
|
||||
d.updated_at AS match_updated_at
|
||||
FROM commercial_documents d
|
||||
JOIN opportunities o ON o.id = d.opportunity_id
|
||||
JOIN _effective_document_links l ON l.document_id=d.id AND l.relationship='PRIMARY'
|
||||
JOIN opportunities o ON o.id = l.opportunity_id
|
||||
JOIN refs r ON upper(COALESCE(d.document_number, '')) = upper(r.ref)
|
||||
WHERE o.status = 'open'
|
||||
AND COALESCE(d.is_active, TRUE) = TRUE
|
||||
AND d.opportunity_id IS NOT NULL
|
||||
), recon_matches AS (
|
||||
SELECT
|
||||
o.*, ri.document_number, ri.external_type AS document_kind, ri.amount AS total_amount, ri.amount,
|
||||
@@ -269,6 +271,8 @@ def find_open_company_domain_candidates(task: Dict[str, Any], *, recent_days: in
|
||||
if not domain:
|
||||
return []
|
||||
with engine.begin() as conn:
|
||||
from app.document_reconciliation_service import prepare_effective_document_links
|
||||
prepare_effective_document_links(conn)
|
||||
rows = conn.execute(text("""
|
||||
SELECT DISTINCT ON (o.id)
|
||||
o.id::text AS opportunity_id,
|
||||
@@ -286,7 +290,8 @@ def find_open_company_domain_candidates(task: Dict[str, Any], *, recent_days: in
|
||||
o.updated_at
|
||||
FROM opportunities o
|
||||
LEFT JOIN customers c ON c.id = o.local_customer_id
|
||||
LEFT JOIN commercial_documents d ON d.opportunity_id = o.id AND COALESCE(d.is_active, TRUE) = TRUE
|
||||
LEFT JOIN _effective_document_links l ON l.opportunity_id=o.id AND l.relationship='PRIMARY'
|
||||
LEFT JOIN commercial_documents d ON d.id=l.document_id AND COALESCE(d.is_active, TRUE)=TRUE
|
||||
WHERE o.status = 'open'
|
||||
AND o.updated_at >= now() - make_interval(days => :recent_days)
|
||||
AND (
|
||||
|
||||
Reference in New Issue
Block a user