"""Tabs in the books (plan step 6.13c, KI-018; owner 2026-11-19: every movement recorded). A shift: 3 items made, 2 paid in cash, 1 put on a customer's tab → the drawer expects the 2 (the tab line is sold, not paid). The tab is paid later: part in cash by someone in another shift (their drawer expects it), the rest by card by the owner with no shift (recorded as theirs, in the day, in no drawer). Every report counts the tab line as sold and the tab money as received.""" from datetime import timedelta def _setup(db, make_user): from core.timeutil import business_date_of, utcnow from models.business_day import BusinessDay from models.customers import Customer from models.order import Order, OrderItem from models.product import Category, Product from models.shift import WaiterShift now = utcnow() owner = make_user(role="owner", full_name="Olga Owner") maria = make_user(role="waiter", full_name="Maria W") nikos = make_user(role="store_manager", full_name="Nikos M") anna = make_user(role="store_manager", full_name="Anna M") day = BusinessDay(status="open", opened_by_id=owner.id, opened_at=now - timedelta(hours=3), business_date=business_date_of(now)) cat = Category(name="Drinks") cust = Customer(name="Kostas", created_by_id=owner.id) db.add_all([day, cat, cust]) db.flush() beer = Product(name="Beer", base_price=4.0, primary_category_id=cat.id) db.add(beer) db.flush() shifts = {u.full_name: WaiterShift(waiter_id=u.id, business_day_id=day.id, started_at=now - timedelta(hours=2), starting_cash=10.0) for u in (maria, nikos, anna)} db.add_all(shifts.values()) db.flush() order = Order(opened_by=maria.id, order_type="takeaway", business_day_id=day.id, customer_id=cust.id, opened_at=now - timedelta(hours=1)) db.add(order) db.flush() line = lambda **kw: OrderItem(order_id=order.id, product_id=beer.id, added_by=maria.id, quantity=1, unit_price=4.0, # noqa: E731 added_at=now - timedelta(minutes=50), **kw) paid = [line(status="paid", paid_by=maria.id, paid_at=now, payment_method="cash", paid_in_shift_id=shifts["Maria W"].id) for _ in range(2)] tabbed = line() tabbed.quantity = 2 # the third line: 2 beers, 8.00, on the tab db.add_all(paid + [tabbed]) db.commit() return dict(owner=owner, maria=maria, nikos=nikos, anna=anna, day=day, shifts=shifts, order=order, tabbed=tabbed) def test_a_shift_and_its_tab_in_the_books(client, db, make_user, login): s = _setup(db, make_user) nikos, anna, owner = login(s["nikos"]), login(s["anna"]), login(s["owner"]) # Nikos (on his shift) puts the line on Kostas's tab r = client.post(f"/api/orders/{s['order'].id}/items/{s['tabbed'].id}/tab", headers=nikos) assert r.status_code == 200, r.text tab_id = r.json()["tab_id"] shift = lambda name: client.get(f"/api/shifts/{s['shifts'][name].id}/summary", headers=owner).json() # noqa: E731 m = shift("Maria W") assert m["activity"] == {"orders": 1, "made_items": 4, "made_value": 16.0, "paid_items": 2, "paid_value": 8.0, "tab_items": 0, "tab_value": 0.0} assert (m["cash_sales"], m["tab_cash"], m["net_to_deliver"]) == (8.0, 0.0, 18.0) # 10 starting + the 2 paid; not the tab line n = shift("Nikos M") assert (n["activity"]["tab_items"], n["activity"]["tab_value"], n["net_to_deliver"]) == (2, 8.0, 10.0) # put on a tab: no money # later: Anna (on her shift) takes 5 in cash; the owner, with no shift, takes the rest by card assert client.post(f"/api/tabs/{tab_id}/pay", headers=anna, json={"amount": 5, "payment_method": "cash"}).status_code == 200 assert client.post(f"/api/tabs/{tab_id}/pay", headers=owner, json={"amount": 3, "payment_method": "card"}).status_code == 200 a = shift("Anna M") assert (a["tab_cash"], a["tab_card"], a["net_to_deliver"], a["total_collected"]) == (5.0, 0.0, 15.0, 5.0) day = client.get("/api/business-day/summary", headers=owner).json() t = day["tabs"] assert t["on_tab"] == {"value": 8.0, "lines": 1, "items": 2} assert (t["received"]["cash"], t["received"]["card"], t["received"]["total"]) == (5.0, 3.0, 8.0) assert (t["received_outside_shifts"]["card"], t["received_outside_shifts"]["total"]) == (3.0, 3.0) assert [(p["received_by_name"], p["method"], p["amount"], p["shift_id"] is None) for p in t["payments"]] == \ [("Anna M", "cash", 5.0, False), ("Olga Owner", "card", 3.0, True)] people = {w["waiter_name"]: w for w in day["waiters"]} assert (people["Nikos M"]["on_tab_value"], people["Nikos M"]["on_tab_items"]) == (8.0, 2) assert people["Anna M"]["tab_cash"] == 5.0 and people["Olga Owner"]["tab_card"] == 3.0 assert day["tabbed_revenue"] == 8.0 # the reports: the tab line is sold (16 = 8 paid + 8 on the tab), 8 of it on a tab cur = client.get("/api/reports/business-days/current", headers=owner).json()["business_day"] assert cur["revenue"] == 16.0 and cur["on_tab"] == 8.0, cur assert (cur["tab_received"]["cash"], cur["tab_received"]["card"]) == (5.0, 3.0) def test_other_payment_methods_are_not_drawer_cash(client, db, make_user, login): """Before 6.13c anything not paid by card counted as cash in the drawer (a line on the house too).""" s = _setup(db, make_user) db.query(type(s["tabbed"])).filter_by(id=s["tabbed"].id).update({"status": "paid", "payment_method": "treat", "paid_in_shift_id": s["shifts"]["Maria W"].id}) db.commit() m = client.get(f"/api/shifts/{s['shifts']['Maria W'].id}/summary", headers=login(s["owner"])).json() assert (m["cash_sales"], m["other_sales"], m["net_to_deliver"]) == (8.0, 8.0, 18.0)