resq/src/Controller/JobOrderController.php

1451 lines
47 KiB
PHP

<?php
namespace App\Controller;
use App\Ramcar\JOStatus;
use App\Ramcar\InvoiceCriteria;
use App\Ramcar\CMBServiceType;
use App\Ramcar\ServiceType;
use App\Ramcar\JOCancelReasons;
use App\Ramcar\TransactionOrigin;
use App\Entity\CustomerVehicle;
use App\Entity\Promo;
use App\Entity\Battery;
use App\Entity\JobOrder;
use App\Entity\VehicleManufacturer;
use App\Entity\Vehicle;
use App\Entity\Hub;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
use App\Service\GISManagerInterface;
use App\Service\MapTools;
use App\Service\MQTTClient;
use App\Service\MQTTClientApiv2;
use App\Service\FCMSender;
use App\Service\APNSClient;
use App\Service\InventoryManager;
use App\Service\HubSelector;
use App\Service\RiderTracker;
use App\Service\MotivConnector;
use App\Service\PriceTierManager;
use App\Service\GeofenceTracker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Doctrine\ORM\EntityManagerInterface;
use Catalyst\MenuBundle\Annotation\Menu;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
class JobOrderController extends Controller
{
public function getJobOrders(Request $req, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
$params = $jo_handler->getJobOrders($req);
$job_orders = $params['job_orders'];
$has_more_pages = $params['has_more_pages'];
// response
return $this->json([
'success' => true,
'results' => $job_orders,
'pagination' => [
'more' => $has_more_pages
]
]);
}
/**
* @Menu(selected="jo_in")
*/
public function incomingForm(JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
$params = $jo_handler->initializeIncomingForm();
$params['submit_url'] = $this->generateUrl('jo_in_submit');
$params['return_url'] = $this->generateUrl('jo_in');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_in")
*/
public function openEditForm($id, JobOrderHandlerInterface $jo_handler, GISManagerInterface $gis)
{
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
$params = $jo_handler->initializeOpenEditForm($id);
$params['submit_url'] = $this->generateUrl('jo_open_edit_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_open');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function openEditSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
{
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
$error_array = [];
$result = $jo_handler->openEditJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_in")
*/
public function incomingVehicleForm($cvid, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
try
{
$params = $jo_handler->initializeIncomingVehicleForm($cvid);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['submit_url'] = $this->generateUrl('jo_in_submit');
$params['return_url'] = $this->generateUrl('jo_in');
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function incomingSubmit(Request $req, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$result = $jo_handler->generateJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_proc")
*/
public function listProcessing(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_list_processing');
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_assign")
*/
public function listAssigning(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_assign.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_list_assigning');
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_fulfill")
*/
public function listFulfillment(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_list_fulfillment');
$params = $jo_handler->getOtherParameters();
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_open")
*/
public function listOpen(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_open.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_list_open');
$params = $jo_handler->getOtherParameters();
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
$params['statuses'] = JOStatus::getCollection();
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_all")
*/
public function listAll(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_all.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_list_all');
$params = $jo_handler->getOtherParameters();
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
return $this->render($template, $params);
}
/*
public function listRows($tier)
{
// check which job order tier is being called for and confirm access
$tier_params = $this->checkTier($tier);
$params = $this->initParameters($tier_params['key']);
$params['tier_name'] = $tier_params['name'];
$params['rows_route'] = $tier_params['rows_route'];
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
// response
return $this->render('job-order/list.html.twig', $params);
}
*/
public function getRows(Request $req, $tier, JobOrderHandlerInterface $jo_handler)
{
try
{
$params = $jo_handler->getRows($req, $tier);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
$rows = $params['rows'];
$meta = $params['meta'];
$tier_params = $params['tier_params'];
foreach ($rows as $key => $data) {
// add crud urls
$jo_id = $rows[$key]['id'];
if ($tier == 'open')
{
$rows[$key]['meta']['reassign_hub_url'] = $this->generateUrl('jo_open_hub_form', ['id' => $jo_id]);
$rows[$key]['meta']['reassign_rider_url'] = $this->generateUrl('jo_open_rider_form', ['id' => $jo_id]);
// $rows[$key]['meta']['edit_url'] = $this->generateUrl('jo_open_edit_form', ['id' => $jo_id]);
$rows[$key]['meta']['edit_url'] = $this->generateUrl($jo_handler->getEditRoute($jo_id, $tier_params['edit_route']), ['id' => $jo_id]);
$rows[$key]['meta']['onestep_edit_url'] = $this->generateUrl('jo_onestep_edit_form', ['id' => $jo_id]);
}
else
{
// $rows[$key]['meta']['update_url'] = $this->generateUrl($tier_params['edit_route'], ['id' => $jo_id]);
$rows[$key]['meta']['update_url'] = $this->generateUrl($jo_handler->getEditRoute($jo_id, $tier_params['edit_route']), ['id' => $jo_id]);
$rows[$key]['meta']['onestep_edit_url'] = $this->generateUrl('jo_onestep_edit_form', ['id' => $jo_id]);
$rows[$key]['meta']['pdf_url'] = $this->generateUrl('jo_pdf_form', ['id' => $jo_id]);
$rows[$key]['meta']['view_url'] = $this->generateUrl('jo_all_view_form',['id' => $jo_id]);
}
if ($tier_params['unlock_route'] != '')
$rows[$key]['meta']['unlock_url'] = $this->generateUrl($tier_params['unlock_route'], ['id' => $jo_id]);
}
// response
return $this->json([
'meta' => $meta,
'data' => $rows
]);
}
/**
* @Menu(selected="jo_proc")
*/
public function processingForm(HubSelector $hub_selector, $id, JobOrderHandlerInterface $jo_handler, GISManagerInterface $gis, MotivConnector $motiv, Request $req)
{
$this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.');
try
{
$params = $jo_handler->initializeProcessingForm($id, $hub_selector, $motiv);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['submit_url'] = $this->generateUrl('jo_proc_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_proc');
$params['map_js_file'] = $gis->getJSJOFile();
// check for origin parameter
$origin = $req->query->get('origin', '');
if (empty($origin))
{
// set return url to default dispatch
$params['return_url'] = $this->generateUrl('jo_proc');
}
else
{
// set return url to resq dispatch
$params['return_url'] = $this->generateUrl('jo_resq_proc');
}
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function processingSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient, MQTTClientApiv2 $mclientv2, FCMSender $fcmclient, $id)
{
$this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->dispatchJobOrder($req, $id, $mclient, $mclientv2, $fcmclient);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array))
{
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_assign")
*/
public function assigningForm($id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis)
{
$this->denyAccessUnlessGranted('jo_assign.list', null, 'No access.');
try
{
$params = $jo_handler->initializeAssignForm($id);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['submit_url'] = $this->generateUrl('jo_assign_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_assign');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function assigningSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTCLient $mclient, FCMSender $fcmclient, APNSClient $aclient, $id)
{
$this->denyAccessUnlessGranted('jo_assign.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->assignJobOrder($req, $id, $mclient, $aclient);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_fulfill")
*/
public function fulfillmentForm(JobOrderHandlerInterface $jo_handler, $id,
GISManagerInterface $gis, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.');
try
{
$params = $jo_handler->initializeFulfillmentForm($id);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$params['submit_url'] = $this->generateUrl('jo_fulfill_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_fulfill');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function fulfillmentSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient, FCMSender $fcmclient, $id)
{
$this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->fulfillJobOrder($req, $id, $mclient);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_open")
*/
public function openHubForm(HubSelector $hub_selector, $id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis, MotivConnector $motiv)
{
$this->denyAccessUnlessGranted('jo_open.list', null, 'No access.');
try
{
$params = $jo_handler->initializeHubForm($id, $hub_selector, $motiv);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['submit_url'] = $this->generateUrl('jo_open_hub_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_open');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function openHubSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient, MQTTClientApiv2 $mclientv2, FCMSender $fcmclient, $id)
{
$this->denyAccessUnlessGranted('jo_open.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->setHub($req, $id, $mclient, $mclientv2, $fcmclient);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_open")
*/
public function openRiderForm($id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis)
{
$this->denyAccessUnlessGranted('jo_open.list', null, 'No access.');
try
{
$params = $jo_handler->initializeRiderForm($id);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['submit_url'] = $this->generateUrl('jo_open_rider_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_open');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function openRiderSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient, MQTTClientApiv2 $mclientv2, FCMSender $fcmclient, $id)
{
$this->denyAccessUnlessGranted('jo_open.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->setRider($req, $id, $mclient, $mclientv2, $fcmclient);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_all")
*/
public function allForm($id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('jo_all.list', null, 'No access.');
try
{
$params = $jo_handler->initializeAllForm($id);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$params['return_url'] = $this->generateUrl('jo_all');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
/**
* @Menu(selected="jo_all")
*/
public function allViewForm($id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('jo_all.list', null, 'No access.');
try
{
$params = $jo_handler->initializeAllViewForm($id);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['return_url'] = $this->generateUrl('jo_all');
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function pdfForm(Request $req, $id, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_pdf.list', null, 'No access.');
try
{
$proj_path = $this->get('kernel')->getProjectDir();
$params = $jo_handler->generatePDFForm($req, $id, $proj_path);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$pdf = $params['obj'];
$filename = $params['filename'];
// return response
return new Response($pdf->Output('I', $filename), 200, [
'Content-Type' => 'application/pdf'
]);
}
public function cancelJobOrder(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient, MQTTClientApiv2 $mclientv2, FCMSender $fcmclient, $id)
{
$this->denyAccessUnlessGranted('joborder.cancel', null, 'No access.');
$cancel_reason = $req->request->get('cancel_reason');
if (empty($cancel_reason))
{
// something happened
return $this->json([
'success' => false,
'error' => 'Reason for cancellation is required.'
], 422);
}
try
{
$jo_handler->cancelJobOrder($req, $id, $mclient, $mclientv2, $fcmclient);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// return successful response
return $this->json([
'success' => 'Job order has been cancelled!'
]);
}
public function generateInvoice(Request $req, InvoiceGeneratorInterface $ic, PriceTierManager $pt_manager)
{
// error_log('generating invoice...');
$error = false;
$stype = $req->request->get('stype');
$items = $req->request->get('items');
$promo_id = $req->request->get('promo');
$cvid = $req->request->get('cvid');
$service_charges = $req->request->get('service_charges', []);
// coordinates
// need to check if lng and lat are set
$lng = $req->request->get('coord_lng', 0);
$lat = $req->request->get('coord_lat', 0);
$price_tier = 0;
if (($lng != 0) && ($lat != 0))
{
$coordinates = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
$price_tier = $pt_manager->getPriceTier($coordinates);
}
$em = $this->getDoctrine()->getManager();
// get customer vehicle
$cv = $em->getRepository(CustomerVehicle::class)->find($cvid);
/*
if ($cv == null)
throw new \Exception('Could not get customer vehicle');
*/
// instantiate invoice criteria
$criteria = new InvoiceCriteria();
$criteria->setServiceType($stype)
->setCustomerVehicle($cv)
->setIsTaxable()
->setSource(TransactionOrigin::CALL)
->setPriceTier($price_tier);
/*
// if it's a jumpstart or troubleshoot only, we know what to charge already
if ($stype == ServiceType::JUMPSTART_TROUBLESHOOT)
{
$invoice = [
'price' => 150.00,
'discount' => 0,
'trade_in' => 0,
'vat' => 0,
'total_price' => 150.00,
'items' => []
];
$invoice['items'][] = [
'title' => 'Troubleshooting fee',
'quantity' => 1,
'unit_price' => 150.00,
'amount' => 150.00
];
return $this->json([
'success' => true,
'invoice' => $invoice
]);
}
*/
$error = $ic->generateDraftInvoice($criteria, $promo_id, $service_charges, $items);
if ($error)
{
// something happened
return $this->json([
'success' => false,
'error' => $error
], 422);
}
// generate the invoice
$iobj = $ic->generateInvoice($criteria);
// use invoice object values in a json friendly array
$invoice = [
'discount' => number_format($iobj->getDiscount(), 2),
'trade_in' => number_format($iobj->getTradeIn(), 2), // TODO: computations not done yet for this on invoice creator
'price' => number_format($iobj->getVATExclusivePrice(), 2),
'vat' => number_format($iobj->getVAT(), 2),
'total_price' => number_format($iobj->getTotalPrice(), 2),
'items' => []
];
foreach ($iobj->getItems() as $item)
{
$invoice['items'][] = [
'title' => $item->getTitle(),
'quantity' => number_format($item->getQuantity()), // TODO: quantities are always 1, hardcoded into InvoiceCreator. no way of accepting quantities on InvoiceCriteria
'unit_price' => number_format($item->getPrice(), 2),
'amount' => number_format($item->getPrice() * $item->getQuantity(), 2) // TODO: should this calculation should be a saved value on InvoiceItem instead?
];
}
// return
return $this->json([
'success' => true,
'invoice' => $invoice
]);
}
public function unlockProcessor($id, JobOrderHandlerInterface $jo_handler, Request $req)
{
$this->denyAccessUnlessGranted('jo_proc.unlock', null, 'No access.');
// call unlockProcessor in job order service
$jo_handler->unlockProcessor($id);
// get the origin
$origin = $req->query->get('origin', '');
if (empty($origin))
{
// redirect to list
return $this->redirectToRoute('jo_proc');
}
else
{
// redirect to resq list
return $this->redirectToRoute('jo_resq_proc');
}
}
public function unlockAssignor($id, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_assign.unlock', null, 'No access.');
// call unlockAssignor in job order service
$jo_handler->unlockAssignor($id);
// redirect to list
return $this->redirectToRoute('jo_assign');
}
public function rejectHubSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
{
$this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.');
// initialize error list
$error_array = [];
try
{
$error_array = $jo_handler->rejectHub($req, $id);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
// check if any errors were found
if (!empty($error_array))
{
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!',
'request' => $req->request->all()
]);
}
/**
* @Menu(selected="jo_onestep_form")
*/
public function oneStepForm(EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler, GISManagerInterface $gis)
{
$this->denyAccessUnlessGranted('jo_onestep.form', null, 'No access.');
$params = $jo_handler->initializeOneStepForm();
$params['submit_url'] = $this->generateUrl('jo_onestep_submit');
$params['return_url'] = $this->generateUrl('jo_onestep_form');
$params['map_js_file'] = $gis->getJSJOFile();
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function oneStepSubmit(Request $req, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_onestep.form', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$error_array = $jo_handler->processOneStepJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_onestep_edit_form")
*/
public function oneStepEditForm($id, EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis, MapTools $map_tools)
{
$this->denyAccessUnlessGranted('jo_onestep.edit', null, 'No access.');
$params = $jo_handler->initializeOneStepEditForm($id, $map_tools);
$params['submit_url'] = $this->generateUrl('jo_onestep_edit_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_open');
$params['map_js_file'] = $gis->getJSJOFile();
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function oneStepEditSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
{
$this->denyAccessUnlessGranted('jo_onestep.edit', null, 'No access.');
$error_array = [];
$error_array = $jo_handler->processOneStepJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @ParamConverter("jo", class="App\Entity\JobOrder")
*/
public function popupInfo(JobOrder $jo, JobOrderHandlerInterface $jo_handler)
{
if ($jo == null)
return new Response('No job order data');
// get the right template
$template = $jo_handler->getTwigTemplate('jo_popup');
$params['jo'] = $jo;
// response
return $this->render($template, $params);
}
/**
* @ParamConverter("jo", class="App\Entity\JobOrder")
*/
public function tracker(
EntityManagerInterface $em,
RiderTracker $rider_tracker,
GISManagerInterface $gis_manager,
JobOrder $jo
)
{
if ($jo === null)
return new Response('No job order data');
$rider = $jo->getRider();
// get map
$params['jo'] = $jo;
$params['rider'] = $rider;
$params['rider_pos'] = $rider_tracker->getRiderLocation($rider->getID());
$params['service_type'] = CMBServiceType::getName($jo->getServiceType());
$params['map_js_file'] = $gis_manager->getJSInitFile();
return $this->render('job-order/tracker.html.twig', $params);
}
/**
* @Menu(selected="jo_walkin_form")
*/
public function walkInForm(EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_walkin.form', null, 'No access.');
$params = $jo_handler->initializeWalkinForm();
$params['submit_url'] = $this->generateUrl('jo_walkin_submit');
$params['return_url'] = $this->generateUrl('jo_walkin_form');
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$params['hubs'] = $em->getRepository(Hub::class)->findAll();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function walkInSubmit(Request $req, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_walkin.form', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$error_array = $jo_handler->processWalkinJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_walkin_edit_form")
*/
public function walkInEditForm($id, EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_walkin.edit', null, 'No access.');
$params = $jo_handler->initializeWalkinEditForm($id);
$params['submit_url'] = $this->generateUrl('jo_walkin_edit_submit', ['id' => $id]);
$params['return_url'] = $this->generateUrl('jo_open');
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$params['hubs'] = $em->getRepository(Hub::class)->findAll();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function walkInEditSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
{
$this->denyAccessUnlessGranted('jo_walkin.edit', null, 'No access.');
$error_array = [];
$error_array = $jo_handler->processWalkinJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @Menu(selected="jo_hub_view")
*/
public function hubView(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_hub.list', null, 'No access.');
$template = $jo_handler->getTwigTemplate('jo_hub_list');
$params = $jo_handler->getOtherParameters();
$params['table_refresh_rate'] = $this->container->getParameter('job_order_refresh_interval');
return $this->render($template, $params);
}
public function getHubViewRows(Request $req, $tier, JobOrderHandlerInterface $jo_handler)
{
try
{
$params = $jo_handler->getHubViewRows($req, $tier);
}
catch (AccessDeniedHttpException $e)
{
throw $this->createAccessDeniedException($e->getMessage());
}
$rows = $params['rows'];
$meta = $params['meta'];
$tier_params = $params['tier_params'];
foreach ($rows as $key => $data) {
// add crud urls
$jo_id = $rows[$key]['id'];
if ($tier == 'open')
{
$rows[$key]['meta']['reassign_hub_url'] = $this->generateUrl('jo_open_hub_form', ['id' => $jo_id]);
$rows[$key]['meta']['reassign_rider_url'] = $this->generateUrl('jo_open_rider_form', ['id' => $jo_id]);
// $rows[$key]['meta']['edit_url'] = $this->generateUrl('jo_open_edit_form', ['id' => $jo_id]);
$rows[$key]['meta']['edit_url'] = $this->generateUrl($jo_handler->getEditRoute($jo_id, $tier_params['edit_route']), ['id' => $jo_id]);
}
else
{
// $rows[$key]['meta']['update_url'] = $this->generateUrl($tier_params['edit_route'], ['id' => $jo_id]);
$rows[$key]['meta']['update_url'] = $this->generateUrl($jo_handler->getEditRoute($jo_id, $tier_params['edit_route']), ['id' => $jo_id]);
}
if ($tier_params['unlock_route'] != '')
$rows[$key]['meta']['unlock_url'] = $this->generateUrl($tier_params['unlock_route'], ['id' => $jo_id]);
}
// response
return $this->json([
'meta' => $meta,
'data' => $rows
]);
}
/**
* @Menu(selected="jo_hub_view")
*/
public function hubViewForm($id, JobOrderHandlerInterface $jo_handler,
GISManagerInterface $gis, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('jo_hub.list', null, 'No access.');
try
{
$params = $jo_handler->initializeHubViewForm($id);
}
catch (NotFoundHttpException $e)
{
throw $this->createNotFoundException($e->getMessage());
}
$params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll();
$params['vmakes'] = $em->getRepository(Vehicle::class)->findAll();
$params['return_url'] = $this->generateUrl('jo_hub_view');
$params['submit_url'] = '';
$params['map_js_file'] = $gis->getJSJOFile();
$template = $params['template'];
// response
return $this->render($template, $params);
}
public function fulfillCancelSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
{
$this->denyAccessUnlessGranted('jo_cancel.fulfill', null, 'No access.');
// TODO: make the service function to fulfill the cancelled JO
$error_array = [];
$result = $jo_handler->fulfillCancelledJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
// ajax call
public function cancelReasons()
{
return $this->json([
'cancel_reasons' => JOCancelReasons::getCollection(),
]);
}
// ajax call
public function checkGeofence(Request $req, GeofenceTracker $geo)
{
$lat = $req->query->get('lat');
$lng = $req->query->get('lng');
$is_covered = $geo->isCovered($lng, $lat);
return $this->json([
'is_covered' => $is_covered
]);
}
/**
* @Menu(selected="jo_autoassign")
*/
// this is uncalled or does not display in admin panel
/*
public function autoAssignForm(GISManagerInterface $gis, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_autoassign.test', null, 'No access.');
$params = $jo_handler->initializeIncomingForm();
$params['submit_url'] = $this->generateUrl('jo_autoassign_test_submit');
$params['return_url'] = $this->generateUrl('jo_autoassign');
$params['map_js_file'] = $gis->getJSJOFile();
$template = 'job-order/autoassign.form.html.twig';
// response
return $this->render($template, $params);
}
public function autoAssignSubmit(Request $req, JobOrderHandlerInterface $jo_handler,
EntityManagerInterface $em, MapTools $map_tools,
InventoryManager $im)
{
$this->denyAccessUnlessGranted('jo_autoassign.test', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$result = $jo_handler->generateJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// get job order
$jo = $result['job_order'];
if (($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) ||
($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY))
$this->autoAssignHubAndRider($jo, $em, $map_tools, $im);
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
protected function autoAssignHubAndRider($jo, EntityManagerInterface $em,
MapTools $map_tools, InventoryManager $im)
{
// get the nearest 10 hubs
// TODO: move this snippet to a function
$hubs = $map_tools->getClosestHubs($jo->getCoordinates(), 10, date("H:i:s"));
$nearest_hubs = [];
$nearest_hubs_with_distance = [];
foreach ($hubs as $hub)
{
$nearest_hubs_with_distance[] = $hub;
// TODO: uncomment this when we have branch codes in data
// and when they have real codes and not test ones
//$nearest_branch_codes[] = $hub['hub']->getBranchCode();
$nearest_branch_codes = ['WestAve','jay_franchise'];
}
// get battery sku
$invoice = $jo->getInvoice();
if ($invoice != null)
{
$items = $invoice->getItems();
$skus = [];
foreach ($items as $item)
{
// TODO: uncomment this when they have real codes and not test ones
//$skus[] = $item->getBattery()->getSAPCode();
$skus[] = 'WMGD31EL-CPNM0-L';
}
// api call to check inventory
// pass the list of branch codes of nearest hubs and the skus
// go through returned list of branch codes
$hubs_with_inventory = $im->getBranchesInventory($nearest_branch_codes, $skus);
if (!empty($hubs_with_inventory))
{
$nearest = [];
$flag_hub_found = false;
foreach ($hubs_with_inventory as $hub_with_inventory)
{
// find hub according to branch code
$found_hub = $em->getRepository(Hub::class)->findOneBy(['branch_code' => $hub_with_inventory['BranchCode']]);
if ($found_hub != null)
{
// check rider availability
if (count($found_hub->getAvailableRiders()) > 0)
{
// check against nearest hubs with distance
foreach ($nearest_hubs_with_distance as $nhd)
{
// get distance of hub from location, compare with $nearest. if less, replace nearest
if ($found_hub->getID() == $nhd['hub']->getID())
{
if (empty($nearest))
{
$nearest = $nhd;
$flag_hub_found = true;
}
else
{
if ($nhd['distance'] < $nearest['distance'])
{
$nearest = $nhd;
$flag_hub_found = true;
}
}
}
}
}
}
}
if ($flag_hub_found)
{
$jo->setHub($nearest['hub']);
$hub_riders = $nearest['hub']->getAvailableRiders();
$rider = null;
if (count($hub_riders) > 1)
{
// TODO: this will no longer be necessary when the contents
// of randomizeRider changes
$available_riders = [];
foreach ($hub_riders as $rider)
{
$available_riders[] = $rider;
}
$rider = $this->randomizeRider($available_riders);
}
else
$rider = $hub_riders[0];
$jo->setRider($rider);
$jo->setStatus(JOStatus::ASSIGNED);
$em->persist($jo);
$em->flush();
}
}
}
}
protected function randomizeRider($riders)
{
// TODO: get redis to track the sales per rider per day
// check the time they came in
// for now, randomize the rider
$selected_index = array_rand($riders);
$selected_rider = $riders[$selected_index];
return $selected_rider;
} */
}