Make it possible to provide a different location for added product during inventory (closes #183)

This commit is contained in:
Bernd Bestel
2019-04-05 21:26:44 +02:00
parent 886e272c03
commit b6e80580ed
6 changed files with 25 additions and 4 deletions

View File

@@ -171,7 +171,13 @@ class StockApiController extends BaseApiController
$bestBeforeDate = $requestBody['best_before_date']; $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)); return $this->ApiResponse($this->Database->stock_log($bookingId));
} }
catch (\Exception $ex) catch (\Exception $ex)

View File

@@ -48,7 +48,8 @@ class StockController extends BaseController
public function Inventory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Inventory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->AppContainer->view->render($response, 'inventory', [ 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')
]); ]);
} }

View File

@@ -1229,6 +1229,11 @@
"type": "string", "type": "string",
"format": "date", "format": "date",
"description": "The best before date which applies to added products" "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)"
} }
} }
} }

View File

@@ -11,6 +11,7 @@
var jsonData = { }; var jsonData = { };
jsonData.new_amount = jsonForm.new_amount; jsonData.new_amount = jsonForm.new_amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue(); 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, Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/inventory', jsonData,
function(result) function(result)
@@ -112,6 +113,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
$("#tare-weight-handling-info").addClass("d-none"); $("#tare-weight-handling-info").addClass("d-none");
} }
Grocy.Components.LocationPicker.SetId(productDetails.location.id);
$('#new_amount').focus(); $('#new_amount').focus();
}, },
function(xhr) 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))); $('#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.DateTimePicker.GetInputElement().attr('required', '');
Grocy.Components.LocationPicker.GetInputElement().attr('required', '');
} }
else if (newAmount < productStockAmount + containerWeight) 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))); $('#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.DateTimePicker.GetInputElement().removeAttr('required');
Grocy.Components.LocationPicker.GetInputElement().removeAttr('required');
} }
Grocy.FrontendHelpers.ValidateForm('inventory-form'); Grocy.FrontendHelpers.ValidateForm('inventory-form');

View File

@@ -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)) if (!$this->ProductExists($productId))
{ {
@@ -318,7 +318,7 @@ class StockService extends BaseService
$bookingAmount = $newAmount; $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) else if ($newAmount < $productDetails->stock_amount + $containerWeight)
{ {

View File

@@ -45,6 +45,11 @@
'earlierThanInfoText' => $L('The given date is earlier than today, are you sure?') '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'
))
<button id="save-inventory-button" class="btn btn-success">{{ $L('OK') }}</button> <button id="save-inventory-button" class="btn btn-success">{{ $L('OK') }}</button>
</form> </form>