Add WarrantyCleanupCommand. #402

This commit is contained in:
Korina Cordero 2020-05-12 10:53:13 +00:00
parent 4802d6187f
commit aef2279a91

View file

@ -0,0 +1,185 @@
<?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 WarrantyCleanupCommand 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:cleanup')
->setDescription('Cleanup warranties.')
->setHelp('Cleanup warranties')
->addArgument('start_date', InputArgument::REQUIRED, 'Start Date')
->addArgument('end_date', InputArgument::REQUIRED, 'End Date');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
$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);
// 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();
error_log($cv->getPlateNumber());
$clean_plate_num = Warranty::cleanPlateNumber($cv->getPlateNumber());
if (!($clean_plate_num))
{
error_log('invalid plate');
continue;
}
if ($invoice_item == null)
{
error_log('invoice item null');
continue;
}
if ($invoice_item->getBattery() == null)
{
error_log('invoice item not a battery');
continue;
}
error_log('clean plate, invoice item not null, battery not 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';
}
// check if a warranty exists with the plate number and date_schedule
// get date_schedule
$datetime_schedule = $jo->getDateSchedule();
$datetime_schedule_str = $datetime_schedule->format('Y-m-d');
$date_schedule = DateTime::createFromFormat('Y-m-d', $datetime_schedule_str);
$warranties_date_schedule = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_schedule]);
if (empty($warranties_date_schedule))
{
// check if warranty exists with plate number and date_fulfilled
$datetime_fulfilled = $jo->getDateSchedule();
$datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d');
$date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str);
$warranties_date_fulfilled = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_fulfilled]);
if (!(empty($warranties_date_fulfilled)))
{
foreach ($warranties_date_fulfilled as $warranty)
{
// fix expiration date
// get warranty class of JO
$jo_warranty_class = $jo->getWarrantyClass();
$warranty_period = 0;
if ($jo_warranty_class == WarrantyClass::WTY_PRIVATE)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyPrivate();
}
if ($jo_warranty_class == WarrantyClass::WTY_COMMERCIAL)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyCommercial();
}
if ($jo_warranty_class == WarrantyClass::WTY_TNV)
{
$warranty_period = $invoice_item->getBattery()->getWarrantyTnv();
}
// compute expiration date
$expiry_date = $this->wh->computeDateExpire($date_schedule, $warranty_period);
$date_expiry = $expiry_date->format('Y-m-d');
$value = '\' . $date_expiry . \';';
$update_sql_statement = 'UPDATE `warranty` SET date_expire=' . $value . ' WHERE id=\'' . $warranty->getID() . '\'' . "\n";
echo $update_sql_statement;
}
}
else
error_log('no warranty date_fulfilled found.');
}
else
error_log('no warranty date_schedule found.');
$em->detach($row[0]);
$em->clear();
}
}
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;
}
}