resq/src/Command/ComputeWarrantyExpiryDateCommand.php

74 lines
1.9 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\Common\Persistence\ObjectManager;
use App\Entity\Warranty;
use App\Entity\Battery;
use App\Service\WarrantyHandler;
use App\Ramcar\WarrantyClass;
use DateTime;
use DateInterval;
class ComputeWarrantyExpiryDateCommand extends Command
{
protected $em;
protected $wh;
public function __construct(ObjectManager $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();
$warr_period = $this->wh->getWarrantyPeriod($warr);
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();
}
}
}