Update calculate script to account for import changes

This commit is contained in:
2025-01-27 10:41:18 -05:00
parent 44d9ae2aad
commit 5781b45f37
10 changed files with 508 additions and 297 deletions

View File

@@ -5,6 +5,9 @@ process.chdir(path.dirname(__filename));
require('dotenv').config({ path: path.resolve(__dirname, '..', '.env') });
// Set to 1 to skip product metrics and only calculate the remaining metrics
const SKIP_PRODUCT_METRICS = 0;
// Add error handler for uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
@@ -43,9 +46,6 @@ const calculateCategoryMetrics = require('./metrics/category-metrics');
const calculateBrandMetrics = require('./metrics/brand-metrics');
const calculateSalesForecasts = require('./metrics/sales-forecasts');
// Set to 1 to skip product metrics and only calculate the remaining metrics
const SKIP_PRODUCT_METRICS = 1;
// Add cancel handler
let isCancelled = false;

View File

@@ -31,12 +31,24 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount) {
WITH brand_data AS (
SELECT
p.brand,
COUNT(DISTINCT p.product_id) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.product_id END) as active_products,
SUM(p.stock_quantity) as total_stock_units,
SUM(p.stock_quantity * p.cost_price) as total_stock_cost,
SUM(p.stock_quantity * p.price) as total_stock_retail,
SUM(o.price * o.quantity) as total_revenue,
COUNT(DISTINCT CASE WHEN p.stock_quantity <= 5000 THEN p.pid END) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true AND p.stock_quantity <= 5000 THEN p.pid END) as active_products,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 THEN 0
ELSE p.stock_quantity
END) as total_stock_units,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 OR p.cost_price IS NULL OR p.cost_price < 0 THEN 0
ELSE p.stock_quantity * p.cost_price
END) as total_stock_cost,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 OR p.price IS NULL OR p.price < 0 THEN 0
ELSE p.stock_quantity * p.price
END) as total_stock_retail,
SUM(CASE
WHEN o.quantity IS NULL OR o.price IS NULL OR o.quantity < 0 OR o.price < 0 THEN 0
ELSE o.price * o.quantity
END) as total_revenue,
CASE
WHEN SUM(o.price * o.quantity) > 0 THEN
(SUM((o.price - p.cost_price) * o.quantity) * 100.0) / SUM(o.price * o.quantity)
@@ -55,7 +67,7 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount) {
ELSE 0
END) as previous_year_period_sales
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
WHERE p.brand IS NOT NULL
GROUP BY p.brand
)
@@ -111,19 +123,31 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount) {
p.brand,
YEAR(o.date) as year,
MONTH(o.date) as month,
COUNT(DISTINCT p.product_id) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.product_id END) as active_products,
SUM(p.stock_quantity) as total_stock_units,
SUM(p.stock_quantity * p.cost_price) as total_stock_cost,
SUM(p.stock_quantity * p.price) as total_stock_retail,
SUM(o.price * o.quantity) as total_revenue,
COUNT(DISTINCT CASE WHEN p.stock_quantity <= 5000 THEN p.pid END) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true AND p.stock_quantity <= 5000 THEN p.pid END) as active_products,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 THEN 0
ELSE p.stock_quantity
END) as total_stock_units,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 OR p.cost_price IS NULL OR p.cost_price < 0 THEN 0
ELSE p.stock_quantity * p.cost_price
END) as total_stock_cost,
SUM(CASE
WHEN p.stock_quantity IS NULL OR p.stock_quantity < 0 OR p.stock_quantity > 5000 OR p.price IS NULL OR p.price < 0 THEN 0
ELSE p.stock_quantity * p.price
END) as total_stock_retail,
SUM(CASE
WHEN o.quantity IS NULL OR o.price IS NULL OR o.quantity < 0 OR o.price < 0 THEN 0
ELSE o.price * o.quantity
END) as total_revenue,
CASE
WHEN SUM(o.price * o.quantity) > 0 THEN
(SUM((o.price - p.cost_price) * o.quantity) * 100.0) / SUM(o.price * o.quantity)
ELSE 0
END as avg_margin
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
WHERE p.brand IS NOT NULL
AND o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)
GROUP BY p.brand, YEAR(o.date), MONTH(o.date)

