resq/src/Command/GenerateWarrantyFromJobOrderCommand.php

215 lines
7.8 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\Ramcar\ServiceType;
use App\Ramcar\WarrantyStatus;
class GenerateWarrantyFromJobOrderCommand extends Command
{
protected $em;
protected $sapbatt_hash;
protected $warranties_hash;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->loadSAPBatteries();
$this->loadWarranties();
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');
}
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 loadWarranties()
{
$this->warranties_hash = [];
/*
$warranties = $this->em->getRepository(Warranty::class)->findAll();
foreach($warranties as $warranty)
{
$plate_number = $warranty->getPlateNumber();
$date_expire = $warranty->getDateExpire();
// skip null date expire
if ($date_expire == null)
continue;
$expiry_date = $date_expire->format('Y-m-d');
$this->warranties_hash[$plate_number][$expiry_date] = $warranty->getID();
}
*/
}
protected function findSAPBattery($batt_id)
{
if (!isset($this->sapbatt_hash[$batt_id]))
{
return false;
}
return true;
}
protected function findWarranty($plate_number, $expiry_date)
{
$date_expire = $expiry_date->format('Y-m-d');
if (!isset($this->warranties_hash[$plate_number][$date_expire]))
{
return false;
}
return true;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
// 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');
$query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW);
/*
$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();
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())))
{
continue;
}
// check if warranty already exists
$cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber());
$expiry_date = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period);
$found_warranty = $this->findWarranty($cleaned_plate_number, $expiry_date);
if (!$found_warranty)
{
$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->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 . '\');';
$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) VALUES ' . $values . "\n";
echo $sql_statement;
}
}
}
}
$em->detach($row[0]);
$em->clear();
}
}
}