feat(backend,terminal): tables at the counter, shared live with the waiter phones (platform-v2 3.3c)

- 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>
This commit is contained in:
2026-11-15 02:15:54 +02:00
co-authored by Claude Opus 5.5
parent bff82a332a
commit 988d413d8e
19 changed files with 1452 additions and 218 deletions
+57 -7
View File
@@ -1,5 +1,6 @@
// The counter POS's server data (plan step 3.3a): the cashier's session (shift gate), the
// menu, and the cart priced by the backend. Kept fresh by the live connection.
// The counter POS's server data (plan steps 3.3a, 3.3c): the cashier's session (shift gate),
// the menu, the cart priced by the backend, the tables and a table's order. Kept fresh by the
// live connection, so a waiter's phone and the counter see each other's changes at once.
import { useCallback, useEffect, useRef, useState } from 'react'
import client from '../../api/client'
import { useLiveEvent, useLiveEvents } from '../../device/context'
@@ -40,25 +41,74 @@ export function useMenu() {
return { menu, reload: load }
}
/** The cart priced by POST /api/pos/quote whenever it changes (latest answer wins).
/** The cart priced by POST /api/pos/quote whenever it changes (latest answer wins). With
* `orderId` (a table's order) the order's items count for deals, as when it's sent.
* → { quote, pending, error } */
export function useQuote(lines, orderType) {
export function useQuote(lines, orderType, orderId = null) {
const [state, setState] = useState({ quote: null, pending: false, error: null })
const seq = useRef(0)
const key = JSON.stringify([lines, orderType])
const key = JSON.stringify([lines, orderType, orderId])
useEffect(() => {
const mine = ++seq.current
if (!lines.length) { setState({ quote: { lines: [], total: 0 }, pending: false, error: null }); return undefined }
setState(s => ({ ...s, pending: true }))
const id = setTimeout(async () => {
try {
const { data } = await client.post('/api/pos/quote', { order_type: orderType, lines }, quiet)
const { data } = await client.post('/api/pos/quote', { order_type: orderType, lines, order_id: orderId ?? undefined }, quiet)
if (mine === seq.current) setState({ quote: data, pending: false, error: null })
} catch (err) {
if (mine === seq.current) setState(s => ({ ...s, pending: false, error: err }))
}
}, QUOTE_DELAY_MS)
return () => clearTimeout(id)
}, [key]) // eslint-disable-line react-hooks/exhaustive-deps -- `key` is lines + orderType
}, [key]) // eslint-disable-line react-hooks/exhaustive-deps -- `key` is lines + orderType + orderId
return state
}
const TABLE_EVENTS = ['order_updated', 'order_paid', 'order_closed', 'table_flags_changed', 'table_list_changed', 'kds_order_updated', 'kds_item_updated']
const COALESCE_MS = 150
/** Calls `load` once for a burst of events (a payment sends several). */
function useCoalesced(load) {
const timer = useRef(null)
useEffect(() => () => clearTimeout(timer.current), [])
return useCallback(() => {
clearTimeout(timer.current)
timer.current = setTimeout(load, COALESCE_MS)
}, [load])
}
/** GET /api/pos/tables: { groups, tables } (null while loading); live. */
export function useTables() {
const [data, setData] = useState(null)
const load = useCallback(async () => {
try { setData((await client.get('/api/pos/tables', quiet)).data) } catch { /* the shell shows offline */ }
}, [])
const soon = useCoalesced(load)
useEffect(() => { load() }, [load])
useLiveEvent('ready', load)
useLiveEvents(TABLE_EVENTS, soon)
return { data, reload: load }
}
/** GET /api/orders/{id} for one order (null id → null); reloads on that order's events.
* → { order, gone, reload } (gone: closed or removed meanwhile). */
export function useOrder(orderId) {
const [order, setOrder] = useState(null)
const [gone, setGone] = useState(false)
const load = useCallback(async () => {
if (orderId == null) { setOrder(null); setGone(false); return }
try {
const { data } = await client.get(`/api/orders/${orderId}`, quiet)
setOrder(data)
setGone(data.status === 'closed' || data.status === 'cancelled')
} catch (err) {
if (err?.xenia?.status === 404) setGone(true)
}
}, [orderId])
const soon = useCoalesced(load)
useEffect(() => { load() }, [load])
useLiveEvent('ready', load)
useLiveEvents(TABLE_EVENTS, (_type, data) => { if (data?.order_id === orderId) soon() })
return { order, gone, reload: load }
}