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:
@@ -0,0 +1,236 @@
|
||||
// The counter POS's menu and cart, without prices (plan step 3.3a). Prices always come from
|
||||
// the backend: the menu's `price` and POST /api/pos/quote (services/pos_cart.py). This file
|
||||
// only knows what was chosen; the rules for a complete choice mirror the backend's, so the
|
||||
// screen can say what's missing before asking. Pure: unit-tested in Node (tests/pos.test.mjs).
|
||||
|
||||
// ── menu ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Lower case, accents and Greek tonos removed: "Φρέντο" matches "φρεντο". */
|
||||
export function fold(text) {
|
||||
return String(text ?? '').normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim()
|
||||
}
|
||||
|
||||
/** Products in a category (a product listed in several shows in each), in that category's
|
||||
* order; categoryId null = all products (each once). */
|
||||
export function productsIn(products, categoryId) {
|
||||
if (categoryId == null) return products
|
||||
const pos = p => p.categories.find(c => c.category_id === categoryId)
|
||||
return products.filter(pos).sort((a, b) => pos(a).sort_order - pos(b).sort_order || a.name.localeCompare(b.name))
|
||||
}
|
||||
|
||||
/** Search by name (any word order, accents ignored), each product once whatever its
|
||||
* categories. */
|
||||
export function searchProducts(products, query) {
|
||||
const words = fold(query).split(/\s+/).filter(Boolean)
|
||||
if (!words.length) return []
|
||||
return products.filter(p => { const name = fold(p.name); return words.every(w => name.includes(w)) })
|
||||
}
|
||||
|
||||
/** Categories that have products, in their order (top level first, then by sort order). */
|
||||
export function categoriesWithProducts(categories, products) {
|
||||
const used = new Set(products.flatMap(p => p.categories.map(c => c.category_id)))
|
||||
return categories.filter(c => used.has(c.id))
|
||||
}
|
||||
|
||||
// ── one product's choices ───────────────────────────────────────────────────
|
||||
|
||||
const defaultSub = subs => (subs?.length ? (subs.find(s => s.is_default) ?? subs[0]).name : null)
|
||||
|
||||
/** The choices a product starts with: default preference choices (+ their default subs and
|
||||
* the set's default shared sub), nothing else. */
|
||||
export function defaultSelection(product) {
|
||||
const prefs = {}
|
||||
for (const ps of product.prefs ?? []) {
|
||||
const def = ps.choices.find(c => c.id === ps.default_choice_id)
|
||||
prefs[ps.id] = def
|
||||
? { choices: [{ id: def.id, qty: 1, sub: ps.multi ? null : defaultSub(def.subs) }], shared: ps.shared && !def.disables_shared ? defaultSub(ps.shared.subs) : null }
|
||||
: { choices: [], shared: null }
|
||||
}
|
||||
return { quick: {}, options: {}, prefs, removed: [], notes: '' }
|
||||
}
|
||||
|
||||
/** Does the product need the options dialog (a preference to pick)? Otherwise a tap adds it. */
|
||||
export function needsChoices(product) {
|
||||
return (product.prefs ?? []).length > 0
|
||||
}
|
||||
|
||||
/** What's still missing: [{ kind: 'pref' | 'sub' | 'option_sub', name }] (empty = complete). */
|
||||
export function missingChoices(product, sel) {
|
||||
const out = []
|
||||
for (const ps of product.prefs ?? []) {
|
||||
const picked = sel.prefs[ps.id]?.choices ?? []
|
||||
if (!picked.length) { out.push({ kind: 'pref', name: ps.name }); continue }
|
||||
if (ps.multi) continue
|
||||
const c = ps.choices.find(x => x.id === picked[0].id)
|
||||
if (c?.subs?.length && !picked[0].sub) out.push({ kind: 'sub', name: c.name })
|
||||
if (ps.shared && c && !c.disables_shared && !sel.prefs[ps.id]?.shared) out.push({ kind: 'sub', name: ps.shared.name })
|
||||
}
|
||||
for (const o of product.options ?? []) {
|
||||
const s = sel.options[o.id]
|
||||
if (s && o.subs?.length && !(s.subs?.length)) out.push({ kind: 'option_sub', name: o.name })
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** Pick a single-choice preference (its default sub and the set's shared sub come with it). */
|
||||
export function pickChoice(product, sel, setId, choiceId) {
|
||||
const ps = product.prefs.find(p => p.id === setId)
|
||||
const c = ps.choices.find(x => x.id === choiceId)
|
||||
const prev = sel.prefs[setId] ?? { choices: [], shared: null }
|
||||
if (ps.multi) {
|
||||
const has = prev.choices.some(x => x.id === choiceId)
|
||||
const choices = has ? prev.choices.filter(x => x.id !== choiceId) : [...prev.choices, { id: choiceId, qty: 1, sub: null }]
|
||||
return { ...sel, prefs: { ...sel.prefs, [setId]: { ...prev, choices } } }
|
||||
}
|
||||
const shared = ps.shared && !c.disables_shared ? (prev.shared ?? defaultSub(ps.shared.subs)) : null
|
||||
return { ...sel, prefs: { ...sel.prefs, [setId]: { choices: [{ id: choiceId, qty: 1, sub: defaultSub(c.subs) }], shared } } }
|
||||
}
|
||||
|
||||
export function setChoiceQty(sel, setId, choiceId, qty) {
|
||||
const prev = sel.prefs[setId]
|
||||
return { ...sel, prefs: { ...sel.prefs, [setId]: { ...prev, choices: prev.choices.map(x => (x.id === choiceId ? { ...x, qty: Math.max(1, qty) } : x)) } } }
|
||||
}
|
||||
|
||||
export function pickChoiceSub(sel, setId, sub) {
|
||||
const prev = sel.prefs[setId]
|
||||
return { ...sel, prefs: { ...sel.prefs, [setId]: { ...prev, choices: prev.choices.map((x, i) => (i === 0 ? { ...x, sub } : x)) } } }
|
||||
}
|
||||
|
||||
export function pickShared(sel, setId, shared) {
|
||||
return { ...sel, prefs: { ...sel.prefs, [setId]: { ...sel.prefs[setId], shared } } }
|
||||
}
|
||||
|
||||
/** Toggle an extra (with its default sub). */
|
||||
export function toggleOption(product, sel, optionId) {
|
||||
const options = { ...sel.options }
|
||||
if (options[optionId]) delete options[optionId]
|
||||
else {
|
||||
const o = product.options.find(x => x.id === optionId)
|
||||
const sub = defaultSub(o.subs)
|
||||
options[optionId] = { qty: 1, subs: sub ? [sub] : [] }
|
||||
}
|
||||
return { ...sel, options }
|
||||
}
|
||||
|
||||
export function pickOptionSub(product, sel, optionId, name) {
|
||||
const o = product.options.find(x => x.id === optionId)
|
||||
const cur = sel.options[optionId]?.subs ?? []
|
||||
const subs = o.multi_select ? (cur.includes(name) ? cur.filter(n => n !== name) : [...cur, name]) : [name]
|
||||
return { ...sel, options: { ...sel.options, [optionId]: { ...sel.options[optionId], subs } } }
|
||||
}
|
||||
|
||||
export function setOptionQty(sel, optionId, qty) {
|
||||
return { ...sel, options: { ...sel.options, [optionId]: { ...sel.options[optionId], qty: Math.max(1, qty) } } }
|
||||
}
|
||||
|
||||
/** Quick add-ons: qty 0 removes it. */
|
||||
export function setQuick(sel, quickId, qty) {
|
||||
const quick = { ...sel.quick }
|
||||
if (qty > 0) quick[quickId] = qty
|
||||
else delete quick[quickId]
|
||||
return { ...sel, quick }
|
||||
}
|
||||
|
||||
export function toggleRemoved(sel, ingredientId) {
|
||||
const removed = sel.removed.includes(ingredientId) ? sel.removed.filter(i => i !== ingredientId) : [...sel.removed, ingredientId]
|
||||
return { ...sel, removed }
|
||||
}
|
||||
|
||||
/** The cart line POST /api/pos/quote (and 3.3b's sale) takes. */
|
||||
export function toLine(product, sel, quantity) {
|
||||
const line = { product_id: product.id, quantity }
|
||||
const quick = Object.entries(sel.quick).map(([id, qty]) => ({ id: Number(id), qty }))
|
||||
const options = Object.entries(sel.options).map(([id, o]) => ({ id: Number(id), qty: o.qty, subs: o.subs }))
|
||||
const prefs = Object.entries(sel.prefs).filter(([, p]) => p.choices.length)
|
||||
.map(([id, p]) => ({ set_id: Number(id), choices: p.choices.map(c => ({ id: c.id, qty: c.qty, sub: c.sub })), shared: p.shared }))
|
||||
if (quick.length) line.quick = quick
|
||||
if (options.length) line.options = options
|
||||
if (prefs.length) line.prefs = prefs
|
||||
if (sel.removed.length) line.removed = [...sel.removed]
|
||||
if (sel.notes?.trim()) line.notes = sel.notes.trim()
|
||||
return line
|
||||
}
|
||||
|
||||
// ── the cart ────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Two lines are the same sale when everything but the quantity is the same. */
|
||||
export function lineKey(line) {
|
||||
const { quantity, ...rest } = line // eslint-disable-line no-unused-vars
|
||||
return stable(rest)
|
||||
}
|
||||
|
||||
const stable = v => (Array.isArray(v) ? `[${v.map(stable).join(',')}]`
|
||||
: v && typeof v === 'object' ? `{${Object.keys(v).sort().map(k => `${k}:${stable(v[k])}`).join(',')}}`
|
||||
: JSON.stringify(v))
|
||||
|
||||
/** Add a line; the same product with the same choices adds to its quantity. */
|
||||
export function addLine(cart, line) {
|
||||
const key = lineKey(line)
|
||||
const i = cart.findIndex(l => lineKey(l) === key)
|
||||
if (i < 0) return [...cart, line]
|
||||
return cart.map((l, j) => (j === i ? { ...l, quantity: round(l.quantity + line.quantity) } : l))
|
||||
}
|
||||
|
||||
export function setLineQty(cart, index, quantity) {
|
||||
if (quantity <= 0) return cart.filter((_, i) => i !== index)
|
||||
return cart.map((l, i) => (i === index ? { ...l, quantity: round(quantity) } : l))
|
||||
}
|
||||
|
||||
const round = n => Math.round(n * 1000) / 1000
|
||||
|
||||
// Weighed products (kg, L, g, mL): step and starting amount (the waiter app's)
|
||||
export const UNITS = {
|
||||
kg: { step: 0.1, start: 0.5, label: 'kg' },
|
||||
liter: { step: 0.1, start: 0.5, label: 'L' },
|
||||
gram: { step: 10, start: 100, label: 'g' },
|
||||
ml: { step: 5, start: 100, label: 'mL' },
|
||||
}
|
||||
|
||||
export const unitOf = product => UNITS[product.unit_type] ?? null
|
||||
|
||||
// ── paying (3.3b) ───────────────────────────────────────────────────────────
|
||||
|
||||
/** Amounts a customer typically hands over for `total`: the exact amount, then the next
|
||||
* round amounts above it (notes of 5, 10, 20, 50, 100), at most five. */
|
||||
export function quickAmounts(total) {
|
||||
const out = [round2(total)]
|
||||
for (const step of [5, 10, 20, 50, 100]) {
|
||||
const v = Math.ceil((total + 0.0001) / step) * step
|
||||
if (v > total + 0.004 && !out.includes(v)) out.push(v)
|
||||
}
|
||||
return out.slice(0, 5)
|
||||
}
|
||||
|
||||
/** Change to give back (null when not enough was given). */
|
||||
export function changeFor(given, due) {
|
||||
if (given == null || Number.isNaN(given)) return null
|
||||
const c = round2(given - due)
|
||||
return c < 0 ? null : c
|
||||
}
|
||||
|
||||
/** Cash and card parts of a split: methods[i] is 'cash' | 'card' for line i, totals from the
|
||||
* quote. → { cash, card, payments } (payments as POST /api/pos/sales takes them). */
|
||||
export function splitPayments(methods, totals) {
|
||||
const lines = { cash: [], card: [] }
|
||||
methods.forEach((m, i) => lines[m].push(i))
|
||||
const sum = idx => round2(idx.reduce((s, i) => s + (totals[i] ?? 0), 0))
|
||||
const payments = ['card', 'cash'].filter(m => lines[m].length).map(m => ({ method: m, lines: lines[m] }))
|
||||
return { cash: sum(lines.cash), card: sum(lines.card), payments }
|
||||
}
|
||||
|
||||
export const DISCOUNTS = [0, 5, 10, 15, 20, 25, 50, 100]
|
||||
|
||||
const round2 = n => Math.round(n * 100) / 100
|
||||
|
||||
/** The counter's sales point for an order type (phase 3S; backend pos_cart.point_for). */
|
||||
export function salesPointFor(orderType) {
|
||||
return orderType === 'takeaway' ? 'in_store_takeaway' : 'in_store'
|
||||
}
|
||||
|
||||
/** The menu's products sold at a sales point, each with that sales point's price (GET
|
||||
* /api/pos/menu sends `sold_at` and `prices`; older answers: everything, `price`). */
|
||||
export function forSalesPoint(products, point) {
|
||||
return products
|
||||
.filter(p => !p.sold_at || p.sold_at.includes(point))
|
||||
.map(p => (p.prices?.[point] != null ? { ...p, price: p.prices[point] } : p))
|
||||
}
|
||||
Reference in New Issue
Block a user