feat: add weekly sales tab with WoW growth

This commit is contained in:
Christian Vidal Wolf
2026-01-21 13:19:13 +01:00
parent cc6fd25065
commit 01c90ad889
7 changed files with 1315 additions and 2 deletions
+56 -1
View File
@@ -509,6 +509,7 @@ export const mergeSalesAndAdsData = (salesData: SalesRecord[], adsData: AdsRecor
marketplace: sale.customer,
customer: sale.customer,
month: sale.month,
week: sale.week || 0, // Preserve week info
year: sale.year,
asin: sale.asin,
title: sale.title,
@@ -1252,4 +1253,58 @@ export const aggregateForComparisonTimeSeries = (data: SalesRecord[]): Compariso
return Object.values(d).some(val => typeof val === 'number' && val > 0);
})
.sort((a, b) => a.week - b.week);
};
};
export interface WeeklyPivotRow {
id: string;
sku: string;
title: string;
asin: string;
line: string;
customer: string;
unitsByWeek: { [weekKey: string]: number }; // Key: "YYYY-WW"
}
export const pivotWeeklySalesData = (data: CombinedKPIs[]): {
rows: WeeklyPivotRow[],
weeks: string[]
} => {
// 1. Identify all unique weeks and sort descending (YYYY-WW)
const weekKeys = new Set<string>();
data.forEach(d => {
if (d.week) {
const weekKey = `${d.year}-${String(d.week).padStart(2, '0')}`;
weekKeys.add(weekKey);
}
});
const sortedWeeks = Array.from(weekKeys).sort((a, b) => b.localeCompare(a));
const map = new Map<string, WeeklyPivotRow>();
data.forEach(record => {
const key = record.sku || record.asin || `${record.title}-${record.line}`;
if (!key) return;
if (!map.has(key)) {
map.set(key, {
id: key,
sku: record.sku || '',
title: record.title || '',
asin: record.asin || '',
line: record.line || '',
customer: record.customer || record.marketplace || '',
unitsByWeek: {}
});
}
const row = map.get(key)!;
if (record.week) {
const weekKey = `${record.year}-${String(record.week).padStart(2, '0')}`;
row.unitsByWeek[weekKey] = (row.unitsByWeek[weekKey] || 0) + record.unitsTotal;
}
});
return {
rows: Array.from(map.values()),
weeks: sortedWeeks
};
};