From 476c55abc0e95458a7dc0147a0744e943a99b887 Mon Sep 17 00:00:00 2001 From: bonamin Date: Fri, 20 Nov 2026 15:11:35 +0200 Subject: [PATCH] feat(manager): products on the page rule, a wide product editor, alternate prices per sales point - 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 --- .../src/pages/pricing/ChannelPricesTab.jsx | 152 ++++++++++++++++++ .../src/pages/pricing/PricingPage.jsx | 37 ++--- .../pricing/ProductChannelPricesDialog.jsx | 95 +++++++++++ .../src/pages/pricing/channelPricing.js | 76 +++++++++ .../src/pages/pricing/pricingApi.js | 7 + .../src/pages/pricing/useActionText.js | 15 ++ .../src/pages/products/CategoryTree.jsx | 6 +- .../src/pages/products/ProductList.jsx | 4 +- .../src/pages/products/ProductsPage.jsx | 16 +- .../src/pages/products/Toolbar.jsx | 39 ++--- .../src/pages/products/editor/InfoTabs.jsx | 91 +++++++---- .../pages/products/editor/ProductEditor.jsx | 19 +-- .../tests/channelPricing.test.mjs | 34 ++++ packages/i18n/locales/el/manager.json | 73 ++++++++- packages/i18n/locales/en/manager.json | 73 ++++++++- packages/ui/src/components.css | 2 + tests/e2e/manager.mjs | 1 + 17 files changed, 636 insertions(+), 104 deletions(-) create mode 100644 manager_dashboard/src/pages/pricing/ChannelPricesTab.jsx create mode 100644 manager_dashboard/src/pages/pricing/ProductChannelPricesDialog.jsx create mode 100644 manager_dashboard/src/pages/pricing/channelPricing.js create mode 100644 manager_dashboard/src/pages/pricing/useActionText.js create mode 100644 manager_dashboard/tests/channelPricing.test.mjs diff --git a/manager_dashboard/src/pages/pricing/ChannelPricesTab.jsx b/manager_dashboard/src/pages/pricing/ChannelPricesTab.jsx new file mode 100644 index 0000000..5455981 --- /dev/null +++ b/manager_dashboard/src/pages/pricing/ChannelPricesTab.jsx @@ -0,0 +1,152 @@ +// Pricing → Order channel prices (2026-11-20): a product's price at a sales point. A rule is for +// one or more sales points and names products, categories, or neither (the whole channel); it +// sets the price (one product), adds an amount or a percentage (with rounding). The most precise +// rule wins: the product's own, then its category's, then the channel's. Backend: +// /api/pricing/channel-prices (services/channel_prices.py). Quick edit per product: the product +// editor's "Edit alternate prices" (ProductChannelPricesDialog). +import { useMemo, useState } from 'react' +import { useMutation, useQueryClient } from '@tanstack/react-query' +import toast from 'react-hot-toast' +import { Pencil, Plus, Trash2 } from 'lucide-react' +import { errorMessage } from '@xenia/api' +import { useTranslation } from '@xenia/i18n' +import { SALES_POINTS } from '@xenia/access' +import { Button, Dialog, Dropdown, EmptyState, Field, IconButton, Input, Segmented, Spinner, Switch } from '@xenia/ui' +import { Pill } from '../overview/parts' +import { useLiveQuery } from '../../live/useLiveQuery' +import * as api from './pricingApi' +import { ACTIONS, ROUNDINGS, blankRule, levelOf, roundingKey, ruleBody, ruleProblems, ruleToForm } from './channelPricing' +import { useCatalog } from './useCatalog' +import { useActionText } from './useActionText' + +const LEVEL_TONE = { product: 'accent', category: 'info', channel: 'neutral' } + +export default function ChannelPricesTab() { + const { t } = useTranslation('manager') + const { t: tc } = useTranslation('common') + const qc = useQueryClient() + const catalog = useCatalog() + const actionText = useActionText() + const { data: rules, isPending } = useLiveQuery({ queryKey: ['pricing', 'channel-prices'], queryFn: api.listChannelPrices, events: api.PRICING_EVENTS }) + const [edit, setEdit] = useState(null) // a rule (or blank) being edited + const [removing, setRemoving] = useState(null) + const names = useMemo(() => ({ + product: Object.fromEntries(catalog.products.map(p => [p.id, p.name])), + category: Object.fromEntries(catalog.categories.map(c => [c.id, c.plain])), + }), [catalog]) + const refresh = () => qc.invalidateQueries({ queryKey: ['pricing', 'channel-prices'] }) + const remove = useMutation({ + mutationFn: r => api.deleteChannelPrice(r.id), + onSuccess: () => { setRemoving(null); refresh(); toast.success(t('pricing.channels.removed')) }, + onError: e => toast.error(errorMessage(e, t)), + }) + const toggle = useMutation({ + mutationFn: r => api.saveChannelPrice(r.id, { ...ruleBody(ruleToForm(r)), is_active: !r.is_active }), + onSuccess: refresh, onError: e => toast.error(errorMessage(e, t)), + }) + const order = { product: 0, category: 1, channel: 2 } + const list = [...(rules ?? [])].sort((a, b) => order[levelOf(a)] - order[levelOf(b)] || b.id - a.id) + + return ( +
+
+

