"""Pricing set-up on platform v2 (plan step 6.7): who may change it, conditions the price engine can evaluate, the venue's discount limits, live changes, and a happy-hour rule pricing a sale.""" from tests.test_live import err, owner_h, pair, terminal_login # noqa: F401 (fixtures) from tests.test_pos import menu, till # noqa: F401 (fixtures) def happy_hour(**extra): return {"name": "Happy hour", "action_type": "add_percent", "action_value": -20, "scope": "global", "targets": [{"target_type": "all"}], "conditions": [{"condition_type": "time_range", "params": {"from": "00:00", "to": "23:59"}}], **extra} def test_only_pricing_managers_change_the_rules(client, owner_h, make_user, login): waiter = login(make_user(role="waiter"), client="waiter") assert client.get("/api/pricing/modifiers", headers=waiter).status_code == 200 # the phones read them assert client.post("/api/pricing/modifiers", headers=waiter, json=happy_hour()).status_code == 403 assert client.put("/api/pricing/discount-settings/global", headers=waiter, json={"enabled": False}).status_code == 403 assert client.post("/api/pricing/modifiers", headers=owner_h, json=happy_hour()).status_code == 201 def test_incomplete_conditions_are_refused(client, owner_h): for cond in ({"condition_type": "time_range", "params": {"from": "17:00"}}, {"condition_type": "date_range", "params": {"from": "2026-12-01", "to": "2026-11-01"}}, {"condition_type": "day_of_week", "params": {"days": []}}, {"condition_type": "price_group_active", "params": {}}, {"condition_type": "min_category_qty", "params": {"min": 2}}): r = client.post("/api/pricing/modifiers", headers=owner_h, json=happy_hour(conditions=[cond])) assert err(r) == "pricing.bad_condition", cond deal = {"name": "Beer", "action_type": "free_item", "conditions": [{"condition_type": "order_channel", "params": {"channels": []}}]} assert err(client.post("/api/pricing/deals", headers=owner_h, json=deal)) == "pricing.bad_condition" def test_the_venue_discount_limits(client, owner_h, db): from models.settings import PosSettings assert client.get("/api/pricing/discount-settings/global", headers=owner_h).json() == { "enabled": True, "max_total_value_workday": None, "max_total_value_shift": None, "max_items_per_shift": None} r = client.put("/api/pricing/discount-settings/global", headers=owner_h, json={"enabled": False, "max_total_value_workday": 50, "max_total_value_shift": 12.5, "max_items_per_shift": None}) assert r.status_code == 200 assert client.get("/api/pricing/discount-settings/global", headers=owner_h).json() == { "enabled": False, "max_total_value_workday": 50, "max_total_value_shift": 12.5, "max_items_per_shift": None} # "no limit" is stored empty (it used to be "None", which broke every later discount) row = db.query(PosSettings).filter(PosSettings.key == "discounts.max_items_per_shift").first() assert row is None or row.value == "" assert err(client.put("/api/pricing/discount-settings/global", headers=owner_h, json={"max_items_per_shift": -1})) == "pricing.bad_limit" def test_changes_are_announced(client, owner_h, monkeypatch): import services.ws_bus as bus sent = [] monkeypatch.setattr(bus, "broadcast_sync", lambda t, d, **kw: sent.append(t)) g = client.post("/api/pricing/groups", headers=owner_h, json={"name": "Summer"}).json() client.patch(f"/api/pricing/groups/{g['id']}/toggle", headers=owner_h) assert sent.count("pricing.changed") == 2 def test_a_happy_hour_rule_prices_a_counter_sale(client, owner_h, till, menu): """The done-when: a happy-hour modifier made in the manager prices a POS sale.""" line = {"product_id": menu["coffee"].id, "quantity": 2} before = client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json() assert before["total"] == 6.0 r = client.post("/api/pricing/modifiers", headers=owner_h, json=happy_hour(targets=[{"target_type": "item", "target_ids": [menu["coffee"].id]}])) assert r.status_code == 201, r.text after = client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json() assert after["lines"][0]["unit_price"] == 2.4 and after["total"] == 4.8 # the burger isn't a target: its price stays burger = client.post("/api/pos/quote", headers=till["h"], json={"lines": [{"product_id": menu["burger"].id, "quantity": 1, "prefs": [{"set_id": menu["cook"].id, "choices": [{"id": menu["rare"].id}], "shared": "Fries"}]}]}).json() assert burger["total"] == 10.0 client.patch(f"/api/pricing/modifiers/{r.json()['id']}/toggle", headers=owner_h) # off: back to the menu price assert client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json()["total"] == 6.0