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>
165 lines
11 KiB
React
165 lines
11 KiB
React
// A table's order lines in the manager (plan step 6.9): what was ordered with its choices, who
|
||
// paid what and how, price changes; pick unpaid lines to pay, discount, cancel or put on the
|
||
// customer's tab. The rules are the phones' (a discount needs pricing.discounts.apply, the
|
||
// server checks every key again).
|
||
import { useState } from 'react'
|
||
import { useMutation } from '@tanstack/react-query'
|
||
import toast from 'react-hot-toast'
|
||
import { Ban, CreditCard, Percent, Printer, Receipt } from 'lucide-react'
|
||
import { useCan, useModule } from '@xenia/access'
|
||
import { errorMessage } from '@xenia/api'
|
||
import { parseNumber, useFormat, useTranslation } from '@xenia/i18n'
|
||
import { Button, Dialog, EmptyState, Field, Input, cx } from '@xenia/ui'
|
||
import client from '../../api/client'
|
||
import { Pill } from '../overview/parts'
|
||
import { lineAmount, lineDetails, nameOf, pathText, selectable, selectedAmount, shownLines, toggleSelected, unitOf } from './orderLogic'
|
||
|
||
const STATUS_TONE = { paid: 'success', cancelled: 'danger', tabbed: 'info', closed: 'warning' }
|
||
const EVENT_TONE = { waiter_discount: 'text-accent-ink', free_item_added: 'text-success-ink', modifier_applied: 'text-info-ink', manual_price: 'text-warning-ink' }
|
||
|
||
export default function OrderLines({ order, open, priceEvents, moves = {}, onPay, onChanged }) {
|
||
const { t } = useTranslation('manager')
|
||
const { formatMoney, formatTime } = useFormat()
|
||
const canDiscount = useCan('pricing.discounts.apply')
|
||
const tabsOn = useModule('tabs')
|
||
const canTab = useCan('tabs.manage') && tabsOn // what the tab route checks (6.13a)
|
||
const [selected, setSelected] = useState([])
|
||
const [cancelling, setCancelling] = useState(false)
|
||
const [discounting, setDiscounting] = useState(false)
|
||
const lines = shownLines(order)
|
||
const picked = selected.filter(id => order.items.some(i => i.id === id && selectable(i)))
|
||
const amount = selectedAmount(order, picked)
|
||
const done = msg => { toast.success(msg); setSelected([]); onChanged() }
|
||
const onError = err => toast.error(errorMessage(err, t))
|
||
|
||
const cancel = useMutation({
|
||
mutationFn: print => client.post(`/api/orders/${order.id}/cancel-items`, { item_ids: picked, print_cancellation: print }),
|
||
onSuccess: () => { setCancelling(false); done(t('orders.lines.cancelled', { count: picked.length })) }, onError,
|
||
})
|
||
const toTab = useMutation({
|
||
mutationFn: async () => { for (const id of picked) await client.post(`/api/orders/${order.id}/items/${id}/tab`) },
|
||
onSuccess: () => done(t('orders.lines.tabbed')), onError: err => { onError(err); onChanged() },
|
||
})
|
||
|
||
return (
|
||
<section className="rounded-card border border-line bg-surface shadow-raised overflow-hidden" data-testid="order-lines">
|
||
{lines.length === 0 ? <EmptyState compact title={t('orders.lines.none')} /> : (
|
||
<ul className="divide-y divide-line">
|
||
{lines.map(item => {
|
||
const can = open && selectable(item)
|
||
const on = picked.includes(item.id)
|
||
const d = lineDetails(item)
|
||
const evs = priceEvents.filter(ev => ev.order_item_id === item.id && EVENT_TONE[ev.event_type])
|
||
const gone = item.status === 'cancelled'
|
||
return (
|
||
<li key={item.id} className={cx('px-4 py-2.5 flex gap-3 items-start', on && 'bg-accent-soft', gone && 'opacity-50')}
|
||
data-testid="order-line" data-status={item.status} data-name={nameOf(item)}>
|
||
<input type="checkbox" className="mt-1 size-4 accent-[var(--x-accent)]" disabled={!can} checked={on}
|
||
aria-label={t('orders.lines.pick', { name: nameOf(item) })} onChange={() => setSelected(s => toggleSelected(s, item.id))} data-testid="line-pick" />
|
||
<div className={cx('flex-1 min-w-0 grid gap-0.5', gone && 'line-through')}>
|
||
<div className="flex flex-wrap items-center gap-1.5 text-body font-semibold text-ink">
|
||
<span>{item.quantity !== 1 || item.unit_type !== 'piece' ? `${item.quantity}× ` : ''}{nameOf(item)}</span>
|
||
{item.deal_id && <Pill tone="accent">{t('orders.lines.free')}</Pill>}
|
||
{STATUS_TONE[item.status] && (
|
||
<Pill tone={STATUS_TONE[item.status]}>
|
||
{t(`orders.lines.status.${item.status}`)}{item.status === 'paid' && item.payment_method ? ` · ${t(`orders.pay.method.${item.payment_method}`, { defaultValue: item.payment_method })}` : ''}
|
||
</Pill>
|
||
)}
|
||
{item.status === 'active' && !item.printed && <Pill tone="warning"><Printer size={12} aria-hidden="true" />{t('orders.lines.notPrinted')}</Pill>}
|
||
</div>
|
||
{d.choices.length > 0 && <div className="text-caption text-ink-2">{d.choices.join(', ')}</div>}
|
||
{d.removed.length > 0 && <div className="text-caption text-ink-2">{t('orders.lines.without', { list: d.removed.join(', ') })}</div>}
|
||
{d.note && <div className="text-caption text-warning-ink">{d.note}</div>}
|
||
{moves[item.id]?.length > 0 && (
|
||
<div className="text-caption text-info-ink" data-testid="line-moved">
|
||
↪ {t('orders.moves.linePath', { path: pathText(moves[item.id]) })}
|
||
</div>
|
||
)}
|
||
<div className="text-caption text-ink-3">
|
||
{t('orders.lines.added', { time: formatTime(item.added_at), name: item.added_by_name ?? '' })}
|
||
{item.paid_by_name && ` · ${t('orders.lines.paidBy', { name: item.paid_by_name })}`}
|
||
</div>
|
||
{evs.map((ev, i) => (
|
||
<div key={i} className={cx('flex items-center gap-2 text-caption', EVENT_TONE[ev.event_type])}>
|
||
<span aria-hidden="true">↳</span>
|
||
<span>{ev.modifier_name || ev.deal_name || t(`overview.order.event.${ev.event_type}`)}</span>
|
||
{ev.delta_amount != null && <span className="font-semibold">{formatMoney(ev.delta_amount, { signDisplay: 'exceptZero' })}</span>}
|
||
{ev.event_type === 'manual_price' && (
|
||
<span className="text-ink-3" data-testid="line-price-change">
|
||
{formatMoney(ev.price_before)} → {formatMoney(ev.price_after)}
|
||
{ev.applied_by_username && ` · ${ev.applied_by_username}`}{ev.waiter_note && ` · ${ev.waiter_note}`}
|
||
</span>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
<div className="text-right grid">
|
||
<span className={cx('text-body font-semibold tabular-nums', item.price_adjustment ? 'text-info-ink' : 'text-ink')}>{formatMoney(lineAmount(item))}</span>
|
||
{item.quantity !== 1 && <span className="text-caption text-ink-3 tabular-nums">{t('orders.lines.each', { price: formatMoney(unitOf(item)) })}</span>}
|
||
</div>
|
||
</li>
|
||
)
|
||
})}
|
||
</ul>
|
||
)}
|
||
|
||
{open && picked.length > 0 && (
|
||
<div className="sticky bottom-0 flex flex-wrap items-center gap-2 px-4 py-3 border-t border-line bg-surface-2" data-testid="line-actions">
|
||
<span className="flex-1 text-label text-ink-2">{t('orders.lines.picked', { count: picked.length, amount: formatMoney(amount) })}</span>
|
||
<Button size="sm" variant="ghost" onClick={() => setSelected([])}>{t('orders.lines.clear')}</Button>
|
||
{order.customer_id && canTab && <Button size="sm" variant="secondary" icon={<Receipt size={14} />} loading={toTab.isPending} onClick={() => toTab.mutate()}>{t('orders.lines.toTab')}</Button>}
|
||
{canDiscount && <Button size="sm" variant="secondary" icon={<Percent size={14} />} onClick={() => setDiscounting(true)} data-testid="lines-discount">{t('orders.lines.discount')}</Button>}
|
||
<Button size="sm" variant="danger" icon={<Ban size={14} />} onClick={() => setCancelling(true)} data-testid="lines-cancel">{t('orders.lines.cancel')}</Button>
|
||
<Button size="sm" icon={<CreditCard size={14} />} onClick={() => onPay(picked, amount, () => setSelected([]))} data-testid="lines-pay">{t('orders.lines.pay')}</Button>
|
||
</div>
|
||
)}
|
||
|
||
{cancelling && (
|
||
<Dialog open size="sm" onClose={() => setCancelling(false)} title={t('orders.lines.cancelTitle', { count: picked.length })}
|
||
description={t('orders.lines.cancelBody')}
|
||
footer={<>
|
||
<Button variant="secondary" onClick={() => setCancelling(false)}>{t('orders.back')}</Button>
|
||
<Button variant="secondary" loading={cancel.isPending && cancel.variables === false} onClick={() => cancel.mutate(false)} data-testid="cancel-no-print">{t('orders.lines.cancelNoPrint')}</Button>
|
||
<Button variant="danger" icon={<Printer size={16} />} loading={cancel.isPending && cancel.variables === true} onClick={() => cancel.mutate(true)}>{t('orders.lines.cancelPrint')}</Button>
|
||
</>} />
|
||
)}
|
||
{discounting && <DiscountDialog orderId={order.id} itemIds={picked} amount={amount} onClose={() => setDiscounting(false)}
|
||
onDone={() => { setDiscounting(false); done(t('orders.discount.done')) }} />}
|
||
</section>
|
||
)
|
||
}
|
||
|
||
function DiscountDialog({ orderId, itemIds, amount, onClose, onDone }) {
|
||
const { t } = useTranslation('manager')
|
||
const { formatMoney } = useFormat()
|
||
const [pct, setPct] = useState('10')
|
||
const [note, setNote] = useState('')
|
||
const value = parseNumber(String(pct)) ?? NaN
|
||
const ok = value > 0 && value <= 100
|
||
const apply = useMutation({
|
||
mutationFn: () => client.post(`/api/orders/${orderId}/items/discount`, { item_ids: itemIds, discount_percent: value, note: note.trim() || null }),
|
||
onSuccess: onDone, onError: err => toast.error(errorMessage(err, t)),
|
||
})
|
||
return (
|
||
<Dialog open size="sm" onClose={onClose} title={t('orders.discount.title', { count: itemIds.length })} description={t('orders.discount.body')}
|
||
footer={<>
|
||
<Button variant="secondary" onClick={onClose}>{t('orders.back')}</Button>
|
||
<Button disabled={!ok} loading={apply.isPending} onClick={() => apply.mutate()} data-testid="discount-apply">{t('orders.discount.apply')}</Button>
|
||
</>}>
|
||
<div className="grid gap-3">
|
||
<div className="flex flex-wrap gap-1.5">
|
||
{[5, 10, 15, 20, 50, 100].map(p => (
|
||
<Button key={p} size="sm" variant={value === p ? 'primary' : 'secondary'} onClick={() => setPct(String(p))}>{p}%</Button>
|
||
))}
|
||
</div>
|
||
<Field label={t('orders.discount.pct')} hint={ok ? t('orders.discount.about', { amount: formatMoney(-(amount * value) / 100) }) : undefined}>
|
||
<Input inputMode="decimal" value={pct} onChange={e => setPct(e.target.value)} data-testid="discount-pct" />
|
||
</Field>
|
||
<Field label={t('orders.discount.note')}>
|
||
<Input value={note} maxLength={200} onChange={e => setNote(e.target.value)} />
|
||
</Field>
|
||
</div>
|
||
</Dialog>
|
||
)
|
||
}
|