fix(retry): pass ANAF kwargs on retry/resync so VAT-payer partner is matched

retry_service called import_single_order without cod_fiscal_override /
anaf_strict / denumire_override, so re-imports searched the partner by the raw
GoMag CUI in dual mode and could match the non-payer twin (bare CUI) of a
payer (RO CUI). Add anaf_import_kwargs mirroring the sync flow, plus test and
agent docs for prod partner diagnostics.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L3v311J3SfjroTebzR96ng
This commit is contained in:
2026-09-29 11:01:09 +03:00
parent 371c73f5cf
commit 136c4e9986
4 changed files with 111 additions and 2 deletions

View File

@@ -9,6 +9,40 @@ from ..constants import OrderStatus
logger = logging.getLogger(__name__)
async def anaf_import_kwargs(order) -> dict:
"""ANAF-derived kwargs for import_single_order (mirrors the sync flow).
Without these, a re-import searches the partner by the raw GoMag CUI in dual
mode and can match the non-VAT-payer twin (bare CUI) of a VAT payer (RO CUI).
Empty dict when not an RO company or ANAF has no verdict (sync tolerates that too).
"""
from . import sqlite_service, import_service, anaf_service
b = order.billing
if not (b.is_company and b.company_code and (b.country or "").strip().lower() == "romania"):
return {}
bare, _ = anaf_service.sanitize_cui(import_service.clean_web_text(b.company_code) or "")
if not anaf_service.validate_cui(bare):
return {}
data = await sqlite_service.get_anaf_cache(bare)
if not data:
try:
fresh = await anaf_service.check_vat_status_batch([bare])
if fresh:
await sqlite_service.bulk_populate_anaf_cache(fresh)
data = fresh.get(bare)
except Exception as e:
logger.warning(f"ANAF check failed for {bare}: {e}")
if not data or data.get("scpTVA") is None:
return {}
name = (data.get("denumire_anaf") or "").strip().upper()
return {
"cod_fiscal_override": anaf_service.determine_correct_cod_fiscal(bare, data["scpTVA"]),
"anaf_strict": 1,
"denumire_override": name or None,
}
async def _download_and_reimport(order_number: str, order_date_str: str, customer_name: str, app_settings: dict) -> dict:
"""Download order from GoMag and re-import it into Oracle.
@@ -97,7 +131,8 @@ async def _download_and_reimport(order_number: str, order_date_str: str, custome
result = await asyncio.to_thread(
import_service.import_single_order,
target_order, id_pol=id_pol, id_sectie=id_sectie,
app_settings=app_settings, id_gestiuni=id_gestiuni
app_settings=app_settings, id_gestiuni=id_gestiuni,
**await anaf_import_kwargs(target_order),
)
except Exception as e:
logger.error(f"Retry import failed for {order_number}: {e}")