Add all other services to auto assign hub / rider #374

This commit is contained in:
Kendrick Chan 2020-04-11 18:37:35 +08:00
parent 9c0f86e588
commit 1ee744eba0

View file

@ -992,6 +992,12 @@ class APIController extends Controller implements LoggedController
{ {
// 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);
}
else
{
$nearest_hub = $this->findNearestHub($jo, $em, $map_tools, $im);
}
if (!empty($nearest_hub)) if (!empty($nearest_hub))
{ {
// assign rider // assign rider
@ -1020,7 +1026,6 @@ class APIController extends Controller implements LoggedController
$jo->setStatus(JOStatus::ASSIGNED); $jo->setStatus(JOStatus::ASSIGNED);
} }
} }
}
$em->persist($jo); $em->persist($jo);
$em->persist($invoice); $em->persist($invoice);
@ -2253,6 +2258,48 @@ class APIController extends Controller implements LoggedController
return $cust; return $cust;
} }
protected function findNearestHub($jo, Battery $batt, EntityManagerInterface $em,
MapTools $map_tools, InventoryManager $im)
{
// get the nearest 10 hubs
$selected_hub = null;
$hubs = $map_tools->getClosestOpenHubs($jo->getCoordinates(), 10, date("H:i:s"));
$nearest_hubs_with_distance = [];
$nearest_branch_codes = [];
foreach ($hubs as $hub)
{
$nearest_hubs_with_distance[] = $hub;
//if (!empty($hub['hub']->getBranchCode()))
// $nearest_branch_codes[] = $hub['hub']->getBranchCode();
}
// check if nearest hubs have branch codes
//if (count($nearest_branch_codes) == 0)
// return $selected_hub;
// assume all 10 have stock
// find the nearest hub with available riders
$nearest = null;
foreach ($nearest_hubs_with_distance as $nhd)
{
if (count($nhd['hub']->getAvailableRiders()) > 0)
{
if (empty($nearest))
$nearest = $nhd;
else
{
if ($nhd['distance'] < $nearest['distance'])
$nearest = $nhd;
}
}
}
$selected_hub = $nearest['hub'];
return $selected_hub;
}
protected function findNearestHubWithInventory($jo, Battery $batt, EntityManagerInterface $em, protected function findNearestHubWithInventory($jo, Battery $batt, EntityManagerInterface $em,
MapTools $map_tools, InventoryManager $im) MapTools $map_tools, InventoryManager $im)
{ {