Files
xenia-pos-local/local_backend/services/floor_live.py
T
bonaminandClaude Opus 5.5 f4aae6abbc feat: the table map (platform-v2 step 6.8, D-31)
- floor_plans / floor_items (0035), tables' plan_id + width/height/rotation/shape;
  /api/floor-plans (set-up, catalog.tables.manage, floor_plans.changed) and
  /api/floor-plans/live (each table's state, guests, waiters, time; zone rule)
- @xenia/floorplan: the drawing (Floor, Item, TableShape, FloorView) and its geometry
  (grid snap in whole cells, outlines, chairs), tested
- manager: Management -> Tables -> Floor plans (editor: drag, grid, irregular floor,
  walls/bar/labels/areas, background picture), Tables page List / Map
- counter POS: Tables List / Map; waiter: Map mode, the list stays the default
- tests: test_floor_plans.py, floor + geometry unit tests, manager/waiter/terminal e2e

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 17:00:45 +02:00

81 lines
3.3 KiB
Python

"""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]