CREATE OR REPLACE FUNCTION retire_batch_from_shelves(batch_ integer, location_ integer)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  taskowner integer := id from taskowners where "name" = 'stocker';
  sku integer := sku from itemsonshelf s join "batches" b on s.batch = b.id where b.id = batch_;
BEGIN
UPDATE
  itemsonshelf
SET
  quantity = 0
WHERE
  batch = batch_
  AND "location" = location_;
INSERT INTO
  tasks
  ("location", sku, taskowner, "description")
VALUES
  (location_, sku, taskowner, format('Retire batch %s', batch_));
END;
$$;
