// 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
}
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 => {c.product_name} },
{ 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 (
} />
{isPending ?
: error ?
: (<>
{byPerson.length > 1 && (
fmt.formatMoney(v)} rows={byPerson.sort((a, b) => b.value - a.value).map(p => ({ ...p, sub: t('reports.misc.lines', { count: p.n }) }))} />
)}
>)}
)
}
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 => #{d.order_id} },
{ 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 => {fmt.formatMoney(d.discount_amount)} },
{ 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 (
{isPending ?
: error ?
: (<>
{data.by_waiter?.length > 0 && (
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 }) }))} />
)}
>)}
)
}
/** 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 => {c.product_name} },
{ 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 => {fmt.formatMoney(c.value, { signDisplay: 'exceptZero' })} },
{ 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 (
} />
{isPending ?
: error ?
: (<>
{data.by_person.length > 1 && (
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 }) }))} />
)}
>)}
)
}