resq/src/Service/JobOrderManager.php
2021-07-21 10:12:27 +00:00

167 lines
5.4 KiB
PHP

<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use App\Entity\JobOrder;
use App\Entity\JOEvent;
use App\Entity\User;
use App\Entity\Customer;
use App\Ramcar\ServiceType;
use App\Ramcar\JOStatus;
use App\Ramcar\JOEventType;
use DateTime;
class JobOrderManager
{
protected $em;
protected $security;
protected $rah;
protected $mclient;
protected $promo_logger;
public function __construct(EntityManagerInterface $em, Security $security,
RiderAssignmentHandlerInterface $rah, MQTTClient $mclient,
PromoLogger $promo_logger)
{
$this->em = $em;
$this->security = $security;
$this->rah = $rah;
$this->mclient = $mclient;
$this->promo_logger = $promo_logger;
}
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);
$cust_vehicle->setHasMotoliteBattery(true);
}
$this->em->flush();
}
}
public function processJobOrderEvents(JobOrder $jo, $jo_event_type)
{
$em = $this->em;
// check for the jo event type
if ($jo_event_type == JOEventType::CREATE)
{
// add event log for JO
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID($jo_event_type)
->setJobOrder($jo);
$user = $this->security->getUser();
// check if user is User or APIUser
if ($user instanceof User)
$event->setUser($user);
$em->persist($event);
// check for JO status for additional events
// (1) when mobile app gets a new JO, finds a hub and assigns a rider
if ($jo->getStatus() == JOStatus::ASSIGNED)
{
// add event logs for hub and rider assignments
$hub_assign_event = new JOEvent();
$hub_assign_event->setDateHappen(new DateTime())
->setTypeID(JOEventType::HUB_ASSIGN)
->setJobOrder($jo);
$em->persist($hub_assign_event);
$rider_assign_event = new JOEvent();
$rider_assign_event->setDateHappen(new DateTime())
->setTypeID(JOEventType::RIDER_ASSIGN)
->setJobOrder($jo);
$em->persist($rider_assign_event);
}
// (2) when mobile app gets a new JO, finds a hub but no rider
if ($jo->getStatus() == JOStatus::RIDER_ASSIGN)
{
// add event logs for hub assignments
$hub_assign_event = new JOEvent();
$hub_assign_event->setDateHappen(new DateTime())
->setTypeID(JOEventType::HUB_ASSIGN)
->setJobOrder($jo);
$em->persist($hub_assign_event);
}
}
else
{
// TODO: check for other JO event types. See if all other event types have no special processing
// cancel, edit, dispatch are the same
// add event log for JO
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID($jo_event_type)
->setJobOrder($jo);
$user = $this->security->getUser();
// check if user is User or APIUser
if ($user instanceof User)
$event->setUser($user);
$em->persist($event);
}
}
public function removeCustomerTag(JobOrder $jo, Customer $customer, $customer_tags, $username)
{
foreach ($customer_tags as $customer_tag)
{
if ($customer_tag->getID() == $jo->getInvoice()->getUsedCustomerTagId())
{
// remove associated entity
$customer->removeCustomerTag($customer_tag);
// log the availment of promo from customer
$created_by = $username;
$cust_id = $jo->getCustomer()->getID();
$cust_fname = $jo->getCustomer()->getFirstName();
$cust_lname = $jo->getCustomer()->getLastName();
$jo_id = $jo->getID();
$invoice_id = $jo->getInvoice()->getID();
// TODO: check if we store total price of invoice or just the discounted amount
$amount = $jo->getInvoice()->getTotalPrice();
$this->promo_logger->logPromoInfo($created_by, $cust_id, $cust_fname, $cust_lname, $jo_id,
$invoice_id, $amount);
}
}
}
}