Owner 2026-11-23 (plan 6F.2): new DecimalInput / MoneyInput in @xenia/ui (keeps what is typed,
reports a number, null when empty, NaN when not a number) on parseNumber. The waiter app's starting
cash, weighed quantity and typed prices were type=number + parseFloat (a Greek keypad's comma was
dropped: 12,50 became 12 or nothing); now text fields read with parseNumber, and a wrong starting
cash shows a message. The manager's price rules, deals and order-total condition use the new
fields; every Number(x.replace(',', '.')) now reads through parseNumber (1.234,50 too). Guard test
packages/i18n/tests/typed-numbers.test.mjs refuses parseFloat, .replace(',', '.') and decimal
type=number fields in the apps. e2e: shift close with 20,50, waiter starting cash 25,50.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
7.2 KiB
JavaScript
153 lines
7.2 KiB
JavaScript
// Delivery areas and fee rules, the manager's side (plan phase 3S, step S.3). The rules are
|
|
// evaluated by the backend's engine (local_backend/services/fees.py, the same file the cloud
|
|
// runs); this file only turns forms into request bodies and rules into readable lines.
|
|
// Pure: unit-tested in Node (tests/fees.test.mjs).
|
|
import { parseNumber } from '@xenia/i18n/number'
|
|
|
|
export const ACTIONS = ['set', 'add', 'percent', 'free', 'minimum']
|
|
export const DAYS = [0, 1, 2, 3, 4, 5, 6] // Monday = 0
|
|
|
|
const num = v => (v === '' || v == null ? null : (parseNumber(String(v)) ?? NaN))
|
|
|
|
/** Postcodes typed with commas, semicolons or new lines → a tidy list without duplicates. */
|
|
export function parsePostcodes(text) {
|
|
const out = []
|
|
for (const raw of String(text ?? '').split(/[,;\n]/)) {
|
|
const code = raw.trim().replace(/\s+/g, ' ')
|
|
if (code && !out.includes(code)) out.push(code)
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** An empty rule form (a delivery fee by default). */
|
|
export function emptyRule() {
|
|
return {
|
|
name: '', is_active: true, sales_points: ['online_delivery'], area_ids: [],
|
|
subtotal_min: '', subtotal_max: '', items_min: '', items_max: '', days: [],
|
|
time_from: '', time_to: '', action: 'set', value: '', stop: false,
|
|
}
|
|
}
|
|
|
|
/** A saved rule → the form (numbers as text, nulls as empty). */
|
|
export function ruleToForm(rule) {
|
|
const f = emptyRule()
|
|
for (const k of Object.keys(f)) {
|
|
const v = rule[k]
|
|
f[k] = v == null ? f[k] : (typeof f[k] === 'string' && typeof v === 'number' ? String(v) : v)
|
|
}
|
|
return f
|
|
}
|
|
|
|
/** The form → the request body (POST/PUT /api/connect/fees/rules). */
|
|
export function ruleBody(form) {
|
|
const int = v => (num(v) == null ? null : Math.round(num(v)))
|
|
return {
|
|
name: form.name.trim(), is_active: !!form.is_active, sales_points: form.sales_points, area_ids: form.area_ids,
|
|
subtotal_min: num(form.subtotal_min), subtotal_max: num(form.subtotal_max),
|
|
items_min: int(form.items_min), items_max: int(form.items_max), days: [...form.days].sort((a, b) => a - b),
|
|
time_from: form.time_from || null, time_to: form.time_to || null,
|
|
action: form.action, value: form.action === 'free' ? 0 : (num(form.value) ?? 0), stop: !!form.stop,
|
|
}
|
|
}
|
|
|
|
/** What the form still needs before it can be saved: a list of message keys (empty = ok). */
|
|
export function ruleProblems(form) {
|
|
const out = []
|
|
if (!form.name.trim()) out.push('name')
|
|
const v = num(form.value)
|
|
if (form.action !== 'free' && (v == null || Number.isNaN(v))) out.push('value')
|
|
if (form.action !== 'add' && v != null && v < 0) out.push('negative')
|
|
if (form.action === 'percent' && v > 100) out.push('percent')
|
|
const lo = num(form.subtotal_min); const hi = num(form.subtotal_max)
|
|
if (lo != null && hi != null && lo >= hi) out.push('subtotalRange')
|
|
const il = num(form.items_min); const ih = num(form.items_max)
|
|
if (il != null && ih != null && il > ih) out.push('itemsRange')
|
|
if (!!form.time_from !== !!form.time_to) out.push('times')
|
|
if (!form.sales_points.length) out.push('salesPoints')
|
|
return out
|
|
}
|
|
|
|
/** One rule as readable parts: { when: [[key, params]], action: [key, params] }, for t(). */
|
|
export function describeRule(rule, areaNames = {}) {
|
|
const when = []
|
|
if (rule.area_ids?.length) when.push(['areas', { names: rule.area_ids.map(id => areaNames[id] ?? `#${id}`).join(', ') }])
|
|
if (rule.subtotal_min != null && rule.subtotal_max != null) when.push(['subtotalBetween', { min: rule.subtotal_min, max: rule.subtotal_max }])
|
|
else if (rule.subtotal_min != null) when.push(['subtotalFrom', { min: rule.subtotal_min }])
|
|
else if (rule.subtotal_max != null) when.push(['subtotalBelow', { max: rule.subtotal_max }])
|
|
if (rule.items_min != null && rule.items_max != null) when.push(['itemsBetween', { min: rule.items_min, max: rule.items_max }])
|
|
else if (rule.items_min != null) when.push(['itemsFrom', { min: rule.items_min }])
|
|
else if (rule.items_max != null) when.push(['itemsUpTo', { max: rule.items_max }])
|
|
if (rule.days?.length && rule.days.length < 7) when.push(['days', { days: rule.days }])
|
|
if (rule.time_from && rule.time_to) when.push(['time', { from: rule.time_from, to: rule.time_to }])
|
|
return { when, action: [rule.action, { value: rule.value }] }
|
|
}
|
|
|
|
/** The rules in a new order after moving one up (-1) or down (+1): their ids. */
|
|
export function moveRule(rules, id, step) {
|
|
const ids = rules.map(r => r.id)
|
|
const i = ids.indexOf(id)
|
|
const j = i + step
|
|
if (i < 0 || j < 0 || j >= ids.length) return ids
|
|
;[ids[i], ids[j]] = [ids[j], ids[i]]
|
|
return ids
|
|
}
|
|
|
|
/** "Test an order" form → the preview body. */
|
|
export function previewBody(form) {
|
|
return {
|
|
sales_point: form.sales_point,
|
|
area_id: form.where === 'area' && form.area_id !== '' ? Number(form.area_id) : null,
|
|
postcode: form.where === 'postcode' ? form.postcode.trim() || null : null,
|
|
subtotal: num(form.subtotal) ?? 0, items: Math.max(0, Math.round(num(form.items) ?? 0)),
|
|
weekday: form.day === 'now' ? null : Number(form.day),
|
|
time: form.day === 'now' ? null : (form.time || null),
|
|
}
|
|
}
|
|
|
|
/** Online ordering settings (S.4) → the form: hours as editable rows per day. */
|
|
const toDays = hours => DAYS.map(d => (hours?.[String(d)] ?? []).map(([from, to]) => ({ from, to })))
|
|
function toHours(anyTime, days) {
|
|
const hours = {}
|
|
if (!anyTime) {
|
|
days.forEach((ranges, d) => {
|
|
const clean = ranges.filter(r => r.from && r.to && r.from !== r.to).map(r => [r.from, r.to])
|
|
if (clean.length) hours[String(d)] = clean
|
|
})
|
|
}
|
|
return hours
|
|
}
|
|
const int = (v, dflt) => (Number.isFinite(Number(v)) && String(v).trim() !== '' ? Math.round(Number(v)) : dflt)
|
|
|
|
/** Table bookings online (S.6): the defaults the backend has. */
|
|
export const RES_DEFAULTS = { enabled: false, with_food: false, hours: {}, slot_minutes: 30, days_ahead: 30, notice_minutes: 60, party_max: 10, fire_minutes: 20 }
|
|
|
|
export function orderingToForm(o) {
|
|
const hours = o?.hours ?? {}
|
|
const r = { ...RES_DEFAULTS, ...(o?.reservations ?? {}) }
|
|
return {
|
|
services: o?.services ?? [], anyTime: Object.keys(hours).length === 0,
|
|
days: toDays(hours),
|
|
prep_minutes: String(o?.prep_minutes ?? 20), slot_minutes: String(o?.slot_minutes ?? 15),
|
|
days_ahead: String(o?.days_ahead ?? 0), asap: o?.asap ?? true,
|
|
res: {
|
|
enabled: !!r.enabled, with_food: !!r.with_food, anyTime: Object.keys(r.hours ?? {}).length === 0, days: toDays(r.hours),
|
|
slot_minutes: String(r.slot_minutes), days_ahead: String(r.days_ahead), notice_minutes: String(r.notice_minutes),
|
|
party_max: String(r.party_max), fire_minutes: String(r.fire_minutes),
|
|
},
|
|
}
|
|
}
|
|
|
|
/** The form → PUT /api/connect/fees/ordering. A day without ranges is closed (unless any time). */
|
|
export function orderingBody(form) {
|
|
const r = form.res ?? orderingToForm(null).res
|
|
return {
|
|
services: form.services, hours: toHours(form.anyTime, form.days), prep_minutes: int(form.prep_minutes, 20), slot_minutes: int(form.slot_minutes, 15),
|
|
days_ahead: int(form.days_ahead, 0), asap: !!form.asap,
|
|
reservations: {
|
|
enabled: !!r.enabled, with_food: !!r.enabled && !!r.with_food, hours: toHours(r.anyTime, r.days),
|
|
slot_minutes: int(r.slot_minutes, 30), days_ahead: int(r.days_ahead, 30), notice_minutes: int(r.notice_minutes, 60),
|
|
party_max: int(r.party_max, 10), fire_minutes: int(r.fire_minutes, 20),
|
|
},
|
|
}
|
|
}
|