diff --git a/config/acl.yaml b/config/acl.yaml index c6100978..1546a0d9 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -296,6 +296,8 @@ access_keys: label: Popapp Comparison Report - id: report.meh.customer label: RESQ MEH Customer Report + - id: report.warranty.class + label: Warranty Class Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index 5d815490..f847cfe5 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -47,3 +47,13 @@ rep_resq_meh_export_csv: path: /report/meh_customer_export controller: App\Controller\ReportController::mehCustomerExportCSV methods: [POST] + +rep_warranty_class_form: + path: /report/warranty_class_report + controller: App\Controller\ReportController::warrantyClassForm + methods: [GET] + +rep_warranty_class_export_csv: + path: /report/warranty_class_report + controller: App\Controller\ReportController::warrantyClassExportCSV + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 60db4d9d..d1bd1bcd 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -36,6 +36,8 @@ use DateTime; class ReportController extends Controller { + const PREREGISTER_PREFIX = '9'; + /** * @Menu(selected="outlet_list") */ @@ -547,6 +549,71 @@ class ReportController extends Controller return $resp; } + /** + * @Menu(selected="outlet_list") + */ + public function warrantyClassForm() + { + $this->denyAccessUnlessGranted('report.warranty.class', null, 'No access.'); + $params['mode'] = 'form'; + + return $this->render('report/warranty-class/form.html.twig', $params); + } + + /** + * @Menu(selected="outlet_list") + */ + public function warrantyClassExportCSV(Request $req, EntityManagerInterface $em) + { + $data = $this->getWarrantyClassData($em); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($data) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Customer Last Name', + 'Customer First Name', + 'Vehicle Manufacturer', + 'Vehicle Make', + 'Model Year', + 'Vehicle Color', + 'Warranty Serial', + 'Warranty Class', + 'Plate Number', + 'Warranty Last Name', + 'Warranty First Name', + 'Warranty Mobile Number', + 'Warranty Battery Model', + 'Warranty Battery Size', + 'Warranty SAP Battery', + 'Warranty Status', + 'Warranty Created', + 'Warranty Purchased', + 'Warranty Expiry Date', + 'Warranty Claim Date', + 'Warranty Claimed From', + 'Warranty Privacy Policy', + 'Is Warranty Activated?', + ]); + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'warranty_class_report' . '.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) { // attempt to open file @@ -723,6 +790,129 @@ class ReportController extends Controller } return $results; + } + + protected function getWarrantyClassData(EntityManagerInterface $em) + { + $results = []; + + // query preregistered ustomers using search term '%9' + $cust_query = $em->createQuery('select c from App\Entity\Customer c where c.phone_mobile like :search_mobile') + ->setParameter('search_mobile', "%" . self::PREREGISTER_PREFIX); + $customers = $cust_query->iterate(); + + foreach($customers as $crow) + { + $cust = $crow[0]; + //error_log('Processing customer ' . $cust->getID()); + + // get list of customer vehicles + $c_vehicles = $cust->getVehicles(); + + foreach($c_vehicles as $cv) + { + if (!empty($c_vehicles)) + { + // find warranty for plate number + $clean_cv_plate = $this->cleanPlateNumber($cv->getPlateNumber()); + + $warranties = $em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_cv_plate]); + + foreach ($warranties as $warr) + { + //error_log('Found warranty for plate number ' . $warr->getPlateNumber()); + + // form the result row + $results[] = $this->formWarrantyClassResult($cust, $cv, $warr); + } + } + } + $em->clear(); + + } + + return $results; + } + + protected function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + + protected function formWarrantyClassResult($cust, $cv, $warr) + { + $batt_model = ''; + $batt_size = ''; + $sap_batt = ''; + $policy = ''; + $date_purchased = ''; + $date_expire = ''; + $date_claim = ''; + + $create_date = $warr->getDateCreate(); + $date_create = $create_date->format('d/M/y'); + + if ($warr->getDatePurchase() != null) + { + $p_date = $warr->getDatePurchase(); + $date_purchased = $p_date->format('d/M/y'); + } + if ($warr->getDateClaim() != null) + { + $c_date = $warr->getDateClaim(); + $date_claim = $c_date->format('d/M/y'); + } + if ($warr->getDateExpire() != null) + { + $e_date = $warr->getDateExpire(); + $date_expire = $e_date->format('d/M/y'); + } + + if ($warr->getBatteryModel() != null) + { + $batt_model = $warr->getBatteryModel()->getName(); + } + if ($warr->getBatterySize() != null) + { + $batt_size = $warr->getBatterySize()->getName(); + } + if ($warr->getSAPBattery() != null) + { + $sap_batt = $warr->getSAPBattery()->getBrand()->getName(); + } + if ($warr->getPrivacyPolicy() != null) + { + $policy = $warr->getPrivacyPolicy()->getName(); + } + + $data = [ + 'c_last_name' => $cust->getLastName(), + 'c_first_name' => $cust->getFirstName(), + 'manufacturer' => $cv->getVehicle()->getManufacturer()->getName(), + 'make' => $cv->getVehicle()->getMake(), + 'model_year' => $cv->getModelYear(), + 'color' => $cv->getColor(), + 'serial' => $warr->getSerial(), + 'class' => $warr->getWarrantyClass(), + 'plate_number' => $warr->getPlateNumber(), + 'w_last_name' => $warr->getLastName(), + 'w_first_name' => $warr->getFirstName(), + 'w_mobile_num' => $warr->getMobileNumber(), + 'w_batt_model' => $batt_model, + 'w_batt_size' => $batt_size, + 'w_sap_batt' => $sap_batt, + 'w_status' => $warr->getStatus(), + 'w_date_create' => $date_create, + 'w_date_purchase' => $date_purchased, + 'w_date_expire' => $date_expire, + 'w_date_claim' => $date_claim, + 'w_claimed_from' => $warr->getClaimedFrom(), + 'w_privacy_policy' => $policy, + 'w_activated' => ($warr->isActivated() ? 'Active' : 'Inactive'), + ]; + + return $data; } } diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index 605d81ce..113fbb93 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -14,7 +14,8 @@ use Exception; * name="warranty", * uniqueConstraints={ * @ORM\UniqueConstraint(columns={"serial"}) - * } + * }, + * indexes={@ORM\Index(name="plate_number_idx", columns={"plate_number"})}) * ) */ class Warranty diff --git a/templates/base.html.twig b/templates/base.html.twig index 3ef5e050..c6f1b9d5 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -165,6 +165,14 @@ RESQ MEH Customer Report + + + + + + Warranty Class Report + + diff --git a/templates/report/warranty-class/form.html.twig b/templates/report/warranty-class/form.html.twig new file mode 100644 index 00000000..a2313b1d --- /dev/null +++ b/templates/report/warranty-class/form.html.twig @@ -0,0 +1,50 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Warranty Class Report +

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

+ Generate Warranty Class CSV File +

+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+{% endblock %} +