Add auto assignment of hub and rider to APIController. #374
This commit is contained in:
parent
6c02c30370
commit
ab6ed4a185
2 changed files with 125 additions and 2 deletions
|
|
@ -29,6 +29,8 @@ use App\Service\RisingTideGateway;
|
|||
use App\Service\MQTTClient;
|
||||
use App\Service\GeofenceTracker;
|
||||
use App\Service\RiderTracker;
|
||||
use App\Service\MapTools;
|
||||
use App\Service\InventoryManager;
|
||||
|
||||
use App\Entity\MobileSession;
|
||||
use App\Entity\Customer;
|
||||
|
|
@ -45,6 +47,7 @@ use App\Entity\Service;
|
|||
use App\Entity\Partner;
|
||||
use App\Entity\Review;
|
||||
use App\Entity\PrivacyPolicy;
|
||||
use App\Entity\Hub;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
|
@ -817,7 +820,8 @@ class APIController extends Controller
|
|||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function requestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo)
|
||||
public function requestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo,
|
||||
MapTools $map_tools, InventoryManager $im)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$required_params = [
|
||||
|
|
@ -982,6 +986,42 @@ class APIController extends Controller
|
|||
$invoice = $ic->generateInvoice($icrit);
|
||||
$jo->setInvoice($invoice);
|
||||
|
||||
// assign hub and rider
|
||||
if (($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) ||
|
||||
($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY))
|
||||
{
|
||||
// get nearest hub
|
||||
$nearest_hub = $this->findNearestHubWithInventory($jo, $batt, $em, $map_tools, $im);
|
||||
if (!empty($nearest_hub))
|
||||
{
|
||||
// assign rider
|
||||
$available_riders = $nearest_hub->getAvailableRiders();
|
||||
if (count($available_riders) > 0)
|
||||
{
|
||||
$assigned_rider = null;
|
||||
if (count($available_riders) > 1)
|
||||
{
|
||||
// TODO: the setting of riders into an array
|
||||
// will no longer be necessary when the contents
|
||||
// of randomizeRider changes
|
||||
$riders = [];
|
||||
foreach ($available_riders as $rider)
|
||||
{
|
||||
$riders[] = $rider;
|
||||
}
|
||||
|
||||
$assigned_rider = $this->randomizeRider($riders);
|
||||
}
|
||||
else
|
||||
$assigned_rider = $available_riders[0];
|
||||
|
||||
$jo->setHub($nearest_hub);
|
||||
$jo->setRider($assigned_rider);
|
||||
$jo->setStatus(JOStatus::ASSIGNED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$em->persist($jo);
|
||||
$em->persist($invoice);
|
||||
|
||||
|
|
@ -2213,4 +2253,88 @@ class APIController extends Controller
|
|||
return $cust;
|
||||
}
|
||||
|
||||
protected function findNearestHubWithInventory($jo, Battery $batt, EntityManagerInterface $em,
|
||||
MapTools $map_tools, InventoryManager $im)
|
||||
{
|
||||
// get the nearest 10 hubs
|
||||
$selected_hub = null;
|
||||
$hubs = $map_tools->getClosestHubs($jo->getCoordinates(), 10, date("H:i:s"));
|
||||
|
||||
$nearest_hubs_with_distance = [];
|
||||
$nearest_branch_codes = [];
|
||||
foreach ($hubs as $hub)
|
||||
{
|
||||
$nearest_hubs_with_distance[] = $hub;
|
||||
if (!empty($hub['hub']->getBranchCode()))
|
||||
$nearest_branch_codes[] = $hub['hub']->getBranchCode();
|
||||
}
|
||||
|
||||
// check if nearest hubs have branch codes
|
||||
if (count($nearest_branch_codes) == 0)
|
||||
return $selected_hub;
|
||||
|
||||
// get battery sku
|
||||
if ($batt != null)
|
||||
{
|
||||
$skus[] = $batt->getSAPCode();
|
||||
|
||||
// api call to check inventory
|
||||
// pass the list of branch codes of nearest hubs and the skus
|
||||
// go through returned list of branch codes
|
||||
$hubs_with_inventory = $im->getBranchesInventory($nearest_branch_codes, $skus);
|
||||
if (!empty($hubs_with_inventory))
|
||||
{
|
||||
$nearest = [];
|
||||
$flag_hub_found = false;
|
||||
foreach ($hubs_with_inventory as $hub_with_inventory)
|
||||
{
|
||||
// find hub according to branch code
|
||||
$found_hub = $em->getRepository(Hub::class)->findOneBy(['branch_code' => $hub_with_inventory['BranchCode']]);
|
||||
if ($found_hub != null)
|
||||
{
|
||||
// check rider availability
|
||||
if (count($found_hub->getAvailableRiders()) > 0)
|
||||
{
|
||||
// check against nearest hubs with distance
|
||||
foreach ($nearest_hubs_with_distance as $nhd)
|
||||
{
|
||||
// get distance of hub from location, compare with $nearest. if less, replace nearest
|
||||
if ($found_hub->getID() == $nhd['hub']->getID())
|
||||
{
|
||||
if (empty($nearest))
|
||||
{
|
||||
$nearest = $nhd;
|
||||
$flag_hub_found = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($nhd['distance'] < $nearest['distance'])
|
||||
{
|
||||
$nearest = $nhd;
|
||||
$flag_hub_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$selected_hub = $nearest['hub'];
|
||||
}
|
||||
}
|
||||
return $selected_hub;
|
||||
}
|
||||
|
||||
protected function randomizeRider($riders)
|
||||
{
|
||||
// TODO: get redis to track the sales per rider per day
|
||||
// check the time they came in
|
||||
// for now, randomize the rider
|
||||
$selected_index = array_rand($riders);
|
||||
|
||||
$selected_rider = $riders[$selected_index];
|
||||
|
||||
return $selected_rider;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1130,7 +1130,6 @@ class JobOrderController extends Controller
|
|||
// TODO: uncomment this when we have branch codes in data
|
||||
// and when they have real codes and not test ones
|
||||
//$nearest_branch_codes[] = $hub['hub']->getBranchCode();
|
||||
$nearest_branch_codes[] = $hub['hub'];
|
||||
$nearest_branch_codes = ['WestAve','jay_franchise'];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue