diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 94f3b725..56620ae7 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -40,25 +40,28 @@ class CreateCustomerFromWarrantyCommand extends Command { // load all customers $this->loadCustomers(); + // get all warranties $warranties = $this->em->getRepository(Warranty::class)->findAll(); foreach($warranties as $warr) { + $cust_found = false; // check if warranty mobile already exists in customer $w_mobile = $warr->getMobileNumber(); if (empty($w_mobile)) { // TODO: for now, if warranty mobile number is empty, do nothing + $output->writeln('Move to next warranty since mobile number is empty'); continue; } // parse warranty mobile in case of multiple numbers // check for spaces, slash, and forward slash $w_mobile_array = []; - if (preg_match('/[\/\\]/', $w_mobile)) + if (preg_match('/[\\\s\/]/', $w_mobile)) { - $w_mobile_array = preg_split('/[\/\\]/', $w_mobile); + $w_mobile_array = preg_split('/[\\\s\/]/', $w_mobile); } else { @@ -66,11 +69,9 @@ class CreateCustomerFromWarrantyCommand extends Command $w_mobile_array[] = $w_mobile; } - $cust_found = false; - // set values for new customer vehicle $w_plate_number = $warr->getPlateNumber(); - $default_vehicle = $this->em->getRepository(Vehicle::class)->findBy(['name' =>'Unknown']); + $default_vehicle = $this->em->getRepository(Vehicle::class)->findOneBy(['make' =>'Unknown']); if (empty($default_vehicle)) { $output->writeln("Need to add vehicle with manufacturer name Unknown and make name Unknown"); @@ -84,11 +85,11 @@ class CreateCustomerFromWarrantyCommand extends Command $c_mobile = $customer->getPhoneMobile(); if (!(empty($c_mobile))) { - if (strpos($c_mobile, $w_mobile_num)) + $pos = strpos($c_mobile, $w_mobile_num); + if ($pos !== false) { // mobile number belongs to existing customer // get customer vehicles - $c_vehicles = $customer->getVehicles(); if (!(empty($c_vehicles))) { // check if plate number of customer vehicle matches warranty plate number @@ -99,32 +100,42 @@ class CreateCustomerFromWarrantyCommand extends Command { // move to the next warranty since current warranty belongs to an // existing customer and customer vehicle + $cust_found = true; break 3; } } } // add customer vehicle to existing customer with unknown manufacturer and make + $cust_found = true; $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); } - $cust_found = true; } // no customer mobile number, ignore for now } // if customer not found, add customer and customer vehicle - if ($cust_found != true) + if (!($cust_found)) { // 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); + $new_cust = new Customer(); $new_cust->setFirstName($w_first_name) - ->setLastName($w_last_name); + ->setLastName($w_last_name) + ->setPhoneMobile($w_mobile_num); - $this->em->persist($cust); + $this->em->persist($new_cust); + $this->em->flush(); - $this->createCustomerVehicle($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; } } } @@ -150,7 +161,10 @@ class CreateCustomerFromWarrantyCommand extends Command $new_cv->setCustomer($cust) ->setPlateNumber($plate_number) ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) ->setVehicle($vehicle); $this->em->persist($new_cv);