373 lines
12 KiB
PHP
373 lines
12 KiB
PHP
<?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\ORM\EntityManagerInterface;
|
|
|
|
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 ImportCMBBatteryVehicleCompatibilityCommand extends Command
|
|
{
|
|
// field index in csv file
|
|
// vehicle info
|
|
const F_VEHICLE_MANUFACTURER = 1;
|
|
const F_VEHICLE_MAKE = 2;
|
|
const F_VEHICLE_YEAR = 3;
|
|
|
|
// CENTURY MARATHONER BATTERY
|
|
const F_BATT_MARATHONER_SIZE = 4;
|
|
const F_BATT_MARATHONER_PRICE = 5;
|
|
const F_BATT_MARATHONER_TRADEIN_PRICE = 6;
|
|
const F_BATT_MARATHONER_DISCOUNT = 7;
|
|
|
|
// MOTOLITE CLASSIC BATTERY
|
|
const F_BATT_CLASSIC_SIZE = 8;
|
|
const F_BATT_CLASSIC_PRICE = 9;
|
|
const F_BATT_CLASSIC_TRADEIN_PRICE = 10;
|
|
const F_BATT_CLASSIC_DISCOUNT = 11;
|
|
|
|
// CENTURY EXCEL BATTERY
|
|
const F_BATT_EXCEL_SIZE = 12;
|
|
const F_BATT_EXCEL_PRICE = 13;
|
|
const F_BATT_EXCEL_TRADEIN_PRICE = 14;
|
|
const F_BATT_EXCEL_DISCOUNT = 15;
|
|
|
|
// CENTURY SDFC BATTERY
|
|
const F_BATT_SDFC_SIZE = 16;
|
|
const F_BATT_SDFC_PRICE = 17;
|
|
const F_BATT_SDFC_TRADEIN_PRICE = 18;
|
|
const F_BATT_SDFC_DISCOUNT = 19;
|
|
|
|
// constants for battery manufacturer names
|
|
const STR_CENTURY = 'century';
|
|
const STR_MOTOLITE = 'motolite';
|
|
|
|
// constants for battery models
|
|
const STR_MARATHONER = 'marathoner';
|
|
const STR_CLASSIC = 'classic';
|
|
const STR_EXCEL = 'excel';
|
|
const STR_SDFC = 'sdfc';
|
|
|
|
// special case for battery size
|
|
const STR_M_42 = 'M-42';
|
|
const STR_M42 = 'M42';
|
|
|
|
// for the model year
|
|
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(EntityManagerInterface $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('cmbbatteryvehiclecompatibility:import')
|
|
->setDescription('Retrieve from a CSV file vehicle and battery compatibility information.')
|
|
->setHelp('Creates vehicles and their compatible batteries based on data from imported CSV.')
|
|
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.')
|
|
->addArgument('output_file', InputArgument::REQUIRED, 'Path to output file for vehicles not added.');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
// get the sizes, vehicles, battery prices from the csv file
|
|
$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.');
|
|
}
|
|
|
|
$output_file = $input->getArgument('output_file');
|
|
|
|
// attempt to open file
|
|
try
|
|
{
|
|
$output_fh = fopen($output_file, "w");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $output_file . '" could be read.');
|
|
}
|
|
|
|
|
|
// get entity manager
|
|
$em = $this->em;
|
|
|
|
$row_num = 0;
|
|
error_log('Processing vehicle and battery compatibility file...');
|
|
while (($fields = fgetcsv($fh)) !== false)
|
|
{
|
|
$comp_batteries = [];
|
|
if ($row_num < 2)
|
|
{
|
|
$row_num++;
|
|
continue;
|
|
}
|
|
|
|
// get vehicle info from file
|
|
$manufacturer = trim(strtolower($fields[self::F_VEHICLE_MANUFACTURER]));
|
|
$make = trim(strtolower($fields[self::F_VEHICLE_MAKE]));
|
|
$year = trim($fields[self::F_VEHICLE_YEAR]);
|
|
|
|
// get battery info from file
|
|
$marathoner_size = $this->normalizeName(trim($fields[self::F_BATT_MARATHONER_SIZE]));
|
|
$marathoner_price = trim($fields[self::F_BATT_MARATHONER_PRICE]);
|
|
$marathoner_tradein_price = trim($fields[self::F_BATT_MARATHONER_TRADEIN_PRICE]);
|
|
|
|
$classic_size = $this->normalizeName(trim($fields[self::F_BATT_CLASSIC_SIZE]));
|
|
$classic_price = trim($fields[self::F_BATT_CLASSIC_PRICE]);
|
|
$classic_tradein_price = trim($fields[self::F_BATT_CLASSIC_TRADEIN_PRICE]);
|
|
|
|
$excel_size = $this->normalizeName(trim($fields[self::F_BATT_EXCEL_SIZE]));
|
|
$excel_price = trim($fields[self::F_BATT_EXCEL_PRICE]);
|
|
$excel_tradein_price = trim($fields[self::F_BATT_EXCEL_TRADEIN_PRICE]);
|
|
|
|
$sdfc_size = $this->normalizeName(trim($fields[self::F_BATT_SDFC_SIZE]));
|
|
$sdfc_price = trim($fields[self::F_BATT_SDFC_PRICE]);
|
|
$sdfc_tradein_price = trim($fields[self::F_BATT_SDFC_TRADEIN_PRICE]);
|
|
|
|
// get the compatible batteries
|
|
// get marathoner battery
|
|
if (strlen($marathoner_size) > 0)
|
|
{
|
|
// check if battery in system
|
|
if (isset($this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$marathoner_size]))
|
|
$comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_MARATHONER][$marathoner_size];
|
|
}
|
|
|
|
// get classic battery
|
|
if (strlen($classic_size) > 0)
|
|
{
|
|
// check if battery in system
|
|
if (isset($this->batt_hash[self::STR_MOTOLITE][self::STR_CLASSIC][$classic_size]))
|
|
$comp_batteries[] = $this->batt_hash[self::STR_MOTOLITE][self::STR_CLASSIC][$classic_size];
|
|
}
|
|
|
|
// get excel battery
|
|
if (strlen($excel_size) > 0)
|
|
{
|
|
// check if battery in system
|
|
if (isset($this->batt_hash[self::STR_CENTURY][self::STR_EXCEL][$excel_size]))
|
|
$comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_EXCEL][$excel_size];
|
|
}
|
|
|
|
// get sdfc battery
|
|
if (strlen($sdfc_size) > 0)
|
|
{
|
|
// check if battery in system
|
|
if (isset($this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$sdfc_size]))
|
|
$comp_batteries[] = $this->batt_hash[self::STR_CENTURY][self::STR_SDFC][$sdfc_size];
|
|
}
|
|
|
|
// 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]))
|
|
{
|
|
$this->addVehicleMake($manufacturer, $make, $year, $comp_batteries);
|
|
}
|
|
|
|
$row_num++;
|
|
}
|
|
|
|
$em->flush();
|
|
}
|
|
|
|
protected function addVehicleManufacturer($name)
|
|
{
|
|
// save to db
|
|
$vehicle_manufacturer = new VehicleManufacturer();
|
|
|
|
$vehicle_manufacturer->setName(strtoupper($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(strtoupper($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 = $this->normalizeName($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 = $this->normalizeName($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 = $this->normalizeName($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 = $this->normalizeName($batt->getManufacturer()->getName());
|
|
$model = $this->normalizeName($batt->getModel()->getName());
|
|
$size = $this->normalizeName($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 = $this->normalizeName($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 = $this->normalizeName($vmake->getMake());
|
|
|
|
$this->vmake_hash[$manufacturer][$make] = $vmake;
|
|
}
|
|
}
|
|
|
|
protected function normalizeName($name)
|
|
{
|
|
// check if name contains M-42. Need to convert to M42
|
|
if (strpos($name, self::STR_M_42) !== false)
|
|
{
|
|
// contains M-42
|
|
$changed_name = str_replace(self::STR_M_42, self::STR_M42, $name);
|
|
$normalized_key = strtolower($changed_name);
|
|
}
|
|
else
|
|
{
|
|
$normalized_key = trim(strtolower($name));
|
|
}
|
|
|
|
$normalized_key = trim(strtolower($name));
|
|
|
|
return $normalized_key;
|
|
}
|
|
}
|