Update calculate script to account for import changes
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
const { outputProgress, logError } = require('./utils/progress');
|
||||
const { getConnection } = require('./utils/db');
|
||||
|
||||
// Helper function to handle NaN and undefined values
|
||||
function sanitizeValue(value) {
|
||||
if (value === undefined || value === null || Number.isNaN(value)) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
async function calculateProductMetrics(startTime, totalProducts, processedCount = 0) {
|
||||
const connection = await getConnection();
|
||||
try {
|
||||
// Process in batches of 250
|
||||
const batchSize = 250;
|
||||
for (let offset = 0; offset < totalProducts; offset += batchSize) {
|
||||
const [products] = await connection.query('SELECT product_id, vendor FROM products LIMIT ? OFFSET ?', [batchSize, offset])
|
||||
const [products] = await connection.query('SELECT pid, vendor FROM products LIMIT ? OFFSET ?', [batchSize, offset])
|
||||
.catch(err => {
|
||||
logError(err, `Failed to fetch products batch at offset ${offset}`);
|
||||
throw err;
|
||||
@@ -34,12 +42,12 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
const [configs] = await connection.query(`
|
||||
WITH product_info AS (
|
||||
SELECT
|
||||
p.product_id,
|
||||
p.pid,
|
||||
p.vendor,
|
||||
pc.category_id
|
||||
pc.cat_id as category_id
|
||||
FROM products p
|
||||
LEFT JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
WHERE p.product_id = ?
|
||||
LEFT JOIN product_categories pc ON p.pid = pc.pid
|
||||
WHERE p.pid = ?
|
||||
),
|
||||
threshold_options AS (
|
||||
SELECT
|
||||
@@ -141,7 +149,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
ORDER BY priority LIMIT 1),
|
||||
95.0
|
||||
) as service_level
|
||||
`, [product.product_id]);
|
||||
`, [product.pid]);
|
||||
|
||||
const config = configs[0];
|
||||
|
||||
@@ -163,9 +171,9 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
END as rolling_weekly_avg,
|
||||
SUM(CASE WHEN o.date >= DATE_SUB(CURDATE(), INTERVAL ? DAY) THEN o.quantity ELSE 0 END) as last_month_qty
|
||||
FROM orders o
|
||||
JOIN products p ON o.product_id = p.product_id
|
||||
WHERE o.canceled = 0 AND o.product_id = ?
|
||||
GROUP BY o.product_id
|
||||
JOIN products p ON o.pid = p.pid
|
||||
WHERE o.canceled = 0 AND o.pid = ?
|
||||
GROUP BY o.pid
|
||||
)
|
||||
SELECT
|
||||
total_quantity_sold,
|
||||
@@ -184,7 +192,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
config.weekly_window_days,
|
||||
config.weekly_window_days,
|
||||
config.monthly_window_days,
|
||||
product.product_id,
|
||||
product.pid,
|
||||
config.daily_window_days,
|
||||
config.weekly_window_days,
|
||||
config.monthly_window_days
|
||||
@@ -201,8 +209,8 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
DATEDIFF(received_date, date) as lead_time_days,
|
||||
ROW_NUMBER() OVER (ORDER BY date DESC) as order_rank
|
||||
FROM purchase_orders
|
||||
WHERE status = 'closed'
|
||||
AND product_id = ?
|
||||
WHERE receiving_status >= 30 -- Partial or fully received
|
||||
AND pid = ?
|
||||
AND received > 0
|
||||
AND received_date IS NOT NULL
|
||||
),
|
||||
@@ -220,7 +228,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
MAX(received_date) as last_received_date,
|
||||
AVG(lead_time_days) as avg_lead_time_days
|
||||
FROM lead_time_orders
|
||||
`, [product.product_id]);
|
||||
`, [product.pid]);
|
||||
|
||||
// Get stock info
|
||||
const [stockInfo] = await connection.query(`
|
||||
@@ -230,269 +238,267 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
p.created_at,
|
||||
p.replenishable,
|
||||
p.moq,
|
||||
p.notions_inv_count,
|
||||
p.date_last_sold,
|
||||
p.total_sold,
|
||||
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()
|
||||
)) 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,
|
||||
DATEDIFF(CURDATE(), COALESCE(p.date_last_sold, CURDATE())) as days_since_last_sale,
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM orders o
|
||||
WHERE o.product_id = p.product_id
|
||||
WHERE o.pid = p.pid
|
||||
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
|
||||
WHERE o2.pid = p.pid
|
||||
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'
|
||||
LEFT JOIN purchase_orders po ON p.pid = po.pid
|
||||
AND po.receiving_status >= 30 -- Partial or fully received
|
||||
AND po.received > 0
|
||||
WHERE p.product_id = ?
|
||||
GROUP BY p.product_id
|
||||
`, [product.product_id]);
|
||||
WHERE p.pid = ?
|
||||
GROUP BY p.pid
|
||||
`, [product.pid]);
|
||||
|
||||
// Calculate metrics
|
||||
const metrics = salesMetrics[0] || {};
|
||||
const purchases = purchaseMetrics[0] || {};
|
||||
const stock = stockInfo[0] || {};
|
||||
const salesData = salesMetrics[0] || {};
|
||||
const purchaseData = purchaseMetrics[0] || {};
|
||||
const stockData = stockInfo[0] || {};
|
||||
|
||||
const daily_sales_avg = metrics.rolling_daily_avg || 0;
|
||||
const weekly_sales_avg = metrics.rolling_weekly_avg || 0;
|
||||
const monthly_sales_avg = metrics.total_quantity_sold ? metrics.total_quantity_sold / 30 : 0;
|
||||
|
||||
// Calculate days of inventory
|
||||
const days_of_inventory = daily_sales_avg > 0 ?
|
||||
Math.ceil(
|
||||
(stock.stock_quantity / daily_sales_avg) +
|
||||
(purchases.avg_lead_time_days || config.reorder_days) *
|
||||
(1 + (config.service_level / 100))
|
||||
) : null;
|
||||
// Sales velocity metrics
|
||||
const daily_sales_avg = sanitizeValue(salesData.rolling_daily_avg) || 0;
|
||||
const weekly_sales_avg = sanitizeValue(salesData.rolling_weekly_avg) || 0;
|
||||
const monthly_sales_avg = sanitizeValue(salesData.rolling_monthly_avg) || 0;
|
||||
|
||||
const weeks_of_inventory = days_of_inventory ? Math.ceil(days_of_inventory / 7) : null;
|
||||
|
||||
// Calculate margin percent
|
||||
const margin_percent = metrics.total_revenue > 0 ?
|
||||
((metrics.total_revenue - metrics.total_cost) / metrics.total_revenue) * 100 :
|
||||
null;
|
||||
|
||||
// Calculate inventory value
|
||||
const inventory_value = (stock.stock_quantity || 0) * (stock.cost_price || 0);
|
||||
// Stock metrics
|
||||
const stock_quantity = sanitizeValue(stockData.stock_quantity) || 0;
|
||||
const days_of_inventory = daily_sales_avg > 0 ? Math.floor(stock_quantity / daily_sales_avg) : 999;
|
||||
const weeks_of_inventory = Math.floor(days_of_inventory / 7);
|
||||
|
||||
// Calculate stock status
|
||||
const stock_status = calculateStockStatus(stock, config, daily_sales_avg, weekly_sales_avg, monthly_sales_avg);
|
||||
const stock_status = calculateStockStatus(
|
||||
stock_quantity,
|
||||
config,
|
||||
daily_sales_avg,
|
||||
weekly_sales_avg,
|
||||
monthly_sales_avg
|
||||
);
|
||||
|
||||
// Calculate reorder quantity and overstocked amount
|
||||
const { reorder_qty, overstocked_amt } = calculateReorderQuantities(
|
||||
stock,
|
||||
// Calculate reorder quantities
|
||||
const reorder_quantities = calculateReorderQuantities(
|
||||
stock_quantity,
|
||||
stock_status,
|
||||
daily_sales_avg,
|
||||
purchases.avg_lead_time_days,
|
||||
sanitizeValue(purchaseData.avg_lead_time_days) || 0,
|
||||
config
|
||||
);
|
||||
|
||||
// Add to batch update
|
||||
// Financial metrics
|
||||
const cost_price = sanitizeValue(stockData.cost_price) || 0;
|
||||
const inventory_value = stock_quantity * cost_price;
|
||||
const total_revenue = sanitizeValue(salesData.total_revenue) || 0;
|
||||
const total_cost = sanitizeValue(salesData.total_cost) || 0;
|
||||
const gross_profit = total_revenue - total_cost;
|
||||
const avg_margin_percent = total_revenue > 0 ? ((gross_profit / total_revenue) * 100) : 0;
|
||||
const gmroi = inventory_value > 0 ? (gross_profit / inventory_value) : 0;
|
||||
|
||||
// Add to batch update with sanitized values
|
||||
metricsUpdates.push([
|
||||
product.product_id,
|
||||
daily_sales_avg || null,
|
||||
weekly_sales_avg || null,
|
||||
monthly_sales_avg || null,
|
||||
metrics.avg_quantity_per_order || null,
|
||||
metrics.number_of_orders || 0,
|
||||
metrics.first_sale_date || null,
|
||||
metrics.last_sale_date || null,
|
||||
days_of_inventory,
|
||||
weeks_of_inventory,
|
||||
daily_sales_avg > 0 ? Math.max(1, Math.ceil(daily_sales_avg * config.reorder_days)) : null,
|
||||
margin_percent,
|
||||
metrics.total_revenue || 0,
|
||||
inventory_value || 0,
|
||||
purchases.avg_lead_time_days || null,
|
||||
purchases.last_purchase_date || null,
|
||||
purchases.first_received_date || null,
|
||||
purchases.last_received_date || null,
|
||||
product.pid,
|
||||
sanitizeValue(daily_sales_avg),
|
||||
sanitizeValue(weekly_sales_avg),
|
||||
sanitizeValue(monthly_sales_avg),
|
||||
sanitizeValue(salesData.avg_quantity_per_order),
|
||||
sanitizeValue(salesData.number_of_orders),
|
||||
salesData.first_sale_date || null,
|
||||
salesData.last_sale_date || null,
|
||||
sanitizeValue(days_of_inventory),
|
||||
sanitizeValue(weeks_of_inventory),
|
||||
sanitizeValue(reorder_quantities.reorder_point),
|
||||
sanitizeValue(reorder_quantities.safety_stock),
|
||||
sanitizeValue(reorder_quantities.reorder_qty),
|
||||
sanitizeValue(reorder_quantities.overstocked_amt),
|
||||
sanitizeValue(avg_margin_percent),
|
||||
sanitizeValue(total_revenue),
|
||||
sanitizeValue(inventory_value),
|
||||
sanitizeValue(total_cost),
|
||||
sanitizeValue(gross_profit),
|
||||
sanitizeValue(gmroi),
|
||||
sanitizeValue(purchaseData.avg_lead_time_days),
|
||||
purchaseData.last_purchase_date || null,
|
||||
purchaseData.first_received_date || null,
|
||||
purchaseData.last_received_date || null,
|
||||
null, // abc_class - calculated separately
|
||||
stock_status,
|
||||
reorder_qty,
|
||||
overstocked_amt
|
||||
sanitizeValue(0), // turnover_rate - calculated separately
|
||||
sanitizeValue(purchaseData.avg_lead_time_days),
|
||||
sanitizeValue(config.target_days),
|
||||
stock_status === 'Critical' ? 'Warning' : 'Normal',
|
||||
null, // forecast_accuracy
|
||||
null, // forecast_bias
|
||||
null // last_forecast_date
|
||||
]);
|
||||
} catch (err) {
|
||||
logError(err, `Failed processing product ${product.product_id}`);
|
||||
logError(err, `Failed processing product ${product.pid}`);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Batch update metrics
|
||||
if (metricsUpdates.length > 0) {
|
||||
await connection.query(`
|
||||
INSERT INTO product_metrics (
|
||||
product_id,
|
||||
daily_sales_avg,
|
||||
weekly_sales_avg,
|
||||
monthly_sales_avg,
|
||||
avg_quantity_per_order,
|
||||
number_of_orders,
|
||||
first_sale_date,
|
||||
last_sale_date,
|
||||
days_of_inventory,
|
||||
weeks_of_inventory,
|
||||
reorder_point,
|
||||
avg_margin_percent,
|
||||
total_revenue,
|
||||
inventory_value,
|
||||
avg_lead_time_days,
|
||||
last_purchase_date,
|
||||
first_received_date,
|
||||
last_received_date,
|
||||
stock_status,
|
||||
reorder_qty,
|
||||
overstocked_amt
|
||||
) VALUES ?
|
||||
ON DUPLICATE KEY UPDATE
|
||||
last_calculated_at = NOW(),
|
||||
daily_sales_avg = VALUES(daily_sales_avg),
|
||||
weekly_sales_avg = VALUES(weekly_sales_avg),
|
||||
monthly_sales_avg = VALUES(monthly_sales_avg),
|
||||
avg_quantity_per_order = VALUES(avg_quantity_per_order),
|
||||
number_of_orders = VALUES(number_of_orders),
|
||||
first_sale_date = VALUES(first_sale_date),
|
||||
last_sale_date = VALUES(last_sale_date),
|
||||
days_of_inventory = VALUES(days_of_inventory),
|
||||
weeks_of_inventory = VALUES(weeks_of_inventory),
|
||||
reorder_point = VALUES(reorder_point),
|
||||
avg_margin_percent = VALUES(avg_margin_percent),
|
||||
total_revenue = VALUES(total_revenue),
|
||||
inventory_value = VALUES(inventory_value),
|
||||
avg_lead_time_days = VALUES(avg_lead_time_days),
|
||||
last_purchase_date = VALUES(last_purchase_date),
|
||||
first_received_date = VALUES(first_received_date),
|
||||
last_received_date = VALUES(last_received_date),
|
||||
stock_status = VALUES(stock_status),
|
||||
reorder_qty = VALUES(reorder_qty),
|
||||
overstocked_amt = VALUES(overstocked_amt)
|
||||
`, [metricsUpdates]);
|
||||
try {
|
||||
await connection.query(`
|
||||
INSERT INTO product_metrics (
|
||||
pid,
|
||||
daily_sales_avg,
|
||||
weekly_sales_avg,
|
||||
monthly_sales_avg,
|
||||
avg_quantity_per_order,
|
||||
number_of_orders,
|
||||
first_sale_date,
|
||||
last_sale_date,
|
||||
days_of_inventory,
|
||||
weeks_of_inventory,
|
||||
reorder_point,
|
||||
safety_stock,
|
||||
reorder_qty,
|
||||
overstocked_amt,
|
||||
avg_margin_percent,
|
||||
total_revenue,
|
||||
inventory_value,
|
||||
cost_of_goods_sold,
|
||||
gross_profit,
|
||||
gmroi,
|
||||
avg_lead_time_days,
|
||||
last_purchase_date,
|
||||
first_received_date,
|
||||
last_received_date,
|
||||
abc_class,
|
||||
stock_status,
|
||||
turnover_rate,
|
||||
current_lead_time,
|
||||
target_lead_time,
|
||||
lead_time_status,
|
||||
forecast_accuracy,
|
||||
forecast_bias,
|
||||
last_forecast_date
|
||||
)
|
||||
VALUES ?
|
||||
ON DUPLICATE KEY UPDATE
|
||||
daily_sales_avg = VALUES(daily_sales_avg),
|
||||
weekly_sales_avg = VALUES(weekly_sales_avg),
|
||||
monthly_sales_avg = VALUES(monthly_sales_avg),
|
||||
avg_quantity_per_order = VALUES(avg_quantity_per_order),
|
||||
number_of_orders = VALUES(number_of_orders),
|
||||
first_sale_date = VALUES(first_sale_date),
|
||||
last_sale_date = VALUES(last_sale_date),
|
||||
days_of_inventory = VALUES(days_of_inventory),
|
||||
weeks_of_inventory = VALUES(weeks_of_inventory),
|
||||
reorder_point = VALUES(reorder_point),
|
||||
safety_stock = VALUES(safety_stock),
|
||||
reorder_qty = VALUES(reorder_qty),
|
||||
overstocked_amt = VALUES(overstocked_amt),
|
||||
avg_margin_percent = VALUES(avg_margin_percent),
|
||||
total_revenue = VALUES(total_revenue),
|
||||
inventory_value = VALUES(inventory_value),
|
||||
cost_of_goods_sold = VALUES(cost_of_goods_sold),
|
||||
gross_profit = VALUES(gross_profit),
|
||||
gmroi = VALUES(gmroi),
|
||||
avg_lead_time_days = VALUES(avg_lead_time_days),
|
||||
last_purchase_date = VALUES(last_purchase_date),
|
||||
first_received_date = VALUES(first_received_date),
|
||||
last_received_date = VALUES(last_received_date),
|
||||
stock_status = VALUES(stock_status),
|
||||
turnover_rate = VALUES(turnover_rate),
|
||||
current_lead_time = VALUES(current_lead_time),
|
||||
target_lead_time = VALUES(target_lead_time),
|
||||
lead_time_status = VALUES(lead_time_status),
|
||||
last_calculated_at = CURRENT_TIMESTAMP
|
||||
`, [metricsUpdates]);
|
||||
} catch (err) {
|
||||
logError(err, 'Failed to update metrics batch');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return processedCount;
|
||||
} finally {
|
||||
connection.release();
|
||||
if (connection) {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function calculateStockStatus(stock, config, daily_sales_avg, weekly_sales_avg, monthly_sales_avg) {
|
||||
const days_since_first_stock = stock.days_since_first_stock || 0;
|
||||
const days_since_last_sale = stock.days_since_last_sale || 9999;
|
||||
const total_quantity_sold = stock.total_quantity_sold || 0;
|
||||
const had_recent_stockout = stock.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';
|
||||
if (stock <= 0) {
|
||||
return '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';
|
||||
// Use the most appropriate sales average based on data quality
|
||||
let sales_avg = daily_sales_avg;
|
||||
if (sales_avg === 0) {
|
||||
sales_avg = weekly_sales_avg / 7;
|
||||
}
|
||||
if (sales_avg === 0) {
|
||||
sales_avg = monthly_sales_avg / 30;
|
||||
}
|
||||
|
||||
// 2. Handle zero or very low sales velocity cases
|
||||
if (ds === 0 || (ds < 0.1 && ws < 0.5)) {
|
||||
if (days_since_first_stock > config.overstock_days) {
|
||||
return 'Overstocked';
|
||||
}
|
||||
if (days_since_first_stock > 30) {
|
||||
return 'At Risk';
|
||||
}
|
||||
if (sales_avg === 0) {
|
||||
return stock <= config.low_stock_threshold ? 'Low Stock' : 'In Stock';
|
||||
}
|
||||
|
||||
// 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;
|
||||
const days_of_stock = stock / sales_avg;
|
||||
|
||||
// Critical stock level
|
||||
if (days_of_supply <= config.critical_days) {
|
||||
if (days_of_stock <= 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)) {
|
||||
} else if (days_of_stock <= config.reorder_days) {
|
||||
return 'Reorder';
|
||||
}
|
||||
|
||||
// At Risk cases
|
||||
if (
|
||||
(days_of_supply >= config.overstock_days * 0.8) ||
|
||||
(velocity_trend <= -50 && days_of_supply > config.reorder_days * 2) ||
|
||||
(days_since_last_sale > 45 && dq > 0) ||
|
||||
(ds > 0 && ds < 0.2 && dq > ds * config.overstock_days * 0.5)
|
||||
) {
|
||||
return 'At Risk';
|
||||
}
|
||||
|
||||
// Overstock cases
|
||||
if (days_of_supply >= config.overstock_days) {
|
||||
} else if (days_of_stock > config.overstock_days) {
|
||||
return 'Overstocked';
|
||||
}
|
||||
|
||||
// If none of the above conditions are met
|
||||
return 'Healthy';
|
||||
}
|
||||
|
||||
function calculateReorderQuantities(stock, stock_status, daily_sales_avg, avg_lead_time, config) {
|
||||
// Calculate safety stock based on service level and lead time
|
||||
const z_score = 1.96; // 95% service level
|
||||
const lead_time = avg_lead_time || config.target_days;
|
||||
const safety_stock = Math.ceil(daily_sales_avg * Math.sqrt(lead_time) * z_score);
|
||||
|
||||
// Calculate reorder point
|
||||
const lead_time_demand = daily_sales_avg * lead_time;
|
||||
const reorder_point = Math.ceil(lead_time_demand + safety_stock);
|
||||
|
||||
// Calculate reorder quantity using EOQ formula if we have the necessary data
|
||||
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 = avg_lead_time || 14;
|
||||
const sc = config.safety_stock_days || 14;
|
||||
const ss = config.safety_stock_days || 14;
|
||||
const dq = stock.stock_quantity || 0;
|
||||
const moq = stock.moq || 1;
|
||||
|
||||
// Calculate desired stock level
|
||||
const desired_stock = (ds * (lt + sc)) + ss;
|
||||
if (daily_sales_avg > 0) {
|
||||
const annual_demand = daily_sales_avg * 365;
|
||||
const order_cost = 25; // Fixed cost per order
|
||||
const holding_cost_percent = 0.25; // 25% annual holding cost
|
||||
|
||||
// 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;
|
||||
reorder_qty = Math.ceil(Math.sqrt((2 * annual_demand * order_cost) / holding_cost_percent));
|
||||
} else {
|
||||
// If no sales data, use a basic calculation
|
||||
reorder_qty = Math.max(safety_stock, config.low_stock_threshold);
|
||||
}
|
||||
|
||||
// Calculate overstocked amount for overstocked products
|
||||
if (stock_status === 'Overstocked') {
|
||||
const ds = daily_sales_avg || 0;
|
||||
const dq = stock.stock_quantity || 0;
|
||||
const lt = avg_lead_time || 14;
|
||||
const sc = config.safety_stock_days || 14;
|
||||
const ss = config.safety_stock_days || 14;
|
||||
// Calculate overstocked amount
|
||||
const overstocked_amt = stock_status === 'Overstocked' ?
|
||||
stock - Math.ceil(daily_sales_avg * config.overstock_days) :
|
||||
0;
|
||||
|
||||
// Calculate maximum desired stock
|
||||
const max_desired_stock = (ds * config.overstock_days) + ss;
|
||||
|
||||
// Calculate excess inventory
|
||||
overstocked_amt = Math.max(0, dq - max_desired_stock);
|
||||
}
|
||||
|
||||
return { reorder_qty, overstocked_amt };
|
||||
return {
|
||||
safety_stock,
|
||||
reorder_point,
|
||||
reorder_qty,
|
||||
overstocked_amt
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = calculateProductMetrics;
|
||||
Reference in New Issue
Block a user