Only consider stock amount at the given location on consume, if supplied

This commit is contained in:
Bernd Bestel 2019-11-04 19:17:39 +01:00 committed by Kurt Riddlesperger
parent 8c18a9eb1d
commit 903ebc7d54
3 changed files with 34 additions and 5 deletions

View File

@ -201,13 +201,19 @@ class StockApiController extends BaseApiController
$specificStockEntryId = $requestBody['stock_entry_id']; $specificStockEntryId = $requestBody['stock_entry_id'];
} }
$locationId = null;
if (array_key_exists('location_id', $requestBody) && !empty($requestBody['location_id']) && is_numeric($requestBody['location_id']))
{
$locationId = $requestBody['location_id'];
}
$recipeId = null; $recipeId = null;
if (array_key_exists('recipe_id', $requestBody) && is_numeric($requestBody['recipe_id'])) if (array_key_exists('recipe_id', $requestBody) && is_numeric($requestBody['recipe_id']))
{ {
$recipeId = $requestBody['recipe_id']; $recipeId = $requestBody['recipe_id'];
} }
$bookingId = $this->StockService->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId); $bookingId = $this->StockService->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId, $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

@ -1385,6 +1385,11 @@
"type": "number", "type": "number",
"format": "integer", "format": "integer",
"description": "A valid recipe id for which this product was used (for statistical purposes only)" "description": "A valid recipe id for which this product was used (for statistical purposes only)"
},
"location_id": {
"type": "number",
"format": "integer",
"description": "A valid location id (if supplied, only stock at the given location is considered, if ommitted, stock of any location is considered)"
} }
}, },
"example": { "example": {
@ -1802,6 +1807,11 @@
"type": "number", "type": "number",
"format": "integer", "format": "integer",
"description": "A valid recipe id for which this product was used (for statistical purposes only)" "description": "A valid recipe id for which this product was used (for statistical purposes only)"
},
"location_id": {
"type": "number",
"format": "integer",
"description": "A valid location id (if supplied, only stock at the given location is considered, if ommitted, stock of any location is considered)"
} }
}, },
"example": { "example": {

View File

@ -264,13 +264,18 @@ class StockService extends BaseService
} }
} }
public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null) public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null)
{ {
if (!$this->ProductExists($productId)) if (!$this->ProductExists($productId))
{ {
throw new \Exception('Product does not exist'); throw new \Exception('Product does not exist');
} }
if ($locationId !== null & !$this->LocationExists($locationId))
{
throw new \Exception('Location does not exist');
}
// Tare weight handling // Tare weight handling
// The given amount is the new total amount including the container weight (gross) // The given amount is the new total amount including the container weight (gross)
// The amount to be posted needs to be the absolute value of the given amount - stock amount - tare weight // The amount to be posted needs to be the absolute value of the given amount - stock amount - tare weight
@ -287,12 +292,20 @@ class StockService extends BaseService
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION) if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
{ {
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount'); if ($locationId === null) // Consume from any location
$potentialStockEntries = $this->GetProductStockEntries($productId); {
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount');
$potentialStockEntries = $this->GetProductStockEntries($productId);
}
else // Consume only from the supplied location
{
$productStockAmount = $this->Database->stock()->where('product_id = :1 AND location_id = :2', $productId, $locationId)->sum('amount');
$potentialStockEntries = $this->GetProductStockEntriesForLocation($productId, $locationId);
}
if ($amount > $productStockAmount) if ($amount > $productStockAmount)
{ {
throw new \Exception('Amount to be consumed cannot be > current stock amount'); throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)');
} }
if ($specificStockEntryId !== 'default') if ($specificStockEntryId !== 'default')