Commit fd6cc249 authored by Karamel's avatar Karamel
Browse files

Restore CustomerAPI->getTop, add route, include in Sync. Struct expireDate format.

parent 900ca5a1
......@@ -33,7 +33,8 @@ $app->GET('/api/customer/getAll', function($request, $response, $args) {
* Output-Formats: [application/json]
*/
$app->GET('/api/customer/getTop', function($request, $response, $args) {
return $response->withAPIResult(APIResult::error('Not implemented yet'));
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'customer', 'getTop'));
});
......
......@@ -26,4 +26,26 @@ namespace Pasteque\Server\API;
class CustomerAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\Customer';
public function getTop($limit = 10) {
// This is a report, but there are no reports.
// So it's uglily hardcoded until then.
$em = $this->dao->getEntityManager();
$now = new \DateTime();
$q = $em->createQuery('select t, count(t.id) '
. 'from \Pasteque\Server\Model\Ticket t '
. 'join t.customer c '
. 'where c.visible = true '
. 'and (c.expireDate is null or c.expireDate > :now) '
. 'group by c.id');
$q->setParameter('now', $now);
$q->setMaxResults($limit);
$top = $q->getResult();
$result = [];
foreach ($top as $ticket) {
$customer = $ticket[0]->getCustomer();
$result[] = $customer;
}
return $result;
}
}
\ No newline at end of file
......@@ -83,6 +83,13 @@ class SyncAPI implements API {
$result->set('tariffareas', $this->tariffAreaAPI->getAll());
$result->set('discounts', $this->discountAPI->getAll());
$result->set('customers', $this->customerAPI->getAll());
// Set top customers as id only not to duplicate data
$topCust = $this->customerAPI->getTop();
$top = [];
foreach ($topCust as $cust) {
$top[] = $cust->getId();
}
$result->set('topcustomers', $top);
$result->set('discountprofiles', $this->discountProfileAPI->getAll());
$result->set('resources', $this->resourceAPI->getAll());
return $result;
......
......@@ -21,6 +21,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DoctrineModel;
/**
......@@ -340,6 +341,8 @@ class Customer extends DoctrineModel
$struct = parent::toStruct();
$struct['number'] = $this->getId();
$struct['key'] = sprintf('%d-%s', $this->getId(), $this->getDispName());
$struct['expireDate'] = DateUtils::toTimestamp($this->getExpireDate());
return $struct;
}
}
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