Reuse existing routes for the by-barcode API routes and complete them (references #331)

This commit is contained in:
Bernd Bestel
2019-09-20 10:45:58 +02:00
parent 58890f6bec
commit ca9b8d068a
4 changed files with 183 additions and 93 deletions

View File

@@ -102,48 +102,10 @@ class StockApiController extends BaseApiController
public function AddProductByBarcode(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$requestBody = $request->getParsedBody();
try
{
if ($requestBody === null)
{
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
}
if (!array_key_exists('amount', $requestBody))
{
throw new \Exception('An amount is required');
}
$bestBeforeDate = null;
if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date']))
{
$bestBeforeDate = $requestBody['best_before_date'];
}
$price = null;
if (array_key_exists('price', $requestBody) && is_numeric($requestBody['price']))
{
$price = $requestBody['price'];
}
$locationId = null;
if (array_key_exists('location_id', $requestBody) && is_numeric($requestBody['location_id']))
{
$locationId = $requestBody['location_id'];
}
$transactionType = StockService::TRANSACTION_TYPE_PURCHASE;
if (array_key_exists('transaction_type', $requestBody) && !empty($requestBody['transactiontype']))
{
$transactionType = $requestBody['transactiontype'];
}
$productId = $this->StockService->GetProductIdFromBarcode($args['barcode']);
$bookingId = $this->StockService->AddProduct($productId, $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $locationId);
return $this->ApiResponse($this->Database->stock_log($bookingId));
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']);
return $this->AddProduct($request, $response, $args);
}
catch (\Exception $ex)
{
@@ -202,48 +164,10 @@ class StockApiController extends BaseApiController
public function ConsumeProductByBarcode(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$requestBody = $request->getParsedBody();
try
{
if ($requestBody === null)
{
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
}
if (!array_key_exists('amount', $requestBody))
{
throw new \Exception('An amount is required');
}
$spoiled = false;
if (array_key_exists('spoiled', $requestBody))
{
$spoiled = $requestBody['spoiled'];
}
$transactionType = StockService::TRANSACTION_TYPE_CONSUME;
if (array_key_exists('transaction_type', $requestBody) && !empty($requestBody['transactiontype']))
{
$transactionType = $requestBody['transactiontype'];
}
$specificStockEntryId = 'default';
if (array_key_exists('stock_entry_id', $requestBody) && !empty($requestBody['stock_entry_id']))
{
$specificStockEntryId = $requestBody['stock_entry_id'];
}
$recipeId = null;
if (array_key_exists('recipe_id', $requestBody) && is_numeric($requestBody['recipe_id']))
{
$recipeId = $requestBody['recipe_id'];
}
$productId = $this->StockService->GetProductIdFromBarcode($args['barcode']);
$bookingId = $this->StockService->ConsumeProduct($productId, $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId);
return $this->ApiResponse($this->Database->stock_log($bookingId));
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']);
return $this->ConsumeProduct($request, $response, $args);
}
catch (\Exception $ex)
{
@@ -294,6 +218,19 @@ class StockApiController extends BaseApiController
}
}
public function InventoryProductByBarcode(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try
{
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']);
return $this->InventoryProduct($request, $response, $args);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function OpenProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$requestBody = $request->getParsedBody();
@@ -325,6 +262,19 @@ class StockApiController extends BaseApiController
}
}
public function OpenProductByBarcode(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try
{
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']);
return $this->OpenProduct($request, $response, $args);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiResponse($this->StockService->GetCurrentStock());