diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 364215c1..49762cad 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -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 = []; diff --git a/src/Service/MapTools.php b/src/Service/MapTools.php index 6aa5848c..17511fba 100644 --- a/src/Service/MapTools.php +++ b/src/Service/MapTools.php @@ -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) {