diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index df740b83..fb5b225b 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -94,20 +94,21 @@ class CreateCustomerFromWarrantyCommand extends Command { $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); } - // search cust_index for numbers in mobile_array + foreach ($w_mobile_array as $w_mobile_num) { - foreach ($this->cust_index as $key => $customer) + if (!empty($w_mobile_num)) { - $c_mobile = $customer->getPhoneMobile(); - if (!(empty($c_mobile))) + if (isset($this->cust_index[$w_mobile_num])) { - $pos = strpos($c_mobile, $w_mobile_num); - if ($pos !== false) + $customers = $this->cust_index[$w_mobile_num]; + + foreach ($customers as $customer) { - // mobile number belongs to existing customer - // get customer vehicles - if (!(empty($c_vehicles))) + // get customer vehicles for customer + $c_vehicles = $customer->getVehicles(); + + if (!empty($c_vehicles)) { // check if plate number of customer vehicle matches warranty plate number foreach ($c_vehicles as $c_vehicle) @@ -115,44 +116,52 @@ class CreateCustomerFromWarrantyCommand extends Command $cv_plate_number = $c_vehicle->getPlateNumber(); if ($cv_plate_number == $w_plate_number) { - // move to the next warranty since current warranty belongs to an - // existing customer and customer vehicle + // customer and customer vehicle already exists $cust_found = true; - break 3; + break; } } + if (!$cust_found) + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + } + } + else + { + // customer exists but not customer vehicle + // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; + $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } - // add customer vehicle to existing customer with unknown manufacturer and make - $cust_found = true; - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } } - // no customer mobile number, ignore for now - } - // if customer not found, add customer and customer vehicle - if (!($cust_found)) - { - // get warranty first name, last name - $w_first_name = $warr->getFirstName(); - $w_last_name = $warr->getLastName(); + else + { + // customer not found, add customer and customer vehicle + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); - $output->writeln($w_first_name); - $output->writeln($w_last_name); - $output->writeln($w_plate_number); + //$output->writeln($w_first_name); + //$output->writeln($w_last_name); + //$output->writeln($w_plate_number); - $new_cust = new Customer(); - $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name) - ->setPhoneMobile($w_mobile_num); + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); - $this->em->persist($new_cust); - $this->em->flush(); + $this->em->persist($new_cust); + $this->em->flush(); - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); - // add latest customer to hash - $cust_id = $new_cust->getID(); - $this->cust_index[$cust_id] = $new_cust; + // add latest customer to hash + $this->cust_index[$w_mobile_num][] = $new_cust; + } } } } @@ -186,8 +195,8 @@ class CreateCustomerFromWarrantyCommand extends Command } } - fclose($fh); - } + fclose($fh); + } protected function loadCustomers() { @@ -197,8 +206,32 @@ class CreateCustomerFromWarrantyCommand extends Command $this->cust_index = []; foreach ($customers as $customer) { - $cust_id = $customer->getID(); - $this->cust_index[$cust_id] = $customer; + $mobile = trim($customer->getPhoneMobile()); + if (!empty($mobile)) + { + $mobile_array = []; + // need to check if multiple numbers in mobile + if (preg_match('/[\\\s\/]/', $mobile)) + { + $mobile_array = preg_split('/[\\\s\/]/', $mobile); + } + else + { + // only one mobile number + $mobile_array[] = $mobile; + } + + foreach($mobile_array as $number) + { + if (!(empty($number))) + { + if (!isset($this->cust_index[$number])) + { + $this->cust_index[$number][] = $customer; + } + } + } + } } }