Files
xenia-pos-local/manager_dashboard/src/pages/reports/misc/MiscReport.jsx
T
bonaminandClaude Opus 5.5 f87f9ff23c feat(backend,waiter,manager): prices changed by hand are on the record
Owner 2026-11-23 (plan 6F.3). services/price_changes.py: the venue's switch orders.price_change
(off | before_sending | anytime, default anytime as before), orders.price_change_reason and its
quick reasons; keys ordering.prices.modify (the cart), new modify_sent (a sent or served line) and
view_changes (who sees them). Every by-hand price (waiter cart, a sent line, the counter's %
discount) writes a manual_price PriceEventLog row (before, after, quantity, who, approving manager,
stage, reason) plus a PRICE_CHANGED audit row; before, the sent-line change overwrote the price with
no record. Alembic 0047: event columns; modify_sent for everyone holding modify (and the venues'
role overrides), view_changes for owners and store managers.
Waiter: the reason picker when the venue asks, a sent line's price on modify_sent and the venue's
switch, the line's price history. Manager: Settings -> Operation 'Prices changed by hand', the
changes on the order's lines and history, Reports -> Cancellations, discounts and price changes
(+ export). Counter: no discount when the venue turned price changes off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 07:41:43 +02:00

151 lines
9.8 KiB
React

