From 33e0e6b41b1e07c1ca10fc3c5192f8790e0c54e8 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 29 Aug 2019 07:26:11 +0000 Subject: [PATCH 01/61] Add route and access for warranty upload. Add method to WarrantyController to handle the upload. Add template for warranty upload. #258 --- config/acl.yaml | 2 + config/menu.yaml | 4 + config/routes/warranty.yaml | 10 ++ src/Controller/WarrantyController.php | 143 ++++++++++++++++++++++- templates/warranty/upload.form.html.twig | 52 +++++++++ 5 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 templates/warranty/upload.form.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 64b53ad1..04fac4bb 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -252,6 +252,8 @@ access_keys: label: Search - id: warranty.search label: Customer Battery Search + - id: warranty.upload + label: Warranty Upload - id: ticket label: Ticket Access diff --git a/config/menu.yaml b/config/menu.yaml index 92b29c2d..491e51aa 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -151,6 +151,10 @@ main_menu: acl: warranty.list label: Warranty parent: support + - id: warranty_upload + acl: warranty.upload + label: Warranty Upload + parent: support - id: service acl: service.menu diff --git a/config/routes/warranty.yaml b/config/routes/warranty.yaml index 86c3e9b9..3e049e54 100644 --- a/config/routes/warranty.yaml +++ b/config/routes/warranty.yaml @@ -28,3 +28,13 @@ warranty_update_submit: path: /warranties/{id} controller: App\Controller\WarrantyController::updateSubmit methods: [POST] + +warranty_upload: + path: /warranty/upload + controller: App\Controller\WarrantyController::uploadForm + methods: [GET] + +warranty_upload_submit: + path: /warranty/upload + controller: App\Controller\WarrantyController::uploadSubmit + methods: [POST] diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 0dc5a43e..369c6632 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Warranty; use App\Entity\SAPBattery; +use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; @@ -11,8 +12,10 @@ use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; use Doctrine\ORM\Query; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -348,9 +351,147 @@ class WarrantyController extends Controller 'success' => 'Changes have been saved!' ]); } - } + /** + * @Menu(selected="warranty_list") + */ + public function uploadForm() + { + $this->denyAccessUnlessGranted('warranty.upload', null, 'No access.'); + + return $this->render('warranty/upload.form.html.twig'); + } + + /** + * @Menu(selected="warranty_list") + */ + public function uploadSubmit(Request $req, EntityManagerInterface $em) + { + // retrieve temporary info for file + $file = $req->files->get('csv_file'); + + // process the csv file + $this->processWarrantyFile($file, $em); + + return $this->render('warranty/upload.form.html.twig'); + } + + protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em) + { + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // loop through the rows + // 0 - Owner First Name + // 1 - Owner Last Name + // 2 - Owner Email + // 3 - Owner Address + // 4 - Owner Mobile + // 5 - Owner Telephone + // 6 - Vehicle Make + // 7 - Vehicle Model + // 8 - Vehicle Year + // 9 - Vehicle Plate Number + // 10 - Battery Serial Number + // 11 - Battery Sales Invoice + // 12 - Battery Date of Purchase + // 13 - Distributor Name + // 14 - Distributor Address + // 15 - Application Type ID + // 16 - Battery ID + // 17 - Ownership Type + + $row_num = 0; + while (($fields = fgetcsv($fh)) !== false) + { + // start processing at row 1, not 0 + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get the data + $first_name = trim($fields[0]); + $last_name = trim($fields[1]); + $mobile_number = trim($fields[4]); + $plate_number = trim($fields[9]); + $serial = trim($fields[10]); + $purchase_date = trim($fields[12]); + $battery_id = trim($fields[16]); + + // new warranty + $warranty = new Warranty(); + + // clean the plate number + $cleaned_plate_number = Warranty::cleanPlateNumber($plate_number); + if ($cleaned_plate_number) + { + error_log("mogol plate number " . $cleaned_plate_number); + // continue processing + // check if purchase_date is empty. If so, ignore entry + $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); + if ($date_purchase != false) + { + error_log ("mogol date purchase " . $purchase_date); + // get the battery purchased + // check battery first. If not found, check sap_battery + $battery = $em->getRepository(Battery::class)->find($battery_id); + if ($battery != null) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + } + else + { + // find battery in sap_battery + $battery = $em->getRepository(SAPBattery::class)->find($battery_id); + if ($battery != null) + { + // battery is SAPBattery + $warranty->setSAPBattery($battery); + } + } + + // set and save values + error_log("mogol serial " . $serial); + $warranty->setSerial($serial) + ->setPlateNumber($cleaned_plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $em->persist($warranty); + $em->flush(); + } + } + + $row_num++; + } + } protected function fillDropdownParameters(&$params) { diff --git a/templates/warranty/upload.form.html.twig b/templates/warranty/upload.form.html.twig new file mode 100644 index 00000000..d778bf47 --- /dev/null +++ b/templates/warranty/upload.form.html.twig @@ -0,0 +1,52 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Warranty Upload +

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

+ Upload Warranty CSV File +