View File

@@ -29,9 +29,9 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
)
WITH category_sales AS (
SELECT
c.id as category_id,
COUNT(DISTINCT p.product_id) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.product_id END) as active_products,
c.cat_id as category_id,
COUNT(DISTINCT p.pid) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.pid END) as active_products,
SUM(p.stock_quantity * p.cost_price) as total_value,
CASE
WHEN SUM(o.price * o.quantity) > 0
@@ -68,10 +68,10 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
END) as previous_year_period_sales,
c.status
FROM categories c
LEFT JOIN product_categories pc ON c.id = pc.category_id
LEFT JOIN products p ON pc.product_id = p.product_id
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
GROUP BY c.id, c.status
LEFT JOIN product_categories pc ON c.cat_id = pc.cat_id
LEFT JOIN products p ON pc.pid = p.pid
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
GROUP BY c.cat_id, c.status
)
SELECT
category_id,
@@ -120,11 +120,11 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
turnover_rate
)
SELECT
c.id as category_id,
c.cat_id as category_id,
YEAR(o.date) as year,
MONTH(o.date) as month,
COUNT(DISTINCT p.product_id) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.product_id END) as active_products,
COUNT(DISTINCT p.pid) as product_count,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.pid END) as active_products,
SUM(p.stock_quantity * p.cost_price) as total_value,
SUM(o.price * o.quantity) as total_revenue,
CASE
@@ -138,11 +138,11 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
ELSE 0
END as turnover_rate
FROM categories c
LEFT JOIN product_categories pc ON c.id = pc.category_id
LEFT JOIN products p ON pc.product_id = p.product_id
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
LEFT JOIN product_categories pc ON c.cat_id = pc.cat_id
LEFT JOIN products p ON pc.pid = p.pid
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
WHERE o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)
GROUP BY c.id, YEAR(o.date), MONTH(o.date)
GROUP BY c.cat_id, YEAR(o.date), MONTH(o.date)
ON DUPLICATE KEY UPDATE
product_count = VALUES(product_count),
active_products = VALUES(active_products),
@@ -152,7 +152,7 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
turnover_rate = VALUES(turnover_rate)
`);
// Calculate category sales metrics
// Calculate category sales metrics with NULL brand handling
await connection.query(`
INSERT INTO category_sales_metrics (
category_id,
@@ -184,22 +184,22 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
),
category_metrics AS (
SELECT
c.id as category_id,
p.brand,
c.cat_id as category_id,
COALESCE(p.brand, 'Unbranded') as brand,
dr.period_start,
dr.period_end,
COUNT(DISTINCT p.product_id) as num_products,
COUNT(DISTINCT p.pid) as num_products,
COALESCE(SUM(o.quantity), 0) / DATEDIFF(dr.period_end, dr.period_start) as avg_daily_sales,
COALESCE(SUM(o.quantity), 0) as total_sold,
COALESCE(AVG(o.price), 0) as avg_price
FROM categories c
JOIN product_categories pc ON c.id = pc.category_id
JOIN products p ON pc.product_id = p.product_id
JOIN product_categories pc ON c.cat_id = pc.cat_id
JOIN products p ON pc.pid = p.pid
CROSS JOIN date_ranges dr
LEFT JOIN orders o ON p.product_id = o.product_id
LEFT JOIN orders o ON p.pid = o.pid
AND o.date BETWEEN dr.period_start AND dr.period_end
AND o.canceled = false
GROUP BY c.id, p.brand, dr.period_start, dr.period_end
GROUP BY c.cat_id, COALESCE(p.brand, 'Unbranded'), dr.period_start, dr.period_end
)
SELECT
category_id,

View File

