From c4fa643eca0b984763f3e61428c01a50fc32bfe3 Mon Sep 17 00:00:00 2001 From: Claude Agent Date: Sun, 15 Mar 2026 21:20:57 +0000 Subject: [PATCH] feat(sync): add order_total field to SQLite tracking Parse order total from GoMag JSON, store in SQLite orders table, and expose via sync run API. Enables total display in mobile flat rows. Co-Authored-By: Claude Opus 4.6 (1M context) --- .gitignore | 1 + api/app/database.py | 4 +++- api/app/routers/sync.py | 1 + api/app/services/order_reader.py | 2 ++ api/app/services/sqlite_service.py | 18 +++++++++++------- api/app/services/sync_service.py | 4 ++++ 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 2b77181..b4c119e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.err *.ERR *.log +/screenshots # Python __pycache__/ diff --git a/api/app/database.py b/api/app/database.py index 104efe8..93048d3 100644 --- a/api/app/database.py +++ b/api/app/database.py @@ -103,7 +103,8 @@ CREATE TABLE IF NOT EXISTS orders ( factura_total_fara_tva REAL, factura_total_tva REAL, factura_total_cu_tva REAL, - invoice_checked_at TEXT + invoice_checked_at TEXT, + order_total REAL ); CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status); CREATE INDEX IF NOT EXISTS idx_orders_date ON orders(order_date); @@ -301,6 +302,7 @@ def init_sqlite(): ("factura_total_tva", "REAL"), ("factura_total_cu_tva", "REAL"), ("invoice_checked_at", "TEXT"), + ("order_total", "REAL"), ]: if col not in order_cols: conn.execute(f"ALTER TABLE orders ADD COLUMN {col} {typedef}") diff --git a/api/app/routers/sync.py b/api/app/routers/sync.py index 6117360..9b1d316 100644 --- a/api/app/routers/sync.py +++ b/api/app/routers/sync.py @@ -149,6 +149,7 @@ 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"), + "order_total": o.get("order_total"), "factura_numar": o.get("factura_numar"), "factura_serie": o.get("factura_serie"), } diff --git a/api/app/services/order_reader.py b/api/app/services/order_reader.py index b45a406..ce85b9d 100644 --- a/api/app/services/order_reader.py +++ b/api/app/services/order_reader.py @@ -54,6 +54,7 @@ class OrderData: items: list = field(default_factory=list) # list of OrderItem billing: OrderBilling = field(default_factory=OrderBilling) shipping: Optional[OrderShipping] = None + total: float = 0.0 payment_name: str = "" delivery_name: str = "" source_file: str = "" @@ -163,6 +164,7 @@ def _parse_order(order_id: str, data: dict, source_file: str) -> OrderData: items=items, billing=billing, shipping=shipping, + total=float(data.get("total", 0) or 0), payment_name=str(payment.get("name", "")) if isinstance(payment, dict) else "", delivery_name=str(delivery.get("name", "")) if isinstance(delivery, dict) else "", source_file=source_file diff --git a/api/app/services/sqlite_service.py b/api/app/services/sqlite_service.py index 64f7778..7d4b573 100644 --- a/api/app/services/sqlite_service.py +++ b/api/app/services/sqlite_service.py @@ -50,7 +50,8 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str, id_partener: int = None, error_message: str = None, missing_skus: list = None, items_count: int = 0, shipping_name: str = None, billing_name: str = None, - payment_method: str = None, delivery_method: str = None): + payment_method: str = None, delivery_method: str = None, + order_total: float = None): """Upsert a single order — one row per order_number, status updated in place.""" db = await get_sqlite() try: @@ -59,8 +60,8 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str, (order_number, order_date, customer_name, status, id_comanda, id_partener, error_message, missing_skus, items_count, last_sync_run_id, shipping_name, billing_name, - payment_method, delivery_method) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + payment_method, delivery_method, order_total) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(order_number) DO UPDATE SET status = CASE WHEN orders.status = 'IMPORTED' AND excluded.status = 'ALREADY_IMPORTED' @@ -80,12 +81,13 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str, billing_name = COALESCE(excluded.billing_name, orders.billing_name), payment_method = COALESCE(excluded.payment_method, orders.payment_method), delivery_method = COALESCE(excluded.delivery_method, orders.delivery_method), + order_total = COALESCE(excluded.order_total, orders.order_total), updated_at = datetime('now') """, (order_number, order_date, customer_name, status, id_comanda, id_partener, error_message, json.dumps(missing_skus) if missing_skus else None, items_count, sync_run_id, shipping_name, billing_name, - payment_method, delivery_method)) + payment_method, delivery_method, order_total)) await db.commit() finally: await db.close() @@ -122,8 +124,8 @@ async def save_orders_batch(orders_data: list[dict]): (order_number, order_date, customer_name, status, id_comanda, id_partener, error_message, missing_skus, items_count, last_sync_run_id, shipping_name, billing_name, - payment_method, delivery_method) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + payment_method, delivery_method, order_total) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(order_number) DO UPDATE SET status = CASE WHEN orders.status = 'IMPORTED' AND excluded.status = 'ALREADY_IMPORTED' @@ -143,6 +145,7 @@ async def save_orders_batch(orders_data: list[dict]): billing_name = COALESCE(excluded.billing_name, orders.billing_name), payment_method = COALESCE(excluded.payment_method, orders.payment_method), delivery_method = COALESCE(excluded.delivery_method, orders.delivery_method), + order_total = COALESCE(excluded.order_total, orders.order_total), updated_at = datetime('now') """, [ (d["order_number"], d["order_date"], d["customer_name"], d["status"], @@ -150,7 +153,8 @@ async def save_orders_batch(orders_data: list[dict]): json.dumps(d["missing_skus"]) if d.get("missing_skus") else None, d.get("items_count", 0), d["sync_run_id"], d.get("shipping_name"), d.get("billing_name"), - d.get("payment_method"), d.get("delivery_method")) + d.get("payment_method"), d.get("delivery_method"), + d.get("order_total")) for d in orders_data ]) diff --git a/api/app/services/sync_service.py b/api/app/services/sync_service.py index 53fa961..dc96e6d 100644 --- a/api/app/services/sync_service.py +++ b/api/app/services/sync_service.py @@ -286,6 +286,7 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None "items_count": len(order.items), "shipping_name": shipping_name, "billing_name": billing_name, "payment_method": payment_method, "delivery_method": delivery_method, + "order_total": order.total or None, "items": order_items_data, }) _log_line(run_id, f"#{order.number} [{order.date or '?'}] {customer} → DEJA IMPORTAT (ID: {id_comanda_roa})") @@ -313,6 +314,7 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None "items_count": len(order.items), "shipping_name": shipping_name, "billing_name": billing_name, "payment_method": payment_method, "delivery_method": delivery_method, + "order_total": order.total or None, "items": order_items_data, }) _log_line(run_id, f"#{order.number} [{order.date or '?'}] {customer} → OMIS (lipsa: {', '.join(missing_skus)})") @@ -365,6 +367,7 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None billing_name=billing_name, payment_method=payment_method, delivery_method=delivery_method, + order_total=order.total or None, ) await sqlite_service.add_sync_run_order(run_id, order.number, "IMPORTED") # Store ROA address IDs (R9) @@ -390,6 +393,7 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None billing_name=billing_name, payment_method=payment_method, delivery_method=delivery_method, + order_total=order.total or None, ) await sqlite_service.add_sync_run_order(run_id, order.number, "ERROR") await sqlite_service.add_order_items(order.number, order_items_data)