+
+
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+{% endblock %} From dbee60bf466464f8bd3b8e6f6b39660f403b4b6e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 29 Aug 2019 10:48:08 +0000 Subject: [PATCH 02/61] Include invalid plate numbers in warranty. Return a csv file with the invalid entries that cannot be inserted into the database. #258 --- src/Controller/WarrantyController.php | 173 ++++++++++++++++++-------- 1 file changed, 118 insertions(+), 55 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 369c6632..9385d157 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -15,6 +15,7 @@ use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -372,9 +373,47 @@ class WarrantyController extends Controller $file = $req->files->get('csv_file'); // process the csv file - $this->processWarrantyFile($file, $em); + $inv_entries = $this->processWarrantyFile($file, $em); - return $this->render('warranty/upload.form.html.twig'); + $resp = new StreamedResponse(); + $resp->setCallback(function() use($inv_entries) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Owner First Name', + 'Owner Last Name', + 'Owner Email', + 'Owner Address', + 'Owner Mobile', + 'Owner Telephone', + 'Vehicle Make', + 'Vehicle Model', + 'Vehicle Year', + 'Vehicle Plate Number', + 'Battery Serial Number', + 'Battery Sales Invoice', + 'Battery Date of Purchase', + 'Distributor Name', + 'Distributor Address', + 'Application Type ID', + 'Battery ID', + 'Ownership Type', + ]); + foreach ($inv_entries as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'invalid_warranties' . '.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 processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em) @@ -410,6 +449,7 @@ class WarrantyController extends Controller // 17 - Ownership Type $row_num = 0; + $invalid_entries = []; while (($fields = fgetcsv($fh)) !== false) { // start processing at row 1, not 0 @@ -428,69 +468,92 @@ class WarrantyController extends Controller $purchase_date = trim($fields[12]); $battery_id = trim($fields[16]); - // new warranty - $warranty = new Warranty(); + // check if purchase_date or plate_number or serial is empty or if + // purchase date is valid + $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); - // clean the plate number - $cleaned_plate_number = Warranty::cleanPlateNumber($plate_number); - if ($cleaned_plate_number) + if (empty($purchase_date) || + empty($plate_number) || + empty($serial) || + ($date_purchase == false)) { - error_log("mogol plate number " . $cleaned_plate_number); - // continue processing - // check if purchase_date is empty. If so, ignore entry - $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); - if ($date_purchase != false) + // add to invalid_entries + $invalid_entries[] = [ + 'owner_first_name' => $first_name, + 'owner_last_name' => $last_name, + 'owner_email' => trim($fields[2]), + 'owner_address' => trim($fields[3]), + 'owner_mobile' => $mobile_number, + 'owner_telephone' => trim($fields[5]), + 'vehicle_make' => trim($fields[6]), + 'vehicle_model' => trim($fields[7]), + 'vehicle_year' => trim($fields[8]), + 'vehicle_plate_number' => $plate_number, + 'battery_serial_number' => $serial, + 'battery_sales_invoice' => trim($fields[11]), + 'battery_date_purchase' => $purchase_date, + 'distributor_name' => trim($fields[13]), + 'distributor_address' => trim($fields[14]), + 'application_type_id' => trim($fields[15]), + 'battery_id' => $battery_id, + 'ownership_typ' => trim($fields[17]), + ]; + } + else + { + + // new warranty + $warranty = new Warranty(); + + // get the battery purchased + // check battery first. If not found, check sap_battery + $battery = $em->getRepository(Battery::class)->find($battery_id); + if ($battery != null) { - error_log ("mogol date purchase " . $purchase_date); - // get the battery purchased - // check battery first. If not found, check sap_battery - $battery = $em->getRepository(Battery::class)->find($battery_id); + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + } + else + { + // find battery in sap_battery + $battery = $em->getRepository(SAPBattery::class)->find($battery_id); if ($battery != null) { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) - { - $warranty->setBatteryModel($bty_model); - } - - if ($bty_size != null) - { - $warranty->setBatterySize($bty_size); - } + // battery is SAPBattery + $warranty->setSAPBattery($battery); } - else - { - // find battery in sap_battery - $battery = $em->getRepository(SAPBattery::class)->find($battery_id); - if ($battery != null) - { - // battery is SAPBattery - $warranty->setSAPBattery($battery); - } - } - - // set and save values - error_log("mogol serial " . $serial); - $warranty->setSerial($serial) - ->setPlateNumber($cleaned_plate_number) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase); - - $em->persist($warranty); - $em->flush(); } - } + // set and save values + $warranty->setSerial($serial) + ->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $em->persist($warranty); + $em->flush(); + } + $row_num++; } + + return $invalid_entries; } protected function fillDropdownParameters(&$params) From 590b9b82b475d9c517a6cb2f5815b95929abd593 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 22 Oct 2019 05:51:43 +0000 Subject: [PATCH 03/61] Add geofence variable to env.dist. Add checking to APIController if geofence is on or not. #271 --- .env.dist | 3 +++ src/Controller/APIController.php | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.env.dist b/.env.dist index 4e10dffc..87f679ac 100644 --- a/.env.dist +++ b/.env.dist @@ -42,3 +42,6 @@ POLICY_MOBILE=insertmobilepolicyidhere # OTP OTP_MODE=settotestorrandom + +# geofence +GEOFENCE=settotrueorfalse diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 075879bb..aefb8792 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -839,18 +839,24 @@ class APIController extends Controller $long = $req->request->get('long'); $lat = $req->request->get('lat'); - /* // geofence - $is_covered = $geo->isCovered($long, $lat); - if (!$is_covered) - { - // TODO: put geofence error message in config file somewhere - $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to Metro Manila only. We will update you as soon as we are able to cover your area'); - return $res->getReturnResponse(); - } - */ + // check if geofence is on + $dotenv = new Dotenv(); + $dotenv->loadEnv(__DIR__.'/../../.env'); + $geo_flag = $_ENV['GEOFENCE']; + + if ($geo_flag == 'true') + { + $is_covered = $geo->isCovered($long, $lat); + if (!$is_covered) + { + // TODO: put geofence error message in config file somewhere + $res->setError(true) + ->setErrorMessage('Oops! Our service is limited to Metro Manila, Baguio, and Laguna only. We will update you as soon as we are able to cover your area'); + return $res->getReturnResponse(); + } + } $jo = new JobOrder(); $jo->setSource(TransactionOrigin::MOBILE_APP) From e169766efd91bd493db8b92b62688257a49be0b3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 22 Oct 2019 07:07:03 +0000 Subject: [PATCH 04/61] Modify the error message if delivery is not in the coverage areas. #271 --- src/Controller/APIController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index aefb8792..225f81c8 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -853,7 +853,7 @@ class APIController extends Controller { // TODO: put geofence error message in config file somewhere $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to Metro Manila, Baguio, and Laguna only. We will update you as soon as we are able to cover your area'); + ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); return $res->getReturnResponse(); } } From f6dce241ac999ca33ec66db4fd7d7806efb93876 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 23 Oct 2019 05:30:12 +0000 Subject: [PATCH 05/61] Move retrieval of env variable to the geofence service. #271 --- .env.dist | 2 +- config/services.yaml | 4 ++++ src/Controller/APIController.php | 21 ++++++--------------- src/Service/GeofenceTracker.php | 28 ++++++++++++++++++---------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/.env.dist b/.env.dist index 87f679ac..ca93d2d2 100644 --- a/.env.dist +++ b/.env.dist @@ -44,4 +44,4 @@ POLICY_MOBILE=insertmobilepolicyidhere OTP_MODE=settotestorrandom # geofence -GEOFENCE=settotrueorfalse +GEOFENCE_ENABLE=settotrueorfalse diff --git a/config/services.yaml b/config/services.yaml index 8c8024f5..c6f2f877 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -87,6 +87,10 @@ services: $password: "%env(REDIS_CLIENT_PASSWORD)%" $env_flag: "dev" + App\Service\GeofenceTracker: + arguments: + $geofence_flag: "%env(GEOFENCE_ENABLE)%" + Catalyst\APIBundle\Security\APIKeyUserProvider: arguments: $em: "@doctrine.orm.entity_manager" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 225f81c8..f8a247cd 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -840,22 +840,13 @@ class APIController extends Controller $lat = $req->request->get('lat'); // geofence - // check if geofence is on - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $geo_flag = $_ENV['GEOFENCE']; - - if ($geo_flag == 'true') + $is_covered = $geo->isCovered($long, $lat); + if (!$is_covered) { - $is_covered = $geo->isCovered($long, $lat); - if (!$is_covered) - { - // TODO: put geofence error message in config file somewhere - $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); - return $res->getReturnResponse(); - } + // TODO: put geofence error message in config file somewhere + $res->setError(true) + ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); + return $res->getReturnResponse(); } $jo = new JobOrder(); diff --git a/src/Service/GeofenceTracker.php b/src/Service/GeofenceTracker.php index b2052e02..5acc7564 100644 --- a/src/Service/GeofenceTracker.php +++ b/src/Service/GeofenceTracker.php @@ -11,25 +11,33 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point; class GeofenceTracker { protected $em; + protected $geofence_flag; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, $geofence_flag) { $this->em = $em; + $this->geofence_flag = $geofence_flag; } public function isCovered($long, $lat) { - // see if the point is in any of the polygons - $query = $this->em->createQuery('SELECT count(s) from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true') - ->setParameter('long', $long) - ->setParameter('lat', $lat); + // check if geofence is enabled + if ($this->geofence_flag == 'true') + { + // see if the point is in any of the polygons + $query = $this->em->createQuery('SELECT count(s) from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true') + ->setParameter('long', $long) + ->setParameter('lat', $lat); - // number of polygons that contain the point - $count = $query->getSingleScalarResult(); + // number of polygons that contain the point + $count = $query->getSingleScalarResult(); - if ($count > 0) - return true; + if ($count > 0) + return true; - return false; + return false; + } + + return true; } } From ed0486fbf59ffa2e062c79f1c98335caee270fb2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 23 Oct 2019 05:58:05 +0000 Subject: [PATCH 06/61] Improve geofence error message. #271 --- src/Controller/APIController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index f8a247cd..75dcb9d0 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -845,7 +845,7 @@ class APIController extends Controller { // TODO: put geofence error message in config file somewhere $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); + ->setErrorMessage('Oops! Our service is limited to some areas in Metro Manila, Laguna, and Baguio only. We will update you as soon as we are able to cover your area'); return $res->getReturnResponse(); } From a55d093dcee490812899805570c1a5befe2f8bef Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Fri, 15 Nov 2019 09:16:30 +0800 Subject: [PATCH 07/61] Add warranty class to popapp comparison report #273 --- src/Controller/ReportController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index e06d1711..8558de79 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -474,6 +474,7 @@ class ReportController extends Controller 'Plate Number', 'Warranty Create Date', 'Activation Status', + 'Warranty Class', 'Has Mobile App?', 'Date Mobile App Downloaded', 'Mobile Number Using Mobile App', @@ -665,6 +666,7 @@ class ReportController extends Controller $results[$key]['warr_firstname'] = $warranty->getFirstName(); $results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y"); $results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive'); + $results[$key]['warr_class'] = $warranty->getWarrantyClass(); } } } From 0d60fe1a9d3e574bd6b9cbf471bd50650d008868 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 18 Nov 2019 04:42:55 +0000 Subject: [PATCH 08/61] Add reason for invalid warranty. #258 --- src/Controller/WarrantyController.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 9385d157..ede93334 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -26,6 +26,11 @@ use Catalyst\MenuBundle\Annotation\Menu; class WarrantyController extends Controller { + const PURCHASE_DATE_EMPTY = 'Purchase date missing.'; + const PLATE_NUM_EMPTY = 'Plate number missing.'; + const SERIAL_EMPTY = 'Serial number missing.'; + const PURCHASE_DATE_INVALID = 'Invalid purchase date.'; + /** * @Menu(selected="warranty_list") */ @@ -398,6 +403,7 @@ class WarrantyController extends Controller 'Application Type ID', 'Battery ID', 'Ownership Type', + 'Reason Warranty Not Added', ]); foreach ($inv_entries as $row) { @@ -477,6 +483,19 @@ class WarrantyController extends Controller empty($serial) || ($date_purchase == false)) { + $reason = ''; + if (empty($plate_number)) + $reason = $reason . self::PLATE_NUM_EMPTY . ' '; + if (empty($serial)) + $reason = $reason . self::SERIAL_EMPTY . ' '; + if (empty($purchase_date)) + $reason = $reason . self::PURCHASE_DATE_EMPTY . ' '; + else + { + if ($date_purchase == false) + $reason = $reason . self::PURCHASE_DATE_INVALID . ' '; + } + // add to invalid_entries $invalid_entries[] = [ 'owner_first_name' => $first_name, @@ -496,7 +515,8 @@ class WarrantyController extends Controller 'distributor_address' => trim($fields[14]), 'application_type_id' => trim($fields[15]), 'battery_id' => $battery_id, - 'ownership_typ' => trim($fields[17]), + 'ownership_type' => trim($fields[17]), + 'reason' => $reason, ]; } else From 9c4db034d0fcfdf1db273cd9f00be5781c9e1021 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 19 Nov 2019 08:35:51 +0000 Subject: [PATCH 09/61] Add command to create customer and customer vehicle from warranty. #274 --- .../CreateCustomerFromWarrantyCommand.php | 161 ++++++++++++++++++ src/Controller/APIController.php | 1 + 2 files changed, 162 insertions(+) create mode 100644 src/Command/CreateCustomerFromWarrantyCommand.php diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php new file mode 100644 index 00000000..2533d2c9 --- /dev/null +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -0,0 +1,161 @@ +em = $em; + + $this->loadCustomers(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:createfromwarranty') + ->setDescription('Create customers from existing warranties.') + ->setHelp('Creates customers from existing warranties.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get all warranties + $warranties = $this->em->getRepository(Warranty::class)->findAll(); + + foreach($warranties as $warr) + { + // check if warranty mobile already exists in customer + $w_mobile = $warr->getMobileNumber(); + if (empty($w_mobile)) + { + // TODO: for now, if warranty mobile number is empty, do nothing + continue; + } + + // parse warranty mobile in case of multiple numbers + // check for spaces, slash, and forward slash + $w_mobile_array = []; + if (preg_match('/[\/\\]/', $w_mobile)) + { + $w_mobile_array = preg_split('/[\/\\]/', $w_mobile); + } + // only one mobile number + $w_mobile_array[] = $w_mobile; + } + + $cust_found = false; + + // set values for new customer vehicle + $w_plate_number = $warr->getPlateNumber(); + $default_cv_color = 'White'; + // TODO: add checking that default manufacturer is not null + $default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy('name' =>'Unknown'); + $default_make = 'Unknown'; + // search cust_index for numbers in mobile_array + foreach ($w_mobile_array as $w_mobile_num) + { + // if present, check if customer vehicle plate number matches warranty plate number? + foreach ($this->cust_index as $key => $customer) + { + $c_mobile = $customer->getPhoneMobile(); + if (!(empty($c_mobile))) + { + if (strpos($c_mobile, $w_mobile_num)) + { + // mobile number belongs to existing customer + // get customer vehicles + $c_vehicles = $customer->getVehicles(); + if (!(empty($c_vehicles)) + { + // check if plate number of customer vehicle matches warranty plate number + foreach ($c_vehicles as $c_vehicle) + { + $cv_plate_number = $c_vehicle->getPlateNumber(); + if ($cv_plate_number == $w_plate_number) + { + // get out of all loops since current warranty belongs to an + // existing customer and customer vehicle + break 3; + } + } + } + // add customer vehicle to existing customer with unknown manufacturer and make + $this->createCustomerVehicle($customer, $default_manufacturer, + $default_make, $w_plate_number, $default_cv_color); + } + $cust_found = true; + } + // no customer mobile number, ignore for now + } + // if customer not found, add customer and customer vehicle + if ($cust_found != true) + { + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); + + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name); + + $this->em->persist($cust); + + $this->createCustomerVehicle($cust, $default_manufacturer, + $default_make, $w_plate_number, $default_cv_color) + + } + } + + } + + } + + protected function loadCustomers() + { + // get all customers + $customers = $this->em->getRepository(Customer::class)->findAll(); + + $this->cust_index = []; + foreach ($customers as $customer) + { + $cust_id = $customer->getID(); + $this->cust_index[$cust_id] = $customer; + } + } + + protected function createCustomerVehicle(Customer $cust, $manufacturer, $make, + $plate_number, $color) + { + $new_vehicle = new Vehicle(); + $new_cv = new CustomerVehicle(); + + // get manufacturer and make with name 'unknown' + $new_vehicle->setManufacturer($manufacturer) + + // TODO: remove the assert not blank for color and model year + $new_cv->setCustomer($cust) + ->setPlateNumber($plate_number) + ->setModelYear(0) + ->setColor($color) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setFuelType(FuelType::GAS) + ->setVehicle($vehicle); + } +} diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 75dcb9d0..0b360d9c 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -334,6 +334,7 @@ class APIController extends Controller // TODO: check if we have the number registered before and merge + // TODO: check if mobile matches mobile of customer $dupe_sess = $this->findNumberSession($this->session->getPhoneNumber()); if ($dupe_sess != null) { From 515bb84c136277e4cefea50636d3305dfa4470b8 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 01:40:07 +0000 Subject: [PATCH 10/61] Remove Assert NotBlank from color and model year. #274 --- .../CreateCustomerFromWarrantyCommand.php | 20 +++++++++++-------- src/Entity/CustomerVehicle.php | 2 -- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 2533d2c9..992cf2db 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -12,6 +12,8 @@ use Doctrine\Common\Persistence\ObjectManager; use App\Entity\Warranty; use App\Entity\Customer; use App\Entity\CustomerVehicle; +use App\Entity\VehicleManufacturer; +use App\Entity\Vehicle; class CreateCustomerFromWarrantyCommand extends Command { @@ -56,6 +58,8 @@ class CreateCustomerFromWarrantyCommand extends Command { $w_mobile_array = preg_split('/[\/\\]/', $w_mobile); } + else + { // only one mobile number $w_mobile_array[] = $w_mobile; } @@ -66,7 +70,11 @@ class CreateCustomerFromWarrantyCommand extends Command $w_plate_number = $warr->getPlateNumber(); $default_cv_color = 'White'; // TODO: add checking that default manufacturer is not null - $default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy('name' =>'Unknown'); + $default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy(['name' =>'Unknown']); + if (empty($default_manufacturer)) + { + $output->writeln("Need to add manufacturer with Unknown name"); + } $default_make = 'Unknown'; // search cust_index for numbers in mobile_array foreach ($w_mobile_array as $w_mobile_num) @@ -82,7 +90,7 @@ class CreateCustomerFromWarrantyCommand extends Command // mobile number belongs to existing customer // get customer vehicles $c_vehicles = $customer->getVehicles(); - if (!(empty($c_vehicles)) + if (!(empty($c_vehicles))) { // check if plate number of customer vehicle matches warranty plate number foreach ($c_vehicles as $c_vehicle) @@ -90,7 +98,7 @@ class CreateCustomerFromWarrantyCommand extends Command $cv_plate_number = $c_vehicle->getPlateNumber(); if ($cv_plate_number == $w_plate_number) { - // get out of all loops since current warranty belongs to an + // move to the next warranty since current warranty belongs to an // existing customer and customer vehicle break 3; } @@ -118,13 +126,11 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($cust); $this->createCustomerVehicle($cust, $default_manufacturer, - $default_make, $w_plate_number, $default_cv_color) + $default_make, $w_plate_number, $default_cv_color); } } - } - } protected function loadCustomers() @@ -143,11 +149,9 @@ class CreateCustomerFromWarrantyCommand extends Command protected function createCustomerVehicle(Customer $cust, $manufacturer, $make, $plate_number, $color) { - $new_vehicle = new Vehicle(); $new_cv = new CustomerVehicle(); // get manufacturer and make with name 'unknown' - $new_vehicle->setManufacturer($manufacturer) // TODO: remove the assert not blank for color and model year $new_cv->setCustomer($cust) diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index 841f7b16..5f1f1031 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -54,14 +54,12 @@ class CustomerVehicle // model year /** * @ORM\Column(type="smallint") - * @Assert\NotBlank() */ protected $model_year; // color of customer's vehicle /** * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() */ protected $color; From 6bde277fefc5dc2bb011d3e55ef424ce03083836 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 02:01:41 +0000 Subject: [PATCH 11/61] Fix syntax errors. #274 --- .../CreateCustomerFromWarrantyCommand.php | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 992cf2db..94f3b725 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -15,6 +15,9 @@ use App\Entity\CustomerVehicle; use App\Entity\VehicleManufacturer; use App\Entity\Vehicle; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; + class CreateCustomerFromWarrantyCommand extends Command { protected $em; @@ -24,8 +27,6 @@ class CreateCustomerFromWarrantyCommand extends Command { $this->em = $em; - $this->loadCustomers(); - parent::__construct(); } @@ -35,9 +36,10 @@ class CreateCustomerFromWarrantyCommand extends Command ->setDescription('Create customers from existing warranties.') ->setHelp('Creates customers from existing warranties.'); } - protected function execute(InputInterface $input, OutputInterface $output) { + // load all customers + $this->loadCustomers(); // get all warranties $warranties = $this->em->getRepository(Warranty::class)->findAll(); @@ -68,14 +70,11 @@ class CreateCustomerFromWarrantyCommand extends Command // set values for new customer vehicle $w_plate_number = $warr->getPlateNumber(); - $default_cv_color = 'White'; - // TODO: add checking that default manufacturer is not null - $default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy(['name' =>'Unknown']); - if (empty($default_manufacturer)) + $default_vehicle = $this->em->getRepository(Vehicle::class)->findBy(['name' =>'Unknown']); + if (empty($default_vehicle)) { - $output->writeln("Need to add manufacturer with Unknown name"); + $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); } - $default_make = 'Unknown'; // search cust_index for numbers in mobile_array foreach ($w_mobile_array as $w_mobile_num) { @@ -105,8 +104,7 @@ class CreateCustomerFromWarrantyCommand extends Command } } // add customer vehicle to existing customer with unknown manufacturer and make - $this->createCustomerVehicle($customer, $default_manufacturer, - $default_make, $w_plate_number, $default_cv_color); + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } $cust_found = true; } @@ -125,13 +123,12 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($cust); - $this->createCustomerVehicle($cust, $default_manufacturer, - $default_make, $w_plate_number, $default_cv_color); + $this->createCustomerVehicle($cust, $default_vehicle, $w_plate_number); } } } - } + } protected function loadCustomers() { @@ -146,20 +143,17 @@ class CreateCustomerFromWarrantyCommand extends Command } } - protected function createCustomerVehicle(Customer $cust, $manufacturer, $make, - $plate_number, $color) + protected function createCustomerVehicle(Customer $cust, $vehicle, $plate_number) { $new_cv = new CustomerVehicle(); - // get manufacturer and make with name 'unknown' - - // TODO: remove the assert not blank for color and model year $new_cv->setCustomer($cust) ->setPlateNumber($plate_number) - ->setModelYear(0) - ->setColor($color) ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) ->setFuelType(FuelType::GAS) ->setVehicle($vehicle); - } + + $this->em->persist($new_cv); + $this->em->flush(); + } } From a8f069781dff9a4d0bf4842a03e0082187599433 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 03:53:34 +0000 Subject: [PATCH 12/61] Fix issues found during testing. #274 --- .../CreateCustomerFromWarrantyCommand.php | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 94f3b725..56620ae7 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -40,25 +40,28 @@ class CreateCustomerFromWarrantyCommand extends Command { // load all customers $this->loadCustomers(); + // get all warranties $warranties = $this->em->getRepository(Warranty::class)->findAll(); foreach($warranties as $warr) { + $cust_found = false; // check if warranty mobile already exists in customer $w_mobile = $warr->getMobileNumber(); if (empty($w_mobile)) { // TODO: for now, if warranty mobile number is empty, do nothing + $output->writeln('Move to next warranty since mobile number is empty'); continue; } // parse warranty mobile in case of multiple numbers // check for spaces, slash, and forward slash $w_mobile_array = []; - if (preg_match('/[\/\\]/', $w_mobile)) + if (preg_match('/[\\\s\/]/', $w_mobile)) { - $w_mobile_array = preg_split('/[\/\\]/', $w_mobile); + $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); } else { @@ -66,11 +69,9 @@ class CreateCustomerFromWarrantyCommand extends Command $w_mobile_array[] = $w_mobile; } - $cust_found = false; - // set values for new customer vehicle $w_plate_number = $warr->getPlateNumber(); - $default_vehicle = $this->em->getRepository(Vehicle::class)->findBy(['name' =>'Unknown']); + $default_vehicle = $this->em->getRepository(Vehicle::class)->findOneBy(['make' =>'Unknown']); if (empty($default_vehicle)) { $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); @@ -84,11 +85,11 @@ class CreateCustomerFromWarrantyCommand extends Command $c_mobile = $customer->getPhoneMobile(); if (!(empty($c_mobile))) { - if (strpos($c_mobile, $w_mobile_num)) + $pos = strpos($c_mobile, $w_mobile_num); + if ($pos !== false) { // mobile number belongs to existing customer // get customer vehicles - $c_vehicles = $customer->getVehicles(); if (!(empty($c_vehicles))) { // check if plate number of customer vehicle matches warranty plate number @@ -99,32 +100,42 @@ class CreateCustomerFromWarrantyCommand extends Command { // move to the next warranty since current warranty belongs to an // existing customer and customer vehicle + $cust_found = true; break 3; } } } // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } - $cust_found = true; } // no customer mobile number, ignore for now } // if customer not found, add customer and customer vehicle - if ($cust_found != true) + if (!($cust_found)) { // get warranty first name, last name $w_first_name = $warr->getFirstName(); $w_last_name = $warr->getLastName(); + $output->writeln($w_first_name); + $output->writeln($w_last_name); + $output->writeln($w_plate_number); + $new_cust = new Customer(); $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name); + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); - $this->em->persist($cust); + $this->em->persist($new_cust); + $this->em->flush(); - $this->createCustomerVehicle($cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + // add latest customer to hash + $cust_id = $new_cust->getID(); + $this->cust_index[$cust_id] = $new_cust; } } } @@ -150,7 +161,10 @@ class CreateCustomerFromWarrantyCommand extends Command $new_cv->setCustomer($cust) ->setPlateNumber($plate_number) ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) ->setVehicle($vehicle); $this->em->persist($new_cv); From 9b0bd637236882482efab310ec9cd4129179f6cb Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 06:40:37 +0000 Subject: [PATCH 13/61] Add csv file for warranties that have no phone number. #274 --- .../CreateCustomerFromWarrantyCommand.php | 125 +++++++++++++++++- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 56620ae7..df740b83 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -5,6 +5,7 @@ namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; @@ -18,6 +19,8 @@ use App\Entity\Vehicle; use App\Ramcar\FuelType; use App\Ramcar\VehicleStatusCondition; +use DateTime; + class CreateCustomerFromWarrantyCommand extends Command { protected $em; @@ -34,16 +37,30 @@ class CreateCustomerFromWarrantyCommand extends Command { $this->setName('customer:createfromwarranty') ->setDescription('Create customers from existing warranties.') - ->setHelp('Creates customers from existing warranties.'); + ->setHelp('Creates customers from existing warranties.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.'); } protected function execute(InputInterface $input, OutputInterface $output) { + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be opened.'); + } + // load all customers $this->loadCustomers(); // get all warranties $warranties = $this->em->getRepository(Warranty::class)->findAll(); + $invalid_warranties = []; foreach($warranties as $warr) { $cust_found = false; @@ -51,8 +68,9 @@ class CreateCustomerFromWarrantyCommand extends Command $w_mobile = $warr->getMobileNumber(); if (empty($w_mobile)) { - // TODO: for now, if warranty mobile number is empty, do nothing - $output->writeln('Move to next warranty since mobile number is empty'); + // TODO: for now, if warranty mobile number is empty, add to list of invalid entries + $invalid_warranties[] = $this->processInvalidEntries($warr); + continue; } @@ -79,7 +97,6 @@ class CreateCustomerFromWarrantyCommand extends Command // search cust_index for numbers in mobile_array foreach ($w_mobile_array as $w_mobile_num) { - // if present, check if customer vehicle plate number matches warranty plate number? foreach ($this->cust_index as $key => $customer) { $c_mobile = $customer->getPhoneMobile(); @@ -139,6 +156,37 @@ class CreateCustomerFromWarrantyCommand extends Command } } } + + // process invalid warranties, if any + if (count($invalid_warranties) > 0) + { + fputcsv($fh, [ + 'ID', + 'Serial', + 'Warranty Class', + 'Last Name', + 'First Name', + 'Mobile Number', + 'Plate Number', + 'Battery Model', + 'Battery Size', + 'SAP Battery', + 'Status', + 'Date Created', + 'Date Purchased', + 'Expiry Date', + 'Date Claimed', + 'Claimed From', + 'Privacy Policy', + ]); + + foreach($invalid_warranties as $row) + { + fputcsv($fh, $row); + } + } + + fclose($fh); } protected function loadCustomers() @@ -170,4 +218,73 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($new_cv); $this->em->flush(); } + + protected function processInvalidEntries($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(); + } + + $invalid_warranty = [ + 'id' => $warr->getID(), + 'serial' => $warr->getSerial(), + 'warranty_class' => $warr->getWarrantyClass(), + 'last_name' => $warr->getLastName(), + 'first_name' => $warr->getFirstName(), + 'mobile_number' => $warr->getMobileNumber(), + 'plate_number' => $warr->getPlateNumber(), + 'battery_model' => $batt_model, + 'battery_size' => $batt_size, + 'sap_battery' => $sap_batt, + 'status' => $warr->getStatus(), + 'date_create' => $date_create, + 'date_purchase' => $date_purchased, + 'date_expire' => $date_expire, + 'date_claim' => $date_claim, + 'claimed_from' => $warr->getClaimedFrom(), + 'privacy_policy' => $policy, + ]; + + return $invalid_warranty; + } } From 041d5b408b092999e48f16457844855022f2986f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 08:00:12 +0000 Subject: [PATCH 14/61] Modify the customer hash. #274 --- .../CreateCustomerFromWarrantyCommand.php | 113 +++++++++++------- 1 file changed, 73 insertions(+), 40 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index df740b83..fb5b225b 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -94,20 +94,21 @@ class CreateCustomerFromWarrantyCommand extends Command { $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); } - // search cust_index for numbers in mobile_array + foreach ($w_mobile_array as $w_mobile_num) { - foreach ($this->cust_index as $key => $customer) + if (!empty($w_mobile_num)) { - $c_mobile = $customer->getPhoneMobile(); - if (!(empty($c_mobile))) + if (isset($this->cust_index[$w_mobile_num])) { - $pos = strpos($c_mobile, $w_mobile_num); - if ($pos !== false) + $customers = $this->cust_index[$w_mobile_num]; + + foreach ($customers as $customer) { - // mobile number belongs to existing customer - // get customer vehicles - if (!(empty($c_vehicles))) + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + if (!empty($c_vehicles)) { // check if plate number of customer vehicle matches warranty plate number foreach ($c_vehicles as $c_vehicle) @@ -115,44 +116,52 @@ class CreateCustomerFromWarrantyCommand extends Command $cv_plate_number = $c_vehicle->getPlateNumber(); if ($cv_plate_number == $w_plate_number) { - // move to the next warranty since current warranty belongs to an - // existing customer and customer vehicle + // customer and customer vehicle already exists $cust_found = true; - break 3; + break; } } + if (!$cust_found) + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + } + } + else + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } - // add customer vehicle to existing customer with unknown manufacturer and make - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } } - // no customer mobile number, ignore for now - } - // if customer not found, add customer and customer vehicle - if (!($cust_found)) - { - // get warranty first name, last name - $w_first_name = $warr->getFirstName(); - $w_last_name = $warr->getLastName(); + else + { + // customer not found, add customer and customer vehicle + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); - $output->writeln($w_first_name); - $output->writeln($w_last_name); - $output->writeln($w_plate_number); + //$output->writeln($w_first_name); + //$output->writeln($w_last_name); + //$output->writeln($w_plate_number); - $new_cust = new Customer(); - $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name) - ->setPhoneMobile($w_mobile_num); + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); - $this->em->persist($new_cust); - $this->em->flush(); + $this->em->persist($new_cust); + $this->em->flush(); - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); - // add latest customer to hash - $cust_id = $new_cust->getID(); - $this->cust_index[$cust_id] = $new_cust; + // add latest customer to hash + $this->cust_index[$w_mobile_num][] = $new_cust; + } } } } @@ -186,8 +195,8 @@ class CreateCustomerFromWarrantyCommand extends Command } } - fclose($fh); - } + fclose($fh); + } protected function loadCustomers() { @@ -197,8 +206,32 @@ class CreateCustomerFromWarrantyCommand extends Command $this->cust_index = []; foreach ($customers as $customer) { - $cust_id = $customer->getID(); - $this->cust_index[$cust_id] = $customer; + $mobile = trim($customer->getPhoneMobile()); + if (!empty($mobile)) + { + $mobile_array = []; + // need to check if multiple numbers in mobile + if (preg_match('/[\\\s\/]/', $mobile)) + { + $mobile_array = preg_split('/[\\\s\/]/', $mobile); + } + else + { + // only one mobile number + $mobile_array[] = $mobile; + } + + foreach($mobile_array as $number) + { + if (!(empty($number))) + { + if (!isset($this->cust_index[$number])) + { + $this->cust_index[$number][] = $customer; + } + } + } + } } } From 98c5bdc67024af390417a7fc2d3253f0b1efd1c4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 08:41:27 +0000 Subject: [PATCH 15/61] Add clean plate number function. #274 --- .../CreateCustomerFromWarrantyCommand.php | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index fb5b225b..b36419eb 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -54,6 +54,12 @@ class CreateCustomerFromWarrantyCommand extends Command throw new Exception('The file "' . $csv_file . '" could be opened.'); } + // counters for warranties, customers, customer vehicles + $total_warr = 0; + $total_inv_warr = 0; + $total_cust_added = 0; + $total_cv_added = 0; + // load all customers $this->loadCustomers(); @@ -88,7 +94,12 @@ class CreateCustomerFromWarrantyCommand extends Command } // set values for new customer vehicle - $w_plate_number = $warr->getPlateNumber(); + $clean_plate = $this->cleanPlateNumber($warr->getPlateNumber()); + if (!($clean_plate)) + { + continue; + } + $w_plate_number = $clean_plate; $default_vehicle = $this->em->getRepository(Vehicle::class)->findOneBy(['make' =>'Unknown']); if (empty($default_vehicle)) { @@ -113,12 +124,21 @@ class CreateCustomerFromWarrantyCommand extends Command // check if plate number of customer vehicle matches warranty plate number foreach ($c_vehicles as $c_vehicle) { - $cv_plate_number = $c_vehicle->getPlateNumber(); - if ($cv_plate_number == $w_plate_number) + $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); + if (!($clean_cv_plate)) { - // customer and customer vehicle already exists + // add the vehicle from warranty $cust_found = true; - break; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + } + else + { + if ($clean_cv_plate == $w_plate_number) + { + // customer and customer vehicle already exists + $cust_found = true; + break; + } } } if (!$cust_found) @@ -320,4 +340,17 @@ class CreateCustomerFromWarrantyCommand extends Command return $invalid_warranty; } + + protected function cleanPlateNumber($plate) + { + // trim and make upper case + $clean_plate = strtoupper(trim($plate)); + + // check if alphanumeric, max length is 11, no spaces + $res = preg_match("/^[A-Z0-9]{1,11}+$/", $clean_plate); + if ($res) + return $clean_plate; + + return false; + } } From 737dd4e22a1fe3a6c39c687ec74d322ca3aa9dde Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 20 Nov 2019 11:36:56 +0000 Subject: [PATCH 16/61] Add ids to env.dist. #274 --- .env.dist | 5 + .../CreateCustomerFromWarrantyCommand.php | 270 ++++++++++-------- 2 files changed, 153 insertions(+), 122 deletions(-) diff --git a/.env.dist b/.env.dist index ca93d2d2..f170c3c9 100644 --- a/.env.dist +++ b/.env.dist @@ -45,3 +45,8 @@ OTP_MODE=settotestorrandom # geofence GEOFENCE_ENABLE=settotrueorfalse + +# unknown manufacturer and vehicle ids +CVU_MFG_ID=insertmfgidforunknownvehicles +CVU_BRAND_ID=insertbrandidforunknownvehicles + diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index b36419eb..a0b1104e 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -7,6 +7,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Dotenv\Dotenv; use Doctrine\Common\Persistence\ObjectManager; @@ -42,6 +43,13 @@ class CreateCustomerFromWarrantyCommand extends Command } protected function execute(InputInterface $input, OutputInterface $output) { + // get the default ids from .env + $dotenv = new Dotenv(); + $dotenv->loadEnv(__DIR__.'/../../.env'); + + $cvu_mfg_id = $_ENV['CVU_MFG_ID']; + $cvu_brand_id = $_ENV['CVU_BRAND_ID']; + $csv_file = $input->getArgument('file'); // attempt to open file @@ -60,162 +68,180 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cust_added = 0; $total_cv_added = 0; - // load all customers - $this->loadCustomers(); - - // get all warranties - $warranties = $this->em->getRepository(Warranty::class)->findAll(); - - $invalid_warranties = []; - foreach($warranties as $warr) + $default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id); + if (empty($default_vehicle)) { - $cust_found = false; - // check if warranty mobile already exists in customer - $w_mobile = $warr->getMobileNumber(); - if (empty($w_mobile)) - { - // TODO: for now, if warranty mobile number is empty, add to list of invalid entries - $invalid_warranties[] = $this->processInvalidEntries($warr); + $output->writeln("Need to add vehicle with default values."); + } + else + { + // load all customers + $output->writeln('Loading customer data...'); + $this->loadCustomers($output); - continue; - } + // get all warranties + $warranties = $this->em->getRepository(Warranty::class)->findAll(); - // parse warranty mobile in case of multiple numbers - // check for spaces, slash, and forward slash - $w_mobile_array = []; - if (preg_match('/[\\\s\/]/', $w_mobile)) - { - $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); - } - else - { - // only one mobile number - $w_mobile_array[] = $w_mobile; - } - - // set values for new customer vehicle - $clean_plate = $this->cleanPlateNumber($warr->getPlateNumber()); - if (!($clean_plate)) - { - continue; - } - $w_plate_number = $clean_plate; - $default_vehicle = $this->em->getRepository(Vehicle::class)->findOneBy(['make' =>'Unknown']); - if (empty($default_vehicle)) - { - $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); - } - - foreach ($w_mobile_array as $w_mobile_num) - { - if (!empty($w_mobile_num)) + $invalid_warranties = []; + $output->writeln('Processing warranties... '); + foreach($warranties as $warr) + { + $total_warr++; + $cust_found = false; + // check if warranty mobile already exists in customer + $w_mobile = $warr->getMobileNumber(); + if (empty($w_mobile)) { - if (isset($this->cust_index[$w_mobile_num])) + // TODO: for now, if warranty mobile number is empty, add to list of invalid entries + $invalid_warranties[] = $this->processInvalidEntries($warr); + + continue; + } + + // parse warranty mobile in case of multiple numbers + // check for spaces, slash, and forward slash + $w_mobile_array = []; + if (preg_match('/[\\\s\/]/', $w_mobile)) + { + $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); + } + else + { + // only one mobile number + $w_mobile_array[] = $w_mobile; + } + + // set values for new customer vehicle + $clean_plate = $this->cleanPlateNumber($warr->getPlateNumber()); + if (!($clean_plate)) + { + continue; + } + $w_plate_number = $clean_plate; + + foreach ($w_mobile_array as $w_mobile_num) + { + if (!empty($w_mobile_num)) { - $customers = $this->cust_index[$w_mobile_num]; - - foreach ($customers as $customer) + if (isset($this->cust_index[$w_mobile_num])) { - // get customer vehicles for customer - $c_vehicles = $customer->getVehicles(); + $customers = $this->cust_index[$w_mobile_num]; - if (!empty($c_vehicles)) + foreach ($customers as $customer) { - // check if plate number of customer vehicle matches warranty plate number - foreach ($c_vehicles as $c_vehicle) + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + if (!empty($c_vehicles)) { - $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); - if (!($clean_cv_plate)) + // check if plate number of customer vehicle matches warranty plate number + foreach ($c_vehicles as $c_vehicle) { - // add the vehicle from warranty - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); - } - else - { - if ($clean_cv_plate == $w_plate_number) + $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); + if (!($clean_cv_plate)) { - // customer and customer vehicle already exists + // add the vehicle from warranty $cust_found = true; - break; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + $total_cv_added++; + } + else + { + if ($clean_cv_plate == $w_plate_number) + { + // customer and customer vehicle already exists + $cust_found = true; + break; + } } } + if (!$cust_found) + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + $total_cv_added++; + } } - if (!$cust_found) + else { // customer exists but not customer vehicle // add customer vehicle to existing customer with unknown manufacturer and make $cust_found = true; $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + $total_cv_added++; } } - else - { - // customer exists but not customer vehicle - // add customer vehicle to existing customer with unknown manufacturer and make - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); - } } - } - else - { - // customer not found, add customer and customer vehicle - // get warranty first name, last name - $w_first_name = $warr->getFirstName(); - $w_last_name = $warr->getLastName(); + else + { + // customer not found, add customer and customer vehicle + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); - //$output->writeln($w_first_name); - //$output->writeln($w_last_name); - //$output->writeln($w_plate_number); + //$output->writeln($w_first_name); + //$output->writeln($w_last_name); + //$output->writeln($w_plate_number); - $new_cust = new Customer(); - $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name) - ->setPhoneMobile($w_mobile_num); + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); - $this->em->persist($new_cust); - $this->em->flush(); + $this->em->persist($new_cust); + $this->em->flush(); - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); - // add latest customer to hash - $this->cust_index[$w_mobile_num][] = $new_cust; + // add latest customer to hash + $this->cust_index[$w_mobile_num][] = $new_cust; + + $total_cust_added++; + $total_cv_added++; + } } } } - } - // process invalid warranties, if any - if (count($invalid_warranties) > 0) - { - fputcsv($fh, [ - 'ID', - 'Serial', - 'Warranty Class', - 'Last Name', - 'First Name', - 'Mobile Number', - 'Plate Number', - 'Battery Model', - 'Battery Size', - 'SAP Battery', - 'Status', - 'Date Created', - 'Date Purchased', - 'Expiry Date', - 'Date Claimed', - 'Claimed From', - 'Privacy Policy', - ]); - - foreach($invalid_warranties as $row) + // process invalid warranties, if any + if (count($invalid_warranties) > 0) { - fputcsv($fh, $row); - } - } + fputcsv($fh, [ + 'ID', + 'Serial', + 'Warranty Class', + 'Last Name', + 'First Name', + 'Mobile Number', + 'Plate Number', + 'Battery Model', + 'Battery Size', + 'SAP Battery', + 'Status', + 'Date Created', + 'Date Purchased', + 'Expiry Date', + 'Date Claimed', + 'Claimed From', + 'Privacy Policy', + ]); - fclose($fh); + foreach($invalid_warranties as $row) + { + $total_inv_warr++; + fputcsv($fh, $row); + } + } + + fclose($fh); + + $output->writeln('Total warranties: ' . $total_warr); + $output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); + $output->writeln('Total customers added: ' . $total_cust_added); + $output->writeln('Total customer vehicles added: ' . $total_cv_added); + } } protected function loadCustomers() From d094b5e8d91b59b2ae76c01496977ce5940a47ca Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 00:02:44 +0800 Subject: [PATCH 17/61] Refactor create customer from warranty command #274 --- composer.json | 1 + composer.lock | 53 ++- .../CreateCustomerFromWarrantyCommand.php | 326 +++++++++--------- src/Entity/Customer.php | 2 +- symfony.lock | 3 + utils/fcm_sender/file_send.php | 23 ++ 6 files changed, 247 insertions(+), 161 deletions(-) create mode 100644 utils/fcm_sender/file_send.php diff --git a/composer.json b/composer.json index 6c67a564..e118abfc 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "catalyst/menu-bundle": "dev-master", "creof/doctrine2-spatial": "^1.2", "data-dog/audit-bundle": "^0.1.10", + "edwinhoksberg/php-fcm": "^1.0", "guzzlehttp/guzzle": "^6.3", "predis/predis": "^1.1", "sensio/framework-extra-bundle": "^5.1", diff --git a/composer.lock b/composer.lock index 54f7abba..b3630199 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "ef9a215401e1fec51336e1d6a9df52d9", + "content-hash": "4873ae3fd18db755bc9bf395bbbfb141", "packages": [ { "name": "catalyst/auth-bundle", @@ -1564,6 +1564,55 @@ ], "time": "2018-06-14T14:45:07+00:00" }, + { + "name": "edwinhoksberg/php-fcm", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/EdwinHoksberg/php-fcm.git", + "reference": "7be637139fe54ec23f37c8dba519bafa7543e336" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/EdwinHoksberg/php-fcm/zipball/7be637139fe54ec23f37c8dba519bafa7543e336", + "reference": "7be637139fe54ec23f37c8dba519bafa7543e336", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.3", + "php": ">= 7.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^6.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Fcm\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Edwin Hoksberg", + "email": "mail@edwinhoksberg.nl" + } + ], + "description": "A library for sending Firebase cloud messages and managing user topic subscriptions, device groups and devices.", + "homepage": "https://github.com/EdwinHoksberg/php-fcm", + "keywords": [ + "FCM", + "Firebase Cloud Messaging", + "firebase", + "google", + "notifications" + ], + "time": "2018-04-09T19:32:41+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "6.3.3", diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index a0b1104e..8265b97e 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -44,6 +44,7 @@ class CreateCustomerFromWarrantyCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { // get the default ids from .env + // TODO: DO NOT USE $_ENV $dotenv = new Dotenv(); $dotenv->loadEnv(__DIR__.'/../../.env'); @@ -68,190 +69,207 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cust_added = 0; $total_cv_added = 0; + // get default vehicle $default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id); if (empty($default_vehicle)) { $output->writeln("Need to add vehicle with default values."); + return; } - else - { - // load all customers - $output->writeln('Loading customer data...'); - $this->loadCustomers($output); - // get all warranties - $warranties = $this->em->getRepository(Warranty::class)->findAll(); + /* + // load all customers + $output->writeln('Loading customer data...'); + $this->loadCustomers($output); + */ - $invalid_warranties = []; - $output->writeln('Processing warranties... '); - foreach($warranties as $warr) - { - $total_warr++; - $cust_found = false; - // check if warranty mobile already exists in customer - $w_mobile = $warr->getMobileNumber(); - if (empty($w_mobile)) - { - // TODO: for now, if warranty mobile number is empty, add to list of invalid entries - $invalid_warranties[] = $this->processInvalidEntries($warr); + // get all warranties + error_log('Getting warranties...'); + $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.mobile_number is not null'); + $warranties = $warr_q->getResult(); + // $warranties = $this->em->getRepository(Warranty::class)->findAll(); + + $invalid_warranties = []; + $warr_count = count($warranties); + $output->writeln("Processing $warr_count warranties... "); + foreach($warranties as $warr) + { + $total_warr++; + // check if warranty mobile already exists in customer + $w_mobile = $warr->getMobileNumber(); + if (empty($w_mobile)) + { + // TODO: for now, if warranty mobile number is empty, add to list of invalid entries + $invalid_warranties[] = $this->processInvalidEntries($warr); + continue; + } + + // parse warranty mobile in case of multiple numbers + // check for spaces, slash, and forward slash + $w_mobile_array = []; + if (preg_match('/[\\\s\/]/', $w_mobile)) + { + $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); + } + else + { + // only one mobile number + $w_mobile_array[] = $w_mobile; + } + + // set values for new customer vehicle + $w_plate_number = $this->cleanPlateNumber($warr->getPlateNumber()); + + $cust_found = false; + foreach ($w_mobile_array as $w_mobile_num) + { + // empty mobile num + if (empty($w_mobile_num)) continue; - } - // parse warranty mobile in case of multiple numbers - // check for spaces, slash, and forward slash - $w_mobile_array = []; - if (preg_match('/[\\\s\/]/', $w_mobile)) - { - $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); - } - else - { - // only one mobile number - $w_mobile_array[] = $w_mobile; - } + error_log(''); + error_log("($total_warr) processing $w_mobile_num from warranty..."); - // set values for new customer vehicle - $clean_plate = $this->cleanPlateNumber($warr->getPlateNumber()); - if (!($clean_plate)) - { - continue; - } - $w_plate_number = $clean_plate; + $customers = $this->findCustomerByNumber($w_mobile_num); - foreach ($w_mobile_array as $w_mobile_num) + if (!empty($customers)) { - if (!empty($w_mobile_num)) + error_log('found customer for ' . $w_mobile_num); + foreach ($customers as $customer) { - if (isset($this->cust_index[$w_mobile_num])) + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + $cv_found = false; + if (!empty($c_vehicles)) { - $customers = $this->cust_index[$w_mobile_num]; - - foreach ($customers as $customer) + // check if plate number of customer vehicle matches warranty plate number + foreach ($c_vehicles as $c_vehicle) { - // get customer vehicles for customer - $c_vehicles = $customer->getVehicles(); + $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); - if (!empty($c_vehicles)) + // check if it's already there + if ($clean_cv_plate == $w_plate_number) { - // check if plate number of customer vehicle matches warranty plate number - foreach ($c_vehicles as $c_vehicle) - { - $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); - if (!($clean_cv_plate)) - { - // add the vehicle from warranty - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); - $total_cv_added++; - } - else - { - if ($clean_cv_plate == $w_plate_number) - { - // customer and customer vehicle already exists - $cust_found = true; - break; - } - } - } - if (!$cust_found) - { - // customer exists but not customer vehicle - // add customer vehicle to existing customer with unknown manufacturer and make - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); - $total_cv_added++; - } - } - else - { - // customer exists but not customer vehicle - // add customer vehicle to existing customer with unknown manufacturer and make - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); - $total_cv_added++; + // customer and customer vehicle already exists + $cv_found = true; + break; } } + + } + + // if there was a customer vehicle matched + if ($cv_found) + { + error_log('vehicle found - ' . $w_plate_number); } else { - // customer not found, add customer and customer vehicle - // get warranty first name, last name - $w_first_name = $warr->getFirstName(); - $w_last_name = $warr->getLastName(); - - //$output->writeln($w_first_name); - //$output->writeln($w_last_name); - //$output->writeln($w_plate_number); - - $new_cust = new Customer(); - $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name) - ->setPhoneMobile($w_mobile_num); - - $this->em->persist($new_cust); - $this->em->flush(); - - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); - - // add latest customer to hash - $this->cust_index[$w_mobile_num][] = $new_cust; - - $total_cust_added++; + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + error_log('new vehicle - ' . $w_plate_number); + /* + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); $total_cv_added++; + */ } } } - } - - // process invalid warranties, if any - if (count($invalid_warranties) > 0) - { - fputcsv($fh, [ - 'ID', - 'Serial', - 'Warranty Class', - 'Last Name', - 'First Name', - 'Mobile Number', - 'Plate Number', - 'Battery Model', - 'Battery Size', - 'SAP Battery', - 'Status', - 'Date Created', - 'Date Purchased', - 'Expiry Date', - 'Date Claimed', - 'Claimed From', - 'Privacy Policy', - ]); - - foreach($invalid_warranties as $row) + // customer not found + else { - $total_inv_warr++; - fputcsv($fh, $row); + error_log('NEW customer and vehicle - ' . $w_plate_number); + /* + // customer not found, add customer and customer vehicle + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); + + //$output->writeln($w_first_name); + //$output->writeln($w_last_name); + //$output->writeln($w_plate_number); + + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); + + $this->em->persist($new_cust); + $this->em->flush(); + + $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + + // add latest customer to hash + $this->cust_index[$w_mobile_num][] = $new_cust; + + $total_cust_added++; + $total_cv_added++; + */ } } - - fclose($fh); - - $output->writeln('Total warranties: ' . $total_warr); - $output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); - $output->writeln('Total customers added: ' . $total_cust_added); - $output->writeln('Total customer vehicles added: ' . $total_cv_added); } - } + + /* + // process invalid warranties, if any + if (count($invalid_warranties) > 0) + { + fputcsv($fh, [ + 'ID', + 'Serial', + 'Warranty Class', + 'Last Name', + 'First Name', + 'Mobile Number', + 'Plate Number', + 'Battery Model', + 'Battery Size', + 'SAP Battery', + 'Status', + 'Date Created', + 'Date Purchased', + 'Expiry Date', + 'Date Claimed', + 'Claimed From', + 'Privacy Policy', + ]); + + foreach($invalid_warranties as $row) + { + $total_inv_warr++; + fputcsv($fh, $row); + } + } + */ + + fclose($fh); + + $output->writeln('Total warranties: ' . $total_warr); + $output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); + $output->writeln('Total customers added: ' . $total_cust_added); + $output->writeln('Total customer vehicles added: ' . $total_cv_added); + } + + protected function findCustomerByNumber($number) + { + $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + return $customers; + } protected function loadCustomers() { + error_log('starting query...'); // get all customers $customers = $this->em->getRepository(Customer::class)->findAll(); + $cust_q = $this->em->createQuery('select c from App\Entity\Customer c'); + $cust_iter = $q->iterate(); + error_log('looping through query...'); $this->cust_index = []; - foreach ($customers as $customer) + foreach ($cust_iter as $customer) { + error_log('here'); $mobile = trim($customer->getPhoneMobile()); if (!empty($mobile)) { @@ -272,9 +290,8 @@ class CreateCustomerFromWarrantyCommand extends Command if (!(empty($number))) { if (!isset($this->cust_index[$number])) - { - $this->cust_index[$number][] = $customer; - } + $this->cust_index[$number] = []; + $this->cust_index[$number][] = $customer; } } } @@ -329,7 +346,7 @@ class CreateCustomerFromWarrantyCommand extends Command if ($warr->getBatteryModel() != null) { - $batt_model = $warr->getBatteryModel()-getName(); + $batt_model = $warr->getBatteryModel()->getName(); } if ($warr->getBatterySize() != null) { @@ -369,14 +386,7 @@ class CreateCustomerFromWarrantyCommand extends Command protected function cleanPlateNumber($plate) { - // trim and make upper case - $clean_plate = strtoupper(trim($plate)); - - // check if alphanumeric, max length is 11, no spaces - $res = preg_match("/^[A-Z0-9]{1,11}+$/", $clean_plate); - if ($res) - return $clean_plate; - - return false; + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); } } diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 61b0c392..880f0213 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -10,7 +10,7 @@ use App\Ramcar\CustomerClassification; /** * @ORM\Entity - * @ORM\Table(name="customer") + * @ORM\Table(name="customer", indexes={@ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"})}) */ class Customer { diff --git a/symfony.lock b/symfony.lock index 81b878d9..88d10d33 100644 --- a/symfony.lock +++ b/symfony.lock @@ -98,6 +98,9 @@ "doctrine/reflection": { "version": "v1.0.0" }, + "edwinhoksberg/php-fcm": { + "version": "1.0.0" + }, "guzzlehttp/guzzle": { "version": "6.3.0" }, diff --git a/utils/fcm_sender/file_send.php b/utils/fcm_sender/file_send.php new file mode 100644 index 00000000..e413a8df --- /dev/null +++ b/utils/fcm_sender/file_send.php @@ -0,0 +1,23 @@ +addRecipient($device_id) + ->setTitle('Motolite RES-Q') + ->setBody('Test notification sending') + +// Send the notification to the Firebase servers for further handling. +$client->send($notification); From 142e34ba3165af7c3f8b3e54a46f8b33364020ac Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 08:53:48 +0800 Subject: [PATCH 18/61] Refactor to use bulk processing best practice based on doctrine docs #274 Reference: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/batch-processing.html --- src/Command/CreateCustomerFromWarrantyCommand.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 8265b97e..ddc6d3a4 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -87,14 +87,17 @@ class CreateCustomerFromWarrantyCommand extends Command // get all warranties error_log('Getting warranties...'); $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.mobile_number is not null'); - $warranties = $warr_q->getResult(); + $warranties = $warr_q->iterate(); + // $warranties = $warr_q->getResult(); // $warranties = $this->em->getRepository(Warranty::class)->findAll(); $invalid_warranties = []; - $warr_count = count($warranties); - $output->writeln("Processing $warr_count warranties... "); - foreach($warranties as $warr) - { + // $warr_count = count($warranties); + $output->writeln("Processing warranties... "); + foreach($warranties as $row) + { + $warr = $row[0]; + $total_warr++; // check if warranty mobile already exists in customer $w_mobile = $warr->getMobileNumber(); @@ -209,6 +212,7 @@ class CreateCustomerFromWarrantyCommand extends Command */ } } + $this->em->clear(); } /* From 575bf21639e0e1474713c1680dd29b83db37445f Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 09:10:58 +0800 Subject: [PATCH 19/61] Fix blank phone number issue #274 --- src/Command/CreateCustomerFromWarrantyCommand.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index ddc6d3a4..9303c0c4 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -127,10 +127,19 @@ class CreateCustomerFromWarrantyCommand extends Command $cust_found = false; foreach ($w_mobile_array as $w_mobile_num) { + $w_mobile_num = trim($w_mobile_num); + // empty mobile num if (empty($w_mobile_num)) continue; + // min length 2 + // TODO: we need to check proper phone number format + // format should be '9XXXXXXXXX' + // TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert + if (strlen($w_mobile_num <= 2)) + continue; + error_log(''); error_log("($total_warr) processing $w_mobile_num from warranty..."); @@ -249,6 +258,7 @@ class CreateCustomerFromWarrantyCommand extends Command fclose($fh); + $output->writeln(''); $output->writeln('Total warranties: ' . $total_warr); $output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); $output->writeln('Total customers added: ' . $total_cust_added); From 9a4ead6ab9c74c51934def9e1e93836de1fa939d Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 09:18:31 +0800 Subject: [PATCH 20/61] Return the customer and customer vehicle saving #274 --- src/Command/CreateCustomerFromWarrantyCommand.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 9303c0c4..841be142 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -175,6 +175,7 @@ class CreateCustomerFromWarrantyCommand extends Command // if there was a customer vehicle matched if ($cv_found) { + // vehicle found, do nothing. error_log('vehicle found - ' . $w_plate_number); } else @@ -182,10 +183,8 @@ class CreateCustomerFromWarrantyCommand extends Command // customer exists but not customer vehicle // add customer vehicle to existing customer with unknown manufacturer and make error_log('new vehicle - ' . $w_plate_number); - /* $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); $total_cv_added++; - */ } } } @@ -193,7 +192,6 @@ class CreateCustomerFromWarrantyCommand extends Command else { error_log('NEW customer and vehicle - ' . $w_plate_number); - /* // customer not found, add customer and customer vehicle // get warranty first name, last name $w_first_name = $warr->getFirstName(); @@ -209,7 +207,6 @@ class CreateCustomerFromWarrantyCommand extends Command ->setPhoneMobile($w_mobile_num); $this->em->persist($new_cust); - $this->em->flush(); $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); @@ -218,9 +215,9 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cust_added++; $total_cv_added++; - */ } } + $this->em->flush(); $this->em->clear(); } @@ -326,7 +323,6 @@ class CreateCustomerFromWarrantyCommand extends Command ->setVehicle($vehicle); $this->em->persist($new_cv); - $this->em->flush(); } protected function processInvalidEntries($warr) From fba5d9cb86e5df86ae79ab3636c1c55503159858 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 09:27:35 +0800 Subject: [PATCH 21/61] Add method for getting default vehicle because of em->clear #274 --- .../CreateCustomerFromWarrantyCommand.php | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 841be142..38758990 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -27,10 +27,21 @@ class CreateCustomerFromWarrantyCommand extends Command protected $em; protected $cust_index; + protected $cvu_mfg_id; + protected $cvu_brand_id; + public function __construct(ObjectManager $em) { $this->em = $em; + // get the default ids from .env + // TODO: DO NOT USE $_ENV + $dotenv = new Dotenv(); + $dotenv->loadEnv(__DIR__.'/../../.env'); + + $this->cvu_mfg_id = $_ENV['CVU_MFG_ID']; + $this->cvu_brand_id = $_ENV['CVU_BRAND_ID']; + parent::__construct(); } @@ -41,16 +52,9 @@ class CreateCustomerFromWarrantyCommand extends Command ->setHelp('Creates customers from existing warranties.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.'); } + protected function execute(InputInterface $input, OutputInterface $output) { - // get the default ids from .env - // TODO: DO NOT USE $_ENV - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $cvu_mfg_id = $_ENV['CVU_MFG_ID']; - $cvu_brand_id = $_ENV['CVU_BRAND_ID']; - $csv_file = $input->getArgument('file'); // attempt to open file @@ -69,13 +73,6 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cust_added = 0; $total_cv_added = 0; - // get default vehicle - $default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id); - if (empty($default_vehicle)) - { - $output->writeln("Need to add vehicle with default values."); - return; - } /* // load all customers @@ -183,7 +180,7 @@ class CreateCustomerFromWarrantyCommand extends Command // customer exists but not customer vehicle // add customer vehicle to existing customer with unknown manufacturer and make error_log('new vehicle - ' . $w_plate_number); - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($customer, $this->getDefaultVehicle(), $w_plate_number); $total_cv_added++; } } @@ -208,7 +205,7 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($new_cust); - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $this->getDefaultVehicle(), $w_plate_number); // add latest customer to hash $this->cust_index[$w_mobile_num][] = $new_cust; @@ -262,6 +259,19 @@ class CreateCustomerFromWarrantyCommand extends Command $output->writeln('Total customer vehicles added: ' . $total_cv_added); } + protected function getDefaultVehicle() + { + // get default vehicle + $default_vehicle = $this->em->getRepository(Vehicle::class)->find($this->cvu_brand_id); + if ($default_vehicle == null) + { + $output->writeln("Need to add vehicle with default values."); + return null; + } + + return $default_vehicle; + } + protected function findCustomerByNumber($number) { $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); From 2b21db6c0119bf3ee31f88a55b6ea1a41825c353 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 10:20:07 +0800 Subject: [PATCH 22/61] Filter valid phone numbers and convert the 09XXXXXXXXX format to 9XXXXXXXXX #274 --- src/Command/CreateCustomerFromWarrantyCommand.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 38758990..bfac02d8 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -130,12 +130,26 @@ class CreateCustomerFromWarrantyCommand extends Command if (empty($w_mobile_num)) continue; + // does it fit our 09XXXXXXXXX pattern? + if (preg_match('/^09[0-9]{9}$/', $w_mobile_num)) + { + // remove first '0' + $w_mobile_num = substr($w_mobile_num, 1); + error_log("CONVERTED TO $w_mobile_num"); + } + + // does it fit our 9XXXXXXXXX pattern? + if (!preg_match('/^9[0-9]{9}$/', $w_mobile_num)) + continue; + + /* // min length 2 // TODO: we need to check proper phone number format // format should be '9XXXXXXXXX' // TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert if (strlen($w_mobile_num <= 2)) continue; + */ error_log(''); error_log("($total_warr) processing $w_mobile_num from warranty..."); From f914fce20fb819df914fdc0a37bc16c06302bd15 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 Nov 2019 02:39:31 +0000 Subject: [PATCH 23/61] Add report of added customers and customer vehicles as output. #274 --- .../CreateCustomerFromWarrantyCommand.php | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index bfac02d8..9d24fc03 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -24,6 +24,10 @@ use DateTime; class CreateCustomerFromWarrantyCommand extends Command { + const CV_FOUND = 'Vehicle found'; + const CV_NEW = 'New vehicle'; + const CUST_NEW = 'New customer and vehicle.'; + protected $em; protected $cust_index; @@ -88,8 +92,9 @@ class CreateCustomerFromWarrantyCommand extends Command // $warranties = $warr_q->getResult(); // $warranties = $this->em->getRepository(Warranty::class)->findAll(); - $invalid_warranties = []; + //$invalid_warranties = []; // $warr_count = count($warranties); + $created_objs = []; $output->writeln("Processing warranties... "); foreach($warranties as $row) { @@ -101,7 +106,7 @@ class CreateCustomerFromWarrantyCommand extends Command if (empty($w_mobile)) { // TODO: for now, if warranty mobile number is empty, add to list of invalid entries - $invalid_warranties[] = $this->processInvalidEntries($warr); + //$invalid_warranties[] = $this->processInvalidEntries($warr); continue; } @@ -188,6 +193,7 @@ class CreateCustomerFromWarrantyCommand extends Command { // vehicle found, do nothing. error_log('vehicle found - ' . $w_plate_number); + $created_objs[] = $this->createReportData($warr, self::CV_FOUND); } else { @@ -195,6 +201,7 @@ class CreateCustomerFromWarrantyCommand extends Command // add customer vehicle to existing customer with unknown manufacturer and make error_log('new vehicle - ' . $w_plate_number); $this->createCustomerVehicle($customer, $this->getDefaultVehicle(), $w_plate_number); + $created_objs[] = $this->createReportData($warr, self::CV_NEW); $total_cv_added++; } } @@ -221,8 +228,10 @@ class CreateCustomerFromWarrantyCommand extends Command $this->createCustomerVehicle($new_cust, $this->getDefaultVehicle(), $w_plate_number); + $created_objs[] = $this->createReportData($warr, self::CUST_NEW); + // add latest customer to hash - $this->cust_index[$w_mobile_num][] = $new_cust; + //$this->cust_index[$w_mobile_num][] = $new_cust; $total_cust_added++; $total_cv_added++; @@ -264,11 +273,27 @@ class CreateCustomerFromWarrantyCommand extends Command } */ + // process the report data + if (count($created_objs) > 0) + { + fputcsv($fh, [ + 'ID', + 'Mobile Number', + 'Plate Number', + 'Action Done', + ]); + + foreach($created_objs as $row) + { + fputcsv($fh, $row); + } + } + fclose($fh); $output->writeln(''); $output->writeln('Total warranties: ' . $total_warr); - $output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); + //$output->writeln('Total warranties with no mobile number: ' . $total_inv_warr); $output->writeln('Total customers added: ' . $total_cust_added); $output->writeln('Total customer vehicles added: ' . $total_cv_added); } @@ -349,6 +374,19 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($new_cv); } + protected function createReportData($warr, $action) + { + $obj = [ + 'id' => $warr->getID(), + 'mobile_number' => $warr->getMobileNumber(), + 'plate_number' => $warr->getPlateNumber(), + 'action_done' => $action, + ]; + + return $obj; + + } + protected function processInvalidEntries($warr) { $batt_model = ''; From 0adffd92d7bfd0d0e5a48d5ba9fa0c67456e1f20 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 Nov 2019 06:16:31 +0000 Subject: [PATCH 24/61] Add checking if mobile session belongs to an existing customer. #277 --- src/Controller/APIController.php | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 0b360d9c..8ee33c0a 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -334,7 +334,6 @@ class APIController extends Controller // TODO: check if we have the number registered before and merge - // TODO: check if mobile matches mobile of customer $dupe_sess = $this->findNumberSession($this->session->getPhoneNumber()); if ($dupe_sess != null) { @@ -342,6 +341,15 @@ class APIController extends Controller $this->session->setCustomer($dupe_cust); } + // TODO: check if mobile matches mobile of customer + $customer = $this->findCustomerByNumber($this->session->getPhoneNumber()); + if ($customer != null) + { + // TODO: if there is a dupe_sess, do we need to check if + // dupe_cust is the same as the customer we found? + $this->session->setCustomer($customer); + } + $em->flush(); // response @@ -2180,4 +2188,28 @@ class APIController extends Controller return $res->getReturnResponse(); } + + protected function findCustomerByNumber($number) + { + $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + + // find the customer with the most number of cars + $car_count = 0; + $cust = null; + + foreach($customers as $customer) + { + $vehicles = $customer->getVehicles(); + if (count($vehicles) > $car_count) + { + $car_count = count($vehicles); + + // "save" customer object + $cust = $customer; + } + } + + return $cust; + } + } From bf86a460827c3e94d291eff512ba2431bb4f0a73 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 Nov 2019 08:03:33 +0000 Subject: [PATCH 25/61] Fix for errors found during testing. #277 --- src/Controller/APIController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 8ee33c0a..197021c5 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -2191,7 +2191,8 @@ class APIController extends Controller protected function findCustomerByNumber($number) { - $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + $em = $this->getDoctrine()->getManager(); + $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); // find the customer with the most number of cars $car_count = 0; From e462570d9df590bb40af8cc319708e52ca3b60c2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 22 Nov 2019 03:27:16 +0000 Subject: [PATCH 26/61] Remove dotenv implementation from setting of customer privacy policy. #275 --- config/services.yaml | 6 +++++ src/Command/ImportPartnersCommand.php | 2 +- .../SetCustomerPrivacyPolicyCommand.php | 26 +++++++++---------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index c6f2f877..5e1b95c5 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,6 +91,12 @@ services: arguments: $geofence_flag: "%env(GEOFENCE_ENABLE)%" + App\Command\SetCustomerPrivacyPolicyCommand: + arguments: + $policy_promo: "%env(POLICY_PROMO)%" + $policy_third_party: "%env(POLICY_THIRD_PARTY)%" + $policy_mobile: "%env(POLICY_MOBILE)%" + Catalyst\APIBundle\Security\APIKeyUserProvider: arguments: $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/ImportPartnersCommand.php b/src/Command/ImportPartnersCommand.php index 526fb282..74e02d5b 100644 --- a/src/Command/ImportPartnersCommand.php +++ b/src/Command/ImportPartnersCommand.php @@ -34,7 +34,7 @@ class ImportPartnersCommand extends Command protected $em; - public function __construct(Objectmanager $om) + public function __construct(ObjectManager $om) { $this->em = $om; diff --git a/src/Command/SetCustomerPrivacyPolicyCommand.php b/src/Command/SetCustomerPrivacyPolicyCommand.php index d4431175..4132fa91 100644 --- a/src/Command/SetCustomerPrivacyPolicyCommand.php +++ b/src/Command/SetCustomerPrivacyPolicyCommand.php @@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Dotenv\Dotenv; use Doctrine\Common\Persistence\ObjectManager; @@ -19,10 +18,19 @@ class SetCustomerPrivacyPolicyCommand extends Command { private $em; - public function __construct(ObjectManager $om) + private $policy_promo_id; + private $policy_third_party_id; + private $policy_mobile_id; + + public function __construct(ObjectManager $om, $policy_promo, + $policy_third_party, $policy_mobile) { $this->em = $om; + $this->policy_promo_id = $policy_promo; + $this->policy_third_party_id = $policy_third_party; + $this->policy_mobile_id = $policy_mobile; + parent::__construct(); } @@ -35,16 +43,8 @@ class SetCustomerPrivacyPolicyCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - // get the policy ids from .env - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $policy_promo_id = $_ENV['POLICY_PROMO']; - $policy_third_party_id = $_ENV['POLICY_THIRD_PARTY']; - $policy_mobile_id = $_ENV['POLICY_MOBILE']; - // get third party policy - $third_party_policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_third_party_id); + $third_party_policy = $this->em->getRepository(PrivacyPolicy::class)->find($this->policy_third_party_id); // get customers on third party $third_party_customers = $this->em->getRepository(Customer::class)->findBy(['priv_third_party' => true]); @@ -54,7 +54,7 @@ class SetCustomerPrivacyPolicyCommand extends Command } // get promo policy - $promo_policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_promo_id); + $promo_policy = $this->em->getRepository(PrivacyPolicy::class)->find($this->policy_promo_id); // get customers on promo $promo_customers = $this->em->getRepository(Customer::class)->findBy(['priv_promo' => true]); @@ -66,7 +66,7 @@ class SetCustomerPrivacyPolicyCommand extends Command $this->em->flush(); // get mobile policy - $mobile_policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_mobile_id); + $mobile_policy = $this->em->getRepository(PrivacyPolicy::class)->find($this->policy_mobile_id); // get mobile sessions $mobile_sessions = $this->em->getRepository(MobileSession::class)->findAll(); From 0372dc5a5af2d31a48d6991644b8b286a6263f9f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 22 Nov 2019 04:01:54 +0000 Subject: [PATCH 27/61] Remove dotenv implementation from customer creation from warranty. #275 --- config/services.yaml | 5 +++++ src/Command/CreateCustomerFromWarrantyCommand.php | 11 +++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 5e1b95c5..42044b7d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -97,6 +97,11 @@ services: $policy_third_party: "%env(POLICY_THIRD_PARTY)%" $policy_mobile: "%env(POLICY_MOBILE)%" + App\Command\CreateCustomerFromWarrantyCommand: + arguments: + $cvu_mfg_id: "%env(CVU_MFG_ID)%" + $cvu_brand_id: "%env(CVU_BRAND_ID)%" + Catalyst\APIBundle\Security\APIKeyUserProvider: arguments: $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 9d24fc03..91ae38d1 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -34,17 +34,12 @@ class CreateCustomerFromWarrantyCommand extends Command protected $cvu_mfg_id; protected $cvu_brand_id; - public function __construct(ObjectManager $em) + public function __construct(ObjectManager $em, $cvu_mfg_id, $cvu_brand_id) { $this->em = $em; - // get the default ids from .env - // TODO: DO NOT USE $_ENV - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $this->cvu_mfg_id = $_ENV['CVU_MFG_ID']; - $this->cvu_brand_id = $_ENV['CVU_BRAND_ID']; + $this->cvu_mfg_id = $cvu_mfg_id; + $this->cvu_brand_id = $cvu_brand_id; parent::__construct(); } From 7e5ffdcf26298f61a6ee42c09592dabe48da8aad Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 25 Nov 2019 03:21:50 +0000 Subject: [PATCH 28/61] Add index for plate number. #278 --- src/Controller/ReportController.php | 102 ++++++++++++++-------------- src/Entity/CustomerVehicle.php | 3 +- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 8558de79..60db4d9d 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -589,6 +589,7 @@ class ReportController extends Controller 'plate_num' => '', 'warr_date_create' => '', 'warr_activation_status' => '', + 'warr_class' => '', 'has_mobile' => '', 'date_mobile' => '', 'mobile_number' => '', @@ -606,67 +607,68 @@ class ReportController extends Controller if (!empty($serial)) { // get the warranty for serial - $warr_qb = $this->getDoctrine() - ->getRepository(Warranty::class) - ->createQueryBuilder('q'); - $warranty_query = $warr_qb->select('q') - ->where('q.serial = :serial') - ->setParameter('serial', $serial); - $warranty = $warranty_query->getQuery()->getOneOrNullResult(); + $warranties = $em->getRepository(Warranty::class)->findBy(['serial' => $serial]); - if ($warranty != null) + if (!empty($warranties)) { - $isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber()); - if ($isValid) + foreach ($warranties as $warranty) { - // get customer vehicles using plate number - $customer_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]); - - // check if customer vehicle is empty - if (count($customer_vehicles) != 0) + //error_log('found warranty for serial ' . $serial); + $plate_number = $warranty->getPlateNumber(); + $isValid = InvalidPlateNumber::isInvalid($plate_number); + if ($isValid) { - $has_mobile = false; - $mobile_date = ''; - $mobile_number = ''; - - // get the first customer vehicle, store as best_cv until we find one with a mobile session - $best_cv = current($customer_vehicles); - - foreach($customer_vehicles as $cv) + // get customer vehicles using plate number + $customer_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + + // check if customer vehicle is empty + if (count($customer_vehicles) != 0) { - // get mobile session of customer - //error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName()); - $mobile_session = $em->getRepository(MobileSession::class) - ->findOneBy(['customer' => $cv->getCustomer()->getID()], ['date_generated' => 'ASC']); - if ($mobile_session != null) + //error_log('found customer vehicle for plate number ' . $plate_number); + $has_mobile = false; + $mobile_date = ''; + $mobile_number = ''; + + // get the first customer vehicle, store as best_cv until we find one with a mobile session + $best_cv = current($customer_vehicles); + + foreach($customer_vehicles as $cv) { - // get mobile data - $has_mobile = true; - $mobile_date = $mobile_session->getDateGenerated()->format("d M Y"); - $mobile_number = $mobile_session->getPhoneNumber(); + // get mobile session of customer + //error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName()); + $cust_id = $cv->getCustomer()->getID(); + $mobile_session = $em->getRepository(MobileSession::class) + ->findOneBy(['customer' => $cust_id], ['date_generated' => 'ASC']); + if ($mobile_session != null) + { + // get mobile data + //error_log('found mobile session for customer id ' . $cv->getCustomer()->getID()); + $has_mobile = true; + $mobile_date = $mobile_session->getDateGenerated()->format("d M Y"); + $mobile_number = $mobile_session->getPhoneNumber(); - // set best_cv to this customer vehicle with mobile session - $best_cv = $cv; + // set best_cv to this customer vehicle with mobile session + $best_cv = $cv; + } } + // set the customer data in results + $results[$key]['cust_id'] = $best_cv->getCustomer()->getID(); + $results[$key]['cust_lastname'] = $best_cv->getCustomer()->getLastName(); + $results[$key]['cust_firstname'] = $best_cv->getCustomer()->getFirstName(); + $results[$key]['cust_mobile_number'] = $best_cv->getCustomer()->getPhoneMobile(); + $results[$key]['plate_num'] = $best_cv->getPlateNumber(); + $results[$key]['has_mobile'] = ($has_mobile ? 'Yes' : 'No'); + $results[$key]['date_mobile'] = $mobile_date; + $results[$key]['mobile_number'] = $mobile_number; } - - // set the customer data in results - $results[$key]['cust_id'] = $best_cv->getCustomer()->getID(); - $results[$key]['cust_lastname'] = $best_cv->getCustomer()->getLastName(); - $results[$key]['cust_firstname'] = $best_cv->getCustomer()->getFirstName(); - $results[$key]['cust_mobile_number'] = $best_cv->getCustomer()->getPhoneMobile(); - $results[$key]['plate_num'] = $best_cv->getPlateNumber(); - $results[$key]['has_mobile'] = ($has_mobile ? 'Yes' : 'No'); - $results[$key]['date_mobile'] = $mobile_date; - $results[$key]['mobile_number'] = $mobile_number; } + // set the warranty data in results + $results[$key]['warr_lastname'] = $warranty->getLastName(); + $results[$key]['warr_firstname'] = $warranty->getFirstName(); + $results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y"); + $results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive'); + $results[$key]['warr_class'] = $warranty->getWarrantyClass(); } - // set the warranty data in results - $results[$key]['warr_lastname'] = $warranty->getLastName(); - $results[$key]['warr_firstname'] = $warranty->getFirstName(); - $results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y"); - $results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive'); - $results[$key]['warr_class'] = $warranty->getWarrantyClass(); } } } diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index 5f1f1031..3e4c70a4 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -10,7 +10,8 @@ use DateTime; /** * @ORM\Entity - * @ORM\Table(name="customer_vehicle", indexes={@ORM\Index(columns={"plate_number"}, flags={"fulltext"})}) + * @ORM\Table(name="customer_vehicle", indexes={@ORM\Index(columns={"plate_number"}, flags={"fulltext"}), + @ORM\Index(name="plate_number_idx", columns={"plate_number"})}) */ class CustomerVehicle { From 9efc0e3a75b6cb7ecea0b220e606899afbad75b2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 26 Nov 2019 07:53:31 +0000 Subject: [PATCH 29/61] Add CustomerController to CAPI. #281 --- config/api_acl.yaml | 5 + src/Controller/CAPI/CustomerController.php | 114 +++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/Controller/CAPI/CustomerController.php diff --git a/config/api_acl.yaml b/config/api_acl.yaml index 39557cf6..2ff18704 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -50,3 +50,8 @@ access_keys: acls: - id: privacypolicy.find label: Find Privacy Policy + - id: customer + label: Customer + acls: + - id: customer.register + label: Register Customer diff --git a/src/Controller/CAPI/CustomerController.php b/src/Controller/CAPI/CustomerController.php new file mode 100644 index 00000000..9c07b7f9 --- /dev/null +++ b/src/Controller/CAPI/CustomerController.php @@ -0,0 +1,114 @@ +acl_gen = $acl_gen; + } + + public function register(Request $req, EntityManagerInterface $em) + { + // required parameters + $params = [ + 'first_name', + 'last_name', + 'mobile_number', + 'make_id', + 'model_year', + 'plate_number', + 'color', + 'condition', + 'fuel_type', + ]; + + $msg = $this->checkRequiredParameters($req, $params); + error_log('msg - ' . $msg); + if ($msg) + return new APIResponse(false, $msg); + + $first_name = $req->request->get('first_name'); + $last_name = $req->request->get('last_name'); + $mobile_number = $req->request->get('mobile_number'); + + $make_id = $req->request->get('make_id'); + $model_year = $req->request->get('model_year'); + $plate_number = $this->cleanPlateNumber($req->request->get('plate_number')); + $color = $req->request->get('color'); + $condition = $req->request->get('condition'); + $fuel_type = $req->request->get('fuel_type'); + + // check if vehicle exists + $vehicle = $em->getRepository(Vehicle::class)->find($make_id); + if ($vehicle == null) + return new APIResponse(false, 'Invalid vehicle make.'); + + // check if customer already exists + $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + if (!empty($customers)) + { + foreach($customers as $customer) + { + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + $cv_found = false; + if (!empty($c_vehicles)) + { + // check if plate number of customer vehicle matches plate number + foreach($c_vehicles as $c_vehicle) + { + $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); + + // check if it's already there + if ($clean_cv_plate == $plate_number) + { + // customer and customer vehicle already exists + $cv_found = true; + break; + } + } + } + + // if there is a customer vehicle matched + if ($cv_found) + { + // vehicle found, do nothing + // TODO: do we need to report that vehicle was found + } + else + { + // customer already exists but not customer vehicle + // add customer vehicle + + } + } + } + } + + protected function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + +} From 16cd59dbfd3522e07d45c68f45cf6bd1d1c1aab9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 27 Nov 2019 03:23:39 +0000 Subject: [PATCH 30/61] Add route to register customer. Finish register function. #281 --- .../api-bundle/Command/TestAPICommand.php | 15 ++- config/routes/capi.yaml | 8 ++ src/Controller/CAPI/CustomerController.php | 96 ++++++++++++++++++- 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/catalyst/api-bundle/Command/TestAPICommand.php b/catalyst/api-bundle/Command/TestAPICommand.php index bf35ce1e..9699ce0c 100644 --- a/catalyst/api-bundle/Command/TestAPICommand.php +++ b/catalyst/api-bundle/Command/TestAPICommand.php @@ -41,7 +41,6 @@ class TestAPICommand extends Command // TODO: shift this out of the bundle, since it's project specific - // warranty register $serial = 'AJ34LJADR12134LKJL5'; $plate_num = 'XEN918'; @@ -119,5 +118,19 @@ class TestAPICommand extends Command // privacy policy $privacy_policy_id = 2; $api->get('/capi/privacy_policy/' . $privacy_policy_id ); + + // register new customer + $params = [ + 'first_name' => 'Krispups', + 'last_name' =>'Porzindog', + 'mobile_number' => '9221111111', + 'make_id' => '22241', + 'model_year' => '2018', + 'plate_number' => 'KPP1234', + 'color' => 'White', + 'condition' => 'new', + 'fuel_type' => 'gas', + ]; + $api->post('/capi/customer', $params); } } diff --git a/config/routes/capi.yaml b/config/routes/capi.yaml index cf533814..04fe01d0 100644 --- a/config/routes/capi.yaml +++ b/config/routes/capi.yaml @@ -135,3 +135,11 @@ capi_privacy_policy: path: /capi/privacy_policy/{id} controller: App\Controller\CAPI\PrivacyPolicyController::getPrivacyPolicy methods: [GET] + +# customer + +# register customer and customer vehicle +capi_customer_register: + path: /capi/customer + controller: App\Controller\CAPI\CustomerController::register + methods: [POST] diff --git a/src/Controller/CAPI/CustomerController.php b/src/Controller/CAPI/CustomerController.php index 9c07b7f9..495486ba 100644 --- a/src/Controller/CAPI/CustomerController.php +++ b/src/Controller/CAPI/CustomerController.php @@ -28,6 +28,8 @@ class CustomerController extends APIController public function register(Request $req, EntityManagerInterface $em) { + $this->denyAccessUnlessGranted('customer.register', null, 'No access.'); + // required parameters $params = [ 'first_name', @@ -62,8 +64,33 @@ class CustomerController extends APIController if ($vehicle == null) return new APIResponse(false, 'Invalid vehicle make.'); + // clean up mobile number + // does it fit our 09XXXXXXXXX pattern? + if (preg_match('/^09[0-9]{9}$/', $mobile_number)) + { + // remove first '0' + $mobile_number = substr($mobile_number, 1); + error_log("CONVERTED TO $mobile_number"); + } + + // does it fit our 9XXXXXXXXX pattern? + if (!preg_match('/^9[0-9]{9}$/', $mobile_number)) + return new APIResponse(false, 'Invalid mobile number.'); + + /* + // min length 2 + // TODO: we need to check proper phone number format + // format should be '9XXXXXXXXX' + // TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert + if (strlen($mobile_number <= 2)) + continue; + */ + + + $data = []; + $message = ''; // check if customer already exists - $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $mobile_number]); if (!empty($customers)) { foreach($customers as $customer) @@ -93,16 +120,79 @@ class CustomerController extends APIController if ($cv_found) { // vehicle found, do nothing - // TODO: do we need to report that vehicle was found + $message = 'Customer found.'; } else { // customer already exists but not customer vehicle // add customer vehicle - + $new_cv = new CustomerVehicle(); + + $new_cv->setCustomer($customer) + ->setPlateNumber($plate_number) + ->setStatusCondition($condition) + ->setModelYear($model_year) + ->setColor($color) + ->setFuelType($fuel_type) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $em->persist($new_cv); + + $message = 'Vehicle added.'; + $data[] = [ + 'make_id' => $make_id, + 'model_year' => $model_year, + 'plate_number' => $plate_number, + 'color' => $color, + 'condition' => $condition, + 'fuel_type' => $fuel_type, + ]; } } } + else + { + // customer not found + $new_cust = new Customer(); + $new_cust->setFirstName($first_name) + ->setLastName($last_name) + ->setPhoneMobile($mobile_number); + + $em->persist($new_cust); + + // add customer vehicle + $new_cv = new CustomerVehicle(); + + $new_cv->setCustomer($new_cust) + ->setPlateNumber($plate_number) + ->setStatusCondition($condition) + ->setModelYear($model_year) + ->setColor($color) + ->setFuelType($fuel_type) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $em->persist($new_cv); + + $message = 'Customer and vehicle added.'; + $data[] = [ + 'first_name' => $first_name, + 'last_name' => $last_name, + 'mobile_number' => $mobile_number, + 'make_id' => $make_id, + 'model_year' => $model_year, + 'plate_number' => $plate_number, + 'color' => $color, + 'condition' => $condition, + 'fuel_type' => $fuel_type, + ]; + } + + $em->flush(); + $em->clear(); + + return new APIResponse(true, $message, $data); } protected function cleanPlateNumber($plate) From 2a45de2b31c350804f31b90152c52db3e1dd5c01 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 27 Nov 2019 03:27:53 +0000 Subject: [PATCH 31/61] Rename the path for customer registration. #281 --- config/routes/capi.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes/capi.yaml b/config/routes/capi.yaml index 04fe01d0..ee3c8668 100644 --- a/config/routes/capi.yaml +++ b/config/routes/capi.yaml @@ -140,6 +140,6 @@ capi_privacy_policy: # register customer and customer vehicle capi_customer_register: - path: /capi/customer + path: /capi/quick_registration controller: App\Controller\CAPI\CustomerController::register methods: [POST] From 8921f2442b9074cdcd32e7b224fb303f1a5e2ec2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 27 Nov 2019 03:37:42 +0000 Subject: [PATCH 32/61] Add prefix for vehicle data. #281 --- .../api-bundle/Command/TestAPICommand.php | 14 +++++------ src/Controller/CAPI/CustomerController.php | 24 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/catalyst/api-bundle/Command/TestAPICommand.php b/catalyst/api-bundle/Command/TestAPICommand.php index 9699ce0c..d32097fe 100644 --- a/catalyst/api-bundle/Command/TestAPICommand.php +++ b/catalyst/api-bundle/Command/TestAPICommand.php @@ -124,13 +124,13 @@ class TestAPICommand extends Command 'first_name' => 'Krispups', 'last_name' =>'Porzindog', 'mobile_number' => '9221111111', - 'make_id' => '22241', - 'model_year' => '2018', - 'plate_number' => 'KPP1234', - 'color' => 'White', - 'condition' => 'new', - 'fuel_type' => 'gas', + 'v_make_id' => '22241', + 'v_model_year' => '2018', + 'v_plate_number' => 'KPP1234', + 'v_color' => 'White', + 'v_condition' => 'new', + 'v_fuel_type' => 'gas', ]; - $api->post('/capi/customer', $params); + $api->post('/capi/quick_registration', $params); } } diff --git a/src/Controller/CAPI/CustomerController.php b/src/Controller/CAPI/CustomerController.php index 495486ba..55aae2bd 100644 --- a/src/Controller/CAPI/CustomerController.php +++ b/src/Controller/CAPI/CustomerController.php @@ -35,12 +35,12 @@ class CustomerController extends APIController 'first_name', 'last_name', 'mobile_number', - 'make_id', - 'model_year', - 'plate_number', - 'color', - 'condition', - 'fuel_type', + 'v_make_id', + 'v_model_year', + 'v_plate_number', + 'v_color', + 'v_condition', + 'v_fuel_type', ]; $msg = $this->checkRequiredParameters($req, $params); @@ -52,12 +52,12 @@ class CustomerController extends APIController $last_name = $req->request->get('last_name'); $mobile_number = $req->request->get('mobile_number'); - $make_id = $req->request->get('make_id'); - $model_year = $req->request->get('model_year'); - $plate_number = $this->cleanPlateNumber($req->request->get('plate_number')); - $color = $req->request->get('color'); - $condition = $req->request->get('condition'); - $fuel_type = $req->request->get('fuel_type'); + $make_id = $req->request->get('v_make_id'); + $model_year = $req->request->get('v_model_year'); + $plate_number = $this->cleanPlateNumber($req->request->get('v_plate_number')); + $color = $req->request->get('v_color'); + $condition = $req->request->get('v_condition'); + $fuel_type = $req->request->get('v_fuel_type'); // check if vehicle exists $vehicle = $em->getRepository(Vehicle::class)->find($make_id); From 743bbf9c6174040486b753917d0a100d5aed6afd Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 27 Nov 2019 08:04:42 +0000 Subject: [PATCH 33/61] Add warranty class report to yaml files, templates and ReportController. #283 --- config/acl.yaml | 2 + config/routes/report.yaml | 10 ++ src/Controller/ReportController.php | 115 ++++++++++++++++++ templates/base.html.twig | 8 ++ .../report/warranty-class/form.html.twig | 50 ++++++++ 5 files changed, 185 insertions(+) create mode 100644 templates/report/warranty-class/form.html.twig 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..0e8a06e7 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,70 @@ 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', + '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 +789,55 @@ 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]); + // TODO: test this query to see if this is faster + $warr_query = $em->createQuery('select w from App\Entity\Warranty w where w.plate_number = :plate_num') + ->setParameter('plate_num', $clean_cv_plate); + $warranties = $warr_query->iterate(); + + foreach ($warranties as $wrow) + { + $warr = $wrow[0]; + error_log('Found warranty for plate number ' . $warr->getPlateNumber()); + } + } + + } + + } + + return $results; + } + + protected function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); } } 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 %} + From 28b93a83d0f967288d8a9b65f18a144a65feb8e2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 28 Nov 2019 05:20:29 +0000 Subject: [PATCH 34/61] Add index for plate number in warranty. Finish warranty class report. #283 --- src/Controller/ReportController.php | 95 ++++++++++++++++++++++++++--- src/Entity/Warranty.php | 3 +- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 0e8a06e7..d1bd1bcd 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -593,6 +593,7 @@ class ReportController extends Controller 'Warranty Expiry Date', 'Warranty Claim Date', 'Warranty Claimed From', + 'Warranty Privacy Policy', 'Is Warranty Activated?', ]); foreach ($data as $row) @@ -803,7 +804,7 @@ class ReportController extends Controller foreach($customers as $crow) { $cust = $crow[0]; - error_log('Processing customer ' . $cust->getID()); + //error_log('Processing customer ' . $cust->getID()); // get list of customer vehicles $c_vehicles = $cust->getVehicles(); @@ -815,20 +816,18 @@ class ReportController extends Controller // find warranty for plate number $clean_cv_plate = $this->cleanPlateNumber($cv->getPlateNumber()); - // $warranties = $em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_cv_plate]); - // TODO: test this query to see if this is faster - $warr_query = $em->createQuery('select w from App\Entity\Warranty w where w.plate_number = :plate_num') - ->setParameter('plate_num', $clean_cv_plate); - $warranties = $warr_query->iterate(); + $warranties = $em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_cv_plate]); - foreach ($warranties as $wrow) + foreach ($warranties as $warr) { - $warr = $wrow[0]; - error_log('Found warranty for plate number ' . $warr->getPlateNumber()); + //error_log('Found warranty for plate number ' . $warr->getPlateNumber()); + + // form the result row + $results[] = $this->formWarrantyClassResult($cust, $cv, $warr); } } - } + $em->clear(); } @@ -840,4 +839,80 @@ class ReportController extends Controller // 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 From 6daba0d3052d1f7848f7c63f39d88f1ef51b87fc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 29 Nov 2019 03:41:34 +0000 Subject: [PATCH 35/61] Add command to compute expiry date for warranty. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 108 ++++++++++++++++++ .../CreateCustomerFromWarrantyCommand.php | 1 - 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/Command/ComputeWarrantyExpiryDateCommand.php diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php new file mode 100644 index 00000000..4d547d3d --- /dev/null +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -0,0 +1,108 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:computeexpirydate') + ->setDescription('Compute expiry date for existing warranties.') + ->setHelp('Comput expiry date for existing warranties.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.date_expire is null'); + $warranties = $warr_q->iterate(); + + foreach($warranties as $row) + { + $warr = $row[0]; + + error_log('Processing warranty for ' . $warr->getID()); + + $date_purchase = $warr->getDatePurchase(); + $warr_period = $this->getWarrantyPeriod($warr); + if ($warr_period != null) + { + $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); + + // save expiry date + } + } + + } + + protected function getWarrantyPeriod($warr) + { + $batt_model = $warr->getBatteryModel(); + + $warranty_class = $warr->getWarrantyClass(); + $warr_period = ''; + + if (($batt_model == null) || + (empty($warranty_class))) + + { + return null; + } + + if ($batt_model != null) + { + $batteries = $batt_model->getBatteries(); + foreach($batteries as $battery) + { + // check warranty class to get warranty period + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $battery->getWarrantyPrivate(); + error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + { + $warr_period = $battery->getWarrantyCommercial(); + error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $battery->getWarrantyTnv(); + error_log('Warranty Period for TNV: ' . $warr_period); + } + } + } + + return $warr_period; + } + + protected function computeDateExpire($date_create, $warranty_period) + { + $expire_date = clone $date_create; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } + +} diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 91ae38d1..e644fc06 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Dotenv\Dotenv; use Doctrine\Common\Persistence\ObjectManager; From ced729241387b980c113c410718fe3312794f284 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 10 Dec 2019 08:19:21 +0000 Subject: [PATCH 36/61] Add customer and customer vehicle when warranty is registered via API. #285 --- .../api-bundle/Command/TestAPICommand.php | 10 +- config/services.yaml | 1 + src/Controller/CAPI/WarrantyController.php | 158 +++++++++++++++++- 3 files changed, 166 insertions(+), 3 deletions(-) diff --git a/catalyst/api-bundle/Command/TestAPICommand.php b/catalyst/api-bundle/Command/TestAPICommand.php index d32097fe..46a4ef0a 100644 --- a/catalyst/api-bundle/Command/TestAPICommand.php +++ b/catalyst/api-bundle/Command/TestAPICommand.php @@ -53,12 +53,18 @@ class TestAPICommand extends Command 'date_expire' => '20191001', 'first_name' => 'First', 'last_name' => 'Last', - 'mobile_number' => '12345678910', + 'mobile_number' => '09231234567', ]; $api->post('/capi/warranties', $params); // get all warranties - $api->get('/capi/warranties'); + $params = [ + 'order' => 'DESC', + 'limit' => '5', + 'start' => '1', + ]; + + $api->get('/capi/warranties', $params); // warranty find diff --git a/config/services.yaml b/config/services.yaml index 42044b7d..47daf34d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -10,6 +10,7 @@ parameters: api_access_key: 'api_access_keys' app_acl_file: 'acl.yaml' app_access_key: 'access_keys' + cvu_brand_id: "%env(CVU_BRAND_ID)%" services: # default configuration for services in *this* file diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index d76afffc..83ecf74a 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -17,10 +17,16 @@ use App\Entity\SAPBattery; use App\Entity\SAPBatterySize; use App\Entity\SAPBatteryBrand; use App\Entity\PrivacyPolicy; +use App\Entity\Customer; +use App\Entity\CustomerVehicle; +use App\Entity\Vehicle; use App\Ramcar\NameValue; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; + use DateTime; use Catalyst\APIBundle\Access\Generator as ACLGenerator; @@ -226,6 +232,9 @@ class WarrantyController extends APIController try { $em->persist($warr); + + $this->getCustomerFromMobile($em, $warr); + $em->flush(); } catch (UniqueConstraintViolationException $e) @@ -488,9 +497,156 @@ class WarrantyController extends APIController } $em->persist($warr); - $em->flush(); return new APIResponse(true, 'Privacy policy for warranty set successfully.'); } + + protected function getCustomerFromMobile($em, $warranty) + { + $w_mobile = $warranty->getMobileNumber(); + if (empty($w_mobile)) + { + return null; + } + + // set values for new customer vehicle + $w_plate_number = $this->cleanPlateNumber($warranty->getPlateNumber()); + + $cust_found = false; + + $w_mobile_num = trim($w_mobile); + + // does it fit our 09XXXXXXXXX pattern? + if (preg_match('/^09[0-9]{9}$/', $w_mobile_num)) + { + // remove first '0' + $w_mobile_num = substr($w_mobile_num, 1); + error_log("CONVERTED TO $w_mobile_num"); + } + + // does it fit our 9XXXXXXXXX pattern? + if (!preg_match('/^9[0-9]{9}$/', $w_mobile_num)) + return null; + + /* + // min length 2 + // TODO: we need to check proper phone number format + // format should be '9XXXXXXXXX' + // TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert + if (strlen($w_mobile_num <= 2)) + continue; + */ + + $customers = $this->findCustomerByNumber($em, $w_mobile_num); + + if (!empty($customers)) + { + error_log('found customer for ' . $w_mobile_num); + foreach ($customers as $customer) + { + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + $cv_found = false; + if (!empty($c_vehicles)) + { + // check if plate number of customer vehicle matches warranty plate number + foreach ($c_vehicles as $c_vehicle) + { + $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); + + // check if it's already there + if ($clean_cv_plate == $w_plate_number) + { + // customer and customer vehicle already exists + $cv_found = true; + break; + } + } + + } + + // if there was a customer vehicle matched + if ($cv_found) + { + // vehicle found, do nothing. + error_log('vehicle found - ' . $w_plate_number); + } + else + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + error_log('new vehicle - ' . $w_plate_number); + $this->createCustomerVehicle($em, $customer, $this->getDefaultVehicle($em), $w_plate_number); + } + } + } + // customer not found + else + { + error_log('NEW customer and vehicle - ' . $w_plate_number); + // customer not found, add customer and customer vehicle + // get warranty first name, last name + $w_first_name = $warranty->getFirstName(); + $w_last_name = $warranty->getLastName(); + + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); + + $em->persist($new_cust); + + $this->createCustomerVehicle($em, $new_cust, $this->getDefaultVehicle($em), $w_plate_number); + } + + $em->flush(); + $em->clear(); + } + + protected function getDefaultVehicle($em) + { + // get default vehicle + $cvu_brand_id = $this->getParameter('cvu_brand_id'); + $default_vehicle = $em->getRepository(Vehicle::class)->find($cvu_brand_id); + if ($default_vehicle == null) + { + $output->writeln("Need to add vehicle with default values."); + return null; + } + + return $default_vehicle; + } + + + protected function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + + protected function createCustomerVehicle($em, Customer $cust, $vehicle, $plate_number) + { + $new_cv = new CustomerVehicle(); + + $new_cv->setCustomer($cust) + ->setPlateNumber($plate_number) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') + ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $em->persist($new_cv); + } + + + protected function findCustomerByNumber($em, $number) + { + $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + return $customers; + } + } From 243d238ad7c495d33435c7dea26313597cf28ca8 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Dec 2019 08:43:15 +0000 Subject: [PATCH 37/61] Finish command to compute expiration date for existing warranties. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 77 ++++++++++++------- src/Entity/BatteryModel.php | 1 + src/Entity/BatterySize.php | 1 + 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index 4d547d3d..c2af16b2 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; use App\Entity\Warranty; +use App\Entity\Battery; use App\Ramcar\WarrantyClass; @@ -31,7 +32,7 @@ class ComputeWarrantyExpiryDateCommand extends Command { $this->setName('warranty:computeexpirydate') ->setDescription('Compute expiry date for existing warranties.') - ->setHelp('Comput expiry date for existing warranties.'); + ->setHelp('Compute expiry date for existing warranties.'); } protected function execute(InputInterface $input, OutputInterface $output) @@ -50,9 +51,18 @@ class ComputeWarrantyExpiryDateCommand extends Command if ($warr_period != null) { $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); - - // save expiry date } + else + { + $expiry_date = $date_purchase; + } + + // save expiry date + $warr->setDateExpire($expiry_date); + + $this->em->persist($warr); + $this->em->flush(); + $this->em->clear(); } } @@ -60,38 +70,53 @@ class ComputeWarrantyExpiryDateCommand extends Command protected function getWarrantyPeriod($warr) { $batt_model = $warr->getBatteryModel(); + $batt_size = $warr->getBatterySize(); $warranty_class = $warr->getWarrantyClass(); - $warr_period = ''; + $warr_period = 0; - if (($batt_model == null) || - (empty($warranty_class))) - + if ($batt_model == null) { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + if (empty($warranty_class)) + { + error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + // find batttery using model and size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + + if (empty($batteries)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); return null; } - if ($batt_model != null) + foreach($batteries as $battery) { - $batteries = $batt_model->getBatteries(); - foreach($batteries as $battery) + // check warranty class to get warranty period + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $battery->getWarrantyPrivate(); + error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) { - // check warranty class to get warranty period - if ($warranty_class == WarrantyClass::WTY_PRIVATE) - { - $warr_period = $battery->getWarrantyPrivate(); - error_log('Warranty Period for Private: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) - { - $warr_period = $battery->getWarrantyCommercial(); - error_log('Warranty Period for Commercial: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_TNV) - { - $warr_period = $battery->getWarrantyTnv(); - error_log('Warranty Period for TNV: ' . $warr_period); - } + $warr_period = $battery->getWarrantyCommercial(); + error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $battery->getWarrantyTnv(); + error_log('Warranty Period for TNV: ' . $warr_period); } } diff --git a/src/Entity/BatteryModel.php b/src/Entity/BatteryModel.php index 86f3b666..b516e8a6 100644 --- a/src/Entity/BatteryModel.php +++ b/src/Entity/BatteryModel.php @@ -68,6 +68,7 @@ class BatteryModel public function getBatteries() { + // TODO: fix this to be a proper getter function // has to return set of strings because symfony is trying to move away from role objects $str_batteries = []; foreach ($this->batteries as $battery) diff --git a/src/Entity/BatterySize.php b/src/Entity/BatterySize.php index 021e115c..9fff4d44 100644 --- a/src/Entity/BatterySize.php +++ b/src/Entity/BatterySize.php @@ -89,6 +89,7 @@ class BatterySize public function getBatteries() { + // TODO: fix this to be a proper getter function // has to return set of strings because symfony is trying to move away from role objects $str_batteries = []; foreach ($this->batteries as $battery) From c1203368c04a22acf533bfaefe2cb3a7ebb83a7e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 Dec 2019 08:33:29 +0000 Subject: [PATCH 38/61] Use SKU to find the battery first. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index c2af16b2..7f206cc5 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -48,6 +48,7 @@ class ComputeWarrantyExpiryDateCommand extends Command $date_purchase = $warr->getDatePurchase(); $warr_period = $this->getWarrantyPeriod($warr); + if ($warr_period != null) { $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); @@ -69,30 +70,44 @@ class ComputeWarrantyExpiryDateCommand extends Command protected function getWarrantyPeriod($warr) { + // find battery via sku/sap battery first + // if sku is null, use battery model and battery size to find battery + // if all three are null, do nothing + + $batteries = null; + + $sap_battery = $warr->getSAPBattery(); $batt_model = $warr->getBatteryModel(); $batt_size = $warr->getBatterySize(); - $warranty_class = $warr->getWarrantyClass(); - $warr_period = 0; - if ($batt_model == null) - { - error_log('Battery model is null for warranty id ' . $warr->getID()); - return null; - } - if ($batt_size == null) - { - error_log('Battery size is null for warranty id ' . $warr->getID()); - return null; - } if (empty($warranty_class)) { error_log('Warranty class is empty for warranty id ' . $warr->getID()); return null; } - - // find batttery using model and size - $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + + if ($sap_battery != null) + { + // get the battery linked to SAP Battery using sap_battery id + $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); + } + else + { + if ($batt_model == null) + { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + + // find battery using battery model and battery size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + } if (empty($batteries)) { @@ -100,8 +115,13 @@ class ComputeWarrantyExpiryDateCommand extends Command return null; } - foreach($batteries as $battery) + // set to -1 to show that we haven't set a warranty period yet + // cannot set initial value to 0 because warranty tnv can be 0 + $least_warranty = -1; + $warr_period = 0; + foreach ($batteries as $battery) { + // if multiple batteries, get the smallest warranty period // check warranty class to get warranty period if ($warranty_class == WarrantyClass::WTY_PRIVATE) { @@ -118,9 +138,22 @@ class ComputeWarrantyExpiryDateCommand extends Command $warr_period = $battery->getWarrantyTnv(); error_log('Warranty Period for TNV: ' . $warr_period); } + + if ($least_warranty < 0) + { + // set least warranty to the first obtained warranty period + $least_warranty = $warr_period; + } + + if ($least_warranty > $warr_period) + { + $least_warranty = $warr_period; + } } - return $warr_period; + $warranty_period = $least_warranty; + + return $warranty_period; } protected function computeDateExpire($date_create, $warranty_period) @@ -129,5 +162,4 @@ class ComputeWarrantyExpiryDateCommand extends Command $expire_date->add(new DateInterval('P'.$warranty_period.'M')); return $expire_date; } - } From e62c3dc0971e4228efa20ca29b274dbf9a154b73 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 Dec 2019 09:38:26 +0000 Subject: [PATCH 39/61] Add checking for serial and plate number. #288 --- src/Controller/WarrantyController.php | 164 ++++++++++++++++++++------ 1 file changed, 128 insertions(+), 36 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index ede93334..2adf0abb 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -469,11 +469,13 @@ class WarrantyController extends Controller $first_name = trim($fields[0]); $last_name = trim($fields[1]); $mobile_number = trim($fields[4]); - $plate_number = trim($fields[9]); + $plate = trim($fields[9]); $serial = trim($fields[10]); $purchase_date = trim($fields[12]); $battery_id = trim($fields[16]); + $plate_number = $this->cleanPlateNumber($plate); + // check if purchase_date or plate_number or serial is empty or if // purchase date is valid $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); @@ -521,53 +523,137 @@ class WarrantyController extends Controller } else { - - // new warranty - $warranty = new Warranty(); - - // get the battery purchased - // check battery first. If not found, check sap_battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if ($battery != null) + // additional validation + // check if serial number and plate number already exists + $warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]); + if (!empty($warr_results)) { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) + foreach($warr_results as $warr) { - $warranty->setBatteryModel($bty_model); - } + // check if details are complete + if (empty($warr->getFirstName())) + { + if (!empty($first_name)) + { + $warr->setFirstName($first_name); + } + } + if (empty($warr->getLastName())) + { + if (!empty($last_name)) + { + $warr->setLastName($last_name); + } + } + if (empty($warr->getMobileNumber())) + { + if (!empty($mobile_number)) + { + $warr->setMobileNumber($mobile_number); + } + } + if ((empty($warr->getBatteryModel())) || + (empty($warr->getBatterySize()))) + { + if (!empty($battery_id)) + { + // find battery + $battery = $em->getRepository(Battery::class)->find($battery_id); + if (!empty($battery)) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); - if ($bty_size != null) - { - $warranty->setBatterySize($bty_size); + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warr->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warr->setBatterySize($bty_size); + } + $sap_code = $battery->getSAPCode(); + if (!empty($sap_code)) + { + // find sap battery + $sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code); + if (!empty($sap_batt)) + { + $warr->setSAPBattery($sap_batt); + } + } + } + } + } + if (empty($warr->getDatePurchase())) + { + if (!empty($date_purchase)) + { + $warr->setDatePurchase($date_purchase); + } + } + + // TODO: compute expiry date + $em->persist($warr); + $em->flush(); } } else { - // find battery in sap_battery - $battery = $em->getRepository(SAPBattery::class)->find($battery_id); + // new warranty + $warranty = new Warranty(); + + // get the battery purchased + // check battery first. If not found, check sap_battery + $battery = $em->getRepository(Battery::class)->find($battery_id); if ($battery != null) { - // battery is SAPBattery - $warranty->setSAPBattery($battery); + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } } + else + { + // find battery in sap_battery + $battery = $em->getRepository(SAPBattery::class)->find($battery_id); + if ($battery != null) + { + // battery is SAPBattery + $warranty->setSAPBattery($battery); + } + } + + // TODO: compute expiry date + + // set and save values + $warranty->setSerial($serial) + ->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $em->persist($warranty); + $em->flush(); } - // set and save values - $warranty->setSerial($serial) - ->setPlateNumber($plate_number) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase); - - $em->persist($warranty); - $em->flush(); } $row_num++; @@ -595,4 +681,10 @@ class WarrantyController extends Controller ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); } } + + protected function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } } From 9c9018171ee69e1bfb5f8c9ee241d68743d64e2c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Dec 2019 04:05:04 +0000 Subject: [PATCH 40/61] Add quick registration test for CAPI. #288 --- .../api-bundle/Command/TestAPICommand.php | 34 +++++++++++-------- src/Controller/WarrantyController.php | 3 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/catalyst/api-bundle/Command/TestAPICommand.php b/catalyst/api-bundle/Command/TestAPICommand.php index d32097fe..71497c70 100644 --- a/catalyst/api-bundle/Command/TestAPICommand.php +++ b/catalyst/api-bundle/Command/TestAPICommand.php @@ -55,14 +55,20 @@ class TestAPICommand extends Command 'last_name' => 'Last', 'mobile_number' => '12345678910', ]; - $api->post('/capi/warranties', $params); + //$api->post('/capi/warranties', $params); // get all warranties - $api->get('/capi/warranties'); + $params = [ + 'order' => 'DESC', + 'limit' => '5', + 'start' => '1', + ]; + + $api->get('/capi/warranties', $params); // warranty find - $api->get('/capi/warranties/' . $serial); + //$api->get('/capi/warranties/' . $serial); // warranty update $id = 86811; @@ -77,7 +83,7 @@ class TestAPICommand extends Command 'last_name' => 'Last', 'mobile_number' => '123456789111', ]; - $api->post('/capi/warranties/'. $id, $params); + //$api->post('/capi/warranties/'. $id, $params); // warranty set privacy policy $id = 86811; @@ -85,7 +91,7 @@ class TestAPICommand extends Command $params = [ 'privacy_policy_id' => $policy_id, ]; - $api->post('/capi/warranties/' . $id .'/privacypolicy', $params); + //$api->post('/capi/warranties/' . $id .'/privacypolicy', $params); // warranty claim $id = 86811; @@ -93,27 +99,27 @@ class TestAPICommand extends Command $params = [ 'serial' => $serial, ]; - $api->post('/capi/warranties/' . $id . '/claim', $params); + //$api->post('/capi/warranties/' . $id . '/claim', $params); // warranty cancel $id = 86811; - $api->get('/capi/warranties/' . $id . '/cancel'); + //$api->get('/capi/warranties/' . $id . '/cancel'); // plate warranty - $api->get('/capi/plates/' . $plate_num . '/warranties'); + //$api->get('/capi/plates/' . $plate_num . '/warranties'); // warranty delete $id = 86811; - $api->post('/capi/warranties/' . $id . '/delete'); + //$api->post('/capi/warranties/' . $id . '/delete'); // battery - $api->get('/capi/battery_brands'); - $api->get('/capi/battery_sizes'); - $api->get('/capi/batteries'); + //$api->get('/capi/battery_brands'); + //$api->get('/capi/battery_sizes'); + //$api->get('/capi/batteries'); // vehicle - $api->get('/capi/vehicle_manufacturers'); - $api->get('/capi/vehicles'); + //$api->get('/capi/vehicle_manufacturers'); + //$api->get('/capi/vehicles'); // privacy policy $privacy_policy_id = 2; diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 2adf0abb..1d23bc9b 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -603,7 +603,8 @@ class WarrantyController extends Controller } } else - { + { + // what if serial exists but plate number is different? // new warranty $warranty = new Warranty(); From bc74339fbf711a71a601798b16dd4945b7d7e191 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Dec 2019 06:18:37 +0000 Subject: [PATCH 41/61] Add checking if serial exists. #288 --- src/Controller/WarrantyController.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 1d23bc9b..4da828c6 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -531,6 +531,7 @@ class WarrantyController extends Controller foreach($warr_results as $warr) { // check if details are complete + //error_log('Updating warranty with serial number ' . $serial . ' and plate number ' . $plate_number); if (empty($warr->getFirstName())) { if (!empty($first_name)) @@ -604,7 +605,16 @@ class WarrantyController extends Controller } else { - // what if serial exists but plate number is different? + // TODO: what if serial exists but plate number is different? + // check if just the serial exists + // if warranty exists, ignore for now + $w_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial]); + if (!empty($w_results)) + { + continue; + } + + //error_log('Adding warranty with serial number ' . $serial . ' and plate number ' . $plate_number); // new warranty $warranty = new Warranty(); From bd2ea5fba31fd544111ccf6485ba30a0deabf869 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Dec 2019 07:29:25 +0000 Subject: [PATCH 42/61] Add the WarrantyHandler service. Move the computeDateExpire to the service. #286 --- config/services.yaml | 4 +++ .../ComputeWarrantyExpiryDateCommand.php | 14 ++++---- src/Service/WarrantyHandler.php | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 src/Service/WarrantyHandler.php diff --git a/config/services.yaml b/config/services.yaml index 42044b7d..14a5e6c8 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,6 +91,10 @@ services: arguments: $geofence_flag: "%env(GEOFENCE_ENABLE)%" + App\Service\WarrantyHandler: + arguments: + $em: "@doctrine.orm.entity_manager" + App\Command\SetCustomerPrivacyPolicyCommand: arguments: $policy_promo: "%env(POLICY_PROMO)%" diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index 7f206cc5..ee49b02d 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -12,6 +12,8 @@ use Doctrine\Common\Persistence\ObjectManager; use App\Entity\Warranty; use App\Entity\Battery; +use App\Service\WarrantyHandler; + use App\Ramcar\WarrantyClass; use DateTime; @@ -20,10 +22,12 @@ use DateInterval; class ComputeWarrantyExpiryDateCommand extends Command { protected $em; + protected $wh; - public function __construct(ObjectManager $em) + public function __construct(ObjectManager $em, WarrantyHandler $wh) { $this->em = $em; + $this->wh = $wh; parent::__construct(); } @@ -51,7 +55,7 @@ class ComputeWarrantyExpiryDateCommand extends Command if ($warr_period != null) { - $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); + $expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period); } else { @@ -156,10 +160,4 @@ class ComputeWarrantyExpiryDateCommand extends Command return $warranty_period; } - protected function computeDateExpire($date_create, $warranty_period) - { - $expire_date = clone $date_create; - $expire_date->add(new DateInterval('P'.$warranty_period.'M')); - return $expire_date; - } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php new file mode 100644 index 00000000..3e9018c5 --- /dev/null +++ b/src/Service/WarrantyHandler.php @@ -0,0 +1,35 @@ +em = $em; + } + + public function createWarranty() + { + } + + public function updateWarranty() + { + } + + public function computeDateExpire($date_create, $warranty_period) + { + $expire_date = clone $date_create; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } +} From 7da009f6734a78691e6d852e68191eba8f7b9579 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Dec 2019 09:49:48 +0000 Subject: [PATCH 43/61] Move getWarrantyPeriod into the WarrantyHandler. #286 --- .../ComputeWarrantyExpiryDateCommand.php | 91 +-------- src/Service/WarrantyHandler.php | 177 +++++++++++++++++- 2 files changed, 177 insertions(+), 91 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index ee49b02d..9c271ff6 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -51,7 +51,7 @@ class ComputeWarrantyExpiryDateCommand extends Command error_log('Processing warranty for ' . $warr->getID()); $date_purchase = $warr->getDatePurchase(); - $warr_period = $this->getWarrantyPeriod($warr); + $warr_period = $this->wh->getWarrantyPeriod($warr); if ($warr_period != null) { @@ -71,93 +71,4 @@ class ComputeWarrantyExpiryDateCommand extends Command } } - - protected function getWarrantyPeriod($warr) - { - // find battery via sku/sap battery first - // if sku is null, use battery model and battery size to find battery - // if all three are null, do nothing - - $batteries = null; - - $sap_battery = $warr->getSAPBattery(); - $batt_model = $warr->getBatteryModel(); - $batt_size = $warr->getBatterySize(); - $warranty_class = $warr->getWarrantyClass(); - - if (empty($warranty_class)) - { - error_log('Warranty class is empty for warranty id ' . $warr->getID()); - return null; - } - - if ($sap_battery != null) - { - // get the battery linked to SAP Battery using sap_battery id - $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); - } - else - { - if ($batt_model == null) - { - error_log('Battery model is null for warranty id ' . $warr->getID()); - return null; - } - if ($batt_size == null) - { - error_log('Battery size is null for warranty id ' . $warr->getID()); - return null; - } - - // find battery using battery model and battery size - $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); - } - - if (empty($batteries)) - { - error_log('Battery not found for warranty id ' . $warr->getID()); - return null; - } - - // set to -1 to show that we haven't set a warranty period yet - // cannot set initial value to 0 because warranty tnv can be 0 - $least_warranty = -1; - $warr_period = 0; - foreach ($batteries as $battery) - { - // if multiple batteries, get the smallest warranty period - // check warranty class to get warranty period - if ($warranty_class == WarrantyClass::WTY_PRIVATE) - { - $warr_period = $battery->getWarrantyPrivate(); - error_log('Warranty Period for Private: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) - { - $warr_period = $battery->getWarrantyCommercial(); - error_log('Warranty Period for Commercial: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_TNV) - { - $warr_period = $battery->getWarrantyTnv(); - error_log('Warranty Period for TNV: ' . $warr_period); - } - - if ($least_warranty < 0) - { - // set least warranty to the first obtained warranty period - $least_warranty = $warr_period; - } - - if ($least_warranty > $warr_period) - { - $least_warranty = $warr_period; - } - } - - $warranty_period = $least_warranty; - - return $warranty_period; - } - } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 3e9018c5..b8e832ae 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -5,6 +5,9 @@ namespace App\Service; use Doctrine\ORM\EntityManagerInterface; use App\Entity\Warranty; +use App\Entity\Battery; +use App\Entity\BatterySize; +use App\Entity\SAPBattery; use DateTime; use DateInterval; @@ -22,8 +25,91 @@ class WarrantyHandler { } - public function updateWarranty() + public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $battery_id, DateTime $date_purchase) { + // TODO: add serial and plate number to update + // TODO: check if data from existing warranty matches the new data + // check if details are complete + if (empty($warr->getFirstName())) + { + if (!empty($first_name)) + { + $warr->setFirstName($first_name); + } + } + if (empty($warr->getLastName())) + { + if (!empty($last_name)) + { + $warr->setLastName($last_name); + } + } + if (empty($warr->getMobileNumber())) + { + if (!empty($mobile_number)) + { + $warr->setMobileNumber($mobile_number); + } + } + if ((empty($warr->getBatteryModel())) || + (empty($warr->getBatterySize()))) + { + if (!empty($battery_id)) + { + // find battery + $battery = $em->getRepository(Battery::class)->find($battery_id); + if (!empty($battery)) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warr->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warr->setBatterySize($bty_size); + } + $sap_code = $battery->getSAPCode(); + if (!empty($sap_code)) + { + // find sap battery + $sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code); + if (!empty($sap_batt)) + { + $warr->setSAPBattery($sap_batt); + } + } + } + } + } + + $purchase_date = $warr->getDatePurchase(); + if (empty($purchase_date)) + { + if (!empty($date_purchase)) + { + $warr->setDatePurchase($date_purchase); + } + $purchase_date = $date_purchase; + } + if (empty($warr->getDateExpire())) + { + $period = getWarrantyPeriod($warr); + $expire_date = $this->computeDateExpire($purchase_date, $period); + + $warr->setDateExpire($expire_date); + } + + $em->persist($warr); + $em->flush(); + + } public function computeDateExpire($date_create, $warranty_period) @@ -32,4 +118,93 @@ class WarrantyHandler $expire_date->add(new DateInterval('P'.$warranty_period.'M')); return $expire_date; } + + public function getWarrantyPeriod($warr) + { + // find battery via sku/sap battery first + // if sku is null, use battery model and battery size to find battery + // if all three are null, do nothing + + $batteries = null; + + $sap_battery = $warr->getSAPBattery(); + $batt_model = $warr->getBatteryModel(); + $batt_size = $warr->getBatterySize(); + $warranty_class = $warr->getWarrantyClass(); + + if (empty($warranty_class)) + { + error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + if ($sap_battery != null) + { + // get the battery linked to SAP Battery using sap_battery id + $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); + } + else + { + if ($batt_model == null) + { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + + // find battery using battery model and battery size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + } + + if (empty($batteries)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); + return null; + } + + // set to -1 to show that we haven't set a warranty period yet + // cannot set initial value to 0 because warranty tnv can be 0 + $least_warranty = -1; + $warr_period = 0; + foreach ($batteries as $battery) + { + // if multiple batteries, get the smallest warranty period + // check warranty class to get warranty period + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $battery->getWarrantyPrivate(); + error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + { + $warr_period = $battery->getWarrantyCommercial(); + error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $battery->getWarrantyTnv(); + error_log('Warranty Period for TNV: ' . $warr_period); + } + + if ($least_warranty < 0) + { + // set least warranty to the first obtained warranty period + $least_warranty = $warr_period; + } + + if ($least_warranty > $warr_period) + { + $least_warranty = $warr_period; + } + } + + $warranty_period = $least_warranty; + + return $warranty_period; + } + } From a02c364c1f8ecec87bd86d86032ff1650653c9ba Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Dec 2019 09:58:40 +0000 Subject: [PATCH 44/61] Fix testing issues with command. #286 --- src/Command/ComputeWarrantyExpiryDateCommand.php | 8 -------- src/Service/WarrantyHandler.php | 2 ++ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index 9c271ff6..e1bfd78e 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -9,16 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; -use App\Entity\Warranty; -use App\Entity\Battery; - use App\Service\WarrantyHandler; -use App\Ramcar\WarrantyClass; - -use DateTime; -use DateInterval; - class ComputeWarrantyExpiryDateCommand extends Command { protected $em; diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index b8e832ae..7cfd8b6a 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -9,6 +9,8 @@ use App\Entity\Battery; use App\Entity\BatterySize; use App\Entity\SAPBattery; +use App\Ramcar\WarrantyClass; + use DateTime; use DateInterval; From 0d3a96e0388358207adb37c6e455e46dfc46a4e4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 17 Dec 2019 01:05:55 +0000 Subject: [PATCH 45/61] Move updating of warranty to handler. #286 --- src/Controller/WarrantyController.php | 84 ++++----------------------- src/Service/WarrantyHandler.php | 6 +- 2 files changed, 13 insertions(+), 77 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 4da828c6..0c715319 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -11,6 +11,8 @@ use App\Entity\BatterySize; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; +use App\Service\WarrantyHandler; + use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; @@ -372,13 +374,14 @@ class WarrantyController extends Controller /** * @Menu(selected="warranty_list") */ - public function uploadSubmit(Request $req, EntityManagerInterface $em) + public function uploadSubmit(Request $req, EntityManagerInterface $em, + WarrantyHandler $wh) { // retrieve temporary info for file $file = $req->files->get('csv_file'); // process the csv file - $inv_entries = $this->processWarrantyFile($file, $em); + $inv_entries = $this->processWarrantyFile($file, $em, $wh); $resp = new StreamedResponse(); $resp->setCallback(function() use($inv_entries) { @@ -422,7 +425,8 @@ class WarrantyController extends Controller return $resp; } - protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em) + protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em, + WarrantyHandler $wh) { // attempt to open file try @@ -530,77 +534,9 @@ class WarrantyController extends Controller { foreach($warr_results as $warr) { - // check if details are complete - //error_log('Updating warranty with serial number ' . $serial . ' and plate number ' . $plate_number); - if (empty($warr->getFirstName())) - { - if (!empty($first_name)) - { - $warr->setFirstName($first_name); - } - } - if (empty($warr->getLastName())) - { - if (!empty($last_name)) - { - $warr->setLastName($last_name); - } - } - if (empty($warr->getMobileNumber())) - { - if (!empty($mobile_number)) - { - $warr->setMobileNumber($mobile_number); - } - } - if ((empty($warr->getBatteryModel())) || - (empty($warr->getBatterySize()))) - { - if (!empty($battery_id)) - { - // find battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if (!empty($battery)) - { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) - { - $warr->setBatteryModel($bty_model); - } - if ($bty_size != null) - { - $warr->setBatterySize($bty_size); - } - $sap_code = $battery->getSAPCode(); - if (!empty($sap_code)) - { - // find sap battery - $sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code); - if (!empty($sap_batt)) - { - $warr->setSAPBattery($sap_batt); - } - } - } - } - } - if (empty($warr->getDatePurchase())) - { - if (!empty($date_purchase)) - { - $warr->setDatePurchase($date_purchase); - } - } - - // TODO: compute expiry date - $em->persist($warr); - $em->flush(); + // call service to check if warranty details is incomplete and then update warranty + // using details from csv file + $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $battery_id, $date_purchase); } } else diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 7cfd8b6a..db90828b 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -179,17 +179,17 @@ class WarrantyHandler if ($warranty_class == WarrantyClass::WTY_PRIVATE) { $warr_period = $battery->getWarrantyPrivate(); - error_log('Warranty Period for Private: ' . $warr_period); + //error_log('Warranty Period for Private: ' . $warr_period); } if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) { $warr_period = $battery->getWarrantyCommercial(); - error_log('Warranty Period for Commercial: ' . $warr_period); + //error_log('Warranty Period for Commercial: ' . $warr_period); } if ($warranty_class == WarrantyClass::WTY_TNV) { $warr_period = $battery->getWarrantyTnv(); - error_log('Warranty Period for TNV: ' . $warr_period); + //error_log('Warranty Period for TNV: ' . $warr_period); } if ($least_warranty < 0) From a10ef2b5d909267fa04b2c0170a196e900ae3324 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 Dec 2019 07:57:39 +0000 Subject: [PATCH 46/61] Fix testing issues found for warranty handler. #286 --- .../ComputeWarrantyExpiryDateCommand.php | 35 +-- src/Controller/WarrantyController.php | 88 +++----- src/Service/WarrantyHandler.php | 213 ++++++++++++------ 3 files changed, 205 insertions(+), 131 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index e1bfd78e..1db6fd42 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -9,6 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; +use App\Entity\Battery; + use App\Service\WarrantyHandler; class ComputeWarrantyExpiryDateCommand extends Command @@ -43,24 +45,31 @@ class ComputeWarrantyExpiryDateCommand extends Command error_log('Processing warranty for ' . $warr->getID()); $date_purchase = $warr->getDatePurchase(); - $warr_period = $this->wh->getWarrantyPeriod($warr); - if ($warr_period != null) + $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + if (!empty($batteries)) { - $expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period); - } - else - { - $expiry_date = $date_purchase; + $warranty_class = $warr->getWarrantyClass(); + + $warr_period = $this->wh->getWarrantyPeriod($batteries, $warranty_class); + + if ($warr_period != null) + { + $expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period); + } + else + { + $expiry_date = $date_purchase; + } + + // save expiry date + $warr->setDateExpire($expiry_date); + + $this->em->persist($warr); + $this->em->flush(); } - // save expiry date - $warr->setDateExpire($expiry_date); - - $this->em->persist($warr); - $this->em->flush(); $this->em->clear(); } - } } diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 0c715319..afdd5158 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -7,6 +7,7 @@ use App\Entity\SAPBattery; use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; +use App\Entity\Invoice; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; @@ -477,8 +478,9 @@ class WarrantyController extends Controller $serial = trim($fields[10]); $purchase_date = trim($fields[12]); $battery_id = trim($fields[16]); + $batt_invoice = trim($fields[11]); - $plate_number = $this->cleanPlateNumber($plate); + $plate_number = $wh->cleanPlateNumber($plate); // check if purchase_date or plate_number or serial is empty or if // purchase date is valid @@ -515,7 +517,7 @@ class WarrantyController extends Controller 'vehicle_year' => trim($fields[8]), 'vehicle_plate_number' => $plate_number, 'battery_serial_number' => $serial, - 'battery_sales_invoice' => trim($fields[11]), + 'battery_sales_invoice' => $batt_invoice, 'battery_date_purchase' => $purchase_date, 'distributor_name' => trim($fields[13]), 'distributor_address' => trim($fields[14]), @@ -530,13 +532,42 @@ class WarrantyController extends Controller // additional validation // check if serial number and plate number already exists $warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]); + + // get battery via the invoice because battery_id doesn't match what's in the live data + // get job order via invoice to get the warranty class + $warranty_class = ''; + $batt_list = array(); + + // find invoice + $invoice = $em->getRepository(Invoice::class)->find($batt_invoice); + if (!empty($invoice)) + { + // get job order + $jo = $invoice->getJobOrder(); + + // get warranty class + $warranty_class = $jo->getWarrantyClass(); + + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + { + $batt_list[] = $item->getBattery(); + } + } + } + if (!empty($warr_results)) { foreach($warr_results as $warr) { // call service to check if warranty details is incomplete and then update warranty // using details from csv file - $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $battery_id, $date_purchase); + //error_log('Updating warranty for ' . $warr->getID()); + $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase); } } else @@ -550,57 +581,10 @@ class WarrantyController extends Controller continue; } - //error_log('Adding warranty with serial number ' . $serial . ' and plate number ' . $plate_number); - // new warranty - $warranty = new Warranty(); + //error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); - // get the battery purchased - // check battery first. If not found, check sap_battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if ($battery != null) - { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) - { - $warranty->setBatteryModel($bty_model); - } - - if ($bty_size != null) - { - $warranty->setBatterySize($bty_size); - } - } - else - { - // find battery in sap_battery - $battery = $em->getRepository(SAPBattery::class)->find($battery_id); - if ($battery != null) - { - // battery is SAPBattery - $warranty->setSAPBattery($battery); - } - } - - // TODO: compute expiry date - - // set and save values - $warranty->setSerial($serial) - ->setPlateNumber($plate_number) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase); - - $em->persist($warranty); - $em->flush(); + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } $row_num++; diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index db90828b..4dd4dcae 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -8,6 +8,7 @@ use App\Entity\Warranty; use App\Entity\Battery; use App\Entity\BatterySize; use App\Entity\SAPBattery; +use App\Entity\BatteryModel; use App\Ramcar\WarrantyClass; @@ -23,11 +24,66 @@ class WarrantyHandler $this->em = $em; } - public function createWarranty() + public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, + $batt_list, DateTime $date_purchase, $warranty_class) { + // new warranty + $warranty = new Warranty(); + + foreach ($batt_list as $battery) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + + $sap_code = $battery->getSAPCode(); + if (!empty($sap_code)) + { + // find sap battery + $sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code); + if ($sap_battery != null) + { + $warranty->setSAPBattery($sap_battery); + } + } + } + + // compute expiry date + if ((!empty($warranty_class)) && + (count($batt_list) != 0)) + { + $period = $this->getWarrantyPeriod($batt_list, $warranty_class); + $date_expire = $this->computeDateExpire($date_purchase, $period); + + $warranty->setDateExpire($date_expire); + } + + // set and save values + $warranty->setSerial($serial) + ->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $this->em->persist($warranty); + $this->em->flush(); + $this->em->clear(); } - public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $battery_id, DateTime $date_purchase) + public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) { // TODO: add serial and plate number to update // TODO: check if data from existing warranty matches the new data @@ -56,35 +112,34 @@ class WarrantyHandler if ((empty($warr->getBatteryModel())) || (empty($warr->getBatterySize()))) { - if (!empty($battery_id)) + if (count($batt_list) != 0) { - // find battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if (!empty($battery)) + foreach ($batt_list as $battery) { // get the battery model and battery size $model_id = $battery->getModel()->getID(); $size_id = $battery->getSize()->getID(); - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); if ($bty_model != null) { - $warr->setBatteryModel($bty_model); + $warranty->setBatteryModel($bty_model); } if ($bty_size != null) { - $warr->setBatterySize($bty_size); + $warranty->setBatterySize($bty_size); } + $sap_code = $battery->getSAPCode(); if (!empty($sap_code)) { // find sap battery - $sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code); - if (!empty($sap_batt)) + $sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code); + if ($sap_battery != null) { - $warr->setSAPBattery($sap_batt); + $warranty->setSAPBattery($sap_battery); } } } @@ -100,18 +155,33 @@ class WarrantyHandler } $purchase_date = $date_purchase; } + if (empty($warr->getDateExpire())) { - $period = getWarrantyPeriod($warr); - $expire_date = $this->computeDateExpire($purchase_date, $period); + $batteries = []; + if (count($batt_list) == 0) + { + $batteries = $this->getBatteriesForWarrantyPeriod($warr); + } + else + { + $batteries = $batt_list; + } - $warr->setDateExpire($expire_date); + if (!empty($batteries)) + { + $period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass()); + if (!empty($purchase_date)) + { + $expire_date = $this->computeDateExpire($purchase_date, $period); + $warr->setDateExpire($expire_date); + } + } } - $em->persist($warr); - $em->flush(); - - + $this->em->persist($warr); + $this->em->flush(); + $this->em->clear(); } public function computeDateExpire($date_create, $warranty_period) @@ -121,53 +191,8 @@ class WarrantyHandler return $expire_date; } - public function getWarrantyPeriod($warr) + public function getWarrantyPeriod($batteries, $warranty_class) { - // find battery via sku/sap battery first - // if sku is null, use battery model and battery size to find battery - // if all three are null, do nothing - - $batteries = null; - - $sap_battery = $warr->getSAPBattery(); - $batt_model = $warr->getBatteryModel(); - $batt_size = $warr->getBatterySize(); - $warranty_class = $warr->getWarrantyClass(); - - if (empty($warranty_class)) - { - error_log('Warranty class is empty for warranty id ' . $warr->getID()); - return null; - } - - if ($sap_battery != null) - { - // get the battery linked to SAP Battery using sap_battery id - $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); - } - else - { - if ($batt_model == null) - { - error_log('Battery model is null for warranty id ' . $warr->getID()); - return null; - } - if ($batt_size == null) - { - error_log('Battery size is null for warranty id ' . $warr->getID()); - return null; - } - - // find battery using battery model and battery size - $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); - } - - if (empty($batteries)) - { - error_log('Battery not found for warranty id ' . $warr->getID()); - return null; - } - // set to -1 to show that we haven't set a warranty period yet // cannot set initial value to 0 because warranty tnv can be 0 $least_warranty = -1; @@ -209,4 +234,60 @@ class WarrantyHandler return $warranty_period; } + public function getBatteriesForWarrantyPeriod($warr) + { + // find battery via sku/sap battery first + // if sku is null, use battery model and battery size to find battery + // if all three are null, do nothing + $batteries = null; + + $sap_battery = $warr->getSAPBattery(); + $batt_model = $warr->getBatteryModel(); + $batt_size = $warr->getBatterySize(); + $warranty_class = $warr->getWarrantyClass(); + + if (empty($warranty_class)) + { + error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + if ($sap_battery != null) + { + // get the battery linked to SAP Battery using sap_battery id + $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); + } + else + { + if ($batt_model == null) + { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + + // find battery using battery model and battery size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + } + + if (empty($batteries)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); + return null; + } + + return $batteries; + } + + + public function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + } From ff56db09b08d544783bbc631bdda63c3006e1e85 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 20 Dec 2019 07:30:08 +0000 Subject: [PATCH 47/61] Add command to update customer vehicle from warranty data. #290 --- .../UpdateCustomerVehicleWarrantyCommand.php | 85 +++++++++++++++++++ src/Entity/CustomerVehicle.php | 2 +- src/Service/WarrantyHandler.php | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/Command/UpdateCustomerVehicleWarrantyCommand.php diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php new file mode 100644 index 00000000..b8a1554d --- /dev/null +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -0,0 +1,85 @@ +em = $em; + $this->wh = $wh; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customervehicle:updatewarrantyinfo') + ->setDescription('Update customer vehicle warranty.') + ->setHelp('Update customer vehicle warranty.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get all warranties + // since it's possible that the same plate number will have multiple warranties, order them from earliest to latest + $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.plate_number is not null order by w.date_purchase asc'); + $warranties = $warr_q->iterate(); + + foreach ($warranties as $row) + { + $warr = $row[0]; + + // clean plate number + $plate_number = $this->wh->cleanPlateNumber($warr->getPlateNumber()); + + // get other warranty information + $serial = $warr->getSerial(); + $expiry_date = $warr->getDateExpire(); + + // find battery + $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + + // find customer vehicle using plate number + error_log('Finding customer vehicle with plate number ' . $plate_number); + $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + + if (!empty($cust_vehicles)) + { + foreach ($cust_vehicles as $cv) + { + if (!empty($batteries)) + { + // set current battery to the last battery in list. + // there are cases where multiple batteries linked to an SAP code. + foreach ($batteries as $batt) + { + $cv->setCurrentBattery($batt); + } + } + $cv->setWarrantyCode($serial) + ->setWarrantyExpiration($expiry_date); + + $this->em->persist($cv); + $this->em->flush(); + } + } + $this->em->clear(); + } + } +} diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index 3e4c70a4..98db2ed7 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -81,7 +81,7 @@ class CustomerVehicle // warranty code // TODO: figure out how to check expiration /** - * @ORM\Column(type="string", length=20, nullable=true) + * @ORM\Column(type="string", length=50, nullable=true) */ protected $warranty_code; diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 4dd4dcae..70a79d94 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -234,6 +234,7 @@ class WarrantyHandler return $warranty_period; } + // TODO: Need to rename this function public function getBatteriesForWarrantyPeriod($warr) { // find battery via sku/sap battery first From 5544d99a98abb8895c9475dd32eeca7ca42c2b7d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 20 Dec 2019 10:17:31 +0000 Subject: [PATCH 48/61] Modify the updating of customer vehicle. #290 --- .../UpdateCustomerVehicleWarrantyCommand.php | 55 ++++++++++++++----- src/Entity/CustomerVehicle.php | 2 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php index b8a1554d..82ce0d58 100644 --- a/src/Command/UpdateCustomerVehicleWarrantyCommand.php +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -52,6 +52,14 @@ class UpdateCustomerVehicleWarrantyCommand extends Command $serial = $warr->getSerial(); $expiry_date = $warr->getDateExpire(); + // TODO: check length of serial for now. Should not exceed 20. + // there is a warranty with 2 serial codes in live + // for now, ignore the warranty + if (strlen($serial) > 20) + { + continue; + } + // find battery $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); @@ -61,23 +69,42 @@ class UpdateCustomerVehicleWarrantyCommand extends Command if (!empty($cust_vehicles)) { - foreach ($cust_vehicles as $cv) - { - if (!empty($batteries)) - { - // set current battery to the last battery in list. + //foreach ($cust_vehicles as $cv) + //{ + // if (!empty($batteries)) + // { + // set current battery to the first battery in list. // there are cases where multiple batteries linked to an SAP code. - foreach ($batteries as $batt) - { - $cv->setCurrentBattery($batt); - } - } - $cv->setWarrantyCode($serial) - ->setWarrantyExpiration($expiry_date); + //foreach ($batteries as $batt) + //{ + // $cv->setCurrentBattery($batt); + //} + // } + //$cv->setWarrantyCode($serial) + // ->setWarrantyExpiration($expiry_date); - $this->em->persist($cv); - $this->em->flush(); + //$this->em->persist($cv); + //$this->em->flush(); + + if (!empty($batteries)) + { + // set current battery to the first battery in list. + // there are cases where multiple batteries linked to an SAP code. + $battery = $batteries[0]; + $battery_id = $battery->getID(); } + $q = $this->em->createQuery('update App\Entity\CustomerVehicle cv + set cv.curr_battery = :batt_id, + cv.warranty_code = :serial, + cv.warranty_expiration = :expiry_date + where cv.plate_number = :plate_number') + ->setParameters([ + 'batt_id' => $battery_id, + 'serial' => $serial, + 'expiry_date' => $expiry_date, + 'plate_number' => $plate_number]); + $q->execute(); + } $this->em->clear(); } diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index 98db2ed7..3e4c70a4 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -81,7 +81,7 @@ class CustomerVehicle // warranty code // TODO: figure out how to check expiration /** - * @ORM\Column(type="string", length=50, nullable=true) + * @ORM\Column(type="string", length=20, nullable=true) */ protected $warranty_code; From 85483ffab808e6781bfc605f24b44c050c119f3b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 20 Dec 2019 13:16:45 +0000 Subject: [PATCH 49/61] Add updating of customer vehicle information when warranty is added. #290 --- src/Controller/WarrantyController.php | 35 +++++++++++++++++++++++++-- src/Service/WarrantyHandler.php | 5 ++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index afdd5158..87cb8db7 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -8,6 +8,7 @@ use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; use App\Entity\Invoice; +use App\Entity\CustomerVehicle; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; @@ -583,10 +584,40 @@ class WarrantyController extends Controller //error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); - $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + $warranty = $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + + // update customer vehicle with warranty info + // get expiry date + // TODO: test this + $expiry_date = $warranty->getDateExpire(); + + // find customer vehicle + $cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + if (!empty($cust_vehicles)) + { + if (!empty($batt_list)) + { + // set current battery to the first battery in list. + // there are cases where multiple batteries linked to an SAP code. + $battery = $batt_list[0]; + $battery_id = $battery->getID(); + } + $q = $em->createQuery('update App\Entity\CustomerVehicle cv + set cv.curr_battery = :batt_id, + cv.warranty_code = :serial, + cv.warranty_expiration = :expiry_date + where cv.plate_number = :plate_number') + ->setParameters([ + 'batt_id' => $battery_id, + 'serial' => $serial, + 'expiry_date' => $expiry_date, + 'plate_number' => $plate_number]); + $q->execute(); + } } } - + + $em->clear(); $row_num++; } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 70a79d94..f56b0f5c 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -81,6 +81,11 @@ class WarrantyHandler $this->em->persist($warranty); $this->em->flush(); $this->em->clear(); + + if ($warranty == null) + error_log('warranty is null'); + + return $warranty; } public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) From 10d9e6e5aecc4da018fdc31c036e21a9f56bf454 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 23 Dec 2019 02:04:48 +0000 Subject: [PATCH 50/61] Update customer vehicle warranty information when warranty is uploaded. #290 --- .../UpdateCustomerVehicleWarrantyCommand.php | 19 +------- src/Controller/WarrantyController.php | 35 ++------------- src/Service/WarrantyHandler.php | 44 ++++++++++++++++--- 3 files changed, 41 insertions(+), 57 deletions(-) diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php index 82ce0d58..4449b54a 100644 --- a/src/Command/UpdateCustomerVehicleWarrantyCommand.php +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -61,7 +61,7 @@ class UpdateCustomerVehicleWarrantyCommand extends Command } // find battery - $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + $batteries = $this->wh->getBatteriesForWarranty($warr); // find customer vehicle using plate number error_log('Finding customer vehicle with plate number ' . $plate_number); @@ -69,23 +69,6 @@ class UpdateCustomerVehicleWarrantyCommand extends Command if (!empty($cust_vehicles)) { - //foreach ($cust_vehicles as $cv) - //{ - // if (!empty($batteries)) - // { - // set current battery to the first battery in list. - // there are cases where multiple batteries linked to an SAP code. - //foreach ($batteries as $batt) - //{ - // $cv->setCurrentBattery($batt); - //} - // } - //$cv->setWarrantyCode($serial) - // ->setWarrantyExpiration($expiry_date); - - //$this->em->persist($cv); - //$this->em->flush(); - if (!empty($batteries)) { // set current battery to the first battery in list. diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 87cb8db7..40afec1a 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -567,7 +567,7 @@ class WarrantyController extends Controller { // call service to check if warranty details is incomplete and then update warranty // using details from csv file - //error_log('Updating warranty for ' . $warr->getID()); + error_log('Updating warranty for ' . $warr->getID()); $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase); } } @@ -582,38 +582,9 @@ class WarrantyController extends Controller continue; } - //error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); + error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); - $warranty = $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); - - // update customer vehicle with warranty info - // get expiry date - // TODO: test this - $expiry_date = $warranty->getDateExpire(); - - // find customer vehicle - $cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); - if (!empty($cust_vehicles)) - { - if (!empty($batt_list)) - { - // set current battery to the first battery in list. - // there are cases where multiple batteries linked to an SAP code. - $battery = $batt_list[0]; - $battery_id = $battery->getID(); - } - $q = $em->createQuery('update App\Entity\CustomerVehicle cv - set cv.curr_battery = :batt_id, - cv.warranty_code = :serial, - cv.warranty_expiration = :expiry_date - where cv.plate_number = :plate_number') - ->setParameters([ - 'batt_id' => $battery_id, - 'serial' => $serial, - 'expiry_date' => $expiry_date, - 'plate_number' => $plate_number]); - $q->execute(); - } + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index f56b0f5c..4d4b4b62 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -9,6 +9,7 @@ use App\Entity\Battery; use App\Entity\BatterySize; use App\Entity\SAPBattery; use App\Entity\BatteryModel; +use App\Entity\CustomerVehicle; use App\Ramcar\WarrantyClass; @@ -61,6 +62,7 @@ class WarrantyHandler } // compute expiry date + $date_expire = null; if ((!empty($warranty_class)) && (count($batt_list) != 0)) { @@ -80,12 +82,41 @@ class WarrantyHandler $this->em->persist($warranty); $this->em->flush(); + + // update customer vehicle with warranty info + $this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire); + $this->em->clear(); + } - if ($warranty == null) - error_log('warranty is null'); - - return $warranty; + public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire) + { + // find customer vehicle using plate number + // error_log('Finding customer vehicle with plate number ' . $plate_number); + $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + $battery_id = null; + if (!empty($cust_vehicles)) + { + if (!empty($batteries)) + { + // set current battery to the first battery in list. + // there are cases where multiple batteries linked to an SAP code. + $battery = $batteries[0]; + $battery_id = $battery->getID(); + } + //error_log('Serial/Warranty Code = ' . $serial); + $q = $this->em->createQuery('update App\Entity\CustomerVehicle cv + set cv.curr_battery = :batt_id, + cv.warranty_code = :serial, + cv.warranty_expiration = :expiry_date + where cv.plate_number = :plate_number') + ->setParameters([ + 'batt_id' => $battery_id, + 'serial' => $serial, + 'expiry_date' => $date_expire, + 'plate_number' => $plate_number]); + $q->execute(); + } } public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) @@ -166,7 +197,7 @@ class WarrantyHandler $batteries = []; if (count($batt_list) == 0) { - $batteries = $this->getBatteriesForWarrantyPeriod($warr); + $batteries = $this->getBatteriesForWarranty($warr); } else { @@ -239,8 +270,7 @@ class WarrantyHandler return $warranty_period; } - // TODO: Need to rename this function - public function getBatteriesForWarrantyPeriod($warr) + public function getBatteriesForWarranty($warr) { // find battery via sku/sap battery first // if sku is null, use battery model and battery size to find battery From eea64645b54cbf69161cd41a2ccdf9b4d97b31c5 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 23 Dec 2019 03:54:01 +0000 Subject: [PATCH 51/61] Move updating of customer vehicle out of the command and into the warranty service. #290 --- src/Command/UpdateCustomerVehicleWarrantyCommand.php | 5 ++++- src/Service/WarrantyHandler.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php index 4449b54a..c771044f 100644 --- a/src/Command/UpdateCustomerVehicleWarrantyCommand.php +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -64,6 +64,7 @@ class UpdateCustomerVehicleWarrantyCommand extends Command $batteries = $this->wh->getBatteriesForWarranty($warr); // find customer vehicle using plate number + /* error_log('Finding customer vehicle with plate number ' . $plate_number); $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); @@ -88,7 +89,9 @@ class UpdateCustomerVehicleWarrantyCommand extends Command 'plate_number' => $plate_number]); $q->execute(); - } + } */ + $this->wh->updateCustomerVehicle($serial, $batteries, $plate_number, $expiry_date); + $this->em->clear(); } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 4d4b4b62..c63f0302 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -92,7 +92,7 @@ class WarrantyHandler public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire) { // find customer vehicle using plate number - // error_log('Finding customer vehicle with plate number ' . $plate_number); + error_log('Finding customer vehicle with plate number ' . $plate_number); $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); $battery_id = null; if (!empty($cust_vehicles)) From 16763c538601b9640200ce3654cf9339f3ce7391 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 7 Jan 2020 08:36:00 +0000 Subject: [PATCH 52/61] Add vehicle battery compatibility report. #291 --- config/acl.yaml | 2 + config/routes/report.yaml | 10 ++++ src/Controller/ReportController.php | 50 ++++++++++++++++++- templates/base.html.twig | 15 ++++-- .../form.html.twig | 50 +++++++++++++++++++ 5 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 templates/report/vehicle-battery-compatibility/form.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 1546a0d9..a9c20c0d 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -298,6 +298,8 @@ access_keys: label: RESQ MEH Customer Report - id: report.warranty.class label: Warranty Class Report + - id: report.vehicle.battery.compatibility + label: Vehicle Battery Compatibility Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index f847cfe5..05c6781d 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -57,3 +57,13 @@ rep_warranty_class_export_csv: path: /report/warranty_class_report controller: App\Controller\ReportController::warrantyClassExportCSV methods: [POST] + +rep_vehicle_battery_compatibility_form: + path: /report/vehicle_battery_compatibility_report + controller: App\Controller\ReportController::vehicleBatteryCompatibilityForm + methods: [GET] + +rep_vehicle_battery_compatibility_export_csv: + path: /report/vehicle_battery_compatibility_report + controller: App\Controller\ReportController::vehicleBatteryCompatibilityExportCSV + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index d1bd1bcd..99c7bcb0 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -27,7 +27,6 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\Dotenv\Dotenv; use Catalyst\MenuBundle\Annotation\Menu; @@ -614,6 +613,48 @@ class ReportController extends Controller } + /** + * @Menu(selected="outlet_list") + */ + public function vehicleBatteryCompatibilityForm() + { + $this->denyAccessUnlessGranted('report.vehicle.battery.compatibility', null, 'No access.'); + $params['mode'] = 'form'; + + return $this->render('report/vehicle-battery-compatibility/form.html.twig', $params); + } + + /** + * @Menu(selected="outlet_list") + */ + public function vehicleBatteryCompatibilityExportCSV(Request $req, EntityManagerInterface $em) + { + $data = $this->getVehicleBatteryCompatibilityData($em); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($data) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + ]); + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'vehicle_battery_compatibility_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 @@ -915,4 +956,11 @@ class ReportController extends Controller return $data; } + + protected function getVehicleBatteryCompatibilityData(EntityManagerInterface $em) + { + $results = []; + + return $results; + } } diff --git a/templates/base.html.twig b/templates/base.html.twig index c6f1b9d5..cf350525 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -149,7 +149,7 @@ diff --git a/templates/report/vehicle-battery-compatibility/form.html.twig b/templates/report/vehicle-battery-compatibility/form.html.twig new file mode 100644 index 00000000..83dea44a --- /dev/null +++ b/templates/report/vehicle-battery-compatibility/form.html.twig @@ -0,0 +1,50 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Vehicle Battery Compatibility Report +

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

