// The pricing page's rules as plain data (plan step 6.7): the forms of a price group, a price rule // (modifier) and a deal, what they send to the server, what's missing before they can be saved, // and their one-line summaries. Tested in tests/pricing-logic.test.mjs. // The server's engine: local_backend/services/pricing.py (conditions AND-ed, targets OR-ed). export const WEEKDAYS = [0, 1, 2, 3, 4, 5, 6] // Monday = 0, as the server counts // conditions a rule can have; the deals take fewer export const MODIFIER_CONDITIONS = ['time_range', 'day_of_week', 'date_range', 'specific_date', 'price_group_active', 'scene_active', 'order_channel', 'min_item_quantity', 'min_category_qty', 'min_order_value'] export const DEAL_CONDITIONS = ['min_item_quantity', 'min_order_value', 'time_range', 'day_of_week', 'date_range', 'order_channel', 'price_group_active', 'scene_active'] export const OLD_PLACEHOLDERS = ['user_tier', 'low_stock'] // never did anything; shown, not offered export const MODIFIER_TARGETS = ['all', 'item', 'category', 'prep_zone', 'tag'] export const DEAL_TARGETS = ['any', 'item', 'category', 'tag'] export const MODIFIER_ACTIONS = ['add_percent', 'add_amount', 'set'] export const DEAL_ACTIONS = ['free_item', 'free_choice', 'apply_modifier', 'set_price', 'add_amount', 'add_percent'] export const ROUNDINGS = ['', '0.05', '0.10', '0.20', '0.50', 'x.99', 'x.00'] export const FREE_CHOICE_FROM = ['item', 'category', 'tag'] /** A condition of a type with its starting values. */ export function blankCondition(type) { switch (type) { case 'time_range': return { condition_type: type, params: { from: '17:00', to: '19:00' } } case 'date_range': return { condition_type: type, params: { from: '', to: '' } } case 'specific_date': return { condition_type: type, params: { dates: [] } } case 'day_of_week': return { condition_type: type, params: { days: [] } } case 'min_item_quantity': return { condition_type: type, params: { min: 2 } } case 'min_category_qty': return { condition_type: type, params: { category_id: null, min: 2 } } case 'min_order_value': return { condition_type: type, params: { min: 20 } } case 'order_channel': return { condition_type: type, params: { channels: [] } } case 'price_group_active': return { condition_type: type, params: { price_group_id: null } } case 'scene_active': return { condition_type: type, params: { scene_id: null } } // a scene is on (2026-11-21) default: return { condition_type: type, params: {} } } } const HHMM = /^([01]\d|2[0-3]):[0-5]\d$/ const DAY = /^\d{4}-\d{2}-\d{2}$/ const positive = (v, low) => typeof v === 'number' && Number.isFinite(v) && v >= low /** Whether a condition is complete (the server refuses an incomplete one: pricing.bad_condition). */ export function conditionComplete({ condition_type: type, params: p = {} }) { switch (type) { case 'time_range': return HHMM.test(p.from ?? '') && HHMM.test(p.to ?? '') && p.from !== p.to case 'date_range': return DAY.test(p.from ?? '') && DAY.test(p.to ?? '') && p.from <= p.to case 'specific_date': return (p.dates ?? []).length > 0 && p.dates.every(d => DAY.test(d)) case 'day_of_week': return (p.days ?? []).length > 0 case 'min_item_quantity': return positive(p.min ?? 1, 1) case 'min_category_qty': return Number.isInteger(p.category_id) && positive(p.min ?? 1, 1) case 'min_order_value': return positive(p.min ?? 0, 0) case 'order_channel': return (p.channels ?? []).length > 0 case 'price_group_active': return Number.isInteger(p.price_group_id) case 'scene_active': return Number.isInteger(p.scene_id) default: return OLD_PLACEHOLDERS.includes(type) } } export const toggleIn = (list, v) => (list.includes(v) ? list.filter(x => x !== v) : [...list, v]) /** Move an item of a list from one place to another (drag, or the arrows). */ export function moveItem(list, from, to) { if (from === to || from < 0 || to < 0 || from >= list.length || to >= list.length) return list const out = [...list] const [x] = out.splice(from, 1) out.splice(to, 0, x) return out } // ── price groups ────────────────────────────────────────────────────────────── export const blankGroup = colour => ({ name: '', description: '', color: colour, is_active: false, auto_enable_time: '', auto_disable_time: '', auto_days: [] }) export const groupForm = (g, colour) => (g ? { ...blankGroup(colour), ...g, description: g.description ?? '', auto_enable_time: g.auto_enable_time ?? '', auto_disable_time: g.auto_disable_time ?? '', auto_days: g.auto_days ?? [] } : blankGroup(colour)) /** A schedule is all three (from, to, a day) or nothing. */ export function groupProblems(f) { const out = [] if (!f.name.trim()) out.push('name') const parts = [f.auto_enable_time, f.auto_disable_time, f.auto_days.length ? 'x' : ''].filter(Boolean).length if (parts && (parts < 3 || !HHMM.test(f.auto_enable_time) || !HHMM.test(f.auto_disable_time) || f.auto_enable_time === f.auto_disable_time)) out.push('schedule') return out } export function groupBody(f) { const scheduled = f.auto_enable_time && f.auto_disable_time && f.auto_days.length return { name: f.name.trim(), description: f.description.trim() || null, color: f.color, is_active: !!f.is_active, auto_enable_time: scheduled ? f.auto_enable_time : null, auto_disable_time: scheduled ? f.auto_disable_time : null, auto_days: scheduled ? [...f.auto_days].sort() : null } } export const groupSchedule = g => (g.auto_enable_time && g.auto_disable_time && g.auto_days?.length ? { from: g.auto_enable_time, to: g.auto_disable_time, days: [...g.auto_days].sort() } : null) // ── price rules (modifiers) ─────────────────────────────────────────────────── const ids = t => t.target_ids ?? (t.target_id != null ? [t.target_id] : []) const tags = t => t.target_tags ?? (t.target_tag ? [t.target_tag] : []) export const normalizeTarget = t => ({ target_type: t.target_type, target_ids: ids(t), target_tags: tags(t) }) export const blankModifier = colour => ({ name: '', description: '', color: colour, is_active: true, is_favorite: false, allow_stack: false, sort_order: 0, scope: 'global', item_id: null, action_type: 'add_percent', action_value: -10, round_to: '', conditions: [], targets: [{ target_type: 'all', target_ids: [], target_tags: [] }], }) export const modifierForm = (m, colour) => (m ? { ...blankModifier(colour), ...m, description: m.description ?? '', round_to: m.round_to ?? '', conditions: (m.conditions ?? []).map(c => ({ condition_type: c.condition_type, params: c.params ?? {} })), targets: (m.targets ?? []).map(normalizeTarget), } : blankModifier(colour)) const targetComplete = t => ['all', 'any'].includes(t.target_type) || (t.target_type === 'tag' ? t.target_tags.length > 0 : t.target_ids.length > 0) export function modifierProblems(f) { const out = [] if (!f.name.trim()) out.push('name') if (f.scope === 'item' && !f.item_id) out.push('product') if (f.scope === 'global' && (!f.targets.length || !f.targets.every(targetComplete))) out.push('targets') if (!f.conditions.every(conditionComplete)) out.push('conditions') if (!Number.isFinite(f.action_value) || (f.action_type === 'set' && f.action_value < 0)) out.push('value') return out } export function modifierBody(f) { return { name: f.name.trim(), description: f.description.trim() || null, color: f.color, is_active: !!f.is_active, is_favorite: !!f.is_favorite, allow_stack: !!f.allow_stack, sort_order: f.sort_order ?? 0, scope: f.scope, item_id: f.scope === 'item' ? f.item_id : null, action_type: f.action_type, action_value: f.action_value, round_to: f.action_type === 'add_percent' && f.round_to ? f.round_to : null, conditions: f.conditions.map(c => ({ condition_type: c.condition_type, params: c.params })), targets: f.scope === 'global' ? f.targets.map(t => ({ target_type: t.target_type, target_ids: t.target_type === 'tag' || t.target_type === 'all' ? null : t.target_ids, target_id: t.target_type === 'tag' || t.target_type === 'all' ? null : t.target_ids[0] ?? null, target_tags: t.target_type === 'tag' ? t.target_tags : null, target_tag: t.target_type === 'tag' ? t.target_tags[0] ?? null : null, })) : [], } } /** "−10%", "+€0.50", "= €3.00" (money formatted by the caller's formatter). */ export function actionText(type, value, money) { const v = Number(value ?? 0) if (type === 'set' || type === 'set_price') return `= ${money(v)}` const sign = v < 0 ? '−' : '+' if (type === 'add_amount') return `${sign}${money(Math.abs(v))}` return `${sign}${Math.abs(v)}%` } /** What a percentage or amount does to a price (the dialog's example), rounded like the server. */ export function examplePrice(base, type, value, roundTo) { let p = type === 'set' ? value : type === 'add_amount' ? base + value : base * (1 + value / 100) p = Math.max(p, 0) if (type === 'add_percent' && roundTo) { const step = { '0.05': 0.05, '0.10': 0.1, '0.20': 0.2, '0.50': 0.5 }[roundTo] if (step) p = Math.round(p / step) * step else if (roundTo === 'x.99') p = p >= 1 ? Math.floor(p) + 0.99 : p else if (roundTo === 'x.00') p = Math.round(p) } return Math.round(Math.round(p / 0.1) * 0.1 * 100) / 100 // the system's final rounding to 0.10 } // ── deals ───────────────────────────────────────────────────────────────────── export const blankDeal = colour => ({ name: '', description: '', color: colour, is_active: true, sort_order: 0, action_type: 'free_item', action_modifier_id: null, action_value: 0, action_free_item_id: null, action_free_target_type: 'item', action_free_target_ids: [], action_free_quantity: 1, conditions: [], targets: [{ target_type: 'any', target_ids: [], target_tags: [] }], }) export const dealForm = (d, colour) => (d ? { ...blankDeal(colour), ...d, description: d.description ?? '', action_value: d.action_value ?? 0, action_free_target_type: d.action_free_target_type ?? 'item', action_free_target_ids: d.action_free_target_ids ?? [], conditions: (d.conditions ?? []).map(c => ({ condition_type: c.condition_type, params: c.params ?? {} })), targets: (d.targets?.length ? d.targets : [{ target_type: 'any' }]).map(normalizeTarget), } : blankDeal(colour)) export function dealProblems(f) { const out = [] if (!f.name.trim()) out.push('name') if (!f.targets.length || !f.targets.every(targetComplete)) out.push('targets') if (!f.conditions.every(conditionComplete)) out.push('conditions') const a = f.action_type if (a === 'apply_modifier' && !f.action_modifier_id) out.push('modifier') if (a === 'free_item' && !f.action_free_item_id) out.push('freeItem') if (a === 'free_choice' && !f.action_free_target_ids.length) out.push('freeChoice') if (['set_price', 'add_amount', 'add_percent'].includes(a) && !Number.isFinite(f.action_value)) out.push('value') if (['free_item', 'free_choice'].includes(a) && !(f.action_free_quantity >= 1)) out.push('quantity') return out } export function dealBody(f) { const a = f.action_type return { name: f.name.trim(), description: f.description.trim() || null, color: f.color, is_active: !!f.is_active, sort_order: f.sort_order ?? 0, action_type: a, action_modifier_id: a === 'apply_modifier' ? f.action_modifier_id : null, action_value: ['set_price', 'add_amount', 'add_percent'].includes(a) ? f.action_value : null, action_free_item_id: a === 'free_item' ? f.action_free_item_id : null, action_free_target_type: a === 'free_choice' ? f.action_free_target_type : null, action_free_target_ids: a === 'free_choice' ? f.action_free_target_ids : null, action_free_quantity: ['free_item', 'free_choice'].includes(a) ? f.action_free_quantity : 1, conditions: f.conditions.map(c => ({ condition_type: c.condition_type, params: c.params })), targets: f.targets.map(t => ({ target_type: t.target_type, target_ids: ['item', 'category'].includes(t.target_type) ? t.target_ids : null, target_id: ['item', 'category'].includes(t.target_type) ? t.target_ids[0] ?? null : null, target_tags: t.target_type === 'tag' ? t.target_tags : null, target_tag: t.target_type === 'tag' ? t.target_tags[0] ?? null : null, })), } } // ── discount limits ─────────────────────────────────────────────────────────── export const VENUE_LIMITS = ['max_total_value_workday', 'max_total_value_shift', 'max_items_per_shift'] export const WAITER_LIMITS = ['max_discount_percent', 'max_discount_amount', 'max_total_value_shift', 'max_total_value_workday', 'max_items_per_shift', 'max_items_per_workday', 'max_items_per_order'] export const COUNT_LIMITS = new Set(['max_items_per_shift', 'max_items_per_workday', 'max_items_per_order']) /** A limit typed in a box: '' = no limit; a comma works as the decimal point. null when it isn't a number ≥ 0. */ export function parseLimit(text, count = false) { const s = String(text ?? '').trim().replace(',', '.') if (s === '') return { ok: true, value: null } const v = Number(s) if (!Number.isFinite(v) || v < 0 || (count && !Number.isInteger(v))) return { ok: false, value: null } return { ok: true, value: v } } export const limitText = v => (v == null ? '' : String(v))