"""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