Commit efae5016 authored by Karamel's avatar Karamel
Browse files

Set api/cash/search route.

parent 90baf211
......@@ -4,6 +4,7 @@ use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
use \Pasteque\Server\System\DAO\DAOCondition;
/**
* GET cashIdGet
......@@ -51,30 +52,49 @@ $app->POST('/api/cash', function($request, $response, $args) {
* Output-Formats: [application/json]
*/
$app->GET('/api/cash/search/', function($request, $response, $args) {
$srv = new CashesService();
$db = DB::get();
$ptApp = $this->get('settings')['ptApp'];
$queryParams = $request->getQueryParams();
$cashRegisterId = $queryParams['cashRegisterId'];
$dateStart = $queryParams['dateStart'];
$dateEnd = $queryParams['dateEnd'];
$conditions = array();
$cashRegisterId = (empty($queryParams['cashRegister'])) ? null
: $queryParams['cashRegister'];
// Check mandatory dateStart
$dateStart = (empty($queryParams['dateStart'])) ? null
: DateUtils::readDate($queryParams['dateStart']);
if ($dateStart === null) {
return $response->withStatus(400, 'Missing mandatory dateStart.');
}
if ($dateStart === false) {
return $response->withStatus(400, 'Invalid dateStart.');
}
// Get optional dateStop
$dateStop = (empty($queryParams['dateStop'])) ? null
: DateUtils::readDate(intval($queryParams['dateStop']));
// Check optional cash register
$cashRegister = null;
if ($cashRegisterId !== null) {
$conditions[] = array("cashRegisterId", "=", $cashRegisterId);
$cashRegApiResp = APICaller::run($ptApp, 'cashRegister', 'get',
$cashRegisterId);
if ($cashRegApiResp->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($cashRegResp);
}
$cashRegister = $cashRegApiResp->getContent();
if ($cashRegister === null) {
return $response->withStatus(404, 'Cash register not found');
}
}
if ($dateStart !== null) {
$conditions[] = array("openDate", ">=",
$db->dateVal($dateStart));
// Run search and return
$conditions = array();
if ($cashRegister !== null) {
$conditions[] = new DAOCondition('cashRegister', '=', $cashRegister);
}
if ($dateEnd !== null) {
$conditions[] = array("closeDate", "<=",
$db->dateVal($dateEnd));
$conditions[] = new DAOCondition('openDate', '>=', $dateStart);
if ($dateStop !== null) {
$conditions[] = new DAOCondition('openDate', '<=', $dateStop);
}
return $response->withJson($srv->search($conditions));
// Always exclude non-closed cash because most of the sums are not updated.
$conditions[] = new DAOCondition('closeDate', '!=', null);
$searchResp = APICaller::run($ptApp, 'cashSession', 'search',
[$conditions, null, null, 'openDate']);
return $response->withApiResult($searchResp);
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment