From 9c4db034d0fcfdf1db273cd9f00be5781c9e1021 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 19 Nov 2019 08:35:51 +0000 Subject: [PATCH 01/15] 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 02/15] 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 03/15] 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 04/15] 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 05/15] 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 06/15] 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 07/15] 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 08/15] 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 09/15] 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 10/15] 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 11/15] 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 12/15] 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 13/15] 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 14/15] 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 15/15] 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 = '';