326 lines
12 KiB
PHP
326 lines
12 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\JobOrder;
|
|
use App\Entity\Warranty;
|
|
use App\Entity\SAPBattery;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
|
|
use App\Service\WarrantyHandler;
|
|
|
|
use DateTime;
|
|
|
|
class GetFlawedWarrantiesCommand extends Command
|
|
{
|
|
protected $em;
|
|
protected $wh;
|
|
protected $sapbatt_hash;
|
|
|
|
public function __construct(EntityManagerInterface $em, WarrantyHandler $wh)
|
|
{
|
|
$this->em = $em;
|
|
$this->wh = $wh;
|
|
|
|
$this->loadSAPBatteries();
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('warranty:getflawedwarranties')
|
|
->setDescription('Get warranties that need to be fixed.')
|
|
->setHelp('Get flawed warranties')
|
|
->addArgument('start_date', InputArgument::REQUIRED, 'Start Date')
|
|
->addArgument('end_date', InputArgument::REQUIRED, 'End Date');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$s_date = $input->getArgument('start_date');
|
|
$e_date = $input->getArgument('end_date');
|
|
|
|
$start_date = DateTime::createFromFormat('Ymd', $s_date);
|
|
$end_date = DateTime::createFromFormat('Ymd', $e_date);
|
|
$end_date->setTime(23, 59);
|
|
|
|
$missing_warranties = [];
|
|
$comm_private_warranties = [];
|
|
$invalid_plate_numbers = [];
|
|
$duplicate_warranties = [];
|
|
$sdate_expiry_wrong_warranties = [];
|
|
|
|
$first_results_set = $this->getMissingWarranties($start_date, $end_date);
|
|
//$second_results_set = $this->getWarrantiesWithWrongEntries($start_date, $end_date);
|
|
//$third_results_set = $this->getDuplicateWarranties($start_date, $end_date);
|
|
|
|
$missing_warranties = $first_results_set['missing'];
|
|
$invalid_plate_numbers = $first_results_set['invalid_plate_number'];
|
|
$comm_private_warranties = $first_results_set['commercial_private'];
|
|
|
|
// our warranty error categories: missing, commercial_private, invalid plate number, duplicate, start date of expiry computation wrong
|
|
$this->outputResults($s_date, $e_date, $missing_warranties, $invalid_plate_numbers, $comm_private_warranties);
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function getMissingWarranties($start_date, $end_date)
|
|
{
|
|
$em = $this->em;
|
|
|
|
// get all job orders with battery new service type within date range
|
|
$jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end');
|
|
$jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
->setParameter('date_start', $start_date)
|
|
->setParameter('date_end', $end_date);
|
|
|
|
$jos = $jo_query->iterate();
|
|
|
|
$invalids = [];
|
|
$missings = [];
|
|
$comm_privates = [];
|
|
|
|
foreach($jos as $row)
|
|
{
|
|
$invoice_item = $row[0];
|
|
$invoice = $invoice_item->getInvoice();
|
|
$jo = $invoice->getJobOrder();
|
|
$cv = $jo->getCustomerVehicle();
|
|
$customer = $jo->getCustomer();
|
|
|
|
if ($invoice_item != null)
|
|
{
|
|
if($invoice_item->getBattery() != null)
|
|
{
|
|
// manually retrieve the SAPBattery using the SAPCode
|
|
$battery_sap_code = $invoice_item->getBattery()->getSAPCode();
|
|
$found_battery = $this->findSAPBattery($battery_sap_code);
|
|
$sap_code = '\'' . $battery_sap_code . '\'';
|
|
if (!$found_battery)
|
|
{
|
|
$sap_code = 'NULL';
|
|
}
|
|
|
|
// have to use this erroneous setting of the warranty period
|
|
// to be able to find those warranties that were added with this warranty period.
|
|
// warranties found with this warranty period will be marked as commercial_private
|
|
$warranty_period = 0;
|
|
if ($invoice_item->getBattery()->getWarrantyPrivate() != null)
|
|
{
|
|
$warranty_period = $invoice_item->getBattery()->getWarrantyPrivate();
|
|
}
|
|
if ($invoice_item->getBattery()->getWarrantyCommercial() != null)
|
|
{
|
|
$warranty_period = $invoice_item->getBattery()->getWarrantyCommercial();
|
|
}
|
|
if ($invoice_item->getBattery()->getWarrantyTnv() != null)
|
|
{
|
|
$warranty_period = $invoice_item->getBattery()->getWarrantyTnv();
|
|
}
|
|
|
|
if ($invoice->getDateCreate() != null)
|
|
{
|
|
// check if plate number is "clean". If not, do not insert into warranty
|
|
if (!(Warranty::cleanPlateNumber($cv->getPlateNumber())))
|
|
{
|
|
$invalids[] = [
|
|
'jo_id' => $jo->getID(),
|
|
'plate_number' => $cv->getPlateNumber(),
|
|
'expiry_date' => '',
|
|
];
|
|
|
|
continue;
|
|
}
|
|
|
|
// check if warranty already exists
|
|
$cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber());
|
|
|
|
// use date_create of invoice as start date, as decided in group chat
|
|
$expiry_date_date_create = $this->wh->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
|
|
$results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number,
|
|
'date_expire' => $expiry_date_date_create]);;
|
|
|
|
if (empty($results))
|
|
{
|
|
$missings[] = [
|
|
'jo_id' => $jo->getID(),
|
|
'plate_number' => $cv->getPlateNumber(),
|
|
'expiry_date' => $expiry_date_date_create->format('Y-m-d'),
|
|
];
|
|
}
|
|
else
|
|
{
|
|
foreach ($results as $warranty)
|
|
{
|
|
$comm_privates[] = [
|
|
'warranty_id' => $warranty->getID(),
|
|
'plate_number' => $warranty->getPlateNumber(),
|
|
'expiry_date' => $warranty->getDateExpire()->format('Y-m-d'),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$em->detach($row[0]);
|
|
$em->clear();
|
|
}
|
|
|
|
$res = [
|
|
'missing' => $missings,
|
|
'invalid_plate_number' => $invalids,
|
|
'commercial_private' => $comm_privates,
|
|
];
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
protected function getWarrantiesWithWrongEntries($start_date, $end_date)
|
|
{
|
|
$em = $this->em;
|
|
|
|
// get all job orders with battery new service type within date range
|
|
$jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end');
|
|
$jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
->setParameter('date_start', $start_date)
|
|
->setParameter('date_end', $end_date);
|
|
|
|
$jos = $jo_query->iterate();
|
|
|
|
foreach($jos as $row)
|
|
{
|
|
$invoice_item = $row[0];
|
|
$invoice = $invoice_item->getInvoice();
|
|
$jo = $invoice->getJobOrder();
|
|
$cv = $jo->getCustomerVehicle();
|
|
$customer = $jo->getCustomer();
|
|
|
|
$em->detach($row[0]);
|
|
$em->clear();
|
|
}
|
|
|
|
}
|
|
|
|
protected function getDuplicateWarranties($start_date, $end_date)
|
|
{
|
|
}
|
|
|
|
protected function outputResults($start_date, $end_date, $missing, $invalid, $commercial_private)
|
|
{
|
|
$date_range = $start_date . '-' . $end_date;
|
|
// output one file per array in csv format
|
|
// missing warranties
|
|
// we output the JOs with no warranties
|
|
$missing_csv_filename = $date_range . '-' . 'missing_warranties.csv';
|
|
try
|
|
{
|
|
$missing_fh = fopen($missing_csv_filename, 'a');
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $missing_csv_filename . '" could not be opened.');
|
|
}
|
|
|
|
if ((count($missing)) > 0)
|
|
{
|
|
fputcsv($missing_fh, [
|
|
'JO ID',
|
|
'Plate Number',
|
|
'Expiry Date',
|
|
]);
|
|
|
|
foreach($missing as $missing_row)
|
|
{
|
|
fputcsv($missing_fh, $missing_row);
|
|
}
|
|
}
|
|
|
|
// invalid plate numbers
|
|
$invalid_plates_csv_filename = $date_range . '-' . 'invalid_plate_jos.csv';
|
|
try
|
|
{
|
|
$invalid_plates_fh = fopen($invalid_plates_csv_filename, 'a');
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $invalid_plates_csv_filename . '" could not be opened.');
|
|
}
|
|
|
|
if ((count($invalid)) > 0)
|
|
{
|
|
fputcsv($invalid_plates_fh, [
|
|
'JO ID',
|
|
'Plate Number',
|
|
'Expiry Date',
|
|
]);
|
|
|
|
foreach($invalid as $invalid_row)
|
|
{
|
|
fputcsv($invalid_plates_fh, $invalid_row);
|
|
}
|
|
}
|
|
|
|
// warranties whose expiration dates were set to the commercial period
|
|
// but warranty class is private
|
|
$comm_private_csv_filename = $date_range . '-' . 'comm_private_warranties.csv';
|
|
try
|
|
{
|
|
$comm_private_fh = fopen($comm_private_csv_filename, 'a');
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $comm_private_csv_filename . '" could not be opened.');
|
|
}
|
|
|
|
if ((count($commercial_private)) > 0)
|
|
{
|
|
fputcsv($comm_private_fh, [
|
|
'Warranty ID',
|
|
'Plate Number',
|
|
'Expiry Date',
|
|
]);
|
|
|
|
foreach($commercial_private as $comm_private_row)
|
|
{
|
|
fputcsv($comm_private_fh, $comm_private_row);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function loadSAPBatteries()
|
|
{
|
|
$this->sapbatt_hash = [];
|
|
|
|
$sap_batteries = $this->em->getRepository(SAPBattery::class)->findAll();
|
|
foreach($sap_batteries as $sap_batt)
|
|
{
|
|
$id = $sap_batt->getID();
|
|
$brand_size = $sap_batt->getBrand()->getID() . " " . $sap_batt->getSize()->getID();
|
|
$this->sapbatt_hash[$id] = $brand_size;
|
|
}
|
|
}
|
|
|
|
protected function findSAPBattery($batt_id)
|
|
{
|
|
if (!isset($this->sapbatt_hash[$batt_id]))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
}
|