Fixed that "Spoil rate" and "Average shelf life" on the product card was wrong in most cases

This commit is contained in:
Bernd Bestel 2019-09-21 13:30:44 +02:00
parent b57fd83cb8
commit e4437f5db1
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
4 changed files with 20 additions and 2 deletions

View File

@ -36,6 +36,7 @@
- `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING` to disable product best before date tracking - `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 - `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/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 list improvements
- Shopping lists now have a notes field (HTML/WYSIWYG editor, just to save some notes per shopping list) - Shopping lists now have a notes field (HTML/WYSIWYG editor, just to save some notes per shopping list)

17
migrations/0090.sql Normal file
View File

@ -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;

View File

@ -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').text((productDetails.last_used || __t('never')).substring(0, 10));
$('#productcard-product-last-used-timeago').attr("datetime", productDetails.last_used || ''); $('#productcard-product-last-used-timeago').attr("datetime", productDetails.last_used || '');
$('#productcard-product-location').text(productDetails.location.name); $('#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) if (productDetails.is_aggregated_amount == 1)
{ {

View File

@ -115,7 +115,7 @@ class StockService extends BaseService
$lastPrice = $lastLogRow->price; $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; $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) if ($consumeCount == 0)
{ {