diff --git a/src/Command/ImportCarClubCustomerDataCommand.php b/src/Command/ImportCarClubCustomerDataCommand.php index 87ff2b75..9e623057 100644 --- a/src/Command/ImportCarClubCustomerDataCommand.php +++ b/src/Command/ImportCarClubCustomerDataCommand.php @@ -12,6 +12,7 @@ use Doctrine\ORM\EntityManagerInterface; use DateTime; use App\Entity\Customer; +use App\Entity\CustomerTag; class ImportCarClubCustomerDataCommand extends Command { @@ -40,10 +41,12 @@ class ImportCarClubCustomerDataCommand extends Command const F_QTY = 21; protected $em; + protected $cust_tag_hash; public function __construct(EntityManagerInterface $em) { $this->em = $em; + $this->loadCustomerTags(); parent::__construct(); } @@ -106,6 +109,32 @@ class ImportCarClubCustomerDataCommand extends Command $dpa = trim($fields[self::F_DPA]); $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 contact number if mobile or not // check for spaces, slash, and forward slash @@ -166,6 +195,8 @@ class ImportCarClubCustomerDataCommand extends Command if (empty($customers)) { + error_log('Creating customer...'); + error_log('cust tag id ' . $cust_tag->getID()); // check dpa $is_dpa = false; if (strtoupper($dpa) == 'YES') @@ -177,7 +208,8 @@ class ImportCarClubCustomerDataCommand extends Command ->setLastName($lname) ->setPhoneMobile($clean_number) ->setDpaConsent($is_dpa) - ->setCreateSource('car_club_file'); + ->setCreateSource('car_club_file') + ->addCustomerTag($cust_tag); $this->em->persist($new_cust); $this->em->flush(); @@ -185,16 +217,48 @@ class ImportCarClubCustomerDataCommand extends Command $cust_id = $new_cust->getId(); $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 { - // 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++; } + + // write to output file + $this->outputCustomerInfo($output_file, $output_info); + fclose($fh); return 0; } @@ -205,7 +269,42 @@ class ImportCarClubCustomerDataCommand extends Command 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 { @@ -240,8 +339,48 @@ class ImportCarClubCustomerDataCommand extends Command 'SAP CODE', 'SKU', 'QTY', + 'Status', + 'Reason', + 'Customer ID', ]); + foreach($entries as $row) + { + fputcsv($fh, $row); + } + 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; + } } diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 2f593e00..2257bfb2 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -210,7 +210,7 @@ class Customer // customer tags /** - * @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers") + * @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers", indexBy="id") * @ORM\JoinTable(name="customer_customer_tags") */ protected $customer_tags; @@ -643,6 +643,14 @@ class Customer 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) { $this->customer_tags->removeElement($customer_tag); diff --git a/src/Entity/CustomerTag.php b/src/Entity/CustomerTag.php index 4ce840ab..57d9a22f 100644 --- a/src/Entity/CustomerTag.php +++ b/src/Entity/CustomerTag.php @@ -15,14 +15,14 @@ class CustomerTag { /** * @ORM\Id - * @ORM\Column(type="string", length=80, nullable=false, unique=true) + * @ORM\Column(type="string", length=200, nullable=false, unique=true) * @Assert\NotBlank() */ protected $id; // name of tag /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=200) * @Assert\NotBlank() */ protected $name;