// Reports → Cancellations and discounts (plan step 6.12c; replaces the v1 "misc" logs): every
// cancelled line (what, how much, who sent it, who cancelled it, why) and every discount given
// (on what, how much, by whom, why), with totals per person; exports for Excel.
import { useState } from 'react'
import { useCan } from '@xenia/access'
import { useFormat, useTranslation } from '@xenia/i18n'
import client from '../../../api/client'
import { useLiveQuery } from '../../../live/useLiveQuery'
import { BarList } from '../kit/charts'
import { ExportButton, Failed, Loading, Panel, ReportPage, Stat, Table } from '../kit/parts'
import { PeriodPicker } from '../kit/Period'
import { defaultPeriod, periodParams, periodSlug } from '../kit/reportLogic'
export default function MiscReport() {
const { t } = useTranslation('manager')
const seesChanges = useCan('ordering.prices.view_changes')
const tabs = [
{ id: 'cancellations', label: t('reports.misc.cancellations'), Component: CancellationsTab },
{ id: 'discounts', label: t('reports.misc.discounts'), Component: DiscountsTab },
...(seesChanges ? [{ id: 'price-changes', label: t('reports.misc.priceChanges'), Component: PriceChangesTab }] : []),
]
return <ReportPage title={t('reports.misc.title')} tabs={tabs} testId="report-misc" />
}
function CancellationsTab() {
const { t } = useTranslation('manager')
const fmt = useFormat()
const [period, setPeriod] = useState(() => defaultPeriod(fmt.venueToday()))
const params = periodParams(period)
const { data, isPending, error, refetch } = useLiveQuery({ queryKey: ['report-cancellations', params], queryFn: () => client.get('/api/reports/cancellations', { params }).then(r => r.data) })
const rows = data?.cancellations ?? []
const byPerson = Object.values(rows.reduce((m, c) => {
const k = c.cancelled_by || c.waiter_name || '—'
m[k] = m[k] ?? { key: k, label: k, value: 0, n: 0 }
m[k].value += c.value
m[k].n += 1
return m
}, {}))
const columns = [
{ key: 'cancelled_at', label: t('reports.misc.when'), render: c => fmt.formatDateTime(c.cancelled_at, 'datetimeShort') },
{ key: 'product_name', label: t('reports.prod.product'), render: c => <span className="font-semibold text-ink">{c.product_name}</span> },
{ key: 'quantity', label: t('reports.prod.units'), align: 'right', render: c => fmt.formatNumber(c.quantity, { maxDecimals: 3 }) },
{ key: 'value', label: t('reports.misc.value'), align: 'right', render: c => fmt.formatMoney(c.value) },
{ key: 'table', label: t('reports.place') },
{ key: 'waiter_name', label: t('reports.misc.sentBy') },
{ key: 'cancelled_by', label: t('reports.misc.cancelledBy') },
{ key: 'cancel_reason', label: t('reports.misc.reason'), render: c => c.cancel_reason || '' },
]
return (
<div className="grid gap-4">
<PeriodPicker value={period} onChange={setPeriod} right={<ExportButton endpoint="/api/reports/cancellations/export" params={params} filename={`cancellations-${periodSlug(period)}.csv`} />} />
{isPending ? <Loading /> : error ? <Failed error={error} retry={refetch} /> : (<>
<div className="grid gap-3 grid-cols-[repeat(auto-fill,minmax(12rem,1fr))]">
<Stat label={t('reports.misc.cancelledLines')} value={rows.length} testId="cancellations-count" />
<Stat label={t('reports.misc.cancelledValue')} tone="danger" value={fmt.formatMoney(data.total_value)} sub={t('reports.misc.notSales')} />
</div>
{byPerson.length > 1 && (
<Panel title={t('reports.misc.byWhom')}>
<BarList format={v => fmt.formatMoney(v)} rows={byPerson.sort((a, b) => b.value - a.value).map(p => ({ ...p, sub: t('reports.misc.lines', { count: p.n }) }))} />
</Panel>
)}
<Panel padded={false} title={t('reports.misc.cancellations')}>
<Table columns={columns} rows={rows} rowTestId="report-cancellation" initialSort={{ key: 'cancelled_at', dir: 'desc' }} limit={500} />
</Panel>
</>)}
</div>
)
}
function DiscountsTab() {
const { t } = useTranslation('manager')
const fmt = useFormat()
const [period, setPeriod] = useState(() => defaultPeriod(fmt.venueToday()))
const params = periodParams(period)
const { data, isPending, error, refetch } = useLiveQuery({ queryKey: ['report-discounts', params], queryFn: () => client.get('/api/reports/discounts', { params }).then(r => r.data) })
const rows = data?.discounts ?? []
const columns = [
{ key: 'applied_at', label: t('reports.misc.when'), render: d => fmt.formatDateTime(d.applied_at, 'datetimeShort') },
{ key: 'order_id', label: '#', render: d => <span className="tabular-nums text-ink-2">#{d.order_id}</span> },
{ key: 'table_name', label: t('reports.place'), render: d => d.table_name || t('reports.noTable') },
{ key: 'discount_value', label: t('reports.misc.discount'), align: 'right',
render: d => (d.discount_type === 'percent' ? `${fmt.formatNumber(d.discount_value, { maxDecimals: 1 })}%` : fmt.formatMoney(d.discount_value)) },
{ key: 'discount_amount', label: t('reports.misc.amount'), align: 'right', render: d => <span className="font-semibold">{fmt.formatMoney(d.discount_amount)}</span> },
{ key: 'order_total_before', label: t('reports.misc.before'), align: 'right', render: d => (d.order_total_before != null ? fmt.formatMoney(d.order_total_before) : '') },
{ key: 'applied_by_name', label: t('reports.misc.givenBy') },
{ key: 'reason', label: t('reports.misc.reason'), render: d => d.reason || '' },
]
return (
<div className="grid gap-4">
<PeriodPicker value={period} onChange={setPeriod} />
{isPending ? <Loading /> : error ? <Failed error={error} retry={refetch} /> : (<>
<div className="grid gap-3 grid-cols-[repeat(auto-fill,minmax(12rem,1fr))]">
<Stat label={t('reports.misc.discountsGiven')} value={rows.length} sub={t('reports.misc.onOrders', { count: data.order_count })} />
<Stat label={t('reports.misc.discountTotal')} tone="warning" value={fmt.formatMoney(data.total_discount_value)} sub={t('reports.misc.alreadyOff')} />
</div>
{data.by_waiter?.length > 0 && (
<Panel title={t('reports.misc.byWhom')}>
<BarList format={v => fmt.formatMoney(v)} rows={data.by_waiter.map(w => ({ key: w.waiter_id, label: w.waiter_name, value: w.total_value, sub: t('reports.misc.times', { count: w.count }) }))} />
</Panel>
)}
<Panel padded={false} title={t('reports.misc.discounts')}>
<Table columns={columns} rows={rows} rowTestId="report-discount" initialSort={{ key: 'applied_at', dir: 'desc' }} limit={500} />
</Panel>
</>)}
</div>
)
}
/** Prices changed by hand (6F.3): each change, before → after, its value for the line's quantity,
* where (cart / sent / served), who, the approving manager, why; totals per person. */
function PriceChangesTab() {
const { t } = useTranslation('manager')
const fmt = useFormat()
const [period, setPeriod] = useState(() => defaultPeriod(fmt.venueToday()))
const params = periodParams(period)
const { data, isPending, error, refetch } = useLiveQuery({ queryKey: ['report-price-changes', params], queryFn: () => client.get('/api/reports/price-changes', { params }).then(r => r.data) })
const rows = data?.changes ?? []
const columns = [
{ key: 'at', label: t('reports.misc.when'), render: c => fmt.formatDateTime(c.at, 'datetimeShort') },
{ key: 'product_name', label: t('reports.prod.product'), render: c => <span className="font-semibold text-ink">{c.product_name}</span> },
{ key: 'table_name', label: t('reports.place'), render: c => c.table_name || t('reports.noTable') },
{ key: 'before', label: t('reports.misc.priceWas'), align: 'right', render: c => fmt.formatMoney(c.before) },
{ key: 'after', label: t('reports.misc.priceNow'), align: 'right', render: c => fmt.formatMoney(c.after) },
{ key: 'value', label: t('reports.misc.value'), align: 'right', render: c => <span className={c.value < 0 ? 'font-semibold text-danger-ink' : 'font-semibold text-success-ink'}>{fmt.formatMoney(c.value, { signDisplay: 'exceptZero' })}</span> },
{ key: 'stage', label: t('reports.misc.stage'), render: c => (c.stage ? t(`orders.history.stage.${c.stage}`) : '') },
{ key: 'by', label: t('reports.misc.changedBy'), render: c => c.by || '' },
{ key: 'approved_by', label: t('reports.misc.approvedBy'), render: c => c.approved_by || '' },
{ key: 'reason', label: t('reports.misc.reason'), render: c => c.reason || '' },
]
return (
<div className="grid gap-4">
<PeriodPicker value={period} onChange={setPeriod} right={<ExportButton endpoint="/api/reports/price-changes/export" params={params} filename={`price-changes-${periodSlug(period)}.csv`} />} />
{isPending ? <Loading /> : error ? <Failed error={error} retry={refetch} /> : (<>
<div className="grid gap-3 grid-cols-[repeat(auto-fill,minmax(12rem,1fr))]">
<Stat label={t('reports.misc.changes')} value={data.count} testId="price-changes-count" />
<Stat label={t('reports.misc.changesValue')} tone={data.total_value < 0 ? 'danger' : 'success'} value={fmt.formatMoney(data.total_value, { signDisplay: 'exceptZero' })} tip={t('reports.misc.changesValueTip')} />
</div>
{data.by_person.length > 1 && (
<Panel title={t('reports.misc.byWhom')}>
<BarList format={v => fmt.formatMoney(v, { signDisplay: 'exceptZero' })} rows={data.by_person.map(p => ({ key: p.user_id ?? '—', label: p.name ?? '—', value: p.total_value, sub: t('reports.misc.changesCount', { count: p.count }) }))} />
</Panel>
)}
<Panel padded={false} title={t('reports.misc.priceChanges')}>
<Table columns={columns} rows={rows} rowTestId="report-price-change" initialSort={{ key: 'at', dir: 'desc' }} limit={500} />
</Panel>
</>)}
</div>
)
}