mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 01:32:38 +00:00
212 lines
7.0 KiB
PHP
212 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
use \Grocy\Services\StockService;
|
|
|
|
class StockApiController extends BaseApiController
|
|
{
|
|
public function __construct(\Slim\Container $container)
|
|
{
|
|
parent::__construct($container);
|
|
$this->StockService = new StockService();
|
|
}
|
|
|
|
protected $StockService;
|
|
|
|
public function ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
try
|
|
{
|
|
return $this->ApiResponse($this->StockService->GetProductDetails($args['productId']));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ProductPriceHistory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
try
|
|
{
|
|
return $this->ApiResponse($this->StockService->GetProductPriceHistory($args['productId']));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function AddProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$bestBeforeDate = date('Y-m-d');
|
|
if (isset($request->getQueryParams()['bestbeforedate']) && !empty($request->getQueryParams()['bestbeforedate']) && IsIsoDate($request->getQueryParams()['bestbeforedate']))
|
|
{
|
|
$bestBeforeDate = $request->getQueryParams()['bestbeforedate'];
|
|
}
|
|
|
|
$price = null;
|
|
if (isset($request->getQueryParams()['price']) && !empty($request->getQueryParams()['price']) && is_numeric($request->getQueryParams()['price']))
|
|
{
|
|
$price = $request->getQueryParams()['price'];
|
|
}
|
|
|
|
$transactionType = StockService::TRANSACTION_TYPE_PURCHASE;
|
|
if (isset($request->getQueryParams()['transactiontype']) && !empty($request->getQueryParams()['transactiontype']))
|
|
{
|
|
$transactionType = $request->getQueryParams()['transactiontype'];
|
|
}
|
|
|
|
try
|
|
{
|
|
$bookingId = $this->StockService->AddProduct($args['productId'], $args['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price);
|
|
return $this->ApiResponse(array('booking_id' => $bookingId));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ConsumeProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$spoiled = false;
|
|
if (isset($request->getQueryParams()['spoiled']) && !empty($request->getQueryParams()['spoiled']) && $request->getQueryParams()['spoiled'] == '1')
|
|
{
|
|
$spoiled = true;
|
|
}
|
|
|
|
$transactionType = StockService::TRANSACTION_TYPE_CONSUME;
|
|
if (isset($request->getQueryParams()['transactiontype']) && !empty($request->getQueryParams()['transactiontype']))
|
|
{
|
|
$transactionType = $request->getQueryParams()['transactiontype'];
|
|
}
|
|
|
|
$specificStockEntryId = "default";
|
|
if (isset($request->getQueryParams()['stock_entry_id']) && !empty($request->getQueryParams()['stock_entry_id']))
|
|
{
|
|
$specificStockEntryId = $request->getQueryParams()['stock_entry_id'];
|
|
}
|
|
|
|
try
|
|
{
|
|
$bookingId = $this->StockService->ConsumeProduct($args['productId'], $args['amount'], $spoiled, $transactionType, $specificStockEntryId);
|
|
return $this->ApiResponse(array('booking_id' => $bookingId));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function InventoryProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$bestBeforeDate = date('Y-m-d');
|
|
if (isset($request->getQueryParams()['bestbeforedate']) && !empty($request->getQueryParams()['bestbeforedate']) && IsIsoDate($request->getQueryParams()['bestbeforedate']))
|
|
{
|
|
$bestBeforeDate = $request->getQueryParams()['bestbeforedate'];
|
|
}
|
|
|
|
try
|
|
{
|
|
$bookingId = $this->StockService->InventoryProduct($args['productId'], $args['newAmount'], $bestBeforeDate);
|
|
return $this->ApiResponse(array('booking_id' => $bookingId));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function OpenProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$specificStockEntryId = "default";
|
|
if (isset($request->getQueryParams()['stock_entry_id']) && !empty($request->getQueryParams()['stock_entry_id']))
|
|
{
|
|
$specificStockEntryId = $request->getQueryParams()['stock_entry_id'];
|
|
}
|
|
|
|
try
|
|
{
|
|
$bookingId = $this->StockService->OpenProduct($args['productId'], $args['amount'], $specificStockEntryId);
|
|
return $this->ApiResponse(array('booking_id' => $bookingId));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
return $this->ApiResponse($this->StockService->GetCurrentStock());
|
|
}
|
|
|
|
public function CurrentVolatilStock(\Slim\Http\Request $request, \Slim\Http\Response $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->StockService->GetExpiringProducts($nextXDays, true);
|
|
$expiredProducts = $this->StockService->GetExpiringProducts(-1);
|
|
$missingProducts = $this->StockService->GetMissingProducts();
|
|
return $this->ApiResponse(array(
|
|
'expiring_products' => $expiringProducts,
|
|
'expired_products' => $expiredProducts,
|
|
'missing_products' => $missingProducts
|
|
));
|
|
}
|
|
|
|
public function AddMissingProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$this->StockService->AddMissingProductsToShoppingList();
|
|
return $this->VoidApiActionResponse($response);
|
|
}
|
|
|
|
public function ClearShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
$this->StockService->ClearShoppingList();
|
|
return $this->VoidApiActionResponse($response);
|
|
}
|
|
|
|
public function ExternalBarcodeLookup(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
try
|
|
{
|
|
$addFoundProduct = false;
|
|
if (isset($request->getQueryParams()['add']) && ($request->getQueryParams()['add'] === 'true' || $request->getQueryParams()['add'] === 1))
|
|
{
|
|
$addFoundProduct = true;
|
|
}
|
|
|
|
return $this->ApiResponse($this->StockService->ExternalBarcodeLookup($args['barcode'], $addFoundProduct));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function UndoBooking(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
try
|
|
{
|
|
$this->ApiResponse($this->StockService->UndoBooking($args['bookingId']));
|
|
return $this->ApiResponse(array('success' => true));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ProductStockEntries(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
|
{
|
|
return $this->ApiResponse($this->StockService->GetProductStockEntries($args['productId']));
|
|
}
|
|
}
|