From 74f6fa22786ab2a0f46df7334fdfbea76bd70acf Mon Sep 17 00:00:00 2001 From: "christian.vidal" Date: Sun, 1 Feb 2026 16:00:01 +0100 Subject: [PATCH] fix: Improve Vendor Stock column detection to prioritize units over cost --- services/dataProcessor.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/services/dataProcessor.ts b/services/dataProcessor.ts index 7ec0568..8926582 100644 --- a/services/dataProcessor.ts +++ b/services/dataProcessor.ts @@ -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];