Add command to create customer from car club file. #572

This commit is contained in:
Korina Cordero 2021-05-28 10:06:18 +00:00
parent 7f7bd15118
commit 079c8a033a

View file

@ -0,0 +1,156 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use DateTime;
use App\Entity\Customer;
class ImportCarClubCustomerDataCommand extends Command
{
// field index in csv file
const F_TIMESTAMP = 0;
const F_DPA = 1;
const F_FIRST_NAM = 2;
const F_MIDDLE_NAME = 3;
const F_LAST_NAME = 4;
const F_BIRTHDATE = 5;
const F_ADDRESS = 6;
const F_CITY = 7;
const F_REGION = 8;
const F_CAR_CLUB = 9;
const F_POSITION = 10;
const F_MEMBER_NUM = 11;
const F_VEHICLE = 12;
const F_VEHICLE_EXCEL = 13;
const F_BATT_SIZE = 14;
const F_CONTACT_NUM = 15;
const F_BWI_LOCATION = 16;
const F_SAP_CODE = 17;
const F_SKU = 18;
const F_QTY = 19;
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('customer:createfromcarclub')
->setDescription('Create customers from car club file.')
->setHelp('Creates customers from car club file.')
->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the car club info.');
}
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.');
}
$row_num = 1;
while (($fields = fgetcsv($fh)) !== false)
{
// ignore first row
if ($row_num == 1)
{
$row_num++;
continue;
}
$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 in case of multiple numbers
// check contact number if mobile or not
// check for spaces, slash, and forward slash
// mobile has length 10 if no 63, 12 if has 63
// landline has 8 if no area code, 10 if with area code
$contact_num_array = [];
if (preg_match('/[\\\s\/]/', $contact_number))
{
$contact_num_array = preg_split('/[\\\s\/]/', $contact_number);
}
else
{
// only one mobile number
$contact_number_array[] = $contact_number;
}
foreach ($contact_num_array as $contact_num)
{
$c_num = trim($contact_num);
if (empty($c_num))
continue;
// does it fit our 09XXXXXXXXX pattern?
if (preg_match('/^09[0-9]{9}$/', $c_num))
{
// remove first '0'
$c_num = substr($c_num, 1);
error_log("CONVERTED TO $c_num");
}
// does it fit our 9XXXXXXXXX pattern?
if (!preg_match('/^9[0-9]{9}$/', $c_num))
continue;
$customers = $this->findCustomerByNumber($c_num);
if (empty($customers))
{
// 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($c_num)
->setDpaConsent($is_dpa)
->setCreateSource('car_club_file');
$this->em->persist($new_cust);
}
}
$this->em->flush();
$this->em->clear();
$row_num++;
}
fclose($fh);
return 0;
}
protected function findCustomerByNumber($number)
{
$customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
return $customers;
}
}