From e4437f5db1879137cd1ed1e1676746dec35fcc47 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 21 Sep 2019 13:30:44 +0200 Subject: [PATCH] Fixed that "Spoil rate" and "Average shelf life" on the product card was wrong in most cases --- changelog/52_UNRELEASED_2019-xx-xx.md | 1 + migrations/0090.sql | 17 +++++++++++++++++ public/viewjs/components/productcard.js | 2 +- services/StockService.php | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 migrations/0090.sql diff --git a/changelog/52_UNRELEASED_2019-xx-xx.md b/changelog/52_UNRELEASED_2019-xx-xx.md index 45dfc54e..9335f024 100644 --- a/changelog/52_UNRELEASED_2019-xx-xx.md +++ b/changelog/52_UNRELEASED_2019-xx-xx.md @@ -36,6 +36,7 @@ - `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING` to disable product best before date tracking - `FEATURE_FLAG_STOCK_PRODUCT_OPENED_TRACKING` to disable product opened tracking - Fixed/refined some things regarding purchase/consume/inventory of products with enabled tare weight handling (nothing was broken, but the success popups may not displayed the correct amount that was posted) +- Fixed that "Spoil rate" and "Average shelf life" on the product card was wrong in most cases ### Shopping list improvements - Shopping lists now have a notes field (HTML/WYSIWYG editor, just to save some notes per shopping list) diff --git a/migrations/0090.sql b/migrations/0090.sql new file mode 100644 index 00000000..b851cf30 --- /dev/null +++ b/migrations/0090.sql @@ -0,0 +1,17 @@ +DROP VIEW stock_average_product_shelf_life; +CREATE VIEW stock_average_product_shelf_life +AS +SELECT + p.id, + CASE WHEN x.product_id IS NULL THEN -1 ELSE AVG(x.shelf_life_days) END AS average_shelf_life_days +FROM products p +LEFT JOIN ( + SELECT + sl_p.product_id, + JULIANDAY(sl_p.best_before_date) - JULIANDAY(sl_p.purchased_date) AS shelf_life_days + FROM stock_log sl_p + WHERE sl_p.transaction_type = 'purchase' + AND sl_p.undone = 0 + ) x + ON p.id = x.product_id +GROUP BY p.id; diff --git a/public/viewjs/components/productcard.js b/public/viewjs/components/productcard.js index 98ac2a74..8343bf03 100644 --- a/public/viewjs/components/productcard.js +++ b/public/viewjs/components/productcard.js @@ -16,7 +16,7 @@ Grocy.Components.ProductCard.Refresh = function(productId) $('#productcard-product-last-used').text((productDetails.last_used || __t('never')).substring(0, 10)); $('#productcard-product-last-used-timeago').attr("datetime", productDetails.last_used || ''); $('#productcard-product-location').text(productDetails.location.name); - $('#productcard-product-spoil-rate').text(parseFloat(productDetails.spoil_rate_percent).toLocaleString(undefined, { style: "percent" })); + $('#productcard-product-spoil-rate').text((parseFloat(productDetails.spoil_rate_percent) / 100).toLocaleString(undefined, { style: "percent" })); if (productDetails.is_aggregated_amount == 1) { diff --git a/services/StockService.php b/services/StockService.php index 8a593031..2c1889d8 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -115,7 +115,7 @@ class StockService extends BaseService $lastPrice = $lastLogRow->price; } - $consumeCount = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone', 0)->sum('amount') * -1; + $consumeCount = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0 AND spoiled = 0')->sum('amount') * -1; $consumeCountSpoiled = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0 AND spoiled = 1')->sum('amount') * -1; if ($consumeCount == 0) {