Add ids to env.dist. #274

This commit is contained in:
Korina Cordero 2019-11-20 11:36:56 +00:00
parent 98c5bdc670
commit 737dd4e22a
2 changed files with 153 additions and 122 deletions

View file

@ -45,3 +45,8 @@ OTP_MODE=settotestorrandom
# geofence # geofence
GEOFENCE_ENABLE=settotrueorfalse GEOFENCE_ENABLE=settotrueorfalse
# unknown manufacturer and vehicle ids
CVU_MFG_ID=insertmfgidforunknownvehicles
CVU_BRAND_ID=insertbrandidforunknownvehicles

View file

@ -7,6 +7,7 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Dotenv\Dotenv;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
@ -42,6 +43,13 @@ class CreateCustomerFromWarrantyCommand extends Command
} }
protected function execute(InputInterface $input, OutputInterface $output) 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'); $csv_file = $input->getArgument('file');
// attempt to open file // attempt to open file
@ -60,162 +68,180 @@ class CreateCustomerFromWarrantyCommand extends Command
$total_cust_added = 0; $total_cust_added = 0;
$total_cv_added = 0; $total_cv_added = 0;
// load all customers $default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id);
$this->loadCustomers(); if (empty($default_vehicle))
// get all warranties
$warranties = $this->em->getRepository(Warranty::class)->findAll();
$invalid_warranties = [];
foreach($warranties as $warr)
{ {
$cust_found = false; $output->writeln("Need to add vehicle with default values.");
// check if warranty mobile already exists in customer }
$w_mobile = $warr->getMobileNumber(); else
if (empty($w_mobile)) {
{ // load all customers
// TODO: for now, if warranty mobile number is empty, add to list of invalid entries $output->writeln('Loading customer data...');
$invalid_warranties[] = $this->processInvalidEntries($warr); $this->loadCustomers($output);
continue; // get all warranties
} $warranties = $this->em->getRepository(Warranty::class)->findAll();
// parse warranty mobile in case of multiple numbers $invalid_warranties = [];
// check for spaces, slash, and forward slash $output->writeln('Processing warranties... ');
$w_mobile_array = []; foreach($warranties as $warr)
if (preg_match('/[\\\s\/]/', $w_mobile)) {
{ $total_warr++;
$w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); $cust_found = false;
} // check if warranty mobile already exists in customer
else $w_mobile = $warr->getMobileNumber();
{ if (empty($w_mobile))
// 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))
{ {
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]; if (isset($this->cust_index[$w_mobile_num]))
foreach ($customers as $customer)
{ {
// get customer vehicles for customer $customers = $this->cust_index[$w_mobile_num];
$c_vehicles = $customer->getVehicles();
if (!empty($c_vehicles)) foreach ($customers as $customer)
{ {
// check if plate number of customer vehicle matches warranty plate number // get customer vehicles for customer
foreach ($c_vehicles as $c_vehicle) $c_vehicles = $customer->getVehicles();
if (!empty($c_vehicles))
{ {
$clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber()); // check if plate number of customer vehicle matches warranty plate number
if (!($clean_cv_plate)) foreach ($c_vehicles as $c_vehicle)
{ {
// add the vehicle from warranty $clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber());
$cust_found = true; if (!($clean_cv_plate))
$this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number);
}
else
{
if ($clean_cv_plate == $w_plate_number)
{ {
// customer and customer vehicle already exists // add the vehicle from warranty
$cust_found = true; $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 // customer exists but not customer vehicle
// add customer vehicle to existing customer with unknown manufacturer and make // add customer vehicle to existing customer with unknown manufacturer and make
$cust_found = true; $cust_found = true;
$this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); $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
else {
{ // customer not found, add customer and customer vehicle
// customer not found, add customer and customer vehicle // get warranty first name, last name
// get warranty first name, last name $w_first_name = $warr->getFirstName();
$w_first_name = $warr->getFirstName(); $w_last_name = $warr->getLastName();
$w_last_name = $warr->getLastName();
//$output->writeln($w_first_name); //$output->writeln($w_first_name);
//$output->writeln($w_last_name); //$output->writeln($w_last_name);
//$output->writeln($w_plate_number); //$output->writeln($w_plate_number);
$new_cust = new Customer(); $new_cust = new Customer();
$new_cust->setFirstName($w_first_name) $new_cust->setFirstName($w_first_name)
->setLastName($w_last_name) ->setLastName($w_last_name)
->setPhoneMobile($w_mobile_num); ->setPhoneMobile($w_mobile_num);
$this->em->persist($new_cust); $this->em->persist($new_cust);
$this->em->flush(); $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 // 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++;
}
} }
} }
} }
}
// process invalid warranties, if any // process invalid warranties, if any
if (count($invalid_warranties) > 0) 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); 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() protected function loadCustomers()