Refactor create customer from warranty command #274

This commit is contained in:
Kendrick Chan 2019-11-21 00:02:44 +08:00
parent 737dd4e22a
commit d094b5e8d9
6 changed files with 247 additions and 161 deletions

View file

@ -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",

53
composer.lock generated
View file

@ -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",

View file

@ -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));
}
}

View file

@ -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
{

View file

@ -98,6 +98,9 @@
"doctrine/reflection": {
"version": "v1.0.0"
},
"edwinhoksberg/php-fcm": {
"version": "1.0.0"
},
"guzzlehttp/guzzle": {
"version": "6.3.0"
},

View file

@ -0,0 +1,23 @@
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Fcm\FcmClient;
use Fcm\Push\Notification;
$api_token = 'AAAArpsiQbQ:APA91bHQVXxkUxNp3i2kviEmRDAnXKUyP0-Z5HZD15gV5V4TgidjmEiVVHilPnLffTW4u9txWkA_PPthTv7Baq8_wx99hr9Bx62q4JWrPsrfnNMeEw9w5ojPPu0eCOGOKHn_l1HoY_SJBS1n6DTt7IC0L14Kn33kmw';
$sender_id = 'AIzaSyD5gUUkXwaKarWYIigJGLLTzf9KQwoJ2wM';
// Instantiate the client with the project api_token and sender_id.
$client = new FcmClient($api_token, $sender_id);
// Instantiate the push notification request object.
$notification = new Notification();
// Enhance the notification object with our custom options.
$notification->addRecipient($device_id)
->setTitle('Motolite RES-Q')
->setBody('Test notification sending')
// Send the notification to the Firebase servers for further handling.
$client->send($notification);