Filter only open hubs for API autoassign #374

This commit is contained in:
Kendrick Chan 2020-04-11 11:44:45 +08:00
parent 3cc20c7410
commit 9c0f86e588
2 changed files with 47 additions and 1 deletions

View file

@ -2258,7 +2258,7 @@ class APIController extends Controller implements LoggedController
{
// get the nearest 10 hubs
$selected_hub = null;
$hubs = $map_tools->getClosestHubs($jo->getCoordinates(), 10, date("H:i:s"));
$hubs = $map_tools->getClosestOpenHubs($jo->getCoordinates(), 10, date("H:i:s"));
$nearest_hubs_with_distance = [];
$nearest_branch_codes = [];

View file

@ -148,6 +148,52 @@ class MapTools
return $final_data;
*/
}
public function getClosestOpenHubs(Point $point, $limit, $time = false)
{
// get closest hubs based on st_distance function from db
$query = $this->em->createQuery('SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h' . ($time ? ' WHERE h.status_open = true AND :time BETWEEN h.time_open AND h.time_close' : '') . ' ORDER BY dist')
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
if ($time) {
$query->setParameter('time', $time);
}
$query->setMaxResults($limit);
// error_log($query->getSql());
$result = $query->getResult();
$hubs = [];
$final_data = [];
foreach ($result as $row)
{
//error_log($row[0]->getName() . ' - ' . $row['dist']);
$hubs[] = $row[0];
// get coordinates of hub
$hub_coordinates = $row[0]->getCoordinates();
$cust_lat = $point->getLatitude();
$cust_lng = $point->getLongitude();
$hub_lat = $hub_coordinates->getLatitude();
$hub_lng = $hub_coordinates->getLongitude();
// get distance in kilometers from customer point to hub point
$dist = $this->distance($cust_lat, $cust_lng, $hub_lat, $hub_lng);
$final_data[] = [
'hub' => $row[0],
'db_distance' => $row['dist'],
'distance' => $dist,
'duration' => 0,
];
}
return $final_data;
}
protected function distance($lat1, $lon1, $lat2, $lon2)
{