mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-03 08:33:39 +00:00
78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { apiClient } from '@/lib/api-client';
|
|
|
|
export type FinanceGranularity = 'daily' | 'weekly' | 'monthly';
|
|
|
|
/**
|
|
* Domestic vs cross-border traffic. `intercity` means both endpoints sit in Ethiopia;
|
|
* `international` means either endpoint is outside it — in practice Djibouti, including a
|
|
* trip wholly inside Djibouti. Mirrors `FinanceTripType` on the API.
|
|
*/
|
|
export type FinanceTripType = 'intercity' | 'international';
|
|
|
|
export interface FinanceSummaryFilters {
|
|
dateFrom: string;
|
|
dateTo: string;
|
|
granularity?: FinanceGranularity;
|
|
originStationId?: string;
|
|
destinationStationId?: string;
|
|
method?: string;
|
|
tripType?: FinanceTripType;
|
|
}
|
|
|
|
export interface FinanceBucketRow {
|
|
period: string;
|
|
originStationId: string;
|
|
destinationStationId: string;
|
|
segmentLabel: string;
|
|
tripType: FinanceTripType;
|
|
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;
|
|
/** The trip-type filter that was applied, or `null` when every trip is included. */
|
|
tripType: FinanceTripType | null;
|
|
/** Grand totals, one entry per currency present — never summed across currencies. */
|
|
totals: FinanceRollupRow[];
|
|
byPeriod: FinanceRollupRow[];
|
|
bySegment: FinanceRollupRow[];
|
|
byMethod: FinanceRollupRow[];
|
|
/** Intercity vs international split. One entry per trip type per currency. */
|
|
byTripType: 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;
|
|
if (filters.tripType) params.tripType = filters.tripType;
|
|
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) }),
|
|
};
|