Add command to import CarFix data. #460

This commit is contained in:
Korina Cordero 2020-08-18 10:36:19 +00:00
parent ae2a22b159
commit eed91a413a
2 changed files with 316 additions and 1 deletions

View file

@ -0,0 +1,316 @@
<?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 DateTime;
class ImportCMBCarFixDataCommand extends Command
{
// field index in csv file
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 $cust_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;
error_log('Processing CarFix data csv file...');
while (($fields = fgetcsv($fh)) !== false)
{
if ($row_num < 1)
{
$row_num++;
continue;
}
// get the information
$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
// parse car_model
// vehicle manufacturer is the first entry
// vehicle model is the 2nd entry
// check if manufacturer is in hash
// if not, add to csv of unprocessed entries
// if so, check if model is in hash
// if not, add to csv of unprocessed entries
// check batteries
// if battery info not in hash, add to csv of unprocessed entries
$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 cust_hash
$clean_plate = $this->cleanPlateNumber($vehicle_number);
//error_log($clean_plate . ' ' . $new_jo->getServiceType());
// check if plate number has been added
if (!isset($this->cust_hash[$clean_plate]))
$this->addCustomer($clean_plate, $customer_name, $customer_mobile);
$row_num++;
}
return 0;
}
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;
}
}
public function addCustomer($plate_num, $name, $mobile)
{
}
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;
}
}

View file

@ -42,7 +42,6 @@ class Customer
// last name
/**
* @ORM\Column(type="string", length=80)
* @Assert\NotBlank()
*/
protected $last_name;