Files
xenia-pos-local/terminal/src/apps/pos/logic/tables.js
T
bonaminandClaude Opus 5.5 988d413d8e 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>
2026-11-15 02:15:54 +02:00

89 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Tables at the counter (plan step 3.3c): what the table list and a table's order show.
// Amounts come from the backend (GET /api/pos/tables, GET /api/orders/{id}); this file only
// groups, labels and adds up what it was given. Pure: unit-tested in Node (tests/pos.test.mjs).
/** free | mine | open (someone else's) | paid (nothing left to pay). */
export function tableState(table) {
const o = table.order
if (!o) return 'free'
if (o.items > 0 && o.unpaid <= 0.004) return 'paid'
return o.mine ? 'mine' : 'open'
}
/** Tables of one zone (null = all), in the backend's order. */
export function tablesIn(tables, groupId) {
return groupId == null ? tables : tables.filter(t => t.group_id === groupId)
}
/** Counts for the zone list: { [groupId|'all']: { total, busy } }. */
export function zoneCounts(tables) {
const out = { all: { total: 0, busy: 0 } }
for (const t of tables) {
const k = t.group_id ?? 'none'
out[k] = out[k] ?? { total: 0, busy: 0 }
for (const c of [out.all, out[k]]) {
c.total += 1
if (t.order) c.busy += 1
}
}
return out
}
const round2 = n => Math.round(n * 100) / 100
/** The option names stored on an order item (selected_options is JSON text or a list). */
export function optionNames(selected) {
let list = selected
if (typeof selected === 'string') {
try { list = JSON.parse(selected) } catch { list = [] }
}
return Array.isArray(list) ? list.map(o => o?.name).filter(Boolean) : []
}
/** An order's items as the counter lists them (cancelled and written-off ones left out),
* unpaid first, each with its total. */
export function orderLines(order) {
if (!order?.items) return []
const lines = order.items
.filter(i => i.status === 'active' || i.status === 'ready' || i.status === 'paid')
.map(i => ({
id: i.id,
name: i.product_name ?? i.product?.name ?? '',
quantity: i.quantity,
unitType: i.unit_type,
total: round2((i.unit_price + (i.price_adjustment || 0)) * i.quantity),
paid: i.status === 'paid',
method: i.payment_method,
options: optionNames(i.selected_options),
notes: i.notes,
kds: i.kds_status ?? 'pending',
}))
return [...lines.filter(l => !l.paid), ...lines.filter(l => l.paid)]
}
/** { total, paid, unpaid, unpaidIds } of the lines. */
export function orderSums(lines) {
const sum = ls => round2(ls.reduce((s, l) => s + l.total, 0))
const open = lines.filter(l => !l.paid)
return { total: sum(lines), paid: sum(lines.filter(l => l.paid)), unpaid: sum(open), unpaidIds: open.map(l => l.id) }
}
/** What "Pay" takes: the selected unpaid lines, or every unpaid line when none is selected. */
export function toPay(lines, selected) {
const open = lines.filter(l => !l.paid)
const chosen = open.filter(l => selected.includes(l.id))
return chosen.length ? chosen : open
}
/** The dialog's payments (by row index) as POST /api/pos/orders/{id}/pay takes them. */
export function itemPayments(payments, rows) {
return payments.map(p => ({ method: p.method, item_ids: p.lines.map(i => rows[i].id) }))
}
/** "2×" or "0.5 kg" for an order line. */
export function qtyLabel(quantity, unitType) {
const units = { kg: 'kg', liter: 'L', gram: 'g', ml: 'mL' }
if (!units[unitType]) return `${quantity}×`
return unitType === 'kg' || unitType === 'liter' ? `${Number(quantity).toFixed(1)} ${units[unitType]}` : `${quantity} ${units[unitType]}`
}