diff --git a/grocy.openapi.json b/grocy.openapi.json index 85dc302d..4c753cd1 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -2075,6 +2075,9 @@ "stock_amount": { "type": "integer" }, + "stock_amount_opened": { + "type": "integer" + }, "next_best_before_date": { "type": "string", "format": "date-time" diff --git a/migrations/0046.sql b/migrations/0046.sql index 262b1d6b..57aaf9e9 100644 --- a/migrations/0046.sql +++ b/migrations/0046.sql @@ -15,3 +15,14 @@ ADD default_best_before_days_after_open INTEGER NOT NULL DEFAULT 0; UPDATE products SET default_best_before_days_after_open = 0; + +DROP VIEW stock_current; +CREATE VIEW stock_current +AS +SELECT + s.product_id, + SUM(s.amount) AS amount, + 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 +FROM stock s +GROUP BY s.product_id; diff --git a/public/viewjs/components/productcard.js b/public/viewjs/components/productcard.js index 2d55bdc8..f6467a4d 100644 --- a/public/viewjs/components/productcard.js +++ b/public/viewjs/components/productcard.js @@ -6,6 +6,7 @@ Grocy.Components.ProductCard.Refresh = function(productId) function(productDetails) { var stockAmount = productDetails.stock_amount || '0'; + var stockAmountOpened = productDetails.stock_amount_opened || '0'; $('#productcard-product-name').text(productDetails.product.name); $('#productcard-product-stock-amount').text(stockAmount); $('#productcard-product-stock-qu-name').text(productDetails.quantity_unit_stock.name); @@ -15,6 +16,15 @@ Grocy.Components.ProductCard.Refresh = function(productId) $('#productcard-product-last-used').text((productDetails.last_used || L('never')).substring(0, 10)); $('#productcard-product-last-used-timeago').text($.timeago(productDetails.last_used || '')); + if (stockAmountOpened > 0) + { + $('#productcard-product-stock-opened-amount').text(L('#1 opened', stockAmountOpened)); + } + else + { + $('#productcard-product-stock-opened-amount').text(""); + } + $('#productcard-product-edit-button').attr("href", U("/product/" + productDetails.product.id.toString())); $('#productcard-product-edit-button').removeClass("disabled"); diff --git a/public/viewjs/stockoverview.js b/public/viewjs/stockoverview.js index 74264426..25e48c80 100644 --- a/public/viewjs/stockoverview.js +++ b/public/viewjs/stockoverview.js @@ -193,6 +193,12 @@ $(document).on('click', '.product-open-button', function(e) }); $('#product-' + productId + '-next-best-before-date-timeago').attr('datetime', result.next_best_before_date); + $('#product-' + productId + '-opened-amount').parent().effect('highlight', {}, 500); + $('#product-' + productId + '-opened-amount').fadeOut(500, function () + { + $(this).text(L('#1 opened', result.stock_amount_opened)).fadeIn(500); + }); + toastr.success(L('Marked #1 #2 of #3 as opened', 1, productQuName, productName)); RefreshContextualTimeago(); RefreshStatistics(); diff --git a/services/StockService.php b/services/StockService.php index 7545b420..bcf96b62 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -48,6 +48,7 @@ class StockService extends BaseService $product = $this->Database->products($productId); $productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount'); + $productStockAmountOpened = $this->Database->stock()->where('product_id = :1 AND open = 1', $productId)->sum('amount'); $productLastPurchased = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_PURCHASE)->where('undone', 0)->max('purchased_date'); $productLastUsed = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone', 0)->max('used_date'); $nextBestBeforeDate = $this->Database->stock()->where('product_id', $productId)->min('best_before_date'); @@ -66,6 +67,7 @@ class StockService extends BaseService 'last_purchased' => $productLastPurchased, 'last_used' => $productLastUsed, 'stock_amount' => $productStockAmount, + 'stock_amount_opened' => $productStockAmountOpened, 'quantity_unit_purchase' => $quPurchase, 'quantity_unit_stock' => $quStock, 'last_price' => $lastPrice, diff --git a/views/components/productcard.blade.php b/views/components/productcard.blade.php index 8c9fd6d9..3fa5d8d2 100644 --- a/views/components/productcard.blade.php +++ b/views/components/productcard.blade.php @@ -13,7 +13,7 @@

{{ $L('Stock quantity unit') }}:
- {{ $L('Stock amount') }}:
+ {{ $L('Stock amount') }}:
{{ $L('Last purchased') }}:
{{ $L('Last used') }}:
{{ $L('Last price') }}: diff --git a/views/stockoverview.blade.php b/views/stockoverview.blade.php index 77130846..76be1792 100644 --- a/views/stockoverview.blade.php +++ b/views/stockoverview.blade.php @@ -112,6 +112,7 @@ {{ $currentStockEntry->amount }} {{ Pluralize($currentStockEntry->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name_plural) }} + @if($currentStockEntry->amount_opened > 0){{ $L('#1 opened', $currentStockEntry->amount_opened) }}@endif {{ $currentStockEntry->best_before_date }}