Files
xenia-pos-local/terminal/tests/pos.test.mjs
T
bonaminandClaude Opus 5.5 f2182dcb37 feat(pos): refunds at the counter on a fiscal driver layer (platform-v2 3.3e)
- services/fiscal/: one driver interface (prepare_receipt / prepare_credit_note -> send,
  credit_notes flag, status); the dTEC100extra folder bridge moved into txt_file.py as the
  first driver, same command files. fiscal_service.py removed, callers moved.
- Refunds: models/refund.py + Alembic 0016 (refunds, refund_items; ordering.refunds for
  existing owners/managers). services/refunds.py: today's business day, the cashier's open
  shift, partial quantities, items from another shift need ordering.refunds or a manager's
  approval (check()/authorize() return the override row). Paid items are never edited.
- POS: GET /api/pos/receipts, GET /api/pos/receipts/{id}, POST /api/pos/orders/{id}/refund
  (cash opens the drawer; fiscal "manual" when the driver can't issue credit notes).
- Shift totals subtract refunds and now count discounts; day close and business-day
  reports carry refunds.
- Terminal: Receipts mode (ReceiptsView, ReceiptPanel, logic/refunds.js), en/el texts.
- Tests: test_pos_refunds.py (12), terminal unit +1, terminal.mjs +7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:44:17 +02:00

176 lines
11 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.
// 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] }] })
})
test('tables at the counter: state, zones, order lines, what Pay takes (3.3c)', async () => {
const { itemPayments, optionNames, orderLines, orderSums, qtyLabel, tableState, tablesIn, toPay, zoneCounts } = await import('../src/apps/pos/logic/tables.js')
const free = { id: 1, group_id: 7, order: null }
const mine = { id: 2, group_id: 7, order: { items: 2, unpaid: 6, total: 6, mine: true } }
const theirs = { id: 3, group_id: 8, order: { items: 1, unpaid: 3, total: 3, mine: false } }
const paid = { id: 4, group_id: 8, order: { items: 1, unpaid: 0, total: 3, mine: false } }
assert.deepEqual([free, mine, theirs, paid].map(tableState), ['free', 'mine', 'open', 'paid'])
assert.deepEqual(tablesIn([free, mine, theirs], 8).map(t => t.id), [3])
assert.deepEqual(zoneCounts([free, mine, theirs, paid]), { all: { total: 4, busy: 3 }, 7: { total: 2, busy: 1 }, 8: { total: 2, busy: 2 } })
assert.deepEqual(optionNames('[{"name":"Rare"},{"name":"Fries"}]'), ['Rare', 'Fries'])
assert.deepEqual(optionNames('not json'), [])
const order = { items: [
{ id: 10, product_name: 'Freddo', quantity: 2, unit_price: 3, price_adjustment: 0, status: 'paid', payment_method: 'cash' },
{ id: 11, product_name: 'Burger', quantity: 1, unit_price: 10, price_adjustment: -1, status: 'active', selected_options: '[{"name":"Rare"}]' },
{ id: 12, product_name: 'Old', quantity: 1, unit_price: 5, status: 'cancelled' },
{ id: 13, product_name: 'Wine', quantity: 0.5, unit_type: 'liter', unit_price: 12, status: 'ready', kds_status: 'done' },
] }
const lines = orderLines(order)
assert.deepEqual(lines.map(l => [l.id, l.total, l.paid]), [[11, 9, false], [13, 6, false], [10, 6, true]])
assert.deepEqual(lines[0].options, ['Rare'])
assert.deepEqual(orderSums(lines), { total: 21, paid: 6, unpaid: 15, unpaidIds: [11, 13] })
assert.deepEqual(toPay(lines, []).map(l => l.id), [11, 13]) // nothing selected → everything unpaid
assert.deepEqual(toPay(lines, [13, 10]).map(l => l.id), [13]) // a paid line can't be chosen
const rows = toPay(lines, []).map(l => ({ id: l.id, total: l.total }))
assert.deepEqual(itemPayments([{ method: 'card', lines: [1] }, { method: 'cash', lines: [0] }], rows),
[{ method: 'card', item_ids: [13] }, { method: 'cash', item_ids: [11] }])
assert.equal(qtyLabel(2, 'piece'), '2×')
assert.equal(qtyLabel(0.5, 'liter'), '0.5 L')
})
test('online orders: the next step and finding an order (3.3d)', async () => {
const { findOnline, minutesSince, nextStep } = await import('../src/apps/pos/logic/online.js')
assert.deepEqual(nextStep({ status: 'pending_acceptance' }), { kind: 'answer' })
assert.deepEqual(nextStep({ status: 'accepted' }), { kind: 'status', to: 'preparing' })
assert.deepEqual(nextStep({ status: 'preparing', type: 'delivery' }), { kind: 'status', to: 'out_for_delivery' })
assert.deepEqual(nextStep({ status: 'preparing', type: 'dine_in' }), { kind: 'status', to: 'ready' })
assert.deepEqual(nextStep({ status: 'out_for_delivery', unpaid: 22 }), { kind: 'finish', pay: true })
assert.deepEqual(nextStep({ status: 'ready', unpaid: 0 }), { kind: 'finish', pay: false })
assert.equal(nextStep({ status: 'delivered' }), null)
assert.equal(nextStep({ status: 'rejected' }), null)
const board = { incoming: [{ id: 1 }], in_progress: [{ id: 2 }], finished: [{ id: 3 }] }
assert.equal(findOnline(board, 2).id, 2)
assert.equal(findOnline(board, 3).id, 3)
assert.equal(findOnline(board, 9), null)
assert.equal(findOnline(null, 1), null)
const at = Date.parse('2026-11-15T10:00:00Z')
assert.equal(minutesSince('2026-11-15T09:57:30Z', at), 2)
assert.equal(minutesSince('2026-11-15T10:01:00Z', at), 0)
})
test('refunds: picking items, quantities, total, method, approval (3.3e)', async () => {
const { byPiece, defaultMethod, needsApproval, pickAll, refundLines, refundTotal, setPickQty, togglePick } = await import('../src/apps/pos/logic/refunds.js')
const coffee = { id: 1, unit_type: 'piece', refundable: 2, unit_total: 3, method: 'cash', mine: true }
const feta = { id: 2, unit_type: 'kg', refundable: 0.25, unit_total: 12, method: 'card', mine: true }
const done = { id: 3, unit_type: 'piece', refundable: 0, unit_total: 5, method: 'card', mine: false }
const items = [coffee, feta, done]
assert.equal(byPiece(coffee), true)
assert.equal(byPiece(feta), false)
let pick = togglePick({}, coffee)
assert.deepEqual(pick, { 1: 2 })
pick = setPickQty(pick, coffee, 1)
assert.deepEqual(refundLines(items, pick), [{ item_id: 1, quantity: 1 }])
assert.equal(refundTotal(items, pick), 3)
assert.deepEqual(setPickQty(pick, coffee, 9), { 1: 2 }, 'not more than what is left')
assert.deepEqual(setPickQty(pick, coffee, 0), {}, '0 removes it')
assert.deepEqual(togglePick(pick, coffee), {}, 'a second tap removes it')
assert.deepEqual(togglePick({}, done), {}, 'nothing left to refund')
assert.deepEqual(pickAll(items), { 1: 2, 2: 0.25 })
assert.equal(refundTotal(items, pickAll(items)), 9)
assert.equal(defaultMethod(items, { 2: 0.25 }), 'card')
assert.equal(defaultMethod(items, pickAll(items)), 'cash')
assert.equal(needsApproval(items, pickAll(items)), false)
assert.equal(needsApproval([{ ...coffee, mine: false }], { 1: 1 }), true)
})