mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:25:22 +02:00
feat: integrate Amazon Vendor Stock data across all views and fix local data loading
This commit is contained in:
@@ -1694,6 +1694,68 @@ export const calculateForecastViewData = (
|
||||
});
|
||||
};
|
||||
|
||||
export const processVendorStockExcel = async (fileOrBuffer: File | ArrayBuffer): Promise<Map<string, { eu: number; uk: number }>> => {
|
||||
try {
|
||||
const arrayBuffer = fileOrBuffer instanceof File
|
||||
? await fileOrBuffer.arrayBuffer()
|
||||
: fileOrBuffer;
|
||||
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
const worksheet = workbook.Sheets[sheetName];
|
||||
const jsonData: any[][] = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
|
||||
|
||||
const vendorStockMap = new Map<string, { eu: number; uk: number }>();
|
||||
|
||||
// Find header row (it contains "ASIN")
|
||||
let headerRowIndex = -1;
|
||||
for (let i = 0; i < Math.min(jsonData.length, 20); i++) {
|
||||
if (jsonData[i] && jsonData[i].includes('ASIN')) {
|
||||
headerRowIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (headerRowIndex === -1) {
|
||||
console.warn("Could not find header row in Vendor Stock Excel");
|
||||
return vendorStockMap;
|
||||
}
|
||||
|
||||
// Skip headers
|
||||
// A (0): ASIN
|
||||
// D (3): Marketplace (Country)
|
||||
// P (15): Sellable on hands units
|
||||
for (let i = headerRowIndex + 1; i < jsonData.length; i++) {
|
||||
const row = jsonData[i];
|
||||
if (!row || row.length < 16) continue;
|
||||
|
||||
const asin = String(row[0] || '').trim().toUpperCase();
|
||||
const marketplace = String(row[3] || '').trim().toLowerCase();
|
||||
const stockValue = parseUnits(String(row[15] || '0'));
|
||||
|
||||
if (!asin) continue;
|
||||
|
||||
if (!vendorStockMap.has(asin)) {
|
||||
vendorStockMap.set(asin, { eu: 0, uk: 0 });
|
||||
}
|
||||
|
||||
const current = vendorStockMap.get(asin)!;
|
||||
|
||||
// Map to UK or EU
|
||||
if (marketplace.includes('uk') || marketplace.includes('kingdom') || marketplace === 'gb') {
|
||||
current.uk += stockValue;
|
||||
} else {
|
||||
// Assume everything else is Pan-EU for now if it's not UK
|
||||
current.eu += stockValue;
|
||||
}
|
||||
}
|
||||
|
||||
return vendorStockMap;
|
||||
} catch (error) {
|
||||
console.error("Error processing Vendor Stock Excel:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const processStockExcel = async (fileOrBuffer: File | ArrayBuffer): Promise<Map<string, number>> => {
|
||||
try {
|
||||
const arrayBuffer = fileOrBuffer instanceof File
|
||||
|
||||
Reference in New Issue
Block a user