Merge branch '204-set-customer-vehicle-current-battery' into 'master'

Resolve "Set customer vehicle current battery"

Closes #204

See merge request jankstudio/resq!238
This commit is contained in:
Kendrick Chan 2019-05-27 02:06:46 +00:00
commit e6239eb053
3 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,40 @@
<?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 App\Service\JobOrderManager;
class TestJobOrderManagerCommand extends Command
{
protected function configure()
{
$this->setName('test:fulfilljoborder')
->setDescription('Test fulfill job order service.')
->setHelp('Test the fulfill job order service.')
->addArgument('job_order_id', InputArgument::REQUIRED, 'Job Order ID');
}
public function __construct(JobOrderManager $jo_manager)
{
$this->jo_manager = $jo_manager;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$jo_id = $input->getArgument('job_order_id');
$result = $this->jo_manager->fulfillJobOrder($jo_id);
if ($result)
echo "Job Order successfully updated" . "\n";
else
echo "Job Order not updated" . "\n";
}
}

View 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();
}
}
}
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace App\Service;
use App\Entity\JobOrder;
use App\Ramcar\ServiceType;
use Doctrine\ORM\EntityManagerInterface;
class JobOrderManager
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function fulfillJobOrder($jo_id)
{
$results = $this->em->getRepository(JobOrder::class)->findby(array('id' => $jo_id, 'service_type' => ServiceType::BATTERY_REPLACEMENT_NEW));
if (isset($results[0]))
{
$jo = $results[0];
$jo->fulfill();
$cust_vehicle = $jo->getCustomerVehicle();
$invoice = $jo->getInvoice();
$this->updateCustomerVehicleBattery($cust_vehicle, $invoice);
return true;
}
return false;
}
protected function updateCustomerVehicleBattery($cust_vehicle, $invoice)
{
if (($cust_vehicle != null) && ($invoice != null))
{
$items = $invoice->getItems();
foreach ($items as $item)
{
$new_battery = $item->getBattery();
$cust_vehicle->setCurrentBattery($new_battery);
}
$this->em->flush();
}
}
}