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:
@@ -893,6 +893,57 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None
|
||||
except Exception as e:
|
||||
logger.warning(f"Invoice/order status check failed: {e}")
|
||||
|
||||
# Step 4c: ANAF backfill — populate anaf_platitor_tva for orders with CUI but no ANAF data
|
||||
try:
|
||||
orders_needing_anaf = await sqlite_service.get_orders_missing_anaf()
|
||||
if orders_needing_anaf:
|
||||
# Group orders by unique CUI
|
||||
from collections import defaultdict
|
||||
cui_to_orders = defaultdict(list)
|
||||
for o in orders_needing_anaf:
|
||||
bare = anaf_service.strip_ro_prefix(o["cod_fiscal_roa"])
|
||||
if anaf_service.validate_cui(bare):
|
||||
cui_to_orders[bare].append(o)
|
||||
|
||||
# Batch cache lookup
|
||||
unique_cuis = list(cui_to_orders.keys())
|
||||
anaf_cache = await sqlite_service.get_anaf_cache_batch(unique_cuis)
|
||||
|
||||
# Single ANAF API call for uncached CUIs
|
||||
uncached = [c for c in unique_cuis if c not in anaf_cache]
|
||||
if uncached:
|
||||
fresh = await anaf_service.check_vat_status_batch(uncached)
|
||||
if fresh:
|
||||
await sqlite_service.bulk_populate_anaf_cache(fresh)
|
||||
anaf_cache.update(fresh)
|
||||
|
||||
# Build batch updates
|
||||
db_updates = []
|
||||
for cui, orders_for_cui in cui_to_orders.items():
|
||||
data = anaf_cache.get(cui)
|
||||
if not data or data.get("scpTVA") is None:
|
||||
continue
|
||||
platitor = 1 if data["scpTVA"] else 0
|
||||
checked_at = data.get("checked_at")
|
||||
denumire_anaf = data.get("denumire_anaf") or ""
|
||||
for o in orders_for_cui:
|
||||
mismatch = 0
|
||||
den_store = None
|
||||
if denumire_anaf:
|
||||
norm_roa = anaf_service.normalize_company_name(o.get("denumire_roa") or o.get("customer_name") or "")
|
||||
norm_anaf = anaf_service.normalize_company_name(denumire_anaf)
|
||||
if norm_roa and norm_anaf and norm_roa != norm_anaf:
|
||||
mismatch = 1
|
||||
den_store = denumire_anaf
|
||||
db_updates.append((platitor, checked_at, mismatch, den_store, o["order_number"]))
|
||||
|
||||
await sqlite_service.bulk_update_order_anaf_data(db_updates)
|
||||
if db_updates:
|
||||
_log_line(run_id, f"ANAF backfill: {len(db_updates)}/{len(orders_needing_anaf)} comenzi actualizate")
|
||||
except Exception as e:
|
||||
logger.warning(f"ANAF backfill failed: {e}")
|
||||
_log_line(run_id, f"ANAF backfill eroare: {e}")
|
||||
|
||||
# Step 5: Update sync run
|
||||
total_imported = imported_count + already_imported_count # backward-compat
|
||||
status = "completed" if error_count <= 10 else "failed"
|
||||
|
||||
Reference in New Issue
Block a user