feat(sync): already_imported tracking, invoice cache, path fixes, remove vfp
- Track already_imported/new_imported counts separately in sync_runs and surface them in status API + dashboard last-run card - Cache invoice data in SQLite orders table (factura_* columns); dashboard falls back to Oracle only for uncached imported orders - Resolve JSON_OUTPUT_DIR and SQLITE_DB_PATH relative to known anchored roots in config.py, independent of CWD (fixes WSL2 start) - Use single Oracle connection for entire validation phase (perf) - Batch upsert web_products instead of one-by-one - Remove stale VFP scripts (replaced by gomag-vending.prg workflow) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -84,6 +84,8 @@ async def sync_status():
|
||||
"imported": row_dict.get("imported", 0),
|
||||
"skipped": row_dict.get("skipped", 0),
|
||||
"errors": row_dict.get("errors", 0),
|
||||
"already_imported": row_dict.get("already_imported", 0),
|
||||
"new_imported": row_dict.get("new_imported", 0),
|
||||
}
|
||||
finally:
|
||||
await db.close()
|
||||
@@ -147,6 +149,8 @@ async def sync_run_log(run_id: str):
|
||||
"id_partener": o.get("id_partener"),
|
||||
"error_message": o.get("error_message"),
|
||||
"missing_skus": o.get("missing_skus"),
|
||||
"factura_numar": o.get("factura_numar"),
|
||||
"factura_serie": o.get("factura_serie"),
|
||||
}
|
||||
for o in orders
|
||||
]
|
||||
@@ -179,6 +183,9 @@ def _format_text_log_from_detail(detail: dict) -> str:
|
||||
if status == "IMPORTED":
|
||||
id_cmd = o.get("id_comanda", "?")
|
||||
lines.append(f"#{number} [{order_date}] {customer} → IMPORTAT (ID: {id_cmd})")
|
||||
elif status == "ALREADY_IMPORTED":
|
||||
id_cmd = o.get("id_comanda", "?")
|
||||
lines.append(f"#{number} [{order_date}] {customer} → DEJA IMPORTAT (ID: {id_cmd})")
|
||||
elif status == "SKIPPED":
|
||||
missing = o.get("missing_skus", "")
|
||||
if isinstance(missing, str):
|
||||
@@ -210,7 +217,12 @@ def _format_text_log_from_detail(detail: dict) -> str:
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
lines.append(f"Finalizat: {imported} importate, {skipped} nemapate, {errors} erori din {total} comenzi{duration_str}")
|
||||
already = run.get("already_imported", 0)
|
||||
new_imp = run.get("new_imported", 0)
|
||||
if already:
|
||||
lines.append(f"Finalizat: {new_imp} importate, {already} deja importate, {skipped} nemapate, {errors} erori din {total} comenzi{duration_str}")
|
||||
else:
|
||||
lines.append(f"Finalizat: {imported} importate, {skipped} nemapate, {errors} erori din {total} comenzi{duration_str}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -240,7 +252,7 @@ async def sync_run_text_log(run_id: str):
|
||||
|
||||
@router.get("/api/sync/run/{run_id}/orders")
|
||||
async def sync_run_orders(run_id: str, status: str = "all", page: int = 1, per_page: int = 50,
|
||||
sort_by: str = "created_at", sort_dir: str = "asc"):
|
||||
sort_by: str = "order_date", sort_dir: str = "desc"):
|
||||
"""Get filtered, paginated orders for a sync run (R1)."""
|
||||
return await sqlite_service.get_run_orders_filtered(run_id, status, page, per_page,
|
||||
sort_by=sort_by, sort_dir=sort_dir)
|
||||
@@ -327,23 +339,37 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
period_end=period_end if period_days == 0 else "",
|
||||
)
|
||||
|
||||
# Enrich imported orders with invoice data from Oracle
|
||||
# Enrich orders with invoice data — prefer SQLite cache, fallback to Oracle
|
||||
all_orders = result["orders"]
|
||||
imported_orders = [o for o in all_orders if o.get("id_comanda")]
|
||||
invoice_data = {}
|
||||
if imported_orders:
|
||||
id_comanda_list = [o["id_comanda"] for o in imported_orders]
|
||||
invoice_data = await asyncio.to_thread(
|
||||
invoice_service.check_invoices_for_orders, id_comanda_list
|
||||
)
|
||||
|
||||
for o in all_orders:
|
||||
idc = o.get("id_comanda")
|
||||
if idc and idc in invoice_data:
|
||||
o["invoice"] = invoice_data[idc]
|
||||
if o.get("factura_numar"):
|
||||
# Use cached invoice data from SQLite
|
||||
o["invoice"] = {
|
||||
"facturat": True,
|
||||
"serie_act": o.get("factura_serie"),
|
||||
"numar_act": o.get("factura_numar"),
|
||||
"total_fara_tva": o.get("factura_total_fara_tva"),
|
||||
"total_tva": o.get("factura_total_tva"),
|
||||
"total_cu_tva": o.get("factura_total_cu_tva"),
|
||||
}
|
||||
else:
|
||||
o["invoice"] = None
|
||||
|
||||
# For orders without cached invoice, check Oracle (only uncached imported orders)
|
||||
uncached_orders = [o for o in all_orders if o.get("id_comanda") and not o.get("invoice")]
|
||||
if uncached_orders:
|
||||
try:
|
||||
id_comanda_list = [o["id_comanda"] for o in uncached_orders]
|
||||
invoice_data = await asyncio.to_thread(
|
||||
invoice_service.check_invoices_for_orders, id_comanda_list
|
||||
)
|
||||
for o in uncached_orders:
|
||||
idc = o.get("id_comanda")
|
||||
if idc and idc in invoice_data:
|
||||
o["invoice"] = invoice_data[idc]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Add shipping/billing name fields + is_different_person flag
|
||||
s_name = o.get("shipping_name") or ""
|
||||
b_name = o.get("billing_name") or ""
|
||||
|
||||
Reference in New Issue
Block a user