diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 038ac73b..9b5fd28c 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -51,6 +51,7 @@ use App\Entity\PrivacyPolicy; use App\Entity\Hub; use DateTime; +use DateInterval; use Exception; // mobile API @@ -2289,8 +2290,7 @@ class APIController extends Controller implements LoggedController return $cust; } - protected function findNearestHub($jo, EntityManagerInterface $em, - MapTools $map_tools) + protected function findNearestHub($jo, EntityManagerInterface $em, MapTools $map_tools) { // get the nearest 10 hubs $selected_hub = null; @@ -2314,7 +2314,33 @@ class APIController extends Controller implements LoggedController $nearest = null; foreach ($nearest_hubs_with_distance as $nhd) { - if (count($nhd['hub']->getAvailableRiders()) > 0) + // get number of available riders + $count_riders = count($nhd['hub']->getAvailableRiders()); + + // get number of advance orders in the next 3 hours + $time_now = new DateTime(); + $date_end = new DateTime(); + $date_end->add(new DateInterval('PT2H')); + + // NOTE: get advance orders via query + // get JOs assigned to hub that are advance orders and scheduled within 3 hours with + // for rider assignment status + $query = $em->createQuery('select count(jo) from App\Entity\JobOrder jo where jo.hub = :hub and jo.flag_advance = true and jo.date_schedule <= :date_end and jo.status = :status'); + $count_advance_orders = $query->setParameters([ + 'hub' => $nhd['hub'], + 'date_end' => $date_end, + 'status' => JOStatus::RIDER_ASSIGN, + ]) + ->setMaxResults(1) + ->getSingleScalarResult(); + + error_log('HUB - ' . $nhd['hub']->getID()); + error_log('RIDER COUNT - ' . $count_riders); + error_log('ADVANCE ORDER COUNT - ' . $count_advance_orders); + + // if (count($nhd['hub']->getAvailableRiders()) > 0) + // if we have more riders than we have advance orders + if ($count_riders - $count_advance_orders > 0) { if (empty($nearest)) $nearest = $nhd;