-- v4.16: Add courier assignment to counter remission notes.

ALTER TABLE sales
    ADD COLUMN IF NOT EXISTS courier_id INTEGER;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'sales_courier_id_fkey'
    ) THEN
        ALTER TABLE sales
            ADD CONSTRAINT sales_courier_id_fkey
            FOREIGN KEY (courier_id) REFERENCES couriers(id);
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_sales_courier_id ON sales(courier_id);
