From eed91a413aeb9eccadd5b20fdbcc5c8dc94c2c2b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 18 Aug 2020 10:36:19 +0000 Subject: [PATCH] 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;