+ Generate Vehicle Battery Compatibility CSV File +

+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+{% endblock %} + From f74b8cff88e4b7c174583188834a6c5b4acad4b1 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 7 Jan 2020 10:15:53 +0000 Subject: [PATCH 53/61] Add sql query to get vehicle battery compatibility data. #291 --- src/Controller/ReportController.php | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 99c7bcb0..75bdc2b4 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -636,6 +636,11 @@ class ReportController extends Controller // csv output $csv_handle = fopen('php://output', 'w+'); fputcsv($csv_handle, [ + 'Vehicle Manufacturer', + 'Vehicle Make', + 'Battery Manufacturer', + 'Battery Model', + 'Battery Size', ]); foreach ($data as $row) { @@ -961,6 +966,38 @@ class ReportController extends Controller { $results = []; + $conn = $em->getConnection(); + $sql = 'SELECT vm.name AS vm_name, v.make, + bm.name AS bm_name, bmodel.name AS bmodel_name, + bsize.name AS bsize_name + FROM vehicle_manufacturer vm, vehicle v, battery_vehicle bv, + battery b, battery_manufacturer bm, battery_model bmodel, + battery_size bsize + WHERE vm.id = v.manufacturer_id + AND v.id = bv.vehicle_id + AND bv.battery_id = b.id + AND b.manufacturer_id = bm.id + AND b.model_id = bmodel.id + AND b.size_id = bsize.id + ORDER BY vm.name, v.make'; + + $stmt = $conn->prepare($sql); + $stmt->execute(); + + $query_results = $stmt->fetchAll(); + + foreach($query_results as $row) + { + $results[] = [ + 'vehicle_manufacturer' => $row['vm_name'], + 'vehicle_make' => $row['make'], + 'battery_manufacturer' => $row['bm_name'], + 'battery_model' => $row['bmodel_name'], + 'battery_size' => $row['bsize_name'], + ]; + } + + return $results; } } From 380bb23e25b05252be1177c50b79f3f6122580d9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 8 Jan 2020 01:35:59 +0000 Subject: [PATCH 54/61] Add model year from and model year to to report. #291 --- src/Controller/ReportController.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 75bdc2b4..f64b1cb0 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -638,6 +638,8 @@ class ReportController extends Controller fputcsv($csv_handle, [ 'Vehicle Manufacturer', 'Vehicle Make', + 'Vehicle Model Year From', + 'Vehicle Model Year To', 'Battery Manufacturer', 'Battery Model', 'Battery Size', @@ -967,7 +969,8 @@ class ReportController extends Controller $results = []; $conn = $em->getConnection(); - $sql = 'SELECT vm.name AS vm_name, v.make, + $sql = 'SELECT vm.name AS vm_name, v.make, + v.model_year_from, v.model_year_to, bm.name AS bm_name, bmodel.name AS bmodel_name, bsize.name AS bsize_name FROM vehicle_manufacturer vm, vehicle v, battery_vehicle bv, @@ -991,6 +994,8 @@ class ReportController extends Controller $results[] = [ 'vehicle_manufacturer' => $row['vm_name'], 'vehicle_make' => $row['make'], + 'vehicle_model_year_from' => $row['model_year_from'], + 'vehicle_model_year_to' => $row['model_year_to'], 'battery_manufacturer' => $row['bm_name'], 'battery_model' => $row['bmodel_name'], 'battery_size' => $row['bsize_name'], From b4f1cbfcf031bba1d9f25e5a56cdef9b92414f00 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 9 Jan 2020 08:31:03 +0000 Subject: [PATCH 55/61] Add battery sap code to report. #291 --- src/Controller/ReportController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index f64b1cb0..fc2bcb3a 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -643,6 +643,7 @@ class ReportController extends Controller 'Battery Manufacturer', 'Battery Model', 'Battery Size', + 'Battery SAP Code', ]); foreach ($data as $row) { @@ -972,7 +973,8 @@ class ReportController extends Controller $sql = 'SELECT vm.name AS vm_name, v.make, v.model_year_from, v.model_year_to, bm.name AS bm_name, bmodel.name AS bmodel_name, - bsize.name AS bsize_name + bsize.name AS bsize_name, + b.sap_code FROM vehicle_manufacturer vm, vehicle v, battery_vehicle bv, battery b, battery_manufacturer bm, battery_model bmodel, battery_size bsize @@ -999,6 +1001,7 @@ class ReportController extends Controller 'battery_manufacturer' => $row['bm_name'], 'battery_model' => $row['bmodel_name'], 'battery_size' => $row['bsize_name'], + 'battery_sap_code' => $row['sap_code'], ]; } From d810fc821a25f2ef13241aab8fb500c3ad38966a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 17 Jan 2020 06:35:03 +0000 Subject: [PATCH 56/61] Add Warranty Details Report. #297 --- config/acl.yaml | 2 + config/routes/report.yaml | 10 ++ src/Controller/ReportController.php | 154 ++++++++++++++++++ templates/base.html.twig | 9 +- .../report/warranty-details/form.html.twig | 50 ++++++ 5 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 templates/report/warranty-details/form.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index a9c20c0d..ecccd5f5 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -300,6 +300,8 @@ access_keys: label: Warranty Class Report - id: report.vehicle.battery.compatibility label: Vehicle Battery Compatibility Report + - id: report.warranty.details + label: Warranty Details Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index 05c6781d..85ad83ee 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -67,3 +67,13 @@ rep_vehicle_battery_compatibility_export_csv: path: /report/vehicle_battery_compatibility_report controller: App\Controller\ReportController::vehicleBatteryCompatibilityExportCSV methods: [POST] + +rep_warranty_details_form: + path: /report/warranty_details_report + controller: App\Controller\ReportController::warrantyDetailsForm + methods: [GET] + +rep_warranty_details_export_csv: + path: /report/warranty_details_report + controller: App\Controller\ReportController::warrantyDetailsExportCSV + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index fc2bcb3a..2f72b3fa 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -14,6 +14,8 @@ use App\Entity\Warranty; use App\Entity\CustomerVehicle; use App\Entity\MobileSession; use App\Entity\Customer; +use App\Entity\BatteryModel; +use App\Entity\BatterySize; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; @@ -659,6 +661,66 @@ class ReportController extends Controller $resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); + return $resp; + } + + /** + * @Menu(selected="outlet_list") + */ + public function warrantyDetailsForm() + { + $this->denyAccessUnlessGranted('report.warranty.details', null, 'No access.'); + $params['mode'] = 'form'; + + return $this->render('report/warranty-details/form.html.twig', $params); + } + + /** + * @Menu(selected="outlet_list") + */ + public function warrantyDetailsExportCSV(Request $resq, EntityManagerInterface $em) + { + $data = $this->getWarrantyDetailsData($em); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($data) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Warranty ID', + 'Serial', + 'Battery Model', + 'Battery Size', + 'Warranty Class', + 'Plate Number', + 'Status', + 'Date Created', + 'Date Purchased', + 'Expiry Date', + 'Date Claimed', + 'SAP Battery ID', + 'Claim ID', + 'Last Name', + 'First Name', + 'Mobile Number', + 'Privacy Policy Number', + 'Activated?', + + ]); + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'warranty_details_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; } @@ -1005,7 +1067,99 @@ class ReportController extends Controller ]; } + return $results; + } + + protected function getWarrantyDetailsData(EntityManagerInterface $em) + { + $bm_hash = $this->loadBatteryModels($em); + $bs_hash = $this->loadBatterySizes($em); + + $results = []; + + $conn = $em->getConnection(); + $sql = 'SELECT w.id, w.serial, w.warranty_class, w.plate_number, + w.status, w.date_create, w.date_purchase, w.date_expire, + w.date_claim, w.sap_bty_id, w.claim_id, w.first_name, + w.last_name, w.mobile_number, w.flag_activated, + w.warranty_privacy_policy, w.bty_model_id, w.bty_size_id + FROM warranty w'; + + $stmt = $conn->prepare($sql); + $stmt->execute(); + + $query_results = $stmt->fetchAll(); + + foreach ($query_results as $row) + { + // get battery model and size names + $bmodel_name = ''; + $bm_id = $row['bty_model_id']; + if (!empty($bm_id)) + { + if (isset($bm_hash[$bm_id])) + $bmodel_name = $bm_hash[$bm_id]; + } + + $bsize_name = ''; + $bs_id = $row['bty_size_id']; + if (!empty($bs_id)) + { + if (isset($bs_hash[$bs_id])) + $bsize_name = $bs_hash[$bs_id]; + } + + $results[] = [ + 'id' => $row['id'], + 'serial' => $row['serial'], + 'battery_model' => $bmodel_name, + 'battery_size' => $bsize_name, + 'warranty_class' => $row['warranty_class'], + 'plate_number' => $row['plate_number'], + 'status' => $row['status'], + 'date_create' => $row['date_create'], + 'date_purchase' => $row['date_purchase'], + 'date_expire' => $row['date_expire'], + 'date_claim' => $row['date_claim'], + 'sap_bty_id' => $row['sap_bty_id'], + 'claim_id' => $row['claim_id'], + 'last_name' => $row['last_name'], + 'first_name' => $row['first_name'], + 'mobile_number' => $row['mobile_number'], + 'privacy_policy' => $row['warranty_privacy_policy'], + 'flag_activated' => (boolean) $row['flag_activated'], + ]; + } return $results; } + + protected function loadBatteryModels(EntityManagerInterface $em) + { + $bmodel_hash = []; + + $models = $em->getRepository(BatteryModel::class)->findAll(); + foreach ($models as $model) + { + $bmodel_id = $model->getID(); + $bmodel_hash[$bmodel_id] = $model->getName(); + } + + return $bmodel_hash; + } + + protected function loadBatterySizes(EntityManagerInterface $em) + { + $bsize_hash = []; + + $sizes = $em->getRepository(BatterySize::class)->findAll(); + foreach ($sizes as $size) + { + $bsize_id = $size->getID(); + $bsize_hash[$bsize_id] = $size->getName(); + } + + return $bsize_hash; + } + } diff --git a/templates/base.html.twig b/templates/base.html.twig index cf350525..251d8f55 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -181,7 +181,14 @@ Vehicle Battery Compatibility Report - + + + + + + Warranty Details Report + + diff --git a/templates/report/warranty-details/form.html.twig b/templates/report/warranty-details/form.html.twig new file mode 100644 index 00000000..c329785a --- /dev/null +++ b/templates/report/warranty-details/form.html.twig @@ -0,0 +1,50 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Warranty Details Report +

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

