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

122 lines
6.0 KiB
Python

"""Moves on the record (owner 2026-11-23, plan 6F.4, services/moves.py).
- order_moves / order_move_items: every table transfer, lines moved, orders merged, with its lines.
- order_items.split_from_id: the line a line was cut from (a split), so its path follows it.
- Backfill from the audit log written before 6F.4 (notes "Transferred from table A to table B",
"Moved to order #N (table T)", "Items merged from order #N (table T)"): one move each, marked
note='backfill'. A transfer's lines are the order's lines added before it; quantities unknown.
Revision ID: 0048_order_moves
Revises: 0047_price_changes
Create Date: 2026-11-23 15:00:00
"""
import json
import re
from alembic import op
import sqlalchemy as sa
from core.timeutil import UTCDateTime
revision = '0048_order_moves'
down_revision = '0047_price_changes'
branch_labels = None
depends_on = None
_TABLE = r"(?:table )?(\d+|None)"
def _int(v):
return int(v) if v not in (None, "None", "") else None
def upgrade() -> None:
op.create_table(
'order_moves',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('kind', sa.String(), nullable=False),
sa.Column('from_order_id', sa.Integer(), sa.ForeignKey('orders.id'), nullable=True),
sa.Column('to_order_id', sa.Integer(), sa.ForeignKey('orders.id'), nullable=True),
sa.Column('from_table_id', sa.Integer(), sa.ForeignKey('tables.id'), nullable=True),
sa.Column('to_table_id', sa.Integer(), sa.ForeignKey('tables.id'), nullable=True),
sa.Column('by_user_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('approved_by_user_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('business_day_id', sa.Integer(), sa.ForeignKey('business_days.id'), nullable=True),
sa.Column('at', UTCDateTime(), nullable=False),
sa.Column('note', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id'),
)
for col in ('id', 'kind', 'from_order_id', 'to_order_id', 'business_day_id', 'at'):
op.create_index(f'ix_order_moves_{col}', 'order_moves', [col])
op.create_table(
'order_move_items',
sa.Column('move_id', sa.Integer(), sa.ForeignKey('order_moves.id', ondelete='CASCADE'), nullable=False),
sa.Column('item_id', sa.Integer(), sa.ForeignKey('order_items.id'), nullable=False),
sa.Column('quantity', sa.Float(), nullable=True),
sa.PrimaryKeyConstraint('move_id', 'item_id'),
)
op.create_index('ix_order_move_items_item_id', 'order_move_items', ['item_id'])
with op.batch_alter_table('order_items') as b:
b.add_column(sa.Column('split_from_id', sa.Integer(), nullable=True))
b.create_index('ix_order_items_split_from_id', ['split_from_id'])
b.create_foreign_key('fk_order_items_split_from', 'order_items', ['split_from_id'], ['id'])
_backfill(op.get_bind())
def _backfill(bind) -> None:
rows = bind.execute(sa.text(
"select a.id, a.order_id, a.event_type, a.waiter_id, a.item_ids, a.note, a.created_at, o.table_id, o.business_day_id "
"from order_audit_log a join orders o on o.id = a.order_id "
"where a.event_type in ('TABLE_TRANSFER', 'ITEMS_MOVED_OUT', 'ITEMS_ADDED') and a.note is not null order by a.id")).fetchall()
existing_items = {r[0] for r in bind.execute(sa.text("select id from order_items")).fetchall()}
existing_orders = {r[0] for r in bind.execute(sa.text("select id from orders")).fetchall()}
def add(kind, frm, to, ft, tt, by, at, day, items):
res = bind.execute(sa.text(
"insert into order_moves (kind, from_order_id, to_order_id, from_table_id, to_table_id, by_user_id, business_day_id, at, note) "
"values (:k, :fo, :to, :ft, :tt, :by, :day, :at, 'backfill')"),
{'k': kind, 'fo': frm if frm in existing_orders else None, 'to': to if to in existing_orders else None,
'ft': ft, 'tt': tt, 'by': by, 'day': day, 'at': at})
mid = res.lastrowid
for iid in sorted({i for i in items if i in existing_items}):
bind.execute(sa.text("insert or ignore into order_move_items (move_id, item_id, quantity) values (:m, :i, null)"),
{'m': mid, 'i': iid})
def ids(raw):
try:
v = json.loads(raw or '[]')
return [int(x) for x in v if isinstance(x, int)]
except (ValueError, TypeError):
return []
for (_id, order_id, kind, by, item_ids, note, at, table_id, day) in rows:
if kind == 'TABLE_TRANSFER':
m = re.match(rf"Transferred from table (\d+|None) to table (\d+|None)", note)
if not m:
continue
items = [r[0] for r in bind.execute(sa.text(
"select id from order_items where order_id = :o and added_at <= :at"), {'o': order_id, 'at': at}).fetchall()]
add('table_transfer', order_id, order_id, _int(m.group(1)), _int(m.group(2)), by, at, day, items)
elif kind == 'ITEMS_MOVED_OUT':
m = re.match(rf"Moved to order #(\d+) \({_TABLE}\)", note)
if not m:
continue
add('items_moved', order_id, int(m.group(1)), table_id, _int(m.group(2)), by, at, day, ids(item_ids))
elif kind == 'ITEMS_ADDED':
m = re.match(rf"Items merged from order #(\d+) \({_TABLE}\)", note)
if not m:
continue
add('orders_merged', int(m.group(1)), order_id, _int(m.group(2)), table_id, by, at, day, ids(item_ids))
def downgrade() -> None:
with op.batch_alter_table('order_items') as b:
b.drop_constraint('fk_order_items_split_from', type_='foreignkey')
b.drop_index('ix_order_items_split_from_id')
b.drop_column('split_from_id')
op.drop_index('ix_order_move_items_item_id', 'order_move_items')
op.drop_table('order_move_items')
for col in ('id', 'kind', 'from_order_id', 'to_order_id', 'business_day_id', 'at'):
op.drop_index(f'ix_order_moves_{col}', 'order_moves')
op.drop_table('order_moves')