Add writing to file actions done. #572
This commit is contained in:
parent
be48ecdb82
commit
7d99eab2cc
3 changed files with 154 additions and 7 deletions
|
|
@ -12,6 +12,7 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
use App\Entity\Customer;
|
use App\Entity\Customer;
|
||||||
|
use App\Entity\CustomerTag;
|
||||||
|
|
||||||
class ImportCarClubCustomerDataCommand extends Command
|
class ImportCarClubCustomerDataCommand extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -40,10 +41,12 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
const F_QTY = 21;
|
const F_QTY = 21;
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
@ -106,6 +109,32 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
$dpa = trim($fields[self::F_DPA]);
|
$dpa = trim($fields[self::F_DPA]);
|
||||||
$contact_number = trim($fields[SELF::F_CONTACT_NUM]);
|
$contact_number = trim($fields[SELF::F_CONTACT_NUM]);
|
||||||
|
|
||||||
|
// check customer tag
|
||||||
|
// find the customer tag for club
|
||||||
|
$tag_name = $this->normalizeClubName($car_club);
|
||||||
|
error_log($tag_name);
|
||||||
|
$cust_tag = null;
|
||||||
|
if (isset($this->cust_tag_hash[$tag_name]))
|
||||||
|
{
|
||||||
|
error_log('customer tag in hash');
|
||||||
|
$cust_tag = $this->cust_tag_hash[$tag_name];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 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);
|
||||||
|
// add to hash
|
||||||
|
$this->cust_tag_hash[$tag_name] = $new_cust_tag;
|
||||||
|
|
||||||
|
$this->em->persist($new_cust_tag);
|
||||||
|
|
||||||
|
$cust_tag = $new_cust_tag;
|
||||||
|
}
|
||||||
|
|
||||||
// check in case of multiple numbers
|
// check in case of multiple numbers
|
||||||
// check contact number if mobile or not
|
// check contact number if mobile or not
|
||||||
// check for spaces, slash, and forward slash
|
// check for spaces, slash, and forward slash
|
||||||
|
|
@ -166,6 +195,8 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
|
|
||||||
if (empty($customers))
|
if (empty($customers))
|
||||||
{
|
{
|
||||||
|
error_log('Creating customer...');
|
||||||
|
error_log('cust tag id ' . $cust_tag->getID());
|
||||||
// check dpa
|
// check dpa
|
||||||
$is_dpa = false;
|
$is_dpa = false;
|
||||||
if (strtoupper($dpa) == 'YES')
|
if (strtoupper($dpa) == 'YES')
|
||||||
|
|
@ -177,7 +208,8 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
->setLastName($lname)
|
->setLastName($lname)
|
||||||
->setPhoneMobile($clean_number)
|
->setPhoneMobile($clean_number)
|
||||||
->setDpaConsent($is_dpa)
|
->setDpaConsent($is_dpa)
|
||||||
->setCreateSource('car_club_file');
|
->setCreateSource('car_club_file')
|
||||||
|
->addCustomerTag($cust_tag);
|
||||||
|
|
||||||
$this->em->persist($new_cust);
|
$this->em->persist($new_cust);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
@ -185,16 +217,48 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
$cust_id = $new_cust->getId();
|
$cust_id = $new_cust->getId();
|
||||||
$this->em->clear();
|
$this->em->clear();
|
||||||
|
|
||||||
// output to file
|
// 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
|
else
|
||||||
{
|
{
|
||||||
// update customer
|
error_log('Updating customer...');
|
||||||
|
// add customer tag to existing customer
|
||||||
|
foreach ($customers as $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();
|
||||||
|
$this->em->clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$row_num++;
|
$row_num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write to output file
|
||||||
|
$this->outputCustomerInfo($output_file, $output_info);
|
||||||
|
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -205,7 +269,42 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
return $customers;
|
return $customers;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function outputCustomerInfo($output_file)
|
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 = [
|
||||||
|
$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
|
||||||
|
];
|
||||||
|
|
||||||
|
return $output_entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function outputCustomerInfo($output_file, $entries)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -240,8 +339,48 @@ class ImportCarClubCustomerDataCommand extends Command
|
||||||
'SAP CODE',
|
'SAP CODE',
|
||||||
'SKU',
|
'SKU',
|
||||||
'QTY',
|
'QTY',
|
||||||
|
'Status',
|
||||||
|
'Reason',
|
||||||
|
'Customer ID',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
foreach($entries as $row)
|
||||||
|
{
|
||||||
|
fputcsv($fh, $row);
|
||||||
|
}
|
||||||
|
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,7 @@ class Customer
|
||||||
|
|
||||||
// customer tags
|
// customer tags
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers")
|
* @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers", indexBy="id")
|
||||||
* @ORM\JoinTable(name="customer_customer_tags")
|
* @ORM\JoinTable(name="customer_customer_tags")
|
||||||
*/
|
*/
|
||||||
protected $customer_tags;
|
protected $customer_tags;
|
||||||
|
|
@ -643,6 +643,14 @@ class Customer
|
||||||
return $this->customer_tags;
|
return $this->customer_tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCustomerTag($id)
|
||||||
|
{
|
||||||
|
if (isset($this->customer_tags[$id]))
|
||||||
|
return $this->customer_tags[$id];
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function removeCustomerTag(CustomerTag $customer_tag)
|
public function removeCustomerTag(CustomerTag $customer_tag)
|
||||||
{
|
{
|
||||||
$this->customer_tags->removeElement($customer_tag);
|
$this->customer_tags->removeElement($customer_tag);
|
||||||
|
|
|
||||||
|
|
@ -15,14 +15,14 @@ class CustomerTag
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @ORM\Id
|
* @ORM\Id
|
||||||
* @ORM\Column(type="string", length=80, nullable=false, unique=true)
|
* @ORM\Column(type="string", length=200, nullable=false, unique=true)
|
||||||
* @Assert\NotBlank()
|
* @Assert\NotBlank()
|
||||||
*/
|
*/
|
||||||
protected $id;
|
protected $id;
|
||||||
|
|
||||||
// name of tag
|
// name of tag
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=80)
|
* @ORM\Column(type="string", length=200)
|
||||||
* @Assert\NotBlank()
|
* @Assert\NotBlank()
|
||||||
*/
|
*/
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue