Add hub selector filtering for available riders, refactor inventory filter to send a single batch call instead #800

This commit is contained in:
Ramon Gutierrez 2024-05-14 03:39:45 +08:00
parent b19d9c203a
commit a45c3dd65c
3 changed files with 119 additions and 8 deletions

View file

@ -12,6 +12,7 @@ class HubCriteria
protected $limit_results; // number of results to return
protected $limit_distance; // distance limit for search in km
protected $flag_inventory_check; // flag if we need to check for inventory
protected $flag_riders_check; // flag if we need to check for riders available
protected $jo_type; // jo service needed
protected $date_time; // date and time to check if hub is open or not
protected $items; // array of items: items[sku] = quantity to check for
@ -29,6 +30,7 @@ class HubCriteria
$this->jo_type = '';
$this->date_time = new DateTime();
$this->flag_inventory_check = false;
$this->flag_riders_check = false;
$this->items = [];
$this->payment_method = '';
$flag_emergency = false;
@ -81,6 +83,17 @@ class HubCriteria
return $this->flag_inventory_check;
}
public function setRidersCheck($flag_riders_check = true)
{
$this->flag_riders_check = $flag_riders_check;
return $this;
}
public function hasRidersCheck()
{
return $this->flag_riders_check;
}
public function setJoType($jo_type)
{
// TODO: validate the jo type

View file

@ -46,6 +46,7 @@ class HubSelector
$limit_distance = $criteria->getLimitDistance();
$jo_type = $criteria->getJoType();
$flag_inventory_check = $criteria->hasInventoryCheck();
$flag_riders_check = $criteria->hasRidersCheck();
$items = $criteria->getItems();
$date_time = $criteria->getDateTime();
$payment_method = $criteria->getPaymentMethod();
@ -86,10 +87,13 @@ class HubSelector
//error_log('payment hubs ' . json_encode($filtered_hubs));
// inventory filter
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check,
$jo_type, $items, $jo_id, $customer_id);
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id);
$filtered_hubs = $hubs_inventory;
// available riders filter
$hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_type, $jo_id, $customer_id);
$filtered_hubs = $hubs_riders;
//error_log('inventory hubs ' . json_encode($filtered_hubs));
// round robin filter
@ -262,13 +266,70 @@ class HubSelector
protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id)
{
if (empty($hubs))
// check if this is enabled
if (!$flag_inventory_check) {
return $hubs;
}
if (!$flag_inventory_check)
// check hub list is not empty
if (empty($hubs)) {
return $hubs;
}
// check item list is not empty
if (empty($items)) {
return $hubs;
}
// check this is a battery item related JO
if ($jo_type != ServiceType::BATTERY_REPLACEMENT_NEW &&
$jo_type != ServiceType::BATTERY_REPLACEMENT_WARRANTY
) {
return $hubs;
}
// get a list of all hubs with branch codes
$hub_array = $hubs->toArray();
$branch_codes = array_filter(array_column($hub_array, 'branch_code'));
$hubs_to_filter = [];
$results = [];
// call inventory manager for all hubs for selected SKUs
$skus = array_keys($items);
$branches = $this->im->getBranchesInventory($branch_codes, $skus);
// check each result to see if sufficient quantity exists to meet request
foreach ($branches as $branch) {
// filter out branch if it does not have sufficient inventory
if ($branch['Quantity'] < $items[$branch['SapCode']] &&
!isset($hubs_to_filter[$branch['BranchCode']])
) {
$hubs_to_filter[$branch['BranchCode']] = true;
}
}
// remove filtered hubs from list
foreach ($hubs as $hub_data) {
$hub_obj = $hub_data['hub'];
// check if we are filtering this hub
if (isset($hubs_to_filter[$hub_data['hub']->getBranchCode()])) {
// send SMS to hub
$this->sendSMSMessage($hub_obj, $items);
// log this filter
$this->hub_filter_logger->logFilteredHub($hub_obj, 'no_inventory', $jo_id, $customer_id);
} else {
$results[] = $hub_data;
}
}
// return filtered hubs
return $results;
// NOTE: leaving the old code here for posterity, for now
/*
if ($flag_inventory_check)
{
foreach ($hubs as $hub_data)
@ -334,6 +395,43 @@ class HubSelector
}
}
return $results;
*/
}
protected function filterHubsByRiderAvailability($hubs, $flag_riders_check, $jo_type, $jo_id, $customer_id)
{
// check if this is enabled
if (!$flag_riders_check) {
return $hubs;
}
// check hub list is not empty
if (empty($hubs)) {
return $hubs;
}
$results = [];
foreach ($hubs as $hub_data) {
$hub_obj = $hub_data['hub'];
// check we have available riders
if ($hub_obj->getAvailableRiders() === 0) {
// send SMS to hub
$this->rt->sendSMS(
$hub_obj->getNotifNumber(),
$this->trans->trans('message.battery_brand_allcaps'),
$this->trans->trans('message.no_riders_message')
);
// log this filter
$this->hub_filter_logger->logFilteredHub($hub_obj, 'no_available_rider', $jo_id, $customer_id);
} else {
$results[] = $hub_data;
}
}
return $results;
}
@ -386,14 +484,13 @@ class HubSelector
return $hubs_data;
}
protected function checkInventory($items, $hub)
protected function checkInventory($items, $branch_codes)
{
// check if hub has all items
$skus = [];
$branch_codes[] = $hub->getBranchCode();
$result = false;
foreach ($items as $key=> $value)
foreach ($items as $key => $value)
{
// add sap code of item/battery into array since
// getBranchesInventory takes in an array of hubs/branches

View file

@ -13,7 +13,8 @@ add_cust_vehicle_battery_info: This vehicle is using a Motolite battery
jo_title_pdf: Motolite Res-Q Job Order
country_code_prefix: '+63'
delivery_instructions_label: Delivery Instructions
no_inventory_message: No stock for [item_display]
no_inventory_message: 'A RESQ Job Order was created but there is no stock for the following SKU(s) on this branch: [item_display]'
no_riders_message: A RESQ Job Order was created but there are no riders available for this branch.
# images
image_logo_login: /assets/images/logo-resq.png