Fix testing issues found for warranty handler. #286
This commit is contained in:
parent
0d3a96e038
commit
a10ef2b5d9
3 changed files with 205 additions and 131 deletions
|
|
@ -9,6 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\Battery;
|
||||
|
||||
use App\Service\WarrantyHandler;
|
||||
|
||||
class ComputeWarrantyExpiryDateCommand extends Command
|
||||
|
|
@ -43,24 +45,31 @@ class ComputeWarrantyExpiryDateCommand extends Command
|
|||
error_log('Processing warranty for ' . $warr->getID());
|
||||
|
||||
$date_purchase = $warr->getDatePurchase();
|
||||
$warr_period = $this->wh->getWarrantyPeriod($warr);
|
||||
|
||||
if ($warr_period != null)
|
||||
$batteries = $this->wh->getBatteriesForWarrantyPeriod($warr);
|
||||
if (!empty($batteries))
|
||||
{
|
||||
$expiry_date = $this->wh->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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use App\Entity\SAPBattery;
|
|||
use App\Entity\Battery;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\Invoice;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\WarrantyStatus;
|
||||
|
|
@ -477,8 +478,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
|
||||
|
|
@ -515,7 +517,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]),
|
||||
|
|
@ -530,13 +532,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)
|
||||
{
|
||||
// call service to check if warranty details is incomplete and then update warranty
|
||||
// using details from csv file
|
||||
$wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $battery_id, $date_purchase);
|
||||
//error_log('Updating warranty for ' . $warr->getID());
|
||||
$wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -550,57 +581,10 @@ 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$row_num++;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use App\Entity\Warranty;
|
|||
use App\Entity\Battery;
|
||||
use App\Entity\BatterySize;
|
||||
use App\Entity\SAPBattery;
|
||||
use App\Entity\BatteryModel;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
|
|
@ -23,11 +24,66 @@ class WarrantyHandler
|
|||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function createWarranty()
|
||||
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
|
||||
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();
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $battery_id, DateTime $date_purchase)
|
||||
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
|
||||
|
|
@ -56,35 +112,34 @@ class WarrantyHandler
|
|||
if ((empty($warr->getBatteryModel())) ||
|
||||
(empty($warr->getBatterySize())))
|
||||
{
|
||||
if (!empty($battery_id))
|
||||
if (count($batt_list) != 0)
|
||||
{
|
||||
// find battery
|
||||
$battery = $em->getRepository(Battery::class)->find($battery_id);
|
||||
if (!empty($battery))
|
||||
foreach ($batt_list as $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);
|
||||
$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id);
|
||||
$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id);
|
||||
|
||||
if ($bty_model != null)
|
||||
{
|
||||
$warr->setBatteryModel($bty_model);
|
||||
$warranty->setBatteryModel($bty_model);
|
||||
}
|
||||
if ($bty_size != null)
|
||||
{
|
||||
$warr->setBatterySize($bty_size);
|
||||
$warranty->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))
|
||||
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
||||
if ($sap_battery != null)
|
||||
{
|
||||
$warr->setSAPBattery($sap_batt);
|
||||
$warranty->setSAPBattery($sap_battery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -100,18 +155,33 @@ class WarrantyHandler
|
|||
}
|
||||
$purchase_date = $date_purchase;
|
||||
}
|
||||
|
||||
if (empty($warr->getDateExpire()))
|
||||
{
|
||||
$period = getWarrantyPeriod($warr);
|
||||
$expire_date = $this->computeDateExpire($purchase_date, $period);
|
||||
$batteries = [];
|
||||
if (count($batt_list) == 0)
|
||||
{
|
||||
$batteries = $this->getBatteriesForWarrantyPeriod($warr);
|
||||
}
|
||||
else
|
||||
{
|
||||
$batteries = $batt_list;
|
||||
}
|
||||
|
||||
$warr->setDateExpire($expire_date);
|
||||
if (!empty($batteries))
|
||||
{
|
||||
$period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass());
|
||||
if (!empty($purchase_date))
|
||||
{
|
||||
$expire_date = $this->computeDateExpire($purchase_date, $period);
|
||||
$warr->setDateExpire($expire_date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$em->persist($warr);
|
||||
$em->flush();
|
||||
|
||||
|
||||
$this->em->persist($warr);
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
public function computeDateExpire($date_create, $warranty_period)
|
||||
|
|
@ -121,53 +191,8 @@ class WarrantyHandler
|
|||
return $expire_date;
|
||||
}
|
||||
|
||||
public function getWarrantyPeriod($warr)
|
||||
public function getWarrantyPeriod($batteries, $warranty_class)
|
||||
{
|
||||
// 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;
|
||||
|
|
@ -209,4 +234,60 @@ class WarrantyHandler
|
|||
return $warranty_period;
|
||||
}
|
||||
|
||||
public function getBatteriesForWarrantyPeriod($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