Merge branch '280-expiration-computation-for-warranties' into 'master'
Resolve "Expiration computation for warranties" Closes #280 See merge request jankstudio/resq!326
This commit is contained in:
commit
1008d73089
4 changed files with 167 additions and 1 deletions
165
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
165
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
<?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\Battery;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
||||
class ComputeWarrantyExpiryDateCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
|
||||
public function __construct(ObjectManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('warranty:computeexpirydate')
|
||||
->setDescription('Compute expiry date for existing warranties.')
|
||||
->setHelp('Compute expiry date for existing warranties.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.date_expire is null');
|
||||
$warranties = $warr_q->iterate();
|
||||
|
||||
foreach($warranties as $row)
|
||||
{
|
||||
$warr = $row[0];
|
||||
|
||||
error_log('Processing warranty for ' . $warr->getID());
|
||||
|
||||
$date_purchase = $warr->getDatePurchase();
|
||||
$warr_period = $this->getWarrantyPeriod($warr);
|
||||
|
||||
if ($warr_period != null)
|
||||
{
|
||||
$expiry_date = $this->computeDateExpire($date_purchase, $warr_period);
|
||||
}
|
||||
else
|
||||
{
|
||||
$expiry_date = $date_purchase;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputOption;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@ class BatteryModel
|
|||
|
||||
public function getBatteries()
|
||||
{
|
||||
// TODO: fix this to be a proper getter function
|
||||
// has to return set of strings because symfony is trying to move away from role objects
|
||||
$str_batteries = [];
|
||||
foreach ($this->batteries as $battery)
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@ class BatterySize
|
|||
|
||||
public function getBatteries()
|
||||
{
|
||||
// TODO: fix this to be a proper getter function
|
||||
// has to return set of strings because symfony is trying to move away from role objects
|
||||
$str_batteries = [];
|
||||
foreach ($this->batteries as $battery)
|
||||
|
|
|
|||
Loading…
Reference in a new issue