Fix: Corrected ForecastView data structure and missing aggregated properties

This commit is contained in:
Christian Vidal Wolf
2026-01-28 09:11:13 +01:00
parent 65accfdb26
commit 49decf9b9f
2 changed files with 26 additions and 7 deletions
+22 -6
View File
@@ -1656,23 +1656,39 @@ export const calculateForecastViewData = (
} }
} }
// 3. Build monthly points // 3. Build monthly points and aggregate
const monthlyData: MonthlyForecastPoint[] = MONTH_ORDER.map((m, idx) => { const monthlyData: Record<string, MonthlyForecastPoint> = {};
let totalActualUnits = 0;
let totalForecastUnits = 0;
MONTH_ORDER.forEach((m, idx) => {
const forecastUnits = Math.round(fc.annualForecast * productWeights[idx]); const forecastUnits = Math.round(fc.annualForecast * productWeights[idx]);
const actualUnits = actuals2026.get(identifier)?.get(m) || 0; const actualUnits = actuals2026.get(identifier)?.get(m) || 0;
return {
monthlyData[m] = {
month: m, month: m,
forecastUnits, forecastUnits,
actualUnits actualUnits,
}; units: actualUnits // Added for chart compatibility
} as any;
totalActualUnits += actualUnits;
totalForecastUnits += forecastUnits;
}); });
const accuracy = totalForecastUnits > 0
? Math.min(100, Math.round((1 - Math.abs(totalActualUnits - totalForecastUnits) / totalForecastUnits) * 100))
: 0;
return { return {
asin: identifier, asin: identifier,
sku: meta?.sku || fc.sku || identifier, sku: meta?.sku || fc.sku || identifier,
title: meta?.title || fc.title || identifier, title: meta?.title || fc.title || identifier,
line: meta?.line || fc.line || "", line: meta?.line || fc.line || "Unassigned",
annualForecast: fc.annualForecast, annualForecast: fc.annualForecast,
actualUnits: totalActualUnits,
forecastUnits: totalForecastUnits,
accuracy: Math.max(0, accuracy),
monthlyData monthlyData
}; };
}); });
+4 -1
View File
@@ -216,5 +216,8 @@ export interface ProductForecastData {
title: string; title: string;
line: string; line: string;
annualForecast: number; annualForecast: number;
monthlyData: MonthlyForecastPoint[]; actualUnits: number;
forecastUnits: number;
accuracy: number;
monthlyData: Record<string, MonthlyForecastPoint>;
} }