From 111bb55a50bf515d5ea8d19f018b38c0074df668 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 10 Oct 2019 06:59:46 +0000 Subject: [PATCH] Add command to add prices to batteries using a csv file. #270 --- src/Command/ImportCMBBatteryPriceCommand.php | 236 +++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 src/Command/ImportCMBBatteryPriceCommand.php diff --git a/src/Command/ImportCMBBatteryPriceCommand.php b/src/Command/ImportCMBBatteryPriceCommand.php new file mode 100644 index 00000000..9af64b1d --- /dev/null +++ b/src/Command/ImportCMBBatteryPriceCommand.php @@ -0,0 +1,236 @@ +em = $om; + + // load existing batteries and sizes + $this->loadBatteryModels(); + $this->loadBatteries(); + $this->loadBatterySizes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('cmbbatterydata:importprice') + ->setDescription('Import a CSV file with battery prices.') + ->setHelp('Adds the battery 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; + } + + // battery price info + $desc = trim($fields[self::F_BATT_DESC]); + $price = trim($fields[self::F_BATT_PRICE]); + + $clean_price = trim($price, '$'); + + $battery_info = explode(' ', $desc); + + // if battery_info has 3 elements, get the last two + // if only 2, get both + // if 4, get the first and the last + $battery_model = ''; + $battery_size = ''; + if (count($battery_info) == 3) + { + // sample: Century Marathoner 120-7L + $battery_model = trim($battery_info[1]); + $battery_size = trim($battery_info[2]); + } + if (count($battery_info) == 2) + { + // sample: Marshall DIN55R + $battery_model = trim($battery_info[0]); + $battery_size = trim($battery_info[1]); + } + if (count($battery_info) == 4) + { + // sample: Motolite Classic Wetcharged DIN100L + $battery_model = trim($battery_info[0]); + $battery_size = trim($battery_info[3]); + } + + // find the battery size + $batt_size = $this->findBatterySize($battery_model, $battery_size); + + // add checking if null is returned + if ($batt_size != null) + { + //error_log('battery model ' . $battery_model); + //error_log('battery size ' . $batt_size); + + // get the battery + if (!isset($this->batt_hash[$battery_model][$battery_model][$batt_size])) + { + error_log('No battery in system with model ' . $battery_model . ' and size ' . $batt_size); + } + else + { + $batt = $this->batt_hash[$battery_model][$battery_model][$batt_size]; + + // set battery price + if ($batt != null) + { + $batt->setSellingPrice($clean_price); + + $this->em->persist($batt); + $this->em->flush(); + } + } + } + else + { + error_log('Cannot find battery with model ' . $battery_model . ' and size ' . $battery_size); + } + } + } + + protected function findBatterySize($bmodel, $bsize) + { + $batt_size = null; + // check if model is valid + if (!isset($this->bmodel_hash[$bmodel])) + { + return 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->batt_hash[$bmodel][$bmodel][$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; + return $batt_size; + } + } + } + } + } + else + { + $batt_size = $bsize; + } + + return $batt_size; + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $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 = $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 = $batt->getManufacturer()->getName(); + $model = $batt->getModel()->getName(); + $size = $batt->getSize()->getName(); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + +} +