mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 15:25:23 +02:00
Implement Custom Numeric Filtering (>10, 5-10) for Stock/WOC columns in Weekly Sales
This commit is contained in:
+51
-55
@@ -49,6 +49,55 @@ const parseUnits = (value: string): number => {
|
||||
|
||||
const MONTH_ORDER = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
// Helper for numeric filtering (e.g. ">5", "10-20")
|
||||
export const checkNumericConditions = (value: number, filters: string[]): boolean => {
|
||||
if (!filters || filters.length === 0) return true;
|
||||
|
||||
return filters.some(f => {
|
||||
// Handle specific string labels
|
||||
if (f.includes('Out of Stock') || f === 'Out of Stock') return value === 0;
|
||||
if (f.includes('In Stock') && !f.includes('Low')) return value > 0;
|
||||
if (f.includes('Low Stock')) return value < 10;
|
||||
if (f === '< 4 Weeks') return value < 4;
|
||||
if (f === '> 4 Weeks') return value >= 4;
|
||||
if (f === 'Infinite Cover') return value === 999;
|
||||
|
||||
const input = f.trim().toLowerCase();
|
||||
|
||||
// Range: 10-20
|
||||
if (input.includes('-') && !input.startsWith('-')) { // Avoid negative numbers confusion if possible, though simple range usually 10-20
|
||||
const parts = input.split('-').map(s => parseFloat(s.trim()));
|
||||
if (parts.length === 2 && !isNaN(parts[0]) && !isNaN(parts[1])) {
|
||||
return value >= parts[0] && value <= parts[1];
|
||||
}
|
||||
}
|
||||
|
||||
// Expressions
|
||||
if (input.startsWith('<=')) {
|
||||
const val = parseFloat(input.substring(2));
|
||||
return !isNaN(val) && value <= val;
|
||||
}
|
||||
if (input.startsWith('>=')) {
|
||||
const val = parseFloat(input.substring(2));
|
||||
return !isNaN(val) && value >= val;
|
||||
}
|
||||
if (input.startsWith('<')) {
|
||||
const val = parseFloat(input.substring(1));
|
||||
return !isNaN(val) && value < val;
|
||||
}
|
||||
if (input.startsWith('>')) {
|
||||
const val = parseFloat(input.substring(1));
|
||||
return !isNaN(val) && value > val;
|
||||
}
|
||||
|
||||
// Exact Match
|
||||
const val = parseFloat(input);
|
||||
if (!isNaN(val)) return value === val;
|
||||
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
// Comprehensive Month Mapping (English + Spanish + Short/Full)
|
||||
const MONTH_MAP: Record<string, string> = {
|
||||
// English Short
|
||||
@@ -735,34 +784,7 @@ const checkStockFilter = (sku: string, filters: string[], stockMap?: Map<string,
|
||||
const baseSku = sku?.replace(/(DE|EN)$/i, '');
|
||||
const stockValue = stockMap.get(baseSku) || 0;
|
||||
|
||||
return filters.some(f => {
|
||||
// Smart Filters
|
||||
if (f === 'Out of Stock (0)') return stockValue === 0;
|
||||
if (f === 'In Stock (>0)') return stockValue > 0;
|
||||
if (f === 'Low Stock (<10)') return stockValue < 10;
|
||||
|
||||
// Numeric Range (e.g. ">10", "1-50")
|
||||
const input = f.trim().toLowerCase();
|
||||
if (input.includes('-')) {
|
||||
const [start, end] = input.split('-').map(s => parseFloat(s.trim()));
|
||||
if (!isNaN(start) && !isNaN(end)) return stockValue >= start && stockValue <= end;
|
||||
} else if (input.startsWith('<=')) {
|
||||
const val = parseFloat(input.substring(2).trim());
|
||||
if (!isNaN(val)) return stockValue <= val;
|
||||
} else if (input.startsWith('>=')) {
|
||||
const val = parseFloat(input.substring(2).trim());
|
||||
if (!isNaN(val)) return stockValue >= val;
|
||||
} else if (input.startsWith('<')) {
|
||||
const val = parseFloat(input.substring(1).trim());
|
||||
if (!isNaN(val)) return stockValue < val;
|
||||
} else if (input.startsWith('>')) {
|
||||
const val = parseFloat(input.substring(1).trim());
|
||||
if (!isNaN(val)) return stockValue > val;
|
||||
}
|
||||
|
||||
// Exact Match
|
||||
return stockValue.toString() === f;
|
||||
});
|
||||
return checkNumericConditions(stockValue, filters);
|
||||
};
|
||||
|
||||
const checkVendorStockFilter = (asin: string, filters: string[], vendorStockMap?: Map<string, { eu: number; uk: number }>, mode: 'eu' | 'uk' = 'eu'): boolean => {
|
||||
@@ -772,33 +794,7 @@ const checkVendorStockFilter = (asin: string, filters: string[], vendorStockMap?
|
||||
const data = vendorStockMap.get(asin);
|
||||
const stockValue = data ? (mode === 'uk' ? data.uk : data.eu) : 0;
|
||||
|
||||
return filters.some(f => {
|
||||
if (f === 'Out of Stock (0)') return stockValue === 0;
|
||||
if (f === 'In Stock (>20)') return stockValue > 20;
|
||||
if (f === 'In Stock (>0)') return stockValue > 0;
|
||||
if (f === 'Low Stock (<10)') return stockValue < 10;
|
||||
|
||||
const input = f.trim().toLowerCase();
|
||||
if (input.includes('-')) {
|
||||
const [start, end] = input.split('-').map(s => parseFloat(s.trim()));
|
||||
if (!isNaN(start) && !isNaN(end)) return stockValue >= start && stockValue <= end;
|
||||
} else if (input.startsWith('<=')) {
|
||||
const val = parseFloat(input.substring(2).trim());
|
||||
if (!isNaN(val)) return stockValue <= val;
|
||||
} else if (input.startsWith('>=')) {
|
||||
const val = parseFloat(input.substring(2).trim());
|
||||
if (!isNaN(val)) return stockValue >= val;
|
||||
} else if (input.startsWith('<')) {
|
||||
const val = parseFloat(input.substring(1).trim());
|
||||
if (!isNaN(val)) return stockValue < val;
|
||||
} else if (input.startsWith('>')) {
|
||||
const val = parseFloat(input.substring(1).trim());
|
||||
if (!isNaN(val)) return stockValue > val;
|
||||
} else if (!isNaN(parseFloat(input))) {
|
||||
return stockValue === parseFloat(input);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return checkNumericConditions(stockValue, filters);
|
||||
};
|
||||
|
||||
// Filter Ads Data by Country, Year, Week, ASIN, SKU, and Product Line
|
||||
|
||||
Reference in New Issue
Block a user