diff --git a/src/Command/ImportCarClubCustomerDataCommand.php b/src/Command/ImportCarClubCustomerDataCommand.php new file mode 100644 index 00000000..2d5f5ac2 --- /dev/null +++ b/src/Command/ImportCarClubCustomerDataCommand.php @@ -0,0 +1,156 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:createfromcarclub') + ->setDescription('Create customers from car club file.') + ->setHelp('Creates customers from car club file.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the car club info.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be opened.'); + } + + $row_num = 1; + while (($fields = fgetcsv($fh)) !== false) + { + // ignore first row + if ($row_num == 1) + { + $row_num++; + continue; + } + + $fname = trim($fields[self::F_FIRST_NAME]); + $lname = trim($fields[self::F_LAST_NAME]); + $dpa = trim($fields[self::F_DPA]); + $contact_number = trim($fields[SELF::F_CONTACT_NUM]); + + // check in case of multiple numbers + // check contact number if mobile or not + // check for spaces, slash, and forward slash + // mobile has length 10 if no 63, 12 if has 63 + // landline has 8 if no area code, 10 if with area code + $contact_num_array = []; + if (preg_match('/[\\\s\/]/', $contact_number)) + { + $contact_num_array = preg_split('/[\\\s\/]/', $contact_number); + } + else + { + // only one mobile number + $contact_number_array[] = $contact_number; + } + + foreach ($contact_num_array as $contact_num) + { + $c_num = trim($contact_num); + + if (empty($c_num)) + continue; + + // does it fit our 09XXXXXXXXX pattern? + if (preg_match('/^09[0-9]{9}$/', $c_num)) + { + // remove first '0' + $c_num = substr($c_num, 1); + error_log("CONVERTED TO $c_num"); + } + + // does it fit our 9XXXXXXXXX pattern? + if (!preg_match('/^9[0-9]{9}$/', $c_num)) + continue; + + $customers = $this->findCustomerByNumber($c_num); + + if (empty($customers)) + { + // check dpa + $is_dpa = false; + if (strtoupper($dpa) == 'YES') + $is_dpa = true; + + // create new customer + $new_cust = new Customer(); + $new_cust->setFirstName($fname) + ->setLastName($lname) + ->setPhoneMobile($c_num) + ->setDpaConsent($is_dpa) + ->setCreateSource('car_club_file'); + + $this->em->persist($new_cust); + } + } + + $this->em->flush(); + $this->em->clear(); + + $row_num++; + } + fclose($fh); + return 0; + } + + protected function findCustomerByNumber($number) + { + $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); + return $customers; + } +}