mirror of
				https://github.com/grocy/grocy.git
				synced 2025-11-03 20:15:04 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			747 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			747 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace Grocy\Controllers;
 | 
						|
 | 
						|
use Grocy\Controllers\Users\User;
 | 
						|
use Grocy\Services\StockService;
 | 
						|
 | 
						|
class StockApiController extends BaseApiController
 | 
						|
{
 | 
						|
	public function AddMissingProductsToShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$requestBody = $request->getParsedBody();
 | 
						|
 | 
						|
			$listId = 1;
 | 
						|
 | 
						|
			if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
 | 
						|
			{
 | 
						|
				$listId = intval($requestBody['list_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			$this->getStockService()->AddMissingProductsToShoppingList($listId);
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function AddProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_PURCHASE);
 | 
						|
 | 
						|
		$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'];
 | 
						|
			}
 | 
						|
 | 
						|
			$shoppingLocationId = null;
 | 
						|
 | 
						|
			if (array_key_exists('shopping_location_id', $requestBody) && is_numeric($requestBody['shopping_location_id']))
 | 
						|
			{
 | 
						|
				$shoppingLocationId = $requestBody['shopping_location_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$quFactorPurchaseToStock = null;
 | 
						|
 | 
						|
			if (array_key_exists('qu_factor_purchase_to_stock', $requestBody) && is_numeric($requestBody['qu_factor_purchase_to_stock']))
 | 
						|
			{
 | 
						|
				$quFactorPurchaseToStock = $requestBody['qu_factor_purchase_to_stock'];
 | 
						|
			}
 | 
						|
 | 
						|
			$transactionType = StockService::TRANSACTION_TYPE_PURCHASE;
 | 
						|
 | 
						|
			if (array_key_exists('transaction_type', $requestBody) && !empty($requestBody['transactiontype']))
 | 
						|
			{
 | 
						|
				$transactionType = $requestBody['transactiontype'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $quFactorPurchaseToStock, $locationId, $shoppingLocationId);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function AddProductByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->AddProduct($request, $response, $args);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function AddProductToShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$requestBody = $request->getParsedBody();
 | 
						|
 | 
						|
			$listId = 1;
 | 
						|
			$amount = 1;
 | 
						|
			$productId = null;
 | 
						|
			$note = null;
 | 
						|
 | 
						|
			if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
 | 
						|
			{
 | 
						|
				$listId = intval($requestBody['list_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			if (array_key_exists('product_amount', $requestBody) && !empty($requestBody['product_amount']) && is_numeric($requestBody['product_amount']))
 | 
						|
			{
 | 
						|
				$amount = intval($requestBody['product_amount']);
 | 
						|
			}
 | 
						|
 | 
						|
			if (array_key_exists('product_id', $requestBody) && !empty($requestBody['product_id']) && is_numeric($requestBody['product_id']))
 | 
						|
			{
 | 
						|
				$productId = intval($requestBody['product_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			if (array_key_exists('note', $requestBody) && !empty($requestBody['note']))
 | 
						|
			{
 | 
						|
				$note = $requestBody['note'];
 | 
						|
			}
 | 
						|
 | 
						|
			if ($productId == null)
 | 
						|
			{
 | 
						|
				throw new \Exception('No product id was supplied');
 | 
						|
			}
 | 
						|
 | 
						|
			$this->getStockService()->AddProductToShoppingList($productId, $amount, $note, $listId);
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ClearShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_DELETE);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$requestBody = $request->getParsedBody();
 | 
						|
 | 
						|
			$listId = 1;
 | 
						|
 | 
						|
			if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
 | 
						|
			{
 | 
						|
				$listId = intval($requestBody['list_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			$this->getStockService()->ClearShoppingList($listId);
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ConsumeProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_CONSUME);
 | 
						|
 | 
						|
		$requestBody = $request->getParsedBody();
 | 
						|
 | 
						|
		$result = null;
 | 
						|
 | 
						|
		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'];
 | 
						|
			}
 | 
						|
 | 
						|
			$locationId = null;
 | 
						|
 | 
						|
			if (array_key_exists('location_id', $requestBody) && !empty($requestBody['location_id']) && is_numeric($requestBody['location_id']))
 | 
						|
			{
 | 
						|
				$locationId = $requestBody['location_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$recipeId = null;
 | 
						|
 | 
						|
			if (array_key_exists('recipe_id', $requestBody) && is_numeric($requestBody['recipe_id']))
 | 
						|
			{
 | 
						|
				$recipeId = $requestBody['recipe_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId, $locationId);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			$result = $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
		return $result;
 | 
						|
	}
 | 
						|
 | 
						|
	public function ConsumeProductByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->ConsumeProduct($request, $response, $args);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function CurrentStock(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		return $this->ApiResponse($response, $this->getStockService()->GetCurrentStock());
 | 
						|
	}
 | 
						|
 | 
						|
	public function CurrentVolatileStock(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		$nextXDays = 5;
 | 
						|
 | 
						|
		if (isset($request->getQueryParams()['expiring_days']) && !empty($request->getQueryParams()['expiring_days']) && is_numeric($request->getQueryParams()['expiring_days']))
 | 
						|
		{
 | 
						|
			$nextXDays = $request->getQueryParams()['expiring_days'];
 | 
						|
		}
 | 
						|
 | 
						|
		$expiringProducts = $this->getStockService()->GetExpiringProducts($nextXDays, true);
 | 
						|
		$expiredProducts = $this->getStockService()->GetExpiringProducts(-1);
 | 
						|
		$missingProducts = $this->getStockService()->GetMissingProducts();
 | 
						|
		return $this->ApiResponse($response, [
 | 
						|
			'expiring_products' => $expiringProducts,
 | 
						|
			'expired_products' => $expiredProducts,
 | 
						|
			'missing_products' => $missingProducts
 | 
						|
		]);
 | 
						|
	}
 | 
						|
 | 
						|
	public function EditStockEntry(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_EDIT);
 | 
						|
 | 
						|
		$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'];
 | 
						|
			}
 | 
						|
 | 
						|
			$shoppingLocationId = null;
 | 
						|
 | 
						|
			if (array_key_exists('shopping_location_id', $requestBody) && is_numeric($requestBody['shopping_location_id']))
 | 
						|
			{
 | 
						|
				$shoppingLocationId = $requestBody['shopping_location_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->EditStockEntry($args['entryId'], $requestBody['amount'], $bestBeforeDate, $locationId, $shoppingLocationId, $price, $requestBody['open'], $requestBody['purchased_date'], $requestBody['qu_factor_purchase_to_stock']);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ExternalBarcodeLookup(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$addFoundProduct = false;
 | 
						|
 | 
						|
			if (isset($request->getQueryParams()['add']) && ($request->getQueryParams()['add'] === 'true' || $request->getQueryParams()['add'] === 1))
 | 
						|
			{
 | 
						|
				$addFoundProduct = true;
 | 
						|
			}
 | 
						|
 | 
						|
			return $this->ApiResponse($response, $this->getStockService()->ExternalBarcodeLookup($args['barcode'], $addFoundProduct));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function InventoryProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_INVENTORY);
 | 
						|
 | 
						|
		$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('new_amount', $requestBody))
 | 
						|
			{
 | 
						|
				throw new \Exception('An new amount is required');
 | 
						|
			}
 | 
						|
 | 
						|
			$bestBeforeDate = null;
 | 
						|
 | 
						|
			if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date']))
 | 
						|
			{
 | 
						|
				$bestBeforeDate = $requestBody['best_before_date'];
 | 
						|
			}
 | 
						|
 | 
						|
			$locationId = null;
 | 
						|
 | 
						|
			if (array_key_exists('location_id', $requestBody) && is_numeric($requestBody['location_id']))
 | 
						|
			{
 | 
						|
				$locationId = $requestBody['location_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$price = null;
 | 
						|
 | 
						|
			if (array_key_exists('price', $requestBody) && is_numeric($requestBody['price']))
 | 
						|
			{
 | 
						|
				$price = $requestBody['price'];
 | 
						|
			}
 | 
						|
 | 
						|
			$shoppingLocationId = null;
 | 
						|
 | 
						|
			if (array_key_exists('shopping_location_id', $requestBody) && is_numeric($requestBody['shopping_location_id']))
 | 
						|
			{
 | 
						|
				$shoppingLocationId = $requestBody['shopping_location_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price, $shoppingLocationId);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function InventoryProductByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->InventoryProduct($request, $response, $args);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function OpenProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_OPEN);
 | 
						|
 | 
						|
		$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');
 | 
						|
			}
 | 
						|
 | 
						|
			$specificStockEntryId = 'default';
 | 
						|
 | 
						|
			if (array_key_exists('stock_entry_id', $requestBody) && !empty($requestBody['stock_entry_id']))
 | 
						|
			{
 | 
						|
				$specificStockEntryId = $requestBody['stock_entry_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->OpenProduct($args['productId'], $requestBody['amount'], $specificStockEntryId);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function OpenProductByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->OpenProduct($request, $response, $args);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductBarcodeDetails(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->product_barcodes()->where('barcode = :1', $args['barcode'])->fetch());
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductDetails(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			return $this->ApiResponse($response, $this->getStockService()->GetProductDetails($args['productId']));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductDetailsByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$productId = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->ApiResponse($response, $this->getStockService()->GetProductDetails($productId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductPriceHistory(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			return $this->ApiResponse($response, $this->getStockService()->GetProductPriceHistory($args['productId']));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductStockEntries(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		$allowSubproductSubstitution = false;
 | 
						|
 | 
						|
		if (isset($request->getQueryParams()['include_sub_products']) && filter_var($request->getQueryParams()['include_sub_products'], FILTER_VALIDATE_BOOLEAN))
 | 
						|
		{
 | 
						|
			$allowSubproductSubstitution = true;
 | 
						|
		}
 | 
						|
 | 
						|
		return $this->ApiResponse($response, $this->getStockService()->GetProductStockEntries($args['productId'], false, $allowSubproductSubstitution));
 | 
						|
	}
 | 
						|
 | 
						|
	public function ProductStockLocations(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		return $this->ApiResponse($response, $this->getStockService()->GetProductStockLocations($args['productId']));
 | 
						|
	}
 | 
						|
 | 
						|
	public function RemoveProductFromShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_DELETE);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$requestBody = $request->getParsedBody();
 | 
						|
 | 
						|
			$listId = 1;
 | 
						|
			$amount = 1;
 | 
						|
			$productId = null;
 | 
						|
 | 
						|
			if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
 | 
						|
			{
 | 
						|
				$listId = intval($requestBody['list_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			if (array_key_exists('product_amount', $requestBody) && !empty($requestBody['product_amount']) && is_numeric($requestBody['product_amount']))
 | 
						|
			{
 | 
						|
				$amount = intval($requestBody['product_amount']);
 | 
						|
			}
 | 
						|
 | 
						|
			if (array_key_exists('product_id', $requestBody) && !empty($requestBody['product_id']) && is_numeric($requestBody['product_id']))
 | 
						|
			{
 | 
						|
				$productId = intval($requestBody['product_id']);
 | 
						|
			}
 | 
						|
 | 
						|
			if ($productId == null)
 | 
						|
			{
 | 
						|
				throw new \Exception('No product id was supplied');
 | 
						|
			}
 | 
						|
 | 
						|
			$this->getStockService()->RemoveProductFromShoppingList($productId, $amount, $listId);
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function StockBooking(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$stockLogRow = $this->getDatabase()->stock_log($args['bookingId']);
 | 
						|
 | 
						|
			if ($stockLogRow === null)
 | 
						|
			{
 | 
						|
				throw new \Exception('Stock booking does not exist');
 | 
						|
			}
 | 
						|
 | 
						|
			return $this->ApiResponse($response, $stockLogRow);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function StockEntry(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		return $this->ApiResponse($response, $this->getStockService()->GetStockEntry($args['entryId']));
 | 
						|
	}
 | 
						|
 | 
						|
	public function StockTransactions(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$transactionRows = $this->getDatabase()->stock_log()->where('transaction_id = :1', $args['transactionId'])->fetchAll();
 | 
						|
 | 
						|
			if (count($transactionRows) === 0)
 | 
						|
			{
 | 
						|
				throw new \Exception('No transaction was found by the given transaction id');
 | 
						|
			}
 | 
						|
 | 
						|
			return $this->ApiResponse($response, $transactionRows);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function TransferProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_TRANSFER);
 | 
						|
 | 
						|
		$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');
 | 
						|
			}
 | 
						|
 | 
						|
			if (!array_key_exists('location_id_from', $requestBody))
 | 
						|
			{
 | 
						|
				throw new \Exception('A transfer from location is required');
 | 
						|
			}
 | 
						|
 | 
						|
			if (!array_key_exists('location_id_to', $requestBody))
 | 
						|
			{
 | 
						|
				throw new \Exception('A transfer to location is required');
 | 
						|
			}
 | 
						|
 | 
						|
			$specificStockEntryId = 'default';
 | 
						|
 | 
						|
			if (array_key_exists('stock_entry_id', $requestBody) && !empty($requestBody['stock_entry_id']))
 | 
						|
			{
 | 
						|
				$specificStockEntryId = $requestBody['stock_entry_id'];
 | 
						|
			}
 | 
						|
 | 
						|
			$bookingId = $this->getStockService()->TransferProduct($args['productId'], $requestBody['amount'], $requestBody['location_id_from'], $requestBody['location_id_to'], $specificStockEntryId);
 | 
						|
			return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId));
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function TransferProductByBarcode(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
 | 
						|
			return $this->TransferProduct($request, $response, $args);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function UndoBooking(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_EDIT);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$this->ApiResponse($response, $this->getStockService()->UndoBooking($args['bookingId']));
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function UndoTransaction(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
 | 
						|
	{
 | 
						|
		User::checkPermission($request, User::PERMISSION_STOCK_EDIT);
 | 
						|
 | 
						|
		try
 | 
						|
		{
 | 
						|
			$this->ApiResponse($response, $this->getStockService()->UndoTransaction($args['transactionId']));
 | 
						|
			return $this->EmptyApiResponse($response);
 | 
						|
		}
 | 
						|
		catch (\Exception $ex)
 | 
						|
		{
 | 
						|
			return $this->GenericErrorResponse($response, $ex->getMessage());
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
 | 
						|
	public function __construct(\DI\Container $container)
 | 
						|
	{
 | 
						|
		parent::__construct($container);
 | 
						|
	}
 | 
						|
 | 
						|
}
 |