Import/calculations improvements
This commit is contained in:
@@ -60,26 +60,31 @@ BEGIN
|
||||
GROUP BY p.vendor
|
||||
),
|
||||
VendorPOAggregates AS (
|
||||
-- Aggregate PO related stats including lead time calculated from POs to receivings
|
||||
-- Lead time per PO line = days to its FIRST receiving from the same supplier
|
||||
-- (within 180 days), then averaged per vendor. Joining each PO line to EVERY
|
||||
-- later receiving overstated lead time and weighted it toward busy products.
|
||||
-- Same shape as the per-product calc in update_periodic_metrics.sql.
|
||||
SELECT
|
||||
po.vendor,
|
||||
COUNT(DISTINCT po.po_id) AS po_count_365d,
|
||||
-- Calculate lead time by averaging the days between PO date and receiving date
|
||||
AVG(GREATEST(1, CASE
|
||||
WHEN r.received_date IS NOT NULL AND po.date IS NOT NULL
|
||||
THEN (r.received_date::date - po.date::date)
|
||||
ELSE NULL
|
||||
END))::int AS avg_lead_time_days_hist -- Avg lead time from HISTORICAL received POs
|
||||
FROM public.purchase_orders po
|
||||
-- Join to receivings table to find when items were received
|
||||
LEFT JOIN public.receivings r ON r.pid = po.pid AND r.supplier_id = po.supplier_id
|
||||
WHERE po.vendor IS NOT NULL AND po.vendor <> ''
|
||||
AND po.date >= CURRENT_DATE - INTERVAL '1 year' -- Look at POs created in the last year
|
||||
AND po.status = 'done' -- Only calculate lead time on completed POs
|
||||
AND r.received_date IS NOT NULL
|
||||
AND po.date IS NOT NULL
|
||||
AND r.received_date >= po.date
|
||||
GROUP BY po.vendor
|
||||
vendor,
|
||||
COUNT(DISTINCT po_id) AS po_count_365d,
|
||||
ROUND(AVG(GREATEST(1, first_receive_date - po_date)))::int AS avg_lead_time_days_hist
|
||||
FROM (
|
||||
SELECT
|
||||
po.vendor,
|
||||
po.po_id,
|
||||
po.pid,
|
||||
po.date::date AS po_date,
|
||||
MIN(r.received_date::date) AS first_receive_date
|
||||
FROM public.purchase_orders po
|
||||
JOIN public.receivings r ON r.pid = po.pid AND r.supplier_id = po.supplier_id
|
||||
AND r.received_date >= po.date
|
||||
AND r.received_date <= po.date + INTERVAL '180 days'
|
||||
WHERE po.status = 'done'
|
||||
AND po.date >= CURRENT_DATE - INTERVAL '1 year'
|
||||
AND po.vendor IS NOT NULL AND po.vendor <> ''
|
||||
GROUP BY po.vendor, po.po_id, po.pid, po.date
|
||||
) po_first_receiving
|
||||
GROUP BY vendor
|
||||
),
|
||||
AllVendors AS (
|
||||
-- Ensure all vendors from products table are included
|
||||
@@ -154,7 +159,11 @@ BEGIN
|
||||
vendor_metrics.on_order_units IS DISTINCT FROM EXCLUDED.on_order_units OR
|
||||
vendor_metrics.sales_30d IS DISTINCT FROM EXCLUDED.sales_30d OR
|
||||
vendor_metrics.revenue_30d IS DISTINCT FROM EXCLUDED.revenue_30d OR
|
||||
vendor_metrics.lifetime_sales IS DISTINCT FROM EXCLUDED.lifetime_sales;
|
||||
vendor_metrics.lifetime_sales IS DISTINCT FROM EXCLUDED.lifetime_sales OR
|
||||
-- Cost revisions can change profit/cogs with unchanged sales/revenue
|
||||
vendor_metrics.profit_30d IS DISTINCT FROM EXCLUDED.profit_30d OR
|
||||
vendor_metrics.cogs_30d IS DISTINCT FROM EXCLUDED.cogs_30d OR
|
||||
vendor_metrics.avg_lead_time_days IS DISTINCT FROM EXCLUDED.avg_lead_time_days;
|
||||
|
||||
-- Update calculate_status
|
||||
INSERT INTO public.calculate_status (module_name, last_calculation_timestamp)
|
||||
|
||||
Reference in New Issue
Block a user