From eed91a413aeb9eccadd5b20fdbcc5c8dc94c2c2b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 18 Aug 2020 10:36:19 +0000 Subject: [PATCH 01/12] Add command to import CarFix data. #460 --- src/Command/ImportCMBCarFixDataCommand.php | 316 +++++++++++++++++++++ src/Entity/Customer.php | 1 - 2 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 src/Command/ImportCMBCarFixDataCommand.php diff --git a/src/Command/ImportCMBCarFixDataCommand.php b/src/Command/ImportCMBCarFixDataCommand.php new file mode 100644 index 00000000..f1fa23f3 --- /dev/null +++ b/src/Command/ImportCMBCarFixDataCommand.php @@ -0,0 +1,316 @@ +em = $em; + + // load existing battery data + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + $this->loadBatteries(); + + // load existing vehicle data + $this->loadVehicleManufacturers(); + $this->loadVehicleMakes(); + + parent::__construct(); + } + + public function configure() + { + $this->setName('cmbcarfixdata:import') + ->setDescription('Retrieve from a CSV file CarFix data.') + ->setHelp('Creates job orders based on data from imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + $row_num = 0; + error_log('Processing CarFix data csv file...'); + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get the information + $date_create = trim($fields[self::F_CREATED_DATE]); + $case_number = trim($fields[self::F_CASE_NO]); + $insurer = trim($fields[self::F_INSURER]); + $vehicle_number = trim($fields[self::F_VEHICLE_NO]); + $car_model = trim($fields[self::F_CAR_MODEL]); + $nature_of_call = trim($fields[self::F_NATURE_OF_CALL]); + $service_needed = trim($fields[self::F_SERVICE_NEEDED]); + $location = trim($fields[self::F_LOCATION]); + $state = trim($fields[self::F_STATE]); + $driver = trim($fields[self::F_DRIVER]); + $truck = trim($fields[self::F_TRUCK]); + $workshop_arrival_time = trim($fields[self::F_WORKSHOP_ARRIVAL_TIME]); + $status = trim($fields[self::F_STATUS]); + $customer_name = trim($fields[self::F_CUSTOMER_NAME]); + $customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]); + $reference = trim($fields[self::F_OUR_REFERENCE]); + $odometer = trim($fields[self::F_ODOMETER]); + $batt_model = trim(strtolower($fields[self::F_BATT_MODEL])); + $batt_size = trim(strtolower($fields[self::F_BATT_SIZE])); + $trade_in = trim($fields[self::F_BATT_TRADE_IN]); + $replaced_by = trim($fields[self::F_REPLACED_BY]); + $remark = trim($fields[self::F_REMARK]); + $satisfaction = trim($fields[self::F_SATISFACTION]); + + //error_log($date_create . ' ' . $case_number . ' ' . $driver . ' ' . $customer_name . ' ' . $remark . ' ' . $satisfaction); + + // get customer vehicle + // parse car_model + // vehicle manufacturer is the first entry + // vehicle model is the 2nd entry + // check if manufacturer is in hash + // if not, add to csv of unprocessed entries + // if so, check if model is in hash + // if not, add to csv of unprocessed entries + + // check batteries + // if battery info not in hash, add to csv of unprocessed entries + + $new_jo = new JobOrder(); + + // add to metadata + // case number, nature of call, workshop arrival time, reference, odometer, replaced by, satisfaction + $new_jo->addMeta('case_number', $case_number); + $new_jo->addMeta('nature_of_call', $nature_of_call); + $new_jo->addMeta('workshop_arrival_time', $workshop_arrival_time); + $new_jo->addMeta('reference', $reference); + $new_jo->addMeta('odometer', $odometer); + $new_jo->addMeta('replaced_by', $replaced_by); + $new_jo->addMeta('satisfaction', $satisfaction); + + // date_create + $created_date = DateTime::createFromFormat('d-m-Y H:i', $date_create); + $new_jo->setDateCreate($created_date); + + // insurer == responsible_party + $new_jo->setResponsibleParty($insurer); + + // delivery address = location + state + $delivery_address = $location . ', ' . $state; + $new_jo->setDeliveryAddress($delivery_address); + + // remarks == tier 1 notes + $new_jo->setTier1Notes($remark); + + // service_needed = service type + // check service needed: + // Battery == Battery Sales + // Battery Warranty Claim == Warranty Claim + // Battery Warranty Replacement == Warranty Replacement + $service = $this->normalizeName($service_needed); + switch ($service) + { + case 'battery': + $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_NEW); + break; + case 'battery warranty claim': + $new_jo->setServiceType(CMBServiceType::WARRANTY_CLAIM); + break; + case 'battery warranty replacement': + $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_WARRANTY); + break; + } + + // status set everything to fulfilled + // store old status to metadata + $new_jo->setStatus(JOStatus::FULFILLED); + $new_jo->addMeta('status', $status); + + // plate number == vehicle_number. Use as key to cust_hash + $clean_plate = $this->cleanPlateNumber($vehicle_number); + //error_log($clean_plate . ' ' . $new_jo->getServiceType()); + + // check if plate number has been added + if (!isset($this->cust_hash[$clean_plate])) + $this->addCustomer($clean_plate, $customer_name, $customer_mobile); + + $row_num++; + } + + return 0; + } + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $this->normalizeName($batt_manu->getName()); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $this->normalizeName($batt_model->getName()); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $this->normalizeName($batt_size->getName()); + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function loadBatteries() + { + $this->batt_hash = []; + + $batts = $this->em->getRepository(Battery::class)->findAll(); + foreach ($batts as $batt) + { + $brand = $this->normalizeName($batt->getManufacturer()->getName()); + $model = $this->normalizeName($batt->getModel()->getName()); + $size = $this->normalizeName($batt->getSize()->getName()); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + + protected function loadVehicleManufacturers() + { + $this->vmanu_hash = []; + + $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + foreach ($vmanus as $vmanu) + { + $name = $vmanu->getName(); + $this->vmanu_hash[$name] = $vmanu; + } + } + + protected function loadVehicleMakes() + { + $this->vmake_hash = []; + + $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); + foreach ($vmakes as $vmake) + { + $manufacturer = $vmake->getManufacturer()->getName(); + $make = $vmake->getMake(); + + $this->vmake_hash[$manufacturer][$make] = $vmake; + } + } + + public function addCustomer($plate_num, $name, $mobile) + { + } + + protected function normalizeName($name) + { + $normalized_key = trim(strtolower($name)); + + return $normalized_key; + } + + protected function cleanPlateNumber($plate_number) + { + // remove spaces and make upper case + $clean_plate_number = strtoupper(str_replace(' ' , '', $plate_number)); + + return $clean_plate_number; + } +} diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 9ba043c9..eae98fc3 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -42,7 +42,6 @@ class Customer // last name /** * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() */ protected $last_name; From 4796a9be14cea3bc202ce0ec12ea7dd8cac96aa7 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 19 Aug 2020 08:32:50 +0000 Subject: [PATCH 02/12] Fix a matching issue in the previous commands. Add saving of customer and customer vehicle for CarFix command. #460 --- src/Command/ImportCMBBatteryDataCommand.php | 6 +- src/Command/ImportCMBCarFixDataCommand.php | 207 ++++++++++++++++-- .../ImportCMBVehicleCompatibilityCommand.php | 8 +- 3 files changed, 197 insertions(+), 24 deletions(-) diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index 79e84571..30a6f4a4 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -148,19 +148,19 @@ class ImportCMBBatteryDataCommand extends Command // save battery manufacturer if not yet in system if (!isset($this->bmanu_hash[$normalized_manu])) { - $this->addBatteryManufacturer($battery_manufacturer); + $this->addBatteryManufacturer(strtoupper($battery_manufacturer)); } // save battery model if not yet in system if (!isset($this->bmodel_hash[$normalized_model])) { - $this->addBatteryModel($battery_model); + $this->addBatteryModel(strtoupper($battery_model)); } // save battery size if not yet in system if (!isset($this->bsize_hash[$normalized_size])) { - $this->addBatterySize($clean_size); + $this->addBatterySize(strtoupper($clean_size)); } // save battery if not yet in system diff --git a/src/Command/ImportCMBCarFixDataCommand.php b/src/Command/ImportCMBCarFixDataCommand.php index f1fa23f3..ff7f3b23 100644 --- a/src/Command/ImportCMBCarFixDataCommand.php +++ b/src/Command/ImportCMBCarFixDataCommand.php @@ -21,12 +21,15 @@ use App\Entity\CustomerVehicle; use App\Ramcar\CMBServiceType; use App\Ramcar\JOStatus; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; use DateTime; class ImportCMBCarFixDataCommand extends Command { // field index in csv file + const F_INDEX = 0; const F_CREATED_DATE = 1; const F_CASE_NO = 2; const F_INSURER = 3; @@ -61,7 +64,7 @@ class ImportCMBCarFixDataCommand extends Command protected $vmanu_hash; protected $vmake_hash; - protected $cust_hash; + protected $cv_hash; public function __construct(EntityManagerInterface $em) { @@ -106,6 +109,7 @@ class ImportCMBCarFixDataCommand extends Command $em = $this->em; $row_num = 0; + $invalid_entries = []; error_log('Processing CarFix data csv file...'); while (($fields = fgetcsv($fh)) !== false) { @@ -116,6 +120,7 @@ class ImportCMBCarFixDataCommand extends Command } // get the information + $entry_num = trim($fields[self::F_INDEX]); $date_create = trim($fields[self::F_CREATED_DATE]); $case_number = trim($fields[self::F_CASE_NO]); $insurer = trim($fields[self::F_INSURER]); @@ -143,16 +148,32 @@ class ImportCMBCarFixDataCommand extends Command //error_log($date_create . ' ' . $case_number . ' ' . $driver . ' ' . $customer_name . ' ' . $remark . ' ' . $satisfaction); // get customer vehicle - // parse car_model - // vehicle manufacturer is the first entry - // vehicle model is the 2nd entry - // check if manufacturer is in hash - // if not, add to csv of unprocessed entries - // if so, check if model is in hash - // if not, add to csv of unprocessed entries + $v_status = $this->processVehicleInfo($car_model); + if ($v_status != null) + { + error_log($v_status . ' ' . $car_model); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $v_status); + + // move to next entry + continue; + } // check batteries - // if battery info not in hash, add to csv of unprocessed entries + $batt_status = $this->processBatteryInfo($batt_model, $batt_size); + if ($batt_status != null) + { + error_log($batt_status); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $batt_status); + + // move to next entry + continue; + } $new_jo = new JobOrder(); @@ -204,20 +225,176 @@ class ImportCMBCarFixDataCommand extends Command $new_jo->setStatus(JOStatus::FULFILLED); $new_jo->addMeta('status', $status); - // plate number == vehicle_number. Use as key to cust_hash + // plate number == vehicle_number. Use as key to cv_hash $clean_plate = $this->cleanPlateNumber($vehicle_number); //error_log($clean_plate . ' ' . $new_jo->getServiceType()); // check if plate number has been added - if (!isset($this->cust_hash[$clean_plate])) - $this->addCustomer($clean_plate, $customer_name, $customer_mobile); - + if (!isset($this->cv_hash[$clean_plate])) + { + $cust = $this->addCustomer($customer_name, $customer_mobile); + $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); + } + else + { + // get customer vehicle from hash + $cv = $this->cv_hash[$clean_plate]; + $cust = $cv->getCustomer(); + } + + $new_jo->setCustomer($cust) + ->setCustomerVehicle($cv); + $row_num++; } + $this->em->flush(); + $this->em->clear(); + + // check for invalid entries. if any, write to csv + return 0; } + protected function processVehicleInfo($car_model) + { + // vehicle manufacturer is the first entry + // vehicle model is the 2nd entry + whatever follows + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + //error_log($v_manufacturer . ' ' . $v_model); + + // check if manufacturer is in hash + if (!isset($this->vmanu_hash[$v_manufacturer])) + { + //error_log($v_manufacturer . ' invalid.'); + return 'Vehicle manufacturer not in system.'; + } + + // check if manufacturer and make is in hash + if (!isset($this->vmake_hash[$v_manufacturer][$v_model])) + { + //error_log($v_model . ' invalid.'); + return 'Vehicle model not in system.'; + } + // car model info valid + return null; + } + + protected function processBatteryInfo($batt_model, $batt_size) + { + // check if battery model is in hash + if (!isset($this->bmodel_hash[$batt_model])) + return 'Battery model not in system.'; + + // check if battery size is in hash + if (!isset($this->bsize_hash[$batt_size])) + return 'Battery size not in system.'; + + // battery info valid + return null; + } + + protected function addCustomer($name, $mobile) + { + $new_cust = new Customer(); + + $new_cust->setFirstName($name) + ->setLastName('') + ->setPhoneMobile($mobile); + + $this->em->persist($new_cust); + + return $new_cust; + } + + protected function addCustomerVehicle($plate_num, $car_model, $customer) + { + $new_cv = new CustomerVehicle(); + + // get vehicle from hash + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + + $vehicle = $this->vmake_hash[$v_manufacturer][$v_model]; + + $new_cv->setCustomer($customer) + ->setPlateNumber($plate_num) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') + ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $this->em->persist($new_cv); + + // add customer vehicle to cv_hash + $this->cv_hash[$plate_num] = $new_cv; + + return $new_cv; + } + + protected function addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $v_status) + { + $inv_entry = [ + 'number' => $entry_num, + 'created_date' => $date_create, + 'case_number' => $case_number, + 'insurer' => $insurer, + 'vehicle_number' => $vehicle_number, + 'car_model' => $car_model, + 'nature_of_call' => $nature_of_call, + 'service_needed' => $service_needed, + 'location' => $location, + 'state' => $state, + 'driver' => $driver, + 'truck' => $truck, + 'workshop_arrival_time' => $workshop_arrival_time, + 'status' => $status, + 'customer_name' => $customer_name, + 'customer_phone_number' => $customer_mobile, + 'reference' => $reference, + 'odometer' => $odometer, + 'batt_model' => $batt_model, + 'batt_size' => $batt_size, + 'batt_trade_in' => $trade_in, + 'replaced_by' => $replaced_by, + 'remark' => $remark, + 'satisfaction' => $satisfaction, + 'reason' => $v_status, + ]; + + return $inv_entry; + } + protected function loadBatteryManufacturers() { $this->bmanu_hash = []; @@ -295,10 +472,6 @@ class ImportCMBCarFixDataCommand extends Command } } - public function addCustomer($plate_num, $name, $mobile) - { - } - protected function normalizeName($name) { $normalized_key = trim(strtolower($name)); diff --git a/src/Command/ImportCMBVehicleCompatibilityCommand.php b/src/Command/ImportCMBVehicleCompatibilityCommand.php index 4034aec8..47393c01 100644 --- a/src/Command/ImportCMBVehicleCompatibilityCommand.php +++ b/src/Command/ImportCMBVehicleCompatibilityCommand.php @@ -222,8 +222,8 @@ class ImportCMBVehicleCompatibilityCommand extends Command //} // vehicle info - $manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]); - $make = trim($fields[self::F_VEHICLE_MAKE]); + $manufacturer = trim(strtolower($fields[self::F_VEHICLE_MANUFACTURER])); + $make = trim(strtolower($fields[self::F_VEHICLE_MAKE])); $year = trim($fields[self::F_VEHICLE_YEAR]); // vehicle data @@ -298,7 +298,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command // save to db $vehicle_manufacturer = new VehicleManufacturer(); - $vehicle_manufacturer->setName($name); + $vehicle_manufacturer->setName(strtoupper($name)); $this->em->persist($vehicle_manufacturer); $this->em->flush(); @@ -332,7 +332,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command } $vehicle->setManufacturer($vmanu) - ->setMake($make) + ->setMake(strtoupper($make)) ->setModelYearFrom($year_from) ->setModelYearTo($year_to); From 4bfbfbf9951c9ca905c94b8ceab039c50438032c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 20 Aug 2020 06:11:34 +0000 Subject: [PATCH 03/12] Create command to import Carfix data. #460 --- .../ImportCMBLegacyJobOrderCommand.php | 164 ++++++++++++++++++ src/Entity/CMBLegacyJobOrder.php | 52 ++++++ 2 files changed, 216 insertions(+) create mode 100644 src/Command/ImportCMBLegacyJobOrderCommand.php create mode 100644 src/Entity/CMBLegacyJobOrder.php diff --git a/src/Command/ImportCMBLegacyJobOrderCommand.php b/src/Command/ImportCMBLegacyJobOrderCommand.php new file mode 100644 index 00000000..be25c4b3 --- /dev/null +++ b/src/Command/ImportCMBLegacyJobOrderCommand.php @@ -0,0 +1,164 @@ +em = $em; + + parent::__construct(); + } + + public function configure() + { + $this->setName('cmblegacyjoborderdata:import') + ->setDescription('Retrieve from a CSV file CarFix data.') + ->setHelp('Creates legacy job order entries based on data from imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + $row_num = 0; + error_log('Processing CarFix data csv file...'); + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get the information + $entry_num = trim($fields[self::F_INDEX]); + $date_create = trim($fields[self::F_CREATED_DATE]); + $case_number = trim($fields[self::F_CASE_NO]); + $insurer = trim($fields[self::F_INSURER]); + $vehicle_number = trim($fields[self::F_VEHICLE_NO]); + $car_model = trim($fields[self::F_CAR_MODEL]); + $nature_of_call = trim($fields[self::F_NATURE_OF_CALL]); + $service_needed = trim($fields[self::F_SERVICE_NEEDED]); + $location = trim($fields[self::F_LOCATION]); + $state = trim($fields[self::F_STATE]); + $driver = trim($fields[self::F_DRIVER]); + $truck = trim($fields[self::F_TRUCK]); + $workshop_arrival_time = trim($fields[self::F_WORKSHOP_ARRIVAL_TIME]); + $status = trim($fields[self::F_STATUS]); + $customer_name = trim($fields[self::F_CUSTOMER_NAME]); + $customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]); + $reference = trim($fields[self::F_OUR_REFERENCE]); + $odometer = trim($fields[self::F_ODOMETER]); + $batt_model = trim(strtolower($fields[self::F_BATT_MODEL])); + $batt_size = trim(strtolower($fields[self::F_BATT_SIZE])); + $trade_in = trim($fields[self::F_BATT_TRADE_IN]); + $replaced_by = trim($fields[self::F_REPLACED_BY]); + $remark = trim($fields[self::F_REMARK]); + $satisfaction = trim($fields[self::F_SATISFACTION]); + + $data_entry = $this->processEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction); + + $legacy_data = new CMBLegacyJobOrder(); + $legacy_data->setData($data_entry); + + $this->em->persist($legacy_data); + } + + $this->em->flush(); + $this->em->clear(); + + return 0; + } + + protected function processEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction) + { + $data_entry = [ + 'entry_num' => $entry_num, + 'created_date' => $date_create, + 'case_number' => $case_number, + 'insurer' => $insurer, + 'vehicle_number' => $vehicle_number, + 'car_model' => $car_model, + 'nature_of_call' => $nature_of_call, + 'service_needed' => $service_needed, + 'location' => $location, + 'state' => $state, + 'driver' => $driver, + 'truck' => $truck, + 'workshop_arrival_time' => $workshop_arrival_time, + 'status' => $status, + 'customer_name' => $customer_name, + 'customer_phone_number' => $customer_mobile, + 'reference' => $reference, + 'odometer' => $odometer, + 'batt_model' => $batt_model, + 'batt_size' => $batt_size, + 'batt_trade_in' => $trade_in, + 'replaced_by' => $replaced_by, + 'remark' => $remark, + 'satisfaction' => $satisfaction, + ]; + + return $data_entry; + } + +} diff --git a/src/Entity/CMBLegacyJobOrder.php b/src/Entity/CMBLegacyJobOrder.php new file mode 100644 index 00000000..603706be --- /dev/null +++ b/src/Entity/CMBLegacyJobOrder.php @@ -0,0 +1,52 @@ +data = []; + } + + public function addData($id, $value) + { + $this->data[$id] = $value; + return $this; + } + + public function setData(array $data_array) + { + $this->data = $data_array; + return $this; + } + + public function getData($id) + { + // return null if we don't have it + if (!isset($this->data[$id])) + return null; + + return $this->data[$id]; + } +} From 108d49ca5fe8b2c8efee569782b88aace3c6a181 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 20 Aug 2020 09:51:00 +0000 Subject: [PATCH 04/12] Create migration command for cmb legacy job order. #460 --- .../MigrateCMBLegacyJobOrderCommand.php | 342 ++++++++++++++++++ src/Entity/CMBLegacyJobOrder.php | 7 +- 2 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 src/Command/MigrateCMBLegacyJobOrderCommand.php diff --git a/src/Command/MigrateCMBLegacyJobOrderCommand.php b/src/Command/MigrateCMBLegacyJobOrderCommand.php new file mode 100644 index 00000000..4be27570 --- /dev/null +++ b/src/Command/MigrateCMBLegacyJobOrderCommand.php @@ -0,0 +1,342 @@ +em = $em; + + // load existing battery data + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + $this->loadBatteries(); + + // load existing vehicle data + $this->loadVehicleManufacturers(); + $this->loadVehicleMakes(); + + parent::__construct(); + } + + public function configure() + { + $this->setName('cmblegacyjoborderdata:migrate') + ->setDescription('Retrieve database cmb legacy job orders and insert into job order.') + ->setHelp('Creates job orders based on data from imported CSV.'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + // get all legacy job orders + $query = $this->em->createQuery('SELECT legacy_jo FROM App\Entity\CMBLegacyJobOrder legacy_jo'); + $result = $query->iterate(); + + $invalid_entries = []; + foreach ($result as $row) + { + $entry = $row[0]; + $jo_entry = $entry->getData(); + + // get the entry information + $entry_num = $jo_entry['entry_num']; + $date_created = $jo_entry['created_date']; + $case_number = $jo_entry['case_number']; + $insurer = $jo_entry['insurer']; + $plate_number = $this->cleanPlateNumber($jo_entry['vehicle_number']); + $car_model = $jo_entry['car_model']; + $nature_of_call = $jo_entry['nature_of_call']; + $service_needed = $jo_entry['service_needed']; + $location = $jo_entry['location']; + $state = $jo_entry['state']; + $driver = $jo_entry['driver']; + $truck = $jo_entry['truck']; + $workshop_arrival_time = $jo_entry['workshop_arrival_time']; + $status = $jo_entry['status']; + $customer_name = $jo_entry['customer_name']; + $customer_mobile = $jo_entry['customer_phone_number']; + $reference = $jo_entry['reference']; + $odometer = $jo_entry['odometer']; + $batt_model = $jo_entry['batt_model']; + $batt_size = $jo_entry['batt_size']; + $batt_trade_in = $jo_entry['batt_trade_in']; + $replaced_by = $jo_entry['replaced_by']; + $remark = $jo_entry['remark']; + $satisfaction = $jo_entry['satisfaction']; + + // check vehicle info if valid + $v_status = $this->processVehicleInfo($car_model); + if ($v_status != null) + { + error_log($v_status . ' ' . $car_model); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $v_status); + + // move to next entry + continue; + } + + // check battery info if valid + $batt_status = $this->processBatteryInfo($batt_model, $batt_size); + if ($batt_status != null) + { + error_log($batt_status); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $batt_status); + + // move to next entry + continue; + } + + // create job order + $new_jo = new JobOrder(); + + // add to metadata + // case number, nature of call, workshop arrival time, reference, odometer, replaced by, satisfaction + $new_jo->addMeta('case_number', $case_number); + $new_jo->addMeta('nature_of_call', $nature_of_call); + $new_jo->addMeta('workshop_arrival_time', $workshop_arrival_time); + $new_jo->addMeta('reference', $reference); + $new_jo->addMeta('odometer', $odometer); + $new_jo->addMeta('replaced_by', $replaced_by); + $new_jo->addMeta('satisfaction', $satisfaction); + + // date_create + $created_date = DateTime::createFromFormat('d-m-Y H:i', $date_create); + $new_jo->setDateCreate($created_date); + + // insurer == responsible_party + $new_jo->setResponsibleParty($insurer); + + // delivery address = location + state + $delivery_address = $location . ', ' . $state; + $new_jo->setDeliveryAddress($delivery_address); + + // remarks == tier 1 notes + $new_jo->setTier1Notes($remark); + + // service_needed = service type + // check service needed: + // Battery == Battery Sales + // Battery Warranty Claim == Warranty Claim + // Battery Warranty Replacement == Warranty Replacement + $service = $this->normalizeName($service_needed); + switch ($service) + { + case 'battery': + $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_NEW); + break; + case 'battery warranty claim': + $new_jo->setServiceType(CMBServiceType::WARRANTY_CLAIM); + break; + case 'battery warranty replacement': + $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_WARRANTY); + break; + } + + // status set everything to fulfilled + // store old status to metadata + $new_jo->setStatus(JOStatus::FULFILLED); + $new_jo->addMeta('status', $status); + + // plate number == vehicle_number. Use as key to cv_hash + $clean_plate = $this->cleanPlateNumber($vehicle_number); + //error_log($clean_plate . ' ' . $new_jo->getServiceType()); + + // check if plate number has been added + if (!isset($this->cv_hash[$clean_plate])) + { + $cust = $this->addCustomer($customer_name, $customer_mobile); + $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); + } + else + { + // get customer vehicle from hash + $cv = $this->cv_hash[$clean_plate]; + $cust = $cv->getCustomer(); + } + + $new_jo->setCustomer($cust) + ->setCustomerVehicle($cv); + + // create the invoice + + $this->em->persist($new_jo); + + $this->em->detach($row[0]); + } + + $this->em->flush(); + $this->em->clear(); + + return 0; + } + + protected function processVehicleInfo($car_model) + { + // vehicle manufacturer is the first entry + // vehicle model is the 2nd entry + whatever follows + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + //error_log($v_manufacturer . ' ' . $v_model); + + // check if manufacturer is in hash + if (!isset($this->vmanu_hash[$v_manufacturer])) + { + //error_log($v_manufacturer . ' invalid.'); + return 'Vehicle manufacturer not in system.'; + } + + // check if manufacturer and make is in hash + if (!isset($this->vmake_hash[$v_manufacturer][$v_model])) + { + //error_log($v_model . ' invalid.'); + return 'Vehicle model not in system.'; + } + // car model info valid + return null; + } + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $this->normalizeName($batt_manu->getName()); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $this->normalizeName($batt_model->getName()); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $this->normalizeName($batt_size->getName()); + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function loadBatteries() + { + $this->batt_hash = []; + + $batts = $this->em->getRepository(Battery::class)->findAll(); + foreach ($batts as $batt) + { + $brand = $this->normalizeName($batt->getManufacturer()->getName()); + $model = $this->normalizeName($batt->getModel()->getName()); + $size = $this->normalizeName($batt->getSize()->getName()); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + + protected function loadVehicleManufacturers() + { + $this->vmanu_hash = []; + + $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + foreach ($vmanus as $vmanu) + { + $name = $vmanu->getName(); + $this->vmanu_hash[$name] = $vmanu; + } + } + + protected function loadVehicleMakes() + { + $this->vmake_hash = []; + + $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); + foreach ($vmakes as $vmake) + { + $manufacturer = $vmake->getManufacturer()->getName(); + $make = $vmake->getMake(); + + $this->vmake_hash[$manufacturer][$make] = $vmake; + } + } + + protected function normalizeName($name) + { + $normalized_key = trim(strtolower($name)); + + return $normalized_key; + } + + protected function cleanPlateNumber($plate_number) + { + // remove spaces and make upper case + $clean_plate_number = strtoupper(str_replace(' ' , '', $plate_number)); + + return $clean_plate_number; + } + +} diff --git a/src/Entity/CMBLegacyJobOrder.php b/src/Entity/CMBLegacyJobOrder.php index 603706be..38aea6a0 100644 --- a/src/Entity/CMBLegacyJobOrder.php +++ b/src/Entity/CMBLegacyJobOrder.php @@ -41,7 +41,7 @@ class CMBLegacyJobOrder return $this; } - public function getData($id) + public function getDataById($id) { // return null if we don't have it if (!isset($this->data[$id])) @@ -49,4 +49,9 @@ class CMBLegacyJobOrder return $this->data[$id]; } + + public function getData() + { + return $this->data; + } } From 832d96c171a3ded7cdcc0f6739b37087abe690d8 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 25 Aug 2020 10:06:24 +0000 Subject: [PATCH 05/12] Add saving of customer and customer vehicle from legacy job order data. #460 --- .../ImportCMBLegacyJobOrderCommand.php | 4 +- .../ImportCMBVehicleCompatibilityCommand.php | 4 +- .../MigrateCMBLegacyJobOrderCommand.php | 213 ++++++++++++++++-- 3 files changed, 197 insertions(+), 24 deletions(-) diff --git a/src/Command/ImportCMBLegacyJobOrderCommand.php b/src/Command/ImportCMBLegacyJobOrderCommand.php index be25c4b3..6ee7022a 100644 --- a/src/Command/ImportCMBLegacyJobOrderCommand.php +++ b/src/Command/ImportCMBLegacyJobOrderCommand.php @@ -102,8 +102,8 @@ class ImportCMBLegacyJobOrderCommand extends Command $customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]); $reference = trim($fields[self::F_OUR_REFERENCE]); $odometer = trim($fields[self::F_ODOMETER]); - $batt_model = trim(strtolower($fields[self::F_BATT_MODEL])); - $batt_size = trim(strtolower($fields[self::F_BATT_SIZE])); + $batt_model = trim($fields[self::F_BATT_MODEL]); + $batt_size = trim($fields[self::F_BATT_SIZE]); $trade_in = trim($fields[self::F_BATT_TRADE_IN]); $replaced_by = trim($fields[self::F_REPLACED_BY]); $remark = trim($fields[self::F_REMARK]); diff --git a/src/Command/ImportCMBVehicleCompatibilityCommand.php b/src/Command/ImportCMBVehicleCompatibilityCommand.php index 47393c01..8add2968 100644 --- a/src/Command/ImportCMBVehicleCompatibilityCommand.php +++ b/src/Command/ImportCMBVehicleCompatibilityCommand.php @@ -412,7 +412,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); foreach ($vmanus as $vmanu) { - $name = $vmanu->getName(); + $name = $this->normalizeName($vmanu->getName()); $this->vmanu_hash[$name] = $vmanu; } } @@ -425,7 +425,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command foreach ($vmakes as $vmake) { $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); + $make = $this->normalizeName($vmake->getMake()); $this->vmake_hash[$manufacturer][$make] = $vmake; } diff --git a/src/Command/MigrateCMBLegacyJobOrderCommand.php b/src/Command/MigrateCMBLegacyJobOrderCommand.php index 4be27570..72a114c4 100644 --- a/src/Command/MigrateCMBLegacyJobOrderCommand.php +++ b/src/Command/MigrateCMBLegacyJobOrderCommand.php @@ -22,6 +22,13 @@ use App\Entity\CustomerVehicle; use App\Entity\Invoice; use App\Entity\InvoiceItem; +use App\Ramcar\CMBServiceType; +use App\Ramcar\JOStatus; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; + +use DateTime; + class MigrateCMBLegacyJobOrderCommand extends Command { protected $em; @@ -50,6 +57,9 @@ class MigrateCMBLegacyJobOrderCommand extends Command $this->loadVehicleManufacturers(); $this->loadVehicleMakes(); + // load existing customer vehicle data + $this->loadCustomerVehicles(); + parent::__construct(); } @@ -74,11 +84,11 @@ class MigrateCMBLegacyJobOrderCommand extends Command // get the entry information $entry_num = $jo_entry['entry_num']; - $date_created = $jo_entry['created_date']; + $date_create = $jo_entry['created_date']; $case_number = $jo_entry['case_number']; $insurer = $jo_entry['insurer']; $plate_number = $this->cleanPlateNumber($jo_entry['vehicle_number']); - $car_model = $jo_entry['car_model']; + $car_model = $this->normalizeName($jo_entry['car_model']); $nature_of_call = $jo_entry['nature_of_call']; $service_needed = $jo_entry['service_needed']; $location = $jo_entry['location']; @@ -91,8 +101,8 @@ class MigrateCMBLegacyJobOrderCommand extends Command $customer_mobile = $jo_entry['customer_phone_number']; $reference = $jo_entry['reference']; $odometer = $jo_entry['odometer']; - $batt_model = $jo_entry['batt_model']; - $batt_size = $jo_entry['batt_size']; + $batt_model = $this->normalizeName($jo_entry['batt_model']); + $batt_size = $this->normalizeName($jo_entry['batt_size']); $batt_trade_in = $jo_entry['batt_trade_in']; $replaced_by = $jo_entry['replaced_by']; $remark = $jo_entry['remark']; @@ -103,10 +113,10 @@ class MigrateCMBLegacyJobOrderCommand extends Command if ($v_status != null) { error_log($v_status . ' ' . $car_model); - $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, - $trade_in, $replaced_by, $remark, $satisfaction, $v_status); + $batt_trade_in, $replaced_by, $remark, $satisfaction, $v_status); // move to next entry continue; @@ -116,11 +126,11 @@ class MigrateCMBLegacyJobOrderCommand extends Command $batt_status = $this->processBatteryInfo($batt_model, $batt_size); if ($batt_status != null) { - error_log($batt_status); - $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + error_log($batt_status . ' ' . $batt_model . ' ' . $batt_size); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, - $trade_in, $replaced_by, $remark, $satisfaction, $batt_status); + $batt_trade_in, $replaced_by, $remark, $satisfaction, $batt_status); // move to next entry continue; @@ -178,19 +188,16 @@ class MigrateCMBLegacyJobOrderCommand extends Command $new_jo->addMeta('status', $status); // plate number == vehicle_number. Use as key to cv_hash - $clean_plate = $this->cleanPlateNumber($vehicle_number); - //error_log($clean_plate . ' ' . $new_jo->getServiceType()); - // check if plate number has been added - if (!isset($this->cv_hash[$clean_plate])) + if (!isset($this->cv_hash[$plate_number])) { $cust = $this->addCustomer($customer_name, $customer_mobile); - $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); + $cv = $this->addCustomerVehicle($plate_number, $car_model, $cust); } else { // get customer vehicle from hash - $cv = $this->cv_hash[$clean_plate]; + $cv = $this->cv_hash[$plate_number]; $cust = $cv->getCustomer(); } @@ -198,10 +205,37 @@ class MigrateCMBLegacyJobOrderCommand extends Command ->setCustomerVehicle($cv); // create the invoice + $invoice_item = new InvoiceItem(); + $invoice = new Invoice(); - $this->em->persist($new_jo); + // get the battery + // cannot get the battery from the hash since we have no manufacturer data + // have to find the battery using model and size + $b_model = $this->bmodel_hash[$batt_model]; + $b_size = $this->bsize_hash[$batt_size]; - $this->em->detach($row[0]); + $battery = $this->findBattery($b_model, $b_size); + if ($battery != null) + { + // assume quantity of 1 + $invoice_item->setBattery($battery) + ->setPrice($battery->getSellingPrice()) + ->setQuantity(1) + ->setInvoice($invoice); + } + + // TODO: invoice need to check if entry was cancelled or fulfilled + // if fulfilled, set date_paid else set date_cancel + // check if trade_in + // set InvoiceStatus to fulfilled if fulfilled, cancelled if cancelled + // compute totals + + $invoice->setJobOrder($new_jo); + + + //$this->em->persist($new_jo); + + $this->em->detach($row[0]); } $this->em->flush(); @@ -247,6 +281,132 @@ class MigrateCMBLegacyJobOrderCommand extends Command return null; } + protected function processBatteryInfo($batt_model, $batt_size) + { + // check if battery model is in hash + if (!isset($this->bmodel_hash[$batt_model])) + return 'Battery model not in system.'; + + // check if battery size is in hash + if (!isset($this->bsize_hash[$batt_size])) + return 'Battery size not in system.'; + + // battery info valid + return null; + } + + protected function addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $batt_trade_in, $replaced_by, $remark, $satisfaction, $v_status) + { + $inv_entry = [ + 'number' => $entry_num, + 'created_date' => $date_create, + 'case_number' => $case_number, + 'insurer' => $insurer, + 'vehicle_number' => $plate_number, + 'car_model' => $car_model, + 'nature_of_call' => $nature_of_call, + 'service_needed' => $service_needed, + 'location' => $location, + 'state' => $state, + 'driver' => $driver, + 'truck' => $truck, + 'workshop_arrival_time' => $workshop_arrival_time, + 'status' => $status, + 'customer_name' => $customer_name, + 'customer_phone_number' => $customer_mobile, + 'reference' => $reference, + 'odometer' => $odometer, + 'batt_model' => $batt_model, + 'batt_size' => $batt_size, + 'batt_trade_in' => $batt_trade_in, + 'replaced_by' => $replaced_by, + 'remark' => $remark, + 'satisfaction' => $satisfaction, + 'reason' => $v_status, + ]; + + return $inv_entry; + } + + protected function addCustomer($name, $mobile) + { + $new_cust = new Customer(); + + $new_cust->setFirstName($name) + ->setLastName('') + ->setPhoneMobile($mobile); + + $this->em->persist($new_cust); + + return $new_cust; + } + + protected function addCustomerVehicle($plate_num, $car_model, $customer) + { + $new_cv = new CustomerVehicle(); + + // get vehicle from hash + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + + $vehicle = $this->vmake_hash[$v_manufacturer][$v_model]; + + $new_cv->setCustomer($customer) + ->setPlateNumber($plate_num) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') + ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $this->em->persist($new_cv); + + // add customer vehicle to cv_hash + $this->cv_hash[$plate_num] = $new_cv; + + return $new_cv; + } + + protected function findBattery($batt_model, $batt_size) + { + $battery = null; + $b_query = $this->em->createQuery('SELECT battery from App\Entity\Battery battery + INNER JOIN battery.model bm + INNER JOIN battery.size bs + WHERE battery.model = :bmodel + AND battery.size = :bsize'); + $b_query->setParameter('bmodel', $batt_model) + ->setParameter('bsize', $batt_size); + + $b_results = $b_query->iterate(); + + foreach ($b_results as $b_result) + { + $battery = $b_result[0]; + //error_log($battery->getID() . ' ' . $battery->getProductCode()); + + $this->em->detach($b_result[0]); + } + + return $battery; + } + protected function loadBatteryManufacturers() { $this->bmanu_hash = []; @@ -305,7 +465,7 @@ class MigrateCMBLegacyJobOrderCommand extends Command $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); foreach ($vmanus as $vmanu) { - $name = $vmanu->getName(); + $name = $this->normalizeName($vmanu->getName()); $this->vmanu_hash[$name] = $vmanu; } } @@ -317,13 +477,26 @@ class MigrateCMBLegacyJobOrderCommand extends Command $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); foreach ($vmakes as $vmake) { - $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); + $manufacturer = $this->normalizeName($vmake->getManufacturer()->getName()); + $make = $this->normalizeName($vmake->getMake()); $this->vmake_hash[$manufacturer][$make] = $vmake; } } + protected function loadCustomerVehicles() + { + $this->cv_hash = []; + + $cvs = $this->em->getRepository(CustomerVehicle::class)->findAll(); + foreach ($cvs as $cv) + { + $plate_number = $this->cleanPlateNumber($cv->getPlateNumber()); + + $this->cv_hash[$plate_number] = $cv; + } + } + protected function normalizeName($name) { $normalized_key = trim(strtolower($name)); From 4e9c503eb201235168c3ef5feab8976592748809 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 1 Sep 2020 09:59:49 +0000 Subject: [PATCH 06/12] Add command to migrate the imported CarFix data into CMBLegacyJobOrder. #460 --- .../ImportCMBLegacyJobOrderCommand.php | 4 +- .../MigrateCMBLegacyJobOrderCommand.php | 254 ++++++------ src/Entity/CMBLegacyJobOrder.php | 376 +++++++++++++++++- src/Entity/CMBLegacyJobOrderRow.php | 57 +++ src/Entity/Customer.php | 4 +- 5 files changed, 560 insertions(+), 135 deletions(-) create mode 100644 src/Entity/CMBLegacyJobOrderRow.php diff --git a/src/Command/ImportCMBLegacyJobOrderCommand.php b/src/Command/ImportCMBLegacyJobOrderCommand.php index 6ee7022a..e6726f25 100644 --- a/src/Command/ImportCMBLegacyJobOrderCommand.php +++ b/src/Command/ImportCMBLegacyJobOrderCommand.php @@ -9,7 +9,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\ORM\EntityManagerInterface; -use App\Entity\CMBLegacyJobOrder; +use App\Entity\CMBLegacyJobOrderRow; class ImportCMBLegacyJobOrderCommand extends Command { @@ -114,7 +114,7 @@ class ImportCMBLegacyJobOrderCommand extends Command $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, $trade_in, $replaced_by, $remark, $satisfaction); - $legacy_data = new CMBLegacyJobOrder(); + $legacy_data = new CMBLegacyJobOrderRow(); $legacy_data->setData($data_entry); $this->em->persist($legacy_data); diff --git a/src/Command/MigrateCMBLegacyJobOrderCommand.php b/src/Command/MigrateCMBLegacyJobOrderCommand.php index 72a114c4..3b09b6e8 100644 --- a/src/Command/MigrateCMBLegacyJobOrderCommand.php +++ b/src/Command/MigrateCMBLegacyJobOrderCommand.php @@ -9,8 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\ORM\EntityManagerInterface; +use App\Entity\CMBLegacyJobOrderRow; use App\Entity\CMBLegacyJobOrder; -use App\Entity\JobOrder; use App\Entity\VehicleManufacturer; use App\Entity\Vehicle; use App\Entity\BatteryManufacturer; @@ -19,8 +19,6 @@ use App\Entity\BatterySize; use App\Entity\Battery; use App\Entity\Customer; use App\Entity\CustomerVehicle; -use App\Entity\Invoice; -use App\Entity\InvoiceItem; use App\Ramcar\CMBServiceType; use App\Ramcar\JOStatus; @@ -31,6 +29,32 @@ use DateTime; class MigrateCMBLegacyJobOrderCommand extends Command { + /* + id = 'entry_num' + trans_date = 'created_date' + case_number = 'case_number' + insurer = 'insurer' + plate_number = 'vehicle_number' + car_brand and car_make = split the 'car_model' + nature_of_call = nature_of_call + trans_type = 'service_needed' + address = 'location' + address = 'state' + driver = 'driver' + truck = 'truck' + workshop_arrival_time => 'workshop_arrival_time' + status = 'status' + cust_name = 'customer_name' + cust_mobile = 'customer_phone_number' + reference = 'reference' + odometer = 'odometer' + batt_model = 'batt_model' + batt_size = 'batt_size' + is_trade_in = 'batt_trade_in' + replaced_by = 'replaced_by' + remarks = 'remark' + satisfaction => 'satisfaction' + */ protected $em; protected $bmanu_hash; @@ -67,21 +91,39 @@ class MigrateCMBLegacyJobOrderCommand extends Command { $this->setName('cmblegacyjoborderdata:migrate') ->setDescription('Retrieve database cmb legacy job orders and insert into job order.') - ->setHelp('Creates job orders based on data from imported CSV.'); + ->setHelp('Creates job orders based on data from imported CSV.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Path to the output CSV file of the entries not added.'); } public function execute(InputInterface $input, OutputInterface $output) { + $csv_file = $input->getArgument('output_file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be opened.'); + } + // get all legacy job orders - $query = $this->em->createQuery('SELECT legacy_jo FROM App\Entity\CMBLegacyJobOrder legacy_jo'); + $query = $this->em->createQuery('SELECT legacy_jo_row FROM App\Entity\CMBLegacyJobOrderRow legacy_jo_row'); $result = $query->iterate(); $invalid_entries = []; + $added_entries = 0; + $total_entries = 0; + $total_inv_entries = 0; foreach ($result as $row) { $entry = $row[0]; $jo_entry = $entry->getData(); + $total_entries++; + // get the entry information $entry_num = $jo_entry['entry_num']; $date_create = $jo_entry['created_date']; @@ -112,12 +154,13 @@ class MigrateCMBLegacyJobOrderCommand extends Command $v_status = $this->processVehicleInfo($car_model); if ($v_status != null) { - error_log($v_status . ' ' . $car_model); + //error_log($v_status . ' ' . $car_model); $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, $batt_trade_in, $replaced_by, $remark, $satisfaction, $v_status); + $total_inv_entries++; // move to next entry continue; } @@ -126,66 +169,70 @@ class MigrateCMBLegacyJobOrderCommand extends Command $batt_status = $this->processBatteryInfo($batt_model, $batt_size); if ($batt_status != null) { - error_log($batt_status . ' ' . $batt_model . ' ' . $batt_size); + //error_log($batt_status . ' ' . $batt_model . ' ' . $batt_size); $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, $batt_trade_in, $replaced_by, $remark, $satisfaction, $batt_status); + $total_inv_entries++; // move to next entry continue; } // create job order - $new_jo = new JobOrder(); + $cmb_legacy_jo = new CMBLegacyJobOrder(); - // add to metadata - // case number, nature of call, workshop arrival time, reference, odometer, replaced by, satisfaction - $new_jo->addMeta('case_number', $case_number); - $new_jo->addMeta('nature_of_call', $nature_of_call); - $new_jo->addMeta('workshop_arrival_time', $workshop_arrival_time); - $new_jo->addMeta('reference', $reference); - $new_jo->addMeta('odometer', $odometer); - $new_jo->addMeta('replaced_by', $replaced_by); - $new_jo->addMeta('satisfaction', $satisfaction); + // date_create + $created_date = DateTime::createFromFormat("d-m-Y H:i", $date_create); + $cmb_legacy_jo->setTransDate($created_date); - // date_create - $created_date = DateTime::createFromFormat('d-m-Y H:i', $date_create); - $new_jo->setDateCreate($created_date); + // parse vehicle info + // get vehicle from hash + $v_array = explode(' ', $car_model); - // insurer == responsible_party - $new_jo->setResponsibleParty($insurer); + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); // delivery address = location + state $delivery_address = $location . ', ' . $state; - $new_jo->setDeliveryAddress($delivery_address); - // remarks == tier 1 notes - $new_jo->setTier1Notes($remark); + // check if trade in + $trade_in = false; + if (strtolower($batt_trade_in) == 'yes') + $trade_in = true; - // service_needed = service type - // check service needed: - // Battery == Battery Sales - // Battery Warranty Claim == Warranty Claim - // Battery Warranty Replacement == Warranty Replacement - $service = $this->normalizeName($service_needed); - switch ($service) - { - case 'battery': - $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_NEW); - break; - case 'battery warranty claim': - $new_jo->setServiceType(CMBServiceType::WARRANTY_CLAIM); - break; - case 'battery warranty replacement': - $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_WARRANTY); - break; - } - - // status set everything to fulfilled - // store old status to metadata - $new_jo->setStatus(JOStatus::FULFILLED); - $new_jo->addMeta('status', $status); + $cmb_legacy_jo->setCaseNumber($case_number) + ->setInsurer($insurer) + ->setNatureOfCall($nature_of_call) + ->setTransType($service_needed) + ->setCarBrand(strtoupper($v_manufacturer)) + ->setCarMake(strtoupper($v_model)) + ->setCustName($customer_name) + ->setCustMobile($customer_mobile) + ->setAddress($delivery_address) + ->setDriver($driver) + ->setTruck($truck) + ->setWorkshopArrivalTime($workshop_arrival_time) + ->setPlateNumber($plate_number) + ->setStatus($status) + ->setReference($reference) + ->setOdometer($odometer) + ->setBatteryModel(strtoupper($batt_model)) + ->setBatterySize(strtoupper($batt_size)) + ->setIsTradeIn($trade_in) + ->setReplacedBy($replaced_by) + ->setSatisfaction($satisfaction); // plate number == vehicle_number. Use as key to cv_hash // check if plate number has been added @@ -194,53 +241,60 @@ class MigrateCMBLegacyJobOrderCommand extends Command $cust = $this->addCustomer($customer_name, $customer_mobile); $cv = $this->addCustomerVehicle($plate_number, $car_model, $cust); } - else - { - // get customer vehicle from hash - $cv = $this->cv_hash[$plate_number]; - $cust = $cv->getCustomer(); - } - - $new_jo->setCustomer($cust) - ->setCustomerVehicle($cv); - - // create the invoice - $invoice_item = new InvoiceItem(); - $invoice = new Invoice(); - - // get the battery - // cannot get the battery from the hash since we have no manufacturer data - // have to find the battery using model and size - $b_model = $this->bmodel_hash[$batt_model]; - $b_size = $this->bsize_hash[$batt_size]; - - $battery = $this->findBattery($b_model, $b_size); - if ($battery != null) - { - // assume quantity of 1 - $invoice_item->setBattery($battery) - ->setPrice($battery->getSellingPrice()) - ->setQuantity(1) - ->setInvoice($invoice); - } - - // TODO: invoice need to check if entry was cancelled or fulfilled - // if fulfilled, set date_paid else set date_cancel - // check if trade_in - // set InvoiceStatus to fulfilled if fulfilled, cancelled if cancelled - // compute totals - - $invoice->setJobOrder($new_jo); - - - //$this->em->persist($new_jo); + $this->em->persist($cmb_legacy_jo); $this->em->detach($row[0]); + + $added_entries++; } $this->em->flush(); $this->em->clear(); + // output the entries that were not added + if (count($invalid_entries) > 0) + { + fputcsv($fh, [ + 'Entry Num', + 'Created Date', + 'Case Number', + 'Insurer', + 'Vehicle Number', + 'Car Model', + 'Nature of Call', + 'Service Needed', + 'Location', + 'State', + 'Driver', + 'Truck', + 'Workshop Arrival Time', + 'Status', + 'Customer Name', + 'Customer Phone Number', + 'Reference', + 'Odometer', + 'Batt Model', + 'Batt Size', + 'Batt Trade In', + 'Replaced By', + 'Remark', + 'Satisfaction', + 'Reason', + + ]); + + foreach($invalid_entries as $row) + { + fputcsv($fh, $row); + } + } + + fclose($fh); + + error_log('Processed ' . $total_entries . ' entries.'); + error_log('Added ' . $added_entries . ' entries.'); + error_log('Did not add ' . $total_inv_entries . ' entries.'); + return 0; } @@ -383,30 +437,6 @@ class MigrateCMBLegacyJobOrderCommand extends Command return $new_cv; } - protected function findBattery($batt_model, $batt_size) - { - $battery = null; - $b_query = $this->em->createQuery('SELECT battery from App\Entity\Battery battery - INNER JOIN battery.model bm - INNER JOIN battery.size bs - WHERE battery.model = :bmodel - AND battery.size = :bsize'); - $b_query->setParameter('bmodel', $batt_model) - ->setParameter('bsize', $batt_size); - - $b_results = $b_query->iterate(); - - foreach ($b_results as $b_result) - { - $battery = $b_result[0]; - //error_log($battery->getID() . ' ' . $battery->getProductCode()); - - $this->em->detach($b_result[0]); - } - - return $battery; - } - protected function loadBatteryManufacturers() { $this->bmanu_hash = []; diff --git a/src/Entity/CMBLegacyJobOrder.php b/src/Entity/CMBLegacyJobOrder.php index 38aea6a0..6f9c0826 100644 --- a/src/Entity/CMBLegacyJobOrder.php +++ b/src/Entity/CMBLegacyJobOrder.php @@ -3,14 +3,15 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; +use DateTime; /** * @ORM\Entity - * @ORM\Table(name="legacy_cmb_job_order") + * @ORM\Table(name="cmb_legacy_job_order") */ class CMBLegacyJobOrder { - // unique id + // legacy internal id /** * @ORM\Id * @ORM\Column(type="integer") @@ -18,40 +19,377 @@ class CMBLegacyJobOrder */ protected $id; - // data from csv file /** - * @ORM\Column(type="json") + * @ORM\Column(type="datetime") */ - protected $data; + protected $trans_date; + + /** + * @ORM\Column(type="string", length=30, nullable=true) + */ + protected $case_number; + + /** + * @ORM\Column(type="string", length=30, nullable=true) + */ + protected $insurer; + + /** + * @ORM\Column(type="string", length=20, nullable=true) + */ + protected $plate_number; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $trans_type; + + /** + * @ORM\Column(type="string", length=20, nullable=true) + */ + protected $car_brand; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $car_make; + + /** + * @ORM\Column(type="string", length=30, nullable=true) + */ + protected $nature_of_call; + + /** + * @ORM\Column(type="string", length=400, nullable=true) + */ + protected $address; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $driver; + + /** + * @ORM\Column(type="string", length=20, nullable=true) + */ + protected $truck; + + /** + * @ORM\Column(type="integer", nullable=true) + */ + protected $workshop_arrival_time; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $status; + + /** + * @ORM\Column(type="string", length=100, nullable=true) + */ + protected $cust_name; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $cust_mobile; + +/** + * @ORM\Column(type="string", length=20, nullable=true) + */ + protected $reference; + + /** + * @ORM\Column(type="integer", nullable=true) + */ + protected $odometer; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $batt_model; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $batt_size; + + /** + * @ORM\Column(type="boolean", options={"default":false}) + */ + protected $flag_trade_in; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $replaced_by; + + /** + * @ORM\Column(type="string", length=4000, nullable=true) + */ + protected $remarks; + + /** + * @ORM\Column(type="string", length=50, nullable=true) + */ + protected $satisfaction; public function __construct() { - $this->data = []; + $this->trans_date = new DateTime(); } - public function addData($id, $value) + public function setID($id) { - $this->data[$id] = $value; + $this->id = $id; return $this; } - public function setData(array $data_array) + public function getID() { - $this->data = $data_array; + return $this->id; + } + + public function setTransDate(DateTime $trans_date) + { + $this->trans_date = $trans_date; return $this; } - public function getDataById($id) + public function getTransDate() { - // return null if we don't have it - if (!isset($this->data[$id])) - return null; - - return $this->data[$id]; + return $this->trans_date; } - public function getData() + public function setTransType($trans_type) { - return $this->data; - } + $this->trans_type = $trans_type; + return $this; + } + + public function getTransType() + { + return $this->trans_type; + } + + public function setCaseNumber($case_number) + { + $this->case_number = $case_number; + return $this; + } + + public function getCaseNumber() + { + return $this->case_number; + } + + public function setInsurer($insurer) + { + $this->insurer = $insurer; + return $this; + } + + public function getInsurer() + { + return $this->insurer; + } + + public function setNatureOfCall($nature_of_call) + { + $this->nature_of_call = $nature_of_call; + return $this; + } + + public function getNatureOfCall() + { + return $this->nature_of_call; + } + + public function setPlateNumber($plate_number) + { + $this->plate_number = $plate_number; + return $this; + } + + public function getPlateNumber() + { + return $this->plate_number; + } + + public function setCarBrand($car_brand) + { + $this->car_brand = $car_brand; + return $this; + } + + public function getCarBrand() + { + return $this->car_brand; + } + + public function setCarMake($car_make) + { + $this->car_make = $car_make; + return $this; + } + + public function getCarMake() + { + return $this->car_make; + } + + public function setAddress($address) + { + $this->address = $address; + return $this; + } + + public function getAddress() + { + return $this->address; + } + + public function setDriver($driver) + { + $this->driver = $driver; + return $this; + } + + public function getDriver() + { + return $this->driver; + } + + public function setTruck($truck) + { + $this->truck = $truck; + return $this; + } + + public function getTruck() + { + return $this->truck; + } + + public function setWorkshopArrivalTime($workshop_arrival_time) + { + $this->workshop_arrival_time = $workshop_arrival_time; + return $this; + } + + public function getWorkshopArrivalTime() + { + return $this->workshop_arrival_time; + } + + public function setStatus($status) + { + $this->status = $status; + return $this; + } + + public function getStatus() + { + return $this->status; + } + + public function setCustName($cust_name) + { + $this->cust_name = $cust_name; + return $this; + } + + public function getCustName() + { + return $this->cust_name; + } + + public function setCustMobile($cust_mobile) + { + $this->cust_mobile = $cust_mobile; + return $this; + } + + public function getCustMobile() + { + return $this->cust_mobile; + } + + public function setReference($reference) + { + $this->reference = $reference; + return $this; + } + + public function getReference() + { + return $this->reference; + } + + public function setOdometer($odometer) + { + $this->odometer = $odometer; + return $this; + } + + public function getOdometer() + { + return $this->odometer; + } + + public function setBatteryModel($batt_model) + { + $this->batt_model = $batt_model; + return $this; + } + + public function getBatteryModel() + { + return $this->batt_model; + } + + public function setBatterySize($batt_size) + { + $this->batt_size = $batt_size; + return $this; + } + + public function getBatterySize() + { + return $this->batt_size; + } + + public function setIsTradeIn($flag_trade_in = true) + { + $this->flag_trade_in = $flag_trade_in; + return $this; + } + + public function isTradeIn() + { + return $this->flag_trade_in; + } + + public function setReplacedBy($replaced_by) + { + $this->replaced_by = $replaced_by; + return $this; + } + + public function getReplacedBy() + { + return $this->replaced_by; + } + + public function setSatisfaction($satisfaction) + { + $this->satisfaction = $satisfaction; + return $this; + } + + public function getSatisfaction() + { + return $this->satisfaction; + } + } diff --git a/src/Entity/CMBLegacyJobOrderRow.php b/src/Entity/CMBLegacyJobOrderRow.php new file mode 100644 index 00000000..0a6fdc35 --- /dev/null +++ b/src/Entity/CMBLegacyJobOrderRow.php @@ -0,0 +1,57 @@ +data = []; + } + + public function addData($id, $value) + { + $this->data[$id] = $value; + return $this; + } + + public function setData(array $data_array) + { + $this->data = $data_array; + return $this; + } + + public function getDataById($id) + { + // return null if we don't have it + if (!isset($this->data[$id])) + return null; + + return $this->data[$id]; + } + + public function getData() + { + return $this->data; + } +} diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index eae98fc3..c0016ec5 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -34,7 +34,7 @@ class Customer // first name /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=100) * @Assert\NotBlank() */ protected $first_name; @@ -65,7 +65,7 @@ class Customer // mobile phone /** - * @ORM\Column(type="string", length=30) + * @ORM\Column(type="string", length=50) */ protected $phone_mobile; From d0fcbe8cf311a4c3e15328e8685637f82fac295c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 02:46:58 +0000 Subject: [PATCH 07/12] Removed old import carfix data command. #460 --- src/Command/ImportCMBCarFixDataCommand.php | 489 --------------------- 1 file changed, 489 deletions(-) delete mode 100644 src/Command/ImportCMBCarFixDataCommand.php diff --git a/src/Command/ImportCMBCarFixDataCommand.php b/src/Command/ImportCMBCarFixDataCommand.php deleted file mode 100644 index ff7f3b23..00000000 --- a/src/Command/ImportCMBCarFixDataCommand.php +++ /dev/null @@ -1,489 +0,0 @@ -em = $em; - - // load existing battery data - $this->loadBatteryManufacturers(); - $this->loadBatteryModels(); - $this->loadBatterySizes(); - $this->loadBatteries(); - - // load existing vehicle data - $this->loadVehicleManufacturers(); - $this->loadVehicleMakes(); - - parent::__construct(); - } - - public function configure() - { - $this->setName('cmbcarfixdata:import') - ->setDescription('Retrieve from a CSV file CarFix data.') - ->setHelp('Creates job orders based on data from imported CSV.') - ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); - } - - public function execute(InputInterface $input, OutputInterface $output) - { - $csv_file = $input->getArgument('file'); - - // attempt to open file - try - { - $fh = fopen($csv_file, "r"); - } - catch (Exception $e) - { - throw new Exception('The file "' . $csv_file . '" could be read.'); - } - - // get entity manager - $em = $this->em; - - $row_num = 0; - $invalid_entries = []; - error_log('Processing CarFix data csv file...'); - while (($fields = fgetcsv($fh)) !== false) - { - if ($row_num < 1) - { - $row_num++; - continue; - } - - // get the information - $entry_num = trim($fields[self::F_INDEX]); - $date_create = trim($fields[self::F_CREATED_DATE]); - $case_number = trim($fields[self::F_CASE_NO]); - $insurer = trim($fields[self::F_INSURER]); - $vehicle_number = trim($fields[self::F_VEHICLE_NO]); - $car_model = trim($fields[self::F_CAR_MODEL]); - $nature_of_call = trim($fields[self::F_NATURE_OF_CALL]); - $service_needed = trim($fields[self::F_SERVICE_NEEDED]); - $location = trim($fields[self::F_LOCATION]); - $state = trim($fields[self::F_STATE]); - $driver = trim($fields[self::F_DRIVER]); - $truck = trim($fields[self::F_TRUCK]); - $workshop_arrival_time = trim($fields[self::F_WORKSHOP_ARRIVAL_TIME]); - $status = trim($fields[self::F_STATUS]); - $customer_name = trim($fields[self::F_CUSTOMER_NAME]); - $customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]); - $reference = trim($fields[self::F_OUR_REFERENCE]); - $odometer = trim($fields[self::F_ODOMETER]); - $batt_model = trim(strtolower($fields[self::F_BATT_MODEL])); - $batt_size = trim(strtolower($fields[self::F_BATT_SIZE])); - $trade_in = trim($fields[self::F_BATT_TRADE_IN]); - $replaced_by = trim($fields[self::F_REPLACED_BY]); - $remark = trim($fields[self::F_REMARK]); - $satisfaction = trim($fields[self::F_SATISFACTION]); - - //error_log($date_create . ' ' . $case_number . ' ' . $driver . ' ' . $customer_name . ' ' . $remark . ' ' . $satisfaction); - - // get customer vehicle - $v_status = $this->processVehicleInfo($car_model); - if ($v_status != null) - { - error_log($v_status . ' ' . $car_model); - $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, - $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, - $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, - $trade_in, $replaced_by, $remark, $satisfaction, $v_status); - - // move to next entry - continue; - } - - // check batteries - $batt_status = $this->processBatteryInfo($batt_model, $batt_size); - if ($batt_status != null) - { - error_log($batt_status); - $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, - $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, - $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, - $trade_in, $replaced_by, $remark, $satisfaction, $batt_status); - - // move to next entry - continue; - } - - $new_jo = new JobOrder(); - - // add to metadata - // case number, nature of call, workshop arrival time, reference, odometer, replaced by, satisfaction - $new_jo->addMeta('case_number', $case_number); - $new_jo->addMeta('nature_of_call', $nature_of_call); - $new_jo->addMeta('workshop_arrival_time', $workshop_arrival_time); - $new_jo->addMeta('reference', $reference); - $new_jo->addMeta('odometer', $odometer); - $new_jo->addMeta('replaced_by', $replaced_by); - $new_jo->addMeta('satisfaction', $satisfaction); - - // date_create - $created_date = DateTime::createFromFormat('d-m-Y H:i', $date_create); - $new_jo->setDateCreate($created_date); - - // insurer == responsible_party - $new_jo->setResponsibleParty($insurer); - - // delivery address = location + state - $delivery_address = $location . ', ' . $state; - $new_jo->setDeliveryAddress($delivery_address); - - // remarks == tier 1 notes - $new_jo->setTier1Notes($remark); - - // service_needed = service type - // check service needed: - // Battery == Battery Sales - // Battery Warranty Claim == Warranty Claim - // Battery Warranty Replacement == Warranty Replacement - $service = $this->normalizeName($service_needed); - switch ($service) - { - case 'battery': - $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_NEW); - break; - case 'battery warranty claim': - $new_jo->setServiceType(CMBServiceType::WARRANTY_CLAIM); - break; - case 'battery warranty replacement': - $new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_WARRANTY); - break; - } - - // status set everything to fulfilled - // store old status to metadata - $new_jo->setStatus(JOStatus::FULFILLED); - $new_jo->addMeta('status', $status); - - // plate number == vehicle_number. Use as key to cv_hash - $clean_plate = $this->cleanPlateNumber($vehicle_number); - //error_log($clean_plate . ' ' . $new_jo->getServiceType()); - - // check if plate number has been added - if (!isset($this->cv_hash[$clean_plate])) - { - $cust = $this->addCustomer($customer_name, $customer_mobile); - $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); - } - else - { - // get customer vehicle from hash - $cv = $this->cv_hash[$clean_plate]; - $cust = $cv->getCustomer(); - } - - $new_jo->setCustomer($cust) - ->setCustomerVehicle($cv); - - $row_num++; - } - - $this->em->flush(); - $this->em->clear(); - - // check for invalid entries. if any, write to csv - - return 0; - } - - protected function processVehicleInfo($car_model) - { - // vehicle manufacturer is the first entry - // vehicle model is the 2nd entry + whatever follows - $v_array = explode(' ', $car_model); - - // manufacturer - $v_manufacturer = trim($v_array[0]); - - // get model - $model_info = ''; - $v_model = ''; - for ($i = 1; $i < count($v_array); $i++) - { - $model_info = $model_info . ' ' . trim($v_array[$i]); - } - - $v_model = trim($model_info); - //error_log($v_manufacturer . ' ' . $v_model); - - // check if manufacturer is in hash - if (!isset($this->vmanu_hash[$v_manufacturer])) - { - //error_log($v_manufacturer . ' invalid.'); - return 'Vehicle manufacturer not in system.'; - } - - // check if manufacturer and make is in hash - if (!isset($this->vmake_hash[$v_manufacturer][$v_model])) - { - //error_log($v_model . ' invalid.'); - return 'Vehicle model not in system.'; - } - // car model info valid - return null; - } - - protected function processBatteryInfo($batt_model, $batt_size) - { - // check if battery model is in hash - if (!isset($this->bmodel_hash[$batt_model])) - return 'Battery model not in system.'; - - // check if battery size is in hash - if (!isset($this->bsize_hash[$batt_size])) - return 'Battery size not in system.'; - - // battery info valid - return null; - } - - protected function addCustomer($name, $mobile) - { - $new_cust = new Customer(); - - $new_cust->setFirstName($name) - ->setLastName('') - ->setPhoneMobile($mobile); - - $this->em->persist($new_cust); - - return $new_cust; - } - - protected function addCustomerVehicle($plate_num, $car_model, $customer) - { - $new_cv = new CustomerVehicle(); - - // get vehicle from hash - $v_array = explode(' ', $car_model); - - // manufacturer - $v_manufacturer = trim($v_array[0]); - - // get model - $model_info = ''; - $v_model = ''; - for ($i = 1; $i < count($v_array); $i++) - { - $model_info = $model_info . ' ' . trim($v_array[$i]); - } - - $v_model = trim($model_info); - - $vehicle = $this->vmake_hash[$v_manufacturer][$v_model]; - - $new_cv->setCustomer($customer) - ->setPlateNumber($plate_num) - ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) - ->setModelYear('') - ->setColor('') - ->setFuelType(FuelType::GAS) - ->setHasMotoliteBattery(true) - ->setVehicle($vehicle); - - $this->em->persist($new_cv); - - // add customer vehicle to cv_hash - $this->cv_hash[$plate_num] = $new_cv; - - return $new_cv; - } - - protected function addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, - $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, - $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, - $trade_in, $replaced_by, $remark, $satisfaction, $v_status) - { - $inv_entry = [ - 'number' => $entry_num, - 'created_date' => $date_create, - 'case_number' => $case_number, - 'insurer' => $insurer, - 'vehicle_number' => $vehicle_number, - 'car_model' => $car_model, - 'nature_of_call' => $nature_of_call, - 'service_needed' => $service_needed, - 'location' => $location, - 'state' => $state, - 'driver' => $driver, - 'truck' => $truck, - 'workshop_arrival_time' => $workshop_arrival_time, - 'status' => $status, - 'customer_name' => $customer_name, - 'customer_phone_number' => $customer_mobile, - 'reference' => $reference, - 'odometer' => $odometer, - 'batt_model' => $batt_model, - 'batt_size' => $batt_size, - 'batt_trade_in' => $trade_in, - 'replaced_by' => $replaced_by, - 'remark' => $remark, - 'satisfaction' => $satisfaction, - 'reason' => $v_status, - ]; - - return $inv_entry; - } - - protected function loadBatteryManufacturers() - { - $this->bmanu_hash = []; - - $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); - foreach ($batt_manufacturers as $batt_manu) - { - $name = $this->normalizeName($batt_manu->getName()); - $this->bmanu_hash[$name] = $batt_manu; - } - } - - protected function loadBatteryModels() - { - $this->bmodel_hash = []; - - $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); - foreach ($batt_models as $batt_model) - { - $name = $this->normalizeName($batt_model->getName()); - $this->bmodel_hash[$name] = $batt_model; - } - } - - protected function loadBatterySizes() - { - $this->bsize_hash = []; - - $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); - foreach ($batt_sizes as $batt_size) - { - $name = $this->normalizeName($batt_size->getName()); - $this->bsize_hash[$name] = $batt_size; - } - } - - protected function loadBatteries() - { - $this->batt_hash = []; - - $batts = $this->em->getRepository(Battery::class)->findAll(); - foreach ($batts as $batt) - { - $brand = $this->normalizeName($batt->getManufacturer()->getName()); - $model = $this->normalizeName($batt->getModel()->getName()); - $size = $this->normalizeName($batt->getSize()->getName()); - - $this->batt_hash[$brand][$model][$size] = $batt; - } - } - - protected function loadVehicleManufacturers() - { - $this->vmanu_hash = []; - - $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); - foreach ($vmanus as $vmanu) - { - $name = $vmanu->getName(); - $this->vmanu_hash[$name] = $vmanu; - } - } - - protected function loadVehicleMakes() - { - $this->vmake_hash = []; - - $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); - foreach ($vmakes as $vmake) - { - $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); - - $this->vmake_hash[$manufacturer][$make] = $vmake; - } - } - - protected function normalizeName($name) - { - $normalized_key = trim(strtolower($name)); - - return $normalized_key; - } - - protected function cleanPlateNumber($plate_number) - { - // remove spaces and make upper case - $clean_plate_number = strtoupper(str_replace(' ' , '', $plate_number)); - - return $clean_plate_number; - } -} From 9b7fa2048a410cc5370515af0808c965de078650 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 08:36:39 +0000 Subject: [PATCH 08/12] Add command to create battery manufacturers and models and import battery sizes. #460 --- .../ImportCMBBatteryModelSizeCommand.php | 392 ++++++++++++++++++ 1 file changed, 392 insertions(+) create mode 100644 src/Command/ImportCMBBatteryModelSizeCommand.php diff --git a/src/Command/ImportCMBBatteryModelSizeCommand.php b/src/Command/ImportCMBBatteryModelSizeCommand.php new file mode 100644 index 00000000..0157f55e --- /dev/null +++ b/src/Command/ImportCMBBatteryModelSizeCommand.php @@ -0,0 +1,392 @@ +em = $om; + + // load existing battery data + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + $this->loadBatteries(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('cmbbatterymodelsize:import') + ->setDescription('Retrieve from a CSV file battery information.') + ->setHelp('Creates battery manufacturers, models, sizes based on data from imported CSV.') + ->addArgument('input_file', InputArgument::REQUIRED, 'Path to the CSV file.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Path to the output CSV file for entries not added.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // check if battery manufacturers have been created + if (count($this->bmanu_hash) == 0) + { + // create the manufacturers + $this->createBatteryManufacturerData(); + + // reload the hash + $this->loadBatteryManufacturers(); + } + + // check if battery models have been created + if (count($this->bmodel_hash) == 0) + { + // create the battery models + $this->createBatteryModelData(); + + // reload the hash + $this->loadBatteryModels(); + } + + // get the sizes, vehicles, battery prices from the csv file + $csv_file = $input->getArgument('input_file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could not be read.'); + } + + $output_file = $input->getArgument('output_file'); + + // attempt to open file + try + { + $out_fh = fopen($output_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $output_file . '" could not be read.'); + } + + // get entity manager + $em = $this->em; + + $row_num = 0; + $not_added = []; + error_log('Processing battery model and size file...'); + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 2) + { + $row_num++; + continue; + } + + // get battery info from file + $marathoner_size = $this->normalizeName(trim($fields[self::F_BATT_MARATHONER_SIZE])); + $marathoner_price = trim($fields[self::F_BATT_MARATHONER_PRICE]); + $marathoner_tradein_price = trim($fields[self::F_BATT_MARATHONER_TRADEIN_PRICE]); + + $classic_size = $this->normalizeName(trim($fields[self::F_BATT_CLASSIC_SIZE])); + $classic_price = trim($fields[self::F_BATT_CLASSIC_PRICE]); + $classic_tradein_price = trim($fields[self::F_BATT_CLASSIC_TRADEIN_PRICE]); + + $excel_size = $this->normalizeName(trim($fields[self::F_BATT_EXCEL_SIZE])); + $excel_price = trim($fields[self::F_BATT_EXCEL_PRICE]); + $excel_tradein_price = trim($fields[self::F_BATT_EXCEL_TRADEIN_PRICE]); + + $sdfc_size = $this->normalizeName(trim($fields[self::F_BATT_SDFC_SIZE])); + $sdfc_price = trim($fields[self::F_BATT_SDFC_PRICE]); + $sdfc_tradein_price = trim($fields[self::F_BATT_SDFC_TRADEIN_PRICE]); + + // add the battery sizes + // check if size is empty or if price and tradein prices are N/A + if (!isset($this->bsize_hash[$marathoner_size])) + { + if ((strlen($marathoner_size) > 0) && + (strlen($marathoner_price) > 0)) + { + if ((is_numeric($marathoner_price)) && + (is_numeric($marathoner_tradein_price))) + { + $this->addBatterySize($marathoner_size, $marathoner_price, $marathoner_tradein_price); + } + else + { + $not_added[] = $this->addInvalidEntry('MARATHONER', $marathoner_size, $marathoner_price, + $marathoner_tradein_price, 'Non numeric price/tradein price.'); + } + } + else + { + $not_added[] = $this->addInvalidEntry('MARATHONER', $marathoner_size, $marathoner_price, + $marathoner_tradein_price, 'Empty size and price.'); + } + } + if (!isset($this->bsize_hash[$classic_size])) + { + if ((strlen($classic_size) > 0) && + (strlen($classic_price) > 0)) + { + if (($classic_price != 'N/A') || + ($classic_tradein_price != 'N/A')) + { + $this->addBatterySize($classic_size, $classic_price, $classic_tradein_price); + } + else + { + $not_added[] = $this->addInvalidEntry('CLASSIC', $classic_size, $classic_price, + $classic_tradein_price, 'Non numeric price/tradein price.'); + } + } + else + { + $not_added[] = $this->addInvalidEntry('CLASSIC', $classic_size, $classic_price, + $classic_tradein_price, 'Empty size and price.'); + } + } + if (!isset($this->bsize_hash[$excel_size])) + { + if ((strlen($excel_size) > 0) && + (strlen($excel_price) > 0)) + { + if (($excel_price != 'N/A') || + ($excel_tradein_price != 'N/A')) + { + $this->addBatterySize($excel_size, $excel_price, $excel_tradein_price); + } + else + { + $not_added[] = $this->addInvalidEntry('EXCEL', $excel_size, $excel_price, + $excel_tradein_price, 'Non numeric price/tradein price.'); + } + } + else + { + $not_added[] = $this->addInvalidEntry('EXCEL', $excel_size, $excel_price, + $excel_tradein_price, 'Empty size and price.'); + } + + } + if (!isset($this->bsize_hash[$sdfc_size])) + { + if ((strlen($sdfc_size) > 0) && + (strlen($sdfc_price) > 0)) + { + if (($sdfc_price != 'N/A') || + ($sdfc_tradein_price != 'N/A')) + { + $this->addBatterySize($sdfc_size, $sdfc_price, $sdfc_tradein_price); + } + else + { + $not_added[] = $this->addInvalidEntry('SDFC', $sdfc_size, $sdfc_price, + $sdfc_tradein_price, 'Non numeric price/tradein price.'); + } + } + else + { + $not_added[] = $this->addInvalidEntry('SDFC', $sdfc_size, $sdfc_price, + $sdfc_tradein_price, 'Empty size and price.'); + } + } + + $row_num++; + } + + // output the battery sizes that were not added + if (count($not_added) > 0) + { + fputcsv($out_fh, [ + 'Battery Model', + 'Battery Size', + 'Price', + 'Trade In Price', + 'Reason', + ]); + foreach($not_added as $row) + { + fputcsv($out_fh, $row); + } + } + + fclose($out_fh); + } + + protected function createBatteryManufacturerData() + { + foreach ($this->batt_manufacturers as $name) + { + $new_bmanu = new BatteryManufacturer(); + $new_bmanu->setName($name); + + $this->em->persist($new_bmanu); + } + + $this->em->flush(); + } + + protected function createBatteryModelData() + { + foreach ($this->batt_models as $name) + { + $new_bmodel = new BatteryModel(); + $new_bmodel->setName($name); + + $this->em->persist($new_bmodel); + } + + $this->em->flush(); + } + + protected function addBatterySize($size, $price, $tradein_price) + { + $new_bsize = new BatterySize(); + $new_bsize->setName(strtoupper($size)); + $new_bsize->setTIPriceMotolite($tradein_price); + + $this->em->persist($new_bsize); + + // add to hash + $this->bsize_hash[$size] = $new_bsize; + + $this->em->flush(); + } + + protected function addInvalidEntry($model, $size, $price, + $tradein_price, $reason) + { + $entry = [ + $model, + $size, + $price, + $tradein_price, + $reason, + ]; + + return $entry; + } + + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $this->normalizeName($batt_manu->getName()); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $this->normalizeName($batt_model->getName()); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $this->normalizeName($batt_size->getName()); + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function loadBatteries() + { + $this->batt_hash = []; + + $batts = $this->em->getRepository(Battery::class)->findAll(); + foreach ($batts as $batt) + { + $brand = $this->normalizeName($batt->getManufacturer()->getName()); + $model = $this->normalizeName($batt->getModel()->getName()); + $size = $this->normalizeName($batt->getSize()->getName()); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + + protected function normalizeName($name) + { + $normalized_key = trim(strtolower($name)); + + return $normalized_key; + } +} From b8885edaaa9b8a18c1d0b16caf4a510541d17947 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 09:55:45 +0000 Subject: [PATCH 09/12] Modify commands for importing battery information. #460 --- src/Command/ImportCMBBatteryDataCommand.php | 73 +++++++++++++++---- .../ImportCMBBatteryModelSizeCommand.php | 69 ++++++------------ 2 files changed, 79 insertions(+), 63 deletions(-) diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index 30a6f4a4..dd907d0e 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -70,8 +70,8 @@ class ImportCMBBatteryDataCommand extends Command error_log('Processing battery csv file...'); while (($fields = fgetcsv($fh)) !== false) { - // data starts at row 2 - if ($row_num < 2) + // data starts at row 1 + if ($row_num < 1) { $row_num++; continue; @@ -82,6 +82,10 @@ class ImportCMBBatteryDataCommand extends Command $desc = trim($fields[self::F_BATT_DESC]); $price = trim($fields[self::F_BATT_PRICE]); + error_log($code); + error_log($desc); + error_log($price); + $clean_price = trim($price, '$'); $battery_info = explode(' ', $desc); @@ -90,22 +94,42 @@ class ImportCMBBatteryDataCommand extends Command // [0] = battery manufacturer // [1] = battery model // [2] = battery size - // if only 2, get both - // [0] = battery manufacturer and battery model - // [1] = battery size // if 4, // [0] = battery manufacturer - // concatenate [1] and [2] for the battery model + // [1] = battery model + // [2] = extra info // [3] = battery size + // OR + // [0] = battery manufacturer + // [1] = battery model + // [2] = battery size + // [3] = battery size --> this one would have () + // if 5, + // [0] = battery manufacturer + // [1] = battery model + // [2] = extra info + // [3] = extra info + // [4] = battery size $battery_manufacturer = ''; $battery_model = ''; $battery_size = ''; if (count($battery_info) == 3) { // sample: Century Marathoner 120-7L + // sample: Century Marathoner M42(60B20L) $battery_manufacturer = trim($battery_info[0]); $battery_model = trim($battery_info[1]); - $battery_size = trim($battery_info[2]); + + // check for parenthesis in battery_info[2] + if (strpos($battery_info[2], '(') === false) + { + // no parenthesis found + $battery_size = trim($battery_info[2]); + } + else + { + $battery_size = trim(str_replace('(', ' (', $battery_info[2])); + } } if (count($battery_info) == 2) { @@ -117,13 +141,32 @@ class ImportCMBBatteryDataCommand extends Command if (count($battery_info) == 4) { // sample: Motolite Classic Wetcharged DIN100L + // sample: Century Excel NS60LS (60B24LS) $battery_manufacturer = trim($battery_info[0]); - $battery_model = trim($battery_info[1]) . ' ' . trim($battery_info[2]); - $battery_size = trim($battery_info[3]); + $battery_model = trim($battery_info[1]); + // check for parenthesis in battery_info[3] + if (strpos($battery_info[3], '(') === false) + { + // no parenthesis found + $battery_size = trim($battery_info[3]); + } + else + { + // need to concatenate [2] and [3] + $battery_size = trim($battery_info[2]) . ' ' . trim($battery_info[3]); + } + } + if (count($battery_info) == 5) + { + // sample: Century Marathoner Max Wet NS40ZL + $battery_manufacturer = trim($battery_info[0]); + $battery_model = trim($battery_info[1]); + $battery_size = trim($battery_info[4]); } // check if battery size has () // if so, trim it to ignore the parenthesis and what's after (. + /* $pos = stripos($battery_size, '('); if ($pos == true) { @@ -133,17 +176,17 @@ class ImportCMBBatteryDataCommand extends Command else { $clean_size = $battery_size; - } + } */ - //error_log('battery manufacturer ' . $battery_manufacturer); - //error_log('battery model ' . $battery_model); - //error_log('battery size ' . $battery_size); + error_log('battery manufacturer ' . $battery_manufacturer); + error_log('battery model ' . $battery_model); + error_log('battery size ' . $battery_size); // normalize the manufacturer, model and size for the hash // when we add to db for manufacturer, model, and size, we do not use the normalized versions $normalized_manu = $this->normalizeName($battery_manufacturer); $normalized_model = $this->normalizeName($battery_model); - $normalized_size = $this->normalizeName($clean_size); + $normalized_size = $this->normalizeName($battery_size); // save battery manufacturer if not yet in system if (!isset($this->bmanu_hash[$normalized_manu])) @@ -160,7 +203,7 @@ class ImportCMBBatteryDataCommand extends Command // save battery size if not yet in system if (!isset($this->bsize_hash[$normalized_size])) { - $this->addBatterySize(strtoupper($clean_size)); + $this->addBatterySize(strtoupper($battery_size)); } // save battery if not yet in system diff --git a/src/Command/ImportCMBBatteryModelSizeCommand.php b/src/Command/ImportCMBBatteryModelSizeCommand.php index 0157f55e..33545c4e 100644 --- a/src/Command/ImportCMBBatteryModelSizeCommand.php +++ b/src/Command/ImportCMBBatteryModelSizeCommand.php @@ -167,24 +167,16 @@ class ImportCMBBatteryModelSizeCommand extends Command // check if size is empty or if price and tradein prices are N/A if (!isset($this->bsize_hash[$marathoner_size])) { + // ignore blank sizes if ((strlen($marathoner_size) > 0) && (strlen($marathoner_price) > 0)) - { + { + // non-numeric entries are ignored since these are N/A == they don't sell it if ((is_numeric($marathoner_price)) && (is_numeric($marathoner_tradein_price))) { $this->addBatterySize($marathoner_size, $marathoner_price, $marathoner_tradein_price); } - else - { - $not_added[] = $this->addInvalidEntry('MARATHONER', $marathoner_size, $marathoner_price, - $marathoner_tradein_price, 'Non numeric price/tradein price.'); - } - } - else - { - $not_added[] = $this->addInvalidEntry('MARATHONER', $marathoner_size, $marathoner_price, - $marathoner_tradein_price, 'Empty size and price.'); } } if (!isset($this->bsize_hash[$classic_size])) @@ -192,21 +184,12 @@ class ImportCMBBatteryModelSizeCommand extends Command if ((strlen($classic_size) > 0) && (strlen($classic_price) > 0)) { - if (($classic_price != 'N/A') || - ($classic_tradein_price != 'N/A')) + // non-numeric entries are ignored since these are N/A == they don't sell it + if ((is_numeric($classic_price)) && + (is_numeric($classic_tradein_price))) { $this->addBatterySize($classic_size, $classic_price, $classic_tradein_price); } - else - { - $not_added[] = $this->addInvalidEntry('CLASSIC', $classic_size, $classic_price, - $classic_tradein_price, 'Non numeric price/tradein price.'); - } - } - else - { - $not_added[] = $this->addInvalidEntry('CLASSIC', $classic_size, $classic_price, - $classic_tradein_price, 'Empty size and price.'); } } if (!isset($this->bsize_hash[$excel_size])) @@ -214,21 +197,12 @@ class ImportCMBBatteryModelSizeCommand extends Command if ((strlen($excel_size) > 0) && (strlen($excel_price) > 0)) { - if (($excel_price != 'N/A') || - ($excel_tradein_price != 'N/A')) + // non-numeric entries are ignored since these are N/A == they don't sell it + if ((is_numeric($excel_price)) && + (is_numeric($excel_tradein_price))) { $this->addBatterySize($excel_size, $excel_price, $excel_tradein_price); } - else - { - $not_added[] = $this->addInvalidEntry('EXCEL', $excel_size, $excel_price, - $excel_tradein_price, 'Non numeric price/tradein price.'); - } - } - else - { - $not_added[] = $this->addInvalidEntry('EXCEL', $excel_size, $excel_price, - $excel_tradein_price, 'Empty size and price.'); } } @@ -237,21 +211,12 @@ class ImportCMBBatteryModelSizeCommand extends Command if ((strlen($sdfc_size) > 0) && (strlen($sdfc_price) > 0)) { - if (($sdfc_price != 'N/A') || - ($sdfc_tradein_price != 'N/A')) + // non-numeric entries are ignored since these are N/A == they don't sell it + if ((is_numeric($sdfc_price)) && + (is_numeric($sdfc_tradein_price))) { $this->addBatterySize($sdfc_size, $sdfc_price, $sdfc_tradein_price); } - else - { - $not_added[] = $this->addInvalidEntry('SDFC', $sdfc_size, $sdfc_price, - $sdfc_tradein_price, 'Non numeric price/tradein price.'); - } - } - else - { - $not_added[] = $this->addInvalidEntry('SDFC', $sdfc_size, $sdfc_price, - $sdfc_tradein_price, 'Empty size and price.'); } } @@ -306,7 +271,15 @@ class ImportCMBBatteryModelSizeCommand extends Command protected function addBatterySize($size, $price, $tradein_price) { $new_bsize = new BatterySize(); - $new_bsize->setName(strtoupper($size)); + + $clean_size = strtoupper($size); + // check if size is M-42, if so, we need to change it to M42 + if (strpos($clean_size, 'M-42') !== false) + { + $clean_size = strtoupper(str_replace('-', '', $size)); + } + + $new_bsize->setName(strtoupper($clean_size)); $new_bsize->setTIPriceMotolite($tradein_price); $this->em->persist($new_bsize); From 49ee85898e6421803646096fdd5e80cf135651a6 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 11:33:17 +0000 Subject: [PATCH 10/12] Create command to add vehicle manufacturers, vehicles and their compatible batteries. #460 --- src/Command/ImportCMBBatteryDataCommand.php | 10 +- .../ImportCMBVehicleCompatibilityCommand.php | 449 ------------------ 2 files changed, 3 insertions(+), 456 deletions(-) delete mode 100644 src/Command/ImportCMBVehicleCompatibilityCommand.php diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index dd907d0e..8a074a86 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -82,10 +82,6 @@ class ImportCMBBatteryDataCommand extends Command $desc = trim($fields[self::F_BATT_DESC]); $price = trim($fields[self::F_BATT_PRICE]); - error_log($code); - error_log($desc); - error_log($price); - $clean_price = trim($price, '$'); $battery_info = explode(' ', $desc); @@ -178,9 +174,9 @@ class ImportCMBBatteryDataCommand extends Command $clean_size = $battery_size; } */ - error_log('battery manufacturer ' . $battery_manufacturer); - error_log('battery model ' . $battery_model); - error_log('battery size ' . $battery_size); + //error_log('battery manufacturer ' . $battery_manufacturer); + //error_log('battery model ' . $battery_model); + //error_log('battery size ' . $battery_size); // normalize the manufacturer, model and size for the hash // when we add to db for manufacturer, model, and size, we do not use the normalized versions diff --git a/src/Command/ImportCMBVehicleCompatibilityCommand.php b/src/Command/ImportCMBVehicleCompatibilityCommand.php deleted file mode 100644 index 8add2968..00000000 --- a/src/Command/ImportCMBVehicleCompatibilityCommand.php +++ /dev/null @@ -1,449 +0,0 @@ -em = $om; - - // load existing battery data - $this->loadBatteryManufacturers(); - $this->loadBatteryModels(); - $this->loadBatterySizes(); - $this->loadBatteries(); - - // load existing vehicle data - $this->loadVehicleManufacturers(); - $this->loadVehicleMakes(); - - parent::__construct(); - } - - protected function configure() - { - $this->setName('cmbvehiclecompatibility:import') - ->setDescription('Retrieve from a CSV file battery and vehicle information.') - ->setHelp('Creates battery manufacturers, models, sizes, vehicle makes, and models based on data from imported CSV.') - ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $csv_file = $input->getArgument('file'); - - // attempt to open file - try - { - $fh = fopen($csv_file, "r"); - } - catch (Exception $e) - { - throw new Exception('The file "' . $csv_file . '" could be read.'); - } - - // get entity manager - $em = $this->em; - - $row_num = 0; - error_log('Processing vehicle compatibility csv file...'); - while (($fields = fgetcsv($fh)) !== false) - { - $comp_batteries = []; - if ($row_num < 2) - { - $row_num++; - continue; - } - - // initialize size battery array for cases where the battery size has '/' - $sdfc_sizes = []; - $ultramax_sizes = []; - $motolite_sizes = []; - $marathoner_sizes = []; - $excel_sizes = []; - - // battery info - $sdfc_size = trim($fields[self::F_BATT_SDFC]); - $ultramax_size = trim($fields[self::F_BATT_ULTRAMAX]); - $motolite_size = trim($fields[self::F_BATT_MOTOLITE]); - $marathoner_size = trim($fields[self::F_BATT_MARATHONER]); - $excel_size = trim($fields[self::F_BATT_EXCEL]); - - // check the sizes for '/' - $pos = stripos($sdfc_size, '/'); - if ($pos == false) - { - // no '/' in size - $sdfc_sizes[] = $this->normalizeName($sdfc_size); - } - else - { - // we have '/' in size so we have to explode - $sizes = explode('/', $sdfc_size); - foreach ($sizes as $size) - { - $sdfc_sizes[] = $this->normalizeName($size); - } - } - - $pos = stripos($motolite_size, '/'); - if ($pos == false) - { - // no '/' in size - $motolite_sizes[] = $this->normalizeName($motolite_size); - } - else - { - // we have '/' in size so we have to explode - $sizes = explode('/', $motolite_size); - foreach ($sizes as $size) - { - $motolite_sizes[] = $this->normalizeName($size); - } - } - - $pos = stripos($marathoner_size, '/'); - if ($pos == false) - { - // no '/' in size - $marathoner_sizes[] = $this->normalizeName($marathoner_size); - } - else - { - // we have '/' in size so we have to explode - $sizes = explode('/', $marathoner_size); - foreach ($sizes as $size) - { - $marathoner_sizes[] = $this->normalizeName($size); - } - } - - $pos = stripos($ultramax_size, '/'); - if ($pos == false) - { - // no '/' in size - $ultramax_sizes[] = $this->normalizeName($ultramax_size); - } - else - { - // we have '/' in size so we have to explode - $sizes = explode('/', $ultramax_size); - foreach ($sizes as $size) - { - $ultramax_sizes[] = $this->normalizeName($size); - } - } - - $pos = stripos($excel_size, '/'); - if ($pos == false) - { - // no '/' in size - $excel_sizes[] = $this->normalizeName($excel_size); - } - else - { - // we have '/' in size so we have to explode - $sizes = explode('/', $excel_size); - foreach ($sizes as $size) - { - $excel_sizes[] = $this->normalizeName($size); - } - } - - - // normalize the battery manufacturers and battery models - $norm_century = $this->normalizeName(self::STR_CENTURY); - $norm_sdfc = $this->normalizeName(self::STR_SDFC); - $norm_motolite = $this->normalizeName(self::STR_MOTOLITE); - $norm_wetcharged = $this->normalizeName(self::STR_WETCHARGED); - $norm_marathoner = $this->normalizeName(self::STR_MARATHONER); - $norm_ultramax = $this->normalizeName(self::STR_ULTRAMAX); - $norm_excel = $this->normalizeName(self::STR_EXCEL); - - //foreach($sdfc_sizes as $size) - //{ - // error_log('sdfc size ' . $size); - //} - //foreach($motolite_sizes as $size) - //{ - // error_log('motolite size ' . $size); - //} - //foreach($marathoner_sizes as $size) - //{ - // error_log('marathoner size ' . $size); - //} - - // vehicle info - $manufacturer = trim(strtolower($fields[self::F_VEHICLE_MANUFACTURER])); - $make = trim(strtolower($fields[self::F_VEHICLE_MAKE])); - $year = trim($fields[self::F_VEHICLE_YEAR]); - - // vehicle data - // check if vehicle manufacturer has been added - if (!isset($this->vmanu_hash[$manufacturer])) - $this->addVehicleManufacturer($manufacturer); - - // check if vehicle make has been added - if (!isset($this->vmake_hash[$manufacturer][$make])) - { - foreach($sdfc_sizes as $size) - { - if (!(empty($size))) - { - if (isset($this->batt_hash[$norm_century][$norm_sdfc][$size])) - $comp_batteries[] = $this->batt_hash[$norm_century][$norm_sdfc][$size]; - else - error_log('Not in the system: ' . $norm_century . ' ' . $norm_sdfc . ' ' . $size); - } - } - foreach($ultramax_sizes as $size) - { - if (!(empty($size))) - { - if (isset($this->batt_hash[$norm_ultramax][$norm_ultramax][$size])) - $comp_batteries[] = $this->batt_hash[$norm_ultramax][$norm_ultramax][$size]; - else - error_log('Not in the system: ' . $norm_ultramax . ' ' . $norm_ultramax . ' ' . $size); - } - } - foreach($motolite_sizes as $size) - { - if (!(empty($size))) - { - if (isset($this->batt_hash[$norm_motolite][$norm_wetcharged][$size])) - $comp_batteries[] = $this->batt_hash[$norm_motolite][$norm_wetcharged][$size]; - else - error_log('Not in the system: ' . $norm_motolite . ' ' . $norm_wetcharged . ' ' . $size); - } - } - foreach($marathoner_sizes as $size) - { - if (!(empty($size))) - { - if (isset($this->batt_hash[$norm_century][$norm_marathoner][$size])) - $comp_batteries[] = $this->batt_hash[$norm_century][$norm_marathoner][$size]; - else - error_log('Not in the system: ' . $norm_century . ' ' . $norm_marathoner . ' ' . $size); - } - } - foreach($excel_sizes as $size) - { - if (!(empty($size))) - { - if (isset($this->batt_hash[$norm_excel][$norm_excel][$size])) - $comp_batteries[] = $this->batt_hash[$norm_excel][$norm_excel][$size]; - else - error_log('Not in the system: ' . $norm_excel . ' ' . $norm_excel . ' ' . $size); - } - } - $this->addVehicleMake($manufacturer, $make, $year, $comp_batteries); - } - - $row_num++; - } - - return 0; - } - - protected function addVehicleManufacturer($name) - { - // save to db - $vehicle_manufacturer = new VehicleManufacturer(); - - $vehicle_manufacturer->setName(strtoupper($name)); - - $this->em->persist($vehicle_manufacturer); - $this->em->flush(); - - // add to hash - $this->vmanu_hash[$name] = $vehicle_manufacturer; - } - - protected function addVehicleMake($manufacturer, $make, $year, $batteries) - { - // save to db - $vehicle = new Vehicle(); - - $vmanu = $this->vmanu_hash[$manufacturer]; - - // parse year from and year to - $year_from = ''; - $year_to = ''; - - if (!empty($year)) - { - $model_years = explode('-', $year); - $year_from = $model_years[0]; - if (!empty($year_to)) - $year_to = $model_years[1]; - - // check if $year_to is the string "Present" - // if so, set to 0, for now - if ($year_to == self::STR_PRESENT) - $year_to = 0; - } - - $vehicle->setManufacturer($vmanu) - ->setMake(strtoupper($make)) - ->setModelYearFrom($year_from) - ->setModelYearTo($year_to); - - // add vehicle to battery - foreach ($batteries as $battery) - { - $battery->addVehicle($vehicle); - $this->em->persist($battery); - } - - // add vehicle to manufacturer - $vmanu->addVehicle($vehicle); - - $this->em->persist($vmanu); - $this->em->persist($vehicle); - $this->em->flush(); - - // add to hash - $this->vmake_hash[$manufacturer][$make] = $vehicle; - } - - protected function loadBatteryManufacturers() - { - $this->bmanu_hash = []; - - $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); - foreach ($batt_manufacturers as $batt_manu) - { - $name = $this->normalizeName($batt_manu->getName()); - $this->bmanu_hash[$name] = $batt_manu; - } - } - - protected function loadBatteryModels() - { - $this->bmodel_hash = []; - - $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); - foreach ($batt_models as $batt_model) - { - $name = $this->normalizeName($batt_model->getName()); - $this->bmodel_hash[$name] = $batt_model; - } - } - - protected function loadBatterySizes() - { - $this->bsize_hash = []; - - $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); - foreach ($batt_sizes as $batt_size) - { - $name = $this->normalizeName($batt_size->getName()); - $this->bsize_hash[$name] = $batt_size; - } - } - - protected function loadBatteries() - { - $this->batt_hash = []; - - $batts = $this->em->getRepository(Battery::class)->findAll(); - foreach ($batts as $batt) - { - $brand = $this->normalizeName($batt->getManufacturer()->getName()); - $model = $this->normalizeName($batt->getModel()->getName()); - $size = $this->normalizeName($batt->getSize()->getName()); - - $this->batt_hash[$brand][$model][$size] = $batt; - } - } - - protected function loadVehicleManufacturers() - { - $this->vmanu_hash = []; - - $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); - foreach ($vmanus as $vmanu) - { - $name = $this->normalizeName($vmanu->getName()); - $this->vmanu_hash[$name] = $vmanu; - } - } - - protected function loadVehicleMakes() - { - $this->vmake_hash = []; - - $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); - foreach ($vmakes as $vmake) - { - $manufacturer = $vmake->getManufacturer()->getName(); - $make = $this->normalizeName($vmake->getMake()); - - $this->vmake_hash[$manufacturer][$make] = $vmake; - } - } - - protected function normalizeName($name) - { - // check for M-42. Need to convert to M42 - if (strcasecmp($name, self::STR_M_42) == 0) - { - $normalized_key = strtolower(self::STR_M42); - } - else - { - $normalized_key = trim(strtolower($name)); - } - - return $normalized_key; - } - -} From 8c32792c386591af72b2941126958cc5ef3c11d2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 11:34:50 +0000 Subject: [PATCH 11/12] Create command to add vehicle manufacturers, vehicles and their compatible batteries. #460 --- ...tCMBBatteryVehicleCompatibilityCommand.php | 373 ++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 src/Command/ImportCMBBatteryVehicleCompatibilityCommand.php diff --git a/src/Command/ImportCMBBatteryVehicleCompatibilityCommand.php b/src/Command/ImportCMBBatteryVehicleCompatibilityCommand.php new file mode 100644 index 00000000..202b1c78 --- /dev/null +++ b/src/Command/ImportCMBBatteryVehicleCompatibilityCommand.php @@ -0,0 +1,373 @@ +em = $om; + + // load existing battery data + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + $this->loadBatteries(); + + // load existing vehicle data + $this->loadVehicleManufacturers(); + $this->loadVehicleMakes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('cmbbatteryvehiclecompatibility:import') + ->setDescription('Retrieve from a CSV file vehicle and battery compatibility information.') + ->setHelp('Creates vehicles and their compatible batteries based on data from imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Path to output file for vehicles not added.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get the sizes, vehicles, battery prices from the csv file + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + $output_file = $input->getArgument('output_file'); + + // attempt to open file + try + { + $output_fh = fopen($output_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $output_file . '" could be read.'); + } + + + // get entity manager + $em = $this->em; + + $row_num = 0; + error_log('Processing vehicle and battery compatibility file...'); + while (($fields = fgetcsv($fh)) !== false) + { + $comp_batteries = []; + if ($row_num < 2) + { + $row_num++; + continue; + } + + // get vehicle info from file + $manufacturer = trim(strtolower($fields[self::F_VEHICLE_MANUFACTURER])); + $make = trim(strtolower($fields[self::F_VEHICLE_MAKE])); + $year = trim($fields[self::F_VEHICLE_YEAR]); + + // get battery info from file + $marathoner_size = $this->normalizeName(trim($fields[self::F_BATT_MARATHONER_SIZE])); + $marathoner_price = trim($fields[self::F_BATT_MARATHONER_PRICE]); + $marathoner_tradein_price = trim($fields[self::F_BATT_MARATHONER_TRADEIN_PRICE]); + + $classic_size = $this->normalizeName(trim($fields[self::F_BATT_CLASSIC_SIZE])); + $classic_price = trim($fields[self::F_BATT_CLASSIC_PRICE]); + $classic_tradein_price = trim($fields[self::F_BATT_CLASSIC_TRADEIN_PRICE]); + + $excel_size = $this->normalizeName(trim($fields[self::F_BATT_EXCEL_SIZE])); + $excel_price = trim($fields[self::F_BATT_EXCEL_PRICE]); + $excel_tradein_price = trim($fields[self::F_BATT_EXCEL_TRADEIN_PRICE]); + + $sdfc_size = $this->normalizeName(trim($fields[self::F_BATT_SDFC_SIZE])); + $sdfc_price = trim($fields[self::F_BATT_SDFC_PRICE]); + $sdfc_tradein_price = trim($fields[self::F_BATT_SDFC_TRADEIN_PRICE]); + + // get the compatible batteries + // get marathoner battery + if (strlen($marathoner_size) > 0) + { + // check if battery in system + if (isset($this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$marathoner_size])) + $comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$marathoner_size]; + } + + // get classic battery + if (strlen($classic_size) > 0) + { + // check if battery in system + if (isset($this->batt_hash[self::STR_MOTOLITE][self::STR_CLASSIC][$classic_size])) + $comp_batteries[] = $this->batt_hash[self::STR_MOTOLITE][self::STR_CLASSIC][$classic_size]; + } + + // get excel battery + if (strlen($excel_size) > 0) + { + // check if battery in system + if (isset($this->batt_hash[self::STR_CENTURY][self::STR_EXCEL][$excel_size])) + $comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_EXCEL][$excel_size]; + } + + // get sdfc battery + if (strlen($sdfc_size) > 0) + { + // check if battery in system + if (isset($this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$sdfc_size])) + $comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$sdfc_size]; + } + + // check if vehicle manufacturer has been added + if (!isset($this->vmanu_hash[$manufacturer])) + $this->addVehicleManufacturer($manufacturer); + + // check if vehicle make has been added + if (!isset($this->vmake_hash[$manufacturer][$make])) + { + $this->addVehicleMake($manufacturer, $make, $year, $comp_batteries); + } + + $row_num++; + } + + $em->flush(); + } + + protected function addVehicleManufacturer($name) + { + // save to db + $vehicle_manufacturer = new VehicleManufacturer(); + + $vehicle_manufacturer->setName(strtoupper($name)); + + $this->em->persist($vehicle_manufacturer); + $this->em->flush(); + + // add to hash + $this->vmanu_hash[$name] = $vehicle_manufacturer; + } + + protected function addVehicleMake($manufacturer, $make, $year, $batteries) + { + // save to db + $vehicle = new Vehicle(); + + $vmanu = $this->vmanu_hash[$manufacturer]; + + // parse year from and year to + $year_from = ''; + $year_to = ''; + + if (!empty($year)) + { + $model_years = explode('-', $year); + $year_from = $model_years[0]; + if (!empty($year_to)) + $year_to = $model_years[1]; + + // check if $year_to is the string "Present" + // if so, set to 0, for now + if ($year_to == self::STR_PRESENT) + $year_to = 0; + } + + $vehicle->setManufacturer($vmanu) + ->setMake(strtoupper($make)) + ->setModelYearFrom($year_from) + ->setModelYearTo($year_to); + + // add vehicle to battery + foreach ($batteries as $battery) + { + $battery->addVehicle($vehicle); + $this->em->persist($battery); + } + + // add vehicle to manufacturer + $vmanu->addVehicle($vehicle); + + $this->em->persist($vmanu); + $this->em->persist($vehicle); + $this->em->flush(); + + // add to hash + $this->vmake_hash[$manufacturer][$make] = $vehicle; + } + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $this->normalizeName($batt_manu->getName()); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $this->normalizeName($batt_model->getName()); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $this->normalizeName($batt_size->getName()); + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function loadBatteries() + { + $this->batt_hash = []; + + $batts = $this->em->getRepository(Battery::class)->findAll(); + foreach ($batts as $batt) + { + $brand = $this->normalizeName($batt->getManufacturer()->getName()); + $model = $this->normalizeName($batt->getModel()->getName()); + $size = $this->normalizeName($batt->getSize()->getName()); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + + protected function loadVehicleManufacturers() + { + $this->vmanu_hash = []; + + $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + foreach ($vmanus as $vmanu) + { + $name = $this->normalizeName($vmanu->getName()); + $this->vmanu_hash[$name] = $vmanu; + } + } + + protected function loadVehicleMakes() + { + $this->vmake_hash = []; + + $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); + foreach ($vmakes as $vmake) + { + $manufacturer = $vmake->getManufacturer()->getName(); + $make = $this->normalizeName($vmake->getMake()); + + $this->vmake_hash[$manufacturer][$make] = $vmake; + } + } + + protected function normalizeName($name) + { + // check if name contains M-42. Need to convert to M42 + if (strpos($name, self::STR_M_42) !== false) + { + // contains M-42 + $changed_name = str_replace(self::STR_M_42, self::STR_M42, $name); + $normalized_key = strtolower($changed_name); + } + else + { + $normalized_key = trim(strtolower($name)); + } + + $normalized_key = trim(strtolower($name)); + + return $normalized_key; + } +} From 8eebf6ee1ccd87c1ef837771ff965ac17cc1e7b0 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 4 Sep 2020 08:07:31 +0000 Subject: [PATCH 12/12] Add invalid mobile numbers to invalid file. #460 --- src/Command/MigrateCMBLegacyJobOrderCommand.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Command/MigrateCMBLegacyJobOrderCommand.php b/src/Command/MigrateCMBLegacyJobOrderCommand.php index 3b09b6e8..2b42ddd0 100644 --- a/src/Command/MigrateCMBLegacyJobOrderCommand.php +++ b/src/Command/MigrateCMBLegacyJobOrderCommand.php @@ -180,6 +180,21 @@ class MigrateCMBLegacyJobOrderCommand extends Command continue; } + // check if mobile phone number has letters or spaces or special characters + if (!(preg_match('/^\d+$/', $customer_mobile))) + { + // not a valid mobile number + //error_log('invalid number ' . $customer_mobile); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $batt_trade_in, $replaced_by, $remark, $satisfaction, 'Invalid mobile number'); + + $total_inv_entries++; + // move to next entry + continue; + } + // create job order $cmb_legacy_jo = new CMBLegacyJobOrder();