{t('pricing.channels.intro')}

+ +
+ {isPending ?
+ : !list.length ? + : ( +
    + {list.map(r => { + const lv = levelOf(r) + const what = lv === 'channel' ? t('pricing.channels.everything') + : (lv === 'product' ? r.product_ids : r.category_ids).map(id => names[lv][id] ?? `#${id}`).join(', ') + return ( +
  • + {t(`pricing.channels.level.${lv}`)} + + {r.name || what} + + {r.name && `${what} · `}{r.sales_points.map(sp => tc(`salesPoints.${sp}.name`)).join(', ')} + + + {actionText(r)} + + setEdit(ruleToForm(r))}> + setRemoving(r)}> + toggle.mutate(r)} aria-label={t('pricing.channels.active')} /> + +
  • + ) + })} +
+ )} + {edit && setEdit(null)} onSaved={() => { setEdit(null); refresh() }} />} + {removing && ( + setRemoving(null)} title={t('pricing.channels.removeTitle')} + footer={<> + + + }> +

{t('pricing.channels.removeBody')}

+
+ )} +
+ ) +} + +function RuleDialog({ rule, catalog, onClose, onSaved }) { + const { t } = useTranslation('manager') + const { t: tc } = useTranslation('common') + const [f, setF] = useState(rule) + const [tried, setTried] = useState(false) + const set = (k, v) => setF(x => ({ ...x, [k]: v })) + const wrong = ruleProblems(f) + const save = useMutation({ + mutationFn: () => api.saveChannelPrice(f.id, ruleBody(f)), + onSuccess: () => { toast.success(t('pricing.channels.saved')); onSaved() }, + onError: e => toast.error(errorMessage(e, t)), + }) + const submit = () => { setTried(true); if (!wrong.length) save.mutate() } + const err = k => (tried && wrong.includes(k) ? t(`pricing.channels.wrong.${k}`) : undefined) + return ( + + + + }> +
+ + set('sales_points', v)} placeholder={t('pricing.channels.pickPoints')} data-testid="channel-price-points" + options={SALES_POINTS.map(sp => ({ value: sp, label: tc(`salesPoints.${sp}.name`) }))} /> + +
+ + set('product_ids', v)} placeholder={t('pricing.channels.none_')} searchable data-testid="channel-price-products" + options={catalog.products.map(p => ({ value: p.id, label: p.name }))} /> + + + set('category_ids', v)} placeholder={t('pricing.channels.none_')} searchable data-testid="channel-price-categories" + options={catalog.categories.map(c => ({ value: c.id, label: c.name }))} /> + +
+ + set('action_type', v)} + options={ACTIONS.map(a => ({ value: a, label: t(`pricing.channels.action.${a}`), testId: `channel-price-action-${a}` }))} /> + +
+ + set('value', e.target.value)} placeholder={f.action_type === 'add_percent' ? '-10' : '0,00'} data-testid="channel-price-value" /> + + + set('round_to', v)} options={ROUNDINGS.map(r => ({ value: r, label: t(`pricing.roundings.${roundingKey(r)}`) }))} /> + +
+ + set('name', e.target.value)} maxLength={120} /> + +

{t('pricing.channels.order')}

