feat(sync): add batched ANAF backfill for orders missing TVA status
Orders imported before the ANAF dedup feature had no anaf_platitor_tva. Step 4c now auto-backfills on each sync: batch cache lookup, single ANAF API call for uncached CUIs, bulk DB update in one transaction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1085,7 +1085,7 @@ async def bulk_populate_anaf_cache(results: dict[str, dict]):
|
||||
|
||||
async def get_expired_cuis_for_prepopulate() -> list[str]:
|
||||
"""Get CUIs from recent orders that need ANAF cache refresh."""
|
||||
from ..services import anaf_service
|
||||
from . import anaf_service
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
@@ -1160,6 +1160,72 @@ async def update_order_partner_data(order_number: str, partner_data: dict):
|
||||
await db.close()
|
||||
|
||||
|
||||
async def get_orders_missing_anaf() -> list[dict]:
|
||||
"""Get orders with cod_fiscal_roa set but no ANAF data (for backfill)."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
SELECT order_number, cod_fiscal_roa, denumire_roa, customer_name
|
||||
FROM orders
|
||||
WHERE cod_fiscal_roa IS NOT NULL
|
||||
AND cod_fiscal_roa != ''
|
||||
AND anaf_platitor_tva IS NULL
|
||||
AND status IN ('IMPORTED', 'ALREADY_IMPORTED')
|
||||
""")
|
||||
rows = await cursor.fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def get_anaf_cache_batch(bare_cuis: list[str]) -> dict[str, dict]:
|
||||
"""Get cached ANAF data for multiple CUIs (valid for 7 days)."""
|
||||
if not bare_cuis:
|
||||
return {}
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
placeholders = ",".join("?" for _ in bare_cuis)
|
||||
cursor = await db.execute(f"""
|
||||
SELECT cui, scp_tva, denumire_anaf, checked_at
|
||||
FROM anaf_cache
|
||||
WHERE cui IN ({placeholders}) AND checked_at > datetime('now', '-7 days')
|
||||
""", bare_cuis)
|
||||
rows = await cursor.fetchall()
|
||||
return {
|
||||
r["cui"]: {
|
||||
"scpTVA": bool(r["scp_tva"]) if r["scp_tva"] is not None else None,
|
||||
"denumire_anaf": r["denumire_anaf"] or "",
|
||||
"checked_at": r["checked_at"],
|
||||
}
|
||||
for r in rows
|
||||
}
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def bulk_update_order_anaf_data(updates: list[tuple]):
|
||||
"""Batch update orders with ANAF data.
|
||||
|
||||
updates: list of (anaf_platitor_tva, anaf_checked_at, anaf_denumire_mismatch, denumire_anaf, order_number)
|
||||
"""
|
||||
if not updates:
|
||||
return
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
await db.executemany("""
|
||||
UPDATE orders SET
|
||||
anaf_platitor_tva = ?,
|
||||
anaf_checked_at = ?,
|
||||
anaf_denumire_mismatch = ?,
|
||||
denumire_anaf = ?,
|
||||
updated_at = datetime('now')
|
||||
WHERE order_number = ?
|
||||
""", updates)
|
||||
await db.commit()
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
# ── Address Quality Cache (via app_settings) ──────
|
||||
|
||||
async def get_incomplete_addresses_count() -> int:
|
||||
|
||||
Reference in New Issue
Block a user