Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
updated shipment arrived trigger to handle free stock
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalnn committed May 13, 2024
1 parent d57e9ef commit cc1f901
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions migrations/012_shipments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,50 @@ CREATE TABLE IF NOT EXISTS shipments (

CREATE FUNCTION shipment_arrived() RETURNS TRIGGER AS $$
DECLARE item_price money;
DECLARE item_ids uuid[];
DECLARE item_id uuid;
DECLARE new_item_id uuid;
DECLARE p_kind char(2);
DECLARE n_missing_items int;
BEGIN

SELECT unit_price INTO item_price
SELECT unit_price, CAST(raw_material_kind AS char(2))
INTO item_price, p_kind
FROM suppliers
JOIN shipments AS sh ON sh.supplier_id = suppliers.id
WHERE sh.id = NEW.id;

UPDATE items
SET status = 'in_stock',
SELECT ARRAY_AGG(items.id) INTO item_ids
FROM items
JOIN raw_material_shipments AS rs
ON rs.raw_material_id = items.id
JOIN shipments AS s
ON rs.shipment_id = s.id
WHERE s.id = NEW.id;

FOREACH item_id IN ARRAY item_ids
LOOP
RAISE NOTICE 'Item % arrived', item_id;
UPDATE items
SET status = 'in_stock',
warehouse = 'W1',
acc_cost = item_price
WHERE id IN
(
SELECT items.id
FROM items
JOIN raw_material_shipments AS rs
ON rs.raw_material_id = items.id
JOIN shipments AS s
ON rs.shipment_id = s.id
WHERE s.id = NEW.id
);
WHERE id = item_id;
END LOOP;

SELECT NEW.quantity - COUNT(item_ids) INTO n_missing_items;
IF n_missing_items > 0 THEN
FOR i IN 1..n_missing_items
LOOP
INSERT INTO items (piece_kind, status, warehouse, acc_cost)
VALUES (CAST(p_kind AS piece_kind), 'in_stock', 'W1', item_price)
RETURNING id INTO new_item_id;
END LOOP;

RAISE NOTICE '% free items added of type %', n_missing_items, p_kind;
END IF;

RAISE NOTICE 'Shipment % arrived', NEW.id;
RETURN NEW;
END;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER shipment_arrived_trigger
Expand Down

0 comments on commit cc1f901

Please sign in to comment.