"""Owner's choices of plan step 4.8: closing with unpaid items is a venue setting, and unpaid lines of the same item can be combined into one.""" import pytest from tests.test_live import err, owner_h, pair, terminal_login # noqa: F401 (fixtures) from tests.test_pos import burger_line, drawer_printer, menu, on_shift, till # noqa: F401 (fixtures) from tests.test_pos_tables import floor, phone # noqa: F401 (fixtures) @pytest.fixture(autouse=True) def _no_printing(monkeypatch): import routers.orders as orders monkeypatch.setattr(orders, "route_and_print_sync", lambda *a, **k: []) def order_with(client, phone, floor, lines): oid = client.post("/api/orders/", headers=phone["h"], json={"table_id": floor["t1"].id, "order_type": "here"}).json()["id"] for line in lines: assert client.post(f"/api/orders/{oid}/items", headers=phone["h"], json={"lines": [line]}).status_code == 200 return oid def items(db, oid): from models.order import OrderItem db.expire_all() return db.query(OrderItem).filter(OrderItem.order_id == oid).order_by(OrderItem.id).all() def test_closing_with_unpaid_items_follows_the_setting(client, db, menu, phone, floor, owner_h): beer = {"product_id": menu["coffee"].id, "quantity": 2} oid = order_with(client, phone, floor, [beer]) r = client.post(f"/api/orders/{oid}/close", headers=phone["h"]) assert r.status_code == 409 and err(r) == "pos.order_unpaid" and r.json()["error"]["params"]["count"] == 1 assert client.put("/api/settings/orders.close_with_unpaid", headers=owner_h, json={"value": "true"}).status_code == 200 assert client.post(f"/api/orders/{oid}/close", headers=phone["h"]).status_code == 200 assert [i.status for i in items(db, oid)] == ["closed"] def test_combine_the_same_item_ordered_at_different_times(client, db, menu, phone, floor): from models.order import OrderAuditLog beer = {"product_id": menu["coffee"].id, "quantity": 2} oid = order_with(client, phone, floor, [beer, beer, beer]) ids = [i.id for i in items(db, oid)] r = client.post(f"/api/orders/{oid}/items/combine", headers=phone["h"], json={"item_ids": ids}) assert r.status_code == 200, r.text left = items(db, oid) assert [(i.id, i.quantity) for i in left] == [(ids[0], 6)] assert db.query(OrderAuditLog).filter(OrderAuditLog.event_type == "ITEMS_COMBINED").count() == 1 @pytest.mark.parametrize("second,reason", [ (lambda m: {"product_id": m["coffee"].id, "quantity": 1, "notes": "no ice"}, "different"), (lambda m: burger_line(m), "different"), ]) def test_only_lines_that_are_the_same(client, db, menu, phone, floor, second, reason): oid = order_with(client, phone, floor, [{"product_id": menu["coffee"].id, "quantity": 1}, second(menu)]) r = client.post(f"/api/orders/{oid}/items/combine", headers=phone["h"], json={"item_ids": [i.id for i in items(db, oid)]}) assert r.status_code == 422 and err(r) == "order.items_not_combinable" and r.json()["error"]["params"]["reason"] == reason assert len(items(db, oid)) == 2 def test_paid_lines_are_not_combined(client, db, menu, phone, floor): beer = {"product_id": menu["coffee"].id, "quantity": 1} oid = order_with(client, phone, floor, [beer, beer]) first, second = items(db, oid) assert client.post(f"/api/orders/{oid}/pay", headers=phone["h"], json={"item_ids": [first.id], "payment_method": "cash"}).status_code == 200 r = client.post(f"/api/orders/{oid}/items/combine", headers=phone["h"], json={"item_ids": [first.id, second.id]}) assert err(r) == "order.items_not_combinable" and r.json()["error"]["params"]["reason"] == "not_unpaid" def test_lines_know_where_they_came_from(client, db, menu, phone, floor, till): """order_items.source (4.8): the phone's lines are 'waiter', the counter's 'counter' (QR: test_qr_table).""" oid = order_with(client, phone, floor, [{"product_id": menu["coffee"].id, "quantity": 1}]) r = client.post(f"/api/pos/tables/{floor['t1'].id}/items", headers=till["h"], json={"lines": [{"product_id": menu["coffee"].id}]}) assert r.status_code == 201, r.text assert [i.source for i in items(db, oid)] == ["waiter", "counter"] assert [i["source"] for i in client.get(f"/api/orders/{oid}", headers=phone["h"]).json()["items"]] == ["waiter", "counter"] def test_migration_0025_marks_online_lines(tmp_path): from alembic import command from sqlalchemy import create_engine, text from core.schema import alembic_config, upgrade_to_head eng = create_engine(f"sqlite:///{(tmp_path / 'm.db').as_posix()}") with eng.begin() as conn: command.upgrade(alembic_config(conn), "0024_role_permissions") conn.execute(text("insert into orders (id, opened_by, status, source, order_type, kds_status) values (1, 1, 'open', 'online', 'delivery', 'pending'), (2, 1, 'open', 'pos', 'here', 'pending')")) conn.execute(text("insert into order_items (id, order_id, product_id, added_by, quantity, unit_price, product_name, status, kds_status, printed, price_adjustment) " "values (1, 1, 1, 1, 1, 3, 'x', 'active', 'pending', 0, 0), (2, 2, 1, 1, 1, 3, 'x', 'active', 'pending', 0, 0)")) upgrade_to_head(eng) with eng.connect() as conn: rows = conn.execute(text("select id, source from order_items order by id")).fetchall() eng.dispose() assert rows == [(1, "online"), (2, None)]