Removed old import carfix data command. #460
This commit is contained in:
parent
885d1911a2
commit
d0fcbe8cf3
1 changed files with 0 additions and 489 deletions
|
|
@ -1,489 +0,0 @@
|
|||
<?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\JobOrder;
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
use App\Entity\BatteryManufacturer;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\CustomerVehicle;
|
||||
|
||||
use App\Ramcar\CMBServiceType;
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\FuelType;
|
||||
use App\Ramcar\VehicleStatusCondition;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class ImportCMBCarFixDataCommand extends Command
|
||||
{
|
||||
// field index in csv file
|
||||
const F_INDEX = 0;
|
||||
const F_CREATED_DATE = 1;
|
||||
const F_CASE_NO = 2;
|
||||
const F_INSURER = 3;
|
||||
const F_VEHICLE_NO = 4;
|
||||
const F_CAR_MODEL = 5;
|
||||
const F_NATURE_OF_CALL = 6;
|
||||
const F_SERVICE_NEEDED = 7;
|
||||
const F_LOCATION = 8;
|
||||
const F_STATE = 9;
|
||||
const F_DRIVER = 10;
|
||||
const F_TRUCK = 11;
|
||||
const F_WORKSHOP_ARRIVAL_TIME = 12;
|
||||
const F_STATUS = 13;
|
||||
const F_CUSTOMER_NAME = 14;
|
||||
const F_CUSTOMER_PHONE_NO = 15;
|
||||
const F_OUR_REFERENCE = 16;
|
||||
const F_ODOMETER = 17;
|
||||
const F_BATT_MODEL = 18;
|
||||
const F_BATT_SIZE = 19;
|
||||
const F_BATT_TRADE_IN = 20;
|
||||
const F_REPLACED_BY = 21;
|
||||
const F_REMARK = 22;
|
||||
const F_SATISFACTION = 23;
|
||||
|
||||
protected $em;
|
||||
|
||||
protected $bmanu_hash;
|
||||
protected $bmodel_hash;
|
||||
protected $bsize_hash;
|
||||
protected $batt_hash;
|
||||
|
||||
protected $vmanu_hash;
|
||||
protected $vmake_hash;
|
||||
|
||||
protected $cv_hash;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
// load existing battery data
|
||||
$this->loadBatteryManufacturers();
|
||||
$this->loadBatteryModels();
|
||||
$this->loadBatterySizes();
|
||||
$this->loadBatteries();
|
||||
|
||||
// load existing vehicle data
|
||||
$this->loadVehicleManufacturers();
|
||||
$this->loadVehicleMakes();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('cmbcarfixdata:import')
|
||||
->setDescription('Retrieve from a CSV file CarFix data.')
|
||||
->setHelp('Creates job orders based on data from imported CSV.')
|
||||
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
|
||||
}
|
||||
|
||||
public 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;
|
||||
$invalid_entries = [];
|
||||
error_log('Processing CarFix data csv file...');
|
||||
while (($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
if ($row_num < 1)
|
||||
{
|
||||
$row_num++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get the information
|
||||
$entry_num = trim($fields[self::F_INDEX]);
|
||||
$date_create = trim($fields[self::F_CREATED_DATE]);
|
||||
$case_number = trim($fields[self::F_CASE_NO]);
|
||||
$insurer = trim($fields[self::F_INSURER]);
|
||||
$vehicle_number = trim($fields[self::F_VEHICLE_NO]);
|
||||
$car_model = trim($fields[self::F_CAR_MODEL]);
|
||||
$nature_of_call = trim($fields[self::F_NATURE_OF_CALL]);
|
||||
$service_needed = trim($fields[self::F_SERVICE_NEEDED]);
|
||||
$location = trim($fields[self::F_LOCATION]);
|
||||
$state = trim($fields[self::F_STATE]);
|
||||
$driver = trim($fields[self::F_DRIVER]);
|
||||
$truck = trim($fields[self::F_TRUCK]);
|
||||
$workshop_arrival_time = trim($fields[self::F_WORKSHOP_ARRIVAL_TIME]);
|
||||
$status = trim($fields[self::F_STATUS]);
|
||||
$customer_name = trim($fields[self::F_CUSTOMER_NAME]);
|
||||
$customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]);
|
||||
$reference = trim($fields[self::F_OUR_REFERENCE]);
|
||||
$odometer = trim($fields[self::F_ODOMETER]);
|
||||
$batt_model = trim(strtolower($fields[self::F_BATT_MODEL]));
|
||||
$batt_size = trim(strtolower($fields[self::F_BATT_SIZE]));
|
||||
$trade_in = trim($fields[self::F_BATT_TRADE_IN]);
|
||||
$replaced_by = trim($fields[self::F_REPLACED_BY]);
|
||||
$remark = trim($fields[self::F_REMARK]);
|
||||
$satisfaction = trim($fields[self::F_SATISFACTION]);
|
||||
|
||||
//error_log($date_create . ' ' . $case_number . ' ' . $driver . ' ' . $customer_name . ' ' . $remark . ' ' . $satisfaction);
|
||||
|
||||
// get customer vehicle
|
||||
$v_status = $this->processVehicleInfo($car_model);
|
||||
if ($v_status != null)
|
||||
{
|
||||
error_log($v_status . ' ' . $car_model);
|
||||
$invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model,
|
||||
$nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time,
|
||||
$status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size,
|
||||
$trade_in, $replaced_by, $remark, $satisfaction, $v_status);
|
||||
|
||||
// move to next entry
|
||||
continue;
|
||||
}
|
||||
|
||||
// check batteries
|
||||
$batt_status = $this->processBatteryInfo($batt_model, $batt_size);
|
||||
if ($batt_status != null)
|
||||
{
|
||||
error_log($batt_status);
|
||||
$invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model,
|
||||
$nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time,
|
||||
$status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size,
|
||||
$trade_in, $replaced_by, $remark, $satisfaction, $batt_status);
|
||||
|
||||
// move to next entry
|
||||
continue;
|
||||
}
|
||||
|
||||
$new_jo = new JobOrder();
|
||||
|
||||
// add to metadata
|
||||
// case number, nature of call, workshop arrival time, reference, odometer, replaced by, satisfaction
|
||||
$new_jo->addMeta('case_number', $case_number);
|
||||
$new_jo->addMeta('nature_of_call', $nature_of_call);
|
||||
$new_jo->addMeta('workshop_arrival_time', $workshop_arrival_time);
|
||||
$new_jo->addMeta('reference', $reference);
|
||||
$new_jo->addMeta('odometer', $odometer);
|
||||
$new_jo->addMeta('replaced_by', $replaced_by);
|
||||
$new_jo->addMeta('satisfaction', $satisfaction);
|
||||
|
||||
// date_create
|
||||
$created_date = DateTime::createFromFormat('d-m-Y H:i', $date_create);
|
||||
$new_jo->setDateCreate($created_date);
|
||||
|
||||
// insurer == responsible_party
|
||||
$new_jo->setResponsibleParty($insurer);
|
||||
|
||||
// delivery address = location + state
|
||||
$delivery_address = $location . ', ' . $state;
|
||||
$new_jo->setDeliveryAddress($delivery_address);
|
||||
|
||||
// remarks == tier 1 notes
|
||||
$new_jo->setTier1Notes($remark);
|
||||
|
||||
// service_needed = service type
|
||||
// check service needed:
|
||||
// Battery == Battery Sales
|
||||
// Battery Warranty Claim == Warranty Claim
|
||||
// Battery Warranty Replacement == Warranty Replacement
|
||||
$service = $this->normalizeName($service_needed);
|
||||
switch ($service)
|
||||
{
|
||||
case 'battery':
|
||||
$new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_NEW);
|
||||
break;
|
||||
case 'battery warranty claim':
|
||||
$new_jo->setServiceType(CMBServiceType::WARRANTY_CLAIM);
|
||||
break;
|
||||
case 'battery warranty replacement':
|
||||
$new_jo->setServiceType(CMBServiceType::BATTERY_REPLACEMENT_WARRANTY);
|
||||
break;
|
||||
}
|
||||
|
||||
// status set everything to fulfilled
|
||||
// store old status to metadata
|
||||
$new_jo->setStatus(JOStatus::FULFILLED);
|
||||
$new_jo->addMeta('status', $status);
|
||||
|
||||
// plate number == vehicle_number. Use as key to cv_hash
|
||||
$clean_plate = $this->cleanPlateNumber($vehicle_number);
|
||||
//error_log($clean_plate . ' ' . $new_jo->getServiceType());
|
||||
|
||||
// check if plate number has been added
|
||||
if (!isset($this->cv_hash[$clean_plate]))
|
||||
{
|
||||
$cust = $this->addCustomer($customer_name, $customer_mobile);
|
||||
$cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust);
|
||||
}
|
||||
else
|
||||
{
|
||||
// get customer vehicle from hash
|
||||
$cv = $this->cv_hash[$clean_plate];
|
||||
$cust = $cv->getCustomer();
|
||||
}
|
||||
|
||||
$new_jo->setCustomer($cust)
|
||||
->setCustomerVehicle($cv);
|
||||
|
||||
$row_num++;
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
|
||||
// check for invalid entries. if any, write to csv
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function processVehicleInfo($car_model)
|
||||
{
|
||||
// vehicle manufacturer is the first entry
|
||||
// vehicle model is the 2nd entry + whatever follows
|
||||
$v_array = explode(' ', $car_model);
|
||||
|
||||
// manufacturer
|
||||
$v_manufacturer = trim($v_array[0]);
|
||||
|
||||
// get model
|
||||
$model_info = '';
|
||||
$v_model = '';
|
||||
for ($i = 1; $i < count($v_array); $i++)
|
||||
{
|
||||
$model_info = $model_info . ' ' . trim($v_array[$i]);
|
||||
}
|
||||
|
||||
$v_model = trim($model_info);
|
||||
//error_log($v_manufacturer . ' ' . $v_model);
|
||||
|
||||
// check if manufacturer is in hash
|
||||
if (!isset($this->vmanu_hash[$v_manufacturer]))
|
||||
{
|
||||
//error_log($v_manufacturer . ' invalid.');
|
||||
return 'Vehicle manufacturer not in system.';
|
||||
}
|
||||
|
||||
// check if manufacturer and make is in hash
|
||||
if (!isset($this->vmake_hash[$v_manufacturer][$v_model]))
|
||||
{
|
||||
//error_log($v_model . ' invalid.');
|
||||
return 'Vehicle model not in system.';
|
||||
}
|
||||
// car model info valid
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function processBatteryInfo($batt_model, $batt_size)
|
||||
{
|
||||
// check if battery model is in hash
|
||||
if (!isset($this->bmodel_hash[$batt_model]))
|
||||
return 'Battery model not in system.';
|
||||
|
||||
// check if battery size is in hash
|
||||
if (!isset($this->bsize_hash[$batt_size]))
|
||||
return 'Battery size not in system.';
|
||||
|
||||
// battery info valid
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function addCustomer($name, $mobile)
|
||||
{
|
||||
$new_cust = new Customer();
|
||||
|
||||
$new_cust->setFirstName($name)
|
||||
->setLastName('')
|
||||
->setPhoneMobile($mobile);
|
||||
|
||||
$this->em->persist($new_cust);
|
||||
|
||||
return $new_cust;
|
||||
}
|
||||
|
||||
protected function addCustomerVehicle($plate_num, $car_model, $customer)
|
||||
{
|
||||
$new_cv = new CustomerVehicle();
|
||||
|
||||
// get vehicle from hash
|
||||
$v_array = explode(' ', $car_model);
|
||||
|
||||
// manufacturer
|
||||
$v_manufacturer = trim($v_array[0]);
|
||||
|
||||
// get model
|
||||
$model_info = '';
|
||||
$v_model = '';
|
||||
for ($i = 1; $i < count($v_array); $i++)
|
||||
{
|
||||
$model_info = $model_info . ' ' . trim($v_array[$i]);
|
||||
}
|
||||
|
||||
$v_model = trim($model_info);
|
||||
|
||||
$vehicle = $this->vmake_hash[$v_manufacturer][$v_model];
|
||||
|
||||
$new_cv->setCustomer($customer)
|
||||
->setPlateNumber($plate_num)
|
||||
->setStatusCondition(VehicleStatusCondition::BRAND_NEW)
|
||||
->setModelYear('')
|
||||
->setColor('')
|
||||
->setFuelType(FuelType::GAS)
|
||||
->setHasMotoliteBattery(true)
|
||||
->setVehicle($vehicle);
|
||||
|
||||
$this->em->persist($new_cv);
|
||||
|
||||
// add customer vehicle to cv_hash
|
||||
$this->cv_hash[$plate_num] = $new_cv;
|
||||
|
||||
return $new_cv;
|
||||
}
|
||||
|
||||
protected function addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model,
|
||||
$nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time,
|
||||
$status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size,
|
||||
$trade_in, $replaced_by, $remark, $satisfaction, $v_status)
|
||||
{
|
||||
$inv_entry = [
|
||||
'number' => $entry_num,
|
||||
'created_date' => $date_create,
|
||||
'case_number' => $case_number,
|
||||
'insurer' => $insurer,
|
||||
'vehicle_number' => $vehicle_number,
|
||||
'car_model' => $car_model,
|
||||
'nature_of_call' => $nature_of_call,
|
||||
'service_needed' => $service_needed,
|
||||
'location' => $location,
|
||||
'state' => $state,
|
||||
'driver' => $driver,
|
||||
'truck' => $truck,
|
||||
'workshop_arrival_time' => $workshop_arrival_time,
|
||||
'status' => $status,
|
||||
'customer_name' => $customer_name,
|
||||
'customer_phone_number' => $customer_mobile,
|
||||
'reference' => $reference,
|
||||
'odometer' => $odometer,
|
||||
'batt_model' => $batt_model,
|
||||
'batt_size' => $batt_size,
|
||||
'batt_trade_in' => $trade_in,
|
||||
'replaced_by' => $replaced_by,
|
||||
'remark' => $remark,
|
||||
'satisfaction' => $satisfaction,
|
||||
'reason' => $v_status,
|
||||
];
|
||||
|
||||
return $inv_entry;
|
||||
}
|
||||
|
||||
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 = $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;
|
||||
}
|
||||
}
|
||||
|
||||
protected function normalizeName($name)
|
||||
{
|
||||
$normalized_key = trim(strtolower($name));
|
||||
|
||||
return $normalized_key;
|
||||
}
|
||||
|
||||
protected function cleanPlateNumber($plate_number)
|
||||
{
|
||||
// remove spaces and make upper case
|
||||
$clean_plate_number = strtoupper(str_replace(' ' , '', $plate_number));
|
||||
|
||||
return $clean_plate_number;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue