Redo the battery vehicle compatibility command. #270
This commit is contained in:
parent
c0d363dd25
commit
02b88bca64
2 changed files with 365 additions and 1 deletions
|
|
@ -284,4 +284,3 @@ class ImportCMBBatteryDataCommand extends Command
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
365
src/Command/ImportCMBVehicleCompatibilityCommand.php
Normal file
365
src/Command/ImportCMBVehicleCompatibilityCommand.php
Normal file
|
|
@ -0,0 +1,365 @@
|
|||
<?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;
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
|
||||
class ImportCMBVehicleCompatibilityCommand extends Command
|
||||
{
|
||||
// field index in csv file
|
||||
const F_VEHICLE_MANUFACTURER = 1;
|
||||
const F_VEHICLE_MAKE = 2;
|
||||
const F_VEHICLE_YEAR = 3;
|
||||
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;
|
||||
|
||||
const STR_CENTURY = 'Century';
|
||||
const STR_MOTOLITE = 'Motolite';
|
||||
const STR_MARSHALL = 'Marshall';
|
||||
const STR_SDFC = 'SDFC';
|
||||
const STR_MARATHONER = 'Marathoner';
|
||||
const STR_WETCHARGED = 'Classic WetCharged';
|
||||
const STR_PRESENT = 'Present';
|
||||
|
||||
protected $em;
|
||||
|
||||
protected $bmanu_hash;
|
||||
protected $bmodel_hash;
|
||||
protected $bsize_hash;
|
||||
protected $batt_hash;
|
||||
|
||||
protected $vmanu_hash;
|
||||
protected $vmake_hash;
|
||||
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->em = $om;
|
||||
|
||||
// load existing battery data
|
||||
$this->loadBatteryManufacturers();
|
||||
$this->loadBatteryModels();
|
||||
$this->loadBatterySizes();
|
||||
$this->loadBatteries();
|
||||
|
||||
// load existing vehicle data
|
||||
$this->loadVehicleManufacturers();
|
||||
$this->loadVehicleMakes();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('cmbvehiclecompatibility:import')
|
||||
->setDescription('Retrieve from a CSV file battery and vehicle information.')
|
||||
->setHelp('Creates battery manufacturers, models, sizes, vehicle makes, and models 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;
|
||||
|
||||
$row_num = 0;
|
||||
error_log('Processing vehicle compatibility csv file...');
|
||||
while (($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
$comp_batteries = [];
|
||||
if ($row_num < 2)
|
||||
{
|
||||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// initialize size battery array for cases where the battery size has '/'
|
||||
$sdfc_sizes = [];
|
||||
$motolite_sizes = [];
|
||||
$marathoner_sizes = [];
|
||||
|
||||
// battery info
|
||||
$sdfc_size = trim($fields[self::F_BATT_SDFC]);
|
||||
//$ultramax = trim($fields[self::F_BATT_ULTRAMAX]);
|
||||
$motolite_size = trim($fields[self::F_BATT_MOTOLITE]);
|
||||
$marathoner_size = trim($fields[self::F_BATT_MARATHONER]);
|
||||
//$excel = trim($fields[self::F_BATT_EXCEL]);
|
||||
|
||||
// check the sizes for '/'
|
||||
$pos = stripos($sdfc_size, '/');
|
||||
if ($pos == false)
|
||||
{
|
||||
// no '/' in size
|
||||
$sdfc_sizes[] = $sdfc_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we have '/' in size so we have to explode
|
||||
$sizes = explode('/', $sdfc_size);
|
||||
foreach ($sizes as $size)
|
||||
{
|
||||
$clean_size = trim($size);
|
||||
$sdfc_sizes[] = $clean_size;
|
||||
}
|
||||
}
|
||||
|
||||
$pos = stripos($motolite_size, '/');
|
||||
if ($pos == false)
|
||||
{
|
||||
// no '/' in size
|
||||
$motolite_sizes[] = $motolite_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we have '/' in size so we have to explode
|
||||
$sizes = explode('/', $motolite_size);
|
||||
foreach ($sizes as $size)
|
||||
{
|
||||
$clean_size = trim($size);
|
||||
$motolite_sizes[] = $clean_size;
|
||||
}
|
||||
}
|
||||
|
||||
$pos = stripos($marathoner_size, '/');
|
||||
if ($pos == false)
|
||||
{
|
||||
// no '/' in size
|
||||
$marathoner_sizes[] = $marathoner_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we have '/' in size so we have to explode
|
||||
$sizes = explode('/', $marathoner_size);
|
||||
foreach ($sizes as $size)
|
||||
{
|
||||
$clean_size = trim($size);
|
||||
$marathoner_sizes[] = $clean_size;
|
||||
}
|
||||
}
|
||||
|
||||
//foreach($sdfc_sizes as $size)
|
||||
//{
|
||||
// error_log('sdfc size ' . $size);
|
||||
//}
|
||||
//foreach($motolite_sizes as $size)
|
||||
//{
|
||||
// error_log('motolite size ' . $size);
|
||||
//}
|
||||
//foreach($marathoner_sizes as $size)
|
||||
//{
|
||||
// error_log('marathoner size ' . $size);
|
||||
//}
|
||||
|
||||
// vehicle info
|
||||
$manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]);
|
||||
$make = trim($fields[self::F_VEHICLE_MAKE]);
|
||||
$year = trim($fields[self::F_VEHICLE_YEAR]);
|
||||
|
||||
// vehicle data
|
||||
// check if vehicle manufacturer has been added
|
||||
if (!isset($this->vmanu_hash[$manufacturer]))
|
||||
$this->addVehicleManufacturer($manufacturer);
|
||||
|
||||
// check if vehicle make has been added
|
||||
if (!isset($this->vmake_hash[$manufacturer][$make]))
|
||||
{
|
||||
foreach($sdfc_sizes as $size)
|
||||
{
|
||||
if (!(empty($size)))
|
||||
{
|
||||
if (isset($this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$size]))
|
||||
$comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$size];
|
||||
}
|
||||
}
|
||||
//if (!(empty($ultramax)))
|
||||
// $comp_batteries[] = $this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax];
|
||||
foreach($motolite_sizes as $size)
|
||||
{
|
||||
if (!(empty($size)))
|
||||
{
|
||||
if (isset($this->batt_hash[self::STR_MOTOLITE][self::STR_WETCHARGED][$size]))
|
||||
$comp_batteries[] = $this->batt_hash[self::STR_MOTOLITE][self::STR_WETCHARGED][$size];
|
||||
}
|
||||
}
|
||||
foreach($marathoner_sizes as $size)
|
||||
{
|
||||
if (!(empty($size)))
|
||||
{
|
||||
if (isset($this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$size]))
|
||||
$comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$size];
|
||||
}
|
||||
}
|
||||
//if (!(empty($excel)))
|
||||
// $comp_batteries[] = $this->batt_hash[$brand_excel][$brand_excel][$excel];
|
||||
|
||||
$this->addVehicleMake($manufacturer, $make, $year, $comp_batteries);
|
||||
}
|
||||
|
||||
$row_num++;
|
||||
}
|
||||
}
|
||||
|
||||
protected function addVehicleManufacturer($name)
|
||||
{
|
||||
// save to db
|
||||
$vehicle_manufacturer = new VehicleManufacturer();
|
||||
|
||||
$vehicle_manufacturer->setName($name);
|
||||
|
||||
$this->em->persist($vehicle_manufacturer);
|
||||
$this->em->flush();
|
||||
|
||||
// add to hash
|
||||
$this->vmanu_hash[$name] = $vehicle_manufacturer;
|
||||
}
|
||||
|
||||
protected function addVehicleMake($manufacturer, $make, $year, $batteries)
|
||||
{
|
||||
// save to db
|
||||
$vehicle = new Vehicle();
|
||||
|
||||
$vmanu = $this->vmanu_hash[$manufacturer];
|
||||
|
||||
// parse year from and year to
|
||||
$year_from = '';
|
||||
$year_to = '';
|
||||
|
||||
if (!empty($year))
|
||||
{
|
||||
$model_years = explode('-', $year);
|
||||
$year_from = $model_years[0];
|
||||
if (!empty($year_to))
|
||||
$year_to = $model_years[1];
|
||||
|
||||
// check if $year_to is the string "Present"
|
||||
// if so, set to 0, for now
|
||||
if ($year_to == self::STR_PRESENT)
|
||||
$year_to = 0;
|
||||
}
|
||||
|
||||
$vehicle->setManufacturer($vmanu)
|
||||
->setMake($make)
|
||||
->setModelYearFrom($year_from)
|
||||
->setModelYearTo($year_to);
|
||||
|
||||
// add vehicle to battery
|
||||
foreach ($batteries as $battery)
|
||||
{
|
||||
$battery->addVehicle($vehicle);
|
||||
$this->em->persist($battery);
|
||||
}
|
||||
|
||||
// add vehicle to manufacturer
|
||||
$vmanu->addVehicle($vehicle);
|
||||
|
||||
$this->em->persist($vmanu);
|
||||
$this->em->persist($vehicle);
|
||||
$this->em->flush();
|
||||
|
||||
// add to hash
|
||||
$this->vmake_hash[$manufacturer][$make] = $vehicle;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadVehicleManufacturers()
|
||||
{
|
||||
$this->vmanu_hash = [];
|
||||
|
||||
$vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll();
|
||||
foreach ($vmanus as $vmanu)
|
||||
{
|
||||
$name = $vmanu->getName();
|
||||
$this->vmanu_hash[$name] = $vmanu;
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadVehicleMakes()
|
||||
{
|
||||
$this->vmake_hash = [];
|
||||
|
||||
$vmakes = $this->em->getRepository(Vehicle::class)->findAll();
|
||||
foreach ($vmakes as $vmake)
|
||||
{
|
||||
$manufacturer = $vmake->getManufacturer()->getName();
|
||||
$make = $vmake->getMake();
|
||||
|
||||
$this->vmake_hash[$manufacturer][$make] = $vmake;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue