Add per-event finance, event team permissions and public door sales state.

Event finance
- Finance tab on the event page: P&L summary with a revenue-to-result
  waterfall, costs and other income in one ledger, and the partner split
  with payouts. Lifecycle stepper (Selling, Adding costs, Ready to close,
  Finalized) with what is left to do; closing the books goes through a
  checklist dialog that freezes the numbers.
- Expense modal shows only the fields each calculation type needs, a live
  preview with the event's real counts, and a category suggested from the
  description. Date inputs follow the UI language.
- Global Finance page: profit per event with outliers clipped and labelled,
  and a "Ready to close" list of past events whose books are still open.
- Calculation service (integer PYG, basis points) with pinned regression
  scenarios; PYG formatting centralised in lib/money with locale-aware
  separators.

Event team permissions
- Per-event members with role presets and requireEventPermission; the
  header stat "Confirmed" is relabelled "Not checked in yet", which is
  what it counts.

Public sales state
- One sales state (online, door, sold out, ended, external, cancelled) for
  the event page, listings and JSON-LD, with the door price and tenders
  shown only while people can still pay at the door.

Frontend unit tests run with vitest (npm test in frontend/).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-10-03 03:11:46 +00:00
co-authored by Claude Opus 5.5
parent b51c1360e2
commit e203fb6c74
80 changed files with 11326 additions and 971 deletions
@@ -0,0 +1,91 @@
'use client';
import { useCallback, useEffect, useMemo, useState } from 'react';
import toast from 'react-hot-toast';
import { useLanguage } from '@/context/LanguageContext';
import { financeApi, type EventFinance } from '@/lib/api';
import { Skeleton } from '@/components/ui/Skeleton';
import { SubNav } from './ui';
import { SummaryView } from './SummaryView';
import { ExpensesView } from './ExpensesView';
import { SplitView } from './SplitView';
import { LifecycleStepper } from './LifecycleStepper';
import { deriveLifecycle } from './derive';
export type FinanceSection = 'summary' | 'expenses' | 'split';
const SECTIONS: FinanceSection[] = ['summary', 'expenses', 'split'];
/** Keep the open section in the URL (?fin=expenses) so reloads and shared links land on it. */
function readSection(): FinanceSection {
if (typeof window === 'undefined') return 'summary';
const v = new URLSearchParams(window.location.search).get('fin');
return SECTIONS.includes(v as FinanceSection) ? (v as FinanceSection) : 'summary';
}
function writeSection(section: FinanceSection) {
const url = new URL(window.location.href);
if (section === 'summary') url.searchParams.delete('fin'); else url.searchParams.set('fin', section);
window.history.replaceState(window.history.state, '', url);
}
/**
* Finance tab: P&L summary with charts, expenses and other income, and the
* partner split with payouts. The server already scopes what comes back (e.g.
* collaborators only get their own share), so this renders what it receives.
*/
export function FinanceTab({ eventId }: { eventId: string }) {
const { t } = useLanguage();
const [data, setData] = useState<EventFinance | null>(null);
const [loading, setLoading] = useState(true);
const [section, setSectionState] = useState<FinanceSection>('summary');
useEffect(() => { setSectionState(readSection()); }, []);
const setSection = useCallback((s: FinanceSection) => { setSectionState(s); writeSection(s); }, []);
const load = useCallback(async () => {
try {
setData(await financeApi.get(eventId));
} catch (error: any) {
toast.error(error.message || t('admin.finance.loadError'));
} finally {
setLoading(false);
}
}, [eventId, t]);
useEffect(() => { load(); }, [load]);
// Recomputed with every reload, so "ended X days ago" stays current.
const lifecycle = useMemo(() => (data ? deriveLifecycle(data) : null), [data]);
if (loading) {
return (
<div className="space-y-3" aria-busy="true">
<Skeleton className="h-24" />
<Skeleton className="h-11 w-full md:w-96" />
<div className="grid grid-cols-2 lg:grid-cols-4 gap-3">
{Array.from({ length: 4 }).map((_, i) => <Skeleton key={i} className="h-24" />)}
</div>
<Skeleton className="h-64" />
</div>
);
}
if (!data || !lifecycle) return <p className="text-sm text-gray-500 py-8 text-center">{t('admin.finance.loadError')}</p>;
return (
<div className="space-y-4">
<LifecycleStepper data={data} lifecycle={lifecycle} goTo={setSection} />
<SubNav<FinanceSection>
items={[
{ key: 'summary', label: t('admin.finance.subnav.summary') },
{ key: 'expenses', label: t('admin.finance.subnav.expenses'), count: data.expenses.length + data.otherIncome.length },
{ key: 'split', label: t('admin.finance.subnav.split'), count: data.partners.length },
]}
active={section}
onChange={setSection}
/>
{section === 'summary' && <SummaryView data={data} goTo={setSection} />}
{section === 'expenses' && <ExpensesView eventId={eventId} data={data} onChange={load} />}
{section === 'split' && <SplitView eventId={eventId} data={data} lifecycle={lifecycle} onChange={load} />}
</div>
);
}