From f6e0ff11f1605b8aa102bdf58cf2daf8975aac1c Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 20 Dec 2022 22:11:35 +0100 Subject: [PATCH 1/3] Fixed column visibility handling when there is a shadow rowgroup column (fixes #2074) --- changelog/70_UNRELEASED_xxxx.xx.xx.md | 5 +++-- public/js/grocy.js | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/changelog/70_UNRELEASED_xxxx.xx.xx.md b/changelog/70_UNRELEASED_xxxx.xx.xx.md index 2d10a348..bd387d23 100644 --- a/changelog/70_UNRELEASED_xxxx.xx.xx.md +++ b/changelog/70_UNRELEASED_xxxx.xx.xx.md @@ -9,6 +9,7 @@ ### Stock - Quantity unit conversions now support transitive conversions, means the QU hierarchy has now unlimited levels (thanks a lot @esclear) +- Fixed that hiding the "Purchased date" column (table options) on the stock entries page didn't work ### Shopping list @@ -16,7 +17,7 @@ ### Recipes -- xxx +- Fixed that hiding the "Requirements fulfilled" column (table options) on the recipes page didn't work ### Meal plan @@ -32,7 +33,7 @@ ### Tasks -- xxx +- Fixed that hiding the "Category" column (table options) on the tasks page didn't work ### Batteries diff --git a/public/js/grocy.js b/public/js/grocy.js index edd5d66c..da608b07 100644 --- a/public/js/grocy.js +++ b/public/js/grocy.js @@ -1029,6 +1029,7 @@ $(".change-table-columns-visibility-button").on("click", function(e) dataTable.columns().every(function() { var index = this.index(); + var indexForGrouping = index; var headerCell = $(this.header()); var title = headerCell.text(); var visible = this.visible(); @@ -1041,7 +1042,7 @@ $(".change-table-columns-visibility-button").on("click", function(e) var shadowColumnIndex = headerCell.attr("data-shadow-rowgroup-column"); if (shadowColumnIndex) { - index = shadowColumnIndex; + indexForGrouping = shadowColumnIndex; } var checked = "checked"; @@ -1076,12 +1077,12 @@ $(".change-table-columns-visibility-button").on("click", function(e) \ \ '; } From 407344e86a1ca913e2d8960260cf3ed9219e8142 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Wed, 21 Dec 2022 20:16:22 +0100 Subject: [PATCH 2/3] Fixed recipe ingredient costs/calories calculation when product substitution and unit conversions is involved at the same time (fixes #2075) --- changelog/70_UNRELEASED_xxxx.xx.xx.md | 1 + migrations/0206.sql | 135 ++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 migrations/0206.sql diff --git a/changelog/70_UNRELEASED_xxxx.xx.xx.md b/changelog/70_UNRELEASED_xxxx.xx.xx.md index bd387d23..45e72a1a 100644 --- a/changelog/70_UNRELEASED_xxxx.xx.xx.md +++ b/changelog/70_UNRELEASED_xxxx.xx.xx.md @@ -18,6 +18,7 @@ ### Recipes - Fixed that hiding the "Requirements fulfilled" column (table options) on the recipes page didn't work +- Fixed that ingredient costs and calories were wrong when product substitution and unit conversions were involved at the same time ### Meal plan diff --git a/migrations/0206.sql b/migrations/0206.sql new file mode 100644 index 00000000..141e48e1 --- /dev/null +++ b/migrations/0206.sql @@ -0,0 +1,135 @@ +DROP VIEW recipes_pos_resolved; +CREATE VIEW recipes_pos_resolved +AS + +-- Multiplication by 1.0 to force conversion to float (REAL) + +SELECT + r.id AS recipe_id, + rp.id AS recipe_pos_id, + rp.product_id AS product_id, + CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END AS recipe_amount, + IFNULL(sc.amount_aggregated, 0) AS stock_amount, + CASE WHEN IFNULL(sc.amount_aggregated, 0) >= CASE WHEN rp.only_check_single_unit_in_stock = 1 THEN 0.00000001 ELSE CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END END THEN 1 ELSE 0 END AS need_fulfilled, + CASE WHEN IFNULL(sc.amount_aggregated, 0) - CASE WHEN rp.only_check_single_unit_in_stock = 1 THEN 0.00000001 ELSE CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END END < 0 THEN ABS(IFNULL(sc.amount_aggregated, 0) - (CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END)) ELSE 0 END AS missing_amount, + IFNULL(sl.amount, 0) AS amount_on_shopping_list, + CASE WHEN ROUND(IFNULL(sc.amount_aggregated, 0) + CASE WHEN r.not_check_shoppinglist = 1 THEN 0 ELSE IFNULL(sl.amount, 0) END, 2) >= ROUND(CASE WHEN rp.only_check_single_unit_in_stock = 1 THEN 0.00000001 ELSE CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END END, 2) THEN 1 ELSE 0 END AS need_fulfilled_with_shopping_list, + rp.qu_id, + (r.desired_servings*1.0 / r.base_servings*1.0) * (rnr.includes_servings*1.0 / CASE WHEN rnr.recipe_id != rnr.includes_recipe_id THEN rnrr.base_servings*1.0 ELSE 1 END) * rp.amount * IFNULL(pcp.price, 0) * rp.price_factor * CASE WHEN rp.only_check_single_unit_in_stock = 1 OR rp.product_id != p_effective.id THEN IFNULL(qucr.factor, 1) ELSE 1 END AS costs, + CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN 0 ELSE 1 END AS is_nested_recipe_pos, + rp.ingredient_group, + pg.name as product_group, + rp.id, -- Just a dummy id column + r.type as recipe_type, + rnr.includes_recipe_id as child_recipe_id, + rp.note, + rp.variable_amount AS recipe_variable_amount, + rp.only_check_single_unit_in_stock, + rp.amount / r.base_servings*1.0 * (rnr.includes_servings*1.0 / CASE WHEN rnr.recipe_id != rnr.includes_recipe_id THEN rnrr.base_servings*1.0 ELSE 1 END) * IFNULL(p_effective.calories, 0) * CASE WHEN rp.only_check_single_unit_in_stock = 1 OR rp.product_id != p_effective.id THEN IFNULL(qucr.factor, 1) ELSE 1 END AS calories, + p.active AS product_active, + CASE pvs.current_due_status + WHEN 'ok' THEN 0 + WHEN 'due_soon' THEN 1 + WHEN 'overdue' THEN 10 + WHEN 'expired' THEN 20 + END AS due_score, + IFNULL(pcs.product_id_effective, rp.product_id) AS product_id_effective, + p.name AS product_name +FROM recipes r +JOIN recipes_nestings_resolved rnr + ON r.id = rnr.recipe_id +JOIN recipes rnrr + ON rnr.includes_recipe_id = rnrr.id +JOIN recipes_pos rp + ON rnr.includes_recipe_id = rp.recipe_id +JOIN products p + ON rp.product_id = p.id +JOIN products_volatile_status pvs + ON rp.product_id = pvs.product_id +LEFT JOIN product_groups pg + ON p.product_group_id = pg.id +LEFT JOIN ( + SELECT product_id, SUM(amount) AS amount + FROM shopping_list + GROUP BY product_id) sl + ON rp.product_id = sl.product_id +LEFT JOIN stock_current sc + ON rp.product_id = sc.product_id +LEFT JOIN products_current_substitutions pcs + ON rp.product_id = pcs.parent_product_id +LEFT JOIN products_current_price pcp + ON IFNULL(pcs.product_id_effective, rp.product_id) = pcp.product_id +LEFT JOIN products p_effective + ON IFNULL(pcs.product_id_effective, rp.product_id) = p_effective.id +LEFT JOIN quantity_unit_conversions_resolved qucr + ON IFNULL(pcs.product_id_effective, rp.product_id) = qucr.product_id + AND CASE WHEN rp.product_id != p_effective.id THEN p.qu_id_stock ELSE rp.qu_id END = qucr.from_qu_id + AND IFNULL(p_effective.qu_id_stock, p.qu_id_stock) = qucr.to_qu_id +WHERE rp.not_check_stock_fulfillment = 0 + +UNION + +-- Just add all recipe positions which should not be checked against stock with fulfilled need + +SELECT + r.id AS recipe_id, + rp.id AS recipe_pos_id, + rp.product_id AS product_id, + CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) ELSE rp.amount * ((r.desired_servings*1.0) / (r.base_servings*1.0)) * ((rnr.includes_servings*1.0) / (rnrr.base_servings*1.0)) END AS recipe_amount, + IFNULL(sc.amount_aggregated, 0) AS stock_amount, + 1 AS need_fulfilled, + 0 AS missing_amount, + IFNULL(sl.amount, 0) AS amount_on_shopping_list, + 1 AS need_fulfilled_with_shopping_list, + rp.qu_id, + (r.desired_servings*1.0 / r.base_servings*1.0) * (rnr.includes_servings*1.0 / CASE WHEN rnr.recipe_id != rnr.includes_recipe_id THEN rnrr.base_servings*1.0 ELSE 1 END) * rp.amount * IFNULL(pcp.price, 0) * rp.price_factor * CASE WHEN rp.only_check_single_unit_in_stock = 1 OR rp.product_id != p_effective.id THEN IFNULL(qucr.factor, 1) ELSE 1 END AS costs, + CASE WHEN rnr.recipe_id = rnr.includes_recipe_id THEN 0 ELSE 1 END AS is_nested_recipe_pos, + rp.ingredient_group, + pg.name as product_group, + rp.id, -- Just a dummy id column + r.type as recipe_type, + rnr.includes_recipe_id as child_recipe_id, + rp.note, + rp.variable_amount AS recipe_variable_amount, + rp.only_check_single_unit_in_stock, + rp.amount / r.base_servings*1.0 * (rnr.includes_servings*1.0 / CASE WHEN rnr.recipe_id != rnr.includes_recipe_id THEN rnrr.base_servings*1.0 ELSE 1 END) * IFNULL(p_effective.calories, 0) * CASE WHEN rp.only_check_single_unit_in_stock = 1 OR rp.product_id != p_effective.id THEN IFNULL(qucr.factor, 1) ELSE 1 END AS calories, + p.active AS product_active, + CASE pvs.current_due_status + WHEN 'ok' THEN 0 + WHEN 'due_soon' THEN 1 + WHEN 'overdue' THEN 10 + WHEN 'expired' THEN 20 + END AS due_score, + IFNULL(pcs.product_id_effective, rp.product_id) AS product_id_effective, + p.name AS product_name +FROM recipes r +JOIN recipes_nestings_resolved rnr + ON r.id = rnr.recipe_id +JOIN recipes rnrr + ON rnr.includes_recipe_id = rnrr.id +JOIN recipes_pos rp + ON rnr.includes_recipe_id = rp.recipe_id +JOIN products p + ON rp.product_id = p.id +JOIN products_volatile_status pvs + ON rp.product_id = pvs.product_id +LEFT JOIN product_groups pg + ON p.product_group_id = pg.id +LEFT JOIN ( + SELECT product_id, SUM(amount) AS amount + FROM shopping_list + GROUP BY product_id) sl + ON rp.product_id = sl.product_id +LEFT JOIN stock_current sc + ON rp.product_id = sc.product_id +LEFT JOIN products_current_substitutions pcs + ON rp.product_id = pcs.parent_product_id +LEFT JOIN products_current_price pcp + ON IFNULL(pcs.product_id_effective, rp.product_id) = pcp.product_id +LEFT JOIN products p_effective + ON IFNULL(pcs.product_id_effective, rp.product_id) = p_effective.id +LEFT JOIN quantity_unit_conversions_resolved qucr + ON IFNULL(pcs.product_id_effective, rp.product_id) = qucr.product_id + AND CASE WHEN rp.product_id != p_effective.id THEN p.qu_id_stock ELSE rp.qu_id END = qucr.from_qu_id + AND IFNULL(p_effective.qu_id_stock, p.qu_id_stock) = qucr.to_qu_id +WHERE rp.not_check_stock_fulfillment = 1; From dd409b4bf9a99b132d4cd7992ac2fff9da64ccad Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Wed, 21 Dec 2022 21:00:49 +0100 Subject: [PATCH 3/3] Fixed consume amount calculation when consuming multiple substituted subproducts at once and when multiple/different conversion factors were involved (fixes #2076) --- changelog/70_UNRELEASED_xxxx.xx.xx.md | 1 + services/StockService.php | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/changelog/70_UNRELEASED_xxxx.xx.xx.md b/changelog/70_UNRELEASED_xxxx.xx.xx.md index 45e72a1a..49d0aa8b 100644 --- a/changelog/70_UNRELEASED_xxxx.xx.xx.md +++ b/changelog/70_UNRELEASED_xxxx.xx.xx.md @@ -10,6 +10,7 @@ - Quantity unit conversions now support transitive conversions, means the QU hierarchy has now unlimited levels (thanks a lot @esclear) - Fixed that hiding the "Purchased date" column (table options) on the stock entries page didn't work +- Fixed that the consumed amount was wrong, when consuming multiple substituted subproducts at once and when multiple/different conversion factors were involved ### Shopping list diff --git a/services/StockService.php b/services/StockService.php index 85f57803..508068c6 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -407,7 +407,7 @@ class StockService extends BaseService } $productStockAmount = floatval($productDetails->stock_amount_aggregated); - if ($amount > $productStockAmount) + if (round($amount, 2) > round($productStockAmount, 2)) { throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)'); } @@ -460,6 +460,13 @@ class StockService extends BaseService $stockEntry->delete(); $amount -= $stockEntry->amount; + + if ($allowSubproductSubstitution && $stockEntry->product_id != $productId && $conversion != null) + { + // A sub product with QU conversions was used + // => Convert the rest amount back to be based on the original (parent) product for the next round + $amount = $amount / floatval($conversion->factor); + } } else {