const { Pool } = require('pg'); async function main() { const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const cycle = '202607'; // If we rebuilt cost using COALESCE(po.actual, po.purchase, insp.purchase) what margins look like const sim = await pool.query( `WITH latest_insp AS ( SELECT DISTINCT ON (i.order_code, i.sku_code) i.order_code, i.sku_code, i.actual_qty, i.purchase_price AS insp_price FROM inspections i JOIN orders o ON o.order_code = i.order_code AND o.cycle_code = $1 WHERE COALESCE(i.actual_qty,0) > 0 ORDER BY i.order_code, i.sku_code, i.inspection_time DESC NULLS LAST, i.created_at DESC, i.id DESC ), priced AS ( SELECT li.order_code, li.sku_code, li.actual_qty, li.insp_price, COALESCE(po.actual_price, po.purchase_price) AS po_price, oi.price AS sell_price, COALESCE( NULLIF(COALESCE(po.actual_price, po.purchase_price), 0), NULLIF(li.insp_price, 0) ) AS better_price FROM latest_insp li LEFT JOIN purchase_orders po ON po.order_code = li.order_code AND po.sku_code = li.sku_code LEFT JOIN LATERAL ( SELECT price FROM order_items oi WHERE oi.order_code = li.order_code AND oi.sku_code = li.sku_code LIMIT 1 ) oi ON TRUE ), sku_cost AS ( SELECT sku_code, ROUND(SUM(actual_qty * insp_price)::numeric,2) AS cost_insp, ROUND(SUM(actual_qty * better_price)::numeric,2) AS cost_better, COUNT(*) FILTER ( WHERE po_price IS NOT NULL AND po_price IS DISTINCT FROM insp_price )::int AS po_diff_lines FROM priced WHERE COALESCE(better_price,0) > 0 GROUP BY sku_code ), sales AS ( SELECT oi.sku_code, ROUND(SUM(CASE WHEN COALESCE(oi.amount,0)>0 THEN oi.amount ELSE oi.price*oi.quantity END)::numeric,2) AS sales FROM order_items oi JOIN orders o ON o.order_code = oi.order_code WHERE o.cycle_code = $1 AND o.order_status >= 20 GROUP BY oi.sku_code ) SELECT COUNT(*) FILTER (WHERE s.sales > 0 AND ABS(s.sales - c.cost_insp) < 0.011)::int AS zero_with_insp, COUNT(*) FILTER (WHERE s.sales > 0 AND ABS(s.sales - c.cost_better) < 0.011)::int AS zero_with_better, COUNT(*) FILTER (WHERE s.sales > 0 AND (s.sales - c.cost_insp) / s.sales * 100 > 1)::int AS gt1pct_insp, COUNT(*) FILTER (WHERE s.sales > 0 AND (s.sales - c.cost_better) / s.sales * 100 > 1)::int AS gt1pct_better, ROUND(SUM(c.cost_insp)::numeric,2) AS sum_insp, ROUND(SUM(c.cost_better)::numeric,2) AS sum_better, SUM(c.po_diff_lines)::int AS po_diff_lines FROM sku_cost c JOIN sales s ON s.sku_code = c.sku_code`, [cycle] ); console.log('rebuild simulation', sim.rows[0]); // supplier prep: is supply price intentionally = sell? const prep = await pool.query( `SELECT COUNT(*)::int AS n, COUNT(*) FILTER (WHERE sp.purchase_price IS NOT DISTINCT FROM oi.price)::int AS prep_eq_sell FROM supplier_preparations sp JOIN orders o ON o.order_code = sp.order_code AND o.cycle_code = $1 JOIN LATERAL ( SELECT price FROM order_items oi WHERE oi.order_code = sp.order_code AND oi.sku_code = sp.sku_code LIMIT 1 ) oi ON TRUE WHERE COALESCE(sp.purchase_price,0) > 0`, [cycle] ); console.log('prep vs sell', prep.rows[0]); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });