diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index b87bc73b..cfe35773 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -2814,49 +2814,81 @@ class APIController extends Controller implements LoggedController { // go through the hub list, find the nearest hub // with an available rider - //error_log('found nearest hub ' . $nearest_hub->getID()); + // error_log('found nearest hub ' . $nearest_hub->getID()); foreach ($nearest_hubs as $nearest_hub) { - $available_riders = $nearest_hub['hub']->getAvailableRiders(); - if (count($available_riders) >= 1) + // check if hub can be auto assigned + // if not, move on to the next hub in the list + if (($nearest_hub['hub']->isHubAutoAssign())) { - $assigned_rider = null; - if (count($available_riders) == 1) + // check if hub has riders that can be auto assigned + // if not, move on to the next hub + if (($nearest_hub['hub']->isRiderAutoAssign())) { - $assigned_rider = $available_riders[0]; + $available_riders = $nearest_hub['hub']->getAvailableRiders(); + if (count($available_riders) >= 1) + { + $assigned_rider = null; + if (count($available_riders) == 1) + { + $assigned_rider = $available_riders[0]; + } + else + { + // TODO: the setting of riders into an array + // will no longer be necessary when the contents + // of randomizeRider changes + $riders = []; + foreach ($available_riders as $rider) + { + $riders[] = $rider; + } + + $assigned_rider = $this->randomizeRider($riders); + } + + $jo->setHub($nearest_hub['hub']); + $jo->setRider($assigned_rider); + $jo->setStatus(JOStatus::ASSIGNED); + $jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED); + + $assigned_rider->setAvailable(false); + + // update redis hub_jo_count for hub + $hub_dist->incrementJoCountForHub($nearest_hub['hub']); + + // break out of loop + break; + } + else + { + // we just create the JO and let admin panel handle the hub assignment + // log hub into hub_filter_log + $hub_filter_logger->logFilteredHub($nearest_hub['hub'], 'no_available_rider', null, $cust->getID()); + // continue to go through list to find hub with an available rider + } } else { - // TODO: the setting of riders into an array - // will no longer be necessary when the contents - // of randomizeRider changes - $riders = []; - foreach ($available_riders as $rider) - { - $riders[] = $rider; - } + // TODO: log hub as cannot be auto rider assigned somewhere + // assign hub + error_log('Rider cannot be auto assigned ' . $nearest_hub['hub']->getID()); + $jo->setHub($nearest_hub['hub']); + $jo->setStatus(JOStatus::RIDER_ASSIGN); + $jo->setStatusAutoAssign(AutoAssignStatus::HUB_ASSIGNED); - $assigned_rider = $this->randomizeRider($riders); + // update redis hub_jo_count for hub + $hub_dist->incrementJoCountForHub($nearest_hub['hub']); + + break; } - - $jo->setHub($nearest_hub['hub']); - $jo->setRider($assigned_rider); - $jo->setStatus(JOStatus::ASSIGNED); - $jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED); - - $assigned_rider->setAvailable(false); - - // update redis hub_jo_count for hub - $hub_dist->incrementJoCountForHub($nearest_hub['hub']); - - // break out of loop - break; } else { - // log hub into hub_filter_log - $hub_filter_logger->logFilteredHub($nearest_hub['hub'], 'no_available_rider', null, $cust->getID()); - // continue to go through list to find hub with an available rider + // TODO: log hub as cannot be auto assigned somewhere + // move to next hub + error_log('Hub cannot be auto-assigned ' . $nearest_hub['hub']->getID()); + continue; } } } diff --git a/src/Controller/HubController.php b/src/Controller/HubController.php index 21c4758d..09eca4bf 100644 --- a/src/Controller/HubController.php +++ b/src/Controller/HubController.php @@ -161,6 +161,8 @@ class HubController extends Controller ->setRiderSlots($req->request->get('rider_slots', 0)) ->setHubViewFlag($req->request->get('flag_hub_view', false)) ->setNotifNumber($req->request->get('notif_number')) + ->setHubAutoAssign($req->request->get('flag_hub_auto_assign', false)) + ->setRiderAutoAssign($req->request->get('flag_rider_auto_assign', false)) ->clearPaymentMethods(); // set payment methods diff --git a/src/Entity/Hub.php b/src/Entity/Hub.php index 2d742b73..da3a44b9 100644 --- a/src/Entity/Hub.php +++ b/src/Entity/Hub.php @@ -80,6 +80,18 @@ class Hub */ protected $payment_methods; + // flag if hub can be auto assigned + /** + * @ORM\Column(type="boolean", options={"default"=false}) + */ + protected $flag_hub_auto_assign; + + // flag if riders assigned to hub can be auto assigned + /** + * @ORM\Column(type="boolean", options={"default"=false}) + */ + protected $flag_rider_auto_assign; + public function __construct() { $this->time_open = new DateTime(); @@ -90,6 +102,8 @@ class Hub $this->flag_hub_view = false; $this->notif_number = ''; $this->payment_methods = new ArrayCollection(); + $this->flag_hub_auto_assign = false; + $this->flag_rider_auto_assign = false; } public function getRiders() @@ -232,4 +246,28 @@ class Hub $this->payment_methods = new ArrayCollection(); return $this; } + + public function setHubAutoAssign($flag_hub_auto_assign = true) + { + $this->flag_hub_auto_assign = $flag_hub_auto_assign; + return $this; + } + + public function isHubAutoAssign() + { + return $this->flag_hub_auto_assign; + } + + public function setRiderAutoAssign($flag_rider_auto_assign = true) + { + $this->flag_rider_auto_assign = $flag_rider_auto_assign; + return $this; + } + + public function isRiderAutoAssign() + { + return $this->flag_rider_auto_assign; + } + + } diff --git a/src/Service/MapTools.php b/src/Service/MapTools.php index e1eac52e..13e3c9f8 100644 --- a/src/Service/MapTools.php +++ b/src/Service/MapTools.php @@ -154,11 +154,13 @@ class MapTools */ } - // NOTE: only the API calls this + // NOTE: only the API(findAdvanceNearestHubAndSlots) calls this public function getClosestOpenHubs(Point $point, $limit, $time = false) { // get closest hubs based on st_distance function from db - $query_string = 'SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h WHERE h.status_open = true'; + $query_string = 'SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h + WHERE h.status_open = true + AND h.flag_hub_auto_assign = true'; if ($time) $query_string .= ' AND :time BETWEEN h.time_open AND h.time_close'; $query_string .= ' ORDER BY dist'; @@ -172,7 +174,7 @@ class MapTools $query->setMaxResults($limit); - // error_log($query->getSql()); + //error_log($query->getSql()); $result = $query->getResult(); $hubs = []; diff --git a/templates/hub/form.html.twig b/templates/hub/form.html.twig index 3c8947c1..9ed950f4 100644 --- a/templates/hub/form.html.twig +++ b/templates/hub/form.html.twig @@ -118,6 +118,22 @@ +