mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 13:45:23 +02:00
fix: Improve Vendor Stock column detection to prioritize units over cost
This commit is contained in:
@@ -1904,13 +1904,40 @@ export const processVendorStockExcel = async (fileOrBuffer: File | ArrayBuffer):
|
||||
|
||||
const stockIdx = headers.findIndex(h => {
|
||||
const sh = String(h || '').toUpperCase();
|
||||
return sh.includes('ON HAND') || sh.includes('SELLABLE') || sh.includes('STOCK') || sh.includes('AVAILABILITY');
|
||||
// EXCLUSION LIST: Skip columns that are clearly monetary or unrelated
|
||||
if (sh.includes('COST') || sh.includes('VALUE') || sh.includes('AMOUNT') || sh.includes('PRICE') || sh.includes('CURRENCY') || sh.includes('SC')) {
|
||||
return false;
|
||||
}
|
||||
// PRIORITY LIST: Look for Units/Qty specific keywords
|
||||
return sh.includes('UNITS') || sh.includes('QTY') || sh.includes('QUANTITY') ||
|
||||
// Fallback to generic stock terms if specific unit terms aren't found,
|
||||
// but only if we haven't found a better match yet (implemented via logic flow below)
|
||||
sh.includes('ON HAND') || sh.includes('SELLABLE') || sh.includes('STOCK') || sh.includes('AVAILABILITY');
|
||||
});
|
||||
|
||||
// REFINED SEARCH STRATEGY:
|
||||
// 1. First pass: strict "Units/Qty" search
|
||||
let finalStockIdx = headers.findIndex(h => {
|
||||
const sh = String(h || '').toUpperCase();
|
||||
return (sh.includes('UNITS') || sh.includes('QTY') || sh.includes('QUANTITY')) &&
|
||||
!sh.includes('COST') && !sh.includes('VALUE') && !sh.includes('AMOUNT');
|
||||
});
|
||||
|
||||
// 2. Second pass: "On Hand" / "Stock" (if no Units found)
|
||||
if (finalStockIdx === -1) {
|
||||
finalStockIdx = headers.findIndex(h => {
|
||||
const sh = String(h || '').toUpperCase();
|
||||
return (sh.includes('ON HAND') || sh.includes('SELLABLE') || sh.includes('STOCK') || sh.includes('AVAILABILITY')) &&
|
||||
!sh.includes('COST') && !sh.includes('VALUE') && !sh.includes('AMOUNT');
|
||||
});
|
||||
}
|
||||
|
||||
// Final sanity check for indexes, fallback to defaults if headers.findIndex returned -1
|
||||
const finalAsinIdx = asinIdx !== -1 ? asinIdx : 0;
|
||||
const finalMarketplaceIdx = marketplaceIdx !== -1 ? marketplaceIdx : 3;
|
||||
const finalStockIdx = stockIdx !== -1 ? stockIdx : 15;
|
||||
finalStockIdx = finalStockIdx !== -1 ? finalStockIdx : 15;
|
||||
|
||||
console.log(`[VendorStock] Selected Stock Column: "${headers[finalStockIdx]}" (Index: ${finalStockIdx})`);
|
||||
|
||||
for (let i = headerRowIndex + 1; i < jsonData.length; i++) {
|
||||
const row = jsonData[i];
|
||||
|
||||
Reference in New Issue
Block a user