- tablet_orders (0034) + services/tablet_orders.py: venue rules (menu_tablet.ordering) with
per-tablet overrides: kitchen or waiter confirms first, no open order open/refuse, limits
(items per order, of one dish, time between orders)
- /api/menu-tablet: quote, order, order/{id}, bill (see + ask), requests accept/decline, ordering
- terminal: Add to the order (POS ProductDialog), cart, note, send, live status, the bill
- waiter: TabletRequests on the table page, tablet_order message theme with Open the table
- manager: Settings -> Menu tablets
- tests: test_tablet_orders.py, menu-tablet unit, menu_tablet.mjs e2e (the done-when)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
9.1 KiB
Python
153 lines
9.1 KiB
Python
"""Ordering from a menu tablet at a table (plan step 6.6, owner decisions D-30)."""
|
|
import pytest
|
|
|
|
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
|
|
from tests.test_menu_tablet import _calls, menu, table, tablet # noqa: F401 (fixtures)
|
|
|
|
|
|
@pytest.fixture
|
|
def day(db, make_user):
|
|
from core.timeutil import business_date_of, utcnow
|
|
from models.business_day import BusinessDay
|
|
now = utcnow()
|
|
d = BusinessDay(status="open", opened_by_id=make_user(role="owner").id, opened_at=now, business_date=business_date_of(now))
|
|
db.add(d)
|
|
db.commit()
|
|
return d
|
|
|
|
|
|
@pytest.fixture
|
|
def waiter(db, make_user, login):
|
|
from models.user import WaiterZone
|
|
w = make_user(role="waiter")
|
|
db.add(WaiterZone(waiter_id=w.id, group_id=None))
|
|
db.commit()
|
|
return {"user": w, "h": login(w, client="waiter")}
|
|
|
|
|
|
@pytest.fixture
|
|
def at_table(client, owner_h, tablet, table, menu, db):
|
|
"""Tablet at table 5, ordering on; the souvlaki has a quick extra (pita)."""
|
|
from models.product import ProductQuickOption
|
|
q = ProductQuickOption(product_id=menu["souvlaki"].id, name="Pita", price=0.5)
|
|
db.add(q)
|
|
db.commit()
|
|
menu["pita"] = q.id
|
|
|
|
def config(**cfg):
|
|
r = client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id, "ordering": True, **cfg}})
|
|
assert r.status_code == 200, r.text
|
|
config()
|
|
tablet["config"] = config
|
|
return tablet
|
|
|
|
|
|
def lines(menu, n=2):
|
|
return [{"product_id": menu["souvlaki"].id, "quantity": n, "quick": [{"id": menu["pita"], "qty": 1}]},
|
|
{"product_id": menu["beer"].id, "quantity": 1}]
|
|
|
|
|
|
def test_done_when_two_items_with_an_option_reach_the_kitchen_and_the_waiter(client, db, at_table, menu, table, day, waiter, monkeypatch):
|
|
"""The done-when: two items with an option from the tablet at table 5; the kitchen gets them,
|
|
the table's order has them, the waiter is told."""
|
|
import routers.orders as orders_api
|
|
from models.message import StaffMessage
|
|
from models.order import Order
|
|
printed = []
|
|
monkeypatch.setattr(orders_api, "route_and_print_sync", lambda order_id, ids, db, **kw: printed.append((order_id, ids)) or [])
|
|
data = client.get("/api/menu-tablet/menu", headers=at_table["h"]).json()
|
|
assert data["table"]["can_order"] is True and data["ordering"]["confirm"] == "kitchen"
|
|
assert str(menu["souvlaki"].id) in data["ordering"]["choices"]
|
|
q = client.post("/api/menu-tablet/quote", headers=at_table["h"], json={"lines": lines(menu)})
|
|
assert q.status_code == 200 and q.json()["total"] == round(2 * (3.5 + 0.5) + 4.5, 2)
|
|
r = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu), "note": "no onions"})
|
|
assert r.status_code == 200, r.text
|
|
assert r.json()["status"] == "sent"
|
|
order = db.query(Order).filter(Order.table_id == table.id).one()
|
|
items = [i for i in order.items if i.status == "active"]
|
|
assert sorted((i.product_id, i.quantity, i.source) for i in items) == sorted([(menu["souvlaki"].id, 2, "tablet"), (menu["beer"].id, 1, "tablet")])
|
|
assert printed and printed[0][0] == order.id and len(printed[0][1]) == 2
|
|
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
|
|
assert msg.message_type == "tablet_order" and str(waiter["user"].id) in msg.target_waiter_ids and "sent to the kitchen" in msg.body
|
|
# guests never send prices or discounts
|
|
cheat = client.post("/api/menu-tablet/quote", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "discount_pct": 100, "unit_price": 0.01}]})
|
|
assert cheat.json()["total"] == 4.5
|
|
|
|
|
|
def test_a_waiter_confirms_first(client, db, at_table, menu, table, day, waiter, monkeypatch):
|
|
import routers.orders as orders_api
|
|
monkeypatch.setattr(orders_api, "route_and_print_sync", lambda *a, **kw: [])
|
|
at_table["config"](order_confirm="waiter")
|
|
r = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu, 1)})
|
|
assert r.json()["status"] == "waiting"
|
|
rid = r.json()["id"]
|
|
from models.order import Order
|
|
assert db.query(Order).filter(Order.table_id == table.id).count() == 0 # nothing in the kitchen yet
|
|
waiting = client.get(f"/api/menu-tablet/requests?table_id={table.id}", headers=waiter["h"]).json()
|
|
assert [w["id"] for w in waiting] == [rid] and waiting[0]["lines"][0]["name"] == "Souvlaki"
|
|
ok = client.post(f"/api/menu-tablet/requests/{rid}/accept", headers=waiter["h"])
|
|
assert ok.status_code == 200 and ok.json()["status"] == "sent" and ok.json()["order_id"]
|
|
assert err(client.post(f"/api/menu-tablet/requests/{rid}/accept", headers=waiter["h"])) == "menu_tablet.order_answered"
|
|
assert client.get(f"/api/menu-tablet/order/{rid}", headers=at_table["h"]).json()["status"] == "sent"
|
|
# a second one, declined with a reason the tablet shows
|
|
import routers.menu_tablet # noqa: F401
|
|
at_table["config"](order_confirm="waiter", order_min_seconds=0)
|
|
r2 = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu, 1)}).json()
|
|
no = client.post(f"/api/menu-tablet/requests/{r2['id']}/decline", headers=waiter["h"], json={"reason": "Kitchen closed"})
|
|
assert no.json()["status"] == "declined"
|
|
assert client.get(f"/api/menu-tablet/order/{r2['id']}", headers=at_table["h"]).json()["reason"] == "Kitchen closed"
|
|
|
|
|
|
def test_limits_from_the_venue_and_the_tablet(client, owner_h, db, at_table, menu, day, monkeypatch):
|
|
import routers.orders as orders_api
|
|
monkeypatch.setattr(orders_api, "route_and_print_sync", lambda *a, **kw: [])
|
|
r = client.put("/api/menu-tablet/ordering", headers=owner_h, json={"confirm": "kitchen", "no_order": "open", "max_items": 3, "max_of_one": 2, "min_seconds": 60})
|
|
assert r.status_code == 200
|
|
too_many = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "quantity": 2}, {"product_id": menu["steak"].id, "quantity": 2}]})
|
|
assert err(too_many) == "menu_tablet.too_many" and too_many.json()["error"]["params"]["most"] == 3
|
|
too_many_of = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "quantity": 3}]})
|
|
assert err(too_many_of) == "menu_tablet.too_many_of"
|
|
assert client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "quantity": 1}]}).status_code == 200
|
|
soon = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "quantity": 1}]})
|
|
assert soon.status_code == 429 and err(soon) == "menu_tablet.too_soon"
|
|
# the tablet's own values win
|
|
at_table["config"](order_min_seconds=0, order_max_of_one=5)
|
|
assert client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": [{"product_id": menu["beer"].id, "quantity": 3}]}).status_code == 200
|
|
|
|
|
|
def test_no_open_order_open_or_refuse(client, owner_h, db, at_table, menu, table, day, waiter, monkeypatch):
|
|
import routers.orders as orders_api
|
|
monkeypatch.setattr(orders_api, "route_and_print_sync", lambda *a, **kw: [])
|
|
at_table["config"](order_no_order="refuse")
|
|
r = client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu, 1)})
|
|
assert err(r) == "menu_tablet.table_closed"
|
|
from models.tablet_order import TabletOrder
|
|
assert db.query(TabletOrder).count() == 0
|
|
# a waiter opens the table: now the tablet adds to it
|
|
from models.order import Order
|
|
db.add(Order(table_id=table.id, opened_by=waiter["user"].id, status="open", order_type="here", business_day_id=day.id))
|
|
db.commit()
|
|
assert client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu, 1)}).json()["status"] == "sent"
|
|
|
|
|
|
def test_the_bill_and_asking_for_it(client, db, at_table, menu, table, day, waiter, monkeypatch):
|
|
import routers.orders as orders_api
|
|
from models.message import StaffMessage
|
|
monkeypatch.setattr(orders_api, "route_and_print_sync", lambda *a, **kw: [])
|
|
assert client.get("/api/menu-tablet/bill", headers=at_table["h"]).json() == {"lines": [], "total": 0, "paid": 0, "due": 0}
|
|
client.post("/api/menu-tablet/order", headers=at_table["h"], json={"lines": lines(menu, 1)})
|
|
bill = client.get("/api/menu-tablet/bill", headers=at_table["h"]).json()
|
|
assert bill["total"] == round(3.5 + 0.5 + 4.5, 2) and bill["due"] == bill["total"] and len(bill["lines"]) == 2
|
|
assert client.post("/api/menu-tablet/bill", headers=at_table["h"]).status_code == 200
|
|
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
|
|
assert msg.body == "Table 5: the guests ask for the bill."
|
|
assert err(client.post("/api/menu-tablet/bill", headers=at_table["h"])) == "menu_tablet.call_too_soon"
|
|
at_table["config"](bill=False)
|
|
assert err(client.get("/api/menu-tablet/bill", headers=at_table["h"])) == "menu_tablet.no_table"
|
|
|
|
|
|
def test_ordering_off_or_not_at_a_table(client, owner_h, tablet, menu):
|
|
r = client.post("/api/menu-tablet/order", headers=tablet["h"], json={"lines": [{"product_id": menu["beer"].id}]})
|
|
assert err(r) == "menu_tablet.no_ordering"
|
|
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["ordering"] is None
|