resq/src/Command/UpdateCustomerVehicleBatteryCommand.php

67 lines
2.3 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Ramcar\JOStatus;
class UpdateCustomerVehicleBatteryCommand extends Command
{
protected $em;
protected $custvehicle_hash;
public function __construct(EntityManagerInterface $om)
{
$this->em = $om;
$this->custvehicle_hash = [];
parent::__construct();
}
protected function configure()
{
$this->setName('joborder:updatebattery')
->setDescription('Update customer vehicle battery information.')
->setHelp('Updates the customer vehicle battery based on the latest battery purchased. ');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// get all job orders with fulfilled status
$jos = $this->em->getRepository(JobOrder::class)->findBy(['status' => JOStatus::FULFILLED], ['date_create' => 'DESC']);
foreach ($jos as $jo)
{
if ($jo->getCustomerVehicle() != null)
{
$custvehicle_id = $jo->getCustomerVehicle()->getID();
// check if custvehicle is in hash. This would mean that the battery has already been set
if(!isset($this->custvehicle_hash[$custvehicle_id]))
{
// get battery purchased from job order
$items = $jo->getInvoice()->getItems();
foreach($items as $item)
{
if ($item->getBattery() != null)
{
// set battery of vehicle to the latest battery purchased
$new_battery = $item->getBattery();
$jo->getCustomerVehicle()->setCurrentBattery($new_battery);
// add customer vehicle to hash
$this->custvehicle_hash[$custvehicle_id] = true;
}
}
$this->em->flush();
}
}
}
}
}