Use SKU to find the battery first. #280

This commit is contained in:
Korina Cordero 2019-12-12 08:33:29 +00:00
parent 243d238ad7
commit c1203368c0

View file

@ -48,6 +48,7 @@ class ComputeWarrantyExpiryDateCommand extends Command
$date_purchase = $warr->getDatePurchase(); $date_purchase = $warr->getDatePurchase();
$warr_period = $this->getWarrantyPeriod($warr); $warr_period = $this->getWarrantyPeriod($warr);
if ($warr_period != null) if ($warr_period != null)
{ {
$expiry_date = $this->computeDateExpire($date_purchase, $warr_period); $expiry_date = $this->computeDateExpire($date_purchase, $warr_period);
@ -69,30 +70,44 @@ class ComputeWarrantyExpiryDateCommand extends Command
protected function getWarrantyPeriod($warr) 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_model = $warr->getBatteryModel();
$batt_size = $warr->getBatterySize(); $batt_size = $warr->getBatterySize();
$warranty_class = $warr->getWarrantyClass(); $warranty_class = $warr->getWarrantyClass();
$warr_period = 0;
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;
}
if (empty($warranty_class)) if (empty($warranty_class))
{ {
error_log('Warranty class is empty for warranty id ' . $warr->getID()); error_log('Warranty class is empty for warranty id ' . $warr->getID());
return null; return null;
} }
// find batttery using model and size if ($sap_battery != null)
$batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); {
// 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)) if (empty($batteries))
{ {
@ -100,8 +115,13 @@ class ComputeWarrantyExpiryDateCommand extends Command
return null; return null;
} }
foreach($batteries as $battery) // 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 // check warranty class to get warranty period
if ($warranty_class == WarrantyClass::WTY_PRIVATE) if ($warranty_class == WarrantyClass::WTY_PRIVATE)
{ {
@ -118,9 +138,22 @@ class ComputeWarrantyExpiryDateCommand extends Command
$warr_period = $battery->getWarrantyTnv(); $warr_period = $battery->getWarrantyTnv();
error_log('Warranty Period for TNV: ' . $warr_period); 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;
}
} }
return $warr_period; $warranty_period = $least_warranty;
return $warranty_period;
} }
protected function computeDateExpire($date_create, $warranty_period) protected function computeDateExpire($date_create, $warranty_period)
@ -129,5 +162,4 @@ class ComputeWarrantyExpiryDateCommand extends Command
$expire_date->add(new DateInterval('P'.$warranty_period.'M')); $expire_date->add(new DateInterval('P'.$warranty_period.'M'));
return $expire_date; return $expire_date;
} }
} }