Files
xenia-pos-local/local_backend/tests/test_moves.py
T
bonaminandClaude Opus 5.5 0a353c5f97 feat(backend,waiter,manager): moves between tables and orders on the record
Owner 2026-11-23 (plan 6F.4). services/moves.py: every table transfer, lines moved and orders merged
is an order_moves row with its lines (order_move_items: line, quantity then); OrderItem.split_from_id
links a split part to its line, so a part carries the moves made before the split; combining moves
the removed lines' moves to the kept one. Alembic 0048: the tables, the column, and a backfill from
the audit notes written before (note='backfill'). A split part now also keeps the line's price change,
kitchen state, course, cost and names (it used to restart as pending at the menu price).
GET /api/orders/{id}/moves (the order's path, each line's); GET /api/reports/moves (+ export).
Waiter: a moved order's banner (T2 -> T5), moved lines marked, a line's Move history. Manager: the
order's Moves tab, each moved line's path, Reports -> Moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 08:03:11 +02:00

141 lines
6.8 KiB
Python

"""Moves on the record (owner 2026-11-23, plan 6F.4, services/moves.py).
A table transfer, lines moved to another order and two orders merged each leave one move with its
lines; a line's path survives being moved three times, being split (the part carries the moves made
before the split, not after) and being combined; the order shows where it came from; Reports →
Moves lists them.
"""
import pytest
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: [])
@pytest.fixture
def floor(db, shop):
"""Four free tables and the shop's waiter's order put on the first."""
from models.table import Table, TableGroup
g = TableGroup(name="Hall")
db.add(g)
db.flush()
ts = [Table(number=n, group_id=g.id, is_active=True) for n in (1, 2, 3, 4)]
db.add_all(ts)
db.flush()
shop["order"].table_id = ts[0].id
shop["order"].status = "open"
for i in shop["items"]:
i.status = "active"
db.commit()
return ts
def new_order(db, shop, table, n=1):
from models.order import Order, OrderItem
o = Order(opened_by=shop["waiter"].id, table_id=table.id, status="open")
db.add(o)
db.flush()
items = [OrderItem(order_id=o.id, product_id=shop["items"][0].product_id, added_by=shop["waiter"].id, quantity=1, unit_price=4.0)
for _ in range(n)]
db.add_all(items)
db.commit()
return o, items
def test_a_transfer_is_one_move_with_its_lines(client, db, shop, floor):
o = shop["order"]
r = client.post(f"/api/orders/{o.id}/transfer", headers=shop["h"], json={"target_table_id": floor[1].id})
assert r.status_code == 200, r.text
got = client.get(f"/api/orders/{o.id}/moves", headers=shop["h"]).json()
[m] = got["order"]
assert m["kind"] == "table_transfer" and (m["from_table_id"], m["to_table_id"]) == (floor[0].id, floor[1].id)
assert m["from_table"] == "T1" and m["to_table"] == "T2" and m["by_id"] == shop["waiter"].id
assert {i["item_id"] for i in m["items"]} == {i.id for i in shop["items"]}
assert set(got["items"]) == {str(i.id) for i in shop["items"]}
def test_a_line_moved_three_times_shows_every_step(client, db, shop, floor):
o1 = shop["order"]
o2, _ = new_order(db, shop, floor[1])
o3, _ = new_order(db, shop, floor[2])
line = shop["items"][0]
for src, dst in ((o1, o2), (o2, o3), (o3, o1)):
r = client.post(f"/api/orders/{src.id}/move-items", headers=shop["h"], json={"item_ids": [line.id], "target_order_id": dst.id})
assert r.status_code == 200, r.text
path = client.get(f"/api/orders/{o1.id}/moves", headers=shop["h"]).json()["items"][str(line.id)]
assert [(m["from_table"], m["to_table"]) for m in path] == [("T1", "T2"), ("T2", "T3"), ("T3", "T1")]
assert all(m["kind"] == "items_moved" for m in path)
def test_a_split_part_keeps_the_moves_before_the_split_only(client, db, shop, floor):
o1 = shop["order"]
o2, _ = new_order(db, shop, floor[1])
o3, _ = new_order(db, shop, floor[2])
line = shop["items"][0]
line.quantity = 3
line.price_adjustment = -0.5
line.kds_status = "served"
db.commit()
client.post(f"/api/orders/{o1.id}/move-items", headers=shop["h"], json={"item_ids": [line.id], "target_order_id": o2.id})
r = client.post(f"/api/orders/{o2.id}/items/{line.id}/split", headers=shop["h"], json={"quantity": 1})
assert r.status_code == 200, r.text
part = [x for x in r.json() if x["id"] != line.id][0]
assert part["split_from_id"] == line.id
# the part keeps what the line was (its price change, the kitchen state)
from models.order import OrderItem
db.expire_all()
p = db.get(OrderItem, part["id"])
assert p.price_adjustment == -0.5 and p.kds_status == "served"
# the original moves on; the part stays
client.post(f"/api/orders/{o2.id}/move-items", headers=shop["h"], json={"item_ids": [line.id], "target_order_id": o3.id})
part_path = client.get(f"/api/orders/{o2.id}/moves", headers=shop["h"]).json()["items"][str(part["id"])]
assert [(m["from_table"], m["to_table"]) for m in part_path] == [("T1", "T2")]
line_path = client.get(f"/api/orders/{o3.id}/moves", headers=shop["h"]).json()["items"][str(line.id)]
assert [(m["from_table"], m["to_table"]) for m in line_path] == [("T1", "T2"), ("T2", "T3")]
def test_a_merge_brings_the_other_orders_path(client, db, shop, floor):
o1 = shop["order"]
o2, lines2 = new_order(db, shop, floor[1])
client.post(f"/api/orders/{o2.id}/transfer", headers=shop["h"], json={"target_table_id": floor[3].id})
r = client.post(f"/api/orders/{o2.id}/merge", headers=shop["h"], json={"target_order_id": o1.id})
assert r.status_code == 200, r.text
got = client.get(f"/api/orders/{o1.id}/moves", headers=shop["h"]).json()
assert [m["kind"] for m in got["order"]] == ["table_transfer", "orders_merged"]
merged = got["order"][1]
assert (merged["from_order_id"], merged["to_order_id"], merged["from_table"], merged["to_table"]) == (o2.id, o1.id, "T4", "T1")
assert [m["kind"] for m in got["items"][str(lines2[0].id)]] == ["table_transfer", "orders_merged"]
def test_combining_lines_keeps_their_moves(client, db, shop, floor):
o1 = shop["order"]
o2, _ = new_order(db, shop, floor[1])
a, b = shop["items"][0], shop["items"][1]
client.post(f"/api/orders/{o1.id}/move-items", headers=shop["h"], json={"item_ids": [a.id, b.id], "target_order_id": o2.id})
r = client.post(f"/api/orders/{o2.id}/items/combine", headers=shop["h"], json={"item_ids": [a.id, b.id]})
assert r.status_code == 200, r.text
from models.order import OrderMoveItem
db.expire_all()
[mi] = db.query(OrderMoveItem).all()
assert mi.item_id == a.id and mi.quantity == 2
def test_the_moves_report(client, db, shop, floor, make_user, login):
o1 = shop["order"]
o2, _ = new_order(db, shop, floor[1])
client.post(f"/api/orders/{o1.id}/move-items", headers=shop["h"], json={"item_ids": [shop["items"][0].id], "target_order_id": o2.id})
client.post(f"/api/orders/{o2.id}/transfer", headers=shop["h"], json={"target_table_id": floor[2].id})
boss = login(make_user(role="owner"))
rep = client.get("/api/reports/moves", headers=boss).json()
assert rep["count"] == 2 and rep["by_kind"] == {"items_moved": 1, "table_transfer": 1}
moved = next(m for m in rep["moves"] if m["kind"] == "items_moved")
assert moved["value"] == 4.0 and moved["items"][0]["name"] and rep["by_person"][0]["count"] == 2
assert client.get(f"/api/reports/moves?table_id={floor[2].id}", headers=boss).json()["count"] == 1
assert client.get("/api/reports/moves?kind=table_transfer", headers=boss).json()["count"] == 1
csv = client.get("/api/reports/moves/export", headers=boss)
assert csv.status_code == 200 and "items_moved" in csv.text