77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?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\ORM\EntityManagerInterface;
|
|
|
|
use App\Entity\Battery;
|
|
|
|
use App\Service\WarrantyHandler;
|
|
|
|
class ComputeWarrantyExpiryDateCommand extends Command
|
|
{
|
|
protected $em;
|
|
protected $wh;
|
|
|
|
public function __construct(EntityManagerInterface $em, WarrantyHandler $wh)
|
|
{
|
|
$this->em = $em;
|
|
$this->wh = $wh;
|
|
|
|
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();
|
|
|
|
$batteries = $this->wh->getBatteriesForWarranty($warr);
|
|
if (!empty($batteries))
|
|
{
|
|
$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();
|
|
}
|
|
|
|
$this->em->clear();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|