350 lines
11 KiB
PHP
350 lines
11 KiB
PHP
<?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\Service\WarrantyAPILogger;
|
|
|
|
use App\Ramcar\WarrantyClass;
|
|
use App\Ramcar\WarrantyStatus;
|
|
|
|
use DateTime;
|
|
use DateInterval;
|
|
|
|
class WarrantyHandler
|
|
{
|
|
protected $em;
|
|
protected $logger;
|
|
|
|
public function __construct(EntityManagerInterface $em, WarrantyAPILogger $logger)
|
|
{
|
|
$this->em = $em;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number,
|
|
$batt_list, DateTime $date_purchase, $warranty_class, $user_id,
|
|
$source, $customer, $cust_vehicle)
|
|
{
|
|
$bty_model = null;
|
|
$bty_size = null;
|
|
$sap_batt = null;
|
|
$w_serial = '';
|
|
|
|
foreach ($batt_list as $battery)
|
|
{
|
|
// get the battery model and battery size
|
|
$bty_model = $battery->getModel();
|
|
$bty_size = $battery->getSize();
|
|
|
|
if ($bty_model != null)
|
|
{
|
|
$bmodel_id = $bty_model->getID();
|
|
$bmodel_name = $bty_model->getName();
|
|
}
|
|
if ($bty_size != null)
|
|
{
|
|
$bsize_id = $bty_size->getID();
|
|
$bsize_name = $bty_size->getName();
|
|
}
|
|
|
|
$sap_code = $battery->getSAPCode();
|
|
if (!empty($sap_code))
|
|
{
|
|
// find sap battery
|
|
$sap_batt = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// set and save values
|
|
if (trim($serial) != '')
|
|
$w_serial = $serial;
|
|
|
|
$warranty = new Warranty();
|
|
$warranty->setWarrantyClass($warranty_class)
|
|
->setPlateNumber($plate_number)
|
|
->setFirstName($first_name)
|
|
->setLastName($last_name)
|
|
->setMobileNumber($mobile_number)
|
|
->setBatteryModel($bty_model)
|
|
->setBatterySize($bty_size)
|
|
->setSAPBattery($sap_batt)
|
|
->setDatePurchase($date_purchase)
|
|
->setCustomer($customer)
|
|
->setVehicle($cust_vehicle)
|
|
->setCreateSource($source);
|
|
|
|
// set serial
|
|
if (!empty($w_serial))
|
|
$warranty->setSerial($w_serial);
|
|
|
|
// set date expire
|
|
if ($date_expire != null)
|
|
$warranty->setDateExpire($date_expire);
|
|
|
|
$this->em->persist($warranty);
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
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);
|
|
$cv_q = $this->em->createQuery('select count(cv) from App\Entity\CustomerVehicle cv where cv.plate_number = :plate_number')
|
|
->setParameter('plate_number', $plate_number);
|
|
$cv_result = $cv_q->getSingleScalarResult();
|
|
|
|
$battery_id = null;
|
|
if ($cv_result != 0)
|
|
{
|
|
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,
|
|
cv.flag_motolite_battery = :flag_motolite_battery
|
|
where cv.plate_number = :plate_number')
|
|
->setParameters([
|
|
'batt_id' => $battery_id,
|
|
'serial' => $serial,
|
|
'expiry_date' => $date_expire,
|
|
'flag_motolite_battery' => true,
|
|
'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)
|
|
{
|
|
// TODO: make this more like Warranty's static cleanPlateNumber?
|
|
// remove spaces and make upper case
|
|
return strtoupper(str_replace(' ', '', $plate));
|
|
}
|
|
|
|
}
|