Files
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

134 lines
6.1 KiB
Python

"""Moves on the record (owner 2026-11-23, plan step 6F.4).
Every move between tables or orders is an `OrderMove` row with the lines that went
(`OrderMoveItem`: the line and its quantity then):
- `table_transfer`: the whole order went to another (free) table (POST /api/orders/{id}/transfer);
- `items_moved`: chosen lines went to another open order (POST /api/orders/{id}/move-items);
- `orders_merged`: an order was joined into another (POST /api/orders/{id}/merge).
A line keeps its id when it moves, so its path is its own moves. A line cut from another
(`OrderItem.split_from_id`: a split, later a lowered quantity or a split payment) also carries
the moves its parent had **before** the cut, however many times that happened. Combining lines
moves the removed lines' moves to the line that stays (routers/orders.combine_items).
Readers: GET /api/orders/{id}/moves (the order's path and each line's), Reports → Moves
(GET /api/reports/moves). The old OrderAuditLog rows stay as they were.
"""
from __future__ import annotations
KINDS = ("table_transfer", "items_moved", "orders_merged")
def record(db, *, kind: str, source, target, items, user_id: int | None, from_table_id=None, to_table_id=None,
approved_by: int | None = None):
"""One move (flushed with the caller's commit). `source` / `target`: orders; `items`: lines."""
from models.order import OrderMove, OrderMoveItem
assert kind in KINDS, kind
move = OrderMove(
kind=kind, from_order_id=getattr(source, "id", None), to_order_id=getattr(target, "id", None),
from_table_id=from_table_id if from_table_id is not None else getattr(source, "table_id", None),
to_table_id=to_table_id if to_table_id is not None else getattr(target, "table_id", None),
by_user_id=user_id, approved_by_user_id=approved_by,
business_day_id=getattr(target, "business_day_id", None) or getattr(source, "business_day_id", None),
)
move.items = [OrderMoveItem(item_id=i.id, quantity=i.quantity) for i in items]
db.add(move)
return move
def repoint(db, from_ids, to_id: int) -> None:
"""Lines removed by a combine: their moves (and children) now belong to the line kept."""
from models.order import OrderItem, OrderMoveItem
from_ids = [i for i in from_ids if i != to_id]
if not from_ids:
return
for mi in db.query(OrderMoveItem).filter(OrderMoveItem.item_id.in_(from_ids)).all():
taken = db.query(OrderMoveItem).filter(OrderMoveItem.move_id == mi.move_id, OrderMoveItem.item_id == to_id).first()
if taken is None:
db.add(OrderMoveItem(move_id=mi.move_id, item_id=to_id, quantity=mi.quantity))
else:
taken.quantity = (taken.quantity or 0) + (mi.quantity or 0)
db.delete(mi)
db.query(OrderItem).filter(OrderItem.split_from_id.in_(from_ids)).update(
{OrderItem.split_from_id: to_id}, synchronize_session=False)
def _lineage(db, item):
"""[(line id, moves up to when) …]: the line itself (all its moves), then each parent up to the
moment the child was cut from it."""
from models.order import OrderItem
out, seen = [(item.id, None)], {item.id}
child = item
while child.split_from_id and child.split_from_id not in seen:
parent = db.get(OrderItem, child.split_from_id)
if parent is None:
break
out.append((parent.id, child.added_at))
seen.add(parent.id)
child = parent
return out
def item_moves(db, item) -> list:
"""The moves a line went through, oldest first (its parents' before it was cut from them)."""
from models.order import OrderMove, OrderMoveItem
found = {}
for line_id, until in _lineage(db, item):
q = db.query(OrderMove).join(OrderMoveItem, OrderMoveItem.move_id == OrderMove.id).filter(OrderMoveItem.item_id == line_id)
if until is not None:
q = q.filter(OrderMove.at <= until)
for m in q.all():
found[m.id] = m
return sorted(found.values(), key=lambda m: (m.at, m.id))
def order_moves(db, order) -> list:
"""The whole order's path: table transfers and merges into it, and the orders merged into it
(their own transfers too), oldest first."""
from models.order import OrderMove
ids, todo, found = set(), [order.id], {}
while todo:
oid = todo.pop()
if oid in ids:
continue
ids.add(oid)
for m in db.query(OrderMove).filter(OrderMove.to_order_id == oid,
OrderMove.kind.in_(("table_transfer", "orders_merged"))).all():
found[m.id] = m
if m.kind == "orders_merged" and m.from_order_id:
todo.append(m.from_order_id)
return sorted(found.values(), key=lambda m: (m.at, m.id))
def _names(db):
from models.table import Table
from models.user import User
tables = {t.id: (t.label or f"T{t.number}") for t in db.query(Table).all()}
users = {u.id: (u.nickname or u.full_name or u.username) for u in db.query(User).all()}
return tables, users
def move_out(m, tables, users, item_names=None) -> dict:
return {
"id": m.id, "kind": m.kind, "at": m.at,
"from_order_id": m.from_order_id, "to_order_id": m.to_order_id,
"from_table_id": m.from_table_id, "to_table_id": m.to_table_id,
"from_table": tables.get(m.from_table_id), "to_table": tables.get(m.to_table_id),
"by_id": m.by_user_id, "by": users.get(m.by_user_id),
"approved_by": users.get(m.approved_by_user_id),
"items": [{"item_id": i.item_id, "quantity": i.quantity,
**({"name": item_names.get(i.item_id)} if item_names is not None else {})} for i in m.items],
}
def for_order(db, order) -> dict:
"""GET /api/orders/{id}/moves: {order: [moves of the whole order], items: {line id: [moves]}}
(only lines that moved)."""
tables, users = _names(db)
per_item = {}
for item in order.items:
ms = item_moves(db, item)
if ms:
per_item[str(item.id)] = [move_out(m, tables, users) for m in ms]
return {"order": [move_out(m, tables, users) for m in order_moves(db, order)], "items": per_item}