resq/src/Command/UpdateCustomerVehicleWarrantyCommand.php

85 lines
2.8 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\CustomerVehicle;
use App\Service\WarrantyHandler;
class UpdateCustomerVehicleWarrantyCommand 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('customervehicle:updatewarrantyinfo')
->setDescription('Update customer vehicle warranty.')
->setHelp('Update customer vehicle warranty.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// get all warranties
// since it's possible that the same plate number will have multiple warranties, order them from earliest to latest
$warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.plate_number is not null order by w.date_purchase asc');
$warranties = $warr_q->iterate();
foreach ($warranties as $row)
{
$warr = $row[0];
// clean plate number
$plate_number = $this->wh->cleanPlateNumber($warr->getPlateNumber());
// get other warranty information
$serial = $warr->getSerial();
$expiry_date = $warr->getDateExpire();
// find battery
$batteries = $this->wh->getBatteriesForWarrantyPeriod($warr);
// find customer vehicle using plate number
error_log('Finding customer vehicle with plate number ' . $plate_number);
$cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]);
if (!empty($cust_vehicles))
{
foreach ($cust_vehicles as $cv)
{
if (!empty($batteries))
{
// set current battery to the last battery in list.
// there are cases where multiple batteries linked to an SAP code.
foreach ($batteries as $batt)
{
$cv->setCurrentBattery($batt);
}
}
$cv->setWarrantyCode($serial)
->setWarrantyExpiration($expiry_date);
$this->em->persist($cv);
$this->em->flush();
}
}
$this->em->clear();
}
}
}