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

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