feat: one way to count sales in every report; exports for Excel; the report kit and Today on v2 (platform-v2 step 6.12a)
- services/sales.py: sold = active + paid lines; a line's value net of what
was refunded of it; system lines (the delivery fee) counted apart as fees;
cost/profit with has_gap. Products, categories, tables, traffic, revenue
trends, business days (list and current), per-waiter sales, order history
totals and the printed products/categories/tables reports all use it (they
used to differ: closed-only vs all orders, fee as a product, refunds never
subtracted)
- the overview's day summary values lines with their price change (discounts
were valued at full price)
- services/csv_export.py: UTF-8 BOM, times in venue time with the zone in the
header, ';' and decimal commas for a Greek venue (',' and '.' otherwise);
every report export uses it; orders export has products / fees / refunded
- fix: /api/reports/shift/orders with business_day_id crashed (no window)
- manager: src/pages/reports/kit (page with tabs, period picker, panels,
figures, sortable table, export button, order dialog reusing the order
page's parts, hour chart on tokens) and reports/today on it; v1 Today removed
- tests: test_sales_accounting.py (a day with a fee and a partial refund adds
up the same in every report), test_reports_export.py (Excel format),
reports.test.mjs, e2e manager.mjs (Today, order dialog, export, Greek)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
// The report kit's building blocks (plan step 6.12a): a report's page with tabs, a panel, a big
|
||||
// figure, a sortable table, loading / error states, the export button. On the tokens, texts in
|
||||
// `manager:reports.*`, money and dates through useFormat().
|
||||
import { useState } from 'react'
|
||||
import { useSearchParams } from 'react-router-dom'
|
||||
import toast from 'react-hot-toast'
|
||||
import { ArrowDown, ArrowUp, Download, Info } from 'lucide-react'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import { useFormat, useTranslation } from '@xenia/i18n'
|
||||
import { Button, EmptyState, InfoTip, Spinner, cx } from '@xenia/ui'
|
||||
import client from '../../../api/client'
|
||||
import { sortRows } from './reportLogic'
|
||||
|
||||
/** A report page: its tabs (kept in the address as ?tab=) over the chosen tab's content. */
|
||||
export function ReportPage({ title, tabs = [], children, testId }) {
|
||||
const [params, setParams] = useSearchParams()
|
||||
const active = tabs.find(t => t.id === params.get('tab'))?.id ?? tabs[0]?.id
|
||||
const tab = tabs.find(t => t.id === active)
|
||||
return (
|
||||
<div className="h-full overflow-y-auto" data-testid={testId}>
|
||||
<div className="max-w-7xl mx-auto p-4 lg:p-6 grid gap-4">
|
||||
<header className="flex flex-wrap items-end gap-3">
|
||||
<h1 className="text-title font-semibold text-ink flex-1 min-w-0">{title}</h1>
|
||||
{tabs.length > 1 && (
|
||||
<nav className="flex flex-wrap gap-1" role="tablist">
|
||||
{tabs.map(t => (
|
||||
<Button key={t.id} size="sm" variant={t.id === active ? 'primary' : 'ghost'} role="tab" aria-selected={t.id === active}
|
||||
onClick={() => setParams(p => { const n = new URLSearchParams(p); n.set('tab', t.id); return n }, { replace: true })} data-testid={`report-tab-${t.id}`}>
|
||||
{t.label}
|
||||
</Button>
|
||||
))}
|
||||
</nav>
|
||||
)}
|
||||
</header>
|
||||
{tab ? <tab.Component key={tab.id} /> : children}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** A card with a title, an optional tip and actions. */
|
||||
export function Panel({ title, tip, actions, children, className, padded = true, testId }) {
|
||||
return (
|
||||
<section className={cx('rounded-card border border-line bg-surface shadow-raised overflow-hidden', className)} data-testid={testId}>
|
||||
{(title || actions) && (
|
||||
<header className="flex flex-wrap items-center gap-2 px-4 py-3 border-b border-line">
|
||||
<h2 className="flex-1 min-w-0 text-body font-semibold text-ink flex items-center gap-1">{title}{tip && <InfoTip>{tip}</InfoTip>}</h2>
|
||||
{actions}
|
||||
</header>
|
||||
)}
|
||||
<div className={padded ? 'p-4' : ''}>{children}</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
const TONE = { success: 'text-success-ink', danger: 'text-danger-ink', warning: 'text-warning-ink', info: 'text-info-ink', accent: 'text-accent-ink' }
|
||||
|
||||
/** A figure: a label over a value, an optional line under it, a tone for the value. */
|
||||
export function Stat({ label, value, sub, tip, tone, big = false, testId }) {
|
||||
return (
|
||||
<div className="rounded-card border border-line bg-surface shadow-raised p-4 grid gap-1 content-start" data-testid={testId}>
|
||||
<span className="text-caption font-semibold uppercase tracking-wide text-ink-3 flex items-center gap-1">{label}{tip && <InfoTip>{tip}</InfoTip>}</span>
|
||||
<span className={cx('font-semibold tabular-nums', big ? 'text-display' : 'text-title', TONE[tone] ?? 'text-ink')}>{value}</span>
|
||||
{sub && <span className="text-caption text-ink-2">{sub}</span>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function Loading() {
|
||||
return <div className="py-16 grid place-items-center"><Spinner /></div>
|
||||
}
|
||||
|
||||
export function Failed({ error, retry }) {
|
||||
const { t } = useTranslation('manager')
|
||||
return <EmptyState title={t('reports.failed')} description={errorMessage(error, t)}
|
||||
action={retry ? <Button variant="secondary" onClick={retry}>{t('reports.retry')}</Button> : undefined} />
|
||||
}
|
||||
|
||||
/**
|
||||
* A sortable table. columns: [{ key, label, align?, render?(row), sort? (key or fn; false = not sortable), tip? }]
|
||||
* Shows at most `limit` rows ("Showing 100 of 240").
|
||||
*/
|
||||
export function Table({ columns, rows, initialSort, onRow, rowTestId, limit = 200, empty, rowKey = r => r.id }) {
|
||||
const { t, i18n } = useTranslation('manager')
|
||||
const [sort, setSort] = useState(initialSort ?? null) // { key, dir }
|
||||
if (!rows.length) return <EmptyState compact title={empty ?? t('reports.nothing')} />
|
||||
const col = sort && columns.find(c => c.key === sort.key)
|
||||
const sorted = col ? sortRows(rows, typeof col.sort === 'function' ? col.sort : (col.sort ?? col.key), sort.dir, i18n.language) : rows
|
||||
const shown = sorted.slice(0, limit)
|
||||
const flip = c => setSort(s => (s?.key === c.key ? { key: c.key, dir: s.dir === 'desc' ? 'asc' : 'desc' } : { key: c.key, dir: c.align === 'right' ? 'desc' : 'asc' }))
|
||||
return (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-label border-collapse">
|
||||
<thead>
|
||||
<tr className="bg-surface-2 text-ink-2">
|
||||
{columns.map(c => (
|
||||
<th key={c.key} className={cx('px-3 py-2 font-semibold whitespace-nowrap', c.align === 'right' ? 'text-right' : 'text-left')}>
|
||||
{c.sort === false ? c.label : (
|
||||
<button type="button" className="inline-flex items-center gap-1 hover:text-ink" onClick={() => flip(c)}>
|
||||
{c.label}{sort?.key === c.key && (sort.dir === 'desc' ? <ArrowDown size={12} /> : <ArrowUp size={12} />)}
|
||||
</button>
|
||||
)}
|
||||
{c.tip && <InfoTip>{c.tip}</InfoTip>}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-line">
|
||||
{shown.map(r => (
|
||||
<tr key={rowKey(r)} onClick={onRow ? () => onRow(r) : undefined} data-testid={rowTestId}
|
||||
className={cx(onRow && 'cursor-pointer hover:bg-surface-2')}>
|
||||
{columns.map(c => (
|
||||
<td key={c.key} className={cx('px-3 py-2 align-top', c.align === 'right' && 'text-right tabular-nums')}>{c.render ? c.render(r) : r[c.key]}</td>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{sorted.length > limit && <p className="px-3 py-2 text-caption text-ink-3 border-t border-line">{t('reports.showing', { shown: limit, total: sorted.length })}</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** Money with an optional "includes estimates" mark (a has_gap total is a lower bound). */
|
||||
export function MoneyCell({ value, partial }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const { formatMoney } = useFormat()
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1">
|
||||
{formatMoney(value ?? 0)}
|
||||
{partial && <span title={t('reports.partialTip')} className="text-warning-ink"><Info size={12} aria-label={t('reports.partial')} /></span>}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
/** Downloads a CSV from a report's export endpoint (venue time, the venue's Excel separators). */
|
||||
export function ExportButton({ endpoint, params, filename }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const [busy, setBusy] = useState(false)
|
||||
async function run() {
|
||||
setBusy(true)
|
||||
try {
|
||||
const r = await client.get(endpoint, { params, responseType: 'blob' })
|
||||
const url = URL.createObjectURL(r.data)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
||||
} catch (err) { toast.error(errorMessage(err, t)) } finally { setBusy(false) }
|
||||
}
|
||||
return <Button size="sm" variant="secondary" icon={<Download size={14} />} loading={busy} onClick={run} data-testid="report-export">{t('reports.export')}</Button>
|
||||
}
|
||||
Reference in New Issue
Block a user