Merge branch '654-script-to-fulflll-job-orders' into 'master'
Resolve "Script to fulflll job orders" Closes #654 See merge request jankstudio/resq!766
This commit is contained in:
commit
9b50efc9d0
2 changed files with 208 additions and 2 deletions
208
src/Command/FulfillAssignedJobOrderCommand.php
Normal file
208
src/Command/FulfillAssignedJobOrderCommand.php
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
<?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 and should not send a SMS message. Date format: YYYY-MM-DD')
|
||||
->addArgument('end_date', InputArgument::REQUIRED, 'End date. Format: YYYY-MM-DD');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
// get the input date
|
||||
$str_date_end = $input->getArgument('end_date');
|
||||
|
||||
// retrieve job orders that have status assigned and has not been fulfilled starting from input date and before.
|
||||
// starting time to count is date schedule
|
||||
$date_end = new DateTime($str_date_end);
|
||||
|
||||
$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)
|
||||
{
|
||||
error_log('Fulfilling job order ' . $jo->getID());
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -125,8 +125,6 @@ class WarrantyHandler
|
|||
|
||||
// update customer vehicle with warranty info
|
||||
$this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire);
|
||||
|
||||
$this->em->clear();
|
||||
}
|
||||
|
||||
public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire)
|
||||
|
|
|
|||
Loading…
Reference in a new issue