mirror of
https://github.com/grocy/grocy.git
synced 2025-08-16 18:54:35 +00:00
Prevent opening more products than are unopened in stock
This commit is contained in:
@@ -25,4 +25,14 @@ SELECT
|
|||||||
MIN(s.best_before_date) AS best_before_date,
|
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
|
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND open = 1), 0) AS amount_opened
|
||||||
FROM stock s
|
FROM stock s
|
||||||
GROUP BY s.product_id;
|
GROUP BY s.product_id
|
||||||
|
|
||||||
|
UNION
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
0,
|
||||||
|
null,
|
||||||
|
0
|
||||||
|
FROM stock_missing_products
|
||||||
|
WHERE is_partly_in_stock = 0;
|
||||||
|
@@ -119,7 +119,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
|
|||||||
Grocy.Components.ProductCard.Refresh(productId);
|
Grocy.Components.ProductCard.Refresh(productId);
|
||||||
|
|
||||||
Grocy.Api.Get('stock/get-product-details/' + productId,
|
Grocy.Api.Get('stock/get-product-details/' + productId,
|
||||||
function (productDetails)
|
function(productDetails)
|
||||||
{
|
{
|
||||||
$('#amount').attr('max', productDetails.stock_amount);
|
$('#amount').attr('max', productDetails.stock_amount);
|
||||||
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
|
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
|
||||||
@@ -137,6 +137,15 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
|
|||||||
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
||||||
$('#amount').focus();
|
$('#amount').focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (productDetails.stock_amount == productDetails.stock_amount_opened)
|
||||||
|
{
|
||||||
|
$("#save-mark-as-open-button").addClass("disabled");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$("#save-mark-as-open-button").removeClass("disabled");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function(xhr)
|
function(xhr)
|
||||||
{
|
{
|
||||||
|
@@ -133,7 +133,7 @@ $(document).on('click', '.product-consume-button', function(e)
|
|||||||
$(this).text(result.next_best_before_date).fadeIn(500);
|
$(this).text(result.next_best_before_date).fadeIn(500);
|
||||||
});
|
});
|
||||||
$('#product-' + productId + '-next-best-before-date-timeago').attr('datetime', result.next_best_before_date);
|
$('#product-' + productId + '-next-best-before-date-timeago').attr('datetime', result.next_best_before_date);
|
||||||
}
|
}
|
||||||
|
|
||||||
toastr.success(L('Removed #1 #2 of #3 from stock', consumeAmount, productQuName, productName));
|
toastr.success(L('Removed #1 #2 of #3 from stock', consumeAmount, productQuName, productName));
|
||||||
RefreshContextualTimeago();
|
RefreshContextualTimeago();
|
||||||
@@ -163,6 +163,7 @@ $(document).on('click', '.product-open-button', function(e)
|
|||||||
var productId = $(e.currentTarget).attr('data-product-id');
|
var productId = $(e.currentTarget).attr('data-product-id');
|
||||||
var productName = $(e.currentTarget).attr('data-product-name');
|
var productName = $(e.currentTarget).attr('data-product-name');
|
||||||
var productQuName = $(e.currentTarget).attr('data-product-qu-name');
|
var productQuName = $(e.currentTarget).attr('data-product-qu-name');
|
||||||
|
var button = $(e.currentTarget);
|
||||||
|
|
||||||
Grocy.Api.Get('stock/open-product/' + productId + '/1',
|
Grocy.Api.Get('stock/open-product/' + productId + '/1',
|
||||||
function()
|
function()
|
||||||
@@ -199,6 +200,11 @@ $(document).on('click', '.product-open-button', function(e)
|
|||||||
$(this).text(L('#1 opened', result.stock_amount_opened)).fadeIn(500);
|
$(this).text(L('#1 opened', result.stock_amount_opened)).fadeIn(500);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.stock_amount == result.stock_amount_opened)
|
||||||
|
{
|
||||||
|
button.addClass("disabled");
|
||||||
|
}
|
||||||
|
|
||||||
toastr.success(L('Marked #1 #2 of #3 as opened', 1, productQuName, productName));
|
toastr.success(L('Marked #1 #2 of #3 as opened', 1, productQuName, productName));
|
||||||
RefreshContextualTimeago();
|
RefreshContextualTimeago();
|
||||||
RefreshStatistics();
|
RefreshStatistics();
|
||||||
|
@@ -265,11 +265,11 @@ class StockService extends BaseService
|
|||||||
throw new \Exception('Product does not exist');
|
throw new \Exception('Product does not exist');
|
||||||
}
|
}
|
||||||
|
|
||||||
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount');
|
$productStockAmountUnopened = $this->Database->stock()->where('product_id = :1 AND open = 0', $productId)->sum('amount');
|
||||||
$potentialStockEntries = $this->GetProductStockEntries($productId, true);
|
$potentialStockEntries = $this->GetProductStockEntries($productId, true);
|
||||||
$product = $this->Database->products($productId);
|
$product = $this->Database->products($productId);
|
||||||
|
|
||||||
if ($amount > $productStockAmount)
|
if ($amount > $productStockAmountUnopened)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -97,7 +97,7 @@
|
|||||||
data-consume-amount="{{ $currentStockEntry->amount }}">
|
data-consume-amount="{{ $currentStockEntry->amount }}">
|
||||||
<i class="fas fa-utensils"></i> {{ $L('All') }}
|
<i class="fas fa-utensils"></i> {{ $L('All') }}
|
||||||
</a>
|
</a>
|
||||||
<a class="btn btn-success btn-sm product-open-button @if($currentStockEntry->amount == 0) disabled @endif" href="#" data-toggle="tooltip" data-placement="left" title="{{ $L('Mark #3 #1 of #2 as open', FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name, 1) }}"
|
<a class="btn btn-success btn-sm product-open-button @if($currentStockEntry->amount == 0 || $currentStockEntry->amount == $currentStockEntry->amount_opened) disabled @endif" href="#" data-toggle="tooltip" data-placement="left" title="{{ $L('Mark #3 #1 of #2 as open', FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name, 1) }}"
|
||||||
data-product-id="{{ $currentStockEntry->product_id }}"
|
data-product-id="{{ $currentStockEntry->product_id }}"
|
||||||
data-product-name="{{ FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name }}"
|
data-product-name="{{ FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name }}"
|
||||||
data-product-qu-name="{{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name }}">
|
data-product-qu-name="{{ FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->qu_id_stock)->name }}">
|
||||||
|
Reference in New Issue
Block a user