Files
ember-market-frontend/lib/services/profit-analytics-service.ts
g b10b2f2701 Add custom date range support to profit analytics
Introduces a date range picker to the profit analytics dashboard, allowing users to select custom date ranges for profit calculations. Updates ProfitAnalyticsChart and profit-analytics-service to handle both period and date range queries, improving flexibility and user experience.
2025-11-28 18:32:38 +00:00

87 lines
2.1 KiB
TypeScript

import { apiRequest } from '../api';
export interface ProfitOverview {
period: string;
startDate?: string;
endDate?: string;
summary: {
totalRevenue: number;
revenueFromTrackedProducts: number;
totalCost: number;
totalProfit: number;
overallProfitMargin: number;
averageProfitPerUnit: number;
costDataCoverage: number;
totalProductsSold: number;
productsWithCostData: number;
};
topProfitableProducts: Array<{
productId: string;
productName: string;
totalQuantitySold: number;
totalRevenue: number;
totalCost: number;
totalProfit: number;
averageProfit: number;
profitMargin: number;
}>;
hasCostData: boolean;
}
export interface ProfitTrend {
_id: {
year: number;
month: number;
day: number;
};
revenue: number;
cost: number;
profit: number;
orders: number;
itemsWithCostData: number;
profitMargin: number;
}
export interface DateRange {
from: Date;
to: Date;
}
export const getProfitOverview = async (
periodOrRange?: string | DateRange
): Promise<ProfitOverview> => {
let url = '/analytics/profit-overview';
if (periodOrRange && typeof periodOrRange !== 'string') {
// Date range provided
const startDate = periodOrRange.from.toISOString().split('T')[0];
const endDate = periodOrRange.to.toISOString().split('T')[0];
url += `?startDate=${startDate}&endDate=${endDate}`;
} else {
// Period provided (backward compatibility)
const period = periodOrRange || '30';
url += `?period=${period}`;
}
return apiRequest(url);
};
export const getProfitTrends = async (
periodOrRange?: string | DateRange
): Promise<ProfitTrend[]> => {
let url = '/analytics/profit-trends';
if (periodOrRange && typeof periodOrRange !== 'string') {
// Date range provided
const startDate = periodOrRange.from.toISOString().split('T')[0];
const endDate = periodOrRange.to.toISOString().split('T')[0];
url += `?startDate=${startDate}&endDate=${endDate}`;
} else {
// Period provided (backward compatibility)
const period = periodOrRange || '30';
url += `?period=${period}`;
}
return apiRequest(url);
};