diff --git a/src/Command/ImportCarClubCustomerHubCommand.php b/src/Command/ImportCarClubCustomerHubCommand.php new file mode 100644 index 00000000..3b16d4b2 --- /dev/null +++ b/src/Command/ImportCarClubCustomerHubCommand.php @@ -0,0 +1,267 @@ +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 ""; + } + + + + +} diff --git a/src/Entity/CarClubCustomerHub.php b/src/Entity/CarClubCustomerHub.php new file mode 100644 index 00000000..2e64cab1 --- /dev/null +++ b/src/Entity/CarClubCustomerHub.php @@ -0,0 +1,43 @@ +id; + } + + public function setHub(Hub $hub) + { + $this->hub = $hub; + return $this; + } + + public function getHub() + { + return $this->hub; + } + +} diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 2257bfb2..4af84d96 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -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; + } }