diff --git a/config/services.yaml b/config/services.yaml index 75c83d1d..8f05c28b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -266,3 +266,7 @@ services: event: 'postPersist' entity: 'App\Entity\CustomerVehicle' + # hub service + App\Service\HubSelector: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 94e9fb38..8f268309 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -1011,12 +1011,14 @@ class APIController extends Controller implements LoggedController if (($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) || ($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY)) { + // TODO: call HubSelector service here // get nearest hub // $nearest_hub = $this->findNearestHubWithInventory($jo, $batt, $em, $map_tools, $im); $nearest_hub = $this->findNearestHub($jo, $em, $map_tools); } else { + // TODO: call HubSelector service here $nearest_hub = $this->findNearestHub($jo, $em, $map_tools); } diff --git a/src/Service/HubSelector.php b/src/Service/HubSelector.php new file mode 100644 index 00000000..cf0d37a7 --- /dev/null +++ b/src/Service/HubSelector.php @@ -0,0 +1,123 @@ +em = $em; + } + + public function find(HubCriteria $criteria) + { + $point = $criteria->getPoint(); + $limit_results = $criteria->getLimitResults(); + $limit_distance = $criteria->getLimitDistance(); + $jo_type = $criteria->getJoType(); + $has_inventory = $criteria->hasInventory(); + $items = $criteria->getItems(); + $day_time = $criteria->getDayTime(); + + // check for jo type + if (($jo_type == ServiceType::BATTERY_REPLACEMENT_NEW)) || + ($jo_type == ServiceType::BATTERY_REPLACEMENT_WARRANTY)) + { + // check if we need to get inventory + if ($has_inventory) + { + if ($day_time != null) + { + // call function for jo type, inventory == true, and check for day and time + } + else + { + // call function for jo type and inventory == true + } + } + else + { + if ($day_time != null) + { + // call function for jo type and check for day and time + } + else + { + // call function for jo type only + } + } + } + + // TODO: this needs to go to a function.. somewhere + // get closest hubs based on st_distance function from db + $query_string = 'SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h WHERE h.status_open = true'; + + $query_string .= ' ORDER BY dist'; + + $query = $this->em->createQuery($query_string) + ->setParameter('lat', $point->getLatitude()) + ->setParameter('lng', $point->getLongitude()); + + $query->setMaxResults($limit); + + // error_log($query->getSql()); + $result = $query->getResult(); + + $hubs = []; + $final_data = []; + foreach ($result as $row) + { + $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); + + if ($dist < $limit_distance) + { + $final_data[] = [ + 'hub' => $row[0], + 'db_distance' => $row['dist'], + 'distance' => $dist, + 'duration' => 0, + ]; + } + } + + return $final_data; + } + + // convert db distance to kilometers + protected function distance($lat1, $lon1, $lat2, $lon2) + { + if (($lat1 == $lat2) && ($lon1 == $lon2)) + return 0; + + $theta = $lon1 - $lon2; + $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); + $dist = acos($dist); + $dist = rad2deg($dist); + $miles = $dist * 60 * 1.1515; + + return round(($miles * 1.609344), 1); + } +}