From 7b4edf3147de0804dd77efb663eb5c0eaa55f112 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 15 Nov 2020 22:38:21 +0100 Subject: [PATCH] Adapt shopping list add expired products for #851 --- changelog/60_UNRELEASED_2020-xx-xx.md | 3 +- controllers/StockApiController.php | 24 +++++++++++++++ grocy.openapi.json | 42 +++++++++++++++++++++++++++ localization/strings.pot | 6 ++-- public/viewjs/shoppinglist.js | 13 +++++++-- routes.php | 1 + services/StockService.php | 26 ++++++++++++++++- views/shoppinglist.blade.php | 4 +-- 8 files changed, 110 insertions(+), 9 deletions(-) diff --git a/changelog/60_UNRELEASED_2020-xx-xx.md b/changelog/60_UNRELEASED_2020-xx-xx.md index a1f35e3b..444a222f 100644 --- a/changelog/60_UNRELEASED_2020-xx-xx.md +++ b/changelog/60_UNRELEASED_2020-xx-xx.md @@ -77,7 +77,7 @@ ### Shopping list improvements - Decimal amounts are now allowed (for any product, rounded by two decimal places) -- Added a button to add all currently in-stock but expired products to the shopping list (thanks @m-byte) +- Added a button to add all currently in-stock but overdue and expired products to the shopping list (thanks @m-byte) - Improved that when `FEATURE_FLAG_STOCK` is disabled, all product/stock related inputs and buttons are now hidden on the shopping list page (thanks @fipwmaqzufheoxq92ebc) - Fixed that "Add products that are below defined min. stock amount" always rounded up the missing amount to an integral number, this now allows decimal numbers @@ -190,6 +190,7 @@ - `` is the value to search for - New endpoints `/stock/journal` and `/stock/journal/summary` to get the stock journal (thanks @fipwmaqzufheoxq92ebc) - New endpoint `/stock/shoppinglist/add-overdue-products` to add all currently in-stock but overdue products to a shopping list (thanks @m-byte) +- New endpoint `/stock/shoppinglist/add-expired-products` to add all currently in-stock but expired products to a shopping list - New endpoints GET/POST/PUT `/users/{userId}/permissions` for the new user permissions feature mentioned above - Performance improvements of the `/stock/products/*` endpoints (thanks @fipwmaqzufheoxq92ebc) - Fixed that the endpoint `/objects/{entity}/{objectId}` always returned successfully, even when the given object not exists (now returns `404` when the object is not found) (thanks @fipwmaqzufheoxq92ebc) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index a18b20e9..6f5bb067 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -55,6 +55,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 = $this->GetParsedAndFilteredRequestBody($request); + + $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 20d7b73a..f3792f1a 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -2572,6 +2572,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/Error400" + } + } + } + } + } + } + }, "/stock/shoppinglist/clear": { "post": { "summary": "Removes all items from the given shopping list", diff --git a/localization/strings.pot b/localization/strings.pot index a3d7ee82..2e1b4411 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -1798,9 +1798,6 @@ msgstr "" msgid "Journal summary for this product" msgstr "" -msgid "Add expired products" -msgstr "" - msgid "Consume exact amount" msgstr "" @@ -1981,3 +1978,6 @@ msgstr "" msgid "Due soon days" msgstr "" + +msgid "Add overdue/expired products" +msgstr "" diff --git a/public/viewjs/shoppinglist.js b/public/viewjs/shoppinglist.js index c40474a2..c84b91cd 100644 --- a/public/viewjs/shoppinglist.js +++ b/public/viewjs/shoppinglist.js @@ -174,12 +174,21 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e) ); }); -$(document).on('click', '#add-overdue-products', function(e) +$(document).on('click', '#add-overdue-expired-products', function(e) { Grocy.Api.Post('stock/shoppinglist/add-overdue-products', { "list_id": $("#selected-shopping-list").val() }, function(result) { - window.location.href = U('/shoppinglist?list=' + $("#selected-shopping-list").val()); + 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); + } + ); }, function(xhr) { diff --git a/routes.php b/routes.php index 29eea235..8b4c4a2f 100644 --- a/routes.php +++ b/routes.php @@ -211,6 +211,7 @@ $app->group('/api', function (RouteCollectorProxy $group) { { $group->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $group->post('/stock/shoppinglist/add-overdue-products', '\Grocy\Controllers\StockApiController:AddOverdueProductsToShoppingList'); + $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 8036d727..2fc88313 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -60,7 +60,6 @@ class StockService extends BaseService } $overdueProducts = $this->GetDueProducts(-1); - foreach ($overdueProducts as $overdueProduct) { $product = $this->getDatabase()->products()->where('id', $overdueProduct->product_id)->fetch(); @@ -78,6 +77,31 @@ class StockService extends BaseService } } + public function AddExpiredProductsToShoppingList($listId = 1) + { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + + $expiredProducts = $this->GetExpiredProducts(); + 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, $locationId = null, $shoppingLocationId = null, &$transactionId = null) { if (!$this->ProductExists($productId)) diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index 946041e4..7a0e04cb 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -115,10 +115,10 @@ href="#"> {{ $__t('Add products that are below defined min. stock amount') }} - - {{ $__t('Add overdue products') }} + {{ $__t('Add overdue/expired products') }}