From cae924eb81573a38a29657afdb6b258e8ef9b95a Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 2 Jul 2021 17:37:06 +0200 Subject: [PATCH] Fixed shopping list QU handling (fixes #1385, fixes #1384) --- changelog/62_UNRELEASED_xxxx-xx-xx.md | 1 + controllers/StockApiController.php | 8 +- migrations/0133.sql | 103 ++++++++++++++++++ .../viewjs/components/productamountpicker.js | 4 +- public/viewjs/shoppinglistitemform.js | 7 +- services/StockService.php | 14 ++- 6 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 migrations/0133.sql diff --git a/changelog/62_UNRELEASED_xxxx-xx-xx.md b/changelog/62_UNRELEASED_xxxx-xx-xx.md index 662ecf9d..654c32c8 100644 --- a/changelog/62_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/62_UNRELEASED_xxxx-xx-xx.md @@ -39,6 +39,7 @@ - The amount now defaults to `1` for adding items quicker - Fixed that shopping list prints had a grey background (thanks @Forceu) - Fixed the form validation on the shopping list item page (thanks @Forceu) +- Fixed that when adding products to the shopping list from the stock overview page, the used quantity unit was always the products default purchase QU (and not the selected one) ### Recipe improvements/fixes - Recipe printing improvements (thanks @Ape) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index e25c1144..1fd36102 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -180,6 +180,7 @@ class StockApiController extends BaseApiController $listId = 1; $amount = 1; + $quId = -1; $productId = null; $note = null; @@ -203,12 +204,17 @@ class StockApiController extends BaseApiController $note = $requestBody['note']; } + if (array_key_exists('qu_id', $requestBody) && !empty($requestBody['qu_id'])) + { + $quId = $requestBody['qu_id']; + } + if ($productId == null) { throw new \Exception('No product id was supplied'); } - $this->getStockService()->AddProductToShoppingList($productId, $amount, $note, $listId); + $this->getStockService()->AddProductToShoppingList($productId, $amount, $quId, $note, $listId); return $this->EmptyApiResponse($response); } catch (\Exception $ex) diff --git a/migrations/0133.sql b/migrations/0133.sql new file mode 100644 index 00000000..aaaf225e --- /dev/null +++ b/migrations/0133.sql @@ -0,0 +1,103 @@ +DROP VIEW quantity_unit_conversions_resolved; +CREATE VIEW quantity_unit_conversions_resolved +AS + +-- 1. Product "purchase to stock" conversion factor +SELECT + -1 AS id, -- Dummy, LessQL needs an id column + p.id AS product_id, + p.qu_id_purchase AS from_qu_id, + qu_from.name AS from_qu_name, + qu_from.name_plural AS from_qu_name_plural, + p.qu_id_stock AS to_qu_id, + qu_to.name AS to_qu_name, + qu_to.name_plural AS to_qu_name_plural, + p.qu_factor_purchase_to_stock AS factor +FROM products p +JOIN quantity_units qu_from + ON p.qu_id_purchase = qu_from.id +JOIN quantity_units qu_to + ON p.qu_id_stock = qu_to.id +UNION -- Inversed +SELECT + -1 AS id, -- Dummy, LessQL needs an id column + p.id AS product_id, + p.qu_id_stock AS from_qu_id, + qu_to.name AS from_qu_name, + qu_to.name_plural AS from_qu_name_plural, + p.qu_id_purchase AS to_qu_id, + qu_from.name AS to_qu_name, + qu_from.name_plural AS to_qu_name_plural, + 1 / p.qu_factor_purchase_to_stock AS factor +FROM products p +JOIN quantity_units qu_from + ON p.qu_id_purchase = qu_from.id +JOIN quantity_units qu_to + ON p.qu_id_stock = qu_to.id + +UNION + +-- 2. Product specific QU overrides +SELECT + -1 AS id, -- Dummy, LessQL needs an id column + p.id AS product_id, + quc.from_qu_id AS from_qu_id, + qu_from.name AS from_qu_name, + qu_from.name_plural AS from_qu_name_plural, + quc.to_qu_id AS to_qu_id, + qu_to.name AS to_qu_name, + qu_to.name_plural AS to_qu_name_plural, + quc.factor AS factor +FROM products p +JOIN quantity_unit_conversions quc + ON p.id = quc.product_id +JOIN quantity_units qu_from + ON quc.from_qu_id = qu_from.id +JOIN quantity_units qu_to + ON quc.to_qu_id = qu_to.id + +UNION + +-- 3. Default (direct) QU conversion factors +SELECT + -1 AS id, -- Dummy, LessQL needs an id column + p.id AS product_id, + p.qu_id_stock AS from_qu_id, + qu_from.name AS from_qu_name, + qu_from.name_plural AS from_qu_name_plural, + quc.to_qu_id AS to_qu_id, + qu_to.name AS to_qu_name, + qu_to.name_plural AS to_qu_name_plural, + quc.factor AS factor +FROM products p +JOIN quantity_unit_conversions quc + ON p.qu_id_stock = quc.from_qu_id + AND quc.product_id IS NULL +JOIN quantity_units qu_from + ON quc.from_qu_id = qu_from.id +JOIN quantity_units qu_to + ON quc.to_qu_id = qu_to.id + +UNION + +-- 4. Default (indirect) QU conversion factors +SELECT + -1 AS id, -- Dummy, LessQL needs an id column + p.id AS product_id, + (SELECT from_qu_id FROM quantity_unit_conversions WHERE to_qu_id = quc.to_qu_id AND product_id = p.id) AS from_qu_id, + qu_from.name AS from_qu_name, + qu_from.name_plural AS from_qu_name_plural, + quc.from_qu_id AS to_qu_id, + qu_to.name AS to_qu_name, + qu_to.name_plural AS to_qu_name_plural, + quc.factor * (SELECT factor FROM quantity_unit_conversions WHERE to_qu_id = quc.to_qu_id AND product_id = p.id) AS factor +FROM products p +JOIN product_qu_relations pqr + ON p.id = pqr.product_id +JOIN quantity_unit_conversions quc + ON pqr.qu_id = quc.from_qu_id + AND quc.product_id IS NULL +JOIN quantity_units qu_from + ON (SELECT from_qu_id FROM quantity_unit_conversions WHERE to_qu_id = quc.to_qu_id AND product_id = p.id) = qu_from.id +JOIN quantity_units qu_to + ON quc.from_qu_id = qu_to.id; diff --git a/public/viewjs/components/productamountpicker.js b/public/viewjs/components/productamountpicker.js index 2afd9960..0d16f242 100644 --- a/public/viewjs/components/productamountpicker.js +++ b/public/viewjs/components/productamountpicker.js @@ -21,7 +21,7 @@ Grocy.Components.ProductAmountPicker.Reload = function(productId, destinationQuI if (!$('#qu_id option[value="' + conversion.to_qu_id + '"]').length) // Don't add the destination QU multiple times { - $("#qu_id").append(''); + $("#qu_id").append(''); } }); } @@ -68,7 +68,7 @@ Grocy.Components.ProductAmountPicker.AllowAnyQu = function(keepInitialQu = false $("#qu_id").find("option").remove().end(); Grocy.QuantityUnits.forEach(qu => { - $("#qu_id").append(''); + $("#qu_id").append('