From 7adcb5244ee4b9e9b118641af750dd6dec7d5ec7 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 10 Oct 2019 07:37:08 +0000 Subject: [PATCH] Add command to add tradein prices to battery sizes using a csv file. #270 --- .../ImportCMBBatteryTradeInPriceCommand.php | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/Command/ImportCMBBatteryTradeInPriceCommand.php diff --git a/src/Command/ImportCMBBatteryTradeInPriceCommand.php b/src/Command/ImportCMBBatteryTradeInPriceCommand.php new file mode 100644 index 00000000..02d1e003 --- /dev/null +++ b/src/Command/ImportCMBBatteryTradeInPriceCommand.php @@ -0,0 +1,163 @@ +em = $om; + + // load existing sizes + $this->loadBatterySizes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('cmbbatterydata:importtradeinprice') + ->setDescription('Import a CSV file with trade in prices.') + ->setHelp('Adds the battery tradein prices to existing batteries based on 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; + + // loop through the rows + $row_num = 0; + error_log('Processing battery price csv file...'); + while (($fields = fgetcsv($fh)) !== false) + { + // data starts at row 2 + if ($row_num < 2) + { + $row_num++; + continue; + } + + // tradein price info + // battery price info + $desc = trim($fields[self::F_SIZE_DESC]); + $price = trim($fields[self::F_TRADEIN_PRICE]); + + $clean_price = trim($price, '$'); + + $size_info = explode(' ', $desc); + + $size = $size_info[1]; + + $batt_size = $this->findBatterySize($size); + + if (isset($this->bsize_hash[$batt_size])) + { + $battery_size = $this->bsize_hash[$batt_size]; + + // use TIPriceMotolite + $battery_size->setTIPriceMotolite($clean_price); + + $this->em->persist($battery_size); + $this->em->flush(); + } + else + { + error_log('Cannot find battery size ' . $size); + } + + + } + } + + protected function findBatterySize($bsize) + { + $batt_size = null; + + // check if battery size has parenthesis + $pos = stripos($bsize, '('); + if ($pos == true) + { + // parse battery size because of Q85(insert string here) and M42? M-42?(insert string here) + // explode and get the first element + $batts = explode('(', $bsize); + $bsize = trim($batts[0]); + //error_log('new battery size ' . $bsize); + } + + // check if size is set + if (!isset($this->bsize_hash[$bsize])) + { + // loop through the hash since the size might be part of the sizes with '/' + foreach ($this->bsize_hash as $key => $data) + { + $pos = stripos($key, '/'); + if ($pos == true) + { + // explode the key + $key_strings = explode('/', $key); + foreach ($key_strings as $ks) + { + $clean_ks = trim($ks); + if (strcasecmp($bsize, $clean_ks) == 0) + { + // bsize is one of the sizes with '/' + $batt_size = $key; + //error_log($batt_size); + return $batt_size; + } + } + } + } + } + else + { + $batt_size = $bsize; + } + + return $batt_size; + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $batt_size->getName(); + $this->bsize_hash[$name] = $batt_size; + } + } + + +}