Add command to add prices to batteries using a csv file. #270

This commit is contained in:
Korina Cordero 2019-10-10 06:59:46 +00:00
parent a8792c3209
commit 111bb55a50

View file

@ -0,0 +1,236 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\Battery;
use App\Entity\BatteryManufacturer;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
class ImportCMBBatteryPriceCommand extends Command
{
const F_BATT_DESC = 2;
const F_BATT_PRICE = 3;
protected $em;
protected $bmodel_hash;
protected $bsize_hash;
protected $batt_hash;
public function __construct(ObjectManager $om)
{
$this->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;
}
}
}