Consume all available products for unfulfilled recipe

Fix #386
This commit is contained in:
Jan-Jaap Korpershoek 2023-02-05 15:43:46 +01:00
parent efc7b999bb
commit c8ed8b6a16
3 changed files with 10 additions and 12 deletions

View File

@ -74,12 +74,6 @@ class RecipesService extends BaseService
throw new \Exception('Recipe does not exist');
}
$recipeResolved = $this->getDatabase()->recipes_resolved()->where('recipe_id', $recipeId)->fetch();
if ($recipeResolved->need_fulfilled == 0)
{
throw new \Exception('Recipe need is not fulfilled, consuming not possible');
}
$transactionId = uniqid();
$recipePositions = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id', $recipeId)->fetchAll();
@ -90,11 +84,11 @@ class RecipesService extends BaseService
{
if ($recipePosition->only_check_single_unit_in_stock == 0)
{
$this->getStockService()->ConsumeProduct($recipePosition->product_id, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId, null, $transactionId, true, true);
$this->getStockService()->ConsumeProduct($recipePosition->product_id, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId, null, $transactionId, true, true, true);
}
}
}
catch (Exception $ex)
catch (\Exception $ex)
{
$this->getDatabaseService()->GetDbConnectionRaw()->rollback();
throw $ex;

View File

@ -357,7 +357,7 @@ class StockService extends BaseService
}
}
public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null, $allowSubproductSubstitution = false, $consumeExactAmount = false)
public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null, $allowSubproductSubstitution = false, $consumeExactAmount = false, $consume_all_available = false)
{
if (!$this->ProductExists($productId))
{
@ -409,7 +409,11 @@ class StockService extends BaseService
$productStockAmount = floatval($productDetails->stock_amount_aggregated);
if (round($amount, 2) > round($productStockAmount, 2))
{
throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)');
if ($consume_all_available) {
$amount = $productStockAmount;
} else {
throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)');
}
}
if ($transactionId === null)

View File

@ -313,7 +313,7 @@
<div class="d-flex justify-content-between align-items-center">
<h3 class="card-title mb-0">{{ $recipe->name }}</h3>
<div class="card-icons d-flex flex-wrap justify-content-end flex-shrink-1">
<a class="btn @if(!GROCY_FEATURE_FLAG_STOCK) d-none @endif recipe-consume @if(FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $recipe->id)->need_fulfilled == 0) disabled @endif"
<a class="btn @if(!GROCY_FEATURE_FLAG_STOCK) d-none @endif recipe-consume"
href="#"
data-toggle="tooltip"
title="{{ $__t('Consume all ingredients needed by this recipe') }}"
@ -349,7 +349,7 @@
<div class="mb-4 @if(!empty($recipe->picture_file_name)) d-none @else d-flex @endif d-print-block justify-content-between align-items-center">
<h1 class="card-title mb-0">{{ $recipe->name }}</h1>
<div class="card-icons d-flex flex-wrap justify-content-end flex-shrink-1 d-print-none">
<a class="btn recipe-consume @if(FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $recipe->id)->need_fulfilled == 0) disabled @endif"
<a class="btn recipe-consume"
href="#"
data-toggle="tooltip"
title="{{ $__t('Consume all ingredients needed by this recipe') }}"