feat(invoice+import): refresh facturi, detalii factura, fix duplicate CODMAT + rollback
- PL/SQL: handle duplicate CODMAT in nom_articole with MAX(id_articol) - import_service: add explicit conn.rollback() on Oracle errors - sync_service: auto-fix stale ERROR orders that exist in Oracle - invoice_service: add data_act (invoice date) from vanzari table - sync router: new POST /api/dashboard/refresh-invoices endpoint - order detail: enrich with invoice data (serie, numar, data factura) - dashboard: refresh invoices button (desktop + mobile icon) - quick map modal: compact single-row layout, pre-populate existing mappings - quick map: link on SKU column instead of CODMAT Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -128,6 +128,7 @@ def import_single_order(order, id_pol: int = None, id_sectie: int = None, app_se
|
||||
"error": None
|
||||
}
|
||||
|
||||
conn = None
|
||||
try:
|
||||
order_number = clean_web_text(order.number)
|
||||
order_date = convert_web_date(order.date)
|
||||
@@ -138,8 +139,8 @@ def import_single_order(order, id_pol: int = None, id_sectie: int = None, app_se
|
||||
|
||||
if database.pool is None:
|
||||
raise RuntimeError("Oracle pool not initialized")
|
||||
with database.pool.acquire() as conn:
|
||||
with conn.cursor() as cur:
|
||||
conn = database.pool.acquire()
|
||||
with conn.cursor() as cur:
|
||||
# Step 1: Process partner — use shipping person data for name
|
||||
id_partener = cur.var(oracledb.DB_TYPE_NUMBER)
|
||||
|
||||
@@ -272,8 +273,24 @@ def import_single_order(order, id_pol: int = None, id_sectie: int = None, app_se
|
||||
error_msg = str(e)
|
||||
result["error"] = error_msg
|
||||
logger.error(f"Oracle error importing order {order.number}: {error_msg}")
|
||||
if conn:
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
result["error"] = str(e)
|
||||
logger.error(f"Error importing order {order.number}: {e}")
|
||||
if conn:
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
if conn:
|
||||
try:
|
||||
database.pool.release(conn)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
@@ -22,7 +22,8 @@ def check_invoices_for_orders(id_comanda_list: list) -> dict:
|
||||
|
||||
cur.execute(f"""
|
||||
SELECT id_comanda, numar_act, serie_act,
|
||||
total_fara_tva, total_tva, total_cu_tva
|
||||
total_fara_tva, total_tva, total_cu_tva,
|
||||
TO_CHAR(data_act, 'YYYY-MM-DD') AS data_act
|
||||
FROM vanzari
|
||||
WHERE id_comanda IN ({placeholders}) AND sters = 0
|
||||
""", params)
|
||||
@@ -34,6 +35,7 @@ def check_invoices_for_orders(id_comanda_list: list) -> dict:
|
||||
"total_fara_tva": float(row[3]) if row[3] else 0,
|
||||
"total_tva": float(row[4]) if row[4] else 0,
|
||||
"total_cu_tva": float(row[5]) if row[5] else 0,
|
||||
"data_act": row[6],
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(f"Invoice check failed (table may not exist): {e}")
|
||||
|
||||
@@ -759,7 +759,8 @@ async def get_uninvoiced_imported_orders() -> list:
|
||||
|
||||
async def update_order_invoice(order_number: str, serie: str = None,
|
||||
numar: str = None, total_fara_tva: float = None,
|
||||
total_tva: float = None, total_cu_tva: float = None):
|
||||
total_tva: float = None, total_cu_tva: float = None,
|
||||
data_act: str = None):
|
||||
"""Cache invoice data from Oracle onto the order record."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
@@ -770,10 +771,11 @@ async def update_order_invoice(order_number: str, serie: str = None,
|
||||
factura_total_fara_tva = ?,
|
||||
factura_total_tva = ?,
|
||||
factura_total_cu_tva = ?,
|
||||
factura_data = ?,
|
||||
invoice_checked_at = datetime('now'),
|
||||
updated_at = datetime('now')
|
||||
WHERE order_number = ?
|
||||
""", (serie, numar, total_fara_tva, total_tva, total_cu_tva, order_number))
|
||||
""", (serie, numar, total_fara_tva, total_tva, total_cu_tva, data_act, order_number))
|
||||
await db.commit()
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
@@ -91,6 +91,40 @@ def _derive_customer_info(order):
|
||||
return shipping_name, billing_name, customer, payment_method, delivery_method
|
||||
|
||||
|
||||
async def _fix_stale_error_orders(existing_map: dict, run_id: str):
|
||||
"""Fix orders stuck in ERROR status that are actually in Oracle.
|
||||
|
||||
This can happen when a previous import committed partially (no rollback on error).
|
||||
If the order exists in Oracle COMENZI, update SQLite status to ALREADY_IMPORTED.
|
||||
"""
|
||||
from ..database import get_sqlite
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute(
|
||||
"SELECT order_number FROM orders WHERE status = 'ERROR'"
|
||||
)
|
||||
error_orders = [row["order_number"] for row in await cursor.fetchall()]
|
||||
fixed = 0
|
||||
for order_number in error_orders:
|
||||
if order_number in existing_map:
|
||||
id_comanda = existing_map[order_number]
|
||||
await db.execute("""
|
||||
UPDATE orders SET
|
||||
status = 'ALREADY_IMPORTED',
|
||||
id_comanda = ?,
|
||||
error_message = NULL,
|
||||
updated_at = datetime('now')
|
||||
WHERE order_number = ? AND status = 'ERROR'
|
||||
""", (id_comanda, order_number))
|
||||
fixed += 1
|
||||
_log_line(run_id, f"#{order_number} → status corectat ERROR → ALREADY_IMPORTED (ID: {id_comanda})")
|
||||
if fixed:
|
||||
await db.commit()
|
||||
logger.info(f"Fixed {fixed} stale ERROR orders that exist in Oracle")
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None) -> dict:
|
||||
"""Run a full sync cycle. Returns summary dict."""
|
||||
global _current_sync
|
||||
@@ -191,6 +225,10 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None
|
||||
validation_service.check_orders_in_roa, min_date, conn
|
||||
)
|
||||
|
||||
# Step 2a-fix: Fix ERROR orders that are actually in Oracle
|
||||
# (can happen if previous import committed partially without rollback)
|
||||
await _fix_stale_error_orders(existing_map, run_id)
|
||||
|
||||
# Step 2b: Validate SKUs (reuse same connection)
|
||||
all_skus = order_reader.get_all_skus(orders)
|
||||
validation = await asyncio.to_thread(validation_service.validate_skus, all_skus, conn)
|
||||
@@ -447,6 +485,7 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None
|
||||
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_updated += 1
|
||||
if invoices_updated:
|
||||
|
||||
Reference in New Issue
Block a user