Create command to fulfill assigned job orders. #654
This commit is contained in:
parent
b4f28a3b17
commit
3315565000
2 changed files with 206 additions and 1 deletions
205
src/Command/FulfillAssignedJobOrderCommand.php
Normal file
205
src/Command/FulfillAssignedJobOrderCommand.php
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
<?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\Entity\JOEvent;
|
||||
use App\Entity\User;
|
||||
use App\Entity\Warranty;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\JOEventType;
|
||||
use App\Ramcar\DeliveryStatus;
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\ServiceType;
|
||||
use App\Ramcar\WarrantySource;
|
||||
|
||||
use App\Service\WarrantyHandler;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
||||
class FulfillAssignedJobOrderCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $wh;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, WarrantyHandler $wh)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->wh = $wh;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('joborder:fulfillassignednosms')
|
||||
->setDescription('Fulfill assigned job orders without sending an SMS message.')
|
||||
->setHelp('Mark assigned job orders as fulfilled after a set time has passed with no update and should not send a SMS message');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
// retrieve job orders that have status assigned and has not been updated after a certain amount of time has passed.
|
||||
// starting time to count is date schedule
|
||||
$time_now = new DateTime();
|
||||
$date_end = new DateTime();
|
||||
// TODO: for now set to 2 hours
|
||||
$date_end->add(new DateInterval('PT2H'));
|
||||
|
||||
$query = $this->em->createQuery('SELECT jo FROM App\Entity\JobOrder jo WHERE jo.status = :status
|
||||
AND jo.date_schedule <= :date_end');
|
||||
|
||||
$jo_results = $query->setParameters([
|
||||
'status' => JOStatus::ASSIGNED,
|
||||
'date_end' => $date_end,
|
||||
])
|
||||
->getResult();
|
||||
|
||||
error_log('Found job orders ' . count($jo_results));
|
||||
|
||||
// get user. Use the admin user.
|
||||
$user = $this->em->getRepository(User::class)->find(1);
|
||||
|
||||
foreach ($jo_results as $jo)
|
||||
{
|
||||
$jo->fulfill();
|
||||
|
||||
// set delivery status
|
||||
$jo->setDeliveryStatus(DeliveryStatus::FULFILLED);
|
||||
|
||||
// create JO event
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::FULFILL)
|
||||
->setJobOrder($jo)
|
||||
->setUser($user);
|
||||
|
||||
$this->em->persist($event);
|
||||
|
||||
// update the customer vehicle battery record
|
||||
$this->updateVehicleBattery($jo);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// check if new battery
|
||||
// if yes, create warranty
|
||||
if ($this->checkIfNewBattery($jo))
|
||||
{
|
||||
$this->createWarranty($jo, $user);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function updateVehicleBattery(JobOrder $jo)
|
||||
{
|
||||
// check if new battery
|
||||
if (!($this->checkIfNewBattery($jo)))
|
||||
return;
|
||||
|
||||
// customer vehicle
|
||||
$cv = $jo->getCustomerVehicle();
|
||||
if ($cv == null)
|
||||
return;
|
||||
|
||||
// invoice
|
||||
$invoice = $jo->getInvoice();
|
||||
if ($invoice == null)
|
||||
return;
|
||||
|
||||
// invoice items
|
||||
$items = $invoice->getItems();
|
||||
if (count($items) <= 0)
|
||||
return;
|
||||
|
||||
// get first battery from invoice
|
||||
$battery = null;
|
||||
foreach ($items as $item)
|
||||
{
|
||||
$battery = $item->getBattery();
|
||||
if ($battery != null)
|
||||
break;
|
||||
}
|
||||
|
||||
// no battery in order
|
||||
if ($battery == null)
|
||||
return;
|
||||
|
||||
// warranty expiration
|
||||
$warr = $jo->getWarrantyClass();
|
||||
$warr_months = 0;
|
||||
if ($warr == WarrantyClass::WTY_PRIVATE)
|
||||
$warr_months = $battery->getWarrantyPrivate();
|
||||
else if ($warr == WarrantyClass::WTY_COMMERCIAL)
|
||||
$warr_months = $battery->getWarrantyCommercial();
|
||||
else if ($warr == WarrantyClass::WTY_TNV)
|
||||
$warr_months = 12;
|
||||
|
||||
$warr_date = new DateTime();
|
||||
$warr_date->add(new DateInterval('P' . $warr_months . 'M'));
|
||||
|
||||
// update customer vehicle battery
|
||||
$cv->setCurrentBattery($battery)
|
||||
->setHasMotoliteBattery(true)
|
||||
->setWarrantyExpiration($warr_date);
|
||||
}
|
||||
|
||||
protected function checkIfNewBattery(JobOrder $jo)
|
||||
{
|
||||
if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function createWarranty(JobOrder $jo, User $user)
|
||||
{
|
||||
$serial = null;
|
||||
$warranty_class = $jo->getWarrantyClass();
|
||||
$first_name = $jo->getCustomer()->getFirstName();
|
||||
$last_name = $jo->getCustomer()->getLastName();
|
||||
$mobile_number = $jo->getCustomer()->getPhoneMobile();
|
||||
|
||||
// use date_schedule for warranty expiration computation
|
||||
$date_purchase = $jo->getDateSchedule();
|
||||
|
||||
// validate plate number
|
||||
$plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber());
|
||||
if ($plate_number != false)
|
||||
{
|
||||
$batt_list = array();
|
||||
$invoice = $jo->getInvoice();
|
||||
if (!empty($invoice))
|
||||
{
|
||||
// get battery
|
||||
$invoice_items = $invoice->getItems();
|
||||
foreach ($invoice_items as $item)
|
||||
{
|
||||
$battery = $item->getBattery();
|
||||
if ($battery != null)
|
||||
{
|
||||
$batt_list[] = $item->getBattery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user_id = $user->getUsername();
|
||||
$source = WarrantySource::ADMIN_PANEL;
|
||||
$this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, $user_id, $source, $jo->getCustomer(), $jo->getCustomerVehicle()->getVehicle());
|
||||
}
|
||||
else
|
||||
{
|
||||
error_log('Invalid plate number for warranty. Plate number = ' . $jo->getCustomerVehicle()->getPlateNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -126,7 +126,7 @@ class WarrantyHandler
|
|||
// update customer vehicle with warranty info
|
||||
$this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire);
|
||||
|
||||
$this->em->clear();
|
||||
// $this->em->clear();
|
||||
}
|
||||
|
||||
public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire)
|
||||
|
|
|
|||
Loading…
Reference in a new issue