feat: the staff, cancellations/discounts, technical, prep zones and expenses reports on v2; reports all on v2 (platform-v2 step 6.12c)

- Reports -> Staff (src/pages/reports/staff): shifts (pay, collected, to hand
  over, the counted difference, cancellations; a shift opens the overview's
  shift dialog; the owner deletes one), cash handover (starting cash, cash,
  card, refunds, to hand over, counted), sales by person (orders, units,
  cancellations, hours, sales net of refunds, per hour, share; ranked) - the
  v1 activity and leaderboard views merged into it
- Reports -> Cancellations and discounts (src/pages/reports/misc), with totals
  per person; Technical (printing: tickets, failed, printed first time, per
  printer, every ticket with its error, the receipt-printer summary; logs for
  the owner) - v1 printer history + printer health merged
- Prep zones report (per zone: items, value, products, orders; per-zone Excel
  downloads built in the browser with the server's Excel rules; print a zone)
  and Expenses report (period, status, category; split by category or
  supplier; payments) - its categories now match what Finance saves (the v1
  report listed categories that never existed)
- kit: csvText() / download(); PrintDialog modes for the printer summary
- v1 report kit (reports/shared) and every v1 report view deleted;
  src/pages/reports is v2 (all but LogsPanel strict)
- tests: reports.test.mjs (+1), e2e manager.mjs (every report opens, a shift's
  details, the ranking)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 09:19:49 +02:00
co-authored by Claude Opus 5.5
parent 7767bdb728
commit 6bdf0bdd79
35 changed files with 919 additions and 4230 deletions
@@ -0,0 +1,104 @@
// 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 { 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 tabs = [
{ id: 'cancellations', label: t('reports.misc.cancellations'), Component: CancellationsTab },
{ id: 'discounts', label: t('reports.misc.discounts'), Component: DiscountsTab },
]
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>
)
}