feat(backend,terminal): online orders on the counter POS (platform-v2 step 3.3d)

- Counter POS gets an "Online" mode (module on + connect_orders.manage): New / In
  progress / Finished, a chime on arrival (and every 30 s while one waits), badge,
  toast in any mode; the order panel offers only the next step: accept / reject with
  a reason, preparing, ready or out for delivery, then take the payment (shared
  PaymentDialog, drawer, shift) and mark delivered/served.
- connect_orders router on v2 conventions: require(connect_orders.manage), error
  codes online.not_found / online.status_changed, the cloud's step order enforced,
  audit rows, GET /api/connect/orders/board. POS device scope gains connect_orders.*;
  barista and barman get the key by default.
- Fixes: the KDS showed online orders before acceptance; accepting printed no
  kitchen tickets; a pull whose synced mark failed stored the order twice
  (store_online_orders skips known cloud ids); pulled orders lacked the business day
  and delivery type; rejected orders kept active items; delivered sets closed_at/by.
- Online orders belong to holders of connect_orders.manage (_can_access_order), so
  the counter pays them with POST /api/pos/orders/{id}/pay.
- Tests: test_pos_online.py (5), terminal unit +1, terminal.mjs +17 with a fake
  cloud (pull, synced, status mirror); fixed a timing race in a 3.3c check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 05:42:06 +02:00
