diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 334242b8..c8cfa3e1 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -674,7 +674,7 @@ class JobOrderController extends Controller return $this->render('job-order/form.html.twig', $params); } - public function assigningSubmit(Request $req, ValidatorInterface $validator, JobOrderHandlerInterface $jo_handler, MQTTCLient $mclient, APNSClient $aclient, $id) + public function assigningSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTCLient $mclient, APNSClient $aclient, $id) { $this->denyAccessUnlessGranted('jo_assign.list', null, 'No access.'); @@ -708,42 +708,20 @@ class JobOrderController extends Controller /** * @Menu(selected="jo_fulfill") */ - public function fulfillmentForm(MapTools $map_tools, $id) + public function fulfillmentForm(JobOrderHandlerInterface $jo_handler, $id) { $this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.'); - $em = $this->getDoctrine()->getManager(); - - $params['mode'] = 'update-fulfillment'; - - // get row data - $obj = $em->getRepository(JobOrder::class)->find($id); - - // make sure this row exists - if (empty($obj)) + try { - throw $this->createNotFoundException('The job order does not exist'); + $params = $jo_handler->initializeFulfillmentForm($id); + } + catch (NotFoundHttpException $e) + { + throw $this->createNotFoundException($e->getMessage()); } - // check status - if (!in_array($obj->getStatus(), [JOStatus::ASSIGNED, JOStatus::IN_PROGRESS])) - { - throw $this->createNotFoundException('The job order does not have a fulfillment status'); - } - - // check if hub is assigned to current user - $user_hubs = $this->getUser()->getHubs(); - if (!in_array($obj->getHub()->getID(), $user_hubs)) - { - throw $this->createNotFoundException('The job order is not on a hub assigned to this user'); - } - - $this->fillDropdownParameters($params); - $this->fillFormTags($params); - - $params['obj'] = $obj; - $params['status_cancelled'] = JOStatus::CANCELLED; - $params['submit_url'] = $this->generateUrl('jo_fulfill_submit', ['id' => $obj->getID()]); + $params['submit_url'] = $this->generateUrl('jo_fulfill_submit', ['id' => $id]); $params['return_url'] = $this->generateUrl('jo_fulfill'); // response @@ -856,83 +834,20 @@ class JobOrderController extends Controller /** * @Menu(selected="jo_open") */ - public function openHubForm(MapTools $map_tools, $id) + public function openHubForm(MapTools $map_tools, $id, JobOrderHandlerInterface $jo_handler) { $this->denyAccessUnlessGranted('jo_open.list', null, 'No access.'); - $em = $this->getDoctrine()->getManager(); - - $params['mode'] = 'update-reassign-hub'; - - // get row data - $obj = $em->getRepository(JobOrder::class)->find($id); - - // make sure this row exists - if (empty($obj)) + try { - throw $this->createNotFoundException('The job order does not exist'); + $params = $jo_handler->initializeHubForm($id, $map_tools); + } + catch (NotFoundHttpException $e) + { + throw $this->createNotFoundException($e->getMessage()); } - $this->fillDropdownParameters($params); - $this->fillFormTags($params); - - // get rejections - $rejections = $obj->getHubRejections(); - - // get rejection reasons - $params['rejection_reasons'] = JORejectionReason::getCollection(); - - // get closest hubs - $hubs = $map_tools->getClosestHubs($obj->getCoordinates(), 50, date("H:i:s")); - - $params['status_cancelled'] = JOStatus::CANCELLED; - $params['hubs'] = []; - - // format duration and distance into friendly time - foreach ($hubs as $hub) { - // duration - $seconds = $hub['duration']; - - if (!empty($seconds) && $seconds > 0) { - $hours = floor($seconds / 3600); - $minutes = ceil(($seconds / 60) % 60); - - $hub['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); - } else { - $hub['duration'] = false; - } - - // distance - $meters = $hub['distance']; - - if (!empty($meters) && $meters > 0) { - $hub['distance'] = round($meters / 1000) . " km"; - } else { - $hub['distance'] = false; - } - - // counters - $hub['rider_count'] = count($hub['hub']->getAvailableRiders()); - $hub['jo_count'] = count($hub['hub']->getForAssignmentJobOrders()); - - // check for rejection - $hub['flag_rejected'] = false; - $hub_id = $hub['hub']->getID(); - - foreach ($rejections as $robj) - { - if ($robj->getHub()->getID() === $hub_id) - { - $hub['flag_rejected'] = true; - break; - } - } - - $params['hubs'][] = $hub; - } - - $params['obj'] = $obj; - $params['submit_url'] = $this->generateUrl('jo_open_hub_submit', ['id' => $obj->getID()]); + $params['submit_url'] = $this->generateUrl('jo_open_hub_submit', ['id' => $id]); $params['return_url'] = $this->generateUrl('jo_open'); // response diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index d3770fab..4e804cb9 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -329,12 +329,11 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); // check if we can assign if (!$obj->canAssign()) - throw $this->createNotFoundException('Cannot assign rider to this job order.'); - + throw new NotFoundHttpException('Cannot assign rider to this job order.'); // check if lat and lng are provided if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) { @@ -436,7 +435,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); // check if lat and lng are provided if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) { @@ -523,7 +522,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); $cancel_reason = $req->request->get('cancel_reason'); $obj->cancel($cancel_reason); @@ -604,7 +603,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface if (empty($cv)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } $jo = new JobOrder(); @@ -630,7 +629,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // make sure this row exists if (empty($obj)) - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); $this->fillDropdownParameters($params); $this->fillFormTags($params); @@ -686,14 +685,14 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface if (empty($obj)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } // check status if ($obj->getStatus() != JOStatus::PENDING) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not have a pending status'); + throw new NotFoundHttpException('The job order does not have a pending status'); } // check if we are the processor @@ -719,7 +718,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface } catch(PessimisticLockException $e) { - throw $this->createAccessDeniedException('Not the processor'); + throw new AccessDeniedHttpException('Not the processor'); } // NOTE: we are able to lock, everything should be fine now @@ -808,14 +807,14 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface if (empty($obj)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } // check status if ($obj->getStatus() != JOStatus::RIDER_ASSIGN) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not have an assigning status'); + throw new NotFoundHttpException('The job order does not have an assigning status'); } // check if super user @@ -833,7 +832,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface if (!in_array($obj->getHub()->getID(), $user_hubs)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order is not on a hub assigned to this user'); + throw new NotFoundHttpException('The job order is not on a hub assigned to this user'); } // check if we are the assignor @@ -842,7 +841,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface if ($assignor != null && $assignor->getID() != $user->getID()) { $em->getConnection()->rollback(); - throw $this->createAccessDeniedException('Not the assignor'); + throw new AccessDeniedHttpException('Not the assignor'); } // make this user be the assignor @@ -856,7 +855,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface } catch (PessimisticLockException $e) { - throw $this->createAccessDeniedException('Not the assignor'); + throw new AccessDeniedHttpException('Not the assignor'); } $this->fillDropdownParameters($params); @@ -866,8 +865,126 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $params['status_cancelled'] = JOStatus::CANCELLED; return $params; + } + // initialize fulflll job order form + public function initializeFulfillmentForm($id) + { + $em = $this->em; + $params['mode'] = 'update-fulfillment'; + + // get row data + $obj = $em->getRepository(JobOrder::class)->find($id); + + // make sure this row exists + if (empty($obj)) + { + throw new NotFoundHttpException('The job order does not exist'); + } + + // check status + if (!in_array($obj->getStatus(), [JOStatus::ASSIGNED, JOStatus::IN_PROGRESS])) + { + throw new NotFoundHttpException('The job order does not have a fulfillment status'); + } + + // get current user + $user = $this->security->getUser(); + + // check if hub is assigned to current user + $user_hubs = $user->getHubs(); + if (!in_array($obj->getHub()->getID(), $user_hubs)) + { + throw new NotFoundHttpException('The job order is not on a hub assigned to this user'); + } + + $this->fillDropdownParameters($params); + $this->fillFormTags($params); + + $params['obj'] = $obj; + $params['status_cancelled'] = JOStatus::CANCELLED; + + return $params; + } + + // initialize hub form + public function initializeHubForm($id, $map_tools) + { + $em = $this->em; + + $params['mode'] = 'update-reassign-hub'; + + // get row data + $obj = $em->getRepository(JobOrder::class)->find($id); + + // make sure this row exists + if (empty($obj)) + { + throw new NotFoundHttpException('The job order does not exist'); + } + + $this->fillDropdownParameters($params); + $this->fillFormTags($params); + + // get rejections + $rejections = $obj->getHubRejections(); + + // get rejection reasons + $params['rejection_reasons'] = JORejectionReason::getCollection(); + + // get closest hubs + $hubs = $map_tools->getClosestHubs($obj->getCoordinates(), 50, date("H:i:s")); + + $params['status_cancelled'] = JOStatus::CANCELLED; + $params['hubs'] = []; + + // format duration and distance into friendly time + foreach ($hubs as $hub) { + // duration + $seconds = $hub['duration']; + + if (!empty($seconds) && $seconds > 0) { + $hours = floor($seconds / 3600); + $minutes = ceil(($seconds / 60) % 60); + + $hub['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); + } else { + $hub['duration'] = false; + } + + // distance + $meters = $hub['distance']; + + if (!empty($meters) && $meters > 0) { + $hub['distance'] = round($meters / 1000) . " km"; + } else { + $hub['distance'] = false; + } + + // counters + $hub['rider_count'] = count($hub['hub']->getAvailableRiders()); + $hub['jo_count'] = count($hub['hub']->getForAssignmentJobOrders()); + + // check for rejection + $hub['flag_rejected'] = false; + $hub_id = $hub['hub']->getID(); + + foreach ($rejections as $robj) + { + if ($robj->getHub()->getID() === $hub_id) + { + $hub['flag_rejected'] = true; + break; + } + } + + $params['hubs'][] = $hub; + } + + $params['obj'] = $obj; + + return $params; } protected function fillDropdownParameters(&$params) diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index c012f262..3ab5fd7c 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -329,12 +329,11 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); // check if we can assign if (!$obj->canAssign()) - throw $this->createNotFoundException('Cannot assign rider to this job order.'); - + throw new NotFoundHttpException('Cannot assign rider to this job order.'); // check if lat and lng are provided if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) { @@ -436,7 +435,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); // check if lat and lng are provided if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) { @@ -523,7 +522,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // make sure this object exists if (empty($obj)) - throw $this->createNotFoundException('The item does not exist'); + throw new NotFoundHttpException('The item does not exist'); $cancel_reason = $req->request->get('cancel_reason'); $obj->cancel($cancel_reason); @@ -604,7 +603,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if (empty($cv)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } $jo = new JobOrder(); @@ -630,7 +629,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // make sure this row exists if (empty($obj)) - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); $this->fillDropdownParameters($params); $this->fillFormTags($params); @@ -686,14 +685,14 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if (empty($obj)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } // check status if ($obj->getStatus() != JOStatus::PENDING) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not have a pending status'); + throw new NotFoundHttpException('The job order does not have a pending status'); } // check if we are the processor @@ -719,7 +718,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } catch(PessimisticLockException $e) { - throw $this->createAccessDeniedException('Not the processor'); + throw new AccessDeniedHttpException('Not the processor'); } // NOTE: we are able to lock, everything should be fine now @@ -808,14 +807,14 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if (empty($obj)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not exist'); + throw new NotFoundHttpException('The job order does not exist'); } // check status if ($obj->getStatus() != JOStatus::RIDER_ASSIGN) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order does not have an assigning status'); + throw new NotFoundHttpException('The job order does not have an assigning status'); } // check if super user @@ -833,7 +832,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if (!in_array($obj->getHub()->getID(), $user_hubs)) { $em->getConnection()->rollback(); - throw $this->createNotFoundException('The job order is not on a hub assigned to this user'); + throw new NotFoundHttpException('The job order is not on a hub assigned to this user'); } // check if we are the assignor @@ -842,7 +841,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if ($assignor != null && $assignor->getID() != $user->getID()) { $em->getConnection()->rollback(); - throw $this->createAccessDeniedException('Not the assignor'); + throw new AccessDeniedHttpException('Not the assignor'); } // make this user be the assignor @@ -856,7 +855,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } catch (PessimisticLockException $e) { - throw $this->createAccessDeniedException('Not the assignor'); + throw new AccessDeniedHttpException('Not the assignor'); } $this->fillDropdownParameters($params); @@ -866,8 +865,123 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $params['status_cancelled'] = JOStatus::CANCELLED; return $params; + } + // initialize fulflll job order form + public function initializeFulfillmentForm($id) + { + $em = $this->em; + $params['mode'] = 'update-fulfillment'; + + // get row data + $obj = $em->getRepository(JobOrder::class)->find($id); + + // make sure this row exists + if (empty($obj)) + { + throw new NotFoundHttpException('The job order does not exist'); + } + + // check status + if (!in_array($obj->getStatus(), [JOStatus::ASSIGNED, JOStatus::IN_PROGRESS])) + { + throw new NotFoundHttpException('The job order does not have a fulfillment status'); + } + + // check if hub is assigned to current user + $user_hubs = $this->getUser()->getHubs(); + if (!in_array($obj->getHub()->getID(), $user_hubs)) + { + throw new NotFoundHttpException('The job order is not on a hub assigned to this user'); + } + + $this->fillDropdownParameters($params); + $this->fillFormTags($params); + + $params['obj'] = $obj; + $params['status_cancelled'] = JOStatus::CANCELLED; + + return $params; + } + + // initialize hub form + public function initializeHubForm($id, $map_tools) + { + $em = $this->em; + + $params['mode'] = 'update-reassign-hub'; + + // get row data + $obj = $em->getRepository(JobOrder::class)->find($id); + + // make sure this row exists + if (empty($obj)) + { + throw new NotFoundHttpException('The job order does not exist'); + } + + $this->fillDropdownParameters($params); + $this->fillFormTags($params); + + // get rejections + $rejections = $obj->getHubRejections(); + + // get rejection reasons + $params['rejection_reasons'] = JORejectionReason::getCollection(); + + // get closest hubs + $hubs = $map_tools->getClosestHubs($obj->getCoordinates(), 50, date("H:i:s")); + + $params['status_cancelled'] = JOStatus::CANCELLED; + $params['hubs'] = []; + + // format duration and distance into friendly time + foreach ($hubs as $hub) { + // duration + $seconds = $hub['duration']; + + if (!empty($seconds) && $seconds > 0) { + $hours = floor($seconds / 3600); + $minutes = ceil(($seconds / 60) % 60); + + $hub['duration'] = ($hours > 0 ? number_format($hours) . " hr" . ($hours > 1 ? "s" : '') . ($minutes > 0 ? ", " : '') : '') . ($minutes > 0 ? number_format($minutes) . " min" . ($minutes > 1 ? "s" : '') : ''); + } else { + $hub['duration'] = false; + } + + // distance + $meters = $hub['distance']; + + if (!empty($meters) && $meters > 0) { + $hub['distance'] = round($meters / 1000) . " km"; + } else { + $hub['distance'] = false; + } + + // counters + $hub['rider_count'] = count($hub['hub']->getAvailableRiders()); + $hub['jo_count'] = count($hub['hub']->getForAssignmentJobOrders()); + + // check for rejection + $hub['flag_rejected'] = false; + $hub_id = $hub['hub']->getID(); + + foreach ($rejections as $robj) + { + if ($robj->getHub()->getID() === $hub_id) + { + $hub['flag_rejected'] = true; + break; + } + } + + $params['hubs'][] = $hub; + } + + $params['obj'] = $obj; + + return $params; } protected function fillDropdownParameters(&$params) diff --git a/src/Service/JobOrderHandlerInterface.php b/src/Service/JobOrderHandlerInterface.php index 0add57a5..d1b8854e 100644 --- a/src/Service/JobOrderHandlerInterface.php +++ b/src/Service/JobOrderHandlerInterface.php @@ -42,4 +42,10 @@ interface JobOrderHandlerInterface // initialize assign job order form public function initializeAssignForm(int $id); + + // initialize fulflll job order form + public function initializeFulfillmentForm(int $id); + + // initialize hub form + public function initializeHubForm(int $id, MapTools $map_tools); }