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>
This commit is contained in:
@@ -1497,6 +1497,9 @@ def transfer_order(
|
||||
order.table_id = body.target_table_id
|
||||
_audit(db, order_id, "TABLE_TRANSFER", waiter_id=user.id,
|
||||
note=f"Transferred from table {old_table_id} to table {body.target_table_id}")
|
||||
from services import moves # 6F.4: the whole order and its lines, on the record
|
||||
moves.record(db, kind="table_transfer", source=order, target=order, items=list(order.items), user_id=user.id,
|
||||
from_table_id=old_table_id, to_table_id=body.target_table_id)
|
||||
db.commit()
|
||||
db.refresh(order)
|
||||
broadcast_sync("order_updated", {"order_id": order.id, "table_id": order.table_id, "old_table_id": old_table_id, "status": order.status, "action": "transferred"})
|
||||
@@ -1539,6 +1542,8 @@ def merge_order(
|
||||
raise XeniaError("order.same_order")
|
||||
|
||||
# Move all items to target order
|
||||
from services import moves # 6F.4
|
||||
moves.record(db, kind="orders_merged", source=source, target=target, items=list(source.items), user_id=user.id)
|
||||
moved_item_ids = []
|
||||
for item in source.items:
|
||||
item.order_id = target.id
|
||||
@@ -1627,6 +1632,11 @@ def split_item(
|
||||
notes=item.notes,
|
||||
status="active",
|
||||
printed=item.printed,
|
||||
split_from_id=item.id, # 6F.4: the moves the line had come with this part
|
||||
# the rest of what the line was (kitchen, course, price, names, cost) goes with the part
|
||||
kds_status=item.kds_status, course_id=item.course_id, price_adjustment=item.price_adjustment,
|
||||
unit_cost=item.unit_cost, product_name=item.product_name, category_id=item.category_id,
|
||||
category_name=item.category_name,
|
||||
)
|
||||
db.add(new_item)
|
||||
db.commit()
|
||||
@@ -1683,6 +1693,20 @@ def adjust_item_price(
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/{order_id}/moves")
|
||||
def order_moves(order_id: int, db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
"""Where an order and its lines came from (6F.4): {order: [the whole order's transfers and
|
||||
merges], items: {line id: [its moves, its parents' before it was split from them]}}. Each move:
|
||||
kind, at, from / to table (id, name) and order, who, the lines and quantities."""
|
||||
from services import moves
|
||||
order = db.get(Order, order_id)
|
||||
if order is None:
|
||||
raise XeniaError("order.not_found", order_id=order_id)
|
||||
if not _can_access_order(order, user, db):
|
||||
raise XeniaError("order.not_yours")
|
||||
return moves.for_order(db, order)
|
||||
|
||||
|
||||
@router.get("/{order_id}/price-changes")
|
||||
def order_price_changes(order_id: int, db: Session = Depends(get_db),
|
||||
user: User = Depends(require("ordering.prices.view_changes"))):
|
||||
@@ -1962,6 +1986,8 @@ def move_items(
|
||||
if not items:
|
||||
raise XeniaError("order.no_active_items")
|
||||
|
||||
from services import moves # 6F.4
|
||||
moves.record(db, kind="items_moved", source=source, target=target, items=items, user_id=user.id)
|
||||
moved_ids = []
|
||||
for item in items:
|
||||
item.order_id = target.id
|
||||
@@ -2164,6 +2190,8 @@ def combine_items(order_id: int, body: CombineItemsRequest, request: Request, db
|
||||
rest_ids = [i.id for i in rest]
|
||||
db.query(PriceEventLog).filter(PriceEventLog.order_item_id.in_(rest_ids)).update(
|
||||
{PriceEventLog.order_item_id: keep.id}, synchronize_session=False)
|
||||
from services import moves # 6F.4
|
||||
moves.repoint(db, rest_ids, keep.id)
|
||||
for i in rest:
|
||||
db.delete(i)
|
||||
_audit(db, order_id, "ITEMS_COMBINED", waiter_id=user.id, item_ids=[keep.id] + rest_ids,
|
||||
|
||||
@@ -1872,6 +1872,72 @@ def print_prep_zone(
|
||||
# Phase 2L — Discount audit report
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@router.get("/moves")
|
||||
def moves_report(
|
||||
from_dt: Optional[str] = Query(default=None, alias="from"),
|
||||
to_dt: Optional[str] = Query(default=None, alias="to"),
|
||||
business_day_id: Optional[int] = None,
|
||||
kind: Optional[str] = None,
|
||||
by: Optional[int] = None,
|
||||
table_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_reports),
|
||||
):
|
||||
"""Moves between tables and orders (6F.4) in a period or a business day: each move (kind, from /
|
||||
to table and order, who, the lines with their quantity and value), counts per kind and per
|
||||
person. `table_id`: moves from or to that table."""
|
||||
from models.order import OrderMove, OrderItem
|
||||
from services import moves
|
||||
q = db.query(OrderMove)
|
||||
if business_day_id:
|
||||
q = q.filter(OrderMove.business_day_id == business_day_id)
|
||||
elif from_dt and to_dt:
|
||||
q = q.filter(OrderMove.at >= history_since(from_dt, 'reports'), OrderMove.at <= parse_api_datetime(to_dt))
|
||||
if kind:
|
||||
q = q.filter(OrderMove.kind == kind)
|
||||
if by:
|
||||
q = q.filter(OrderMove.by_user_id == by)
|
||||
if table_id:
|
||||
q = q.filter((OrderMove.from_table_id == table_id) | (OrderMove.to_table_id == table_id))
|
||||
rows = q.order_by(OrderMove.at.desc()).all()
|
||||
tables, users = moves._names(db)
|
||||
item_ids = {i.item_id for m in rows for i in m.items}
|
||||
lines = {i.id: i for i in db.query(OrderItem).filter(OrderItem.id.in_(item_ids)).all()} if item_ids else {}
|
||||
names = {i.id: i.product_name for i in lines.values()}
|
||||
out, by_kind, by_person = [], {}, {}
|
||||
for m in rows:
|
||||
row = moves.move_out(m, tables, users, names)
|
||||
row["at"] = _dt(m.at)
|
||||
row["value"] = round(sum(((lines[i.item_id].unit_price or 0) + (lines[i.item_id].price_adjustment or 0)) * (i.quantity or lines[i.item_id].quantity or 0)
|
||||
for i in m.items if i.item_id in lines), 2)
|
||||
out.append(row)
|
||||
by_kind[m.kind] = by_kind.get(m.kind, 0) + 1
|
||||
p = by_person.setdefault(m.by_user_id, {"user_id": m.by_user_id, "name": row["by"], "count": 0})
|
||||
p["count"] += 1
|
||||
return {"moves": out, "count": len(out), "by_kind": by_kind,
|
||||
"by_person": sorted(by_person.values(), key=lambda x: -x["count"])}
|
||||
|
||||
|
||||
@router.get("/moves/export")
|
||||
def moves_export(
|
||||
from_dt: Optional[str] = Query(default=None, alias="from"),
|
||||
to_dt: Optional[str] = Query(default=None, alias="to"),
|
||||
business_day_id: Optional[int] = None,
|
||||
kind: Optional[str] = None,
|
||||
by: Optional[int] = None,
|
||||
table_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_reports),
|
||||
):
|
||||
data = moves_report(from_dt=from_dt, to_dt=to_dt, business_day_id=business_day_id, kind=kind, by=by, table_id=table_id, db=db, user=user)
|
||||
rows = [{"id": m["id"], "at": m["at"], "kind": m["kind"], "from_table": m["from_table"] or "", "to_table": m["to_table"] or "",
|
||||
"from_order": m["from_order_id"], "to_order": m["to_order_id"], "by": m["by"] or "",
|
||||
"lines": "; ".join(f"{i.get('name') or i['item_id']} ×{i['quantity'] if i['quantity'] is not None else '?'}" for i in m["items"]),
|
||||
"value": m["value"]} for m in data["moves"]]
|
||||
fields = ["id", "at", "kind", "from_table", "to_table", "from_order", "to_order", "by", "lines", "value"]
|
||||
return csv_export.response(db, rows, fields, f"moves-{(from_dt or '')[:10]}.csv", times=("at",))
|
||||
|
||||
|
||||
@router.get("/price-changes")
|
||||
def price_changes_report(
|
||||
from_dt: Optional[str] = Query(default=None, alias="from"),
|
||||
|
||||
Reference in New Issue
Block a user