From 617b25bda88b106af266d0ce5173a5964299bf44 Mon Sep 17 00:00:00 2001 From: "SourceFactory.at" Date: Sun, 4 Oct 2020 15:20:34 +0200 Subject: [PATCH] added button to add expired products to shoppinglist (#1026) * added button to add expired products to shoppinglist * Localizations are managed via Transifex Revert "added button to add expired products to shoppinglist" This reverts commit ad1ab5d6a0f4bd1dbc88dbb0bda67713d4038014. * Revert unnecessary change * Reuse existing function (GetExpiringProducts) to get expired products Co-authored-by: Mario Klug Co-authored-by: Bernd Bestel --- controllers/StockApiController.php | 24 +++++++++++++++++ grocy.openapi.json | 42 ++++++++++++++++++++++++++++++ localization/strings.pot | 3 +++ public/viewjs/shoppinglist.js | 14 ++++++++++ routes.php | 1 + services/StockService.php | 27 +++++++++++++++++++ views/shoppinglist.blade.php | 5 ++++ 7 files changed, 116 insertions(+) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 1830f943..8e54aae2 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -31,6 +31,30 @@ class StockApiController extends BaseApiController } } + public function AddExpiredProductsToShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) + { + User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD); + + try + { + $requestBody = $request->getParsedBody(); + + $listId = 1; + + if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + { + $listId = intval($requestBody['list_id']); + } + + $this->getStockService()->AddExpiredProductsToShoppingList($listId); + return $this->EmptyApiResponse($response); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } + } + public function AddProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { User::checkPermission($request, User::PERMISSION_STOCK_PURCHASE); diff --git a/grocy.openapi.json b/grocy.openapi.json index b569a82a..e95ca282 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -2356,6 +2356,48 @@ } } }, + "/stock/shoppinglist/add-expired-products": { + "post": { + "summary": "Adds expired products to the given shopping list", + "tags": [ + "Stock" + ], + "requestBody": { + "required": false, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "list_id": { + "type": "integer", + "description": "The shopping list to use, when omitted, the default shopping list (with id 1) is used" + } + }, + "example": { + "list_id": 2 + } + } + } + } + }, + "responses": { + "204": { + "description": "The operation was successful" + }, + "400": { + "description": "The operation was not successful (possible errors are: Not existing shopping list)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/stock/shoppinglist/clear": { "post": { "summary": "Removes all items from the given shopping list", diff --git a/localization/strings.pot b/localization/strings.pot index ed3ccb92..18952083 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -1909,3 +1909,6 @@ msgstr "" msgid "Journal summary for this product" msgstr "" + +msgid "Add expired products" +msgstr "" diff --git a/public/viewjs/shoppinglist.js b/public/viewjs/shoppinglist.js index b1c78433..27802080 100644 --- a/public/viewjs/shoppinglist.js +++ b/public/viewjs/shoppinglist.js @@ -155,6 +155,20 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e) ); }); +$(document).on('click', '#add-expired-products', function(e) +{ + Grocy.Api.Post('stock/shoppinglist/add-expired-products', { "list_id": $("#selected-shopping-list").val() }, + function(result) + { + window.location.href = U('/shoppinglist?list=' + $("#selected-shopping-list").val()); + }, + function(xhr) + { + console.error(xhr); + } + ); +}); + $(document).on('click', '#clear-shopping-list', function(e) { bootbox.confirm({ diff --git a/routes.php b/routes.php index ab1377af..2bf90ac3 100644 --- a/routes.php +++ b/routes.php @@ -212,6 +212,7 @@ $app->group('/api', function (RouteCollectorProxy $group) { if (GROCY_FEATURE_FLAG_SHOPPINGLIST) { $group->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); + $group->post('/stock/shoppinglist/add-expired-products', '\Grocy\Controllers\StockApiController:AddExpiredProductsToShoppingList'); $group->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); $group->post('/stock/shoppinglist/add-product', '\Grocy\Controllers\StockApiController:AddProductToShoppingList'); $group->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveProductFromShoppingList'); diff --git a/services/StockService.php b/services/StockService.php index 768c3e92..9d54d4b0 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -52,6 +52,33 @@ class StockService extends BaseService } } + public function AddExpiredProductsToShoppingList($listId = 1) + { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + + $expiredProducts = $this->GetExpiringProducts(-1); + + foreach ($expiredProducts as $expiredProduct) + { + $product = $this->getDatabase()->products()->where('id', $expiredProduct->product_id)->fetch(); + + $alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $expiredProduct->product_id)->fetch(); + + if (!$alreadyExistingEntry) + { + $shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([ + 'product_id' => $expiredProduct->product_id, + 'amount' => 1, + 'shopping_list_id' => $listId + ]); + $shoppinglistRow->save(); + } + } + } + public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $quFactorPurchaseToStock, $locationId = null, $shoppingLocationId = null, &$transactionId = null) { if (!$this->ProductExists($productId)) diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index cb53234e..4b81e588 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -96,6 +96,11 @@ href="#"> {{ $__t('Add products that are below defined min. stock amount') }} + + {{ $__t('Add expired products') }} +