diff --git a/changelog/60_UNRELEASED_2020-xx-xx.md b/changelog/60_UNRELEASED_2020-xx-xx.md index a19f7a65..b70b733e 100644 --- a/changelog/60_UNRELEASED_2020-xx-xx.md +++ b/changelog/60_UNRELEASED_2020-xx-xx.md @@ -27,6 +27,7 @@ - Fixed that when editing a stock entry without a price, the price field was prefilled with `1` - Fixed that location & product groups filter on the stock overview page did a contains search instead an exact search - Fixed that the amount on the success popup was wrong when consuming a product with "Tare weight handling" enabled +- Fixed that the aggregated amount of parent products was wrong on the stock overview page when the child products had not the same stock quantity units ### Shopping list improvements diff --git a/migrations/0105.sql b/migrations/0105.sql index 7ab17f23..0bf60899 100644 --- a/migrations/0105.sql +++ b/migrations/0105.sql @@ -18,7 +18,8 @@ SELECT (SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name, (SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name_plural, sc.is_aggregated_amount, - sc.amount_opened_aggregated + sc.amount_opened_aggregated, + sc.amount_aggregated FROM ( SELECT * FROM stock_current @@ -51,7 +52,8 @@ SELECT (SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name, (SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name_plural, sc.is_aggregated_amount, - sc.amount_opened_aggregated + sc.amount_opened_aggregated, + sc.amount_aggregated FROM ( SELECT * FROM stock_current diff --git a/migrations/0109.sql b/migrations/0109.sql new file mode 100644 index 00000000..bce1404c --- /dev/null +++ b/migrations/0109.sql @@ -0,0 +1,48 @@ +DROP VIEW stock_current; +CREATE VIEW stock_current +AS +SELECT + pr.parent_product_id AS product_id, + IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = pr.parent_product_id), 0) AS amount, + IFNULL(ROUND((SELECT SUM(amount / qu_factor_purchase_to_stock) FROM stock WHERE product_id = pr.parent_product_id), 2), 0) as factor_purchase_amount, + SUM(s.amount * IFNULL(qucr.factor, 1.0)) AS amount_aggregated, + IFNULL(ROUND((SELECT SUM(IFNULL(price,0) * amount) FROM stock WHERE product_id = pr.parent_product_id), 2), 0) AS value, + MIN(s.best_before_date) AS best_before_date, + IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = pr.parent_product_id AND open = 1), 0) AS amount_opened, + IFNULL((SELECT SUM(amount) FROM stock WHERE product_id IN (SELECT sub_product_id FROM products_resolved WHERE parent_product_id = pr.parent_product_id) AND open = 1), 0) * IFNULL(qucr.factor, 1) AS amount_opened_aggregated, + CASE WHEN p_sub.parent_product_id IS NOT NULL THEN 1 ELSE 0 END AS is_aggregated_amount +FROM products_resolved pr +JOIN stock s + ON pr.sub_product_id = s.product_id +JOIN products p_parent + ON pr.parent_product_id = p_parent.id + AND p_parent.active = 1 +JOIN products p_sub + ON pr.sub_product_id = p_sub.id + AND p_sub.active = 1 +LEFT JOIN quantity_unit_conversions_resolved qucr + ON pr.sub_product_id = qucr.product_id + AND p_sub.qu_id_stock = qucr.from_qu_id + AND p_parent.qu_id_stock = qucr.to_qu_id +GROUP BY pr.parent_product_id +HAVING SUM(s.amount) > 0 + +UNION + +-- This is the same as above but sub products not rolled up (no QU conversion and column is_aggregated_amount = 0 here) +SELECT + pr.sub_product_id AS product_id, + SUM(s.amount) AS amount, + ROUND(SUM(s.amount / s.qu_factor_purchase_to_stock), 2) as factor_purchase_amount, + SUM(s.amount) AS amount_aggregated, + ROUND(SUM(IFNULL(s.price, 0) * s.amount), 2) AS value, + MIN(s.best_before_date) AS best_before_date, + IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND open = 1), 0) AS amount_opened, + IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND open = 1), 0) AS amount_opened_aggregated, + 0 AS is_aggregated_amount +FROM products_resolved pr +JOIN stock s + ON pr.sub_product_id = s.product_id +WHERE pr.parent_product_id != pr.sub_product_id +GROUP BY pr.sub_product_id +HAVING SUM(s.amount) > 0;