diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index e608ddd8..3d39e167 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -1555,82 +1555,23 @@ class JobOrderController extends Controller return $this->redirectToRoute('jo_assign'); } - public function rejectHubSubmit(Request $req, ValidatorInterface $validator, $id) + public function rejectHubSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id) { $this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.'); - // get object data - $em = $this->getDoctrine()->getManager(); - $jo = $em->getRepository(JobOrder::class)->find($id); - $processor = $jo->getProcessedBy(); - $user = $this->getUser(); - - // check if we're the one processing, return error otherwise - if ($processor == null) - throw $this->createAccessDeniedException('Not the processor'); - - if ($processor != null && $processor->getID() != $user->getID()) - throw $this->createAccessDeniedException('Not the processor'); - // initialize error list $error_array = []; - - // make sure job order exists - if (empty($jo)) - throw $this->createNotFoundException('The item does not exist'); - - // check if hub is set - if (empty($req->request->get('hub'))) + try { - $error_array['hub'] = 'No hub selected.'; + $error_array = $jo_handler->rejectHub($req, $id); } - else + catch (AccessDeniedHttpException $e) { - // get hub - $hub = $em->getRepository(Hub::class)->find($req->request->get('hub')); - - if (empty($hub)) - { - $error_array['hub'] = 'Invalid hub specified.'; - } + throw $this->createAccessDeniedException($e->getMessage()); } - - // check if this hub has already been rejected on this job order - $robj = $em->getRepository(JORejection::class)->findOneBy([ - 'job_order' => $jo, - 'hub' => $hub - ]); - - if (!empty($robj)) - $error_array['hub'] = 'This hub has already been rejected for the current job order.'; - - // check if reason is set - if (empty($req->request->get('reason'))) - $error_array['reason'] = 'No reason selected.'; - else if (!JORejectionReason::validate($req->request->get('reason'))) - $error_array['reason'] = 'Invalid reason specified.'; - - if (empty($error_array)) + catch (NotFoundHttpException $e) { - // coordinates - $obj = new JORejection(); - - // set and save values - $obj->setDateCreate(new DateTime()) - ->setHub($hub) - ->setUser($this->getUser()) - ->setJobOrder($jo) - ->setReason($req->request->get('reason')) - ->setRemarks($req->request->get('remarks')) - ->setContactPerson($req->request->get('contact_person')); - - // validate - $errors = $validator->validate($obj); - - // add errors to list - foreach ($errors as $error) { - $error_array[$error->getPropertyPath()] = $error->getMessage(); - } + throw $this->createNotFoundException($e->getMessage()); } // check if any errors were found @@ -1643,10 +1584,6 @@ class JobOrderController extends Controller ], 422); } - // validated! save the entity - $em->persist($obj); - $em->flush(); - // return successful response return $this->json([ 'success' => 'Changes have been saved!', diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index caf79834..a151f76e 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -663,6 +663,101 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface return $error_array; } + // reject hub for job order + public function rejectHub($req, $id) + { + // get object data + $em = $this->em; + $jo = $em->getRepository(JobOrder::class)->find($id); + $processor = $jo->getProcessedBy(); + $user = $this->security->getUser(); + + // check if we're the one processing, return error otherwise + if ($processor == null) + throw new AccessDeniedHttpException('Not the processor'); + + if ($user != null) + { + if ($processor != null && $processor->getID() != $user->getID()) + throw new AccessDeniedHttpException('Not the processor'); + } + + // initialize error list + $error_array = []; + + // make sure job order exists + if (empty($jo)) + throw new NotFoundHttpException('The item does not exist'); + + // check if hub is set + if (empty($req->request->get('hub'))) + { + $error_array['hub'] = 'No hub selected.'; + } + else + { + // get hub + $hub = $em->getRepository(Hub::class)->find($req->request->get('hub')); + + if (empty($hub)) + { + $error_array['hub'] = 'Invalid hub specified.'; + } + } + + // check if this hub has already been rejected on this job order + $robj = $em->getRepository(JORejection::class)->findOneBy([ + 'job_order' => $jo, + 'hub' => $hub + ]); + + if (!empty($robj)) + $error_array['hub'] = 'This hub has already been rejected for the current job order.'; + + // check if reason is set + if (empty($req->request->get('reason'))) + $error_array['reason'] = 'No reason selected.'; + else if (!JORejectionReason::validate($req->request->get('reason'))) + $error_array['reason'] = 'Invalid reason specified.'; + + if (empty($error_array)) + { + // coordinates + $obj = new JORejection(); + + // set and save values + $obj->setDateCreate(new DateTime()) + ->setHub($hub) + ->setJobOrder($jo) + ->setReason($req->request->get('reason')) + ->setRemarks($req->request->get('remarks')) + ->setContactPerson($req->request->get('contact_person')); + + if ($user != null) + { + $obj->setUser($user); + } + + // validate + $errors = $this->validator->validate($obj); + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + } + + if (empty($error_array)) + { + // validated! save the entity + $em->persist($obj); + $em->flush(); + } + + return $error_array; + + } + // set rider for job order public function setRider($req, $id, $mclient) { diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index ab8898af..9d27af14 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -663,6 +663,101 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface return $error_array; } + // reject hub for job order + public function rejectHub($req, $id) + { + // get object data + $em = $this->em; + $jo = $em->getRepository(JobOrder::class)->find($id); + $processor = $jo->getProcessedBy(); + $user = $this->security->getUser(); + + // check if we're the one processing, return error otherwise + if ($processor == null) + throw new AccessDeniedHttpException('Not the processor'); + + if ($user != null) + { + if ($processor != null && $processor->getID() != $user->getID()) + throw new AccessDeniedHttpException('Not the processor'); + } + + // initialize error list + $error_array = []; + + // make sure job order exists + if (empty($jo)) + throw new NotFoundHttpException('The item does not exist'); + + // check if hub is set + if (empty($req->request->get('hub'))) + { + $error_array['hub'] = 'No hub selected.'; + } + else + { + // get hub + $hub = $em->getRepository(Hub::class)->find($req->request->get('hub')); + + if (empty($hub)) + { + $error_array['hub'] = 'Invalid hub specified.'; + } + } + + // check if this hub has already been rejected on this job order + $robj = $em->getRepository(JORejection::class)->findOneBy([ + 'job_order' => $jo, + 'hub' => $hub + ]); + + if (!empty($robj)) + $error_array['hub'] = 'This hub has already been rejected for the current job order.'; + + // check if reason is set + if (empty($req->request->get('reason'))) + $error_array['reason'] = 'No reason selected.'; + else if (!JORejectionReason::validate($req->request->get('reason'))) + $error_array['reason'] = 'Invalid reason specified.'; + + if (empty($error_array)) + { + // coordinates + $obj = new JORejection(); + + // set and save values + $obj->setDateCreate(new DateTime()) + ->setHub($hub) + ->setJobOrder($jo) + ->setReason($req->request->get('reason')) + ->setRemarks($req->request->get('remarks')) + ->setContactPerson($req->request->get('contact_person')); + + if ($user != null) + { + $obj->setUser($user); + } + + // validate + $errors = $this->validator->validate($obj); + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + } + + if (empty($error_array)) + { + // validated! save the entity + $em->persist($obj); + $em->flush(); + } + + return $error_array; + + } + // set rider for job order public function setRider($req, $id, $mclient) { diff --git a/src/Service/JobOrderHandlerInterface.php b/src/Service/JobOrderHandlerInterface.php index 5275b7cc..f371e2e2 100644 --- a/src/Service/JobOrderHandlerInterface.php +++ b/src/Service/JobOrderHandlerInterface.php @@ -28,6 +28,9 @@ interface JobOrderHandlerInterface // set hub for job order public function setHub(Request $req, int $id, MQTTClient $mclient); + // reject hub for job order + public function rejectHub(Request $req, int $id); + // set rider for job order public function setRider(Request $req, int $id, MQTTClient $mclient);