Add command to set current battery of customer vehicle from migrated data. #487
This commit is contained in:
parent
4de3277030
commit
51cdc6ff15
1 changed files with 169 additions and 0 deletions
169
src/Command/UpdateCMBMigratedCustomerVehicleBatteryCommand.php
Normal file
169
src/Command/UpdateCMBMigratedCustomerVehicleBatteryCommand.php
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
<?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\CustomerVehicle;
|
||||
use App\Entity\CMBLegacyJobOrder;
|
||||
use App\Entity\Battery;
|
||||
|
||||
class UpdateCMBMigratedCustomerVehicleBatteryCommand extends Command
|
||||
{
|
||||
// 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';
|
||||
|
||||
protected $em;
|
||||
protected $batt_hash;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
// load existing batteries
|
||||
$this->loadBatteries();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('cmbcustomervehicle:updatebattery')
|
||||
->setDescription('Update customer vehicle battery information.')
|
||||
->setHelp('Updates the customer vehicle battery based on migrated data. ')
|
||||
->addArgument('output_file', InputArgument::REQUIRED, 'Path to the output CSV file of the entries not updated.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$csv_file = $input->getArgument('output_file');
|
||||
|
||||
// attempt to open file
|
||||
try
|
||||
{
|
||||
$fh = fopen($csv_file, "w");
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
throw new Exception('The file "' . $csv_file . '" could be opened.');
|
||||
}
|
||||
|
||||
error_log('Updating customer vehicles...');
|
||||
|
||||
// get all customer vehicles
|
||||
$cv_results = $this->em->getRepository(CustomerVehicle::class)->findAll();
|
||||
|
||||
$not_updated = [];
|
||||
foreach ($cv_results as $cv)
|
||||
{
|
||||
$plate_number = $cv->getPlateNumber();
|
||||
|
||||
// find cmb legacy job order using plate number
|
||||
$cmb_legacy_results = $this->em->getRepository(CMBLegacyJobOrder::class)->findBy(['plate_number' => $plate_number], ['trans_date' => 'DESC']);
|
||||
if ($cmb_legacy_results != null)
|
||||
{
|
||||
// get the latest entry
|
||||
$cmb_legacy = current($cmb_legacy_results);
|
||||
|
||||
// get battery model and size
|
||||
$battery_model = $this->normalizeName($cmb_legacy->getBatteryModel());
|
||||
$battery_size = $this->normalizeName($cmb_legacy->getBatterySize());
|
||||
|
||||
// figure out manufacturer using model
|
||||
// century = marathoner, excel, sdfc
|
||||
// motolite = classic
|
||||
$battery_manu = self::STR_CENTURY;
|
||||
if ($battery_model == self::STR_CLASSIC)
|
||||
$battery_manu = self::STR_MOTOLITE;
|
||||
|
||||
// get battery
|
||||
$cv_battery = null;
|
||||
if (isset($this->batt_hash[$battery_manu][$battery_model][$battery_size]))
|
||||
$cv_battery = $this->batt_hash[$battery_manu][$battery_model][$battery_size];
|
||||
|
||||
if ($cv_battery != null)
|
||||
{
|
||||
//error_log('found battery for ' . $plate_number . ' battery ' . $cv_battery->getModel()->getName() . ' ' . $cv_battery->getSize()->getName());
|
||||
$cv->setCurrentBattery($cv_battery);
|
||||
}
|
||||
else
|
||||
{
|
||||
//error_log('no battery for ' . $battery_model . ' ' . $battery_size);
|
||||
$not_updated[] = $this->addNotUpdatedEntry($cv, $battery_manu, $battery_model, $battery_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
|
||||
// output the entries that were not added
|
||||
if (count($not_updated) > 0)
|
||||
{
|
||||
fputcsv($fh, [
|
||||
'Customer Vehicle ID',
|
||||
'Plate Number',
|
||||
'Battery Manufacturer',
|
||||
'Battery Model',
|
||||
'Battery Size',
|
||||
'Reason',
|
||||
]);
|
||||
|
||||
foreach($not_updated as $row)
|
||||
{
|
||||
fputcsv($fh, $row);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 addNotUpdatedEntry($cv, $manu, $model, $size)
|
||||
{
|
||||
$entry = [
|
||||
$cv->getID(),
|
||||
$cv->getPlateNumber(),
|
||||
strtoupper($manu),
|
||||
strtoupper($model),
|
||||
strtoupper($size),
|
||||
'Battery not in system.',
|
||||
];
|
||||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
protected function normalizeName($name)
|
||||
{
|
||||
$normalized_key = trim(strtolower($name));
|
||||
|
||||
return $normalized_key;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue