- POS: GET /api/pos/tables (the cashier's zones as in the waiter app, each open order's
totals, people, ready items, flags, reservation), POST /api/pos/tables/{id}/items (into
the table's order or a new one via orders.open_order + add_items), POST
/api/pos/orders/{id}/pay (chosen items, cash/card/split, drawer on cash), POST
/api/pos/orders/{id}/close (only when nothing is unpaid). Quote takes order_id so the
order's items count for cart conditions (shared pos_cart.order_cart, used by add_items).
- New codes pos.table_not_found, pos.table_not_yours, pos.items_changed, pos.order_unpaid.
- Fix: item cancels (DELETE item, cancel-items) now broadcast order_updated, so phones and
kitchen screens see them at once.
- Terminal: Sale | Tables switch, zone list, table cards by zone, TablePanel (order lines,
new lines, send, pay selected/all, cancel with approval, close), generic PaymentDialog.
- Tests: test_pos_tables.py (8), terminal unit, terminal.mjs +17 with a live phone socket.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
9.8 KiB
React
174 lines
9.8 KiB
React
// The counter POS screen (plan steps 3.3a–c). Two 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).
|
||
// 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 { useFormat, useLanguage, useTranslation } from '@xenia/i18n'
|
||
import { useCan } from '@xenia/access'
|
||
import { EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
|
||
import { addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, toLine, unitOf } from './logic/cart'
|
||
import { zoneCounts } from './logic/tables'
|
||
import ProductDialog from './ProductDialog'
|
||
import SaleCart from './SaleCart'
|
||
import TablePanel from './TablePanel'
|
||
import TablesView from './TablesView'
|
||
import { useMenu, useTables } from './usePos'
|
||
|
||
export default function PosScreen() {
|
||
const { t } = useTranslation('terminal')
|
||
const { menu } = useMenu()
|
||
if (!menu) return <div className="absolute inset-0 flex items-center justify-center bg-canvas"><Spinner /></div>
|
||
return <Counter menu={menu} key={menu.settings.start_category_id ?? 'all'} t={t} />
|
||
}
|
||
|
||
function Counter({ menu, t }) {
|
||
const { language } = useLanguage()
|
||
const [mode, setMode] = useState('sale') // sale | tables
|
||
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)
|
||
const [query, setQuery] = useState('')
|
||
const [orderType, setOrderType] = useState(menu.settings.default_order_type)
|
||
const [saleCart, setSaleCart] = useState([])
|
||
const [tableCart, setTableCart] = useState([])
|
||
const [dialog, setDialog] = useState(null) // product
|
||
const tables = useTables()
|
||
const canDiscount = useCan('ordering.prices.modify')
|
||
|
||
// 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 setCart = atTable ? setTableCart : setSaleCart
|
||
const leaveTable = () => { setTableId(null); setTableCart([]) }
|
||
|
||
const byId = useMemo(() => new Map(menu.products.map(p => [p.id, p])), [menu.products])
|
||
const categories = useMemo(() => categoriesWithProducts(menu.categories, menu.products), [menu])
|
||
const shown = useMemo(() => (query.trim() ? searchProducts(menu.products, query) : productsIn(menu.products, category)),
|
||
[menu.products, query, category])
|
||
const counts = useMemo(() => zoneCounts(tables.data?.tables ?? []), [tables.data])
|
||
|
||
const tap = product => {
|
||
if (needsChoices(product)) { setDialog(product); return }
|
||
const unit = unitOf(product)
|
||
setCart(c => addLine(c, toLine(product, defaultSelection(product), unit ? unit.start : 1)))
|
||
}
|
||
|
||
return (
|
||
<div className="absolute inset-0 grid grid-cols-[13rem_1fr_24rem] bg-canvas text-ink" data-testid="pos-screen" 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')}>
|
||
<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>
|
||
</div>
|
||
<div className="flex-1 overflow-y-auto p-2 grid content-start gap-1" data-testid={picking ? 'pos-zones' : 'pos-categories'}>
|
||
{picking ? (
|
||
<>
|
||
<NavButton active={zone == null} onClick={() => setZone(null)} count={counts.all}>{t('pos.tables.allZones')}</NavButton>
|
||
{(tables.data?.groups ?? []).map(g => (
|
||
<NavButton key={g.id} colour={g.color} active={zone === g.id} onClick={() => setZone(g.id)} count={counts[g.id]} testId={`zone-${g.id}`}>{g.name}</NavButton>
|
||
))}
|
||
</>
|
||
) : (
|
||
<>
|
||
<NavButton active={category == null && !query} onClick={() => { setCategory(null); setQuery('') }}>{t('pos.allProducts')}</NavButton>
|
||
{categories.map(c => (
|
||
<NavButton key={c.id} colour={c.color} active={category === c.id && !query} onClick={() => { setCategory(c.id); setQuery('') }} testId={`category-${c.id}`}>
|
||
{c.name}
|
||
</NavButton>
|
||
))}
|
||
</>
|
||
)}
|
||
</div>
|
||
</nav>
|
||
|
||
{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>
|
||
) : (
|
||
<>
|
||
<main className="flex flex-col min-h-0 min-w-0">
|
||
<div className="p-3 border-b border-line bg-surface flex items-center gap-2">
|
||
<div className="relative flex-1">
|
||
<Search size={18} className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3 pointer-events-none" />
|
||
<Input value={query} onChange={e => setQuery(e.target.value)} placeholder={t('pos.search')} className="pl-10" data-testid="pos-search" aria-label={t('pos.search')} />
|
||
</div>
|
||
{query && <IconButton label={t('pos.clearSearch')} onClick={() => setQuery('')}><X size={18} /></IconButton>}
|
||
</div>
|
||
<div className="flex-1 overflow-y-auto p-3">
|
||
{shown.length === 0 ? (
|
||
<EmptyState title={query ? t('pos.noResults', { query }) : t('pos.noProducts')} />
|
||
) : (
|
||
<div className="grid gap-3" style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(9.5rem, 1fr))' }} data-testid="pos-grid">
|
||
{shown.map(p => <ProductCard key={p.id} product={p} onTap={() => tap(p)} onInfo={() => setDialog(p)} />)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</main>
|
||
{atTable
|
||
? <TablePanel key={table.id} table={table} cart={tableCart} setCart={setTableCart} byId={byId} canDiscount={canDiscount}
|
||
onBack={leaveTable} onTablesChanged={tables.reload} />
|
||
: <SaleCart cart={saleCart} setCart={setSaleCart} orderType={orderType} setOrderType={setOrderType} byId={byId} canDiscount={canDiscount} />}
|
||
</>
|
||
)}
|
||
|
||
{dialog && (
|
||
<ProductDialog product={dialog} orderType={atTable ? 'here' : orderType} onClose={() => setDialog(null)}
|
||
onAdd={line => setCart(c => addLine(c, line))} />
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
function ModeButton({ active, onClick, icon: Icon, badge, children, testId }) {
|
||
return (
|
||
<button type="button" role="tab" aria-selected={active} onClick={onClick} data-testid={testId}
|
||
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}
|
||
</button>
|
||
)
|
||
}
|
||
|
||
function NavButton({ active, colour, onClick, children, testId, count }) {
|
||
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
|
||
${active ? 'bg-accent-soft text-accent-ink' : 'text-ink hover:bg-surface-2'}`}>
|
||
{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>}
|
||
</button>
|
||
)
|
||
}
|
||
|
||
function ProductCard({ product, onTap, onInfo }) {
|
||
const { t } = useTranslation('terminal')
|
||
const { formatMoney } = useFormat()
|
||
const unit = unitOf(product)
|
||
return (
|
||
<div className="relative bg-surface border border-line rounded-card shadow-raised overflow-hidden flex flex-col" data-testid="pos-product" data-product={product.id}>
|
||
<button type="button" onClick={onTap} className="flex-1 flex flex-col text-left active:scale-[0.98] transition-transform duration-fast">
|
||
{product.image_url
|
||
? <img src={product.image_url} alt="" loading="lazy" className="w-full aspect-[4/3] object-cover bg-surface-2" />
|
||
: <span className="w-full aspect-[4/3] bg-surface-2 flex items-center justify-center text-heading text-ink-3" aria-hidden="true">{product.name.slice(0, 1)}</span>}
|
||
<span className="p-2 grid gap-0.5">
|
||
<span className="text-body font-medium text-ink line-clamp-2">{product.name}</span>
|
||
<span className="text-label text-ink-2">{formatMoney(product.price)}{unit ? ` / ${unit.label}` : ''}</span>
|
||
</span>
|
||
</button>
|
||
<IconButton label={t('pos.checkPrice', { name: product.name })} size="sm" variant="secondary" onClick={onInfo}
|
||
className="absolute top-1.5 right-1.5" data-testid="product-info">
|
||
<Info size={16} />
|
||
</IconButton>
|
||
</div>
|
||
)
|
||
}
|