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:
@@ -292,6 +292,12 @@ def business_day_summary(
|
||||
"shift_pay": None,
|
||||
# distinct tables served (not order count)
|
||||
"tables_served": 0,
|
||||
# 6.13c: lines this person put on customers' tabs, and tab money they received
|
||||
"on_tab_items": 0.0,
|
||||
"on_tab_value": 0.0,
|
||||
"tab_cash": 0.0,
|
||||
"tab_card": 0.0,
|
||||
"tab_other": 0.0,
|
||||
}
|
||||
return waiter_stats[uid]
|
||||
|
||||
@@ -331,6 +337,15 @@ def business_day_summary(
|
||||
else:
|
||||
e["other"] += val
|
||||
|
||||
# 6.13c: tabs: what went on them and the money received on them, per person and for the day
|
||||
from services import tab_books
|
||||
tabs_day = tab_books.for_day(db, day.id, {uid: (u.full_name or u.nickname or u.username) for uid, u in users_map.items()})
|
||||
for uid, x in tabs_day.pop("by_person").items():
|
||||
e = _waiter_entry(uid)
|
||||
e["on_tab_items"] = x["on_tab_items"]
|
||||
e["on_tab_value"] = x["on_tab_value"]
|
||||
e["tab_cash"], e["tab_card"], e["tab_other"] = x["received"]["cash"], x["received"]["card"], x["received"]["other"]
|
||||
|
||||
from routers.shifts import compute_shift_pay
|
||||
for shift in shifts:
|
||||
e = _waiter_entry(shift.waiter_id)
|
||||
@@ -494,7 +509,7 @@ def business_day_summary(
|
||||
expenses_paid_today = round(sum(e.paid_amount for e in expenses_today), 2)
|
||||
expenses_due = round(expenses_total - expenses_paid_today, 2)
|
||||
|
||||
# TABBED REVENUE (items moved to tabs — not yet collected)
|
||||
# Lines of today's orders on customers' tabs: sold, not paid (6.13c)
|
||||
tabbed_items = [i for i in all_items if i.status == "tabbed"]
|
||||
tabbed_revenue = round(sum(gross(i) for i in tabbed_items), 2)
|
||||
|
||||
@@ -545,6 +560,9 @@ def business_day_summary(
|
||||
"expenses_paid_today": expenses_paid_today,
|
||||
"expenses_due": expenses_due,
|
||||
"tabbed_revenue": tabbed_revenue,
|
||||
# 6.13c: put on tabs today (by anyone), tab money received today (by method; and the part
|
||||
# received by people with no shift: in no drawer), every tab payment with who received it
|
||||
"tabs": tabs_day,
|
||||
"net_estimate": net_estimate,
|
||||
"net_has_unknowns": net_has_unknowns,
|
||||
}
|
||||
|
||||
@@ -2023,6 +2023,7 @@ def put_item_on_tab(
|
||||
user: User = Depends(require("tabs.manage")), # also needs the tabs module on (6.13a)
|
||||
):
|
||||
from models.tabs import Tab, TabEntry
|
||||
from services.sales import gross
|
||||
|
||||
order = db.query(Order).filter(Order.id == order_id).first()
|
||||
if not order:
|
||||
@@ -2051,10 +2052,12 @@ def put_item_on_tab(
|
||||
tab_id=tab.id,
|
||||
order_id=order_id,
|
||||
order_item_id=item_id,
|
||||
amount=round(item.unit_price * item.quantity, 2),
|
||||
amount=round(gross(item), 2), # with its price change (a discount), as services/sales.py values it (6.13c)
|
||||
description=description,
|
||||
created_by_id=user.id,
|
||||
)
|
||||
from services import tab_books
|
||||
tab_books.stamp(db, entry, user.id) # 6.13c: sold, not paid: the day and the shift it happened in
|
||||
db.add(entry)
|
||||
|
||||
# Mark item as tabbed — distinct from active/paid/cancelled
|
||||
|
||||
@@ -2,6 +2,7 @@ from core.access.queries import holders_filter
|
||||
from core.errors import XeniaError
|
||||
from core.modules.entitlements import clamp_start, history_floor, history_since, limit
|
||||
from core.timeutil import business_date_of, business_date_window, parse_api_datetime, venue_strftime, to_venue, utcnow
|
||||
from services import tab_books
|
||||
import csv
|
||||
import io
|
||||
import json
|
||||
@@ -1252,6 +1253,7 @@ def current_business_day(
|
||||
"opened_at": _dt(day.opened_at),
|
||||
**money,
|
||||
"refunds": day_refunds(db, day.id),
|
||||
"tab_received": tab_books.for_day(db, day.id)["received"], # 6.13c: money paid on tabs today
|
||||
"orders_closed": len(closed_orders),
|
||||
"orders_open": len(open_orders),
|
||||
"active_waiters": len(active_shifts),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -20,12 +20,13 @@ from models.user import User
|
||||
from schemas.tabs import TabOut, TabEntryOut, TabPaymentOut, TabPayRequest, TabForgiveRequest, TabCloseRequest
|
||||
from core.access.deps import require, require_any
|
||||
from core.errors import XeniaError
|
||||
from services import tab_books
|
||||
from services.ws_bus import broadcast_sync
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
MANAGE = Depends(require("tabs.manage"))
|
||||
METHODS = ("cash", "card", "other")
|
||||
METHODS = tab_books.METHODS
|
||||
|
||||
|
||||
def changed():
|
||||
@@ -150,6 +151,7 @@ def add_tab_entry(
|
||||
tab_id=tab_id, order_id=order_id, order_item_id=order_item_id,
|
||||
amount=amount, description=description, created_by_id=user.id,
|
||||
)
|
||||
tab_books.stamp(db, entry, user.id)
|
||||
db.add(entry)
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
@@ -182,6 +184,7 @@ def pay_tab(
|
||||
payment_method=method, notes=body.notes,
|
||||
received_by_id=user.id,
|
||||
)
|
||||
tab_books.stamp(db, payment, user.id) # 6.13c: the day and the receiver's shift (their drawer)
|
||||
db.add(payment)
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
|
||||
Reference in New Issue
Block a user