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

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