Add vehicle indexing and initial work on customer import #52

This commit is contained in:
Kendrick Chan 2018-03-21 13:11:18 +08:00
parent 30494f9889
commit 516fd0f90e

View file

@ -12,14 +12,43 @@ use Doctrine\Common\Persistence\ObjectManager;
use DateTime;
use App\Entity\Customer;
use App\Entity\CustomerVehicle;
use App\Entity\Vehicle;
use App\Entity\VehicleManufacturer;
class ImportCustomerCommand extends Command
{
// field index in csv file
const F_ID = 0;
const F_TITLE = 1;
const F_FIRST_NAME = 2;
// role
// meh contact
const F_MOBILE_PHONE = 5;
const F_LANDLINE = 6;
const F_OFFICE_PHONE = 7;
const F_FAX = 8;
const F_EMAIL = 9;
const F_PLATE_NUMBER = 10;
const F_V_BRAND = 11;
const F_V_MAKE = 12;
const F_V_MODEL = 13;
const F_V_COLOR = 14;
const F_CUSTOMER_CLASS = 15;
const F_COMMENTS = 16;
const F_NOTES = 17;
protected $em;
// NOTE: we use indeces for these since we tend to run out of memory if we don't
protected $mfg_index;
protected $vehicle_index;
public function __construct(ObjectManager $em)
{
$this->em = $em;
$this->populateMfgIndex();
$this->populateVehicleIndex();
parent::__construct();
}
@ -32,12 +61,100 @@ class ImportCustomerCommand extends Command
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
}
protected function populateMfgIndex()
{
$mfgs = $this->em->getRepository(VehicleManufacturer::class)->findAll();
$this->mfg_index = [];
foreach ($mfgs as $mfg)
{
$this->mfg_index[$mfg->getName()] = $mfg;
}
}
protected function populateVehicleIndex()
{
$vs = $this->em->getRepository(Vehicle::class)->findAll();
$this->vehicle_index = [];
foreach ($vs as $v)
{
$this->vehicle_index[strtoupper($v->getMake())][$v->getModelYearFormatted()] = $v;
}
}
protected function findVehicle($output, $row)
{
// search for manufacturer
$mfg_name = trim($row[self::F_V_BRAND]);
if (!isset($this->mfg_index[$mfg_name]))
{
$output->writeln('manufacturer not found: ' . $mfg_name);
return null;
}
$mfg = $this->mfg_index[$mfg_name];
// check if empty make
$make = strtoupper(trim($row[self::F_V_MAKE]));
if (empty($make))
return null;
// get year from and to from model
$model = trim($row[self::F_V_MODEL]);
if (empty($model))
$model = 'NONE';
if ($model == 'NONE')
{
$year_from = 0;
$year_to = 0;
$model_index = '-';
}
else
{
$ex_model = explode('-', $model);
$year_from = trim($ex_model[0]);
if (isset($ex_model[1]))
{
$year_to = strtolower(trim($ex_model[1]));
if ($year_to == 'on' || $year_to == 'up')
$year_to = '2018';
$model_index = $year_from . ' - ' . $year_to;
}
else
{
$year_to = 0;
$model_index = $year_from;
}
}
// search for make and model from index
if (!isset($this->vehicle_index[$make][$model_index]))
{
// check if we match make but not model
if (isset($this->vehicle_index[$make]))
{
// is there only one entry in the index? that's the one!
if (count($this->vehicle_index[$make]) == 1)
{
$vehicle = reset($this->vehicle_index[$make]);
return $vehicle;
}
}
$output->writeln("vehicle make and model not found: $mfg_name - $make ($model_index)");
return null;
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv_file = $input->getArgument('file');
/*
CSV column order:
old CSV column order:
** NO LONGER APPLIES
0 - internal id
1 - name
2 - duplicate (yes or blank)
@ -83,7 +200,7 @@ class ImportCustomerCommand extends Command
$row_num = 1;
while (($fields = fgetcsv($fh)) !== false)
{
$output->writeln("Parsing row " . $row_num . "...");
// $output->writeln("Parsing row " . $row_num . "...");
// ignore first row
if ($row_num == 1)
@ -92,11 +209,19 @@ class ImportCustomerCommand extends Command
continue;
}
$id = trim($fields[0]);
$fname = trim($fields[23]);
$lname = trim($fields[24]);
$id = trim($fields[self::F_ID]);
$fname = trim($fields[self::F_FIRST_NAME]);
$output->writeln($id . ' - ' . $fname . ' ' . $lname);
// TODO: current csv has no last name yet
// $lname = trim($fields[self::F_LAST_NAME]);
$lname = "TEMP";
// $output->writeln($id . ' - ' . $fname . ' ' . $lname);
// get vehicle object
$vehicle = $this->findVehicle($output, $fields);
$row_num++;
}
}
}