co-authored by Claude Opus 5.5
parent 988d413d8e
commit d542cf7d34
20 changed files with 1052 additions and 176 deletions
+80 -16
View File
@@ -1,22 +1,33 @@
// The counter POS screen (plan steps 3.3a–c). Two modes, switched at the top left:
// The counter POS screen (plan steps 3.3a–d). Up to three modes, switched at the top left:
// - Sale: categories on the left, the product grid with a search, the quick-sale cart on the
// right (SaleCart: Charge → payment, 3.3b).
// - Tables: the zones on the left and the table list (TablesView); a table opens with the
// product grid again and the table's order on the right (TablePanel).
// - Online (when online orders are on and the cashier handles them): new / in progress /
// finished on the left, the orders (OnlineView) and the picked one (OnlinePanel). A new
// order rings (and again every 30 s while one waits for an answer) and shows a notice in any
// mode; the mode's badge counts the orders waiting.
// A tap adds a product (or opens its choices when it has preferences to pick); the (i) button
// checks its price without adding it. Every price is the backend's.
import { useMemo, useState } from 'react'
import { Info, LayoutGrid, Search, ShoppingBag, X } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { Globe, Info, LayoutGrid, Search, ShoppingBag, X } from 'lucide-react'
import { useFormat, useLanguage, useTranslation } from '@xenia/i18n'
import { useCan } from '@xenia/access'
import { EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
import { useCan, useModule } from '@xenia/access'
import { EmptyState, IconButton, Input, Spinner, useToast } from '@xenia/ui'
import { useLiveEvent } from '../../device/context'
import { playChime } from './chime'
import { addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, toLine, unitOf } from './logic/cart'
import { findOnline, ONLINE_LISTS } from './logic/online'
import { zoneCounts } from './logic/tables'
import OnlinePanel from './OnlinePanel'
import OnlineView from './OnlineView'
import ProductDialog from './ProductDialog'
import SaleCart from './SaleCart'
import TablePanel from './TablePanel'
import TablesView from './TablesView'
import { useMenu, useTables } from './usePos'
import { useMenu, useOnline, useTables } from './usePos'
const REMIND_MS = 30_000
export default function PosScreen() {
const { t } = useTranslation('terminal')
@@ -27,7 +38,8 @@ export default function PosScreen() {
function Counter({ menu, t }) {
const { language } = useLanguage()
const [mode, setMode] = useState('sale') // sale | tables
const { formatMoney } = useFormat()
const [mode, setMode] = useState('sale') // sale | tables | online
const [tableId, setTableId] = useState(null) // the open table (tables mode)
const [zone, setZone] = useState(null)
const [category, setCategory] = useState(() => menu.settings.start_category_id ?? null)
@@ -38,11 +50,37 @@ function Counter({ menu, t }) {
const [dialog, setDialog] = useState(null) // product
const tables = useTables()
const canDiscount = useCan('ordering.prices.modify')
const toast = useToast()
const onlineModule = useModule('connect_orders')
const handlesOnline = useCan('connect_orders.manage')
const onlineOn = onlineModule && handlesOnline
const online = useOnline(onlineOn)
const [onlineList, setOnlineList] = useState('incoming')
const [onlineId, setOnlineId] = useState(null)
const [now, setNow] = useState(() => Date.now())
const [chimes, setChimes] = useState(0)
const waiting = online.data?.incoming.length ?? 0
const ring = () => { playChime(); setChimes(c => c + 1) }
useLiveEvent('online_order_received', data => {
if (!onlineOn) return
ring()
if (mode !== 'online') toast.info(t('pos.online.arrived', { count: data?.count ?? 1 }))
})
// a reminder while an order waits for an answer; the waiting minutes move on too
useEffect(() => {
const id = setInterval(() => {
setNow(Date.now())
if (waiting > 0) { playChime(); setChimes(c => c + 1) }
}, REMIND_MS)
return () => clearInterval(id)
}, [waiting])
// a table that left the cashier's list (zone changed) shows the list again
const table = tableId != null ? tables.data?.tables.find(x => x.id === tableId) ?? null : null
const atTable = mode === 'tables' && table != null
const picking = mode === 'tables' && !atTable
const onlineOrder = findOnline(online.data, onlineId)
const setCart = atTable ? setTableCart : setSaleCart
const leaveTable = () => { setTableId(null); setTableCart([]) }
@@ -59,15 +97,27 @@ function Counter({ menu, t }) {
}
return (
<div className="absolute inset-0 grid grid-cols-[13rem_1fr_24rem] bg-canvas text-ink" data-testid="pos-screen" lang={language}>
<div className="absolute inset-0 grid grid-cols-[13rem_1fr_24rem] bg-canvas text-ink" data-testid="pos-screen" data-chimes={chimes} lang={language}>
<nav className="border-r border-line bg-surface min-h-0 flex flex-col">
<div className="p-2 grid grid-cols-2 gap-1 border-b border-line" role="tablist" aria-label={t('pos.mode.label')}>
<div className={`p-2 grid gap-1 border-b border-line ${onlineOn ? 'grid-cols-3' : 'grid-cols-2'}`} role="tablist" aria-label={t('pos.mode.label')}>
<ModeButton active={mode === 'sale'} onClick={() => setMode('sale')} icon={ShoppingBag} testId="mode-sale">{t('pos.mode.sale')}</ModeButton>
<ModeButton active={mode === 'tables'} onClick={() => { setMode('tables'); leaveTable() }} icon={LayoutGrid} testId="mode-tables"
badge={counts.all.busy || null}>{t('pos.mode.tables')}</ModeButton>
{onlineOn && (
<ModeButton active={mode === 'online'} onClick={() => setMode('online')} icon={Globe} testId="mode-online"
badge={waiting || null} alert={waiting > 0}>{t('pos.mode.online')}</ModeButton>
)}
</div>
<div className="flex-1 overflow-y-auto p-2 grid content-start gap-1" data-testid={picking ? 'pos-zones' : 'pos-categories'}>
{picking ? (
<div className="flex-1 overflow-y-auto p-2 grid content-start gap-1"
data-testid={mode === 'online' ? 'online-lists' : picking ? 'pos-zones' : 'pos-categories'}>
{mode === 'online' ? (
ONLINE_LISTS.map(list => (
<NavButton key={list} active={onlineList === list} onClick={() => setOnlineList(list)} testId={`online-list-${list}`}
tally={list === 'finished' ? null : online.data?.[list].length}>
{t(`pos.online.lists.${list}`)}
</NavButton>
))
) : picking ? (
<>
<NavButton active={zone == null} onClick={() => setZone(null)} count={counts.all}>{t('pos.tables.allZones')}</NavButton>
{(tables.data?.groups ?? []).map(g => (
@@ -87,7 +137,20 @@ function Counter({ menu, t }) {
</div>
</nav>
{picking ? (
{mode === 'online' ? (
<>
<main className="flex flex-col min-h-0 min-w-0">
<OnlineView data={online.data} list={onlineList} picked={onlineId} onPick={setOnlineId} now={now} />
</main>
<OnlinePanel key={onlineOrder?.id ?? 'none'} order={onlineOrder}
onChanged={(result, move) => {
online.reload()
if (move?.list) setOnlineList(move.list)
if (move?.clear) setOnlineId(null)
if (result?.change > 0) toast.success(t('pos.done.toastChange', { change: formatMoney(result.change) }))
}} />
</>
) : picking ? (
<main className="col-span-2 flex flex-col min-h-0 min-w-0">
<TablesView data={tables.data} zone={zone} onPick={id => { setTableId(id); setTableCart([]) }} />
</main>
@@ -126,18 +189,18 @@ function Counter({ menu, t }) {
)
}
function ModeButton({ active, onClick, icon: Icon, badge, children, testId }) {
function ModeButton({ active, onClick, icon: Icon, badge, alert, children, testId }) {
return (
<button type="button" role="tab" aria-selected={active} onClick={onClick} data-testid={testId}
<button type="button" role="tab" aria-selected={active} onClick={onClick} data-testid={testId} data-badge={badge ?? 0}
className={`relative min-h-[var(--x-control-lg)] rounded-control flex flex-col items-center justify-center gap-0.5 text-label font-semibold transition-colors duration-fast
${active ? 'bg-accent text-accent-fg' : 'bg-surface-2 text-ink-2 hover:bg-surface-3'}`}>
<Icon size={18} aria-hidden="true" />{children}
{badge ? <span className="absolute top-1 right-1 min-w-5 h-5 px-1 rounded-full bg-warning text-warning-fg text-caption font-bold flex items-center justify-center">{badge}</span> : null}
{badge ? <span className={`absolute top-1 right-1 min-w-5 h-5 px-1 rounded-full bg-warning text-warning-fg text-caption font-bold flex items-center justify-center ${alert ? 'animate-pulse' : ''}`}>{badge}</span> : null}
</button>
)
}
function NavButton({ active, colour, onClick, children, testId, count }) {
function NavButton({ active, colour, onClick, children, testId, count, tally }) {
return (
<button type="button" onClick={onClick} data-testid={testId} aria-pressed={active}
className={`min-h-[var(--x-control-lg)] px-3 rounded-control text-left text-body font-medium flex items-center gap-2 transition-colors duration-fast
@@ -145,6 +208,7 @@ function NavButton({ active, colour, onClick, children, testId, count }) {
{colour && <span className="size-2.5 rounded-full shrink-0" style={{ background: colour }} aria-hidden="true" />}
<span className="flex-1 truncate">{children}</span>
{count && <span className="text-caption text-ink-3">{count.busy}/{count.total}</span>}
{tally != null && <span className="text-caption text-ink-3">{tally}</span>}
</button>
)
}