fix: Improve Vendor Stock column detection to prioritize units over cost

This commit is contained in:
christian.vidal
2026-02-01 16:00:01 +01:00
parent f3e13769d7
commit 74f6fa2278
+29 -2
View File
@@ -1904,13 +1904,40 @@ export const processVendorStockExcel = async (fileOrBuffer: File | ArrayBuffer):
const stockIdx = headers.findIndex(h => { const stockIdx = headers.findIndex(h => {
const sh = String(h || '').toUpperCase(); 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 // Final sanity check for indexes, fallback to defaults if headers.findIndex returned -1
const finalAsinIdx = asinIdx !== -1 ? asinIdx : 0; const finalAsinIdx = asinIdx !== -1 ? asinIdx : 0;
const finalMarketplaceIdx = marketplaceIdx !== -1 ? marketplaceIdx : 3; 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++) { for (let i = headerRowIndex + 1; i < jsonData.length; i++) {
const row = jsonData[i]; const row = jsonData[i];