diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 543b995a..78ba58e0 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -177,7 +177,13 @@ class StockApiController extends BaseApiController $locationId = $requestBody['location_id']; } - $bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId); + $price = null; + if (array_key_exists('price', $requestBody) && is_numeric($requestBody['price'])) + { + $price = $requestBody['price']; + } + + $bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price); return $this->ApiResponse($this->Database->stock_log($bookingId)); } catch (\Exception $ex) diff --git a/grocy.openapi.json b/grocy.openapi.json index c7862444..964d614d 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1351,6 +1351,11 @@ "type": "number", "format": "integer", "description": "If omitted, the default location of the product is used (only applies to added products)" + }, + "price": { + "type": "number", + "format": "double", + "description": "If omitted, the last price of the product is used (only applies to added products)" } } } diff --git a/public/viewjs/inventory.js b/public/viewjs/inventory.js index f7a97d7d..1e103f52 100644 --- a/public/viewjs/inventory.js +++ b/public/viewjs/inventory.js @@ -8,10 +8,17 @@ Grocy.Api.Get('stock/products/' + jsonForm.product_id, function(productDetails) { + var price = ""; + if (!jsonForm.price.toString().isEmpty()) + { + price = parseFloat(jsonForm.price).toFixed(2); + } + var jsonData = { }; jsonData.new_amount = jsonForm.new_amount; jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue(); jsonData.location_id = Grocy.Components.LocationPicker.GetValue(); + jsonData.price = price; Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/inventory', jsonData, function(result) @@ -53,6 +60,7 @@ $("#new_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', '0')); $('#new_amount').val(''); $('#new_amount_qu_unit').text(""); + $('#price').val(''); Grocy.Components.DateTimePicker.Clear(); Grocy.Components.ProductPicker.SetValue(''); Grocy.Components.ProductPicker.GetInputElement().focus(); @@ -111,6 +119,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) $("#tare-weight-handling-info").addClass("d-none"); } + $('#price').val(productDetails.last_price); Grocy.Components.LocationPicker.SetId(productDetails.location.id); $('#new_amount').focus(); }, diff --git a/services/StockService.php b/services/StockService.php index 399cac11..deed7afe 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -303,7 +303,7 @@ class StockService extends BaseService } } - public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate, $locationId = null) + public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate, $locationId = null, $price = null) { if (!$this->ProductExists($productId)) { @@ -312,6 +312,11 @@ class StockService extends BaseService $productDetails = (object)$this->GetProductDetails($productId); + if ($price === null) + { + $price = $productDetails->last_price; + } + // Tare weight handling // The given amount is the new total amount including the container weight (gross) // So assume that the amount in stock is the amount also including the container weight @@ -333,7 +338,7 @@ class StockService extends BaseService $bookingAmount = $newAmount; } - $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $productDetails->last_price, $locationId); + $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $price, $locationId); } else if ($newAmount < $productDetails->stock_amount + $containerWeight) { diff --git a/views/inventory.blade.php b/views/inventory.blade.php index 509b7155..c73796bd 100644 --- a/views/inventory.blade.php +++ b/views/inventory.blade.php @@ -45,6 +45,18 @@ 'earlierThanInfoText' => $__t('The given date is earlier than today, are you sure?') )) + @include('components.numberpicker', array( + 'id' => 'price', + 'label' => 'Price', + 'min' => 0, + 'step' => 0.01, + 'value' => '', + 'hint' => $__t('in %s per purchase quantity unit', GROCY_CURRENCY), + 'additionalHtmlContextHelp' => '
' . $__t('This will apply to added products') . '', + 'invalidFeedback' => $__t('The price cannot be lower than %s', '0'), + 'isRequired' => false + )) + @include('components.locationpicker', array( 'locations' => $locations, 'hint' => 'This will apply to added products'