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
+9 -4
View File
@@ -1,8 +1,9 @@
"""How every report counts sales (plan step 6.12a). One set of rules, so a day adds up the same
in every report, on screen, printed and exported.
- **Sold** = order lines that are `active` (ordered, not paid yet) or `paid`. Cancelled lines,
lines written off at closing (`closed`) and lines on a customer's tab (`tabbed`) are not sales.
- **Sold** = order lines that are `active` (ordered, not paid yet), `paid`, or on a customer's tab
(`tabbed`: sold, not paid; owner 2026-11-19, plan step 6.13c). Cancelled lines and lines written
off at closing (`closed`) are not sales. `on_tab` says how much of the sales is on tabs.
- A line's **value** = (unit price + its price change) × quantity, then **minus what was refunded**
of it (services/refunds.py: refunds are per line and quantity, so they come off the line, the
product, the category, the table, the hour and the day they belong to).
@@ -16,7 +17,7 @@ from __future__ import annotations
from dataclasses import dataclass
SOLD = ("active", "paid")
SOLD = ("active", "paid", "tabbed")
def gross(item, quantity: float | None = None) -> float:
@@ -70,6 +71,7 @@ class Totals:
profit: float = 0.0
has_gap: bool = False
units: float = 0.0 # products' quantity
on_tab: float = 0.0 # the part of revenue + fees on customers' tabs (sold, not paid)
@property
def takings(self) -> float:
@@ -77,6 +79,8 @@ class Totals:
def add(self, line: Line) -> "Totals":
self.refunded += line.refunded
if line.item.status == "tabbed":
self.on_tab += line.value
if line.fee:
self.fees += line.value
return self
@@ -93,7 +97,8 @@ class Totals:
def out(self) -> dict:
r = lambda v: round(v + 0.0, 2) # noqa: E731 (-0.0 → 0.0)
return {"revenue": r(self.revenue), "fees": r(self.fees), "takings": r(self.takings), "refunded": r(self.refunded),
"total_cost": r(self.cost), "trackable_profit": r(self.profit), "has_gap": self.has_gap}
"total_cost": r(self.cost), "trackable_profit": r(self.profit), "has_gap": self.has_gap,
"on_tab": r(self.on_tab)}
def totals(ls) -> Totals:
+103
View File
@@ -0,0 +1,103 @@
"""Tabs in the books (plan step 6.13c, KI-018; owner 2026-11-19: every movement recorded).
- A line put on a customer's tab **was sold but not paid**: the line is `tabbed` (services/sales.py
counts it as sold), and its TabEntry records who, when, the business day and the shift of whoever
did it. It is no money in anyone's hand: it isn't in any drawer.
- A payment on a tab is money received: its TabPayment records when, how (cash / card / other),
who received it, the business day and that person's shift if they are on one. Cash goes in that
shift's drawer, card in its card total. Received by someone with no shift (a manager): it is
theirs in the day's money and in no drawer.
"""
from __future__ import annotations
from sqlalchemy.orm import Session
METHODS = ("cash", "card", "other")
def stamp(db: Session, row, user_id: int) -> None:
"""Set the open business day and the user's running shift on a TabEntry / TabPayment."""
from models.business_day import BusinessDay
from models.shift import WaiterShift
day = db.query(BusinessDay).filter(BusinessDay.status == "open").first()
shift = db.query(WaiterShift).filter(WaiterShift.waiter_id == user_id, WaiterShift.ended_at.is_(None)) \
.order_by(WaiterShift.started_at.desc()).first()
row.business_day_id = day.id if day else (shift.business_day_id if shift else None)
row.shift_id = shift.id if shift else None
def _money(rows) -> dict:
out = {m: 0.0 for m in METHODS}
for p in rows:
m = p.payment_method if p.payment_method in METHODS else "other"
out[m] += p.amount
out = {k: round(v, 2) for k, v in out.items()}
out["total"] = round(sum(out.values()), 2)
out["count"] = len(rows)
return out
def _on_tab(entries) -> dict:
items = 0.0
for e in entries:
it = getattr(e, "_item", None)
items += it.quantity if it is not None else 0
return {"value": round(sum(e.amount for e in entries), 2), "lines": len(entries), "items": items}
def _with_items(db: Session, entries):
from models.order import OrderItem
ids = [e.order_item_id for e in entries if e.order_item_id]
by_id = {i.id: i for i in db.query(OrderItem).filter(OrderItem.id.in_(ids)).all()} if ids else {}
for e in entries:
e._item = by_id.get(e.order_item_id)
return entries
def for_shift(db: Session, shift_id: int) -> dict:
"""{on_tab: {value, lines, items}, received: {cash, card, other, total, count}} of one shift."""
from models.tabs import TabEntry, TabPayment
entries = _with_items(db, db.query(TabEntry).filter(TabEntry.shift_id == shift_id).all())
pays = db.query(TabPayment).filter(TabPayment.shift_id == shift_id).all()
return {"on_tab": _on_tab(entries), "received": _money(pays)}
def for_day(db: Session, day_id: int, names: dict | None = None) -> dict:
"""The day's tabs: what went on them, the money received on them (by method), and every payment
(who received it, in a shift or not)."""
from models.tabs import TabEntry, TabPayment
entries = _with_items(db, db.query(TabEntry).filter(TabEntry.business_day_id == day_id).all())
pays = db.query(TabPayment).filter(TabPayment.business_day_id == day_id).order_by(TabPayment.created_at).all()
names = names or {}
def who(uid):
return names.get(uid) or f"#{uid}"
from core.timeutil import iso_z
return {
"on_tab": _on_tab(entries),
"received": _money(pays),
"received_outside_shifts": _money([p for p in pays if p.shift_id is None]),
"payments": [{
"id": p.id, "tab_id": p.tab_id, "customer_name": p.tab.customer.name if p.tab and p.tab.customer else None,
"amount": p.amount, "method": p.payment_method or "cash", "received_by_id": p.received_by_id,
"received_by_name": who(p.received_by_id), "shift_id": p.shift_id, "at": iso_z(p.created_at),
} for p in pays],
"by_person": _by_person(entries, pays),
}
def _by_person(entries, pays) -> dict:
"""{user_id: {on_tab_value, on_tab_items, received: {cash, card, other}}}."""
out: dict = {}
def e(uid):
return out.setdefault(uid, {"on_tab_value": 0.0, "on_tab_items": 0.0, "received": {m: 0.0 for m in METHODS}})
for t in entries:
x = e(t.created_by_id)
x["on_tab_value"] = round(x["on_tab_value"] + t.amount, 2)
x["on_tab_items"] += t._item.quantity if getattr(t, "_item", None) is not None else 0
for p in pays:
m = p.payment_method if p.payment_method in METHODS else "other"
x = e(p.received_by_id)
x["received"][m] = round(x["received"][m] + p.amount, 2)
return out