Update stock status calculations and add restock/overstock qty fields and calculations
This commit is contained in:
@@ -760,11 +760,45 @@ async function calculateMetrics() {
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Get current stock
|
||||
// Get current stock and stock age
|
||||
const [stockInfo] = await connection.query(`
|
||||
SELECT stock_quantity, cost_price
|
||||
FROM products
|
||||
WHERE product_id = ?
|
||||
SELECT
|
||||
p.stock_quantity,
|
||||
p.cost_price,
|
||||
p.created_at,
|
||||
p.replenishable,
|
||||
p.moq,
|
||||
DATEDIFF(CURDATE(), MIN(po.received_date)) as days_since_first_stock,
|
||||
DATEDIFF(CURDATE(), COALESCE(
|
||||
(SELECT MAX(o2.date)
|
||||
FROM orders o2
|
||||
WHERE o2.product_id = p.product_id
|
||||
AND o2.canceled = false),
|
||||
CURDATE() -- If no sales, use current date
|
||||
)) as days_since_last_sale,
|
||||
(SELECT SUM(quantity)
|
||||
FROM orders o3
|
||||
WHERE o3.product_id = p.product_id
|
||||
AND o3.canceled = false) as total_quantity_sold,
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM orders o
|
||||
WHERE o.product_id = p.product_id
|
||||
AND o.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
AND o.canceled = false
|
||||
AND (SELECT SUM(quantity) FROM orders o2
|
||||
WHERE o2.product_id = p.product_id
|
||||
AND o2.date >= o.date
|
||||
AND o2.canceled = false) = 0
|
||||
) THEN true
|
||||
ELSE false
|
||||
END as had_recent_stockout
|
||||
FROM products p
|
||||
LEFT JOIN purchase_orders po ON p.product_id = po.product_id
|
||||
AND po.status = 'closed'
|
||||
AND po.received > 0
|
||||
WHERE p.product_id = ?
|
||||
GROUP BY p.product_id
|
||||
`, [product.product_id]).catch(err => {
|
||||
logError(err, `Failed to get stock info for product ${product.product_id}`);
|
||||
throw err;
|
||||
@@ -787,17 +821,118 @@ async function calculateMetrics() {
|
||||
// Calculate current inventory value
|
||||
const inventory_value = (stock.stock_quantity || 0) * (stock.cost_price || 0);
|
||||
|
||||
// Calculate stock status using configurable thresholds with proper handling of zero sales
|
||||
const stock_status = daily_sales_avg === 0 ? 'New' :
|
||||
stock.stock_quantity <= Math.max(1, Math.ceil(daily_sales_avg * config.critical_days)) ? 'Critical' :
|
||||
stock.stock_quantity <= Math.max(1, Math.ceil(daily_sales_avg * config.reorder_days)) ? 'Reorder' :
|
||||
stock.stock_quantity > Math.max(1, daily_sales_avg * config.overstock_days) ? 'Overstocked' : 'Healthy';
|
||||
// Calculate stock status with improved handling
|
||||
const stock_status = (() => {
|
||||
const days_since_first_stock = stockInfo[0]?.days_since_first_stock || 0;
|
||||
const days_since_last_sale = stockInfo[0]?.days_since_last_sale || 9999;
|
||||
const total_quantity_sold = stockInfo[0]?.total_quantity_sold || 0;
|
||||
const had_recent_stockout = stockInfo[0]?.had_recent_stockout || false;
|
||||
const dq = stock.stock_quantity || 0;
|
||||
const ds = daily_sales_avg || 0;
|
||||
const ws = weekly_sales_avg || 0;
|
||||
const ms = monthly_sales_avg || 0;
|
||||
|
||||
// If no stock, return immediately
|
||||
if (dq === 0) {
|
||||
return had_recent_stockout ? 'Critical' : 'Out of Stock';
|
||||
}
|
||||
|
||||
// 1. Check if truly "New" (≤30 days and no sales)
|
||||
if (days_since_first_stock <= 30 && total_quantity_sold === 0) {
|
||||
return 'New';
|
||||
}
|
||||
|
||||
// 2. Handle zero or very low sales velocity cases
|
||||
if (ds === 0 || (ds < 0.1 && ws < 0.5)) { // Less than 1 sale per 10 days and less than 0.5 per week
|
||||
if (days_since_first_stock > config.overstock_days) {
|
||||
return 'Overstocked';
|
||||
}
|
||||
if (days_since_first_stock > 30) {
|
||||
return 'At Risk';
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Calculate days of supply and check velocity trends
|
||||
const days_of_supply = ds > 0 ? dq / ds : 999;
|
||||
const velocity_trend = ds > 0 ? (ds / (ms || ds) - 1) * 100 : 0; // Percent change from monthly to daily avg
|
||||
|
||||
// Critical stock level
|
||||
if (days_of_supply <= config.critical_days) {
|
||||
return 'Critical';
|
||||
}
|
||||
|
||||
// Reorder cases
|
||||
if (days_of_supply <= config.reorder_days ||
|
||||
(had_recent_stockout && days_of_supply <= config.reorder_days * 1.5)) {
|
||||
return 'Reorder';
|
||||
}
|
||||
|
||||
// At Risk cases (multiple scenarios)
|
||||
if (
|
||||
// Approaching overstock threshold
|
||||
(days_of_supply >= config.overstock_days * 0.8) ||
|
||||
// Significant sales decline
|
||||
(velocity_trend <= -50 && days_of_supply > config.reorder_days * 2) ||
|
||||
// No recent sales
|
||||
(days_since_last_sale > 45 && dq > 0) ||
|
||||
// Very low velocity with significant stock
|
||||
(ds > 0 && ds < 0.2 && dq > ds * config.overstock_days * 0.5)
|
||||
) {
|
||||
return 'At Risk';
|
||||
}
|
||||
|
||||
// Overstock cases
|
||||
if (days_of_supply >= config.overstock_days) {
|
||||
return 'Overstocked';
|
||||
}
|
||||
|
||||
// If none of the above conditions are met
|
||||
return 'Healthy';
|
||||
})();
|
||||
|
||||
// Calculate safety stock using configured values with proper defaults
|
||||
const safety_stock = daily_sales_avg > 0 ?
|
||||
Math.max(1, Math.ceil(daily_sales_avg * (config.safety_stock_days || 14) * ((config.service_level || 95.0) / 100))) :
|
||||
null;
|
||||
|
||||
// Calculate reorder quantity and overstocked amount
|
||||
let reorder_qty = 0;
|
||||
let overstocked_amt = 0;
|
||||
|
||||
// Only calculate reorder quantity for replenishable products
|
||||
if (stock.replenishable && (stock_status === 'Critical' || stock_status === 'Reorder')) {
|
||||
const ds = daily_sales_avg || 0;
|
||||
const lt = purchases.avg_lead_time_days || 14; // Default to 14 days if no lead time data
|
||||
const sc = config.safety_stock_days || 14;
|
||||
const ss = safety_stock || 0;
|
||||
const dq = stock.stock_quantity || 0;
|
||||
const moq = stock.moq || 1;
|
||||
|
||||
// Calculate desired stock level based on daily sales, lead time, coverage days, and safety stock
|
||||
const desired_stock = (ds * (lt + sc)) + ss;
|
||||
|
||||
// Calculate raw reorder amount
|
||||
const raw_reorder = Math.max(0, desired_stock - dq);
|
||||
|
||||
// Round up to nearest MOQ
|
||||
reorder_qty = Math.ceil(raw_reorder / moq) * moq;
|
||||
}
|
||||
|
||||
// Calculate overstocked amount for overstocked products
|
||||
if (stock_status === 'Overstocked') {
|
||||
const ds = daily_sales_avg || 0;
|
||||
const dq = stock.stock_quantity || 0;
|
||||
const lt = purchases.avg_lead_time_days || 14;
|
||||
const sc = config.safety_stock_days || 14;
|
||||
const ss = safety_stock || 0;
|
||||
|
||||
// Calculate maximum desired stock based on overstock days configuration
|
||||
const max_desired_stock = (ds * config.overstock_days) + ss;
|
||||
|
||||
// Calculate excess inventory
|
||||
overstocked_amt = Math.max(0, dq - max_desired_stock);
|
||||
}
|
||||
|
||||
// Add to batch update
|
||||
metricsUpdates.push([
|
||||
product.product_id,
|
||||
@@ -818,7 +953,9 @@ async function calculateMetrics() {
|
||||
purchases.avg_lead_time_days || null,
|
||||
purchases.last_purchase_date || null,
|
||||
purchases.last_received_date || null,
|
||||
stock_status
|
||||
stock_status,
|
||||
reorder_qty,
|
||||
overstocked_amt
|
||||
]);
|
||||
} catch (err) {
|
||||
logError(err, `Failed processing product ${product.product_id}`);
|
||||
@@ -849,7 +986,9 @@ async function calculateMetrics() {
|
||||
avg_lead_time_days,
|
||||
last_purchase_date,
|
||||
last_received_date,
|
||||
stock_status
|
||||
stock_status,
|
||||
reorder_qty,
|
||||
overstocked_amt
|
||||
) VALUES ?
|
||||
ON DUPLICATE KEY UPDATE
|
||||
last_calculated_at = NOW(),
|
||||
@@ -870,7 +1009,9 @@ async function calculateMetrics() {
|
||||
avg_lead_time_days = VALUES(avg_lead_time_days),
|
||||
last_purchase_date = VALUES(last_purchase_date),
|
||||
last_received_date = VALUES(last_received_date),
|
||||
stock_status = VALUES(stock_status)
|
||||
stock_status = VALUES(stock_status),
|
||||
reorder_qty = VALUES(reorder_qty),
|
||||
overstocked_amt = VALUES(overstocked_amt)
|
||||
`, [metricsUpdates]).catch(err => {
|
||||
logError(err, `Failed to batch update metrics for ${metricsUpdates.length} products`);
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user