feat: tables and orders in the manager on v2 (platform-v2 step 6.9)
- manager: new pages/orders/ (Tables page cards + quick actions, order page with
lines, multi-select pay/discount/cancel/tab, staff + customer, price changes,
history, the menu + cart beside it); a free table opens a new order on first
Send; v1 TablesPage, OrderDetailPage, DesktopOrderingTab, ManagerOrderDrawer
and PaymentMethodModal removed
- ordering by line id like the phones: GET /api/orders/menu (the counter's
product shape, in-store prices); the product dialog, cart rules and quote hook
move to packages/ordering (@xenia/ordering), shared by counter, menu tablet,
manager
- close follows orders.close_with_unpaid (pay first, or write off after saying
so) on the order page and the overview's quick view; paying asks cash or card
- a confirmed booking can keep its table ahead (manager, phones, counter):
POST /api/connect/bookings/{id}/table, reservations create/PATCH; refused with
reservation.table_kept within 2 h of another booking
- tests: test_manager_ordering.py, online bookings +2, manager orders.test.mjs,
@xenia/ordering, e2e manager.mjs (take, change, pay-first close, RESERVED)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
// 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 } from '@xenia/access'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import { 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, 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' }
|
||||
|
||||
export default function OrderLines({ order, open, priceEvents, onPay, onChanged }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const { formatMoney, formatTime } = useFormat()
|
||||
const canDiscount = useCan('pricing.discounts.apply')
|
||||
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>}
|
||||
<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>}
|
||||
</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 && <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 = Number(String(pct).replace(',', '.'))
|
||||
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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user