feat(waiter): a table's earlier orders (platform-v2 step 4.7)
- GET /api/tables/{id}/history: the table's closed / cancelled orders, newest first,
for the waiters of its zone (ordering.orders.open), never older than the history
module's history_days (free tier 31, D-6), module.disabled when history is off;
each order with times, total (cancelled items left out), people and items.
- Waiter: a History tab on the table (and "This table's earlier orders" on a free
table); orders expand to their items; the last answer is kept on the phone and
shown offline, marked as saved; the plan's day limit is shown.
- Tests: test_table_history.py (4), waiter e2e +2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -261,3 +261,69 @@ def update_floorplan(table_id: int, body: TableFloorplanUpdate, db: Session = De
|
||||
db.commit()
|
||||
db.refresh(table)
|
||||
return table
|
||||
|
||||
|
||||
# ── A table's earlier orders (plan step 4.7) ──────────────────────────────────
|
||||
|
||||
def _table_in_zones(db: Session, user: User, table: Table) -> bool:
|
||||
"""The waiter app's table rule: managers every table, staff the tables of their zones
|
||||
(a zone row without a group = every zone)."""
|
||||
if holds(user, "core.dashboard.access"):
|
||||
return True
|
||||
zones = db.query(WaiterZone).filter(WaiterZone.waiter_id == user.id).all()
|
||||
return any(z.group_id is None or z.group_id == table.group_id for z in zones)
|
||||
|
||||
|
||||
from core.access.deps import require as _require # noqa: E402
|
||||
from core.modules.state import module_required as _module_required # noqa: E402
|
||||
|
||||
|
||||
@router.get("/{table_id}/history", dependencies=[Depends(_module_required("history"))])
|
||||
def table_history(table_id: int, limit: int = 20, db: Session = Depends(get_db),
|
||||
user: User = Depends(_require("ordering.orders.open"))):
|
||||
"""The table's finished orders (closed or cancelled), newest first: what the table had
|
||||
earlier today. Never older than the `history` module allows (free tier: 31 days, D-6);
|
||||
`history_days` says how far back that is (null = no limit)."""
|
||||
from sqlalchemy.orm import selectinload
|
||||
from core.errors import XeniaError
|
||||
from core.modules.entitlements import history_floor, limit as module_limit
|
||||
table = db.get(Table, table_id)
|
||||
if table is None:
|
||||
raise XeniaError("pos.table_not_found", table_id=table_id)
|
||||
if not _table_in_zones(db, user, table):
|
||||
raise XeniaError("pos.table_not_yours", table=table.label or f"T{table.number}")
|
||||
q = (db.query(Order).filter(Order.table_id == table_id, Order.status.in_(("closed", "cancelled")))
|
||||
.options(selectinload(Order.items), selectinload(Order.waiters)))
|
||||
floor = history_floor("history")
|
||||
if floor is not None:
|
||||
q = q.filter(Order.opened_at >= floor)
|
||||
orders = q.order_by(Order.opened_at.desc()).limit(max(1, min(limit, 100))).all()
|
||||
people_ids = {w.waiter_id for o in orders for w in o.waiters}
|
||||
people = {u.id: u.nickname or u.full_name or u.username
|
||||
for u in db.query(User).filter(User.id.in_(people_ids)).all()} if people_ids else {}
|
||||
|
||||
def iso(dt):
|
||||
return None if dt is None else (dt.isoformat() + "Z" if dt.tzinfo is None else dt.isoformat())
|
||||
|
||||
def item_out(i):
|
||||
import json as _json
|
||||
try:
|
||||
opts = [o.get("name") for o in _json.loads(i.selected_options or "[]") if isinstance(o, dict) and o.get("name")]
|
||||
except ValueError:
|
||||
opts = []
|
||||
unit = (i.unit_price or 0.0) + (i.price_adjustment or 0.0)
|
||||
return {"id": i.id, "name": i.product_name, "quantity": i.quantity, "unit_price": round(unit, 2),
|
||||
"total": round(unit * i.quantity, 2), "status": i.status, "options": opts, "notes": i.notes}
|
||||
|
||||
def order_out(o):
|
||||
live = [i for i in o.items if i.status != "cancelled"]
|
||||
return {
|
||||
"id": o.id, "status": o.status, "opened_at": iso(o.opened_at), "closed_at": iso(o.closed_at),
|
||||
"total": round(sum(((i.unit_price or 0.0) + (i.price_adjustment or 0.0)) * i.quantity for i in live), 2),
|
||||
"customer_count": o.customer_count,
|
||||
"people": [people[w.waiter_id] for w in o.waiters if w.waiter_id in people],
|
||||
"items": [item_out(i) for i in o.items],
|
||||
}
|
||||
|
||||
return {"table_id": table_id, "history_days": module_limit("history", "history_days"),
|
||||
"orders": [order_out(o) for o in orders]}
|
||||
|
||||
Reference in New Issue
Block a user