// 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]}` }