@@ -19,7 +19,7 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
UPDATE product_metrics pm
JOIN (
SELECT
p.product_id,
p.pid,
p.cost_price * p.stock_quantity as inventory_value,
SUM(o.quantity * o.price) as total_revenue,
SUM(o.quantity * p.cost_price) as cost_of_goods_sold,
@@ -28,11 +28,11 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
MAX(o.date) as last_sale_date,
DATEDIFF(MAX(o.date), MIN(o.date)) + 1 as calculation_period_days
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id
LEFT JOIN orders o ON p.pid = o.pid
WHERE o.canceled = false
AND DATE(o.date) >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY p.product_id
) fin ON pm.product_id = fin.product_id
GROUP BY p.pid
) fin ON pm.pid = fin.pid
SET
pm.inventory_value = COALESCE(fin.inventory_value, 0),
pm.total_revenue = COALESCE(fin.total_revenue, 0),
@@ -50,17 +50,17 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
UPDATE product_time_aggregates pta
JOIN (
SELECT
p.product_id,
p.pid,
YEAR(o.date) as year,
MONTH(o.date) as month,
p.cost_price * p.stock_quantity as inventory_value,
SUM(o.quantity * (o.price - p.cost_price)) as gross_profit,
COUNT(DISTINCT DATE(o.date)) as days_in_period
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id
LEFT JOIN orders o ON p.pid = o.pid
WHERE o.canceled = false
GROUP BY p.product_id, YEAR(o.date), MONTH(o.date)
) fin ON pta.product_id = fin.product_id
GROUP BY p.pid, YEAR(o.date), MONTH(o.date)
) fin ON pta.pid = fin.pid
AND pta.year = fin.year
AND pta.month = fin.month
SET

View File

@@ -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;

View File

