grocy/controllers/RecipesApiController.php
9Lukas5 a42d151c93
allow add full recipe required ingredient amount
This modifies the bootbox on to add recipe ingredients
to the shopping list in a way, that it
- button to open bootbox is always enabled
- popup lists all* ingredients, independent of fulfillment state
- two new quick-select buttons to check only missing or all ingredients
- radio button selection to define if the full or only missing amount
    shall be added to the shopping list

*This still excludes ingredients which have the stock check disabled
in a recipe.
In order to get this information, the RecipesController was modified
that it adds the data from the recipe ingredient setting as new
additional field in the recipePositionsResolved.

closes https://github.com/grocy/grocy/issues/686
2026-01-31 22:42:21 +01:00

115 lines
3.1 KiB
PHP

<?php
namespace Grocy\Controllers;
use Grocy\Controllers\Users\User;
use Grocy\Helpers\WebhookRunner;
use Grocy\Helpers\Grocycode;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class RecipesApiController extends BaseApiController
{
public function AddNotFulfilledProductsToShoppingList(Request $request, Response $response, array $args)
{
User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
$excludedProductIds = null;
$ignoreStock = false;
if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody))
{
$excludedProductIds = $requestBody['excludedProductIds'];
}
if ($requestBody !== null && array_key_exists('ignoreStock', $requestBody))
{
$ignoreStock = $requestBody['ignoreStock'];
}
$this->getRecipesService()->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds, $ignoreStock);
return $this->EmptyApiResponse($response);
}
public function ConsumeRecipe(Request $request, Response $response, array $args)
{
User::checkPermission($request, User::PERMISSION_STOCK_CONSUME);
try
{
$this->getRecipesService()->ConsumeRecipe($args['recipeId']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function GetRecipeFulfillment(Request $request, Response $response, array $args)
{
try
{
if (!isset($args['recipeId']))
{
return $this->FilteredApiResponse($response, $this->getRecipesService()->GetRecipesResolved(), $request->getQueryParams());
}
$recipeResolved = FindObjectInArrayByPropertyValue($this->getRecipesService()->GetRecipesResolved(), 'recipe_id', $args['recipeId']);
if (!$recipeResolved)
{
throw new \Exception('Recipe does not exist');
}
else
{
return $this->ApiResponse($response, $recipeResolved);
}
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function CopyRecipe(Request $request, Response $response, array $args)
{
try
{
return $this->ApiResponse($response, [
'created_object_id' => $this->getRecipesService()->CopyRecipe($args['recipeId'])
]);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function RecipePrintLabel(Request $request, Response $response, array $args)
{
try
{
$recipe = $this->getDatabase()->recipes()->where('id', $args['recipeId'])->fetch();
$webhookData = array_merge([
'recipe' => $recipe->name,
'grocycode' => (string)(new Grocycode(Grocycode::RECIPE, $args['recipeId'])),
'details' => $recipe
], GROCY_LABEL_PRINTER_PARAMS);
if (GROCY_LABEL_PRINTER_RUN_SERVER)
{
(new WebhookRunner())->run(GROCY_LABEL_PRINTER_WEBHOOK, $webhookData, GROCY_LABEL_PRINTER_HOOK_JSON);
}
return $this->ApiResponse($response, $webhookData);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
}