"""One more, and a sent line's quantity (owner 2026-11-23, plan 6F.5). - POST /api/orders/{id}/items/{item}/again: a new line like the old one, priced now, printed as new; only with the product's one_more_enabled, from active / paid / tabbed lines in pieces. - POST /api/orders/{id}/items/{item}/quantity: the venue's switch (off | add_only | add_and_remove), the key ordering.items.change_quantity; up changes the line only (no ticket); down cuts a cancelled part ("quantity lowered") and prints a cancellation only when asked. """ import json import pytest from tests.test_live import err from tests.test_waiter_keys import shop # noqa: F401 (fixture) @pytest.fixture def printed(monkeypatch): """What went to the kitchen: new lines (route_and_print_sync) and cancellations.""" import routers.orders as orders got = {"tickets": [], "cancellations": []} monkeypatch.setattr(orders, "route_and_print_sync", lambda *a, **k: (got["tickets"].append(a), [])[1]) monkeypatch.setattr(orders, "print_cancellation_ticket", lambda order_id, ids: got["cancellations"].append(list(ids))) return got 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 lines(db, order_id): from models.order import OrderItem db.expire_all() return db.query(OrderItem).filter(OrderItem.order_id == order_id).order_by(OrderItem.id).all() def test_one_more_needs_the_products_switch(client, db, shop, printed): o, item = shop["order"], shop["items"][0] url = f"/api/orders/{o.id}/items/{item.id}/again" assert err(client.post(url, headers=shop["h"], json={})) == "order.one_more_off" item.product.one_more_enabled = True item.notes = "no ice" item.selected_options = json.dumps([{"id": None, "name": "Sweet", "price_delta": 0.0, "type": "quick"}]) db.commit() before = len(lines(db, o.id)) r = client.post(url, headers=shop["h"], json={"quantity": 2}) assert r.status_code == 200, r.text new = lines(db, o.id)[-1] assert len(lines(db, o.id)) == before + 1 and new.id != item.id assert (new.product_id, new.quantity, new.notes, new.status) == (item.product_id, 2, "no ice", "active") assert json.loads(new.selected_options)[0]["name"] == "Sweet" and new.unit_price == 4.0 assert len(printed["tickets"]) == 1 # sent to the kitchen like any new line # from a paid line too; never from a cancelled one paid = shop["items"][2] assert client.post(f"/api/orders/{o.id}/items/{paid.id}/again", headers=shop["h"], json={}).status_code == 200 paid.status = "cancelled" db.commit() assert err(client.post(f"/api/orders/{o.id}/items/{paid.id}/again", headers=shop["h"], json={})) == "order.one_more_line" def test_one_more_needs_ordering_orders_open(client, db, shop, login, printed): o, item = shop["order"], shop["items"][0] item.product.one_more_enabled = True db.commit() h = login(shop["new_waiter"](remove=("ordering.orders.open",)), client="waiter") assert err(client.post(f"/api/orders/{o.id}/items/{item.id}/again", headers=h, json={})) == "access.denied" def test_quantity_follows_the_venue_and_the_key(client, db, shop, login, printed): o, item = shop["order"], shop["items"][0] url = f"/api/orders/{o.id}/items/{item.id}/quantity" assert err(client.post(url, headers=shop["h"], json={"quantity": 3})) == "order.quantity_change_off" setting(db, "orders.quantity_change", "add_only") # a waiter without the key: a manager may approve it assert err(client.post(url, headers=shop["h"], json={"quantity": 3})) == "access.override_possible" h = login(shop["new_waiter"](add=("ordering.items.change_quantity",)), client="waiter") r = client.post(url, headers=h, json={"quantity": 3}) assert r.status_code == 200, r.text assert lines(db, o.id)[0].quantity == 3 and printed["tickets"] == [] and printed["cancellations"] == [] assert err(client.post(url, headers=h, json={"quantity": 2})) == "order.quantity_remove_off" from models.order import OrderAuditLog log = db.query(OrderAuditLog).filter(OrderAuditLog.event_type == "QUANTITY_CHANGED").one() assert log.note.startswith("1 → 3") def test_lowering_cancels_the_difference_and_asks_about_the_ticket(client, db, shop, login, printed): o, item = shop["order"], shop["items"][0] item.quantity = 5 db.commit() setting(db, "orders.quantity_change", "add_and_remove") url = f"/api/orders/{o.id}/items/{item.id}/quantity" # the key, but not the cancel key: a manager's approval for the cancel part h = login(shop["new_waiter"](add=("ordering.items.change_quantity",), remove=("ordering.orders.cancel",)), client="waiter") assert err(client.post(url, headers=h, json={"quantity": 3})) == "access.override_possible" h = login(shop["new_waiter"](add=("ordering.items.change_quantity", "ordering.orders.cancel")), client="waiter") r = client.post(url, headers=h, json={"quantity": 3, "print_cancellation": False}) assert r.status_code == 200, r.text part_id = r.json()["cancelled_item_id"] rows = {i.id: i for i in lines(db, o.id)} assert rows[item.id].quantity == 3 and rows[item.id].status == "active" part = rows[part_id] assert (part.quantity, part.status, part.cancel_reason, part.split_from_id) == (2, "cancelled", "quantity lowered", item.id) assert printed["cancellations"] == [] r = client.post(url, headers=h, json={"quantity": 2, "print_cancellation": True}) assert printed["cancellations"] == [[r.json()["cancelled_item_id"]]] def test_only_unpaid_lines_in_pieces(client, db, shop, printed, login): setting(db, "orders.quantity_change", "add_and_remove") o = shop["order"] paid = shop["items"][2] boss = login(shop["owner"]) assert err(client.post(f"/api/orders/{o.id}/items/{paid.id}/quantity", headers=boss, json={"quantity": 2})) == "order.quantity_line" item = shop["items"][0] item.product.unit_type = "kg" db.commit() assert err(client.post(f"/api/orders/{o.id}/items/{item.id}/quantity", headers=boss, json={"quantity": 2})) == "order.quantity_line"