Refactor entire car club import command #572
This commit is contained in:
parent
5830ad0c09
commit
bfc86abd6a
1 changed files with 197 additions and 154 deletions
|
|
@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\CustomerTag;
|
||||
|
|
@ -56,14 +57,84 @@ class ImportCarClubCustomerDataCommand extends Command
|
|||
$this->setName('customer:createfromcarclub')
|
||||
->setDescription('Create customers from car club file.')
|
||||
->setHelp('Creates customers from car club file.')
|
||||
->addArgument('promo_tag', InputArgument::REQUIRED, 'Promo customer tag ID to use.')
|
||||
->addArgument('input_file', InputArgument::REQUIRED, 'Path to the output CSV file with the car club info.')
|
||||
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
|
||||
}
|
||||
|
||||
protected function getValidContactNumbers($contact_number)
|
||||
{
|
||||
// check contact number if mobile or not
|
||||
// check for spaces, slash, and forward slash
|
||||
$contact_num_array = [];
|
||||
if (preg_match('/[\\\s\/]/', $contact_number))
|
||||
{
|
||||
$contact_num_array = preg_split('/[\\\s\/]/', $contact_number);
|
||||
}
|
||||
else
|
||||
{
|
||||
// only one mobile number
|
||||
$contact_num_array[] = $contact_number;
|
||||
}
|
||||
|
||||
// collet clean numbers
|
||||
$clean_nums = [];
|
||||
foreach ($contact_num_array as $contact_num)
|
||||
{
|
||||
$c_num = trim($contact_num);
|
||||
|
||||
// remove any non digit character from string
|
||||
$clean_number = preg_replace('~\D~', '', $c_num);
|
||||
error_log('cleaned ' . $clean_number);
|
||||
|
||||
// QUESTION: why are these all ifs and not else ifs?
|
||||
|
||||
// does it fit our 09XXXXXXXXX pattern?
|
||||
if (preg_match('/^09[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
// remove first '0'
|
||||
$clean_number = substr($clean_number, 1);
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
}
|
||||
|
||||
// does it fit our 63XXXXXXXXXX pattern?
|
||||
if (preg_match('/^63[0-9]{10}$/', $clean_number))
|
||||
{
|
||||
// remove the 63
|
||||
$clean_number = substr($clean_number, 2);
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
}
|
||||
|
||||
// does it fit our 9XXXXXXXXX pattern?
|
||||
if (!preg_match('/^9[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
error_log('not a mobile number');
|
||||
// set clean_number to blank since we don't save a non-mobile number
|
||||
$clean_number = '';
|
||||
}
|
||||
|
||||
// not a blank, save it
|
||||
if ($clean_number != '')
|
||||
{
|
||||
$clean_nums[] = $clean_number;
|
||||
}
|
||||
}
|
||||
|
||||
return $clean_nums;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$csv_file = $input->getArgument('input_file');
|
||||
$output_file = $input->getArgument('output_file');
|
||||
$promo_tag_id = $input->getArgument('promo_tag');
|
||||
|
||||
// fetch promo tag
|
||||
$promo_tag = $this->em->getRepository(CustomerTag::class)->find($promo_tag_id);
|
||||
if ($promo_tag == null)
|
||||
{
|
||||
throw new Exception('Could not find promo tag - ' . $promo_tag_id);
|
||||
}
|
||||
|
||||
// attempt to open file
|
||||
try
|
||||
|
|
@ -85,150 +156,9 @@ class ImportCarClubCustomerDataCommand extends Command
|
|||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$timestamp = trim($fields[self::F_TIMESTAMP]);
|
||||
$mname = trim($fields[self::F_MIDDLE_NAME]);
|
||||
$birthdate = trim($fields[self::F_BIRTHDATE]);
|
||||
$address = trim($fields[self::F_ADDRESS]);
|
||||
$city = trim($fields[self::F_CITY]);
|
||||
$region = trim($fields[self::F_REGION]);
|
||||
$car_club = trim($fields[self::F_CAR_CLUB]);
|
||||
$position = trim($fields[self::F_POSITION]);
|
||||
$member_number = trim($fields[self::F_MEMBER_NUM]);
|
||||
$vehicle = trim($fields[self::F_VEHICLE]);
|
||||
$vehicle_excel = trim($fields[self::F_VEHICLE_EXCEL]);
|
||||
$batt_size = trim($fields[self::F_BATT_SIZE]);
|
||||
$replace_sked = trim($fields[self::F_REPLACE_SKED]);
|
||||
$dealer_visit = trim($fields[self::F_DEALER_VISIT]);
|
||||
$bwi_location = trim($fields[self::F_BWI_LOCATION]);
|
||||
$sap_code = trim($fields[self::F_SAP_CODE]);
|
||||
$sku = trim($fields[self::F_SKU]);
|
||||
$qty = trim($fields[self::F_QTY]);
|
||||
$fname = trim($fields[self::F_FIRST_NAME]);
|
||||
$lname = trim($fields[self::F_LAST_NAME]);
|
||||
$dpa = trim($fields[self::F_DPA]);
|
||||
$contact_number = trim($fields[SELF::F_CONTACT_NUM]);
|
||||
|
||||
// check customer tag
|
||||
$cust_tag = $this->findCustomerTag($car_club);
|
||||
|
||||
// check contact number if mobile or not
|
||||
// check for spaces, slash, and forward slash
|
||||
$contact_num_array = [];
|
||||
$cust_id = '';
|
||||
if (preg_match('/[\\\s\/]/', $contact_number))
|
||||
{
|
||||
$contact_num_array = preg_split('/[\\\s\/]/', $contact_number);
|
||||
}
|
||||
else
|
||||
{
|
||||
// only one mobile number
|
||||
$contact_num_array[] = $contact_number;
|
||||
}
|
||||
|
||||
foreach ($contact_num_array as $contact_num)
|
||||
{
|
||||
$c_num = trim($contact_num);
|
||||
|
||||
if (empty($c_num))
|
||||
{
|
||||
// add info to output array
|
||||
$output_info[] = $this->addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city,
|
||||
$region, $car_club, $position, $member_number, $vehicle, $vehicle_excel, $batt_size, $replace_sked,
|
||||
$dealer_visit, $contact_number, $bwi_location, $sap_code, $sku, $qty, 'NOT CREATED', 'Missing contact number', $cust_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove any non digit character from string
|
||||
$clean_number = preg_replace('~\D~', '', $c_num);
|
||||
error_log('cleaned ' . $clean_number);
|
||||
|
||||
// does it fit our 09XXXXXXXXX pattern?
|
||||
if (preg_match('/^09[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
// remove first '0'
|
||||
$clean_number = substr($clean_number, 1);
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
}
|
||||
|
||||
// does it fit our 63XXXXXXXXXX pattern?
|
||||
if (preg_match('/^63[0-9]{10}$/', $clean_number))
|
||||
{
|
||||
// remove the 63
|
||||
$clean_number = substr($clean_number, 2);
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
}
|
||||
|
||||
// does it fit our 9XXXXXXXXX pattern?
|
||||
if (!preg_match('/^9[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
error_log('not a mobile number');
|
||||
// set clean_number to blank since we don't save a non-mobile number
|
||||
$clean_number = '';
|
||||
}
|
||||
|
||||
$customer = $this->findCustomerByNumber($c_num);
|
||||
|
||||
// if no customer found, create one
|
||||
if ($customer == null)
|
||||
{
|
||||
error_log('Creating customer...');
|
||||
error_log('cust tag id ' . $cust_tag->getID());
|
||||
// check dpa
|
||||
$is_dpa = false;
|
||||
if (strtoupper($dpa) == 'YES')
|
||||
$is_dpa = true;
|
||||
|
||||
// get the customer tag?
|
||||
$c_tag = $this->em->getRepository(CustomerTag::class)->find($cust_tag->getID());
|
||||
|
||||
// create new customer
|
||||
$new_cust = new Customer();
|
||||
$new_cust->setFirstName($fname)
|
||||
->setLastName($lname)
|
||||
->setPhoneMobile($clean_number)
|
||||
->setDpaConsent($is_dpa)
|
||||
->setCreateSource('car_club_file')
|
||||
->addCustomerTag($c_tag);
|
||||
|
||||
$this->em->persist($new_cust);
|
||||
$this->em->flush();
|
||||
// get customer id of new customer here
|
||||
$cust_id = $new_cust->getId();
|
||||
|
||||
// add info to output array
|
||||
$output_info[] = $this->addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city,
|
||||
$region, $car_club, $position, $member_number, $vehicle, $vehicle_excel, $batt_size, $replace_sked,
|
||||
$dealer_visit, $contact_number, $bwi_location, $sap_code, $sku, $qty, 'CREATED', '', $cust_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
error_log('Updating customer...');
|
||||
// add customer tag to existing customer
|
||||
|
||||
$cust_id = $customer->getID();
|
||||
// need to check if customer tag already exists for customer
|
||||
$tag_exists = $customer->getCustomerTag($cust_tag->getID());
|
||||
if ($tag_exists == null)
|
||||
{
|
||||
$customer->addCustomerTag($cust_tag);
|
||||
|
||||
// add info to output array
|
||||
$output_info[] = $this->addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city,
|
||||
$region, $car_club, $position, $member_number, $vehicle, $vehicle_excel, $batt_size, $replace_sked,
|
||||
$dealer_visit, $contact_number, $bwi_location, $sap_code, $sku, $qty, 'UPDATED', '', $cust_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
// add info to output array
|
||||
$output_info[] = $this->addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city,
|
||||
$region, $car_club, $position, $member_number, $vehicle, $vehicle_excel, $batt_size, $replace_sked,
|
||||
$dealer_visit, $contact_number, $bwi_location, $sap_code, $sku, $qty, 'NOT CREATED/UPDATED', 'CUSTOMER AND TAG ALREADY EXIST', $cust_id);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
// process row
|
||||
$output_info[] = $this->processRow($fields, $promo_tag);
|
||||
|
||||
$row_num++;
|
||||
}
|
||||
|
|
@ -240,17 +170,32 @@ class ImportCarClubCustomerDataCommand extends Command
|
|||
return 0;
|
||||
}
|
||||
|
||||
protected function findCustomerByNumber($number)
|
||||
protected function setOutputInfo($fields, $status, $reason, $cust_id)
|
||||
{
|
||||
$customer = $this->em->getRepository(Customer::class)->findOneBy(['phone_mobile' => $number]);
|
||||
return $customer;
|
||||
}
|
||||
$timestamp = trim($fields[self::F_TIMESTAMP]);
|
||||
$mname = trim($fields[self::F_MIDDLE_NAME]);
|
||||
$birthdate = trim($fields[self::F_BIRTHDATE]);
|
||||
$address = trim($fields[self::F_ADDRESS]);
|
||||
$city = trim($fields[self::F_CITY]);
|
||||
$region = trim($fields[self::F_REGION]);
|
||||
$car_club = trim($fields[self::F_CAR_CLUB]);
|
||||
$position = trim($fields[self::F_POSITION]);
|
||||
$member_number = trim($fields[self::F_MEMBER_NUM]);
|
||||
$vehicle = trim($fields[self::F_VEHICLE]);
|
||||
$vehicle_excel = trim($fields[self::F_VEHICLE_EXCEL]);
|
||||
$batt_size = trim($fields[self::F_BATT_SIZE]);
|
||||
$replace_sked = trim($fields[self::F_REPLACE_SKED]);
|
||||
$dealer_visit = trim($fields[self::F_DEALER_VISIT]);
|
||||
$bwi_location = trim($fields[self::F_BWI_LOCATION]);
|
||||
$sap_code = trim($fields[self::F_SAP_CODE]);
|
||||
$sku = trim($fields[self::F_SKU]);
|
||||
$qty = trim($fields[self::F_QTY]);
|
||||
$fname = trim($fields[self::F_FIRST_NAME]);
|
||||
$lname = trim($fields[self::F_LAST_NAME]);
|
||||
$dpa = trim($fields[self::F_DPA]);
|
||||
$contact_number = trim($fields[SELF::F_CONTACT_NUM]);
|
||||
|
||||
protected function addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city,
|
||||
$region, $car_club, $position, $member_number, $vehicle, $vehicle_excel, $batt_size, $replace_sked,
|
||||
$dealer_visit, $contact_number, $bwi_location, $sap_code, $sku, $qty, $status, $reason, $cust_id)
|
||||
{
|
||||
$output_entry = [
|
||||
return [
|
||||
$timestamp,
|
||||
$dpa,
|
||||
$fname,
|
||||
|
|
@ -277,8 +222,106 @@ class ImportCarClubCustomerDataCommand extends Command
|
|||
$reason,
|
||||
$cust_id
|
||||
];
|
||||
}
|
||||
|
||||
return $output_entry;
|
||||
protected function createNewCustomer($fields, $contact_numbers, $cust_tag, $promo_tag)
|
||||
{
|
||||
$fname = trim($fields[self::F_FIRST_NAME]);
|
||||
$lname = trim($fields[self::F_LAST_NAME]);
|
||||
$dpa = trim($fields[self::F_DPA]);
|
||||
|
||||
if (count($contact_numbers) > 0)
|
||||
$clean_number = $contact_numbers[0];
|
||||
else
|
||||
$clean_number = '';
|
||||
|
||||
// check dpa
|
||||
$is_dpa = false;
|
||||
if (strtoupper($dpa) == 'YES')
|
||||
$is_dpa = true;
|
||||
|
||||
// create new customer
|
||||
$new_cust = new Customer();
|
||||
$new_cust->setFirstName($fname)
|
||||
->setLastName($lname)
|
||||
->setPhoneMobile($clean_number)
|
||||
->setDpaConsent($is_dpa)
|
||||
->setCreateSource('car_club_file')
|
||||
->addCustomerTag($cust_tag)
|
||||
->addCustomerTag($promo_tag);
|
||||
|
||||
$this->em->persist($new_cust);
|
||||
$this->em->flush();
|
||||
|
||||
return $new_cust;
|
||||
}
|
||||
|
||||
protected function processRow($fields, $promo_tag)
|
||||
{
|
||||
$contact_number = trim($fields[SELF::F_CONTACT_NUM]);
|
||||
$car_club = trim($fields[self::F_CAR_CLUB]);
|
||||
|
||||
// clean up contact number
|
||||
$valid_contact_numbers = $this->getValidContactNumbers($contact_number);
|
||||
|
||||
// NOTE: commenting this out because we want to add customers without mobile number
|
||||
/*
|
||||
// QUESTION: do we need this?
|
||||
// QUESTION: why are we not adding those without contact numbers? Add customer, leave contact number blank was what was agreed, right?
|
||||
if (count($valid_contact_numbers) <= 0)
|
||||
{
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'NOT CREATED', 'Missing contact number', 0);
|
||||
}
|
||||
*/
|
||||
|
||||
// check customer tag
|
||||
$cust_tag = $this->findCustomerTag($car_club);
|
||||
$cust_id = '';
|
||||
|
||||
// give me first customer that matches any of the numbers
|
||||
$customer = $this->findCustomerByNumbers($valid_contact_numbers);
|
||||
|
||||
// if no customer found, create one
|
||||
if ($customer == null)
|
||||
{
|
||||
error_log('Creating customer...');
|
||||
error_log('cust tag id ' . $cust_tag->getID());
|
||||
|
||||
// create new customer
|
||||
$new_cust = $this->createNewCustomer($fields, $valid_contact_numbers, $cust_tag, $promo_tag);
|
||||
|
||||
// get customer id of new customer here
|
||||
$cust_id = $new_cust->getID();
|
||||
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'CREATED', '', $cust_id);
|
||||
}
|
||||
|
||||
error_log('Updating customer...');
|
||||
// add customer tag to existing customer
|
||||
|
||||
$cust_id = $customer->getID();
|
||||
// need to check if customer tag already exists for customer
|
||||
$tag_exists = $customer->getCustomerTag($cust_tag->getID());
|
||||
if ($tag_exists == null)
|
||||
{
|
||||
$customer->addCustomerTag($cust_tag)
|
||||
->addCustomerTag($promo_tag);
|
||||
$this->em->flush();
|
||||
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'UPDATED', '', $cust_id);
|
||||
}
|
||||
|
||||
return $this->setOutputInfo($fields, 'NOT CREATED/UPDATED', 'Customer and tag already exist', $cust_id);
|
||||
}
|
||||
|
||||
protected function findCustomerByNumbers($numbers)
|
||||
{
|
||||
error_log(print_r($numbers, true));
|
||||
$customer = $this->em->getRepository(Customer::class)->findOneBy(['phone_mobile' => $numbers]);
|
||||
return $customer;
|
||||
}
|
||||
|
||||
protected function outputCustomerInfo($output_file, $entries)
|
||||
|
|
|
|||
Loading…
Reference in a new issue