From 1ab5909dfd6722b03369abbec45764f1ad659ff1 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sat, 29 Feb 2020 23:38:05 +0800 Subject: [PATCH 1/3] Add priority field for JobOrder #360 --- src/Entity/JobOrder.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index 36834cc1..faa427b0 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; + public function __construct() { $this->date_create = new DateTime(); @@ -297,6 +305,8 @@ class JobOrder $this->trade_in_type = null; $this->flag_rider_rating = false; $this->flag_coolant = false; + + $this->priority = 0; } public function getID() @@ -802,4 +812,15 @@ class JobOrder { return $this->hub_rejections; } + + public function setPriority($priority) + { + $this->priority = $priority; + return $this; + } + + public function getPriority() + { + return $this->priority; + } } From 07753274c33d8e6acd879b8e5481ebaa5bd5af4a Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 1 Mar 2020 22:14:13 +0800 Subject: [PATCH 2/3] Add initial job order prioritization features #360 --- config/routes/rider.yaml | 10 +++++++ src/Controller/RiderController.php | 45 ++++++++++++++++++++++++++++++ src/Entity/Rider.php | 1 + templates/rider/form.html.twig | 10 +++++++ 4 files changed, 66 insertions(+) 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..184a649d 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -534,6 +534,51 @@ 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) + { + error_log("HERE"); + error_log($rider->getID()); + error_log($jo->getID()); + + $jos = $rider->getOpenJobOrders(); + + $old_prio = $jo->getPriority(); + $new_prio = $old_prio - 1; + $jo->setPriority($new_prio); + + 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) + { + error_log("HERE"); + error_log($rider->getID()); + error_log($jo->getID()); + return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]); } } 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/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 @@ {{ jo.getDeliveryAddress|default('') }} {% if jo.getID == active_jo_id %}Active{% endif %} + + + + + + + + + + {% if jo.getID != active_jo_id %} From 977559b88dd732279cdd7bbb77f8f3a8fed55461 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Mon, 2 Mar 2020 08:33:50 +0800 Subject: [PATCH 3/3] Set priority properly for job order and handle priority down button #360 --- src/Controller/RiderController.php | 29 ++++++++++++++----- .../JobOrderHandler/CMBJobOrderHandler.php | 16 +++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Controller/RiderController.php b/src/Controller/RiderController.php index 184a649d..43f5d6d6 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -543,16 +543,14 @@ class RiderController extends Controller */ public function priorityUpJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo) { - error_log("HERE"); - error_log($rider->getID()); - error_log($jo->getID()); - $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 @@ -575,9 +573,26 @@ class RiderController extends Controller */ public function priorityDownJO(EntityManagerInterface $em, Rider $rider, JobOrder $jo) { - error_log("HERE"); - error_log($rider->getID()); - error_log($jo->getID()); + $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/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);