Merge branch '651-fix-nearest-hub-issue' into 'master'

Resolve "Fix nearest hub issue"

Closes #651

See merge request jankstudio/resq!763
This commit is contained in:
Kendrick Chan 2022-03-24 06:35:40 +00:00
commit b7159c127c

View file

@ -4306,9 +4306,11 @@ class APIController extends Controller implements LoggedController
// get the slots of hub
$hub_slots = $this->getHubRiderSlots($hub, $em);
$slots = $hub_slots['slot_data'];
$hub_data = [
'hub' => $hub,
'slots' => $hub_slots,
'slots' => $slots,
];
return $hub_data;
}
@ -4324,33 +4326,51 @@ class APIController extends Controller implements LoggedController
}
$nearest = null;
$hub_slots = [];
$slot_found = false;
// find the nearest hub
if (!empty($nearest_hubs_with_distance))
{
// get slots of nearest hub right after getting nearest hub.
// then check if hub has available slots. If not, get next nearest hub.
foreach ($nearest_hubs_with_distance as $nhd)
{
if (empty($nearest))
$nearest = $nhd;
{
// get the slots for the hub to check if hub is available for assignment
$hub_slots = $this->getHubRiderSlots($nhd['hub'], $em);
$flag_hub_available = $hub_slots['flag_hub_available'];
if ($flag_hub_available == true)
{
$nearest = $nhd;
}
}
else
{
if ($nhd['distance'] < $nearest['distance'])
$nearest = $nhd;
{
// get the slots for nearest which is nhd right now
$hub_slots = $this->getHubRiderSlots($nhd['hub'], $em);
$flag_hub_available = $hub_slots['flag_hub_available'];
// if hub is available, set hub to nearest
if ($flag_hub_available == true)
{
$nearest = $nhd;
}
}
}
}
// get slots of nearest hub
if ($nearest != null)
{
$hub_slots = $this->getHubRiderSlots($nearest['hub'], $em);
$hub_data = [
'hub' => $nearest['hub'],
'slots' => $hub_slots,
];
}
}
// set hub data to what is in nearest
$hub_data = [
'hub' => $nearest['hub'],
'slots' => $hub_slots['slot_data'],
];
return $hub_data;
}
@ -4473,18 +4493,25 @@ class APIController extends Controller implements LoggedController
$hub_slots = $this->generateHubSlots($hub_rider_slots, $slots);
error_log(print_r($hub_slots, true));
return $hub_slots;
}
protected function generateHubSlots($rider_slots, $slots)
{
$data = [];
$total_rslots = 0;
$total_unavailable_rslots = 0;
foreach ($rider_slots as $day_id => $rslot)
{
$data[$day_id] = [];
foreach ($rslot as $slot_id => $avail_slots)
{
// increment total rider slots
$total_rslots++;
$slot_data = [
'id' => $slot_id,
'label' => $slots[$slot_id],
@ -4493,14 +4520,32 @@ class APIController extends Controller implements LoggedController
// mark unavailable ones
if ($avail_slots <= 0)
{ // increment total number of unavailable slots
$total_unavailable_rslots++;
$slot_data['available'] = false;
}
// add to day data
$data[$day_id][] = $slot_data;
}
}
return $data;
// check if hub has available slots
$hub_available = true;
error_log('total rider slots ' . $total_rslots);
error_log('total unavailable slots ' . $total_unavailable_rslots);
if ($total_rslots == $total_unavailable_rslots)
{
error_log('hub has no available slots');
$hub_available = false;
}
$hs_data = [
'flag_hub_available' => $hub_available,
'slot_data' => $data,
];
return $hs_data;
}
protected function getTimeFromSlot($slot_id)