Move getWarrantyPeriod into the WarrantyHandler. #286

This commit is contained in:
Korina Cordero 2019-12-16 09:49:48 +00:00
parent bd2ea5fba3
commit 7da009f673
2 changed files with 177 additions and 91 deletions

View file

@ -51,7 +51,7 @@ class ComputeWarrantyExpiryDateCommand extends Command
error_log('Processing warranty for ' . $warr->getID());
$date_purchase = $warr->getDatePurchase();
$warr_period = $this->getWarrantyPeriod($warr);
$warr_period = $this->wh->getWarrantyPeriod($warr);
if ($warr_period != null)
{
@ -71,93 +71,4 @@ class ComputeWarrantyExpiryDateCommand extends Command
}
}
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;
}
}

View file

@ -5,6 +5,9 @@ namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Warranty;
use App\Entity\Battery;
use App\Entity\BatterySize;
use App\Entity\SAPBattery;
use DateTime;
use DateInterval;
@ -22,8 +25,91 @@ class WarrantyHandler
{
}
public function updateWarranty()
public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $battery_id, 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 (!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);
}
}
}
}
}
$purchase_date = $warr->getDatePurchase();
if (empty($purchase_date))
{
if (!empty($date_purchase))
{
$warr->setDatePurchase($date_purchase);
}
$purchase_date = $date_purchase;
}
if (empty($warr->getDateExpire()))
{
$period = getWarrantyPeriod($warr);
$expire_date = $this->computeDateExpire($purchase_date, $period);
$warr->setDateExpire($expire_date);
}
$em->persist($warr);
$em->flush();
}
public function computeDateExpire($date_create, $warranty_period)
@ -32,4 +118,93 @@ class WarrantyHandler
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
return $expire_date;
}
public 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;
}
}