mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:15:23 +02:00
feat: improve Week Coverage visibility, calculation robustness, and add aggregate WOC to grid totals and forecast view
This commit is contained in:
@@ -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));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user