Add command to add batteries. #270
This commit is contained in:
parent
5348d2c51c
commit
a29ea8756b
2 changed files with 176 additions and 3 deletions
174
src/Command/ImportBatteriesCommand.php
Normal file
174
src/Command/ImportBatteriesCommand.php
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
<?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\BatteryManufacturer;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\Battery;
|
||||
|
||||
class ImportBatteriesCommand extends Command
|
||||
{
|
||||
// field index in csv file
|
||||
const F_BATT_SDFC = 4;
|
||||
const F_BATT_ULTRAMAX = 5;
|
||||
const F_BATT_MOTOLITE = 6;
|
||||
const F_BATT_MARATHONER = 7;
|
||||
const F_BATT_EXCEL = 8;
|
||||
|
||||
protected $em;
|
||||
protected $bmanu_hash;
|
||||
protected $bmodel_hash;
|
||||
protected $bsize_hash;
|
||||
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->em = $om;
|
||||
|
||||
$this->loadBatteryManufacturers();
|
||||
$this->loadBatteryModels();
|
||||
$this->loadBatterySizes();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('battery:create')
|
||||
->setDescription('Retrieve from a CSV file battery information.')
|
||||
->setHelp('Creates batteries based on data from 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;
|
||||
|
||||
// go through the file
|
||||
// data starts at row 3
|
||||
$row_num = 0;
|
||||
$sdfc = '';
|
||||
$ultra = '';
|
||||
$motolite = '';
|
||||
$marathoner = '';
|
||||
$excel = '';
|
||||
|
||||
while (($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
if ($row_num < 1)
|
||||
{
|
||||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get manufacturer names
|
||||
if ($row_num == 1)
|
||||
{
|
||||
$sdfc = trim($fields[self::F_BATT_SDFC]);
|
||||
$ultra = trim($fields[self::F_BATT_ULTRAMAX]);
|
||||
$motolite = trim($fields[self::F_BATT_MOTOLITE]);
|
||||
$marathoner = trim($fields[self::F_BATT_MARATHONER]);
|
||||
$excel = trim($fields[self::F_BATT_EXCEL]);
|
||||
|
||||
$row_num++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$sdfc_size = trim($fields[self::F_BATT_SDFC]);
|
||||
$ultra_size = trim($fields[self::F_BATT_ULTRAMAX]);
|
||||
$motolite_size = trim($fields[self::F_BATT_MOTOLITE]);
|
||||
$marathoner_size = trim($fields[self::F_BATT_MARATHONER]);
|
||||
$excel_size = trim($fields[self::F_BATT_EXCEL]);
|
||||
|
||||
//$output->writeln('moogle batt brand ' . $sdfc);
|
||||
//$output->writeln('moogle batt size ' . $sdfc_size);
|
||||
|
||||
// check if battery has been added
|
||||
if (!isset($this->batt_hash[$sdfc][$sdfc][$sdfc_size]))
|
||||
{
|
||||
//$output->writeln('moogle new batt');
|
||||
if (!(empty($sdfc_size)))
|
||||
{
|
||||
//$output->writeln('moogle about to add');
|
||||
$this->addBattery($sdfc, $sdfc_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function addBattery($brand, $size)
|
||||
{
|
||||
// save to db
|
||||
$battery = new Battery();
|
||||
$battery->setManufacturer($this->bmanu_hash[$brand])
|
||||
->setModel($this->bmodel_hash[$brand])
|
||||
->setSize($this->bsize_hash[$size])
|
||||
->setWarrantyPrivate(21)
|
||||
->setWarrantyCommercial(6)
|
||||
->setWarrantyTnv(12);
|
||||
|
||||
$this->em->persist($battery);
|
||||
$this->em->flush();
|
||||
|
||||
|
||||
// insert into hash
|
||||
$this->batt_hash[$brand][$brand][$size] = $battery;
|
||||
}
|
||||
|
||||
protected function loadBatteryManufacturers()
|
||||
{
|
||||
$this->bmanu_hash = [];
|
||||
|
||||
$batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll();
|
||||
foreach ($batt_manufacturers as $batt_manu)
|
||||
{
|
||||
$name = $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 = $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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -60,14 +60,13 @@ class Battery
|
|||
|
||||
// product code
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
* @ORM\Column(type="string", length=80, nullable=true)
|
||||
*/
|
||||
protected $prod_code;
|
||||
|
||||
// sap code
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @ORM\Column(type="string", length=80, nullable=true)
|
||||
*/
|
||||
protected $sap_code;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue