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:
2026-11-18 21:58:46 +02:00
co-authored by Claude Opus 5.5
parent f4aae6abbc
commit 070c9cf625
56 changed files with 2339 additions and 3846 deletions
@@ -0,0 +1,160 @@
// The tables now, in the manager (plan step 6.9; the map since 6.8): each table's state, total,
// time open, waiters, flags and a booking kept for it soon (RESERVED). A table with an order
// opens its order page; a free one opens a new order there. Quick actions set the table's flags
// and can tell its waiters (or everyone) a message. Lines that didn't print are listed below.
// Everything stays current through the live events.
import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { Clock, Lock, Printer, Zap } from 'lucide-react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, EmptyState, IconButton, Select, Spinner, cx } from '@xenia/ui'
import client from '../../api/client'
import { useLiveQuery } from '../../live/useLiveQuery'
import LiveFloor from '../floor/LiveFloor'
import { useDuration } from '../overview/hooks'
import { TABLE_STATES, durationParts, orderTotal, tableCards, tableName } from '../overview/overviewLogic'
import { Money, Pill } from '../overview/parts'
import PendingPrints from '../overview/PendingPrints'
import { BORDER, SOFT, SOLID } from '../overview/tones'
import QuickActions from './QuickActions'
import { NO_ZONE, cardTarget, filterCards, flagsByTable, personName } from './orderLogic'
const ORDER_EVENTS = ['order_updated', 'order_paid', 'order_closed', 'order_refunded']
const STATE_TONE = { open: 'success', partially_paid: 'info', paid: 'accent', free: 'neutral' }
export default function TablesPage() {
const { t } = useTranslation('manager')
const tc = useTranslation('common').t
const navigate = useNavigate()
const [view, setView] = useState('list')
const [states, setStates] = useState([])
const [zone, setZone] = useState('')
const [quick, setQuick] = useState(null) // a card
const { data: tables = [], isPending: loadingTables } = useLiveQuery({
queryKey: ['tables'], queryFn: () => client.get('/api/tables/').then(r => r.data),
events: ['table_list_changed', 'reservation_updated'], refetchInterval: 60_000,
})
const { data: orders = [], isPending: loadingOrders } = useLiveQuery({
queryKey: ['orders-active'], queryFn: () => client.get('/api/orders/').then(r => r.data), events: ORDER_EVENTS, refetchInterval: 60_000,
})
const { data: groups = [] } = useLiveQuery({ queryKey: ['table-groups'], queryFn: () => client.get('/api/tables/groups').then(r => r.data), events: ['table_list_changed'] })
const { data: flagDefs = [] } = useLiveQuery({ queryKey: ['flag-defs'], queryFn: () => client.get('/api/flags/defs').then(r => r.data), staleTime: 60_000 })
const { data: flagRows = [] } = useLiveQuery({ queryKey: ['flag-assignments'], queryFn: () => client.get('/api/flags/assignments').then(r => r.data), events: ['table_flags_changed'] })
const { data: staff = [] } = useLiveQuery({ queryKey: ['waiters'], queryFn: () => client.get('/api/waiters/').then(r => r.data), staleTime: 60_000 })
const cards = tableCards(tables, orders)
const shown = filterCards(cards, { states, zone })
const flags = flagsByTable(flagDefs, flagRows)
const name = id => personName(staff.find(s => s.id === id)) || `#${id}`
const toggleState = s => setStates(list => (list.includes(s) ? list.filter(x => x !== s) : [...list, s]))
if (loadingTables || loadingOrders) return <div className="h-full grid place-items-center"><Spinner /></div>
return (
<div className="h-full overflow-y-auto p-4 lg:p-6 grid gap-4 content-start" data-testid="tables-page">
<div className="flex flex-wrap items-center gap-2">
<div className="flex rounded-control border border-line overflow-hidden" role="group">
{['list', 'map'].map(v => (
<button key={v} type="button" aria-pressed={view === v} onClick={() => setView(v)} data-testid={`tables-view-${v}`}
className={cx('px-3 py-1.5 text-label font-medium', view === v ? 'bg-accent-soft text-accent-ink' : 'bg-surface text-ink-2 hover:bg-surface-2')}>
{tc(`floorMap.${v}`)}
</button>
))}
</div>
<span className="flex-1" />
{view === 'list' && TABLE_STATES.map(s => (
<button key={s} type="button" aria-pressed={states.includes(s)} onClick={() => toggleState(s)} data-testid={`tables-state-${s}`}
className={cx('inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full border text-label font-semibold',
states.includes(s) ? `${SOFT[STATE_TONE[s]]} ${BORDER[STATE_TONE[s]]}` : 'border-line bg-surface text-ink-2 hover:bg-surface-2')}>
<span className={cx('w-2 h-2 rounded-full', SOLID[STATE_TONE[s]])} aria-hidden="true" />
{t(`overview.tableState.${s}`)}
<span className="tabular-nums text-ink">{cards.filter(c => c.state === s).length}</span>
</button>
))}
<Select size="sm" value={zone} onChange={e => setZone(e.target.value)} aria-label={t('orders.tables.zone')} data-testid="tables-zone">
<option value="">{t('orders.tables.allZones')}</option>
{groups.map(g => <option key={g.id} value={String(g.id)}>{g.name}</option>)}
<option value={NO_ZONE}>{t('orders.tables.noZone')}</option>
</Select>
</div>
{view === 'map' && (
<LiveFloor zone={zone && zone !== NO_ZONE ? Number(zone) : null} onOpen={id => navigate(`/orders/${id}`)} />
)}
{view === 'list' && (shown.length === 0
? <EmptyState title={t('orders.tables.none')} />
: (
<div className="grid gap-3 grid-cols-[repeat(auto-fill,minmax(17rem,1fr))]">
{shown.map(card => (
<TableCard key={card.table.id} card={card} zone={groups.length > 1 ? groups.find(g => g.id === card.table.group_id)?.name : null} flags={flags[card.table.id] ?? []} waiterName={name}
onOpen={() => navigate(cardTarget(card))} onQuick={() => setQuick(card)} />
))}
</div>
))}
<PendingPrints cards={cards.filter(c => c.pendingPrint)} onOpen={id => navigate(`/orders/${id}`)} />
{quick && <QuickActions card={quick} flagDefs={flagDefs} current={flags[quick.table.id] ?? []} onClose={() => setQuick(null)} />}
</div>
)
}
function TableCard({ card, zone, flags, waiterName, onOpen, onQuick }) {
const { t } = useTranslation('manager')
const { formatTime } = useFormat()
const since = useDuration()
const { table, order, state, pendingPrint } = card
const tone = STATE_TONE[state]
const booking = table.upcoming_reservation
const open = order ? durationParts(order.opened_at) : null
const long = !!open && open.h * 60 + open.m >= 90 // the v1 card's "been here a while"
return (
<div className={cx('relative rounded-card border bg-surface shadow-raised overflow-hidden grid', BORDER[tone])} data-testid="table-card" data-state={state} data-table={table.id}>
<span className={cx('absolute inset-y-0 left-0 w-1.5', SOLID[tone])} aria-hidden="true" />
<button type="button" onClick={onOpen} className="text-left pl-5 pr-4 pt-3 pb-2 grid gap-2 hover:bg-surface-2 transition-colors duration-fast"
aria-label={t('orders.tables.open', { table: tableName(table), state: t(`overview.tableState.${state}`) })}>
<span className="flex items-start gap-2">
<span className="flex-1 grid gap-1">
<span className="text-display font-bold leading-none text-ink tabular-nums">{tableName(table)}</span>
{zone && <span className="text-caption text-ink-3">{zone}</span>}
</span>
<span className={cx('px-2 py-0.5 rounded-full text-caption font-semibold', SOFT[tone])}>{t(`overview.tableState.${state}`)}</span>
</span>
<span className="flex flex-wrap gap-1 min-h-6">
{pendingPrint && <Pill tone="warning"><Printer size={12} aria-hidden="true" />{t('orders.tables.notPrinted')}</Pill>}
{booking && (
<Pill tone="accent" data-testid="table-reserved" title={`${booking.guest_name} · ${booking.party_size}`}>
<Lock size={12} aria-hidden="true" />{t('orders.tables.reserved', { time: formatTime(booking.reserved_for), name: booking.guest_name })}
</Pill>
)}
{flags.slice(0, 3).map(f => (
<Pill key={f.id} tone="neutral">{f.emoji && <span aria-hidden="true">{f.emoji}</span>}{f.name}</Pill>
))}
{flags.length > 3 && <Pill tone="neutral" title={flags.slice(3).map(f => f.name).join(', ')}>+{flags.length - 3}</Pill>}
</span>
<span className="grid grid-cols-2 gap-2">
<span className="grid">
<span className="text-caption font-semibold uppercase tracking-wide text-ink-3">{t('orders.total')}</span>
{order ? <Money value={orderTotal(order.items)} className="text-title font-semibold text-ink" /> : <span className="text-title text-ink-3">—</span>}
</span>
<span className="grid">
<span className="text-caption font-semibold uppercase tracking-wide text-ink-3">{t('orders.tables.time')}</span>
<span className={cx('text-title tabular-nums inline-flex items-center gap-1', long ? 'font-bold text-warning-ink' : 'text-ink')}>
{order ? <><Clock size={14} aria-hidden="true" />{since(order.opened_at)}</> : <span className="text-ink-3">—</span>}
</span>
</span>
</span>
</button>
<div className="flex items-center gap-2 pl-5 pr-2 py-2 border-t border-line">
<span className="flex-1 min-w-0 truncate text-label text-ink-2">
{!order ? (table.seat_count ? t('orders.tables.seats', { count: table.seat_count }) : '')
: order.waiters.length ? order.waiters.map(w => waiterName(w.waiter_id)).join(', ') : t('orders.tables.noWaiter')}
</span>
<IconButton size="sm" variant="ghost" label={t('orders.quick.title', { table: tableName(table) })} onClick={onQuick} data-testid="table-quick"><Zap size={16} /></IconButton>
<Button size="sm" variant={order ? 'secondary' : 'primary'} onClick={onOpen} data-testid="table-open">{order ? t('orders.tables.view') : t('orders.tables.take')}</Button>
</div>
</div>
)
}