mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 16:40:56 +00:00
64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { apiClient } from '@/lib/api-client';
|
|
|
|
export type FinanceGranularity = 'daily' | 'weekly' | 'monthly';
|
|
|
|
export interface FinanceSummaryFilters {
|
|
dateFrom: string;
|
|
dateTo: string;
|
|
granularity?: FinanceGranularity;
|
|
originStationId?: string;
|
|
destinationStationId?: string;
|
|
method?: string;
|
|
}
|
|
|
|
export interface FinanceBucketRow {
|
|
period: string;
|
|
originStationId: string;
|
|
destinationStationId: string;
|
|
segmentLabel: string;
|
|
method: string;
|
|
currency: string;
|
|
bookingCount: number;
|
|
revenueMinor: number;
|
|
}
|
|
|
|
/** One roll-up entry. Amounts are never mixed across currencies — `currency` names which one this row is in. */
|
|
export interface FinanceRollupRow {
|
|
key: string;
|
|
label: string;
|
|
currency: string;
|
|
revenueMinor: number;
|
|
bookingCount: number;
|
|
}
|
|
|
|
export interface FinanceSummaryReport {
|
|
granularity: FinanceGranularity;
|
|
dateFrom: string;
|
|
dateTo: string;
|
|
/** Grand totals, one entry per currency present — never summed across currencies. */
|
|
totals: FinanceRollupRow[];
|
|
byPeriod: FinanceRollupRow[];
|
|
bySegment: FinanceRollupRow[];
|
|
byMethod: FinanceRollupRow[];
|
|
rows: FinanceBucketRow[];
|
|
}
|
|
|
|
/** Drops blanks so the API applies its own defaults (granularity=daily, no station/method filter). */
|
|
function toParams(filters: FinanceSummaryFilters): Record<string, string> {
|
|
const params: Record<string, string> = { dateFrom: filters.dateFrom, dateTo: filters.dateTo };
|
|
if (filters.granularity) params.granularity = filters.granularity;
|
|
if (filters.originStationId) params.originStationId = filters.originStationId;
|
|
if (filters.destinationStationId) params.destinationStationId = filters.destinationStationId;
|
|
if (filters.method) params.method = filters.method;
|
|
return params;
|
|
}
|
|
|
|
export const financeApi = {
|
|
getSummary: (filters: FinanceSummaryFilters) =>
|
|
apiClient.get<FinanceSummaryReport>('/reports/finance', { params: toParams(filters) }),
|
|
|
|
/** CSV export. `getRaw` because the endpoint streams a bare CSV body, not the `{success,data}` envelope. */
|
|
exportCsv: (filters: FinanceSummaryFilters) =>
|
|
apiClient.getRaw<string>('/reports/finance/export', { params: toParams(filters) }),
|
|
};
|