From 9b7fa2048a410cc5370515af0808c965de078650 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Sep 2020 08:36:39 +0000 Subject: [PATCH] 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; + } +}