Create command to update the battery of the customer vehicle to the battery last purchased. #204
This commit is contained in:
parent
fd6e617f95
commit
24715196e5
1 changed files with 67 additions and 0 deletions
67
src/Command/UpdateCustomerVehicleBatteryCommand.php
Normal file
67
src/Command/UpdateCustomerVehicleBatteryCommand.php
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<?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\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\JobOrder;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
|
||||
class UpdateCustomerVehicleBatteryCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $custvehicle_hash;
|
||||
|
||||
public function __construct(ObjectManager $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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue