// Reports → Expenses (plan step 6.12c; replaces the v1 expenses report): what the venue spent in
// a period (by the day each expense was entered), paid and still due; split by category or by
// supplier; every expense, a tap shows its payments. Entering expenses is Finance → Expenses.
// The period is filtered by the server (venue dates, 6.13a); categories / statuses are `finance.*` texts.
import { useState } from 'react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, Select } from '@xenia/ui'
import client from '../../../api/client'
import { useLiveQuery } from '../../../live/useLiveQuery'
import { Pill } from '../../overview/parts'
import { BarList } from '../kit/charts'
import { Failed, Loading, Panel, ReportPage, Stat, Table } from '../kit/parts'
import { PeriodPicker } from '../kit/Period'
import { defaultPeriod } from '../kit/reportLogic'
import { CATEGORIES, STATUS_TONE } from '../../finance/financeLogic'
export default function ExpensesReport() {
const { t } = useTranslation('manager')
return
}
function Expenses() {
const { t } = useTranslation('manager')
const fmt = useFormat()
const [period, setPeriod] = useState(() => defaultPeriod(fmt.venueToday()))
const [status, setStatus] = useState('')
const [category, setCategory] = useState('')
const [split, setSplit] = useState('category')
const [open, setOpen] = useState(null)
const params = { ...(period.from ? { from: period.from } : {}), ...(period.to ? { to: period.to } : {}), ...(status ? { status } : {}), ...(category ? { category } : {}) }
const { data, isPending, error, refetch } = useLiveQuery({ queryKey: ['report-expenses', params], queryFn: () => client.get('/api/expenses/', { params }).then(r => r.data), events: ['expenses.changed'] })
const catName = c => t(`finance.categories.${c}`, { defaultValue: c })
const rows = data ?? []
const sum = k => rows.reduce((s, e) => s + (e[k] ?? 0), 0)
const groups = Object.values(rows.reduce((m, e) => {
const k = split === 'category' ? e.category : (e.contact_name || t('reports.exp.noSupplier'))
m[k] = m[k] ?? { key: k, label: split === 'category' ? catName(k) : k, value: 0, due: 0 }
m[k].value += e.total_amount
m[k].due += e.due_amount
return m
}, {})).sort((a, b) => b.value - a.value)
const columns = [
{ key: 'created_at', label: t('reports.exp.date'), render: e => fmt.formatDate(e.created_at) },
{ key: 'description', label: t('reports.exp.what'), render: e => {e.description} },
{ key: 'category', label: t('reports.prod.category'), sort: e => catName(e.category), render: e => catName(e.category) },
{ key: 'contact_name', label: t('reports.exp.supplier'), render: e => e.contact_name || '' },
{ key: 'total_amount', label: t('reports.exp.total'), align: 'right', render: e => fmt.formatMoney(e.total_amount) },
{ key: 'due_amount', label: t('reports.exp.due'), align: 'right', render: e => (e.due_amount ? {fmt.formatMoney(e.due_amount)} : '') },
{ key: 'status', label: t('reports.status'), render: e => {t(`finance.status.${e.status}`, { defaultValue: e.status })} },
]
return (
)
}