fix(sync): detect deleted orders and invoices in ROA
Previously, orders deleted from Oracle (sters=1) remained as IMPORTED in SQLite, and deleted invoices kept stale cache data. Now the refresh button and sync cycle re-verify all imported orders against Oracle: - Deleted orders → marked DELETED_IN_ROA with cleared id_comanda - Deleted invoices → invoice cache fields cleared - New status badge for DELETED_IN_ROA in dashboard and logs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -492,34 +492,73 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
|
||||
@router.post("/api/dashboard/refresh-invoices")
|
||||
async def refresh_invoices():
|
||||
"""Force-refresh invoice status from Oracle for all uninvoiced imported orders."""
|
||||
try:
|
||||
uninvoiced = await sqlite_service.get_uninvoiced_imported_orders()
|
||||
if not uninvoiced:
|
||||
return {"updated": 0, "message": "Nicio comanda de verificat"}
|
||||
"""Force-refresh invoice/order status from Oracle.
|
||||
|
||||
id_comanda_list = [o["id_comanda"] for o in uninvoiced]
|
||||
invoice_data = await asyncio.to_thread(
|
||||
invoice_service.check_invoices_for_orders, id_comanda_list
|
||||
)
|
||||
id_to_order = {o["id_comanda"]: o["order_number"] for o in uninvoiced}
|
||||
updated = 0
|
||||
for idc, inv in invoice_data.items():
|
||||
order_num = id_to_order.get(idc)
|
||||
if order_num and inv.get("facturat"):
|
||||
await sqlite_service.update_order_invoice(
|
||||
order_num,
|
||||
serie=inv.get("serie_act"),
|
||||
numar=str(inv.get("numar_act", "")),
|
||||
total_fara_tva=inv.get("total_fara_tva"),
|
||||
total_tva=inv.get("total_tva"),
|
||||
total_cu_tva=inv.get("total_cu_tva"),
|
||||
data_act=inv.get("data_act"),
|
||||
)
|
||||
updated += 1
|
||||
return {"updated": updated, "checked": len(uninvoiced)}
|
||||
Checks:
|
||||
1. Uninvoiced orders → did they get invoiced?
|
||||
2. Invoiced orders → was the invoice deleted?
|
||||
3. All imported orders → was the order deleted from ROA?
|
||||
"""
|
||||
try:
|
||||
invoices_added = 0
|
||||
invoices_cleared = 0
|
||||
orders_deleted = 0
|
||||
|
||||
# 1. Check uninvoiced → new invoices
|
||||
uninvoiced = await sqlite_service.get_uninvoiced_imported_orders()
|
||||
if uninvoiced:
|
||||
id_comanda_list = [o["id_comanda"] for o in uninvoiced]
|
||||
invoice_data = await asyncio.to_thread(
|
||||
invoice_service.check_invoices_for_orders, id_comanda_list
|
||||
)
|
||||
id_to_order = {o["id_comanda"]: o["order_number"] for o in uninvoiced}
|
||||
for idc, inv in invoice_data.items():
|
||||
order_num = id_to_order.get(idc)
|
||||
if order_num and inv.get("facturat"):
|
||||
await sqlite_service.update_order_invoice(
|
||||
order_num,
|
||||
serie=inv.get("serie_act"),
|
||||
numar=str(inv.get("numar_act", "")),
|
||||
total_fara_tva=inv.get("total_fara_tva"),
|
||||
total_tva=inv.get("total_tva"),
|
||||
total_cu_tva=inv.get("total_cu_tva"),
|
||||
data_act=inv.get("data_act"),
|
||||
)
|
||||
invoices_added += 1
|
||||
|
||||
# 2. Check invoiced → deleted invoices
|
||||
invoiced = await sqlite_service.get_invoiced_imported_orders()
|
||||
if invoiced:
|
||||
id_comanda_list = [o["id_comanda"] for o in invoiced]
|
||||
invoice_data = await asyncio.to_thread(
|
||||
invoice_service.check_invoices_for_orders, id_comanda_list
|
||||
)
|
||||
for o in invoiced:
|
||||
if o["id_comanda"] not in invoice_data:
|
||||
await sqlite_service.clear_order_invoice(o["order_number"])
|
||||
invoices_cleared += 1
|
||||
|
||||
# 3. Check all imported → deleted orders in ROA
|
||||
all_imported = await sqlite_service.get_all_imported_orders()
|
||||
if all_imported:
|
||||
id_comanda_list = [o["id_comanda"] for o in all_imported]
|
||||
existing_ids = await asyncio.to_thread(
|
||||
invoice_service.check_orders_exist, id_comanda_list
|
||||
)
|
||||
for o in all_imported:
|
||||
if o["id_comanda"] not in existing_ids:
|
||||
await sqlite_service.mark_order_deleted_in_roa(o["order_number"])
|
||||
orders_deleted += 1
|
||||
|
||||
checked = len(uninvoiced) + len(invoiced) + len(all_imported)
|
||||
return {
|
||||
"checked": checked,
|
||||
"invoices_added": invoices_added,
|
||||
"invoices_cleared": invoices_cleared,
|
||||
"orders_deleted": orders_deleted,
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e), "updated": 0}
|
||||
return {"error": str(e), "invoices_added": 0}
|
||||
|
||||
|
||||
@router.put("/api/sync/schedule")
|
||||
|
||||
Reference in New Issue
Block a user