Commit f366dd17 authored by Karamel's avatar Karamel
Browse files

Ticket search route.

parent fd6cc249
......@@ -32,6 +32,79 @@ $app->GET('/api/ticket/{cashregister}/{number}', function ($request, $response,
return $response->withApiResult($apiRes);
});
$app->GET('/api/ticket/search', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$queryParams = $request->getQueryParams();
// Get input
$cashRegisterId = (empty($queryParams['cashRegister'])) ? null
: $queryParams['cashRegister'];
$dateStart = (empty($queryParams['dateStart'])) ? null
: DateUtils::readDate(intval($queryParams['dateStart']));
$dateStop = (empty($queryParams['dateStop'])) ? null
: DateUtils::readDate(intval($queryParams['dateStop']));
$userId = (empty($queryParams['user'])) ? null
: $queryParams['user'];
$customerId = (empty($queryParams['customer'])) ? null
: $queryParams['customer'];
// Search criterias
if ($dateStart === false) {
return $response->withApiResult(APIResult::reject('Invalid dateStart'));
}
if ($dateStop === false) {
return $response->withApiResult(APIResult::reject('Invalid dateStop'));
}
$cashRegister = null;
if ($cashRegisterId !== null) {
$cashRegRes = APICaller::run($ptApp, 'cashRegister', 'get', $cashRegisterId);
if ($cashRegRes->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($cashRegRes);
}
$cashRegister = $cashRegRes->getContent();
if ($cashRegister === null) {
return $response->withStatus(404, 'Cash register not found');
}
}
$customer = null;
if ($customerId !== null) {
$custRes = APICaller::run($ptApp, 'customer', 'get', $customerId);
if ($custRes->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($custRes);
}
$customer = $custRes->getContent();
if ($customer === null) {
return $response->withStatus(404, 'Customer not found');
}
}
$user = null;
if ($userId !== null) {
$userRes = APICaller::run($ptApp, 'user', 'get', $userId);
if ($userRes->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($userRes);
}
$user = $userRes->getContent();
if ($user === null) {
return $response->withStatus(404, 'User not found');
}
}
$conditions = [];
if ($cashRegister !== null) {
$conditions[] = new DAOCondition('cashRegister', '=', $cashRegister);
}
if ($dateStart !== null) {
$conditions[] = new DAOCondition('date', '>', $dateStart);
}
if ($dateStop !== null) {
$conditions[] = new DAOCondition('date', '<', $dateStop);
}
if ($user !== null) {
$conditions[] = new DAOCondition('user', '=', $user);
}
if ($customer !== null) {
$conditions[] = new DAOCondition('customer', '=', $customer);
}
$tktRes = APICaller::run($ptApp, 'ticket', 'search', [$conditions]);
return $response->withApiResult($tktRes);
});
/** Get tickets from a session. */
$app->GET('/api/ticket/session/{cashregister}/{sequence}', function ($request, $response, $args) {
......
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