jon/jon/db/entry/add_item_and_snack_entry.sql
2023-08-22 13:14:56 +02:00

27 lines
875 B
SQL

DO $$
DECLARE new_item_id integer;
DECLARE new_snack_id integer;
BEGIN
-- Create a new inventory line
INSERT INTO garfield.inventory_items
(item_barcode, name, item_group, location, tax_group, sales_units, unit_price)
VALUES
(%(barcode)s, %(name)s, %(group_id)s, %(location_id)s, %(tax_group_id)s, %(sales_units)s, %(net_unit_price)s)
RETURNING item_id INTO new_item_id;
-- Delete (i.e. mark as inactive) any old snacks with the same barcode in the given location
PERFORM garfield.snack_delete(snack_id)
FROM garfield.snacks
WHERE snack_barcode = %(barcode)s
AND location_id = %(location_id)s;
-- Create a new snack entry...
SELECT garfield.snack_create(%(name)s, %(barcode)s, %(gross_unit_price)s, %(tax_group_id)s, %(location_id)s)
INTO new_snack_id;
-- ... and map it to the new inventory line
PERFORM garfield.inventory_map_snack(new_snack_id, new_item_id);
END$$