From c22496ca7cd6058a7b4c3a43a07235113e7b5194 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 21 Jan 2020 20:04:33 +0100 Subject: [PATCH] Fixed /stockedit opened checkbox (references #506) --- controllers/StockApiController.php | 4 ++-- grocy.openapi.json | 7 ++++++- public/viewjs/stockedit.js | 3 +++ routes.php | 2 +- services/StockService.php | 14 +++++++------- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 133e5cd9..2f1b682e 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -113,7 +113,7 @@ class StockApiController extends BaseApiController } } - public function EditStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + public function EditStockEntry(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { $requestBody = $request->getParsedBody(); @@ -152,7 +152,7 @@ class StockApiController extends BaseApiController $locationId = $requestBody['location_id']; } - $bookingId = $this->StockService->EditStock($requestBody['id'], $requestBody['amount'], $bestBeforeDate, $locationId, $price); + $bookingId = $this->StockService->EditStockEntry($requestBody['id'], $requestBody['amount'], $bestBeforeDate, $locationId, $price, $requestBody['open']); return $this->ApiResponse($this->Database->stock_log($bookingId)); } catch (\Exception $ex) diff --git a/grocy.openapi.json b/grocy.openapi.json index d10a4b4a..66214e02 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1087,6 +1087,10 @@ "type": "number", "format": "number", "description": "The price per purchase quantity unit in configured currency" + }, + "open": { + "type": "boolean", + "description": "If the stock entry was already opened or not" } }, "example": { @@ -1094,7 +1098,8 @@ "amount": 1, "best_before_date": "2019-01-19", "location_id": 2, - "price": "1.99" + "price": "1.99", + "open": false } } } diff --git a/public/viewjs/stockedit.js b/public/viewjs/stockedit.js index 109d092a..ef1790d7 100644 --- a/public/viewjs/stockedit.js +++ b/public/viewjs/stockedit.js @@ -23,6 +23,8 @@ } jsonData.price = price; + jsonData.open = $("#open").is(":checked"); + var stockRowId = GetUriParam('stockRowId'); jsonData.id = stockRowId; @@ -88,6 +90,7 @@ Grocy.Api.Get("stock/" + stockRowId + "/entry", Grocy.Components.LocationPicker.SetId(stockEntry.location_id); $('#amount').val(stockEntry.amount); $('#price').val(stockEntry.price); + $("#open").prop('checked', BoolVal(stockEntry.open)); Grocy.Components.DateTimePicker.SetValue(stockEntry.best_before_date); Grocy.Api.Get('stock/products/' + stockEntry.product_id, diff --git a/routes.php b/routes.php index 679b11fc..3032605b 100644 --- a/routes.php +++ b/routes.php @@ -162,7 +162,7 @@ $app->group('/api', function() { $this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock'); $this->get('/stock/{entryId}/entry', '\Grocy\Controllers\StockApiController:StockEntry'); - $this->put('/stock', '\Grocy\Controllers\StockApiController:EditStock'); + $this->put('/stock', '\Grocy\Controllers\StockApiController:EditStockEntry'); $this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatileStock'); $this->get('/stock/products/{productId}', '\Grocy\Controllers\StockApiController:ProductDetails'); $this->get('/stock/products/{productId}/entries', '\Grocy\Controllers\StockApiController:ProductStockEntries'); diff --git a/services/StockService.php b/services/StockService.php index a7dded39..79d8cf3e 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -562,7 +562,7 @@ class StockService extends BaseService return $this->Database->lastInsertId(); } - public function EditStock(int $stockRowId, int $amount, $bestBeforeDate, $locationId, $price) + public function EditStockEntry(int $stockRowId, int $amount, $bestBeforeDate, $locationId, $price, $open) { $stockRow = $this->Database->stock()->where('id = :1', $stockRowId)->fetch(); @@ -572,8 +572,8 @@ class StockService extends BaseService throw new \Exception('Stock does not exist'); } - $correlationId = uniqid(); - $transactionId = uniqid(); + $correlationId = uniqid(); + $transactionId = uniqid(); $logOldRowForStockUpdate = $this->Database->stock_log()->createRow(array( 'product_id' => $stockRow->product_id, 'amount' => $stockRow->amount, @@ -594,7 +594,8 @@ class StockService extends BaseService 'amount' => $amount, 'price' => $price, 'best_before_date' => $bestBeforeDate, - 'location_id' => $locationId + 'location_id' => $locationId, + 'open' => $open )); $logNewRowForStockUpdate = $this->Database->stock_log()->createRow(array( @@ -613,10 +614,9 @@ class StockService extends BaseService )); $logNewRowForStockUpdate->save(); - $returnValue = $this->Database->lastInsertId(); - - return $returnValue; + return $this->Database->lastInsertId(); } + public function InventoryProduct(int $productId, float $newAmount, $bestBeforeDate, $locationId = null, $price = null) { if (!$this->ProductExists($productId))