feat(backend,waiter,manager): prices changed by hand are on the record

Owner 2026-11-23 (plan 6F.3). services/price_changes.py: the venue's switch orders.price_change
(off | before_sending | anytime, default anytime as before), orders.price_change_reason and its
quick reasons; keys ordering.prices.modify (the cart), new modify_sent (a sent or served line) and
view_changes (who sees them). Every by-hand price (waiter cart, a sent line, the counter's %
discount) writes a manual_price PriceEventLog row (before, after, quantity, who, approving manager,
stage, reason) plus a PRICE_CHANGED audit row; before, the sent-line change overwrote the price with
no record. Alembic 0047: event columns; modify_sent for everyone holding modify (and the venues'
role overrides), view_changes for owners and store managers.
Waiter: the reason picker when the venue asks, a sent line's price on modify_sent and the venue's
switch, the line's price history. Manager: Settings -> Operation 'Prices changed by hand', the
changes on the order's lines and history, Reports -> Cancellations, discounts and price changes
(+ export). Counter: no discount when the venue turned price changes off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 07:41:43 +02:00
co-authored by Claude Opus 5.5
parent e3f4f3eeee
commit f87f9ff23c
40 changed files with 1044 additions and 73 deletions
+1 -1
View File
@@ -94,7 +94,7 @@ def test_waiter_cancels_after_manager_pin(client, db, venue, login):
def test_token_is_bound_to_its_key(client, db, venue, make_user, login):
v = venue
token = approve(client, v["waiter_h"], "ordering.prices.modify", make_user(role="owner").username).json()["override_token"]
token = approve(client, v["waiter_h"], "ordering.prices.modify_sent", make_user(role="owner").username).json()["override_token"]
# used for another key: refused, and NOT burned
r = cancel(client, v, v["items"][0], token)
assert err(r) == "access.override_invalid"
+170
View File
@@ -0,0 +1,170 @@
"""Prices changed by hand, on the record (owner 2026-11-23, plan 6F.3, services/price_changes.py).
- The venue's switch `orders.price_change`: off refuses every by-hand price, before_sending refuses
a sent line's, anytime allows both.
- A sent line needs ordering.prices.modify_sent (modify alone is the cart); a manager's PIN is
recorded as the approver.
- `orders.price_change_reason`: a typed price needs a reason; the counter's % discount doesn't.
- Every change is one manual_price event (before → after, quantity, who, stage, reason) and a
PRICE_CHANGED audit row; only holders of ordering.prices.view_changes see them.
"""
import pytest
from conftest import TEST_PIN
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
from tests.test_order_lines import items_of, open_order
from tests.test_pos import burger_line, menu, on_shift, till # noqa: F401 (fixtures)
from tests.test_pos_tables import floor, phone # noqa: F401 (fixtures)
from tests.test_waiter_keys import shop # noqa: F401 (fixture)
@pytest.fixture(autouse=True)
def _no_printing(monkeypatch):
import routers.orders as orders
monkeypatch.setattr(orders, "route_and_print_sync", lambda *a, **k: [])
def setting(db, key, value):
from models.settings import PosSettings
row = db.query(PosSettings).filter(PosSettings.key == key).first()
if row is None:
db.add(PosSettings(key=key, value=value))
else:
row.value = value
db.commit()
def manual_events(db, order_id=None):
from models.pricing import PriceEventLog
db.expire_all()
q = db.query(PriceEventLog).filter(PriceEventLog.event_type == "manual_price")
if order_id is not None:
q = q.filter(PriceEventLog.order_id == order_id)
return q.order_by(PriceEventLog.id).all()
def test_a_sent_line_changed_by_hand_is_recorded(client, db, shop, login):
o, item = shop["order"], shop["items"][0]
item.kds_status = "served"
db.commit()
h = login(shop["new_waiter"](add=("ordering.prices.modify_sent",)), client="waiter")
r = client.patch(f"/api/orders/{o.id}/items/{item.id}/price-adjust", headers=h,
json={"price_adjustment": -1.5, "reason": " regular customer "})
assert r.status_code == 200, r.text
[ev] = manual_events(db, o.id)
assert (ev.order_item_id, ev.price_before, ev.price_after, ev.delta_amount) == (item.id, 4.0, 2.5, -1.5)
assert ev.stage == "served" and ev.quantity == 1 and ev.waiter_note == "regular customer" and ev.approved_by_user_id is None
# a second change records from the changed price, the first stays
r = client.patch(f"/api/orders/{o.id}/items/{item.id}/price-adjust", headers=h, json={"price_adjustment": 0})
assert r.status_code == 200
evs = manual_events(db, o.id)
assert [(e.price_before, e.price_after) for e in evs] == [(4.0, 2.5), (2.5, 4.0)]
from models.order import OrderAuditLog
assert db.query(OrderAuditLog).filter(OrderAuditLog.order_id == o.id, OrderAuditLog.event_type == "PRICE_CHANGED").count() == 2
def test_the_venue_switch(client, db, shop, login):
o, item = shop["order"], shop["items"][0]
url = f"/api/orders/{o.id}/items/{item.id}/price-adjust"
boss = login(shop["owner"])
setting(db, "orders.price_change", "before_sending")
assert err(client.patch(url, headers=boss, json={"price_adjustment": -1})) == "order.price_change_sent_off"
setting(db, "orders.price_change", "off")
assert err(client.patch(url, headers=boss, json={"price_adjustment": -1})) == "order.price_change_off"
setting(db, "orders.price_change", "anytime")
assert client.patch(url, headers=boss, json={"price_adjustment": -1}).status_code == 200
assert len(manual_events(db, o.id)) == 1
def test_a_reason_when_the_venue_asks(client, db, shop, login):
o, item = shop["order"], shop["items"][0]
url = f"/api/orders/{o.id}/items/{item.id}/price-adjust"
h = login(shop["new_waiter"](add=("ordering.prices.modify_sent",)), client="waiter")
setting(db, "orders.price_change_reason", "true")
assert err(client.patch(url, headers=h, json={"price_adjustment": -1})) == "order.price_reason_required"
assert err(client.patch(url, headers=h, json={"price_adjustment": -1, "reason": " "})) == "order.price_reason_required"
assert client.patch(url, headers=h, json={"price_adjustment": -1, "reason": "Burnt a bit"}).status_code == 200
assert manual_events(db, o.id)[0].waiter_note == "Burnt a bit"
def test_a_managers_pin_is_the_approver(client, db, shop, make_user, login):
o, item = shop["order"], shop["items"][0]
boss = make_user(role="owner")
approved = client.post("/api/access/override", headers=shop["h"],
json={"key": "ordering.prices.modify_sent", "username": boss.username, "pin": TEST_PIN})
assert approved.status_code == 200, approved.text
h = dict(shop["h"], **{"X-Xenia-Override": approved.json()["override_token"]})
assert client.patch(f"/api/orders/{o.id}/items/{item.id}/price-adjust", headers=h, json={"price_adjustment": 1}).status_code == 200
[ev] = manual_events(db, o.id)
assert ev.approved_by_user_id == boss.id and ev.applied_by_user_id == shop["waiter"].id
def test_a_price_typed_in_the_cart(client, db, menu, phone, floor, make_user, login):
oid = open_order(client, phone["h"], floor["t1"])
line = {"product_id": menu["coffee"].id, "quantity": 2, "price_adjustment": -0.5, "price_reason": "Friend of the house"}
from models.user import WaiterZone
w = make_user(role="waiter", add=("ordering.prices.modify",))
db.add(WaiterZone(waiter_id=w.id, group_id=None))
db.commit()
h = login(w, "waiter")
r = client.post(f"/api/orders/{oid}/items", headers=h, json={"lines": [line]})
assert r.status_code == 200, r.text
[item] = items_of(db, oid)
[ev] = manual_events(db, oid)
assert ev.order_item_id == item.id and ev.stage == "cart" and ev.quantity == 2
assert (ev.price_before, ev.price_after) == (item.unit_price, item.unit_price - 0.5) and ev.waiter_note == "Friend of the house"
# the venue asks for a reason: a typed price without one is refused, nothing is added
setting(db, "orders.price_change_reason", "true")
assert err(client.post(f"/api/orders/{oid}/items", headers=h, json={"lines": [{**line, "price_reason": None}]})) == "order.price_reason_required"
assert len(items_of(db, oid)) == 1
setting(db, "orders.price_change", "off")
assert err(client.post(f"/api/orders/{oid}/items", headers=h, json={"lines": [line]})) == "order.price_change_off"
def test_the_counters_discount_is_recorded_without_a_reason(client, db, menu, pair, make_user):
from core.timeutil import business_date_of, utcnow
from models.business_day import BusinessDay
from models.shift import WaiterShift
from tests.test_live import terminal_login
token, _ = pair("pos_counter", "Till 2")
boss = make_user(role="owner")
h = {"X-Xenia-Device": token, "Authorization": f"Bearer {terminal_login(client, token, boss)}"}
day = db.query(BusinessDay).filter(BusinessDay.status == "open").first() or BusinessDay(
status="open", opened_at=utcnow(), business_date=business_date_of(utcnow()), opened_by_id=boss.id)
db.add(day)
db.flush()
db.add(WaiterShift(waiter_id=boss.id, business_day_id=day.id))
db.commit()
setting(db, "orders.price_change_reason", "true")
r = client.post("/api/pos/sales", headers=h, json={"lines": [{"product_id": menu["coffee"].id, "discount_pct": 10}], "payments": [{"method": "card"}]})
assert r.status_code == 201, r.text
[ev] = manual_events(db, r.json()["order_id"])
assert ev.stage == "cart" and ev.waiter_note == "−10 %" and ev.delta_amount < 0
def test_only_holders_see_the_changes(client, db, shop, make_user, login):
o, item = shop["order"], shop["items"][0]
h = login(shop["new_waiter"](add=("ordering.prices.modify_sent",)), client="waiter")
client.patch(f"/api/orders/{o.id}/items/{item.id}/price-adjust", headers=h, json={"price_adjustment": -1, "reason": "x"})
seer = login(make_user(role="store_manager"))
blind = login(make_user(role="store_manager", remove=("ordering.prices.view_changes",)))
assert [e["event_type"] for e in client.get(f"/api/pricing/events/order/{o.id}", headers=seer).json()] == ["manual_price"]
assert client.get(f"/api/pricing/events/order/{o.id}", headers=blind).json() == []
got = client.get(f"/api/orders/{o.id}/price-changes", headers=seer).json()
assert len(got) == 1 and got[0]["before"] == 4.0 and got[0]["after"] == 3.0 and got[0]["reason"] == "x" and got[0]["product_name"]
assert client.get(f"/api/orders/{o.id}/price-changes", headers=blind).status_code == 403
rep = client.get(f"/api/reports/price-changes?business_day_id={o.business_day_id}" if o.business_day_id else
"/api/reports/price-changes", headers=seer).json()
assert rep["count"] >= 1 and rep["total_value"] <= -1.0 and rep["by_person"][0]["count"] >= 1
assert client.get("/api/reports/price-changes", headers=blind).status_code == 403
def test_combining_lines_keeps_their_changes(client, db, shop, login):
o, items = shop["order"], shop["items"]
h = login(shop["new_waiter"](add=("ordering.prices.modify_sent",)), client="waiter")
for it in items[:2]:
assert client.patch(f"/api/orders/{o.id}/items/{it.id}/price-adjust", headers=h, json={"price_adjustment": -1}).status_code == 200
r = client.post(f"/api/orders/{o.id}/items/combine", headers=h, json={"item_ids": [items[0].id, items[1].id]})
assert r.status_code == 200, r.text
assert {e.order_item_id for e in manual_events(db, o.id)} == {items[0].id}
assert len(manual_events(db, o.id)) == 2
+3
View File
@@ -98,10 +98,13 @@ def test_revert_payment_is_a_key_a_manager_can_approve(client, db, shop):
def test_price_change_needs_only_the_key_no_venue_switch(client, shop, login):
"""A sent line's price: ordering.prices.modify_sent since 6F.3 (modify alone is for the cart)."""
o, item = shop["order"], shop["items"][0]
url = f"/api/orders/{o.id}/items/{item.id}/price-adjust"
assert err(client.patch(url, headers=shop["h"], json={"price_adjustment": -1})) == "access.override_possible"
h = login(shop["new_waiter"](add=("ordering.prices.modify",)), client="waiter")
assert err(client.patch(url, headers=h, json={"price_adjustment": -1})) == "access.override_possible"
h = login(shop["new_waiter"](add=("ordering.prices.modify_sent",)), client="waiter")
r = client.patch(url, headers=h, json={"price_adjustment": -1})
assert r.status_code == 200, r.text