Add HubSelector service. #543

This commit is contained in:
Korina Cordero 2021-03-10 10:49:31 +00:00
parent 09d04a8a7e
commit 4efef6d397
3 changed files with 129 additions and 0 deletions

View file

@ -266,3 +266,7 @@ services:
event: 'postPersist'
entity: 'App\Entity\CustomerVehicle'
# hub service
App\Service\HubSelector:
arguments:
$em: "@doctrine.orm.entity_manager"

View file

@ -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);
}

123
src/Service/HubSelector.php Normal file
View file

@ -0,0 +1,123 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Hub;
use App\Ramcar\HubCriteria;
use App\Ramcar\ServiceType;
class HubSelector
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->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);
}
}