Merge branch '290-update-customer-vehicle-info-when-warranty-is-added' into 'master'
Resolve "Update customer vehicle info when warranty is added" Closes #290 See merge request jankstudio/resq!332
This commit is contained in:
commit
4bb67ce7b7
5 changed files with 503 additions and 240 deletions
|
|
@ -92,6 +92,10 @@ services:
|
|||
arguments:
|
||||
$geofence_flag: "%env(GEOFENCE_ENABLE)%"
|
||||
|
||||
App\Service\WarrantyHandler:
|
||||
arguments:
|
||||
$em: "@doctrine.orm.entity_manager"
|
||||
|
||||
App\Command\SetCustomerPrivacyPolicyCommand:
|
||||
arguments:
|
||||
$policy_promo: "%env(POLICY_PROMO)%"
|
||||
|
|
|
|||
|
|
@ -9,21 +9,19 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\Warranty;
|
||||
use App\Entity\Battery;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
use App\Service\WarrantyHandler;
|
||||
|
||||
class ComputeWarrantyExpiryDateCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $wh;
|
||||
|
||||
public function __construct(ObjectManager $em)
|
||||
public function __construct(ObjectManager $em, WarrantyHandler $wh)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->wh = $wh;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
|
@ -47,119 +45,31 @@ class ComputeWarrantyExpiryDateCommand extends Command
|
|||
error_log('Processing warranty for ' . $warr->getID());
|
||||
|
||||
$date_purchase = $warr->getDatePurchase();
|
||||
$warr_period = $this->getWarrantyPeriod($warr);
|
||||
|
||||
if ($warr_period != null)
|
||||
$batteries = $this->wh->getBatteriesForWarrantyPeriod($warr);
|
||||
if (!empty($batteries))
|
||||
{
|
||||
$expiry_date = $this->computeDateExpire($date_purchase, $warr_period);
|
||||
}
|
||||
else
|
||||
{
|
||||
$expiry_date = $date_purchase;
|
||||
$warranty_class = $warr->getWarrantyClass();
|
||||
|
||||
$warr_period = $this->wh->getWarrantyPeriod($batteries, $warranty_class);
|
||||
|
||||
if ($warr_period != null)
|
||||
{
|
||||
$expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period);
|
||||
}
|
||||
else
|
||||
{
|
||||
$expiry_date = $date_purchase;
|
||||
}
|
||||
|
||||
// save expiry date
|
||||
$warr->setDateExpire($expiry_date);
|
||||
|
||||
$this->em->persist($warr);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
// save expiry date
|
||||
$warr->setDateExpire($expiry_date);
|
||||
|
||||
$this->em->persist($warr);
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function getWarrantyPeriod($warr)
|
||||
{
|
||||
// find battery via sku/sap battery first
|
||||
// if sku is null, use battery model and battery size to find battery
|
||||
// if all three are null, do nothing
|
||||
|
||||
$batteries = null;
|
||||
|
||||
$sap_battery = $warr->getSAPBattery();
|
||||
$batt_model = $warr->getBatteryModel();
|
||||
$batt_size = $warr->getBatterySize();
|
||||
$warranty_class = $warr->getWarrantyClass();
|
||||
|
||||
if (empty($warranty_class))
|
||||
{
|
||||
error_log('Warranty class is empty for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($sap_battery != null)
|
||||
{
|
||||
// get the battery linked to SAP Battery using sap_battery id
|
||||
$batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($batt_model == null)
|
||||
{
|
||||
error_log('Battery model is null for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
if ($batt_size == null)
|
||||
{
|
||||
error_log('Battery size is null for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
// find battery using battery model and battery size
|
||||
$batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]);
|
||||
}
|
||||
|
||||
if (empty($batteries))
|
||||
{
|
||||
error_log('Battery not found for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
// set to -1 to show that we haven't set a warranty period yet
|
||||
// cannot set initial value to 0 because warranty tnv can be 0
|
||||
$least_warranty = -1;
|
||||
$warr_period = 0;
|
||||
foreach ($batteries as $battery)
|
||||
{
|
||||
// if multiple batteries, get the smallest warranty period
|
||||
// check warranty class to get warranty period
|
||||
if ($warranty_class == WarrantyClass::WTY_PRIVATE)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyPrivate();
|
||||
error_log('Warranty Period for Private: ' . $warr_period);
|
||||
}
|
||||
if ($warranty_class == WarrantyClass::WTY_COMMERCIAL)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyCommercial();
|
||||
error_log('Warranty Period for Commercial: ' . $warr_period);
|
||||
}
|
||||
if ($warranty_class == WarrantyClass::WTY_TNV)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyTnv();
|
||||
error_log('Warranty Period for TNV: ' . $warr_period);
|
||||
}
|
||||
|
||||
if ($least_warranty < 0)
|
||||
{
|
||||
// set least warranty to the first obtained warranty period
|
||||
$least_warranty = $warr_period;
|
||||
}
|
||||
|
||||
if ($least_warranty > $warr_period)
|
||||
{
|
||||
$least_warranty = $warr_period;
|
||||
}
|
||||
}
|
||||
|
||||
$warranty_period = $least_warranty;
|
||||
|
||||
return $warranty_period;
|
||||
}
|
||||
|
||||
protected function computeDateExpire($date_create, $warranty_period)
|
||||
{
|
||||
$expire_date = clone $date_create;
|
||||
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
|
||||
return $expire_date;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
98
src/Command/UpdateCustomerVehicleWarrantyCommand.php
Normal file
98
src/Command/UpdateCustomerVehicleWarrantyCommand.php
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\Warranty;
|
||||
use App\Entity\CustomerVehicle;
|
||||
|
||||
use App\Service\WarrantyHandler;
|
||||
|
||||
class UpdateCustomerVehicleWarrantyCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $wh;
|
||||
|
||||
public function __construct(ObjectManager $em, WarrantyHandler $wh)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->wh = $wh;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customervehicle:updatewarrantyinfo')
|
||||
->setDescription('Update customer vehicle warranty.')
|
||||
->setHelp('Update customer vehicle warranty.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
// get all warranties
|
||||
// since it's possible that the same plate number will have multiple warranties, order them from earliest to latest
|
||||
$warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.plate_number is not null order by w.date_purchase asc');
|
||||
$warranties = $warr_q->iterate();
|
||||
|
||||
foreach ($warranties as $row)
|
||||
{
|
||||
$warr = $row[0];
|
||||
|
||||
// clean plate number
|
||||
$plate_number = $this->wh->cleanPlateNumber($warr->getPlateNumber());
|
||||
|
||||
// get other warranty information
|
||||
$serial = $warr->getSerial();
|
||||
$expiry_date = $warr->getDateExpire();
|
||||
|
||||
// TODO: check length of serial for now. Should not exceed 20.
|
||||
// there is a warranty with 2 serial codes in live
|
||||
// for now, ignore the warranty
|
||||
if (strlen($serial) > 20)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// find battery
|
||||
$batteries = $this->wh->getBatteriesForWarranty($warr);
|
||||
|
||||
// find customer vehicle using plate number
|
||||
/*
|
||||
error_log('Finding customer vehicle with plate number ' . $plate_number);
|
||||
$cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]);
|
||||
|
||||
if (!empty($cust_vehicles))
|
||||
{
|
||||
if (!empty($batteries))
|
||||
{
|
||||
// set current battery to the first battery in list.
|
||||
// there are cases where multiple batteries linked to an SAP code.
|
||||
$battery = $batteries[0];
|
||||
$battery_id = $battery->getID();
|
||||
}
|
||||
$q = $this->em->createQuery('update App\Entity\CustomerVehicle cv
|
||||
set cv.curr_battery = :batt_id,
|
||||
cv.warranty_code = :serial,
|
||||
cv.warranty_expiration = :expiry_date
|
||||
where cv.plate_number = :plate_number')
|
||||
->setParameters([
|
||||
'batt_id' => $battery_id,
|
||||
'serial' => $serial,
|
||||
'expiry_date' => $expiry_date,
|
||||
'plate_number' => $plate_number]);
|
||||
$q->execute();
|
||||
|
||||
} */
|
||||
$this->wh->updateCustomerVehicle($serial, $batteries, $plate_number, $expiry_date);
|
||||
|
||||
$this->em->clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,10 +7,14 @@ use App\Entity\SAPBattery;
|
|||
use App\Entity\Battery;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\Invoice;
|
||||
use App\Entity\CustomerVehicle;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\WarrantyStatus;
|
||||
|
||||
use App\Service\WarrantyHandler;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
|
@ -372,13 +376,14 @@ class WarrantyController extends Controller
|
|||
/**
|
||||
* @Menu(selected="warranty_list")
|
||||
*/
|
||||
public function uploadSubmit(Request $req, EntityManagerInterface $em)
|
||||
public function uploadSubmit(Request $req, EntityManagerInterface $em,
|
||||
WarrantyHandler $wh)
|
||||
{
|
||||
// retrieve temporary info for file
|
||||
$file = $req->files->get('csv_file');
|
||||
|
||||
// process the csv file
|
||||
$inv_entries = $this->processWarrantyFile($file, $em);
|
||||
$inv_entries = $this->processWarrantyFile($file, $em, $wh);
|
||||
|
||||
$resp = new StreamedResponse();
|
||||
$resp->setCallback(function() use($inv_entries) {
|
||||
|
|
@ -422,7 +427,8 @@ class WarrantyController extends Controller
|
|||
return $resp;
|
||||
}
|
||||
|
||||
protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em)
|
||||
protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em,
|
||||
WarrantyHandler $wh)
|
||||
{
|
||||
// attempt to open file
|
||||
try
|
||||
|
|
@ -473,8 +479,9 @@ class WarrantyController extends Controller
|
|||
$serial = trim($fields[10]);
|
||||
$purchase_date = trim($fields[12]);
|
||||
$battery_id = trim($fields[16]);
|
||||
$batt_invoice = trim($fields[11]);
|
||||
|
||||
$plate_number = $this->cleanPlateNumber($plate);
|
||||
$plate_number = $wh->cleanPlateNumber($plate);
|
||||
|
||||
// check if purchase_date or plate_number or serial is empty or if
|
||||
// purchase date is valid
|
||||
|
|
@ -511,7 +518,7 @@ class WarrantyController extends Controller
|
|||
'vehicle_year' => trim($fields[8]),
|
||||
'vehicle_plate_number' => $plate_number,
|
||||
'battery_serial_number' => $serial,
|
||||
'battery_sales_invoice' => trim($fields[11]),
|
||||
'battery_sales_invoice' => $batt_invoice,
|
||||
'battery_date_purchase' => $purchase_date,
|
||||
'distributor_name' => trim($fields[13]),
|
||||
'distributor_address' => trim($fields[14]),
|
||||
|
|
@ -526,81 +533,42 @@ class WarrantyController extends Controller
|
|||
// additional validation
|
||||
// check if serial number and plate number already exists
|
||||
$warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]);
|
||||
|
||||
// get battery via the invoice because battery_id doesn't match what's in the live data
|
||||
// get job order via invoice to get the warranty class
|
||||
$warranty_class = '';
|
||||
$batt_list = array();
|
||||
|
||||
// find invoice
|
||||
$invoice = $em->getRepository(Invoice::class)->find($batt_invoice);
|
||||
if (!empty($invoice))
|
||||
{
|
||||
// get job order
|
||||
$jo = $invoice->getJobOrder();
|
||||
|
||||
// get warranty class
|
||||
$warranty_class = $jo->getWarrantyClass();
|
||||
|
||||
// get battery
|
||||
$invoice_items = $invoice->getItems();
|
||||
foreach ($invoice_items as $item)
|
||||
{
|
||||
$battery = $item->getBattery();
|
||||
if ($battery != null)
|
||||
{
|
||||
$batt_list[] = $item->getBattery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($warr_results))
|
||||
{
|
||||
foreach($warr_results as $warr)
|
||||
{
|
||||
// check if details are complete
|
||||
//error_log('Updating warranty with serial number ' . $serial . ' and plate number ' . $plate_number);
|
||||
if (empty($warr->getFirstName()))
|
||||
{
|
||||
if (!empty($first_name))
|
||||
{
|
||||
$warr->setFirstName($first_name);
|
||||
}
|
||||
}
|
||||
if (empty($warr->getLastName()))
|
||||
{
|
||||
if (!empty($last_name))
|
||||
{
|
||||
$warr->setLastName($last_name);
|
||||
}
|
||||
}
|
||||
if (empty($warr->getMobileNumber()))
|
||||
{
|
||||
if (!empty($mobile_number))
|
||||
{
|
||||
$warr->setMobileNumber($mobile_number);
|
||||
}
|
||||
}
|
||||
if ((empty($warr->getBatteryModel())) ||
|
||||
(empty($warr->getBatterySize())))
|
||||
{
|
||||
if (!empty($battery_id))
|
||||
{
|
||||
// find battery
|
||||
$battery = $em->getRepository(Battery::class)->find($battery_id);
|
||||
if (!empty($battery))
|
||||
{
|
||||
// get the battery model and battery size
|
||||
$model_id = $battery->getModel()->getID();
|
||||
$size_id = $battery->getSize()->getID();
|
||||
|
||||
$bty_model = $em->getRepository(BatteryModel::class)->find($model_id);
|
||||
$bty_size = $em->getRepository(BatterySize::class)->find($size_id);
|
||||
|
||||
if ($bty_model != null)
|
||||
{
|
||||
$warr->setBatteryModel($bty_model);
|
||||
}
|
||||
if ($bty_size != null)
|
||||
{
|
||||
$warr->setBatterySize($bty_size);
|
||||
}
|
||||
$sap_code = $battery->getSAPCode();
|
||||
if (!empty($sap_code))
|
||||
{
|
||||
// find sap battery
|
||||
$sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code);
|
||||
if (!empty($sap_batt))
|
||||
{
|
||||
$warr->setSAPBattery($sap_batt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($warr->getDatePurchase()))
|
||||
{
|
||||
if (!empty($date_purchase))
|
||||
{
|
||||
$warr->setDatePurchase($date_purchase);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: compute expiry date
|
||||
$em->persist($warr);
|
||||
$em->flush();
|
||||
// call service to check if warranty details is incomplete and then update warranty
|
||||
// using details from csv file
|
||||
error_log('Updating warranty for ' . $warr->getID());
|
||||
$wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -614,59 +582,13 @@ class WarrantyController extends Controller
|
|||
continue;
|
||||
}
|
||||
|
||||
//error_log('Adding warranty with serial number ' . $serial . ' and plate number ' . $plate_number);
|
||||
// new warranty
|
||||
$warranty = new Warranty();
|
||||
error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number);
|
||||
|
||||
// get the battery purchased
|
||||
// check battery first. If not found, check sap_battery
|
||||
$battery = $em->getRepository(Battery::class)->find($battery_id);
|
||||
if ($battery != null)
|
||||
{
|
||||
// get the battery model and battery size
|
||||
$model_id = $battery->getModel()->getID();
|
||||
$size_id = $battery->getSize()->getID();
|
||||
|
||||
$bty_model = $em->getRepository(BatteryModel::class)->find($model_id);
|
||||
$bty_size = $em->getRepository(BatterySize::class)->find($size_id);
|
||||
|
||||
if ($bty_model != null)
|
||||
{
|
||||
$warranty->setBatteryModel($bty_model);
|
||||
}
|
||||
|
||||
if ($bty_size != null)
|
||||
{
|
||||
$warranty->setBatterySize($bty_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// find battery in sap_battery
|
||||
$battery = $em->getRepository(SAPBattery::class)->find($battery_id);
|
||||
if ($battery != null)
|
||||
{
|
||||
// battery is SAPBattery
|
||||
$warranty->setSAPBattery($battery);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: compute expiry date
|
||||
|
||||
// set and save values
|
||||
$warranty->setSerial($serial)
|
||||
->setPlateNumber($plate_number)
|
||||
->setFirstName($first_name)
|
||||
->setLastName($last_name)
|
||||
->setMobileNumber($mobile_number)
|
||||
->setDatePurchase($date_purchase);
|
||||
|
||||
$em->persist($warranty);
|
||||
$em->flush();
|
||||
$wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
$em->clear();
|
||||
$row_num++;
|
||||
}
|
||||
|
||||
|
|
|
|||
329
src/Service/WarrantyHandler.php
Normal file
329
src/Service/WarrantyHandler.php
Normal file
|
|
@ -0,0 +1,329 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\Entity\Warranty;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\SAPBattery;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\CustomerVehicle;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
||||
class WarrantyHandler
|
||||
{
|
||||
protected $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number,
|
||||
$batt_list, DateTime $date_purchase, $warranty_class)
|
||||
{
|
||||
// new warranty
|
||||
$warranty = new Warranty();
|
||||
|
||||
foreach ($batt_list as $battery)
|
||||
{
|
||||
// get the battery model and battery size
|
||||
$model_id = $battery->getModel()->getID();
|
||||
$size_id = $battery->getSize()->getID();
|
||||
|
||||
$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id);
|
||||
$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id);
|
||||
|
||||
if ($bty_model != null)
|
||||
{
|
||||
$warranty->setBatteryModel($bty_model);
|
||||
}
|
||||
if ($bty_size != null)
|
||||
{
|
||||
$warranty->setBatterySize($bty_size);
|
||||
}
|
||||
|
||||
$sap_code = $battery->getSAPCode();
|
||||
if (!empty($sap_code))
|
||||
{
|
||||
// find sap battery
|
||||
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
||||
if ($sap_battery != null)
|
||||
{
|
||||
$warranty->setSAPBattery($sap_battery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compute expiry date
|
||||
$date_expire = null;
|
||||
if ((!empty($warranty_class)) &&
|
||||
(count($batt_list) != 0))
|
||||
{
|
||||
$period = $this->getWarrantyPeriod($batt_list, $warranty_class);
|
||||
$date_expire = $this->computeDateExpire($date_purchase, $period);
|
||||
|
||||
$warranty->setDateExpire($date_expire);
|
||||
}
|
||||
|
||||
// set and save values
|
||||
$warranty->setSerial($serial)
|
||||
->setPlateNumber($plate_number)
|
||||
->setFirstName($first_name)
|
||||
->setLastName($last_name)
|
||||
->setMobileNumber($mobile_number)
|
||||
->setDatePurchase($date_purchase);
|
||||
|
||||
$this->em->persist($warranty);
|
||||
$this->em->flush();
|
||||
|
||||
// update customer vehicle with warranty info
|
||||
$this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire);
|
||||
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire)
|
||||
{
|
||||
// find customer vehicle using plate number
|
||||
error_log('Finding customer vehicle with plate number ' . $plate_number);
|
||||
$cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]);
|
||||
$battery_id = null;
|
||||
if (!empty($cust_vehicles))
|
||||
{
|
||||
if (!empty($batteries))
|
||||
{
|
||||
// set current battery to the first battery in list.
|
||||
// there are cases where multiple batteries linked to an SAP code.
|
||||
$battery = $batteries[0];
|
||||
$battery_id = $battery->getID();
|
||||
}
|
||||
//error_log('Serial/Warranty Code = ' . $serial);
|
||||
$q = $this->em->createQuery('update App\Entity\CustomerVehicle cv
|
||||
set cv.curr_battery = :batt_id,
|
||||
cv.warranty_code = :serial,
|
||||
cv.warranty_expiration = :expiry_date
|
||||
where cv.plate_number = :plate_number')
|
||||
->setParameters([
|
||||
'batt_id' => $battery_id,
|
||||
'serial' => $serial,
|
||||
'expiry_date' => $date_expire,
|
||||
'plate_number' => $plate_number]);
|
||||
$q->execute();
|
||||
}
|
||||
}
|
||||
|
||||
public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase)
|
||||
{
|
||||
// TODO: add serial and plate number to update
|
||||
// TODO: check if data from existing warranty matches the new data
|
||||
// check if details are complete
|
||||
if (empty($warr->getFirstName()))
|
||||
{
|
||||
if (!empty($first_name))
|
||||
{
|
||||
$warr->setFirstName($first_name);
|
||||
}
|
||||
}
|
||||
if (empty($warr->getLastName()))
|
||||
{
|
||||
if (!empty($last_name))
|
||||
{
|
||||
$warr->setLastName($last_name);
|
||||
}
|
||||
}
|
||||
if (empty($warr->getMobileNumber()))
|
||||
{
|
||||
if (!empty($mobile_number))
|
||||
{
|
||||
$warr->setMobileNumber($mobile_number);
|
||||
}
|
||||
}
|
||||
if ((empty($warr->getBatteryModel())) ||
|
||||
(empty($warr->getBatterySize())))
|
||||
{
|
||||
if (count($batt_list) != 0)
|
||||
{
|
||||
foreach ($batt_list as $battery)
|
||||
{
|
||||
// get the battery model and battery size
|
||||
$model_id = $battery->getModel()->getID();
|
||||
$size_id = $battery->getSize()->getID();
|
||||
|
||||
$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id);
|
||||
$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id);
|
||||
|
||||
if ($bty_model != null)
|
||||
{
|
||||
$warranty->setBatteryModel($bty_model);
|
||||
}
|
||||
if ($bty_size != null)
|
||||
{
|
||||
$warranty->setBatterySize($bty_size);
|
||||
}
|
||||
|
||||
$sap_code = $battery->getSAPCode();
|
||||
if (!empty($sap_code))
|
||||
{
|
||||
// find sap battery
|
||||
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
||||
if ($sap_battery != null)
|
||||
{
|
||||
$warranty->setSAPBattery($sap_battery);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$purchase_date = $warr->getDatePurchase();
|
||||
if (empty($purchase_date))
|
||||
{
|
||||
if (!empty($date_purchase))
|
||||
{
|
||||
$warr->setDatePurchase($date_purchase);
|
||||
}
|
||||
$purchase_date = $date_purchase;
|
||||
}
|
||||
|
||||
if (empty($warr->getDateExpire()))
|
||||
{
|
||||
$batteries = [];
|
||||
if (count($batt_list) == 0)
|
||||
{
|
||||
$batteries = $this->getBatteriesForWarranty($warr);
|
||||
}
|
||||
else
|
||||
{
|
||||
$batteries = $batt_list;
|
||||
}
|
||||
|
||||
if (!empty($batteries))
|
||||
{
|
||||
$period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass());
|
||||
if (!empty($purchase_date))
|
||||
{
|
||||
$expire_date = $this->computeDateExpire($purchase_date, $period);
|
||||
$warr->setDateExpire($expire_date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->persist($warr);
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
public function computeDateExpire($date_create, $warranty_period)
|
||||
{
|
||||
$expire_date = clone $date_create;
|
||||
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
|
||||
return $expire_date;
|
||||
}
|
||||
|
||||
public function getWarrantyPeriod($batteries, $warranty_class)
|
||||
{
|
||||
// set to -1 to show that we haven't set a warranty period yet
|
||||
// cannot set initial value to 0 because warranty tnv can be 0
|
||||
$least_warranty = -1;
|
||||
$warr_period = 0;
|
||||
foreach ($batteries as $battery)
|
||||
{
|
||||
// if multiple batteries, get the smallest warranty period
|
||||
// check warranty class to get warranty period
|
||||
if ($warranty_class == WarrantyClass::WTY_PRIVATE)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyPrivate();
|
||||
//error_log('Warranty Period for Private: ' . $warr_period);
|
||||
}
|
||||
if ($warranty_class == WarrantyClass::WTY_COMMERCIAL)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyCommercial();
|
||||
//error_log('Warranty Period for Commercial: ' . $warr_period);
|
||||
}
|
||||
if ($warranty_class == WarrantyClass::WTY_TNV)
|
||||
{
|
||||
$warr_period = $battery->getWarrantyTnv();
|
||||
//error_log('Warranty Period for TNV: ' . $warr_period);
|
||||
}
|
||||
|
||||
if ($least_warranty < 0)
|
||||
{
|
||||
// set least warranty to the first obtained warranty period
|
||||
$least_warranty = $warr_period;
|
||||
}
|
||||
|
||||
if ($least_warranty > $warr_period)
|
||||
{
|
||||
$least_warranty = $warr_period;
|
||||
}
|
||||
}
|
||||
|
||||
$warranty_period = $least_warranty;
|
||||
|
||||
return $warranty_period;
|
||||
}
|
||||
|
||||
public function getBatteriesForWarranty($warr)
|
||||
{
|
||||
// find battery via sku/sap battery first
|
||||
// if sku is null, use battery model and battery size to find battery
|
||||
// if all three are null, do nothing
|
||||
$batteries = null;
|
||||
|
||||
$sap_battery = $warr->getSAPBattery();
|
||||
$batt_model = $warr->getBatteryModel();
|
||||
$batt_size = $warr->getBatterySize();
|
||||
$warranty_class = $warr->getWarrantyClass();
|
||||
|
||||
if (empty($warranty_class))
|
||||
{
|
||||
error_log('Warranty class is empty for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($sap_battery != null)
|
||||
{
|
||||
// get the battery linked to SAP Battery using sap_battery id
|
||||
$batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($batt_model == null)
|
||||
{
|
||||
error_log('Battery model is null for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
if ($batt_size == null)
|
||||
{
|
||||
error_log('Battery size is null for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
// find battery using battery model and battery size
|
||||
$batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]);
|
||||
}
|
||||
|
||||
if (empty($batteries))
|
||||
{
|
||||
error_log('Battery not found for warranty id ' . $warr->getID());
|
||||
return null;
|
||||
}
|
||||
|
||||
return $batteries;
|
||||
}
|
||||
|
||||
|
||||
public function cleanPlateNumber($plate)
|
||||
{
|
||||
// remove spaces and make upper case
|
||||
return strtoupper(str_replace(' ', '', $plate));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue