Rename inventory flag. Fix find function. #543

This commit is contained in:
Korina Cordero 2021-03-12 11:43:39 +00:00
parent d812e34797
commit de74290624
2 changed files with 99 additions and 331 deletions

View file

@ -11,7 +11,7 @@ class HubCriteria
protected $point; // coordinates of source protected $point; // coordinates of source
protected $limit_results; // number of results to return protected $limit_results; // number of results to return
protected $limit_distance; // distance limit for search in km protected $limit_distance; // distance limit for search in km
protected $has_inventory; // flag if we need to check for inventory protected $flag_inventory_check; // flag if we need to check for inventory
protected $jo_type; // jo service needed protected $jo_type; // jo service needed
protected $date_time; // date and time to check if hub is open or not protected $date_time; // date and time to check if hub is open or not
protected $items; // array of items: items[id] = quantity to check for and has_inventory is true protected $items; // array of items: items[id] = quantity to check for and has_inventory is true
@ -55,15 +55,15 @@ class HubCriteria
return $this->limit_distance; return $this->limit_distance;
} }
public function setHasInventory($has_inventory = true) public function setInventoryCheck($flag_inventory_check = true)
{ {
$this->has_inventory = $has_inventory; $this->flag_inventory_check = $flag_inventory_check;
return $this; return $this;
} }
public function hasInventory() public function hasInventoryCheck()
{ {
return $this->has_inventory; return $this->flag_inventory_check;
} }
public function setJoType($jo_type) public function setJoType($jo_type)

View file

@ -8,6 +8,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Hub; use App\Entity\Hub;
use App\Service\RedisClientProvider;
use App\Service\InventoryManager; use App\Service\InventoryManager;
use App\Ramcar\HubCriteria; use App\Ramcar\HubCriteria;
@ -16,11 +17,15 @@ use App\Ramcar\ServiceType;
class HubSelector class HubSelector
{ {
protected $em; protected $em;
protected $im;
protected $redis;
public function __construct(EntityManagerInterface $em, InventoryManager $im) public function __construct(EntityManagerInterface $em, InventoryManager $im,
RedisClientProvider $redis)
{ {
$this->em = $em; $this->em = $em;
$this->im = $im; $this->im = $im;
$this->redis = $redis;
} }
public function find(HubCriteria $criteria) public function find(HubCriteria $criteria)
@ -29,125 +34,100 @@ class HubSelector
$limit_results = $criteria->getLimitResults(); $limit_results = $criteria->getLimitResults();
$limit_distance = $criteria->getLimitDistance(); $limit_distance = $criteria->getLimitDistance();
$jo_type = $criteria->getJoType(); $jo_type = $criteria->getJoType();
$has_inventory = $criteria->hasInventory(); $flag_inventory_check = $criteria->hasInventoryCheck();
$items = $criteria->getItems(); $items = $criteria->getItems();
$date_time = $criteria->getDateTime(); $date_time = $criteria->getDateTime();
$results = []; $results = [];
// check for jo type // get all the hubs within distance
if (($jo_type == ServiceType::BATTERY_REPLACEMENT_NEW) || $filtered_hubs = $this->getClosestHubs($point, $limit_distance);
($jo_type == ServiceType::BATTERY_REPLACEMENT_WARRANTY))
{ // filter the first hub results for date and opening times
// check if we need to get inventory $hubs_date_time = $this->filterHubsByDateAndTime($filtered_hubs, $date_time);
if ($has_inventory) $filtered_hubs = $hubs_date_time;
{
if ($date_time != null) // filter jo types
{ $hubs_jo_type = $this->filterHubsByJoType($filtered_hubs, $jo_type);
// call function for jo type, inventory == true, and check for date and time $filtered_hubs = $hubs_jo_type;
$results = $this->getClosestHubsWithJoTypeAndInventoryAndTime($point, $limit_results, $limit_distance, $jo_type, $items, $date_time);
// inventory filter
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $has_inventory,
$jo_type, $items);
$filtered_hubs = $hubs_inventory;
// round robin filter
$hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs);
$filtered_hubs = $hubs_round_robin;
// max results filter
$hubs_max_result = $this->filterHubsByMaxResults($filtered_hubs, $limit_results);
$filtered_hubs = $hubs_max_result;
$results = $filtered_hubs;
return $results;
} }
else
protected function filterHubsByRoundRobin($hubs)
{ {
// call function for jo type and inventory == true
$results = $this->getClosestHubsWithJoTypeAndInventory($point, $limit_results, $limit_distance, $jo_type, $items);
} }
}
else protected function filterHubsByMaxResults($hubs, $limit_result)
{ {
if ($date_time != null)
{
// call function for jo type and check for date and time
$results = $this->getClosestHubsWithJoTypeAndTime($point, $limit_results, $limit_distance, $jo_type, $date_time);
} }
else
protected function filterHubsByJoType($hubs, $jo_type)
{ {
// call function for jo type only
$results = $this->getClosestHubsWithJoType($point, $limit_results, $limit_distance, $jo_type);
} }
}
} protected function filterHubsByDateAndTime($hubs, $date_time)
else
{ {
// no need to check for items and inventory. if (empty($hubs))
// check for date and time return $hubs;
if ($date_time != null)
if ($date_time == null)
return $hubs;
$results = [];
foreach ($hubs as $hub)
{ {
// call function that gets hubs with opening times // go through each hub's opening times to check if hub is open
$results = $this->getClosestHubsWithTime($point, $limit_results, $limit_distance, $date_time); // for the specified time
} }
else
return $results;
}
protected function filterHubsByInventory($hubs, $has_inventory, $jo_type, $items)
{ {
// call default function that just gets the hubs within distance limit if (empty($hubs))
// and number of results return $hubs;
$results = $this->getClosestHubs($point, $limit_results, $limit_distance);
if (!$flag_inventory_check)
return $hubs;
$results = [];
if ($flag_inventory_check)
{
foreach ($hubs as $hub)
{
if ($jo_type == ServiceType::BATTERY_REPLACEMENT_NEW)
{
// call inventory
}
if ($jo_type == ServiceType::BATTERY_REPLACEMENT_WARRANTY)
{
// call inventory
}
} }
} }
return $results; return $results;
} }
protected function getClosestHubsWithJoTypeAndInventoryAndTime($point, $limit_results, $limit_distance, $jo_type, $items, $date_time) protected function getClosestHubs(Point $point, $limit_distance)
{
// get closest hubs that are open at the given time offering jo_type service 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 :time BETWEEN h.time_open AND h.time_close' : '') . ' ORDER BY dist');
$query = $this->em->createQuery($query_string)
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
// TODO: add the jo_type/service parameter
// TODO: have to add date and time to query
// right now, hub only has opening times
// so we extract the time from date_time
$time = $date_time->format("H:i:s");
if ($time) {
$query->setParameter('time', $time);
}
$query->setMaxResults($limit_results);
// 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)
{
// TODO: insert call to function that will check inventory here
// checkInventory is going to return true if hub has all items
$this->checkInventory($items, $$row[0]);
// TODO: add checking here if hub has all items or not
$final_data[] = [
'hub' => $row[0],
'db_distance' => $row['dist'],
'distance' => $dist,
'duration' => 0,
];
}
}
return $final_data;
}
protected function getClosestHubsWithJoTypeAndInventory($point, $limit_results, $limit_distance, $jo_type, $items)
{ {
// get closest hubs based on st_distance function from db // 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 ORDER BY dist'; $query_string = 'SELECT h, st_distance(h.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Hub h WHERE h.status_open = true ORDER BY dist';
@ -156,15 +136,11 @@ class HubSelector
->setParameter('lat', $point->getLatitude()) ->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude()); ->setParameter('lng', $point->getLongitude());
// TODO: add the jo_type/service parameter
$query->setMaxResults($limit_results);
// error_log($query->getSql()); // error_log($query->getSql());
$result = $query->getResult(); $result = $query->getResult();
$hubs = []; $hubs = [];
$final_data = []; $hubs_data = [];
foreach ($result as $row) foreach ($result as $row)
{ {
$hubs[] = $row[0]; $hubs[] = $row[0];
@ -183,11 +159,7 @@ class HubSelector
if ($dist < $limit_distance) if ($dist < $limit_distance)
{ {
// TODO: insert call to function that will check inventory here $hubs_data[] = [
// checkInventory is going to return true if hub has all items
$this->checkInventory($items, $$row[0]);
// TODO: add checking here if hub has all items or not
$final_data[] = [
'hub' => $row[0], 'hub' => $row[0],
'db_distance' => $row['dist'], 'db_distance' => $row['dist'],
'distance' => $dist, 'distance' => $dist,
@ -196,211 +168,7 @@ class HubSelector
} }
} }
return $final_data; return $hubs_data;
}
protected function getClosestHubsWithJoTypeAndTime($point, $limit_results, $limit_distance, $jo_type, $date_time)
{
// get closest hubs that are open at the given time offering jo_type service 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 :time BETWEEN h.time_open AND h.time_close' : '') . ' ORDER BY dist');
$query = $this->em->createQuery($query_string)
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
// TODO: add the jo_type/service parameter
// TODO: have to add date and time to query
// right now, hub only has opening times
// so we extract the time from date_time
$time = $date_time->format("H:i:s");
if ($time) {
$query->setParameter('time', $time);
}
$query->setMaxResults($limit_results);
// 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;
}
protected function getClosestHubsWithJoType($point, $limit_results, $limit_distance, $jo_type)
{
// get closest hubs offering jo_type service 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 ORDER BY dist';
$query = $this->em->createQuery($query_string)
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
// TODO: add the jo type/service parameter here
$query->setMaxResults($limit_results);
// 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;
}
protected function getClosestHubsWithTime(Point $point, $limit_results, $limit_distance, $date_time)
{
// get closest hubs that are open at the given time 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 :time BETWEEN h.time_open AND h.time_close' : '') . ' ORDER BY dist');
$query = $this->em->createQuery($query_string)
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
// TODO: have to add date and time to query
// right now, hub only has opening times
// so we extract the time from date_time
$time = $date_time->format("H:i:s");
if ($time) {
$query->setParameter('time', $time);
}
$query->setMaxResults($limit_results);
// 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;
}
protected function getClosestHubs(Point $point, $limit_results, $limit_distance)
{
// 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 ORDER BY dist';
$query = $this->em->createQuery($query_string)
->setParameter('lat', $point->getLatitude())
->setParameter('lng', $point->getLongitude());
$query->setMaxResults($limit_results);
// 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;
} }
protected function checkInventory($items, $hub) protected function checkInventory($items, $hub)