Add command to compute expiry date for warranty. #280
This commit is contained in:
parent
ec5b8951da
commit
6daba0d305
2 changed files with 108 additions and 1 deletions
108
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
108
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
<?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\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('Comput 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);
|
||||
|
||||
// save expiry date
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function getWarrantyPeriod($warr)
|
||||
{
|
||||
$batt_model = $warr->getBatteryModel();
|
||||
|
||||
$warranty_class = $warr->getWarrantyClass();
|
||||
$warr_period = '';
|
||||
|
||||
if (($batt_model == null) ||
|
||||
(empty($warranty_class)))
|
||||
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($batt_model != null)
|
||||
{
|
||||
$batteries = $batt_model->getBatteries();
|
||||
foreach($batteries as $battery)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $warr_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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue