feat(terminal): counter POS screen with server-priced cart and shift gate (platform-v2 3.3a)
- Backend /api/pos (pos_counter): session (business day, the cashier's own shift, self-start, who can open the day), menu (sellable products with every kind of choice, no costs; price now from the pricing engine), quote (lines by id → the waiter's selected_options entries with DB prices → resolve_price as orders do; pos.line_invalid). - POS device settings: default order type, start category, receipt printer (new ref sources categories / printers, also in the manager's Devices dialogs). - Terminal apps/pos: shift gate (start shift with drawer cash), categories, search, product grid, price check, options dialog, cart priced by the backend. - Tests: backend test_pos (13), terminal pos.test.mjs, manager refOptions, terminal.mjs +10 POS checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
// 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
|
||||
Reference in New Issue
Block a user