Fixed qu_id_stock change handling when needed conversion is only defined globally (fixes #1974)

This commit is contained in:
Bernd Bestel 2022-08-22 19:53:58 +02:00
parent 06968ac289
commit dfe9868a48
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 57 additions and 0 deletions

View File

@ -14,6 +14,7 @@
- Fixed that consuming a parent product which is not in stock itself (so essentially using any of the child products) may failed when unit conversions were involved (the current stock amount check was wrong in that case)
- Fixed that the status button counters on the stock overview page ("X products are overdue" and so on) included products which have the option `Never show on stock overview` enabled
- Fixed that adding Userfields to existing stock entries was not possible (only editing existing Userfield values, e.g. added during purchase or inventory, was possible)
- Fixed that it was not possible to change a products stock QU, when the needed unit conversion (old QU => new QU) was only defined globally (means on QU level)
### Shopping list

56
migrations/0197.sql Normal file
View File

@ -0,0 +1,56 @@
DROP TRIGGER cascade_change_qu_id_stock;
CREATE TRIGGER cascade_change_qu_id_stock BEFORE UPDATE ON products WHEN NEW.qu_id_stock != OLD.qu_id_stock
BEGIN
-- All amounts anywhere are related to the products stock QU,
-- so apply the appropriate unit conversion to all amounts everywhere on change
-- (and enforce that such a conversion need to exist when the product was once added to stock)
SELECT CASE WHEN((
SELECT 1
FROM quantity_unit_conversions_resolved
WHERE product_id = NEW.id
AND from_qu_id = OLD.qu_id_stock
AND to_qu_id = NEW.qu_id_stock
AND source NOT LIKE '1%'
) ISNULL)
AND
((
SELECT 1
FROM stock_log
WHERE product_id = NEW.id
AND NEW.qu_id_stock != OLD.qu_id_stock
) NOTNULL) THEN RAISE(ABORT, "qu_id_stock can only be changed when a corresponding QU conversion (old QU => new QU) exists when the product was once added to stock") END;
UPDATE chores
SET product_amount = product_amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id;
UPDATE meal_plan
SET product_amount = product_amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE type = 'product'
AND product_id = NEW.id;
UPDATE product_barcodes
SET amount = amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id
AND amount IS NOT NULL;
UPDATE recipes_pos
SET amount = amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id;
UPDATE shopping_list
SET amount = amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id
AND product_id IS NOT NULL;
UPDATE stock
SET amount = amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0),
price = price / IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id;
UPDATE stock_log
SET amount = amount * IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0),
price = price / IFNULL((SELECT factor FROM quantity_unit_conversions_resolved WHERE product_id = NEW.id AND from_qu_id = OLD.qu_id_stock AND to_qu_id = NEW.qu_id_stock AND source NOT LIKE '1%'), 1.0)
WHERE product_id = NEW.id;
END;