- Products: the categories and the list as two containers side by side (the tree sticky), the toolbar on the shared controls (search, filters, list/grid, New all one height), archive / delete red in a container. - Product editor: a wide dialog (Dialog size=wide: ~88% of a 16:9 screen, capped on ultrawides), the shared tab strip (no up/down scrollbar), one name and description with a language switch above them, unit (modern dropdown) and price on one row, 'Edit alternate prices' under Where it's sold (the product's own price per sales point: default / set / +- amount / +- %, rounding). - Pricing: shared tabs in the address, a new 'Order channel prices' tab (rules per product, category or sales point, multi-select, the most precise wins). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.8 KiB
JavaScript
77 lines
3.8 KiB
JavaScript
// Order channel prices (2026-11-20): a product's price at a sales point, as the backend makes it
|
|
// (services/channel_prices.py). Plain JS, unit-tested (tests/channelPricing.test.mjs).
|
|
// set the price there (one product only)
|
|
// add_amount ± euros on the default price
|
|
// add_percent ± % of the default price, then optionally rounded
|
|
// The most precise rule wins: the product's own > its category's > the whole channel's.
|
|
import { parseNumber } from '@xenia/i18n/number'
|
|
|
|
export const ACTIONS = ['set', 'add_amount', 'add_percent']
|
|
export const ROUNDINGS = ['', '0.05', '0.10', '0.20', '0.50', 'x.99', 'x.00']
|
|
export const roundingKey = r => (r ? r.replace('.', '_') : 'none')
|
|
|
|
const num = text => parseNumber(String(text ?? ''), { decimals: 2, allowNegative: true })
|
|
|
|
/** As services/pricing.py _round_to. */
|
|
export function roundTo(v, mode) {
|
|
if (mode === '0.05') return Math.round(v / 0.05) * 0.05
|
|
if (mode === '0.10') return Math.round(v / 0.10) * 0.10
|
|
if (mode === '0.20') return Math.round(v / 0.20) * 0.20
|
|
if (mode === '0.50') return Math.round(v / 0.50) * 0.50
|
|
if (mode === 'x.99') return v >= 1 ? Math.trunc(v) + 0.99 : v
|
|
if (mode === 'x.00') return Math.round(v)
|
|
return v
|
|
}
|
|
|
|
/** The price a rule makes from the default price (never below zero). */
|
|
export function applyRule(base, { action_type: a, value, round_to: r }) {
|
|
const v = Number(value) || 0
|
|
let out = a === 'set' ? v : a === 'add_amount' ? base + v : a === 'add_percent' ? base * (1 + v / 100) : base
|
|
out = Math.max(0, out)
|
|
return Math.round(roundTo(out, r || '') * 100) / 100
|
|
}
|
|
|
|
/** Which level a rule works at. */
|
|
export const levelOf = rule => (rule.product_ids?.length ? 'product' : rule.category_ids?.length ? 'category' : 'channel')
|
|
|
|
export const blankRule = () => ({ name: '', sales_points: [], product_ids: [], category_ids: [], action_type: 'add_percent', value: '', round_to: '', is_active: true })
|
|
|
|
export const ruleToForm = r => ({ ...blankRule(), ...r, name: r.name ?? '', value: String(r.value), round_to: r.round_to ?? '' })
|
|
|
|
/** What is wrong with the form: points, value, set_one (a set price needs exactly one product, no categories). */
|
|
export function ruleProblems(f) {
|
|
const out = []
|
|
if (!f.sales_points.length) out.push('points')
|
|
const v = num(f.value)
|
|
if (v == null || (f.action_type === 'set' && v < 0)) out.push('value')
|
|
if (f.action_type === 'set' && (f.product_ids.length !== 1 || f.category_ids.length)) out.push('set_one')
|
|
return out
|
|
}
|
|
|
|
export function ruleBody(f) {
|
|
return { name: f.name.trim() || null, sales_points: f.sales_points, product_ids: f.product_ids, category_ids: f.category_ids,
|
|
action_type: f.action_type, value: num(f.value), round_to: f.round_to || null, is_active: f.is_active }
|
|
}
|
|
|
|
/** The product editor's quick edit: a row per sales point → { action_type, value, round_to } or null. */
|
|
export function pointsBody(rows) {
|
|
return Object.fromEntries(rows.map(r => [r.sales_point, r.mode === 'default' ? null
|
|
: { action_type: r.mode, value: num(r.value), round_to: r.round_to || null }]))
|
|
}
|
|
|
|
export function pointRowFromApi(p) {
|
|
return { sales_point: p.sales_point, mode: p.own?.action_type ?? 'default', value: p.own ? String(p.own.value) : '',
|
|
round_to: p.own?.round_to ?? '', inherited: p.fallback ?? null, base: p.base_price }
|
|
}
|
|
|
|
/** The price a quick-edit row shows: its own rule's, else what the category / channel rule makes, else the default. */
|
|
export function rowPrice(row) {
|
|
if (row.mode !== 'default') {
|
|
const v = num(row.value)
|
|
return v == null ? null : applyRule(row.base, { action_type: row.mode, value: v, round_to: row.round_to })
|
|
}
|
|
return row.inherited ? applyRule(row.base, row.inherited) : row.base
|
|
}
|
|
|
|
export const rowProblem = row => row.mode !== 'default' && (num(row.value) == null || (row.mode === 'set' && num(row.value) < 0))
|