+
+
+ ) +} diff --git a/manager_dashboard/src/pages/pricing/PricingPage.jsx b/manager_dashboard/src/pages/pricing/PricingPage.jsx index 0eba216..c404a9e 100644 --- a/manager_dashboard/src/pages/pricing/PricingPage.jsx +++ b/manager_dashboard/src/pages/pricing/PricingPage.jsx @@ -1,35 +1,30 @@ -// Management → Pricing (plan step 6.7): price rules, price groups, deals and the waiters' discount -// limits, one tab each. Changes reach the counter, the phones and the tablets at once: new prices -// apply to what is added to orders from then on (lines already on an order keep their price). -import { useState } from 'react' +// Management → Pricing (plan step 6.7): price rules, price groups, deals, the waiters' discount +// limits and (2026-11-20) order channel prices, one tab each. Changes reach the counter, the phones +// and the tablets at once: new prices apply to what is added to orders from then on (lines already +// on an order keep their price). The tab is in the address (?tab=). +import { useSearchParams } from 'react-router-dom' import { useTranslation } from '@xenia/i18n' -import { Page } from '@xenia/ui' +import { Page, PageHeader, Tabs } from '@xenia/ui' +import ChannelPricesTab from './ChannelPricesTab' import DealsTab from './DealsTab' import DiscountsTab from './DiscountsTab' import ModifiersTab from './ModifiersTab' import PriceGroupsTab from './PriceGroupsTab' -const TABS = ['rules', 'groups', 'deals', 'discounts'] +const TABS = ['rules', 'channels', 'groups', 'deals', 'discounts'] export default function PricingPage() { const { t } = useTranslation('manager') - const [tab, setTab] = useState('rules') + const [params, setParams] = useSearchParams() + const tab = TABS.includes(params.get('tab')) ? params.get('tab') : 'rules' + const setTab = v => setParams(p => { const n = new URLSearchParams(p); n.set('tab', v); return n }, { replace: true }) return ( - -
-

{t('pricing.pageTitle')}

-

{t('pricing.intro')}

-
-
- {TABS.map(k => ( - - ))} -
+ ({ id: k, label: t(`pricing.tabs.${k}`), testId: `pricing-tab-${k}` }))} />}> + {tab === 'rules' && } + {tab === 'channels' && } {tab === 'groups' && } {tab === 'deals' && } {tab === 'discounts' && } diff --git a/manager_dashboard/src/pages/pricing/ProductChannelPricesDialog.jsx b/manager_dashboard/src/pages/pricing/ProductChannelPricesDialog.jsx new file mode 100644 index 0000000..0a991bb --- /dev/null +++ b/manager_dashboard/src/pages/pricing/ProductChannelPricesDialog.jsx @@ -0,0 +1,95 @@ +// The product editor's "Edit alternate prices" (2026-11-20): this product's own price at each sales +// point it is sold at — the default price, a set price, ± an amount or ± a percentage (rounded). +// "Default" leaves it to the category's or the channel's rule (Pricing → Order channel prices), +// shown greyed. Saves only this product's own rules (PUT /api/pricing/channel-prices/product/{id}). +import { useEffect, useState } from 'react' +import toast from 'react-hot-toast' +import { errorMessage } from '@xenia/api' +import { useFormat, useTranslation } from '@xenia/i18n' +import { Button, Dialog, Dropdown, EmptyState, Input, Spinner, cx } from '@xenia/ui' +import * as api from './pricingApi' +import { ACTIONS, ROUNDINGS, pointRowFromApi, pointsBody, roundingKey, rowPrice, rowProblem } from './channelPricing' +import { useActionText } from './useActionText' + +export default function ProductChannelPricesDialog({ product, onClose }) { + const { t } = useTranslation('manager') + const { t: tc } = useTranslation('common') + const { formatMoney } = useFormat() + const actionText = useActionText() + const [rows, setRows] = useState(null) + const [busy, setBusy] = useState(false) + const [error, setError] = useState(null) + useEffect(() => { + api.productChannelPrices(product.id).then(d => setRows(d.points.map(pointRowFromApi))).catch(e => setError(errorMessage(e, t))) + }, [product.id, t]) + const set = (i, k, v) => setRows(rs => rs.map((r, j) => (j === i ? { ...r, [k]: v } : r))) + const bad = (rows ?? []).some(rowProblem) + const save = async () => { + setBusy(true) + try { + await api.saveProductChannelPrices(product.id, pointsBody(rows)) + toast.success(t('pricing.channels.savedFor', { name: product.name })) + onClose() + } catch (e) { toast.error(errorMessage(e, t)) } + setBusy(false) + } + return ( + + + + }> + {error ? : !rows ?
+ : !rows.length ? + : ( +
+ + + + + + + + + + + + {rows.map((r, i) => { + const price = rowPrice(r) + const own = r.mode !== 'default' + return ( + + + + + + + + ) + })} + +
{t('pricing.channels.point')}{t('pricing.channels.change')}{t('pricing.channels.value')}{t('pricing.channels.rounding')}{t('pricing.channels.price')}
+ + {tc(`salesPoints.${r.sales_point}.name`)} + {!own && r.inherited && {t(`pricing.channels.from.${r.inherited.level}`, { what: actionText(r.inherited) })}} + + + set(i, 'mode', v)} data-testid="product-channel-mode" + options={[{ value: 'default', label: t('pricing.channels.action.default') }, ...ACTIONS.map(a => ({ value: a, label: t(`pricing.channels.action.${a}`) }))]} /> + + set(i, 'value', e.target.value)} + placeholder={r.mode === 'add_percent' ? '-10' : '0,00'} aria-label={t('pricing.channels.value')} data-testid="product-channel-value" + className={cx(own && rowProblem(r) && 'border-danger')} /> + + set(i, 'round_to', v)} + options={ROUNDINGS.map(x => ({ value: x, label: t(`pricing.roundings.${roundingKey(x)}`) }))} /> + + {price == null ? '—' : formatMoney(price)} +
+
+ )} +

{t('pricing.channels.order')}

+
+ ) +} diff --git a/manager_dashboard/src/pages/pricing/channelPricing.js b/manager_dashboard/src/pages/pricing/channelPricing.js new file mode 100644 index 0000000..53ea35c --- /dev/null +++ b/manager_dashboard/src/pages/pricing/channelPricing.js @@ -0,0 +1,76 @@ +// 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)) diff --git a/manager_dashboard/src/pages/pricing/pricingApi.js b/manager_dashboard/src/pages/pricing/pricingApi.js index 30b9398..9dc90df 100644 --- a/manager_dashboard/src/pages/pricing/pricingApi.js +++ b/manager_dashboard/src/pages/pricing/pricingApi.js @@ -33,3 +33,10 @@ export const listProducts = () => client.get('/api/products/', { params: { all: export const listCategories = () => client.get('/api/products/categories').then(data) export const listPrepZones = () => client.get('/api/prep-zones').then(data) export const listTags = () => client.get('/api/products/tags').then(data) + +// Order channel prices (2026-11-20) +export const listChannelPrices = () => client.get('/api/pricing/channel-prices').then(data) +export const saveChannelPrice = (id, body) => (id ? client.put(`/api/pricing/channel-prices/${id}`, body) : client.post('/api/pricing/channel-prices', body)).then(data) +export const deleteChannelPrice = id => client.delete(`/api/pricing/channel-prices/${id}`) +export const productChannelPrices = id => client.get(`/api/pricing/channel-prices/product/${id}`).then(data) +export const saveProductChannelPrices = (id, points) => client.put(`/api/pricing/channel-prices/product/${id}`, { points }).then(data) diff --git a/manager_dashboard/src/pages/pricing/useActionText.js b/manager_dashboard/src/pages/pricing/useActionText.js new file mode 100644 index 0000000..2b7e67e --- /dev/null +++ b/manager_dashboard/src/pages/pricing/useActionText.js @@ -0,0 +1,15 @@ +// A channel price's change as text: "€2.80", "+€0.50", "−10 % · To the nearest €0.50" (2026-11-20). +import { useFormat, useTranslation } from '@xenia/i18n' +import { roundingKey } from './channelPricing' + +export function useActionText() { + const { t } = useTranslation('manager') + const { formatMoney } = useFormat() + return r => { + const v = Number(r.value) + const text = r.action_type === 'set' ? formatMoney(v) + : r.action_type === 'add_amount' ? `${v >= 0 ? '+' : '−'}${formatMoney(Math.abs(v))}` + : `${v >= 0 ? '+' : '−'}${Math.abs(v)} %` + return r.round_to ? `${text} · ${t(`pricing.roundings.${roundingKey(r.round_to)}`)}` : text + } +} diff --git a/manager_dashboard/src/pages/products/CategoryTree.jsx b/manager_dashboard/src/pages/products/CategoryTree.jsx index 8c00381..6eb56c5 100644 --- a/manager_dashboard/src/pages/products/CategoryTree.jsx +++ b/manager_dashboard/src/pages/products/CategoryTree.jsx @@ -51,14 +51,14 @@ export default function CategoryTree({ categories, products, scope, onScope, onA {!depth && onAdd(cat.id)}>} onMove(cat)}> onEdit(cat)}> - onDelete(cat)}> + onDelete(cat)}> } /> ) return ( -