Create command to import Carfix data. #460
This commit is contained in:
parent
4796a9be14
commit
4bfbfbf995
2 changed files with 216 additions and 0 deletions
164
src/Command/ImportCMBLegacyJobOrderCommand.php
Normal file
164
src/Command/ImportCMBLegacyJobOrderCommand.php
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
<?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\CMBLegacyJobOrder;
|
||||
|
||||
class ImportCMBLegacyJobOrderCommand 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;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('cmblegacyjoborderdata:import')
|
||||
->setDescription('Retrieve from a CSV file CarFix data.')
|
||||
->setHelp('Creates legacy job order entries 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
|
||||
$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]);
|
||||
|
||||
$data_entry = $this->processEntry($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);
|
||||
|
||||
$legacy_data = new CMBLegacyJobOrder();
|
||||
$legacy_data->setData($data_entry);
|
||||
|
||||
$this->em->persist($legacy_data);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function processEntry($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)
|
||||
{
|
||||
$data_entry = [
|
||||
'entry_num' => $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,
|
||||
];
|
||||
|
||||
return $data_entry;
|
||||
}
|
||||
|
||||
}
|
||||
52
src/Entity/CMBLegacyJobOrder.php
Normal file
52
src/Entity/CMBLegacyJobOrder.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="legacy_cmb_job_order")
|
||||
*/
|
||||
class CMBLegacyJobOrder
|
||||
{
|
||||
// unique id
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
// data from csv file
|
||||
/**
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
public function addData($id, $value)
|
||||
{
|
||||
$this->data[$id] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setData(array $data_array)
|
||||
{
|
||||
$this->data = $data_array;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getData($id)
|
||||
{
|
||||
// return null if we don't have it
|
||||
if (!isset($this->data[$id]))
|
||||
return null;
|
||||
|
||||
return $this->data[$id];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue