From 9c4db034d0fcfdf1db273cd9f00be5781c9e1021 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 19 Nov 2019 08:35:51 +0000 Subject: [PATCH] Add command to create customer and customer vehicle from warranty. #274 --- .../CreateCustomerFromWarrantyCommand.php | 161 ++++++++++++++++++ src/Controller/APIController.php | 1 + 2 files changed, 162 insertions(+) create mode 100644 src/Command/CreateCustomerFromWarrantyCommand.php diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php new file mode 100644 index 00000000..2533d2c9 --- /dev/null +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -0,0 +1,161 @@ +em = $em; + + $this->loadCustomers(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:createfromwarranty') + ->setDescription('Create customers from existing warranties.') + ->setHelp('Creates customers from existing warranties.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get all warranties + $warranties = $this->em->getRepository(Warranty::class)->findAll(); + + foreach($warranties as $warr) + { + // 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 + continue; + } + + // parse warranty mobile in case of multiple numbers + // check for spaces, slash, and forward slash + $w_mobile_array = []; + if (preg_match('/[\/\\]/', $w_mobile)) + { + $w_mobile_array = preg_split('/[\/\\]/', $w_mobile); + } + // only one mobile number + $w_mobile_array[] = $w_mobile; + } + + $cust_found = false; + + // set values for new customer vehicle + $w_plate_number = $warr->getPlateNumber(); + $default_cv_color = 'White'; + // TODO: add checking that default manufacturer is not null + $default_manufacturer = $this->em->getRepository(VehicleManufacturer::class)->findBy('name' =>'Unknown'); + $default_make = 'Unknown'; + // search cust_index for numbers in mobile_array + foreach ($w_mobile_array as $w_mobile_num) + { + // if present, check if customer vehicle plate number matches warranty plate number? + foreach ($this->cust_index as $key => $customer) + { + $c_mobile = $customer->getPhoneMobile(); + if (!(empty($c_mobile))) + { + if (strpos($c_mobile, $w_mobile_num)) + { + // 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 + foreach ($c_vehicles as $c_vehicle) + { + $cv_plate_number = $c_vehicle->getPlateNumber(); + if ($cv_plate_number == $w_plate_number) + { + // get out of all loops since current warranty belongs to an + // existing customer and customer vehicle + break 3; + } + } + } + // add customer vehicle to existing customer with unknown manufacturer and make + $this->createCustomerVehicle($customer, $default_manufacturer, + $default_make, $w_plate_number, $default_cv_color); + } + $cust_found = true; + } + // no customer mobile number, ignore for now + } + // if customer not found, add customer and customer vehicle + if ($cust_found != true) + { + // get warranty first name, last name + $w_first_name = $warr->getFirstName(); + $w_last_name = $warr->getLastName(); + + $new_cust = new Customer(); + $new_cust->setFirstName($w_first_name) + ->setLastName($w_last_name); + + $this->em->persist($cust); + + $this->createCustomerVehicle($cust, $default_manufacturer, + $default_make, $w_plate_number, $default_cv_color) + + } + } + + } + + } + + protected function loadCustomers() + { + // get all customers + $customers = $this->em->getRepository(Customer::class)->findAll(); + + $this->cust_index = []; + foreach ($customers as $customer) + { + $cust_id = $customer->getID(); + $this->cust_index[$cust_id] = $customer; + } + } + + protected function createCustomerVehicle(Customer $cust, $manufacturer, $make, + $plate_number, $color) + { + $new_vehicle = new Vehicle(); + $new_cv = new CustomerVehicle(); + + // get manufacturer and make with name 'unknown' + $new_vehicle->setManufacturer($manufacturer) + + // TODO: remove the assert not blank for color and model year + $new_cv->setCustomer($cust) + ->setPlateNumber($plate_number) + ->setModelYear(0) + ->setColor($color) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setFuelType(FuelType::GAS) + ->setVehicle($vehicle); + } +} diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 75dcb9d0..0b360d9c 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -334,6 +334,7 @@ class APIController extends Controller // TODO: check if we have the number registered before and merge + // TODO: check if mobile matches mobile of customer $dupe_sess = $this->findNumberSession($this->session->getPhoneNumber()); if ($dupe_sess != null) {