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])) { // 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); } } } if (!isset($this->bsize_hash[$classic_size])) { if ((strlen($classic_size) > 0) && (strlen($classic_price) > 0)) { // 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); } } } if (!isset($this->bsize_hash[$excel_size])) { if ((strlen($excel_size) > 0) && (strlen($excel_price) > 0)) { // 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); } } } if (!isset($this->bsize_hash[$sdfc_size])) { if ((strlen($sdfc_size) > 0) && (strlen($sdfc_price) > 0)) { // 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); } } } $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(); $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); // 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; } }