+ Generate Warranty Details CSV File +

+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+{% endblock %} + From 2aed73b2d51309d7c8e0591b69f1f2ae7fda065d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 17 Jan 2020 07:55:35 +0000 Subject: [PATCH 57/61] Add Static Content feature. #298 --- config/acl.yaml | 14 ++ config/menu.yaml | 4 + config/routes/static_content.yaml | 33 +++ src/Controller/StaticContentController.php | 272 +++++++++++++++++++++ src/Entity/StaticContent.php | 54 ++++ templates/static-content/form.html.twig | 142 +++++++++++ templates/static-content/list.html.twig | 138 +++++++++++ 7 files changed, 657 insertions(+) create mode 100644 config/routes/static_content.yaml create mode 100644 src/Controller/StaticContentController.php create mode 100644 src/Entity/StaticContent.php create mode 100644 templates/static-content/form.html.twig create mode 100644 templates/static-content/list.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index a9c20c0d..39a2ff50 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -366,3 +366,17 @@ access_keys: label: Add - id: warranty.update label: Update + + - id: staticcontent + label: Static Content + acls: + - id: static_content.menu + label: Menu + - id: static_content.list + label: List + - id: static_content.add + label: Add + - id: static_content.update + label: Update + - id: static_content.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index 491e51aa..b0096cf5 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -155,6 +155,10 @@ main_menu: acl: warranty.upload label: Warranty Upload parent: support + - id: static_content_list + acl: static_content.list + label: Static Content + parent: support - id: service acl: service.menu diff --git a/config/routes/static_content.yaml b/config/routes/static_content.yaml new file mode 100644 index 00000000..88fe3cf4 --- /dev/null +++ b/config/routes/static_content.yaml @@ -0,0 +1,33 @@ +static_content_list: + path: /static_content + controller: App\Controller\StaticContentController::index + +static_content_rows: + path: /static_content/rows + controller: App\Controller\StaticContentController::rows + methods: [POST] + +static_content_create: + path: /static_content/create + controller: App\Controller\StaticContentController::addForm + methods: [GET] + +static_content_create_submit: + path: /static_content/create + controller: App\Controller\StaticContentController::addSubmit + methods: [POST] + +static_content_update: + path: /static_content/{id} + controller: App\Controller\StaticContentController::updateForm + methods: [GET] + +static_content_update_submit: + path: /static_content/{id} + controller: App\Controller\StaticContentController::updateSubmit + methods: [POST] + +static_content_delete: + path: /static_content/{id} + controller: App\Controller\StaticContentController::destroy + methods: [DELETE] diff --git a/src/Controller/StaticContentController.php b/src/Controller/StaticContentController.php new file mode 100644 index 00000000..affe3fd3 --- /dev/null +++ b/src/Controller/StaticContentController.php @@ -0,0 +1,272 @@ +denyAccessUnlessGranted('static_content.list', null, 'No access.'); + + return $this->render('static-content/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('static_content.list', null, 'No access.'); + + // build query + $qb = $this->getDoctrine() + ->getRepository(StaticContent::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + + // add fitlers 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.id', '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(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('static_content.update')) + $row['meta']['update_url'] = $this->generateUrl('static_content_update', ['id' => $row['id']]); + if ($this->isGranted('static_content.delete')) + $row['meta']['delete_url'] = $this->generateUrl('static_content_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="static_content_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('static_content.add', null, 'No access.'); + + $params = []; + $params['obj'] = new StaticContent(); + $params['mode'] = 'create'; + + // response + return $this->render('static-content/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('static_content.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $row = new StaticContent(); + + // set and save values + $row->setID($req->request->get('id')); + $row->setContent($req->request->get('content')); + + // validate + $errors = $validator->validate($row); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->persist($row); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + /** + * @Menu(selected="static_content_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('static_content.update', null, 'No access.'); + + $params = []; + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + $params['obj'] = $row; + + // response + return $this->render('static-content/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('static_content.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // set and save values + $row->setID($req->request->get('id')) + ->setContent($req->request->get('content')); + + // validate + $errors = $validator->validate($row); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + /** + * @Menu(selected="static_content_list") + */ + public function destroy($id) + { + $this->denyAccessUnlessGranted('static_content.delete', null, 'No access.'); + + $params = []; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::class)->find($id); + + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this row + $em->remove($row); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + protected function setQueryFilters($datatable, &$query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.name LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } +} diff --git a/src/Entity/StaticContent.php b/src/Entity/StaticContent.php new file mode 100644 index 00000000..b450bb2a --- /dev/null +++ b/src/Entity/StaticContent.php @@ -0,0 +1,54 @@ +id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } + + public function setContent($content) + { + $this->content = $content; + return $this; + } + + public function getContent() + { + return $this->content; + } +} + diff --git a/templates/static-content/form.html.twig b/templates/static-content/form.html.twig new file mode 100644 index 00000000..dece1005 --- /dev/null +++ b/templates/static-content/form.html.twig @@ -0,0 +1,142 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Static Content

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

