Add CarClubCustomerHub entity. Add command to import customer and hub information for car club. #609
This commit is contained in:
parent
34f651e35b
commit
6df384027e
3 changed files with 328 additions and 0 deletions
267
src/Command/ImportCarClubCustomerHubCommand.php
Normal file
267
src/Command/ImportCarClubCustomerHubCommand.php
Normal file
|
|
@ -0,0 +1,267 @@
|
|||
<?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 App\Entity\Customer;
|
||||
use App\Entity\CarClubCustomerHub;
|
||||
use App\Entity\Hub;
|
||||
|
||||
class ImportCarClubCustomerHubCommand extends Command
|
||||
{
|
||||
// field index in csv file
|
||||
const F_REGION = 0;
|
||||
const F_OUTLET = 1;
|
||||
const F_ADDRESS = 2;
|
||||
const F_LAST_NAME_COUNT = 3;
|
||||
const F_CONTACT_PERSON = 4;
|
||||
const F_CONTACT_NUMBER = 5;
|
||||
|
||||
protected $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customer:importcarclubhub')
|
||||
->setDescription('Import assigned hubs for customers for car club.')
|
||||
->setHelp('Import assigned hubs for customers for car club.')
|
||||
->addArgument('input_file', InputArgument::REQUIRED, 'Path to the input CSV file with the customer and hub info.')
|
||||
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$csv_file = $input->getArgument('input_file');
|
||||
$output_file = $input->getArgument('output_file');
|
||||
|
||||
// attempt to open file
|
||||
try
|
||||
{
|
||||
$fh = fopen($csv_file, "r");
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
throw new Exception('The file "' . $csv_file . '" could be opened.');
|
||||
}
|
||||
|
||||
$row_num = 1;
|
||||
$output_info = [];
|
||||
while (($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
// ignore first row
|
||||
if ($row_num == 1)
|
||||
{
|
||||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// process row
|
||||
$output_info[] = $this->processRow($fields);
|
||||
|
||||
$row_num++;
|
||||
}
|
||||
|
||||
// write to output file
|
||||
$this->outputCarClubCustomerHubInfo($output_file, $output_info);
|
||||
|
||||
fclose($fh);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function processRow($fields)
|
||||
{
|
||||
$hub_name = trim($fields[SELF::F_OUTLET]);
|
||||
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
||||
|
||||
if (empty($contact_number))
|
||||
{
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No mobile number', '');
|
||||
}
|
||||
|
||||
// clean up contact number
|
||||
$valid_contact_numbers = $this->getValidContactNumbers($contact_number);
|
||||
|
||||
// get first customer that matches any of the numbers
|
||||
$customer = $this->findCustomerByNumbers($valid_contact_numbers);
|
||||
|
||||
// if no customer found, log to output info
|
||||
if ($customer == null)
|
||||
{
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No customer found', '');
|
||||
}
|
||||
|
||||
// find hub using name
|
||||
$hub = $this->findHubByName($hub_name);
|
||||
|
||||
// if no hub, log to output info
|
||||
{
|
||||
// add info to output array
|
||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No hub found', '');
|
||||
}
|
||||
|
||||
// at this point, we have customer and hub
|
||||
// create CarClubCustomerHub
|
||||
$car_club_cust_hub = new CarClubCustomerHub();
|
||||
|
||||
$car_club_cust_hub->setHub($hub);
|
||||
$customer->setCarClubCustomerHub($car_club_cust_hub);
|
||||
|
||||
$this->em->persist($car_club_cust_hub);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->setOutputInfo($fields, 'ADDED', '', $customer->getID());
|
||||
}
|
||||
|
||||
protected function setOutputInfo($fields, $status, $reason, $cust_id)
|
||||
{
|
||||
$region = trim($fields[SELF::F_REGION]);
|
||||
$outlet = trim($fields[SELF::F_OUTLET]);
|
||||
$address = trim($fields[SELF::F_ADDRESS]);
|
||||
$last_name_count = trim($fields[SELF::F_LAST_NAME_COUNT]);
|
||||
$contact_person = trim($fields[SELF::F_CONTACT_PERSON]);
|
||||
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
||||
|
||||
return [
|
||||
$region,
|
||||
$outlet,
|
||||
$address,
|
||||
$last_name_count,
|
||||
$contact_person,
|
||||
$contact_number,
|
||||
$status,
|
||||
$reason,
|
||||
$cust_id
|
||||
];
|
||||
}
|
||||
|
||||
protected function outputCarClubCustomerHubInfo($output_file, $entries)
|
||||
{
|
||||
try
|
||||
{
|
||||
$fh = fopen($output_file, "w");
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
throw new Exception('The file "' . $report_file . '" could be opened.');
|
||||
}
|
||||
|
||||
// write the headers
|
||||
fputcsv($fh, [
|
||||
'Region',
|
||||
'Outlet Assignment',
|
||||
'Address',
|
||||
'Count of Last Name',
|
||||
'Contact Person',
|
||||
'Contact Number',
|
||||
'Status',
|
||||
'Reason',
|
||||
'Customer ID',
|
||||
]);
|
||||
|
||||
foreach($entries as $row)
|
||||
{
|
||||
fputcsv($fh, $row);
|
||||
}
|
||||
|
||||
fclose($fh);
|
||||
|
||||
}
|
||||
protected function findCustomerByNumbers($numbers)
|
||||
{
|
||||
error_log(print_r($numbers, true));
|
||||
$customer = $this->em->getRepository(Customer::class)->findOneBy(['phone_mobile' => $numbers]);
|
||||
return $customer;
|
||||
}
|
||||
|
||||
protected function findHubByName($hub_name)
|
||||
{
|
||||
$hname = strtoupper($hub_name);
|
||||
$hub = $this->em->getRepository(Hub::class)->findOneBy(['name' => $hname]);
|
||||
return $hub;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// collect clean numbers
|
||||
$clean_nums = [];
|
||||
foreach ($contact_num_array as $contact_num)
|
||||
{
|
||||
$c_num = trim($contact_num);
|
||||
|
||||
// clean the numbers
|
||||
$cleaned_number = $this->normalizeContactNumber($c_num);
|
||||
|
||||
// not a blank, save it
|
||||
if ($cleaned_number != '')
|
||||
{
|
||||
$clean_nums[] = $cleaned_number;
|
||||
}
|
||||
}
|
||||
|
||||
return $clean_nums;
|
||||
}
|
||||
|
||||
protected function normalizeContactNumber($c_num)
|
||||
{
|
||||
// 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");
|
||||
return $clean_number;
|
||||
}
|
||||
// does it fit our 63XXXXXXXXXX pattern?
|
||||
else if (preg_match('/^63[0-9]{10}$/', $clean_number))
|
||||
{
|
||||
// remove the 63
|
||||
$clean_number = substr($clean_number, 2);
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
return $clean_number;
|
||||
}
|
||||
// does it fit our 9XXXXXXXXX pattern?
|
||||
else if (preg_match('/^9[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
error_log("already cleaned $clean_number");
|
||||
return $clean_number;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
43
src/Entity/CarClubCustomerHub.php
Normal file
43
src/Entity/CarClubCustomerHub.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="car_club_customer_hub")
|
||||
*/
|
||||
class CarClubCustomerHub
|
||||
{
|
||||
// unique id
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* one CarClubCustomerHub has one Hub
|
||||
* @ORM\OneToOne(targetEntity="Hub")
|
||||
*/
|
||||
protected $hub;
|
||||
|
||||
public function getID()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setHub(Hub $hub)
|
||||
{
|
||||
$this->hub = $hub;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getHub()
|
||||
{
|
||||
return $this->hub;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -215,6 +215,13 @@ class Customer
|
|||
*/
|
||||
protected $customer_tags;
|
||||
|
||||
// customer car club hub
|
||||
/**
|
||||
* One customer has one car club customer info
|
||||
* @ORM\OneToOne(targetEntity="CarClubCustomerHub")
|
||||
*/
|
||||
protected $car_club_customer_hub;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->numbers = new ArrayCollection();
|
||||
|
|
@ -656,4 +663,15 @@ class Customer
|
|||
$this->customer_tags->removeElement($customer_tag);
|
||||
$customer_tag->removeCustomer($this);
|
||||
}
|
||||
|
||||
public function setCarClubCustomerHub(CarClubCustomerHub $car_club_customer_hub)
|
||||
{
|
||||
$this->car_club_customer_hub = $car_club_customer_hub;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCarClubCustomerHub()
|
||||
{
|
||||
return $this->car_club_customer_hub;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue