"""The table map's live data (plan step 6.8, D-31): for each table a person can see, its place on a floor plan and its state now, the same for the phones, the counter and the manager. state free | open | paid | reserved (free, with a booking due within a few hours) mine the person is one of its order's waiters order {id, opened_at, guests, people (the waiters' names), total, unpaid, ready} seats the table's seats (a suggestion, 4.8) Which tables: the waiter app's rule (routers/tables.list_tables: managers all, staff their zones). Which plans: those of the zones the person sees (and plans without a zone) that hold a table they see, or every plan for a manager. """ from __future__ import annotations from sqlalchemy.orm import selectinload ACTIVE = ("open", "partially_paid", "paid") UNPAID = ("active", "ready") def _iso(v): from core.timeutil import iso_z return iso_z(v) def _total(i) -> float: return (i.unit_price + (i.price_adjustment or 0.0)) * i.quantity def live_tables(db, user) -> list[dict]: from models.order import Order from models.user import User from routers import tables as tables_api from routers.floor_plans import place_out from models.table import Table visible = tables_api.list_tables(include_inactive=False, db=db, user=user) ids = [t.id for t in visible] if not ids: return [] rows = {t.id: t for t in db.query(Table).filter(Table.id.in_(ids)).all()} orders = {o.table_id: o for o in db.query(Order).filter(Order.table_id.in_(ids), Order.status.in_(ACTIVE)) .options(selectinload(Order.items), selectinload(Order.waiters)).all()} people_ids = {w.waiter_id for o in orders.values() for w in o.waiters} names = {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 order_out(o): live = [i for i in o.items if i.status in UNPAID or i.status == "paid"] return { "id": o.id, "status": o.status, "opened_at": _iso(o.opened_at), "guests": o.customer_count, "total": round(sum(_total(i) for i in live), 2), "unpaid": round(sum(_total(i) for i in live if i.status in UNPAID), 2), "ready": sum(1 for i in live if (i.kds_status or "pending") == "done"), "people": [names[w.waiter_id] for w in o.waiters if w.waiter_id in names], } out = [] for t in visible: o = orders.get(t.id) if o is not None: state = "paid" if o.status == "paid" else "open" else: state = "reserved" if t.upcoming_reservation else "free" res = t.upcoming_reservation out.append({ **place_out(rows[t.id]), "state": state, "mine": bool(o and any(w.waiter_id == user.id for w in o.waiters)), "order": order_out(o) if o else None, "reservation": ({"guest_name": res.guest_name, "party_size": res.party_size, "reserved_for": _iso(res.reserved_for)} if res and not o else None), }) return out def visible_plans(db, user, plans, tables) -> list: from core.access.check import holds if holds(user, "core.dashboard.access"): return plans used = {t["plan_id"] for t in tables if t["plan_id"] is not None} return [p for p in plans if p.id in used]