feat: tabs in the books (platform-v2 step 6.13c, KI-018; owner 2026-11-19: every movement recorded)

- a line on a customer's tab is sold, not paid: services/sales.py counts tabbed lines as sold (on_tab apart)
- TabEntry / TabPayment record the business day and the shift of whoever did it (Alembic 0037, rows backfilled with their day);
  services/tab_books.py: per shift and per day, on tabs and tab money received (cash / card / other, who, in a shift or not)
- shift: tab cash goes in the drawer (net_to_deliver, the counted difference, total_collected), tab card in the card total;
  items paid other ways (transfer, on the house) are recorded apart and no longer counted as drawer cash;
  'activity': orders, items made / paid / put on a tab with values
- day summary: per person put on tabs and tab money received, every tab payment with who took it, the part taken outside shifts (in no drawer);
  the day's money: paid, on tabs, sold, paid on tabs, money taken (it used to subtract tabs from paid sales)
- Today: on tabs, paid on tabs; Staff -> Cash handover: paid on tabs; the shift dialog: made / paid / on a tab / paid on tabs
- fix: a line put on a tab was charged without its price change (a discount)
- tests: test_tab_books.py (the done-when), overview unit, e2e manager.mjs (the day records the tab payments; Today shows them)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 16:32:25 +02:00
co-authored by Claude Opus 5.5
parent 93c0dd7d9c
commit 07c9a0aecf
20 changed files with 510 additions and 64 deletions
+49 -16
View File
@@ -32,33 +32,42 @@ def _get_setting(db: Session, key: str, default: str = "true") -> str:
def compute_shift_total(shift_id: int, db: Session) -> float:
"""What the shift took, net of its refunds (3.3e)."""
"""The money the shift took (cash + card: items paid and tab payments), net of its refunds."""
split = compute_shift_payment_split(shift_id, db)
return round(split["cash_sales"] + split["card_sales"] - split["cash_refunds"] - split["card_refunds"], 2)
return round(split["cash_sales"] + split["card_sales"] + split["tab_cash"] + split["tab_card"]
- split["cash_refunds"] - split["card_refunds"], 2)
def compute_shift_payment_split(shift_id: int, db: Session) -> dict:
"""cash_sales / card_sales (items paid in this shift, with their price adjustments, i.e.
discounts) and cash_refunds / card_refunds (money this shift gave back, 3.3e)."""
"""The shift's money by kind (6.13c: every movement apart).
cash_sales / card_sales: items paid in this shift (with their price changes) in cash / by card;
other_sales: items paid any other way (transfer, on the house…): recorded, in no drawer
(until 6.13c they were counted as cash). tab_cash / tab_card / tab_other: payments on
customers' tabs this shift received. cash_refunds / card_refunds: money it gave back (3.3e).
"""
from services import tab_books
from services.refunds import item_value, shift_refunds
items = db.query(OrderItem).filter(
OrderItem.paid_in_shift_id == shift_id,
OrderItem.status == "paid",
).all()
cash = 0.0
card = 0.0
out = {"cash": 0.0, "card": 0.0, "other": 0.0}
for i in items:
amount = item_value(i)
method = (i.payment_method or "cash").lower()
if method == "card":
card += amount
else:
cash += amount
out[method if method in ("cash", "card") else "other"] += item_value(i)
refunds = shift_refunds(db, shift_id)
return {"cash_sales": round(cash, 2), "card_sales": round(card, 2),
tab = tab_books.for_shift(db, shift_id)["received"]
return {"cash_sales": round(out["cash"], 2), "card_sales": round(out["card"], 2), "other_sales": round(out["other"], 2),
"tab_cash": tab["cash"], "tab_card": tab["card"], "tab_other": tab["other"],
"cash_refunds": refunds["cash"], "card_refunds": refunds["card"]}
def expected_cash(split: dict, starting_cash: float | None) -> float:
"""What should be in the drawer: starting cash + cash for items + cash on tabs − cash refunded."""
return round((starting_cash or 0.0) + split["cash_sales"] + split["tab_cash"] - split["cash_refunds"], 2)
def compute_shift_pay(shift: WaiterShift) -> dict:
"""Return duration_hours and shift_pay. shift_pay is None if no rate snapshot."""
now = utcnow()
@@ -94,6 +103,26 @@ def compute_shift_pay(shift: WaiterShift) -> dict:
return {"duration_hours": round(duration_hours, 2), "shift_pay": shift_pay}
def shift_activity(shift: WaiterShift, db: Session) -> dict:
"""What happened in the shift (6.13c): orders opened and items made by this person in it,
items paid in it (by anyone's lines) and items it put on customers' tabs, with their values."""
from models.order import Order
from services import tab_books
from services.sales import gross
end = shift.ended_at or utcnow()
made = db.query(OrderItem).filter(OrderItem.added_by == shift.waiter_id, OrderItem.added_at >= shift.started_at,
OrderItem.added_at <= end, OrderItem.status != "cancelled").all()
paid = db.query(OrderItem).filter(OrderItem.paid_in_shift_id == shift.id, OrderItem.status == "paid").all()
orders = db.query(Order).filter(Order.opened_by == shift.waiter_id, Order.opened_at >= shift.started_at,
Order.opened_at <= end, Order.status != "cancelled").count()
tab = tab_books.for_shift(db, shift.id)["on_tab"]
r = lambda v: round(v, 2) # noqa: E731
return {"orders": orders,
"made_items": sum(i.quantity for i in made), "made_value": r(sum(gross(i) for i in made)),
"paid_items": sum(i.quantity for i in paid), "paid_value": r(sum(gross(i) for i in paid)),
"tab_items": tab["items"], "tab_value": tab["value"]}
def _enrich_shift(shift: WaiterShift, db: Session) -> dict:
w = shift.waiter
wname = (w.full_name or w.username) if w else f"#{shift.waiter_id}"
@@ -125,7 +154,12 @@ def _enrich_shift(shift: WaiterShift, db: Session) -> dict:
"card_sales": split["card_sales"],
"cash_refunds": split["cash_refunds"],
"card_refunds": split["card_refunds"],
"net_to_deliver": round(split["cash_sales"] - split["cash_refunds"] + (shift.starting_cash or 0.0), 2),
"other_sales": split["other_sales"],
"tab_cash": split["tab_cash"],
"tab_card": split["tab_card"],
"tab_other": split["tab_other"],
"net_to_deliver": expected_cash(split, shift.starting_cash),
"activity": shift_activity(shift, db),
"is_active": shift.ended_at is None,
"notes": shift.notes,
"hourly_rate_snapshot": shift.hourly_rate_snapshot,
@@ -136,9 +170,8 @@ def _enrich_shift(shift: WaiterShift, db: Session) -> dict:
"cancellation_value": cancellation_value,
# Phase 2E — discrepancy computed live against cash-only expected (starting_cash + cash_sales)
"counted_cash_end": shift.counted_cash_end,
"cash_discrepancy": round(
shift.counted_cash_end - (split["cash_sales"] - split["cash_refunds"] + (shift.starting_cash or 0.0)), 2
) if shift.counted_cash_end is not None else None,
"cash_discrepancy": round(shift.counted_cash_end - expected_cash(split, shift.starting_cash), 2)
if shift.counted_cash_end is not None else None,
"breaks": [
{"id": b.id, "shift_id": b.shift_id, "started_at": _dt(b.started_at), "ended_at": _dt(b.ended_at)}
for b in shift.breaks