Merge branch '378-advance-order-auto-assign-algorithm' into 'master'

Resolve "Advance order auto assign algorithm"

Closes #378

See merge request jankstudio/resq!423
This commit is contained in:
Kendrick Chan 2020-04-15 07:18:01 +00:00
commit 0cb3a137fa

View file

@ -51,6 +51,7 @@ use App\Entity\PrivacyPolicy;
use App\Entity\Hub; use App\Entity\Hub;
use DateTime; use DateTime;
use DateInterval;
use Exception; use Exception;
// mobile API // mobile API
@ -993,7 +994,8 @@ class APIController extends Controller implements LoggedController
($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY)) ($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY))
{ {
// get nearest hub // get nearest hub
$nearest_hub = $this->findNearestHubWithInventory($jo, $batt, $em, $map_tools, $im); // $nearest_hub = $this->findNearestHubWithInventory($jo, $batt, $em, $map_tools, $im);
$nearest_hub = $this->findNearestHub($jo, $em, $map_tools);
} }
else else
{ {
@ -2289,8 +2291,7 @@ class APIController extends Controller implements LoggedController
return $cust; return $cust;
} }
protected function findNearestHub($jo, EntityManagerInterface $em, protected function findNearestHub($jo, EntityManagerInterface $em, MapTools $map_tools)
MapTools $map_tools)
{ {
// get the nearest 10 hubs // get the nearest 10 hubs
$selected_hub = null; $selected_hub = null;
@ -2314,7 +2315,33 @@ class APIController extends Controller implements LoggedController
$nearest = null; $nearest = null;
foreach ($nearest_hubs_with_distance as $nhd) 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 X 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)) if (empty($nearest))
$nearest = $nhd; $nearest = $nhd;