@@ -18,7 +18,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount)
// Calculate product-level forecasts
await connection.query(`
INSERT INTO sales_forecasts (
product_id,
pid,
forecast_date,
forecast_units,
forecast_revenue,
@@ -27,14 +27,14 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount)
)
WITH daily_sales AS (
SELECT
o.product_id,
o.pid,
DATE(o.date) as sale_date,
SUM(o.quantity) as daily_quantity,
SUM(o.price * o.quantity) as daily_revenue
FROM orders o
WHERE o.canceled = false
AND o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 90 DAY)
GROUP BY o.product_id, DATE(o.date)
GROUP BY o.pid, DATE(o.date)
),
forecast_dates AS (
SELECT
@@ -51,7 +51,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount)
),
product_stats AS (
SELECT
ds.product_id,
ds.pid,
AVG(ds.daily_quantity) as avg_daily_quantity,
STDDEV_SAMP(ds.daily_quantity) as std_daily_quantity,
AVG(ds.daily_revenue) as avg_daily_revenue,
@@ -66,10 +66,10 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount)
AVG(CASE WHEN DAYOFWEEK(ds.sale_date) = 6 THEN ds.daily_revenue END) as friday_avg,
AVG(CASE WHEN DAYOFWEEK(ds.sale_date) = 7 THEN ds.daily_revenue END) as saturday_avg
FROM daily_sales ds
GROUP BY ds.product_id
GROUP BY ds.pid
)
SELECT
ps.product_id,
ps.pid,
fd.forecast_date,
GREATEST(0,
ps.avg_daily_quantity *
@@ -130,15 +130,15 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount)
)
WITH category_daily_sales AS (
SELECT
pc.category_id,
pc.cat_id as category_id,
DATE(o.date) as sale_date,
SUM(o.quantity) as daily_quantity,
SUM(o.price * o.quantity) as daily_revenue
FROM orders o
JOIN product_categories pc ON o.product_id = pc.product_id
JOIN product_categories pc ON o.pid = pc.pid
WHERE o.canceled = false
AND o.date >= DATE_SUB(CURRENT_DATE, INTERVAL 90 DAY)
GROUP BY pc.category_id, DATE(o.date)
GROUP BY pc.cat_id, DATE(o.date)
),
forecast_dates AS (
SELECT

View File

@@ -6,7 +6,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
// Initial insert of time-based aggregates
await connection.query(`
INSERT INTO product_time_aggregates (
product_id,
pid,
year,
month,
total_quantity_sold,
@@ -20,7 +20,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
)
WITH sales_data AS (
SELECT
o.product_id,
o.pid,
YEAR(o.date) as year,
MONTH(o.date) as month,
SUM(o.quantity) as total_quantity_sold,
@@ -35,23 +35,23 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
SUM((o.price - COALESCE(o.discount, 0)) * o.quantity)) * 100
END as profit_margin
FROM orders o
JOIN products p ON o.product_id = p.product_id
JOIN products p ON o.pid = p.pid
WHERE o.canceled = 0
GROUP BY o.product_id, YEAR(o.date), MONTH(o.date)
GROUP BY o.pid, YEAR(o.date), MONTH(o.date)
),
purchase_data AS (
SELECT
product_id,
pid,
YEAR(date) as year,
MONTH(date) as month,
SUM(received) as stock_received,
SUM(ordered) as stock_ordered
FROM purchase_orders
WHERE status = 'closed'
GROUP BY product_id, YEAR(date), MONTH(date)
WHERE receiving_status >= 30 -- Partial or fully received
GROUP BY pid, YEAR(date), MONTH(date)
)
SELECT
s.product_id,
s.pid,
s.year,
s.month,
s.total_quantity_sold,
@@ -64,12 +64,12 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
s.profit_margin
FROM sales_data s
LEFT JOIN purchase_data p
ON s.product_id = p.product_id
ON s.pid = p.pid
AND s.year = p.year
AND s.month = p.month
UNION
SELECT
p.product_id,
p.pid,
p.year,
p.month,
0 as total_quantity_sold,
@@ -82,10 +82,10 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
0 as profit_margin
FROM purchase_data p
LEFT JOIN sales_data s
ON p.product_id = s.product_id
ON p.pid = s.pid
AND p.year = s.year
AND p.month = s.month
WHERE s.product_id IS NULL
WHERE s.pid IS NULL
ON DUPLICATE KEY UPDATE
total_quantity_sold = VALUES(total_quantity_sold),
total_revenue = VALUES(total_revenue),
@@ -102,17 +102,17 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
UPDATE product_time_aggregates pta
JOIN (
SELECT
p.product_id,
p.pid,
YEAR(o.date) as year,
MONTH(o.date) as month,
p.cost_price * p.stock_quantity as inventory_value,
SUM(o.quantity * (o.price - p.cost_price)) as gross_profit,
COUNT(DISTINCT DATE(o.date)) as days_in_period
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id
LEFT JOIN orders o ON p.pid = o.pid
WHERE o.canceled = false
GROUP BY p.product_id, YEAR(o.date), MONTH(o.date)
) fin ON pta.product_id = fin.product_id
GROUP BY p.pid, YEAR(o.date), MONTH(o.date)
) fin ON pta.pid = fin.pid
AND pta.year = fin.year
AND pta.month = fin.month
SET

View File

@@ -55,14 +55,14 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount)
ELSE 0
END as order_fill_rate
FROM purchase_orders po
WHERE po.status = 'closed'
WHERE po.receiving_status >= 30 -- Partial or fully received
GROUP BY po.vendor
),
vendor_products AS (
SELECT
p.vendor,
COUNT(DISTINCT p.product_id) as total_products,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.product_id END) as active_products,
COUNT(DISTINCT p.pid) as total_products,
COUNT(DISTINCT CASE WHEN p.visible = true THEN p.pid END) as active_products,
SUM(o.price * o.quantity) as total_revenue,
CASE
WHEN SUM(o.price * o.quantity) > 0 THEN
@@ -70,7 +70,7 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount)
ELSE 0
END as avg_margin_percent
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
GROUP BY p.vendor
)
SELECT
@@ -140,8 +140,8 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount)
END as avg_margin_percent
FROM vendor_details vd
LEFT JOIN products p ON vd.vendor = p.vendor
LEFT JOIN purchase_orders po ON p.product_id = po.product_id
LEFT JOIN orders o ON p.product_id = o.product_id AND o.canceled = false
LEFT JOIN purchase_orders po ON p.pid = po.pid
LEFT JOIN orders o ON p.pid = o.pid AND o.canceled = false
WHERE po.date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)
GROUP BY vd.vendor, YEAR(po.date), MONTH(po.date)
)