From cc1c6a6442cd2c958787950cadc59a83aa4f53cb Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 10 Aug 2019 08:20:52 +0200 Subject: [PATCH] Little changes for pull request #338 --- controllers/StockApiController.php | 4 ++-- grocy.openapi.json | 14 +++++++------- routes.php | 2 +- services/StockService.php | 23 +++++++++++++++++------ 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 86cfdc5a..e290d123 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -165,7 +165,7 @@ class StockApiController extends BaseApiController throw new \Exception('An new amount is required'); } - $bestBeforeDate = date('Y-m-d'); + $bestBeforeDate = null; if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date'])) { $bestBeforeDate = $requestBody['best_before_date']; @@ -290,7 +290,7 @@ class StockApiController extends BaseApiController - public function RemoveFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + public function RemoveItemFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try { diff --git a/grocy.openapi.json b/grocy.openapi.json index e55c4b61..cb6a200b 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1534,9 +1534,9 @@ } } }, - "/stock/shoppinglist/remove-from-list": { + "/stock/shoppinglist/remove-product": { "post": { - "summary": "Removes a product from the shoppinglist, if it is on it", + "summary": "Removes the given product from the given shopping list, if it is on it", "tags": [ "Stock" ], @@ -1549,21 +1549,21 @@ "properties": { "product_id": { "type": "integer", - "description": "The product id from the item on the shopping list" + "description": "A valid product id of the item on the shopping list" }, "list_id": { "type": "integer", - "description": "The shopping list id to use, when omitted, the default shopping list (with id 1) is used" + "description": "A valid shopping list id, when omitted, the default shopping list (with id 1) is used" }, "product_amount": { "type": "integer", - "description": "The amount of prodcut units to remove, when omitted, the default amount of 1 is used" + "description": "The amount of product units to remove, when omitted, the default amount of 1 is used" } }, "example": { "product_id": 3, "list_id": 2, - "product_amount": 5, + "product_amount": 5 } } } @@ -1574,7 +1574,7 @@ "description": "The operation was successful" }, "400": { - "description": "The operation was not successful (possible errors are: Not existing shopping list, No product id supplied)", + "description": "The operation was not successful (possible errors are: Not existing shopping list, Invalid product id supplied)", "content": { "application/json": { "schema": { diff --git a/routes.php b/routes.php index 65b378b3..c79a716b 100644 --- a/routes.php +++ b/routes.php @@ -167,7 +167,7 @@ $app->group('/api', function() { $this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $this->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); - $this->post('/stock/shoppinglist/remove-from-list', '\Grocy\Controllers\StockApiController:RemoveFromShoppingList'); + $this->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveItemFromShoppingList'); } // Recipes diff --git a/services/StockService.php b/services/StockService.php index d51a6df9..7bf2cfa2 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -146,7 +146,7 @@ class StockService extends BaseService } } - public function AddProduct(int $productId, float $amount, string $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null) + public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null) { if (!$this->ProductExists($productId)) { @@ -170,10 +170,17 @@ class StockService extends BaseService //Sets the default best before date, if none is supplied if ($bestBeforeDate == null) { - if ($productDetails->product->default_best_before_days == -1) { + if (intval($productDetails->product->default_best_before_days) == -1) + { $bestBeforeDate = date('2999-12-31'); - } else { - $bestBeforeDate = date('Y-m-d', strtotime(date('Y-m-d') . ' + '.$productDetails->product->default_best_before_days.' days')); + } + else if (intval($productDetails->product->default_best_before_days) > 0) + { + $bestBeforeDate = date('Y-m-d', strtotime(date('Y-m-d') . ' + '.$productDetails->product->default_best_before_days.' days')); + } + else + { + $bestBeforeDate = date('Y-m-d'); } } @@ -313,7 +320,7 @@ class StockService extends BaseService } } - public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate, $locationId = null, $price = null) + public function InventoryProduct(int $productId, int $newAmount, $bestBeforeDate, $locationId = null, $price = null) { if (!$this->ProductExists($productId)) { @@ -513,7 +520,9 @@ class StockService extends BaseService { throw new \Exception('Shopping list does not exist'); } + $productRow = $this->Database->shopping_list()->where('product_id = :1', $productId)->fetch(); + //If no entry was found with for this product, we return gracefully if ($productRow != null && !empty($productRow)) { @@ -521,7 +530,9 @@ class StockService extends BaseService if ($newAmount < 1) { $productRow->delete(); - } else { + } + else + { $productRow->update(array('amount' => $newAmount)); }