Fix Global Weights: Fallback to flat distribution if reference data is sparse (<4 months)

This commit is contained in:
Christian Vidal Wolf
2026-01-28 13:24:29 +01:00
parent 894271b549
commit cfff95bff2
+12 -1
View File
@@ -1658,15 +1658,26 @@ export const calculateForecastViewData = (
const getGlobalWeightsInner = (records: SalesRecord[]) => { const getGlobalWeightsInner = (records: SalesRecord[]) => {
const weights = new Array(12).fill(0); const weights = new Array(12).fill(0);
let total = 0; let total = 0;
const seenMonths = new Set<string>();
records.forEach(r => { records.forEach(r => {
const m = r.month.split('-')[0]; const m = r.month.split('-')[0];
const idx = MONTH_ORDER.indexOf(m); const idx = MONTH_ORDER.indexOf(m);
if (idx !== -1) { if (idx !== -1) {
weights[idx] += r.units; weights[idx] += r.units;
total += r.units; total += r.units;
if (r.units > 0) seenMonths.add(m);
} }
}); });
return total === 0 ? new Array(12).fill(1 / 12) : weights.map(w => w / total);
// SAFETY NET: Even for Global Weights, if the reference file (2025 Sales)
// has fewer than 4 months of data (e.g. user only uploaded Jan 2025),
// we should NOT assume 100% seasonality in those months. Fallback to flat.
if (total === 0 || seenMonths.size < 4) {
return new Array(12).fill(1 / 12);
}
return weights.map(w => w / total);
}; };
const panEuWeights = getGlobalWeightsInner(panEuData2025); const panEuWeights = getGlobalWeightsInner(panEuData2025);