diff --git a/src/Command/ImportCarClubCustomerDataCommand.php b/src/Command/ImportCarClubCustomerDataCommand.php index c86948bb..8265be5f 100644 --- a/src/Command/ImportCarClubCustomerDataCommand.php +++ b/src/Command/ImportCarClubCustomerDataCommand.php @@ -110,30 +110,8 @@ class ImportCarClubCustomerDataCommand extends Command $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 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); + $cust_tag = $this->findCustomerTag($car_club); - // need to flush before adding to hash - $this->em->flush(); - - $this->cust_tag_hash[$tag_name] = $new_cust_tag; - - } - $cust_tag = $this->cust_tag_hash[$tag_name]; - - // check in case of multiple numbers // check contact number if mobile or not // check for spaces, slash, and forward slash $contact_num_array = []; @@ -189,9 +167,10 @@ class ImportCarClubCustomerDataCommand extends Command $clean_number = ''; } - $customers = $this->findCustomerByNumber($c_num); + $customer = $this->findCustomerByNumber($c_num); - if (empty($customers)) + // if no customer found, create one + if ($customer == null) { error_log('Creating customer...'); error_log('cust tag id ' . $cust_tag->getID()); @@ -216,7 +195,6 @@ class ImportCarClubCustomerDataCommand extends Command $this->em->flush(); // get customer id of new customer here $cust_id = $new_cust->getId(); - $this->em->clear(); // add info to output array $output_info[] = $this->addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city, @@ -227,30 +205,28 @@ class ImportCarClubCustomerDataCommand extends Command { 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); - } + $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(); } } @@ -266,8 +242,8 @@ class ImportCarClubCustomerDataCommand extends Command protected function findCustomerByNumber($number) { - $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); - return $customers; + $customer = $this->em->getRepository(Customer::class)->findOneBy(['phone_mobile' => $number]); + return $customer; } protected function addCustomerInfoEntry($timestamp, $dpa, $fname, $mname, $lname, $birthdate, $address, $city, @@ -384,4 +360,29 @@ class ImportCarClubCustomerDataCommand extends Command return $tag_name; } + + 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; + } }