Filter only open hubs for API autoassign #374
This commit is contained in:
parent
3cc20c7410
commit
9c0f86e588
2 changed files with 47 additions and 1 deletions
|
|
@ -2258,7 +2258,7 @@ class APIController extends Controller implements LoggedController
|
||||||
{
|
{
|
||||||
// get the nearest 10 hubs
|
// get the nearest 10 hubs
|
||||||
$selected_hub = null;
|
$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_hubs_with_distance = [];
|
||||||
$nearest_branch_codes = [];
|
$nearest_branch_codes = [];
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,52 @@ class MapTools
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
protected function distance($lat1, $lon1, $lat2, $lon2)
|
||||||
{
|
{
|
||||||
if (($lat1 == $lat2) && ($lon1 == $lon2))
|
if (($lat1 == $lat2) && ($lon1 == $lon2))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue