diff --git a/config/routes/rider.yaml b/config/routes/rider.yaml index 16a56993..1934a1b1 100644 --- a/config/routes/rider.yaml +++ b/config/routes/rider.yaml @@ -46,3 +46,13 @@ rider_active_jo: path: /riders/{id}/activejo/{jo_id} controller: App\Controller\RiderController::riderActiveJO methods: [GET] + +rider_priority_up_jo: + path: /riders/{id}/priority_up/{jo_id} + controller: App\Controller\RiderController::priorityUpJO + methods: [GET] + +rider_priority_down_jo: + path: /riders/{id}/priority_down/{jo_id} + controller: App\Controller\RiderController::priorityDownJO + methods: [GET] diff --git a/src/Controller/RiderController.php b/src/Controller/RiderController.php index ace84582..43f5d6d6 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -534,6 +534,66 @@ class RiderController extends Controller $mclient->sendRiderEvent($jo, $payload); + return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]); + } + + /** + * @ParamConverter("rider", class="App\Entity\Rider") + * @ParamConverter("jo", class="App\Entity\JobOrder", options={"id": "jo_id"}) + */ + public function priorityUpJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo) + { + $jos = $rider->getOpenJobOrders(); + + // set new priority + $old_prio = $jo->getPriority(); + $new_prio = $old_prio - 1; + $jo->setPriority($new_prio); + + // go through all rider open JOs and set priority when needed + foreach ($jos as $rider_jo) + { + // check if it's the same + if ($rider_jo->getID() == $jo->getID()) + continue; + + // if priority is the same as old priority, move it down + if ($new_prio == $rider_jo->getPriority()) + $rider_jo->setPriority($rider_jo->getPriority() + 1); + } + + $em->flush(); + + return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]); + } + + /** + * @ParamConverter("rider", class="App\Entity\Rider") + * @ParamConverter("jo", class="App\Entity\JobOrder", options={"id": "jo_id"}) + */ + public function priorityDownJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo) + { + $jos = $rider->getOpenJobOrders(); + + // set new priority + $old_prio = $jo->getPriority(); + $new_prio = $old_prio + 1; + $jo->setPriority($new_prio); + + // go through all rider open JOs and set priority when needed + foreach ($jos as $rider_jo) + { + // check if it's the same + if ($rider_jo->getID() == $jo->getID()) + continue; + + // if priority is the same as old priority, move it down + if ($new_prio == $rider_jo->getPriority()) + $rider_jo->setPriority($rider_jo->getPriority() - 1); + } + + $em->flush(); + return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]); } } diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index 9e902e98..8f5e78e7 100644 --- a/src/Entity/JobOrder.php +++ b/src/Entity/JobOrder.php @@ -280,6 +280,14 @@ class JobOrder */ protected $hub_rejections; + // priority order for riders + // NOTE: this is a workaround since changeing rider to jo rider assignment with details requires + // too many changes and may break too many things. + /** + * @ORM\Column(type="integer", options={"default": 0})) + */ + protected $priority; + // meta /** * @ORM\Column(type="json") @@ -304,6 +312,7 @@ class JobOrder $this->flag_rider_rating = false; $this->flag_coolant = false; + $this->priority = 0; $this->meta = []; } @@ -811,6 +820,17 @@ class JobOrder return $this->hub_rejections; } + public function setPriority($priority) + { + $this->priority = $priority; + return $this; + } + + public function getPriority() + { + return $this->priority; + } + public function addMeta($id, $value) { $this->meta[$id] = $value; @@ -825,5 +845,4 @@ class JobOrder return $this->meta[$id]; } - } diff --git a/src/Entity/Rider.php b/src/Entity/Rider.php index 53731475..c588cc77 100644 --- a/src/Entity/Rider.php +++ b/src/Entity/Rider.php @@ -61,6 +61,7 @@ class Rider // job orders that the rider has done /** * @ORM\OneToMany(targetEntity="JobOrder", mappedBy="rider") + * @ORM\OrderBy({"priority" = "ASC"}) */ protected $job_orders; diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 0e676014..50ef719f 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -523,6 +523,19 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface } } + // set priority based on rider's existing open job orders + $rider_jos = $rider->getOpenJobOrders(); + + // get maximum priority then add 1 + // NOTE: this can be a bit buggy due to concurrency issues + // ideally have to lock jo table, but that isn't feasible right now + $priority = 0; + foreach ($rider_jos as $rider_jo) + { + if ($priority < $rider_jo->getPriority()) + $priority = $rider_jo->getPriority() + 1; + } + // get discount and set to meta $discount = $req->request->get('invoice_discount', []); @@ -560,7 +573,8 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface ->setModeOfPayment($req->request->get('mode_of_payment')) ->setLandmark($req->request->get('landmark')) ->setHub($hub) - ->setRider($rider); + ->setRider($rider) + ->setPriority($priority); $jo->addMeta('discount', $discount); $jo->addMeta('service_charges', $service_charges); diff --git a/templates/rider/form.html.twig b/templates/rider/form.html.twig index a9b0264c..c1c52708 100644 --- a/templates/rider/form.html.twig +++ b/templates/rider/form.html.twig @@ -180,6 +180,16 @@