- POST /api/pos/sales: all checks first (shift, lines, payment split, cash given, discount → ordering.prices.modify or manager approval, fiscal VAT groups), then the existing open_order / add_items / pay_items functions, so tickets, KDS, price events, shift totals and the fiscal receipt work as for any order. - Cash drawer: printer_service.open_cash_drawer (ESC @ + ESC p 0 25 250) to the screen's receipt printer on cash, logged and audited (DRAWER_OPENED / DRAWER_FAILED). - orders._can_access_order: the people assigned to an order may act on it (a cashier's own takeaway order); pos_cart prices a batch with the lines before it, as add_items does. - Terminal: Charge → payment dialog (exact cash pre-selected, quick amounts, change, card, split by line), per-line discounts, sale summary and "Same again". - Tests: backend +6 (socket printer receives the drawer kick, fiscal file in the bridge folder), terminal unit +1, terminal.mjs +9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
6.0 KiB
JavaScript
101 lines
6.0 KiB
JavaScript
// The counter POS's menu and cart logic (plan step 3.3a). No prices here: they come from the
|
|
// backend; this checks what's chosen and how the cart groups it.
|
|
import assert from 'node:assert/strict'
|
|
import { test } from 'node:test'
|
|
import {
|
|
addLine, categoriesWithProducts, defaultSelection, fold, lineKey, missingChoices, needsChoices, pickChoice, pickChoiceSub,
|
|
pickOptionSub, pickShared, productsIn, searchProducts, setLineQty, setQuick, toLine, toggleOption, toggleRemoved,
|
|
} from '../src/apps/pos/logic/cart.js'
|
|
|
|
const burger = {
|
|
id: 1, name: 'Burger', categories: [{ category_id: 10, sort_order: 2 }, { category_id: 20, sort_order: 0 }],
|
|
quick: [{ id: 5, name: 'Cheese', allow_multiple: true }],
|
|
options: [{ id: 7, name: 'Sauce', multi_select: false, subs: [{ name: 'BBQ', is_default: true }, { name: 'Mayo' }] },
|
|
{ id: 8, name: 'Toppings', multi_select: true, subs: [{ name: 'Egg' }, { name: 'Bacon' }] }],
|
|
prefs: [{ id: 3, name: 'Cooking', multi: false, default_choice_id: null,
|
|
shared: { name: 'Side', subs: [{ name: 'Fries', is_default: true }, { name: 'Salad' }] },
|
|
choices: [{ id: 30, name: 'Rare', subs: [] }, { id: 31, name: 'Well done', disables_shared: true, subs: [] },
|
|
{ id: 32, name: 'Medium', subs: [{ name: 'pink' }, { name: 'not pink', is_default: true }] }] }],
|
|
ingredients: [{ id: 9, name: 'Onion' }],
|
|
}
|
|
const coffee = { id: 2, name: 'Φρέντο Εσπρέσο', categories: [{ category_id: 20, sort_order: 1 }], quick: [], options: [], prefs: [], ingredients: [] }
|
|
|
|
test('search ignores accents, case and word order; finds a product once', () => {
|
|
assert.equal(fold('Φρέντο'), 'φρεντο')
|
|
assert.deepEqual(searchProducts([burger, coffee], 'εσπρεσο φρεντ').map(p => p.id), [2])
|
|
assert.deepEqual(searchProducts([burger, coffee], 'bur').map(p => p.id), [1])
|
|
assert.deepEqual(searchProducts([burger, coffee], ' '), [])
|
|
})
|
|
|
|
test('categories: a product listed in several shows in each, in that category\'s order', () => {
|
|
assert.deepEqual(productsIn([burger, coffee], 20).map(p => p.id), [1, 2])
|
|
assert.deepEqual(productsIn([burger, coffee], 10).map(p => p.id), [1])
|
|
assert.equal(productsIn([burger, coffee], null).length, 2)
|
|
assert.deepEqual(categoriesWithProducts([{ id: 10 }, { id: 20 }, { id: 30 }], [burger, coffee]).map(c => c.id), [10, 20])
|
|
})
|
|
|
|
test('choices: a preference must be picked; subs and shared subs come with their defaults', () => {
|
|
assert.equal(needsChoices(burger), true)
|
|
assert.equal(needsChoices(coffee), false)
|
|
let sel = defaultSelection(burger)
|
|
assert.deepEqual(missingChoices(burger, sel), [{ kind: 'pref', name: 'Cooking' }])
|
|
sel = pickChoice(burger, sel, 3, 30)
|
|
assert.deepEqual(sel.prefs[3], { choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' })
|
|
assert.deepEqual(missingChoices(burger, sel), [])
|
|
sel = pickShared(sel, 3, 'Salad')
|
|
sel = pickChoice(burger, sel, 3, 31) // well done: no side
|
|
assert.equal(sel.prefs[3].shared, null)
|
|
sel = pickChoice(burger, sel, 3, 32) // medium: its default sub, side comes back
|
|
assert.deepEqual(sel.prefs[3], { choices: [{ id: 32, qty: 1, sub: 'not pink' }], shared: 'Fries' })
|
|
sel = pickChoiceSub(sel, 3, 'pink')
|
|
assert.equal(sel.prefs[3].choices[0].sub, 'pink')
|
|
})
|
|
|
|
test('extras, add-ons, without, note → the line the backend prices', () => {
|
|
let sel = pickChoice(burger, defaultSelection(burger), 3, 30)
|
|
sel = toggleOption(burger, sel, 7) // Sauce with BBQ
|
|
sel = pickOptionSub(burger, sel, 7, 'Mayo') // single: replaces
|
|
sel = toggleOption(burger, sel, 8) // the first sub comes pre-selected (as on the waiter app)
|
|
assert.deepEqual(sel.options[8].subs, ['Egg'])
|
|
sel = pickOptionSub(burger, sel, 8, 'Egg') // multi: tapping it again takes it off…
|
|
assert.deepEqual(missingChoices(burger, sel), [{ kind: 'option_sub', name: 'Toppings' }])
|
|
sel = pickOptionSub(burger, sel, 8, 'Egg')
|
|
sel = pickOptionSub(burger, sel, 8, 'Bacon') // …and adds the others
|
|
sel = setQuick(sel, 5, 2)
|
|
sel = toggleRemoved(sel, 9)
|
|
sel = { ...sel, notes: ' no salt ' }
|
|
assert.deepEqual(toLine(burger, sel, 1), {
|
|
product_id: 1, quantity: 1, quick: [{ id: 5, qty: 2 }],
|
|
options: [{ id: 7, qty: 1, subs: ['Mayo'] }, { id: 8, qty: 1, subs: ['Egg', 'Bacon'] }],
|
|
prefs: [{ set_id: 3, choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' }],
|
|
removed: [9], notes: 'no salt',
|
|
})
|
|
assert.deepEqual(toLine(coffee, defaultSelection(coffee), 2), { product_id: 2, quantity: 2 })
|
|
assert.deepEqual(setQuick(sel, 5, 0).quick, {})
|
|
})
|
|
|
|
test('cart: the same choices add up, different choices are separate lines; 0 removes', () => {
|
|
const a = { product_id: 2, quantity: 1 }
|
|
const b = { product_id: 1, quantity: 1, prefs: [{ set_id: 3, choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' }] }
|
|
const c = { product_id: 1, quantity: 1, prefs: [{ set_id: 3, choices: [{ id: 31, qty: 1, sub: null }], shared: null }] }
|
|
let cart = addLine(addLine(addLine([], a), b), a)
|
|
assert.deepEqual(cart.map(l => [l.product_id, l.quantity]), [[2, 2], [1, 1]])
|
|
cart = addLine(cart, c)
|
|
assert.equal(cart.length, 3)
|
|
assert.equal(lineKey({ ...b, quantity: 5 }), lineKey(b))
|
|
assert.notEqual(lineKey(b), lineKey(c))
|
|
assert.deepEqual(setLineQty(cart, 0, 0).map(l => l.product_id), [1, 1])
|
|
assert.equal(setLineQty(cart, 0, 0.30000000000000004)[0].quantity, 0.3)
|
|
})
|
|
|
|
test('paying: quick amounts, change, split parts (3.3b)', async () => {
|
|
const { changeFor, quickAmounts, splitPayments } = await import('../src/apps/pos/logic/cart.js')
|
|
assert.deepEqual(quickAmounts(12.2), [12.2, 15, 20, 50, 100])
|
|
assert.deepEqual(quickAmounts(20), [20, 25, 30, 40, 50])
|
|
assert.equal(changeFor(20, 16), 4)
|
|
assert.equal(changeFor(15, 16), null)
|
|
assert.equal(changeFor(16, 16), 0)
|
|
assert.deepEqual(splitPayments(['card', 'cash', 'cash'], [10, 3, 2.5]),
|
|
{ cash: 5.5, card: 10, payments: [{ method: 'card', lines: [0] }, { method: 'cash', lines: [1, 2] }] })
|
|
})
|