Add command to add tradein prices to battery sizes using a csv file. #270

This commit is contained in:
Korina Cordero 2019-10-10 07:37:08 +00:00
parent 111bb55a50
commit 7adcb5244e

View file

@ -0,0 +1,163 @@
<?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\BatterySize;
class ImportCMBBatteryTradeInPriceCommand extends Command
{
const F_SIZE_DESC = 2;
const F_TRADEIN_PRICE = 3;
protected $em;
protected $bsize_hash;
public function __construct(ObjectManager $om)
{
$this->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;
}
}
}