// Finance → Expenses (plan step 6.13a; replaces the v1 ExpensesPage): what the venue owes and has // paid. New expense (what, category, supplier, amount, due by), a payment (in parts until it's // paid), edit, delete; filters (unpaid / status, category, text); what is due and what is late. // The analysis of a period is Reports → Expenses. Live: `expenses.changed`. import { useState } from 'react' import { useMutation } from '@tanstack/react-query' import toast from 'react-hot-toast' import { Banknote, Pencil, Plus, Receipt, Trash2, Paperclip } from 'lucide-react' import { errorMessage } from '@xenia/api' import { useFormat, useTranslation } from '@xenia/i18n' import { Button, Dialog, Dropdown, EmptyState, IconButton, Page, PageHeader, SearchInput, Spinner, Toolbar } from '@xenia/ui' import client from '../../api/client' import { useLiveQuery } from '../../live/useLiveQuery' import { Pill } from '../overview/parts' import { Panel, Stat, Table } from '../reports/kit/parts' import ExpenseDialog from './ExpenseDialog' import ExpensePayDialog from './ExpensePayDialog' import { CATEGORIES, STATUS_TONE, expenseTotals, filterExpenses, isLate } from './financeLogic' export default function ExpensesPage() { const { t } = useTranslation('manager') const fmt = useFormat() // what's still to pay first: due + partly paid (= v1's "unpaid") const [filter, setFilter] = useState({ status: ['due', 'partial'], category: [], q: '' }) const [dialog, setDialog] = useState(null) // { kind: 'new' | 'edit' | 'pay' | 'view' | 'delete', expense } const { data, isPending, refetch } = useLiveQuery({ queryKey: ['expenses'], queryFn: () => client.get('/api/expenses/').then(r => r.data), events: ['expenses.changed'] }) const all = data ?? [] const today = fmt.venueToday() const rows = filterExpenses(all, filter) const owed = expenseTotals(all, today, fmt.venueDateKey) const shown = expenseTotals(rows, today, fmt.venueDateKey) const set = (k, v) => setFilter(f => ({ ...f, [k]: v })) const open = (kind, expense = null) => setDialog({ kind, expense }) const close = () => setDialog(null) const saved = () => { close(); refetch() } const remove = useMutation({ mutationFn: e => client.delete(`/api/expenses/${e.id}`), onSuccess: (_r, e) => { toast.success(t('finance.exp.deleted', { what: e.description })); saved() }, onError: err => toast.error(errorMessage(err, t)), }) const cat = c => t(`finance.categories.${c}`, { defaultValue: c }) const stop = fn => ev => { ev.stopPropagation(); fn() } const columns = [ { key: 'description', label: t('finance.exp.what'), render: e => ( {e.description} {e.documents?.length > 0 && } {e.contact_name && {e.contact_name}} ) }, { key: 'category', label: t('finance.exp.category'), sort: e => cat(e.category), render: e => cat(e.category) }, { key: 'created_at', label: t('finance.exp.entered'), render: e => fmt.formatDate(e.created_at) }, { key: 'due_date', label: t('finance.exp.dueBy'), sort: e => e.due_date ?? '', render: e => (e.due_date ? {fmt.formatDate(e.due_date)} : '') }, { key: 'total_amount', label: t('finance.exp.total'), align: 'right', render: e => fmt.formatMoney(e.total_amount) }, { key: 'due_amount', label: t('finance.exp.due'), align: 'right', render: e => (e.due_amount > 0 ? {fmt.formatMoney(e.due_amount)} : '') }, { key: 'status', label: t('finance.exp.status'), render: e => ( {t(`finance.status.${e.status}`, { defaultValue: e.status })} {isLate(e, today, fmt.venueDateKey) && {t('finance.exp.late')}} ) }, { key: 'actions', label: '', sort: false, render: e => ( {e.status !== 'paid' && open('pay', e))} data-testid="expense-pay">} open('edit', e))}> open('delete', e))}> ) }, ] return ( } onClick={() => open('new')} data-testid="expense-new">{t('finance.exp.new')}} />
set('q', v)} placeholder={t('finance.exp.search')} /> set('status', v)} aria-label={t('finance.exp.status')} data-testid="expenses-status" allLabel={t('finance.exp.all')} options={['due', 'partial', 'paid'].map(s => ({ value: s, label: t(`finance.status.${s}`) }))} /> set('category', v)} aria-label={t('finance.exp.category')} allLabel={t('finance.exp.allCategories')} options={CATEGORIES.map(c => ({ value: c, label: cat(c) }))} /> {isPending ?
: !all.length ? } title={t('finance.exp.none')} description={t('finance.exp.noneHint')} /> : ( open('view', e)} rowTestId="expense-row" empty={t('finance.exp.noMatch')} initialSort={{ key: 'created_at', dir: 'desc' }} /> )} {(dialog?.kind === 'new' || dialog?.kind === 'edit') && } {dialog?.kind === 'pay' && } {dialog?.kind === 'view' && ( open('pay', dialog.expense)} onEdit={() => open('edit', dialog.expense)} /> )} {dialog?.kind === 'delete' && ( } /> )} ) } /** One expense: amounts, who entered it, notes and every payment. */ function ExpenseView({ expense: e, cat, onClose, onPay, onEdit }) { const { t } = useTranslation('manager') const fmt = useFormat() return ( {e.status !== 'paid' && } }>
0 ? 'text-danger-ink' : undefined} />
{e.due_date &&

{t('finance.exp.dueOn', { date: fmt.formatDate(e.due_date) })}

} {e.notes &&

{e.notes}

}

{t('finance.exp.payments')}

{e.payments?.length ? (
    {e.payments.map(p => (
  • {fmt.formatDateTime(p.paid_at, 'datetimeShort')}{p.paid_by_name ? ` · ${p.paid_by_name}` : ''}{p.notes ? ` · ${p.notes}` : ''} {fmt.formatMoney(p.amount)}
  • ))}
) :

{t('finance.exp.noPayments')}

}
) } function Figure({ label, value, tone }) { return (
{label} {value}
) }