feat: improve Week Coverage visibility, calculation robustness, and add aggregate WOC to grid totals and forecast view

This commit is contained in:
Christian Vidal Wolf
2026-04-17 12:35:25 +02:00
parent aa4a916c5d
commit 38f16aac89
4 changed files with 83 additions and 14 deletions
+18 -5
View File
@@ -2470,11 +2470,24 @@ export const calculateVelocityMap = (data: SalesRecord[]): Map<string, number> =
if (validYears.length === 0) return new Map();
const latestYear = validYears.reduce((a, b) => Math.max(a, b), 0);
// Find the latest week with actual units > 0 in the latest year
// This avoids using future weeks with 0 sales that might be in the data
const yearData = data.filter(r => r.year === latestYear);
const latestWeek = yearData.length > 0
? (yearData.map(r => r.week).filter(w => w !== undefined) as number[])
.reduce((a, b) => Math.max(a, b), 0)
: 0;
const weeksWithSales = yearData
.filter(r => (r.units || 0) > 0 && r.week !== undefined)
.map(r => r.week as number);
let latestWeek = 0;
if (weeksWithSales.length > 0) {
latestWeek = Math.max(...weeksWithSales);
} else {
// Fallback to highest week if no sales found
latestWeek = yearData.length > 0
? (yearData.map(r => r.week).filter(w => w !== undefined) as number[])
.reduce((a, b) => Math.max(a, b), 0)
: 0;
}
const last4WeeksKeys = new Set<string>();
for (let i = 0; i < 4; i++) {
@@ -2492,7 +2505,7 @@ export const calculateVelocityMap = (data: SalesRecord[]): Map<string, number> =
if (r.week === undefined) return;
if (last4WeeksKeys.has(`${r.year}|${r.week}`)) {
const key = r.asin.trim().toUpperCase();
asin4WeekSales.set(key, (asin4WeekSales.get(key) || 0) + r.units);
asin4WeekSales.set(key, (asin4WeekSales.get(key) || 0) + (r.units || 0));
}
});