+ {% if mode == 'update' %} + Edit Static Content + {{ obj.getID() }} + {% else %} + New Static Content + {% endif %} +

+
+
+
+
+
+
+
+ + + +
+
+
+
+ + + +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/static-content/list.html.twig b/templates/static-content/list.html.twig new file mode 100644 index 00000000..186f0958 --- /dev/null +++ b/templates/static-content/list.html.twig @@ -0,0 +1,138 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Static Content +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} From 91e495f2519231175599f956e5b0d8b0078e106b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 17 Jan 2020 08:11:09 +0000 Subject: [PATCH 58/61] Add checking for duplicate ID. #298 --- src/Controller/StaticContentController.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Controller/StaticContentController.php b/src/Controller/StaticContentController.php index affe3fd3..395736d4 100644 --- a/src/Controller/StaticContentController.php +++ b/src/Controller/StaticContentController.php @@ -134,7 +134,8 @@ class StaticContentController extends Controller $row = new StaticContent(); // set and save values - $row->setID($req->request->get('id')); + $id = $req->request->get('id'); + $row->setID($id); $row->setContent($req->request->get('content')); // validate @@ -143,6 +144,14 @@ class StaticContentController extends Controller // initialize error list $error_array = []; + // check for duplicate ID + $result = $em->getRepository(StaticContent::class)->find($id); + if ($result != null) + { + error_log($id); + $error_array['id'] = 'Duplicate ID exists.'; + } + // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); @@ -213,6 +222,14 @@ class StaticContentController extends Controller // initialize error list $error_array = []; + // check for duplicate ID + $result = $em->getRepository(StaticContent::class)->find($id); + if ($result != null) + { + error_log($id); + $error_array['id'] = 'Duplicate ID exists.'; + } + // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); From f5899f618d515a0331b70086cb5886b9be4bd253 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 20 Jan 2020 03:21:51 +0000 Subject: [PATCH 59/61] Fix for command that computes the expiration date of existing warranties. #301 --- src/Command/ComputeWarrantyExpiryDateCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index 1db6fd42..383370f7 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -46,7 +46,7 @@ class ComputeWarrantyExpiryDateCommand extends Command $date_purchase = $warr->getDatePurchase(); - $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + $batteries = $this->wh->getBatteriesForWarranty($warr); if (!empty($batteries)) { $warranty_class = $warr->getWarrantyClass(); From c224b1773d2b975d604173e07a9f1725e0d94871 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 20 Jan 2020 06:37:29 +0000 Subject: [PATCH 60/61] Modify query to find customer vehicle using plate number. #302 --- src/Command/UpdateCustomerVehicleWarrantyCommand.php | 2 +- src/Service/WarrantyHandler.php | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php index c771044f..89b6e5e0 100644 --- a/src/Command/UpdateCustomerVehicleWarrantyCommand.php +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -92,7 +92,7 @@ class UpdateCustomerVehicleWarrantyCommand extends Command } */ $this->wh->updateCustomerVehicle($serial, $batteries, $plate_number, $expiry_date); - $this->em->clear(); + $this->em->detach($row[0]); } } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index c63f0302..bfc30021 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -93,9 +93,12 @@ class WarrantyHandler { // find customer vehicle using plate number error_log('Finding customer vehicle with plate number ' . $plate_number); - $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + $cv_q = $this->em->createQuery('select count(cv) from App\Entity\CustomerVehicle cv where cv.plate_number = :plate_number') + ->setParameter('plate_number', $plate_number); + $cv_result = $cv_q->getSingleScalarResult(); + $battery_id = null; - if (!empty($cust_vehicles)) + if ($cv_result != 0) { if (!empty($batteries)) { @@ -115,8 +118,8 @@ class WarrantyHandler 'serial' => $serial, 'expiry_date' => $date_expire, 'plate_number' => $plate_number]); - $q->execute(); - } + $q->execute(); + } } public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) From d418f0ff5ad1d877db66abbe11545652b2b6659f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 22 Jan 2020 06:42:28 +0000 Subject: [PATCH 61/61] Remove hardcoded prefix from customer. Add country code to .env and services.yaml. #307 --- .env.dist | 3 +++ config/services.yaml | 8 ++++++-- src/Entity/Customer.php | 11 ++++------- .../CustomerHandler/CMBCustomerHandler.php | 19 +++++++++++++------ .../CustomerHandler/ResqCustomerHandler.php | 19 +++++++++++++------ .../JobOrderHandler/CMBJobOrderHandler.php | 15 ++++++++------- .../JobOrderHandler/ResqJobOrderHandler.php | 14 +++++++------- templates/customer/form.html.twig | 8 ++++---- 8 files changed, 58 insertions(+), 39 deletions(-) diff --git a/.env.dist b/.env.dist index f170c3c9..a9fe4b6a 100644 --- a/.env.dist +++ b/.env.dist @@ -50,3 +50,6 @@ GEOFENCE_ENABLE=settotrueorfalse CVU_MFG_ID=insertmfgidforunknownvehicles CVU_BRAND_ID=insertbrandidforunknownvehicles +# country code prefix +COUNTRY_CODE=+insertcountrycodehere + diff --git a/config/services.yaml b/config/services.yaml index d0af7f80..00c5c32f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -164,14 +164,18 @@ services: App\Service\InvoiceGeneratorInterface: "@App\\Service\\InvoiceGenerator\\ResqInvoiceGenerator" # job order generator - App\Service\JobOrderHandler\CMBJobOrderHandler: ~ + App\Service\JobOrderHandler\ResqJobOrderHandler: + arguments: + $country_code: "%env(COUNTRY_CODE)%" #job order generator interface #App\Service\JobOrderHandlerInterface: "@App\\Service\\JobOrderHandler\\CMBJobOrderHandler" App\Service\JobOrderHandlerInterface: "@App\\Service\\JobOrderHandler\\ResqJobOrderHandler" # customer generator - App\Service\CustomerHandler\CMBCustomerHandler: ~ + App\Service\CustomerHandler\ResqCustomerHandler: + arguments: + $country_code: "%env(COUNTRY_CODE)%" # customer generator interface #App\Service\CustomerHandlerInterface: "@App\\Service\\CustomerHandler\\CMBCustomerHandler" diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index abe8286b..b96dfae9 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -14,9 +14,6 @@ use App\Ramcar\CustomerClassification; */ class Customer { - // TODO: make this a setting somewhere - const COUNTRY_CODE_PREFIX = '+60'; - // unique id /** * @ORM\Id @@ -266,16 +263,16 @@ class Customer $phones = []; if (!empty($this->phone_mobile)) - $phones[] = self::COUNTRY_CODE_PREFIX . $this->phone_mobile; + $phones[] = $this->phone_mobile; if (!empty($this->phone_landline)) - $phones[] = self::COUNTRY_CODE_PREFIX . $this->phone_landline; + $phones[] = $this->phone_landline; if (!empty($this->phone_office)) - $phones[] = self::COUNTRY_CODE_PREFIX . $this->phone_office; + $phones[] = $this->phone_office; if (!empty($this->phone_fax)) - $phones[] = self::COUNTRY_CODE_PREFIX . $this->phone_fax; + $phones[] = $this->phone_fax; return $phones; } diff --git a/src/Service/CustomerHandler/CMBCustomerHandler.php b/src/Service/CustomerHandler/CMBCustomerHandler.php index a0f3ca2e..fb6a888e 100644 --- a/src/Service/CustomerHandler/CMBCustomerHandler.php +++ b/src/Service/CustomerHandler/CMBCustomerHandler.php @@ -25,17 +25,17 @@ use DateTime; class CMBCustomerHandler implements CustomerHandlerInterface { - const COUNTRY_CODE_PREFIX = '+60'; - protected $em; protected $validator; - + protected $country_code; protected $template_hash; - public function __construct(EntityManagerInterface $em, ValidatorInterface $validator) + public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, + string $country_code) { $this->em = $em; $this->validator = $validator; + $this->country_code = $country_code; $this->loadTemplates(); } @@ -121,7 +121,14 @@ class CMBCustomerHandler implements CustomerHandlerInterface $row['flag_csat'] = $orow->isCSAT(); // TODO: properly add mobile numbers and plate numbers as searchable/sortable fields, use doctrine events - $row['mobile_numbers'] = implode("
", $orow->getMobileNumberList()); + // prepend the country code before each mobile number + $mobile_number_list = []; + $mobile_numbers = $orow->getMobileNumberList(); + foreach ($mobile_numbers as $mobile_number) + { + $mobile_number_list[] = $this->country_code . $mobile_number; + } + $row['mobile_numbers'] = implode("
", $mobile_number_list); $row['plate_numbers'] = implode("
", $orow->getPlateNumberList()); $rows[] = $row; @@ -467,7 +474,7 @@ class CMBCustomerHandler implements CustomerHandlerInterface $cust = $cv->getCustomer(); $vehicles[] = [ 'id' => $cv->getID(), - 'text' => $cv->getPlateNumber() . ' ' . $cust->getFirstName() . ' ' . $cust->getLastName() . ' ('. self::COUNTRY_CODE_PREFIX . $cust->getPhoneMobile() . ')', + 'text' => $cv->getPlateNumber() . ' ' . $cust->getFirstName() . ' ' . $cust->getLastName() . ' ('. $this->country_code . $cust->getPhoneMobile() . ')', ]; } diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index 8ea688cf..5f7e00e2 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -27,17 +27,17 @@ use DateTime; class ResqCustomerHandler implements CustomerHandlerInterface { - const COUNTRY_CODE_PREFIX = '+63'; - protected $em; protected $validator; - + protected $country_code; protected $template_hash; - public function __construct(EntityManagerInterface $em, ValidatorInterface $validator) + public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, + string $country_code) { $this->em = $em; $this->validator = $validator; + $this->country_code = $country_code; $this->loadTemplates(); } @@ -124,7 +124,14 @@ class ResqCustomerHandler implements CustomerHandlerInterface $row['flag_csat'] = $orow->isCSAT(); // TODO: properly add mobile numbers and plate numbers as searchable/sortable fields, use doctrine events - $row['mobile_numbers'] = implode("
", $orow->getMobileNumberList()); + // prepend the country code before each mobile number + $mobile_number_list = []; + $mobile_numbers = $orow->getMobileNumberList(); + foreach ($mobile_numbers as $mobile_number) + { + $mobile_number_list[] = $this->country_code . $mobile_number; + } + $row['mobile_numbers'] = implode("
", $mobile_number_list); $row['plate_numbers'] = implode("
", $orow->getPlateNumberList()); $rows[] = $row; @@ -465,7 +472,7 @@ class ResqCustomerHandler implements CustomerHandlerInterface $cust = $cv->getCustomer(); $vehicles[] = [ 'id' => $cv->getID(), - 'text' => $cv->getPlateNumber() . ' ' . $cust->getFirstName() . ' ' . $cust->getLastName() . ' ('. self::COUNTRY_CODE_PREFIX . $cust->getPhoneMobile() . ')', + 'text' => $cv->getPlateNumber() . ' ' . $cust->getFirstName() . ' ' . $cust->getLastName() . ' ('. $this->country_code . $cust->getPhoneMobile() . ')', ]; } diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 1e4d8f9f..89dc9663 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -55,20 +55,20 @@ use FPDF; class CMBJobOrderHandler implements JobOrderHandlerInterface { - const COUNTRY_CODE_PREFIX = '+60'; - protected $em; protected $ic; protected $security; protected $validator; protected $translator; protected $rah; + protected $country_code; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, - TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah) + TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, + string $country_code) { $this->em = $em; $this->ic = $ic; @@ -76,6 +76,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->validator = $validator; $this->translator = $translator; $this->rah = $rah; + $this->country_code = $country_code; $this->loadTemplates(); } @@ -1860,7 +1861,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Mobile Phone:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneMobile() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneMobile() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneMobile() ? $this->country_code . $customer->getPhoneMobile() : '', 0, 'L'); // get Y after right cell $y2 = $pdf->GetY(); @@ -1877,7 +1878,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Landline:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneLandline() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneLandline() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneLandline() ? $this->country_code . $customer->getPhoneLandline() : '', 0, 'L'); // get Y after right cell $y2 = $pdf->GetY(); @@ -1887,11 +1888,11 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Office Phone:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneOffice() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneOffice() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneOffice() ? $this->country_code . $customer->getPhoneOffice() : '', 0, 'L'); $pdf->SetX($col2_x); $pdf->Cell($label_width, $line_height, 'Fax:'); - $pdf->MultiCell($val_width, $line_height, $customer && $customer->getPhoneFax() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneFax() : '', 0, 'L'); + $pdf->MultiCell($val_width, $line_height, $customer && $customer->getPhoneFax() ? $this->country_code . $customer->getPhoneFax() : '', 0, 'L'); // insert vehicle info $cv = $obj->getCustomerVehicle(); diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index f6f07188..e91e855e 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -53,25 +53,25 @@ use FPDF; class ResqJobOrderHandler implements JobOrderHandlerInterface { - const COUNTRY_CODE_PREFIX = '+60'; - protected $em; protected $ic; protected $security; protected $validator; protected $translator; + protected $country_code; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, - TranslatorInterface $translator) + TranslatorInterface $translator, string $country_code) { $this->em = $em; $this->ic = $ic; $this->security = $security; $this->validator = $validator; $this->translator = $translator; + $this->country_code = $country_code; $this->loadTemplates(); } @@ -1868,7 +1868,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Mobile Phone:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneMobile() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneMobile() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneMobile() ? $this->country_code . $customer->getPhoneMobile() : '', 0, 'L'); // get Y after right cell $y2 = $pdf->GetY(); @@ -1885,7 +1885,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Landline:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneLandline() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneLandline() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneLandline() ? $this->country_code . $customer->getPhoneLandline() : '', 0, 'L'); // get Y after right cell $y2 = $pdf->GetY(); @@ -1895,11 +1895,11 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $pdf->SetXY($col2_x, $y); $pdf->Cell($label_width, $line_height, 'Office Phone:'); - $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneOffice() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneOffice() : '', 0, 'L'); + $pdf->MultiCell(0, $line_height, $customer && $customer->getPhoneOffice() ? $this->country_code . $customer->getPhoneOffice() : '', 0, 'L'); $pdf->SetX($col2_x); $pdf->Cell($label_width, $line_height, 'Fax:'); - $pdf->MultiCell($val_width, $line_height, $customer && $customer->getPhoneFax() ? self::COUNTRY_CODE_PREFIX . $customer->getPhoneFax() : '', 0, 'L'); + $pdf->MultiCell($val_width, $line_height, $customer && $customer->getPhoneFax() ? $this->country_code . $customer->getPhoneFax() : '', 0, 'L'); // insert vehicle info $cv = $obj->getCustomerVehicle(); diff --git a/templates/customer/form.html.twig b/templates/customer/form.html.twig index 4f2f5e9f..f502e778 100644 --- a/templates/customer/form.html.twig +++ b/templates/customer/form.html.twig @@ -131,7 +131,7 @@ Mobile Phone
- +63 + {% trans %}country_code_prefix{% endtrans %}
@@ -141,7 +141,7 @@ Landline
- +63 + {% trans %}country_code_prefix{% endtrans %}
@@ -153,7 +153,7 @@ Office Phone
- +63 + {% trans %}country_code_prefix{% endtrans %}
@@ -163,7 +163,7 @@ Fax
- +63 + {% trans %}country_code_prefix{% endtrans %}