resq/src/Command/UpdateWarrantyDealerInfoCommand.php

184 lines
4.8 KiB
PHP

<?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\Warranty;
class UpdateWarrantyDealerInfoCommand extends Command
{
// field index in csv file
const F_ID = 0;
const F_BTY_MODEL_ID = 1;
const F_BTY_SIZE_ID = 2;
const F_SERIAL = 3;
const F_WARRANTY_CLASS = 4;
const F_PLATE_NUMBER = 5;
const F_STATUS = 6;
const F_DATE_CREATED = 7;
const F_DATE_PURCHASE = 8;
const F_DATE_EXPIRE = 9;
const F_DATE_CLAIM = 10;
const F_SAP_BTY_ID = 11;
const F_CLAIM_ID = 12;
const F_FIRST_NAME = 13;
const F_LAST_NAME = 14;
const F_MOBILE_NUMBER = 15;
const F_ACTIVATED = 16;
const F_WARR_PRIV_POLICY = 17;
const F_EMAIL = 18;
const F_VEHICLE_ID = 19;
const F_CUSTOMER_ID = 20;
const F_FILE_INVOICE = 21;
const F_FILE_WARR_CARD = 22;
const F_V_MODEL_YEAR = 23;
const F_ODOMETER = 24;
const F_DEALER_NAME = 25;
const F_DEALER_ADDRESS = 26;
const F_CONTACT_NUM = 27;
const F_CUST_ADDRESS = 28;
const F_DATE_PURCHASE_CUST = 29;
const F_VALIDATED = 30;
const F_PROVINCE_ID = 31;
const F_MUNICIPALITY_ID = 32;
const F_CREATE_SOURCE = 33;
const F_DEALER_BRANCH_CODE = 34;
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('warranty:updatedealerinfo')
->setDescription('Update warranty dealer information.')
->setHelp('Update warranty dealer information.')
->addArgument('input_file', InputArgument::REQUIRED, 'Path to the CSV file with the warranty info.')
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// for now, hardcode new dealer info (G-SIX SUMULONG)
$new_dealer = [
'name' => 'G-SIX SUMULONG',
'address' => '175 SUMULONG HIGHWAY MAYAMOT RIZAL_X000D_',
'branch_code' => 'ZN03587644',
];
$csv_file = $input->getArgument('input_file');
$output_file = $input->getArgument('output_file');
// attempt to open file
try
{
$fh = fopen($csv_file, "r");
}
catch (Exception $e)
{
throw new Exception('The file "' . $csv_file . '" could be opened.');
}
// start at 1 since 0 has the headers
$row_num = 1;
$output_info = [];
while (($fields = fgetcsv($fh)) !== false)
{
// ignore first row
if ($row_num == 1)
{
$row_num++;
continue;
}
// process row
$output_info[] = $this->processRow($fields, $new_dealer);
$row_num++;
}
// write to output file
$this->outputWarrantyInfo($output_file, $output_info);
fclose($fh);
return 0;
}
protected function processRow($fields, $new_dealer)
{
// get the warranty id
$warranty_id = trim($fields[SELF::F_ID]);
// find the warranty
$warranty = $this->em->getRepository(Warranty::class)->find($warranty_id);
if ($warranty == null)
{
// log warranty not found
return $this->setOutputInfo($fields, 'NOT UPDATED', 'Warranty not found');
}
// update warranty dealer information
$dealer_name = $new_dealer['name'];
$dealer_address = $new_dealer['address'];
$dealer_branch_code = $new_dealer['branch_code'];
$warranty->setDealerName($dealer_name)
->setDealerAddress($dealer_address)
->setDealerBranchCode($dealer_branch_code);
$this->em->flush();
// log successful update
return $this->setOutputInfo($fields, 'UPDATED', '');
}
protected function setOutputInfo($fields, $status, $reason)
{
$warranty_id = trim($fields[SELF::F_ID]);
return [
$warranty_id,
$status,
$reason,
];
}
protected function outputWarrantyInfo($output_file, $entries)
{
try
{
$fh = fopen($output_file, "w");
}
catch (Exception $e)
{
throw new Exception('The file "' . $report_file . '" could be opened.');
}
// write the headers
fputcsv($fh, [
'Warranty ID',
'Status',
'Reason',
]);
foreach ($entries as $row)
{
fputcsv($fh, $row);
}
fclose($fh);
}
}