diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index fb54640a..01097e47 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -171,7 +171,13 @@ class StockApiController extends BaseApiController $bestBeforeDate = $requestBody['best_before_date']; } - $bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate); + $locationId = null; + if (array_key_exists('location_id', $requestBody) && is_numeric($requestBody['location_id'])) + { + $locationId = $requestBody['location_id']; + } + + $bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId); return $this->ApiResponse($this->Database->stock_log($bookingId)); } catch (\Exception $ex) diff --git a/controllers/StockController.php b/controllers/StockController.php index a602c9f5..a82535e6 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -48,7 +48,8 @@ class StockController extends BaseController public function Inventory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { return $this->AppContainer->view->render($response, 'inventory', [ - 'products' => $this->Database->products()->orderBy('name') + 'products' => $this->Database->products()->orderBy('name'), + 'locations' => $this->Database->locations()->orderBy('name') ]); } diff --git a/grocy.openapi.json b/grocy.openapi.json index 5927f4c0..f7830b35 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1229,6 +1229,11 @@ "type": "string", "format": "date", "description": "The best before date which applies to added products" + }, + "location_id": { + "type": "number", + "format": "integer", + "description": "If omitted, the default location of the product is used (only applies to added products)" } } } diff --git a/public/viewjs/inventory.js b/public/viewjs/inventory.js index 9882b24c..f8b31bdd 100644 --- a/public/viewjs/inventory.js +++ b/public/viewjs/inventory.js @@ -11,6 +11,7 @@ var jsonData = { }; jsonData.new_amount = jsonForm.new_amount; jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue(); + jsonData.location_id = Grocy.Components.LocationPicker.GetValue(); Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/inventory', jsonData, function(result) @@ -112,6 +113,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) $("#tare-weight-handling-info").addClass("d-none"); } + Grocy.Components.LocationPicker.SetId(productDetails.location.id); $('#new_amount').focus(); }, function(xhr) @@ -212,11 +214,13 @@ $('#new_amount').on('keyup', function(e) { $('#inventory-change-info').text(L('This means #1 will be added to stock', estimatedBookingAmount.toLocaleString() + ' ' + Pluralize(estimatedBookingAmount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural))); Grocy.Components.DateTimePicker.GetInputElement().attr('required', ''); + Grocy.Components.LocationPicker.GetInputElement().attr('required', ''); } else if (newAmount < productStockAmount + containerWeight) { $('#inventory-change-info').text(L('This means #1 will be removed from stock', estimatedBookingAmount.toLocaleString() + ' ' + Pluralize(estimatedBookingAmount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural))); Grocy.Components.DateTimePicker.GetInputElement().removeAttr('required'); + Grocy.Components.LocationPicker.GetInputElement().removeAttr('required'); } Grocy.FrontendHelpers.ValidateForm('inventory-form'); diff --git a/services/StockService.php b/services/StockService.php index 52678335..cb3cd92a 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -292,7 +292,7 @@ class StockService extends BaseService } } - public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate) + public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate, $locationId = null) { if (!$this->ProductExists($productId)) { @@ -318,7 +318,7 @@ class StockService extends BaseService $bookingAmount = $newAmount; } - $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $productDetails->last_price); + $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $productDetails->last_price, $locationId); } else if ($newAmount < $productDetails->stock_amount + $containerWeight) { diff --git a/views/inventory.blade.php b/views/inventory.blade.php index 6e728f47..32fcf12e 100644 --- a/views/inventory.blade.php +++ b/views/inventory.blade.php @@ -45,6 +45,11 @@ 'earlierThanInfoText' => $L('The given date is earlier than today, are you sure?') )) + @include('components.locationpicker', array( + 'locations' => $locations, + 'hint' => 'This will apply to added products' + )) +