Move the customer list to the customer handler service. #270
This commit is contained in:
parent
3a33a16cc5
commit
fb6823d25c
4 changed files with 209 additions and 97 deletions
|
|
@ -45,99 +45,24 @@ class CustomerController extends Controller
|
||||||
return $this->render($template);
|
return $this->render($template);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rows(Request $req)
|
public function rows(Request $req, CustomerHandlerInterface $cust_handler)
|
||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted('customer.list', null, 'No access.');
|
$this->denyAccessUnlessGranted('customer.list', null, 'No access.');
|
||||||
|
|
||||||
// build query
|
$params = $cust_handler->getCustomers($req);
|
||||||
$tqb = $this->getDoctrine()
|
|
||||||
->getRepository(Customer::class)
|
|
||||||
->createQueryBuilder('q');
|
|
||||||
|
|
||||||
$qb = $this->getDoctrine()
|
|
||||||
->getRepository(Customer::class)
|
|
||||||
->createQueryBuilder('q');
|
|
||||||
|
|
||||||
// get datatable params
|
|
||||||
$datatable = $req->request->get('datatable');
|
|
||||||
|
|
||||||
// count total records
|
|
||||||
$tquery = $tqb->select('COUNT(q)');
|
|
||||||
|
|
||||||
// add filters to count query
|
|
||||||
$this->setQueryFilters($datatable, $tquery);
|
|
||||||
|
|
||||||
$total = $tquery->getQuery()
|
|
||||||
->getSingleScalarResult();
|
|
||||||
|
|
||||||
// get current page number
|
|
||||||
$page = $datatable['pagination']['page'] ?? 1;
|
|
||||||
|
|
||||||
$perpage = $datatable['pagination']['perpage'];
|
|
||||||
$offset = ($page - 1) * $perpage;
|
|
||||||
|
|
||||||
// add metadata
|
|
||||||
$meta = [
|
|
||||||
'page' => $page,
|
|
||||||
'perpage' => $perpage,
|
|
||||||
'pages' => ceil($total / $perpage),
|
|
||||||
'total' => $total,
|
|
||||||
'sort' => 'asc',
|
|
||||||
'field' => 'id'
|
|
||||||
];
|
|
||||||
|
|
||||||
// build query
|
|
||||||
$query = $qb->select('q');
|
|
||||||
|
|
||||||
// add filters to query
|
|
||||||
$this->setQueryFilters($datatable, $query);
|
|
||||||
|
|
||||||
// check if sorting is present, otherwise use default
|
|
||||||
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
|
|
||||||
$order = $datatable['sort']['sort'] ?? 'asc';
|
|
||||||
$query->orderBy('q.' . $datatable['sort']['field'], $order);
|
|
||||||
} else {
|
|
||||||
$query->orderBy('q.first_name', 'asc');
|
|
||||||
}
|
|
||||||
|
|
||||||
// get rows for this page
|
|
||||||
$obj_rows = $query->setFirstResult($offset)
|
|
||||||
->setMaxResults($perpage)
|
|
||||||
->getQuery()
|
|
||||||
->getResult();
|
|
||||||
|
|
||||||
|
$meta = $params['meta'];
|
||||||
|
$rows = $params['rows'];
|
||||||
|
|
||||||
// process rows
|
// process rows
|
||||||
$rows = [];
|
foreach ($rows as $key => $data) {
|
||||||
foreach ($obj_rows as $orow) {
|
|
||||||
// add row data
|
|
||||||
$row['id'] = $orow->getID();
|
|
||||||
$row['title'] = $orow->getTitle();
|
|
||||||
$row['first_name'] = $orow->getFirstName();
|
|
||||||
$row['last_name'] = $orow->getLastName();
|
|
||||||
$row['customer_classification'] = CustomerClassification::getName($orow->getCustomerClassification());
|
|
||||||
$row['flag_mobile_app'] = $orow->hasMobileApp();
|
|
||||||
$row['app_mobile_number'] = $orow->hasMobileApp() && !empty($orow->getMobileSessions()) ? $orow->getMobileSessions()[0]->getPhoneNumber() : '';
|
|
||||||
$row['flag_active'] = $orow->isActive();
|
|
||||||
$row['flag_csat'] = $orow->isCSAT();
|
|
||||||
|
|
||||||
// TODO: properly add mobile numbers and plate numbers as searchable/sortable fields, use doctrine events
|
|
||||||
$row['mobile_numbers'] = implode("<br>", $orow->getMobileNumberList());
|
|
||||||
$row['plate_numbers'] = implode("<br>", $orow->getPlateNumberList());
|
|
||||||
|
|
||||||
// add row metadata
|
|
||||||
$row['meta'] = [
|
|
||||||
'update_url' => '',
|
|
||||||
'delete_url' => ''
|
|
||||||
];
|
|
||||||
|
|
||||||
// add crud urls
|
// add crud urls
|
||||||
if ($this->isGranted('customer.update'))
|
$cust_id = $rows[$key]['id'];
|
||||||
$row['meta']['update_url'] = $this->generateUrl('customer_update', ['id' => $row['id']]);
|
|
||||||
if ($this->isGranted('customer.delete'))
|
|
||||||
$row['meta']['delete_url'] = $this->generateUrl('customer_delete', ['id' => $row['id']]);
|
|
||||||
|
|
||||||
$rows[] = $row;
|
if ($this->isGranted('customer.update'))
|
||||||
|
$rows[$key]['meta']['update_url'] = $this->generateUrl('customer_update', ['id' => $cust_id]);
|
||||||
|
if ($this->isGranted('customer.delete'))
|
||||||
|
$rows[$key]['meta']['delete_url'] = $this->generateUrl('customer_delete', ['id' => $cust_id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// response
|
// response
|
||||||
|
|
@ -440,16 +365,4 @@ class CustomerController extends Controller
|
||||||
'data' => $row
|
'data' => $row
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if datatable filter is present and append to query
|
|
||||||
protected function setQueryFilters($datatable, &$query) {
|
|
||||||
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
|
||||||
$query->join('q.vehicles', 'cv')
|
|
||||||
->where('q.first_name LIKE :filter')
|
|
||||||
->orWhere('q.last_name LIKE :filter')
|
|
||||||
->orWhere('q.customer_classification LIKE :filter')
|
|
||||||
->orWhere('cv.plate_number LIKE :filter')
|
|
||||||
->setParameter('filter', $datatable['query']['data-rows-search'] . '%');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,91 @@ class CMBCustomerHandler implements CustomerHandlerInterface
|
||||||
return $params;
|
return $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get customers
|
||||||
|
public function getCustomers(Request $req)
|
||||||
|
{
|
||||||
|
// build query
|
||||||
|
$tqb = $this->em->getRepository(Customer::class)
|
||||||
|
->createQueryBuilder('q');
|
||||||
|
|
||||||
|
$qb = $this->em->getRepository(Customer::class)
|
||||||
|
->createQueryBuilder('q');
|
||||||
|
|
||||||
|
// get datatable params
|
||||||
|
$datatable = $req->request->get('datatable');
|
||||||
|
|
||||||
|
// count total records
|
||||||
|
$tquery = $tqb->select('COUNT(q)');
|
||||||
|
|
||||||
|
// add filters to count query
|
||||||
|
$this->setQueryFilters($datatable, $tquery);
|
||||||
|
|
||||||
|
$total = $tquery->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
|
||||||
|
// get current page number
|
||||||
|
$page = $datatable['pagination']['page'] ?? 1;
|
||||||
|
|
||||||
|
$perpage = $datatable['pagination']['perpage'];
|
||||||
|
$offset = ($page - 1) * $perpage;
|
||||||
|
|
||||||
|
// add metadata
|
||||||
|
$meta = [
|
||||||
|
'page' => $page,
|
||||||
|
'perpage' => $perpage,
|
||||||
|
'pages' => ceil($total / $perpage),
|
||||||
|
'total' => $total,
|
||||||
|
'sort' => 'asc',
|
||||||
|
'field' => 'id'
|
||||||
|
];
|
||||||
|
|
||||||
|
// build query
|
||||||
|
$query = $qb->select('q');
|
||||||
|
|
||||||
|
// add filters to query
|
||||||
|
$this->setQueryFilters($datatable, $query);
|
||||||
|
|
||||||
|
// check if sorting is present, otherwise use default
|
||||||
|
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
|
||||||
|
$order = $datatable['sort']['sort'] ?? 'asc';
|
||||||
|
$query->orderBy('q.' . $datatable['sort']['field'], $order);
|
||||||
|
} else {
|
||||||
|
$query->orderBy('q.first_name', 'asc');
|
||||||
|
}
|
||||||
|
|
||||||
|
// get rows for this page
|
||||||
|
$obj_rows = $query->setFirstResult($offset)
|
||||||
|
->setMaxResults($perpage)
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
|
||||||
|
// process rows
|
||||||
|
$rows = [];
|
||||||
|
foreach ($obj_rows as $orow) {
|
||||||
|
// add row data
|
||||||
|
$row['id'] = $orow->getID();
|
||||||
|
$row['title'] = $orow->getTitle();
|
||||||
|
$row['first_name'] = $orow->getFirstName();
|
||||||
|
$row['last_name'] = $orow->getLastName();
|
||||||
|
$row['customer_classification'] = CustomerClassification::getName($orow->getCustomerClassification());
|
||||||
|
$row['flag_mobile_app'] = $orow->hasMobileApp();
|
||||||
|
$row['app_mobile_number'] = $orow->hasMobileApp() && !empty($orow->getMobileSessions()) ? $orow->getMobileSessions()[0]->getPhoneNumber() : '';
|
||||||
|
$row['flag_active'] = $orow->isActive();
|
||||||
|
$row['flag_csat'] = $orow->isCSAT();
|
||||||
|
|
||||||
|
// TODO: properly add mobile numbers and plate numbers as searchable/sortable fields, use doctrine events
|
||||||
|
$row['mobile_numbers'] = implode("<br>", $orow->getMobileNumberList());
|
||||||
|
$row['plate_numbers'] = implode("<br>", $orow->getPlateNumberList());
|
||||||
|
|
||||||
|
$rows[] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
$params['meta'] = $meta;
|
||||||
|
$params['rows'] = $rows;
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
// initialize add customer form
|
// initialize add customer form
|
||||||
public function initializeAddCustomerForm()
|
public function initializeAddCustomerForm()
|
||||||
{
|
{
|
||||||
|
|
@ -404,4 +489,16 @@ class CMBCustomerHandler implements CustomerHandlerInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if datatable filter is present and append to query
|
||||||
|
protected function setQueryFilters($datatable, &$query) {
|
||||||
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||||
|
$query->join('q.vehicles', 'cv')
|
||||||
|
->where('q.first_name LIKE :filter')
|
||||||
|
->orWhere('q.last_name LIKE :filter')
|
||||||
|
->orWhere('q.customer_classification LIKE :filter')
|
||||||
|
->orWhere('cv.plate_number LIKE :filter')
|
||||||
|
->setParameter('filter', $datatable['query']['data-rows-search'] . '%');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,92 @@ class ResqCustomerHandler implements CustomerHandlerInterface
|
||||||
return $params;
|
return $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get customers
|
||||||
|
public function getCustomers(Request $req)
|
||||||
|
{
|
||||||
|
// build query
|
||||||
|
$tqb = $this->em->getRepository(Customer::class)
|
||||||
|
->createQueryBuilder('q');
|
||||||
|
|
||||||
|
$qb = $this->em->getRepository(Customer::class)
|
||||||
|
->createQueryBuilder('q');
|
||||||
|
|
||||||
|
// get datatable params
|
||||||
|
$datatable = $req->request->get('datatable');
|
||||||
|
|
||||||
|
// count total records
|
||||||
|
$tquery = $tqb->select('COUNT(q)');
|
||||||
|
|
||||||
|
// add filters to count query
|
||||||
|
$this->setQueryFilters($datatable, $tquery);
|
||||||
|
|
||||||
|
$total = $tquery->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
|
||||||
|
// get current page number
|
||||||
|
$page = $datatable['pagination']['page'] ?? 1;
|
||||||
|
|
||||||
|
$perpage = $datatable['pagination']['perpage'];
|
||||||
|
$offset = ($page - 1) * $perpage;
|
||||||
|
|
||||||
|
// add metadata
|
||||||
|
$meta = [
|
||||||
|
'page' => $page,
|
||||||
|
'perpage' => $perpage,
|
||||||
|
'pages' => ceil($total / $perpage),
|
||||||
|
'total' => $total,
|
||||||
|
'sort' => 'asc',
|
||||||
|
'field' => 'id'
|
||||||
|
];
|
||||||
|
|
||||||
|
// build query
|
||||||
|
$query = $qb->select('q');
|
||||||
|
|
||||||
|
// add filters to query
|
||||||
|
$this->setQueryFilters($datatable, $query);
|
||||||
|
|
||||||
|
// check if sorting is present, otherwise use default
|
||||||
|
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
|
||||||
|
$order = $datatable['sort']['sort'] ?? 'asc';
|
||||||
|
$query->orderBy('q.' . $datatable['sort']['field'], $order);
|
||||||
|
} else {
|
||||||
|
$query->orderBy('q.first_name', 'asc');
|
||||||
|
}
|
||||||
|
|
||||||
|
// get rows for this page
|
||||||
|
$obj_rows = $query->setFirstResult($offset)
|
||||||
|
->setMaxResults($perpage)
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
|
||||||
|
|
||||||
|
// process rows
|
||||||
|
$rows = [];
|
||||||
|
foreach ($obj_rows as $orow) {
|
||||||
|
// add row data
|
||||||
|
$row['id'] = $orow->getID();
|
||||||
|
$row['title'] = $orow->getTitle();
|
||||||
|
$row['first_name'] = $orow->getFirstName();
|
||||||
|
$row['last_name'] = $orow->getLastName();
|
||||||
|
$row['customer_classification'] = CustomerClassification::getName($orow->getCustomerClassification());
|
||||||
|
$row['flag_mobile_app'] = $orow->hasMobileApp();
|
||||||
|
$row['app_mobile_number'] = $orow->hasMobileApp() && !empty($orow->getMobileSessions()) ? $orow->getMobileSessions()[0]->getPhoneNumber() : '';
|
||||||
|
$row['flag_active'] = $orow->isActive();
|
||||||
|
$row['flag_csat'] = $orow->isCSAT();
|
||||||
|
|
||||||
|
// TODO: properly add mobile numbers and plate numbers as searchable/sortable fields, use doctrine events
|
||||||
|
$row['mobile_numbers'] = implode("<br>", $orow->getMobileNumberList());
|
||||||
|
$row['plate_numbers'] = implode("<br>", $orow->getPlateNumberList());
|
||||||
|
|
||||||
|
$rows[] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
$params['meta'] = $meta;
|
||||||
|
$params['rows'] = $rows;
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
// initialize add customer form
|
// initialize add customer form
|
||||||
public function initializeAddCustomerForm()
|
public function initializeAddCustomerForm()
|
||||||
{
|
{
|
||||||
|
|
@ -408,4 +494,17 @@ class ResqCustomerHandler implements CustomerHandlerInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if datatable filter is present and append to query
|
||||||
|
protected function setQueryFilters($datatable, &$query) {
|
||||||
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||||
|
$query->join('q.vehicles', 'cv')
|
||||||
|
->where('q.first_name LIKE :filter')
|
||||||
|
->orWhere('q.last_name LIKE :filter')
|
||||||
|
->orWhere('q.customer_classification LIKE :filter')
|
||||||
|
->orWhere('cv.plate_number LIKE :filter')
|
||||||
|
->setParameter('filter', $datatable['query']['data-rows-search'] . '%');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ interface CustomerHandlerInterface
|
||||||
// initialize form to display customer list
|
// initialize form to display customer list
|
||||||
public function initializeCustomerIndexForm();
|
public function initializeCustomerIndexForm();
|
||||||
|
|
||||||
|
// get customers
|
||||||
|
public function getCustomers(Request $req);
|
||||||
|
|
||||||
// initialize add customer form
|
// initialize add customer form
|
||||||
public function initializeAddCustomerForm();
|
public function initializeAddCustomerForm();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue