From 7a6ccfb8ebba9b08833f589fdbf5f07364a81eb6 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 29 Mar 2021 05:48:26 +0000 Subject: [PATCH] Add Customer Source Report to admin panel. #547 --- config/acl.yaml | 2 + config/routes/report.yaml | 10 ++ src/Controller/ReportController.php | 140 ++++++++++++++++++ templates/base.html.twig | 8 + .../report/customer-source/form.html.twig | 82 ++++++++++ 5 files changed, 242 insertions(+) create mode 100644 templates/report/customer-source/form.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index d39b6be3..e694340c 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -342,6 +342,8 @@ access_keys: label: Auto Assigned Job Order Report - id: report.jo.advance_order label: Advance Order Job Order Report + - id: report.customer.source + label: Customer Source Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index acb6cb18..f9d780ef 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -127,3 +127,13 @@ rep_jo_advance_order_submit: path: /report/jo_advance_order_report controller: App\Controller\ReportController::jobOrderAdvanceOrderSubmit methods: [POST] + +rep_customer_source_form: + path: /report/customer_source_report + controller: App\Controller\ReportController::customerSourceForm + methods: [GET] + +rep_customer_source_submit: + path: /report/customer_source_report + controller: App\Controller\ReportController::customerSourceSubmit + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 73432964..86247929 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -38,6 +38,7 @@ use Catalyst\MenuBundle\Annotation\Menu; use CrEOF\Spatial\PHP\Types\Geometry\Point; use DateTime; +use PDO; class ReportController extends Controller { @@ -1016,6 +1017,64 @@ class ReportController extends Controller } + /** + * @Menu(selected="outlet_list") + */ + public function customerSourceForm() + { + $this->denyAccessUnlessGranted('report.customer.source', null, 'No access.'); + + return $this->render('report/customer-source/form.html.twig'); + } + + public function customerSourceSubmit(Request $req, EntityManagerInterface $em) + { + // get dates + $raw_date_start = $req->request->get('date_start'); + $raw_date_end = $req->request->get('date_end'); + + $date_start = DateTime::createFromFormat('m/d/Y', $raw_date_start); + $date_end = DateTime::createFromFormat('m/d/Y', $raw_date_end); + + $data = $this->getCustomerSourceDetails($req, $em); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($data) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Customer ID', + 'First Name', + 'Last Name', + 'Mobile Phone', + 'Landline', + 'Office Phone', + 'Fax', + 'Email Address', + 'Vehicle Manufacturer', + 'Vehicle Make', + 'Model Year', + 'Plate Number', + 'Source', + 'Date Created', + ]); + + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'customer_source_' . $date_start->format('Ymd') . '_' . $date_end->format('Ymd') . '.csv'; + + $resp->setStatusCode(200); + $resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); + $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); + + return $resp; + } protected function processPopappFile(UploadedFile $csv_file, EntityManagerInterface $em) { @@ -2018,4 +2077,85 @@ class ReportController extends Controller return $result; } + protected function getCustomerSourceDetails(Request $req, EntityManagerInterface $em) + { + // get dates + $raw_date_start = $req->request->get('date_start'); + $raw_date_end = $req->request->get('date_end'); + + $date_start = DateTime::createFromFormat('m/d/Y', $raw_date_start); + $date_end = DateTime::createFromFormat('m/d/Y', $raw_date_end); + + // change to this format: Y-m-d H:i:s + $new_date_start = $date_start->format('Y-m-d H:i:s'); + $new_date_end = $date_end->format('Y-m-d H:i:s'); + + // get all mobile user customer id hash + $resq_cust_ids = $this->getRESQCustomerIDs($em); + + // pdo connection + $db = $em->getConnection(); + + // get all customers + $sql = 'select c.id, c.first_name, c.last_name, c.phone_mobile, c.phone_landline, c.phone_office, c.phone_fax, c.email, c.flag_mobile_app, vm.name, v.make, cv.model_year, cv.plate_number, c.date_create from customer c, customer_vehicle cv, vehicle v, vehicle_manufacturer vm where c.date_create >= :date_start and c.date_create <= :date_end and c.id = cv.customer_id and cv.vehicle_id = v.id and v.manufacturer_id = vm.id'; + $stmt = $db->prepare($sql); + $stmt->execute([ + 'date_start' => $new_date_start, + 'date_end' => $new_date_end, + ]); + + $result = []; + // go through rows + while ($row = $stmt->fetch(PDO::FETCH_NUM)) + { + // TODO: find customer source + // NOTE: flag_mobile_app is 0 for some reason + // customer source + if (isset($resq_cust_ids[$row[0]])) + $source = 'resq'; + else + $source = 'crm / owr'; + + $result[] = [ + $row[0], // id + $row[1], // first name + $row[2], // last name + $row[3], // phone - mobile + $row[4], // phone - landline + $row[5], // phone - office + $row[6], // phone - fax + $row[7], // email + $row[9], // vehicle manufacturer + $row[10], // vehicle make + $row[11], // vehicle model + $row[12], // plate number + $source, // customer source + $row[13], + ]; + } + + return $result; + } + + protected function getRESQCustomerIDs(EntityManagerInterface $em) + { + // pdo connection + $db = $em->getConnection(); + + // get all customer ids of all mobile sessions + $sql = 'select distinct customer_id from mobile_session'; + $stmt = $db->prepare($sql); + $stmt->execute(); + $res = $stmt->fetchAll(); + + $cust_ids = []; + + foreach ($res as $cust) + { + $cust_ids[$cust['customer_id']] = $cust['customer_id']; + } + + return $cust_ids; + } + } diff --git a/templates/base.html.twig b/templates/base.html.twig index 5e5abfc2..3316aa32 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -241,6 +241,14 @@ SMS Messages Report + + + + + + Customer Source Report + + diff --git a/templates/report/customer-source/form.html.twig b/templates/report/customer-source/form.html.twig new file mode 100644 index 00000000..9b34c284 --- /dev/null +++ b/templates/report/customer-source/form.html.twig @@ -0,0 +1,82 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Customer Source Report

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ Select a date range +

+
+
+
+
+
+
+
+ +
+ +
+ +
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %}