diff --git a/local_backend/routers/reports.py b/local_backend/routers/reports.py index 91bcaf1..7f43b4b 100644 --- a/local_backend/routers/reports.py +++ b/local_backend/routers/reports.py @@ -1063,9 +1063,18 @@ def printer_history( printers_db = {p.id: p for p in db.query(Printer).all()} tables_db = {t.id: (t.label or f"T{t.number}") for t in db.query(Table).all()} + # Cache orders to avoid repeated queries + order_cache = {} + def _get_order(oid): + if oid not in order_cache: + order_cache[oid] = db.query(Order).filter(Order.id == oid).first() + return order_cache[oid] + result = [] + seen_order_totals = {} # order_id → total, so we don't double-count reprints + for log in logs: - order = db.query(Order).filter(Order.id == log.order_id).first() + order = _get_order(log.order_id) printer = printers_db.get(log.printer_id) try: item_ids = json.loads(log.item_ids) @@ -1077,6 +1086,18 @@ def printer_history( if oi: pname = oi.product.name if oi.product else f"#{oi.product_id}" items.append({"name": pname, "quantity": oi.quantity}) + + # Compute full order total (all active+paid items, not just printed slice) + order_total = None + if order: + order_total = sum( + i.unit_price * i.quantity + for i in order.items + if i.status in ("active", "paid") + ) + if log.success and log.order_id not in seen_order_totals: + seen_order_totals[log.order_id] = order_total + result.append({ "id": log.id, "printed_at": _dt(log.printed_at), @@ -1086,11 +1107,14 @@ def printer_history( "items": items, "success": log.success, "error_message": log.error_message, + "order_total": round(order_total, 2) if order_total is not None else None, }) total = len(result) failed = sum(1 for r in result if not r["success"]) - return {"logs": result, "total": total, "failed": failed} + # Sum unique order totals (avoid inflating on reprints of the same order) + total_amount = round(sum(seen_order_totals.values()), 2) if seen_order_totals else None + return {"logs": result, "total": total, "failed": failed, "total_amount": total_amount} # --------------------------------------------------------------------------- diff --git a/manager_dashboard/src/pages/reports/operations/PrinterHistory.jsx b/manager_dashboard/src/pages/reports/operations/PrinterHistory.jsx index 536c470..9fe3e00 100644 --- a/manager_dashboard/src/pages/reports/operations/PrinterHistory.jsx +++ b/manager_dashboard/src/pages/reports/operations/PrinterHistory.jsx @@ -289,16 +289,16 @@ export default function PrinterHistory() { const logs = data?.logs || [] const total = data?.total || 0 const failed = data?.failed || 0 + // total_amount comes pre-computed from the backend (unique orders, no double-counting reprints) + const totalAmount = data?.total_amount ?? null // Derived stats for StatCards + Print modal const itemCounts = {} const waiterCounts = {} let totalItemQty = 0 - let totalAmount = null logs.forEach(l => { if (l.waiter) waiterCounts[l.waiter] = (waiterCounts[l.waiter] || 0) + 1 - if (l.order_total != null) totalAmount = (totalAmount || 0) + l.order_total ;(l.items || []).forEach(i => { itemCounts[i.name] = (itemCounts[i.name] || 0) + i.quantity totalItemQty += i.quantity