-- ===========================================================================
-- Migration: link procurement PO lines to stock, so receiving moves stock
-- Date: 2026-07-29
--
-- A purchase-order line can optionally point at a real stock record — a feed
-- type (feed_stock, keyed by feed_type) or an inventory item (inventory_items,
-- by id). When goods are received (GRN) against such a line, that stock's
-- quantity goes up and its weighted-average unit cost is rolled forward using
-- the PO's line price; deleting the GRN reverses it. Lines with no stock link
-- (services, one-off buys) don't touch stock. Feed quantities are in kg.
-- ===========================================================================

ALTER TABLE purchase_order_items
  ADD COLUMN IF NOT EXISTS stock_type ENUM('feed','inventory') NULL AFTER unit,
  ADD COLUMN IF NOT EXISTS stock_ref  VARCHAR(120) NULL AFTER stock_type;
