Fix import command for the file. Fix association between carclubcustomerhub and hub. #609
This commit is contained in:
parent
72a4ef6f72
commit
dd983f11ef
2 changed files with 168 additions and 47 deletions
|
|
@ -10,24 +10,29 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
use App\Entity\Customer;
|
use App\Entity\Customer;
|
||||||
|
use App\Entity\CustomerTag;
|
||||||
use App\Entity\CarClubCustomerHub;
|
use App\Entity\CarClubCustomerHub;
|
||||||
use App\Entity\Hub;
|
use App\Entity\Hub;
|
||||||
|
|
||||||
class ImportCarClubCustomerHubCommand extends Command
|
class ImportCarClubCustomerHubCommand extends Command
|
||||||
{
|
{
|
||||||
// field index in csv file
|
// field index in csv file
|
||||||
const F_REGION = 0;
|
const F_HUB_NAME = 0;
|
||||||
const F_OUTLET = 1;
|
const F_HUB_ADDRESS = 1;
|
||||||
const F_ADDRESS = 2;
|
const F_CAR_CLUB = 2;
|
||||||
const F_LAST_NAME_COUNT = 3;
|
const F_FIRST_NAME = 3;
|
||||||
const F_CONTACT_PERSON = 4;
|
const F_LAST_NAME = 4;
|
||||||
const F_CONTACT_NUMBER = 5;
|
const F_CONTACT_NUMBER = 5;
|
||||||
|
const F_BATT_SIZE = 6;
|
||||||
|
const F_SERIAL = 7;
|
||||||
|
|
||||||
protected $em;
|
protected $em;
|
||||||
|
protected $cust_tag_hash;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
|
$this->loadCustomerTags();
|
||||||
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
@ -37,6 +42,7 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
$this->setName('customer:importcarclubhub')
|
$this->setName('customer:importcarclubhub')
|
||||||
->setDescription('Import assigned hubs for customers for car club.')
|
->setDescription('Import assigned hubs for customers for car club.')
|
||||||
->setHelp('Import assigned hubs for customers for car club.')
|
->setHelp('Import assigned hubs for customers for car club.')
|
||||||
|
->addArgument('promo_tag', InputArgument::REQUIRED, 'Promo customer tag ID to use.')
|
||||||
->addArgument('input_file', InputArgument::REQUIRED, 'Path to the input CSV file with the customer and hub info.')
|
->addArgument('input_file', InputArgument::REQUIRED, 'Path to the input CSV file with the customer and hub info.')
|
||||||
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
|
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
|
||||||
}
|
}
|
||||||
|
|
@ -45,6 +51,14 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
{
|
{
|
||||||
$csv_file = $input->getArgument('input_file');
|
$csv_file = $input->getArgument('input_file');
|
||||||
$output_file = $input->getArgument('output_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
|
// attempt to open file
|
||||||
try
|
try
|
||||||
|
|
@ -68,7 +82,7 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
}
|
}
|
||||||
|
|
||||||
// process row
|
// process row
|
||||||
$output_info[] = $this->processRow($fields);
|
$output_info[] = $this->processRow($fields, $promo_tag);
|
||||||
|
|
||||||
$row_num++;
|
$row_num++;
|
||||||
}
|
}
|
||||||
|
|
@ -81,71 +95,124 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processRow($fields)
|
protected function processRow($fields, $promo_tag)
|
||||||
{
|
{
|
||||||
$hub_name = trim($fields[SELF::F_OUTLET]);
|
|
||||||
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
||||||
|
$car_club = trim($fields[self::F_CAR_CLUB]);
|
||||||
|
|
||||||
if (empty($contact_number))
|
if (empty($contact_number))
|
||||||
{
|
{
|
||||||
// add info to output array
|
// add info to output array
|
||||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No mobile number', '');
|
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
|
$hub_name = trim($fields[SELF::F_HUB_NAME]);
|
||||||
$customer = $this->findCustomerByNumbers($valid_contact_numbers);
|
|
||||||
|
|
||||||
// if no customer found, create one
|
|
||||||
if ($customer == null)
|
|
||||||
{
|
|
||||||
error_log('Creating customer...');
|
|
||||||
|
|
||||||
// TODO: create customer
|
|
||||||
// add info to output array
|
|
||||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No customer found', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
// find hub using name
|
// find hub using name
|
||||||
$hub = $this->findHubByName($hub_name);
|
$hub = $this->findHubByName($hub_name);
|
||||||
|
|
||||||
// if no hub, log to output info
|
if ($hub == null)
|
||||||
{
|
{
|
||||||
// add info to output array
|
// add info to output array
|
||||||
return $this->setOutputInfo($fields, 'NOT ADDED', 'No hub found', '');
|
return $this->setOutputInfo($fields, 'NOT ADDED', 'No hub found', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// at this point, we have customer and hub
|
// clean up contact number
|
||||||
// create CarClubCustomerHub
|
$valid_contact_numbers = $this->getValidContactNumbers($contact_number);
|
||||||
$car_club_cust_hub = new CarClubCustomerHub();
|
|
||||||
|
|
||||||
|
// check customer tag
|
||||||
|
$cust_tag = $this->findCustomerTag($car_club);
|
||||||
|
$cust_id = '';
|
||||||
|
|
||||||
|
// get first customer that matches any of the numbers
|
||||||
|
$customer = $this->findCustomerByNumbers($valid_contact_numbers);
|
||||||
|
|
||||||
|
// if no customer found, create one and add the hub
|
||||||
|
if ($customer == null)
|
||||||
|
{
|
||||||
|
error_log('Creating customer...');
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// create the carclubcustomerhub
|
||||||
|
$this->createCarClubCustomerHub($fields, $new_cust, $hub);
|
||||||
|
|
||||||
|
// add info to output array
|
||||||
|
return $this->setOutputInfo($fields, 'CUSTOMER CREATED AND CUSTOMER HUB ADDED', '', $cust_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
//customer exists, we just need to add the hub and the promo tag
|
||||||
|
// NOTE: for now, we add the promo tag and the hub
|
||||||
|
// to the first customer found with the mobile number
|
||||||
|
$customer->addCustomerTag($promo_tag)
|
||||||
|
->addCustomerTag($cust_tag);
|
||||||
|
|
||||||
|
// create the carclubcustomerhub
|
||||||
|
$this->createCarClubCustomerHub($fields, $customer, $hub);
|
||||||
|
|
||||||
|
return $this->setOutputInfo($fields, 'CUSTOMER HUB ADDED', '', $customer->getID());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createCarClubCustomerHub($fields, $cust, $hub)
|
||||||
|
{
|
||||||
|
$car_club_cust_hub = new CarClubCustomerHub();
|
||||||
$car_club_cust_hub->setHub($hub);
|
$car_club_cust_hub->setHub($hub);
|
||||||
$customer->setCarClubCustomerHub($car_club_cust_hub);
|
|
||||||
|
$cust->setCarClubCustomerHub($car_club_cust_hub);
|
||||||
|
|
||||||
$this->em->persist($car_club_cust_hub);
|
$this->em->persist($car_club_cust_hub);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->setOutputInfo($fields, 'ADDED', '', $customer->getID());
|
protected function createNewCustomer($fields, $contact_numbers, $cust_tag, $promo_tag)
|
||||||
|
{
|
||||||
|
$fname = trim($fields[self::F_FIRST_NAME]);
|
||||||
|
$lname = trim($fields[self::F_LAST_NAME]);
|
||||||
|
|
||||||
|
if (count($contact_numbers) > 0)
|
||||||
|
$clean_number = $contact_numbers[0];
|
||||||
|
else
|
||||||
|
$clean_number = '';
|
||||||
|
|
||||||
|
// create new customer
|
||||||
|
$new_cust = new Customer();
|
||||||
|
$new_cust->setFirstName($fname)
|
||||||
|
->setLastName($lname)
|
||||||
|
->setPhoneMobile($clean_number)
|
||||||
|
->setCreateSource('car_club_file')
|
||||||
|
->addCustomerTag($cust_tag)
|
||||||
|
->addCustomerTag($promo_tag);
|
||||||
|
|
||||||
|
$this->em->persist($new_cust);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $new_cust;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setOutputInfo($fields, $status, $reason, $cust_id)
|
protected function setOutputInfo($fields, $status, $reason, $cust_id)
|
||||||
{
|
{
|
||||||
$region = trim($fields[SELF::F_REGION]);
|
$hub_name = trim($fields[SELF::F_HUB_NAME]);
|
||||||
$outlet = trim($fields[SELF::F_OUTLET]);
|
$hub_address = trim($fields[SELF::F_HUB_ADDRESS]);
|
||||||
$address = trim($fields[SELF::F_ADDRESS]);
|
$car_club_name = trim($fields[SELF::F_CAR_CLUB]);
|
||||||
$last_name_count = trim($fields[SELF::F_LAST_NAME_COUNT]);
|
$first_name = trim($fields[self::F_FIRST_NAME]);
|
||||||
$contact_person = trim($fields[SELF::F_CONTACT_PERSON]);
|
$last_name = trim($fields[self::F_LAST_NAME]);
|
||||||
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
$contact_number = trim($fields[SELF::F_CONTACT_NUMBER]);
|
||||||
|
$battery_size = trim($fields[SELF::F_BATT_SIZE]);
|
||||||
|
$serial = trim($fields[SELF::F_SERIAL]);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
$region,
|
$hub_name,
|
||||||
$outlet,
|
$hub_address,
|
||||||
$address,
|
$car_club_name,
|
||||||
$last_name_count,
|
$first_name,
|
||||||
$contact_person,
|
$last_name,
|
||||||
$contact_number,
|
$contact_number,
|
||||||
|
$battery_size,
|
||||||
|
$serial,
|
||||||
$status,
|
$status,
|
||||||
$reason,
|
$reason,
|
||||||
$cust_id
|
$cust_id
|
||||||
|
|
@ -165,12 +232,14 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
|
|
||||||
// write the headers
|
// write the headers
|
||||||
fputcsv($fh, [
|
fputcsv($fh, [
|
||||||
'Region',
|
'Hub Name',
|
||||||
'Outlet Assignment',
|
'Hub Address',
|
||||||
'Address',
|
'Car Club Name',
|
||||||
'Count of Last Name',
|
'First Name',
|
||||||
'Contact Person',
|
'Last Name',
|
||||||
'Contact Number',
|
'Contact Number',
|
||||||
|
'Battery Size',
|
||||||
|
'Serial',
|
||||||
'Status',
|
'Status',
|
||||||
'Reason',
|
'Reason',
|
||||||
'Customer ID',
|
'Customer ID',
|
||||||
|
|
@ -198,6 +267,31 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
return $hub;
|
return $hub;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function findCustomerTag($car_club)
|
||||||
|
{
|
||||||
|
// find the customer tag for club
|
||||||
|
$tag_name = $this->normalizeClubName($car_club);
|
||||||
|
error_log($tag_name);
|
||||||
|
if (isset($this->cust_tag_hash[$tag_name]))
|
||||||
|
return $this->cust_tag_hash[$tag_name];
|
||||||
|
|
||||||
|
error_log('customer tag not in hash...');
|
||||||
|
// create the customer tag
|
||||||
|
$new_cust_tag = new CustomerTag();
|
||||||
|
$tag_details = json_decode('{"type":"car club"}', true);
|
||||||
|
$new_cust_tag->setID($tag_name)
|
||||||
|
->setName(strtoupper($car_club))
|
||||||
|
->setTagDetails($tag_details);
|
||||||
|
$this->em->persist($new_cust_tag);
|
||||||
|
|
||||||
|
// need to flush before adding to hash
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->cust_tag_hash[$tag_name] = $new_cust_tag;
|
||||||
|
|
||||||
|
return $new_cust_tag;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getValidContactNumbers($contact_number)
|
protected function getValidContactNumbers($contact_number)
|
||||||
{
|
{
|
||||||
// check contact number if mobile or not
|
// check contact number if mobile or not
|
||||||
|
|
@ -264,7 +358,35 @@ class ImportCarClubCustomerHubCommand extends Command
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function loadCustomerTags()
|
||||||
|
{
|
||||||
|
$this->cust_tag_hash = [];
|
||||||
|
|
||||||
|
$cust_tags = $this->em->getRepository(CustomerTag::class)->findAll();
|
||||||
|
foreach ($cust_tags as $cust_tag)
|
||||||
|
{
|
||||||
|
$tag_id = $cust_tag->getID();
|
||||||
|
$this->cust_tag_hash[$tag_id] = $cust_tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function normalizeClubName($name)
|
||||||
|
{
|
||||||
|
// uppercase the name
|
||||||
|
$clean_name = trim(strtoupper($name));
|
||||||
|
|
||||||
|
// remove apostrophes
|
||||||
|
$clean_name = str_replace("'", '', $clean_name);
|
||||||
|
|
||||||
|
// replace all special characters except ampersand (&) with whitespace
|
||||||
|
$clean_name = trim(preg_replace('/[^A-Za-z0-9&]/', ' ', $clean_name));
|
||||||
|
|
||||||
|
// replace spaces with underscore (_)
|
||||||
|
$clean_name = preg_replace('/\s+/', '_', $clean_name);
|
||||||
|
|
||||||
|
// prepend 'TAG_'
|
||||||
|
$tag_name = 'TAG_' . $clean_name;
|
||||||
|
|
||||||
|
return $tag_name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,7 @@ class CarClubCustomerHub
|
||||||
protected $id;
|
protected $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* one CarClubCustomerHub has one Hub
|
* @ORM\ManyToOne(targetEntity="Hub")
|
||||||
* @ORM\OneToOne(targetEntity="Hub")
|
|
||||||
*/
|
*/
|
||||||
protected $hub;
|
protected $hub;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue