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 <noreply@anthropic.com>
This commit is contained in:
@@ -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 (
|
||||
<div className="grid gap-4" data-testid="channel-prices">
|
||||
<div className="flex flex-wrap items-start gap-3">
|
||||
<p className="flex-1 min-w-64 text-body text-ink-2 max-w-3xl">{t('pricing.channels.intro')}</p>
|
||||
<Button icon={<Plus size={16} />} onClick={() => setEdit(blankRule())} data-testid="channel-price-new">{t('pricing.channels.new')}</Button>
|
||||
</div>
|
||||
{isPending ? <div className="py-10 flex justify-center"><Spinner /></div>
|
||||
: !list.length ? <EmptyState title={t('pricing.channels.none')} description={t('pricing.channels.noneHint')} />
|
||||
: (
|
||||
<ul className="grid gap-3">
|
||||
{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 (
|
||||
<li key={r.id} className={`px-card py-card-y rounded-card border border-line bg-surface shadow-raised flex flex-wrap items-center gap-3 ${r.is_active ? '' : 'opacity-60'}`} data-testid="channel-price">
|
||||
<Pill tone={LEVEL_TONE[lv]}>{t(`pricing.channels.level.${lv}`)}</Pill>
|
||||
<span className="flex-1 min-w-56 grid">
|
||||
<span className="text-body font-semibold text-ink">{r.name || what}</span>
|
||||
<span className="text-label text-ink-2">
|
||||
{r.name && `${what} · `}{r.sales_points.map(sp => tc(`salesPoints.${sp}.name`)).join(', ')}
|
||||
</span>
|
||||
</span>
|
||||
<span className="text-body font-semibold tabular-nums text-ink" data-testid="channel-price-action">{actionText(r)}</span>
|
||||
<span className="flex items-center gap-1.5">
|
||||
<IconButton size="sm" variant="ghost" tone="accent" label={t('pricing.channels.edit')} onClick={() => setEdit(ruleToForm(r))}><Pencil size={15} /></IconButton>
|
||||
<IconButton size="sm" variant="ghost" tone="danger" label={t('pricing.channels.remove')} onClick={() => setRemoving(r)}><Trash2 size={15} /></IconButton>
|
||||
<Switch className="ml-2" checked={r.is_active} onChange={() => toggle.mutate(r)} aria-label={t('pricing.channels.active')} />
|
||||
</span>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
{edit && <RuleDialog rule={edit} catalog={catalog} onClose={() => setEdit(null)} onSaved={() => { setEdit(null); refresh() }} />}
|
||||
{removing && (
|
||||
<Dialog open size="sm" onClose={() => setRemoving(null)} title={t('pricing.channels.removeTitle')}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={() => setRemoving(null)}>{t('pricing.channels.cancel')}</Button>
|
||||
<Button variant="danger" loading={remove.isPending} onClick={() => remove.mutate(removing)} data-testid="channel-price-remove">{t('pricing.channels.remove')}</Button>
|
||||
</>}>
|
||||
<p className="text-body text-ink-2">{t('pricing.channels.removeBody')}</p>
|
||||
</Dialog>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
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 (
|
||||
<Dialog open size="lg" onClose={onClose} title={f.id ? t('pricing.channels.editTitle') : t('pricing.channels.newTitle')}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={onClose}>{t('pricing.channels.cancel')}</Button>
|
||||
<Button loading={save.isPending} onClick={submit} data-testid="channel-price-save">{t('pricing.channels.save')}</Button>
|
||||
</>}>
|
||||
<div className="grid gap-4" data-testid="channel-price-dialog">
|
||||
<Field label={t('pricing.channels.points')} required error={err('points')}>
|
||||
<Dropdown multiple value={f.sales_points} onChange={v => 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`) }))} />
|
||||
</Field>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<Field label={t('pricing.channels.products')} hint={t('pricing.channels.productsHint')} error={err('set_one')}>
|
||||
<Dropdown multiple value={f.product_ids} onChange={v => 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 }))} />
|
||||
</Field>
|
||||
<Field label={t('pricing.channels.categories')} hint={t('pricing.channels.categoriesHint')}>
|
||||
<Dropdown multiple value={f.category_ids} onChange={v => 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 }))} />
|
||||
</Field>
|
||||
</div>
|
||||
<Field label={t('pricing.channels.change')}>
|
||||
<Segmented value={f.action_type} onChange={v => set('action_type', v)}
|
||||
options={ACTIONS.map(a => ({ value: a, label: t(`pricing.channels.action.${a}`), testId: `channel-price-action-${a}` }))} />
|
||||
</Field>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<Field label={t(`pricing.channels.valueOf.${f.action_type}`)} hint={f.action_type === 'set' ? undefined : t('pricing.channels.valueHint')} required error={err('value')}>
|
||||
<Input inputMode="decimal" value={f.value} onChange={e => set('value', e.target.value)} placeholder={f.action_type === 'add_percent' ? '-10' : '0,00'} data-testid="channel-price-value" />
|
||||
</Field>
|
||||
<Field label={t('pricing.channels.rounding')}>
|
||||
<Dropdown value={f.round_to} onChange={v => set('round_to', v)} options={ROUNDINGS.map(r => ({ value: r, label: t(`pricing.roundings.${roundingKey(r)}`) }))} />
|
||||
</Field>
|
||||
</div>
|
||||
<Field label={t('pricing.channels.name')} hint={t('pricing.channels.nameHint')}>
|
||||
<Input value={f.name} onChange={e => set('name', e.target.value)} maxLength={120} />
|
||||
</Field>
|
||||
<p className="text-caption text-ink-2 rounded-control bg-surface-2 border border-line px-3 py-2">{t('pricing.channels.order')}</p>
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -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 (
|
||||
<Page testId="pricing-page">
|
||||
<header className="grid gap-1">
|
||||
<h1 className="text-heading font-semibold text-ink">{t('pricing.pageTitle')}</h1>
|
||||
<p className="text-body text-ink-2 max-w-3xl">{t('pricing.intro')}</p>
|
||||
</header>
|
||||
<div className="flex flex-wrap gap-1 border-b border-line" role="tablist" aria-label={t('pricing.pageTitle')}>
|
||||
{TABS.map(k => (
|
||||
<button key={k} type="button" role="tab" aria-selected={tab === k} onClick={() => setTab(k)} data-testid={`pricing-tab-${k}`}
|
||||
className={`px-3 py-2 -mb-px border-b-2 text-label font-medium transition-colors duration-fast
|
||||
${tab === k ? 'border-accent text-accent-ink' : 'border-transparent text-ink-2 hover:text-ink'}`}>
|
||||
{t(`pricing.tabs.${k}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<Page testId="pricing-page"
|
||||
top={<Tabs size="lg" value={tab} onChange={setTab} aria-label={t('pricing.pageTitle')}
|
||||
tabs={TABS.map(k => ({ id: k, label: t(`pricing.tabs.${k}`), testId: `pricing-tab-${k}` }))} />}>
|
||||
<PageHeader title={t('pricing.pageTitle')} subtitle={t('pricing.intro')} />
|
||||
{tab === 'rules' && <ModifiersTab />}
|
||||
{tab === 'channels' && <ChannelPricesTab />}
|
||||
{tab === 'groups' && <PriceGroupsTab />}
|
||||
{tab === 'deals' && <DealsTab />}
|
||||
{tab === 'discounts' && <DiscountsTab />}
|
||||
|
||||
@@ -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 (
|
||||
<Dialog open size="xl" onClose={onClose} title={t('pricing.channels.productTitle', { name: product.name })}
|
||||
description={t('pricing.channels.productIntro', { price: formatMoney(product.base_price ?? 0) })}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={onClose}>{t('pricing.channels.cancel')}</Button>
|
||||
<Button loading={busy} disabled={!rows || bad} onClick={save} data-testid="product-channel-save">{t('pricing.channels.save')}</Button>
|
||||
</>}>
|
||||
{error ? <EmptyState title={error} /> : !rows ? <div className="py-10 flex justify-center"><Spinner /></div>
|
||||
: !rows.length ? <EmptyState title={t('pricing.channels.notSold')} />
|
||||
: (
|
||||
<div className="rounded-card border border-line overflow-x-auto" data-testid="product-channel-prices">
|
||||
<table className="w-full text-label">
|
||||
<thead className="bg-surface-2 text-ink-3">
|
||||
<tr className="text-overline font-semibold uppercase tracking-wide">
|
||||
<th className="px-card py-2 text-left">{t('pricing.channels.point')}</th>
|
||||
<th className="px-2 py-2 text-left w-48">{t('pricing.channels.change')}</th>
|
||||
<th className="px-2 py-2 text-left w-32">{t('pricing.channels.value')}</th>
|
||||
<th className="px-2 py-2 text-left w-52">{t('pricing.channels.rounding')}</th>
|
||||
<th className="px-card py-2 text-right w-28">{t('pricing.channels.price')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-line">
|
||||
{rows.map((r, i) => {
|
||||
const price = rowPrice(r)
|
||||
const own = r.mode !== 'default'
|
||||
return (
|
||||
<tr key={r.sales_point} className="align-middle" data-testid="product-channel-row" data-point={r.sales_point}>
|
||||
<td className="px-card py-2.5">
|
||||
<span className="grid">
|
||||
<span className="text-body font-medium text-ink">{tc(`salesPoints.${r.sales_point}.name`)}</span>
|
||||
{!own && r.inherited && <span className="text-caption text-ink-3">{t(`pricing.channels.from.${r.inherited.level}`, { what: actionText(r.inherited) })}</span>}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-2 py-2">
|
||||
<Dropdown size="sm" value={r.mode} onChange={v => 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}`) }))]} />
|
||||
</td>
|
||||
<td className="px-2 py-2">
|
||||
<Input size="sm" inputMode="decimal" disabled={!own} value={r.value} onChange={e => 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')} />
|
||||
</td>
|
||||
<td className="px-2 py-2">
|
||||
<Dropdown size="sm" disabled={!own || r.mode === 'set'} value={r.round_to} onChange={v => set(i, 'round_to', v)}
|
||||
options={ROUNDINGS.map(x => ({ value: x, label: t(`pricing.roundings.${roundingKey(x)}`) }))} />
|
||||
</td>
|
||||
<td className={cx('px-card py-2 text-right tabular-nums font-semibold', own ? 'text-ink' : 'text-ink-3')} data-testid="product-channel-price">
|
||||
{price == null ? '—' : formatMoney(price)}
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
<p className="mt-4 text-caption text-ink-2">{t('pricing.channels.order')}</p>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -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))
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user