281 lines
12 KiB
PHP
281 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use DateTime;
|
|
use DateInterval;
|
|
|
|
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\Service\WarrantyAPILogger;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
use App\Ramcar\WarrantyStatus;
|
|
use App\Ramcar\WarrantySource;
|
|
|
|
use DoctrineExtensions\Query\Mysql\DateFormat;
|
|
|
|
class GenerateWarrantyFromJobOrderCommand extends Command
|
|
{
|
|
protected $em;
|
|
protected $logger;
|
|
protected $sapbatt_hash;
|
|
protected $warranties_hash;
|
|
|
|
public function __construct(EntityManagerInterface $em, WarrantyAPILogger $logger)
|
|
{
|
|
$this->em = $em;
|
|
$this->logger = $logger;
|
|
|
|
$this->loadSAPBatteries();
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('warranty:generate')
|
|
->setDescription('Generates warranty from job order and inserts into database')
|
|
->setHelp('Generate warranty from job order')
|
|
->addArgument('start_date', InputArgument::REQUIRED, 'Start Date')
|
|
->addArgument('end_date', InputArgument::REQUIRED, 'End Date')
|
|
->addArgument('output_filename', InputArgument::REQUIRED, 'Output Filename');
|
|
}
|
|
|
|
protected function computeDateExpire($date_create, $warranty_period)
|
|
{
|
|
$expire_date = clone $date_create;
|
|
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
|
|
return $expire_date;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
protected function findWarranty($plate_number, $expiry_date)
|
|
{
|
|
// find warranty given plate number and expiration date
|
|
$results = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $plate_number, 'date_expire' => $expiry_date]);
|
|
if (empty($results))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$em = $this->em;
|
|
|
|
$s_date = $input->getArgument('start_date');
|
|
$e_date = $input->getArgument('end_date');
|
|
$output_filename = $input->getArgument('output_filename');
|
|
|
|
$start_date = DateTime::createFromFormat('Ymd', $s_date);
|
|
$end_date = DateTime::createFromFormat('Ymd', $e_date);
|
|
$end_date->setTime(23, 59);
|
|
|
|
// to save on joins, go with invoice item first
|
|
$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');
|
|
$query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
->setParameter('date_start', $start_date)
|
|
->setParameter('date_end', $end_date);
|
|
|
|
/*
|
|
$query = $em->createQuery('SELECT jo FROM App\Entity\JobOrder jo
|
|
WHERE jo.service_type = :service_type');
|
|
$query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW);
|
|
*/
|
|
|
|
$result = $query->iterate();
|
|
|
|
$dupe_warranties = [];
|
|
foreach ($result as $row)
|
|
{
|
|
$invoice_item = $row[0];
|
|
$invoice = $invoice_item->getInvoice();
|
|
$jo = $invoice->getJobOrder();
|
|
$cv = $jo->getCustomerVehicle();
|
|
$customer = $jo->getCustomer();
|
|
/*
|
|
$invoice_items = [];
|
|
// for now, one invoice == one battery
|
|
|
|
|
|
$invoice_items = $jo->getInvoice()->getItems();
|
|
$invoice_item = $invoice_items[0];
|
|
*/
|
|
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';
|
|
}
|
|
|
|
// get warranty period for battery
|
|
// TODO: base it on the battery type
|
|
$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())))
|
|
{
|
|
// log with the dupes
|
|
$dupe_warranties[] = [
|
|
'plate_number' => $cv->getPlateNumber(),
|
|
];
|
|
|
|
continue;
|
|
}
|
|
|
|
// check if warranty already exists
|
|
$cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber());
|
|
|
|
//$expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
|
|
// use date_schedule as the starting point of expiry date computation
|
|
$expiry_date_date_schedule = $this->computeDateExpire($jo->getDateSchedule(), $warranty_period);
|
|
$found_warranty_date_schedule = $this->findWarranty($cleaned_plate_number, $expiry_date_date_schedule);
|
|
|
|
// need to check for warranty using invoice date_create because
|
|
// first version of this command used invoice's date_create for expiry date computation
|
|
$expiry_date_date_create = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
|
|
$found_warranty_date_create = $this->findWarranty($cleaned_plate_number, $expiry_date_date_create);
|
|
|
|
if ((!$found_warranty_date_schedule) && (!$found_warranty_date_create))
|
|
{
|
|
$bty_model_id = $invoice_item->getBattery()->getModel()->getID();
|
|
$bty_size_id = $invoice_item->getBattery()->getSize()->getID();
|
|
$warranty_class = $jo->getWarrantyClass();
|
|
|
|
$date = $jo->getInvoice()->getDateCreate();
|
|
$date_purchase = $date->format('Y-m-d');
|
|
|
|
$date_create = date('Y-m-d H:i:s');
|
|
|
|
$date_expire = $expiry_date_date_schedule->format('Y-m-d');
|
|
|
|
$first_name = addslashes(trim($customer->getFirstName()));
|
|
$last_name = addslashes(trim($customer->getLastName()));
|
|
$mobile_number = addslashes(trim($customer->getPhoneMobile()));
|
|
|
|
$values = '(' . $bty_model_id . ',' . $bty_size_id . ',NULL,\'' . $warranty_class . '\',\''
|
|
. $cleaned_plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $date_create . '\',\'' . $date_purchase
|
|
. '\',\'' . $date_expire . '\',NULL,'
|
|
. $sap_code . ',NULL,\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 0 . ',NULL,\''
|
|
. WarrantySource::COMMAND .'\');';
|
|
|
|
$sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,date_claim,sap_bty_id,claim_id,first_name,last_name,mobile_number,flag_activated,warranty_privacy_policy,create_source) VALUES ' . $values . "\n";
|
|
|
|
echo $sql_statement;
|
|
|
|
$db = $this->em->getConnection();
|
|
$stmt = $db->prepare($sql_statement);
|
|
$stmt->execute();
|
|
|
|
// log warranty creation
|
|
$log_data = [
|
|
'battery_model_id' => $bty_model_id,
|
|
'battery_size_id' => $bty_size_id,
|
|
'warranty_class' => $warranty_class,
|
|
'plate_number' => $cleaned_plate_number,
|
|
'date_create' => $date_create,
|
|
'date_purchase' => $date_purchase,
|
|
'date_expire' => $date_expire,
|
|
'sap_code' => $sap_code,
|
|
'first_name' => $first_name,
|
|
'last_name' => $last_name,
|
|
'mobile_number' => $mobile_number,
|
|
];
|
|
$this->logger->logWarrantyInfo($log_data, '', 'internal', 'create', WarrantySource::COMMAND);
|
|
|
|
}
|
|
else
|
|
{
|
|
$expiry_date = '';
|
|
if ($expiry_date_date_create != $expiry_date_date_schedule)
|
|
$expiry_date = $expiry_date_date_create->format('Y-m-d');
|
|
else
|
|
$expiry_date = $expiry_date_date_schedule->format('Y-m-d');
|
|
|
|
$dupe_warranties[] = [
|
|
'plate_number' => $cleaned_plate_number,
|
|
'expiry_date' => $expiry_date,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$em->detach($row[0]);
|
|
$em->clear();
|
|
}
|
|
|
|
// check if dupes were found
|
|
if (count($dupe_warranties) > 0)
|
|
{
|
|
// output file
|
|
$dupe_outfile = fopen($output_filename, 'a');
|
|
|
|
foreach ($dupe_warranties as $dupe)
|
|
{
|
|
if (empty($dupe['expiry_date']))
|
|
fwrite($dupe_outfile, 'Invalid plate number ' . $dupe['plate_number'] . "\n");
|
|
else
|
|
fwrite($dupe_outfile, 'Warranty already exists for ' . $dupe['plate_number'] . ' with expiration date ' . $dupe['expiry_date'] . "\n");
|
|
}
|
|
|
|
fclose($dupe_outfile);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|