From aed31f2a33785d24763ef561b84644ece8b14728 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Wed, 15 May 2024 17:59:27 +0800 Subject: [PATCH] Rearrange flow logic on hub selector, save inventory counts upon retrieval if enabled #800 --- src/Service/HubDistributor.php | 2 ++ src/Service/HubSelector.php | 63 ++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/Service/HubDistributor.php b/src/Service/HubDistributor.php index 27a70f15..b1d20ad6 100644 --- a/src/Service/HubDistributor.php +++ b/src/Service/HubDistributor.php @@ -81,6 +81,7 @@ class HubDistributor 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => $hub_jo_count, + 'inventory' => $hub_data['inventory'] ?? 0, ]; } else @@ -91,6 +92,7 @@ class HubDistributor 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => 0, + 'inventory' => $hub_data['inventory'] ?? 0, ]; } } diff --git a/src/Service/HubSelector.php b/src/Service/HubSelector.php index f6840cde..eeeba2db 100644 --- a/src/Service/HubSelector.php +++ b/src/Service/HubSelector.php @@ -86,6 +86,10 @@ class HubSelector //error_log('payment hubs ' . json_encode($filtered_hubs)); + //error_log('inventory hubs ' . json_encode($filtered_hubs)); + + // error_log('round robin hubs ' . json_encode($filtered_hubs)); + // available riders filter $hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_type, $jo_id, $customer_id); $filtered_hubs = $hubs_riders; @@ -94,14 +98,10 @@ class HubSelector $hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id); $filtered_hubs = $hubs_inventory; - //error_log('inventory hubs ' . json_encode($filtered_hubs)); - // round robin filter $hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs, $flag_round_robin); $filtered_hubs = $hubs_round_robin; - // error_log('round robin hubs ' . json_encode($filtered_hubs)); - // max results filter $hubs_max_result = $this->filterHubsByMaxResults($filtered_hubs, $limit_results, $jo_id, $customer_id); $filtered_hubs = $hubs_max_result; @@ -172,6 +172,7 @@ class HubSelector 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => 0, + 'inventory' => $hub_data['inventory'], ]; else $this->hub_filter_logger->logFilteredHub($hub, 'job_order_type', $jo_id, $customer_id); @@ -207,6 +208,7 @@ class HubSelector 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => 0, + 'inventory' => $hub_data['inventory'], ]; } $flag_found_pmethod = true; @@ -255,6 +257,7 @@ class HubSelector 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => 0, + 'inventory' => $hub_data['inventory'], ]; } else @@ -266,23 +269,21 @@ class HubSelector protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id) { - error_log("IN INVENTORY CHECK NOW"); - // check if this is enabled if (!$flag_inventory_check) { - error_log("INVENTORY CHECK " . $jo_id . ": DISABLED"); + //rror_log("INVENTORY CHECK " . $jo_id . ": DISABLED"); return $hubs; } // check hub list is not empty if (empty($hubs)) { - error_log("INVENTORY CHECK " . $jo_id . ": NO HUBS"); + //error_log("INVENTORY CHECK " . $jo_id . ": NO HUBS"); return $hubs; } // check item list is not empty if (empty($items)) { - error_log("INVENTORY CHECK " . $jo_id . ": NO ITEMS"); + //error_log("INVENTORY CHECK " . $jo_id . ": NO ITEMS"); return $hubs; } @@ -290,60 +291,69 @@ class HubSelector if ($jo_type != ServiceType::BATTERY_REPLACEMENT_NEW && $jo_type != ServiceType::BATTERY_REPLACEMENT_WARRANTY ) { - error_log("INVENTORY CHECK " . $jo_id . ": INVALID SERVICE TYPE"); + //error_log("INVENTORY CHECK " . $jo_id . ": INVALID SERVICE TYPE"); return $hubs; } // get a list of all hubs with branch codes $branch_codes = []; foreach ($hubs as $hub_data) { - $branch_codes[] = $hub_data['hub']->getBranchCode(); + $branch_code = $hub_data['hub']->getBranchCode(); + if (!empty($branch_code)) { + $branch_codes[] = $branch_code; + } }; $hubs_to_filter = []; $results = []; + $qtys = []; // call inventory manager for all hubs for selected SKUs $skus = array_keys($items); - error_log("CHECKING INVENTORY FOR " . count($skus) . " ITEM(S) ON HUBS " . count($branch_codes) . "..."); + //error_log("CHECKING INVENTORY FOR " . count($skus) . " ITEM(S) ON HUBS " . count($branch_codes) . "..."); $branches = $this->im->getBranchesInventory($branch_codes, $skus); - error_log("REQUEST COMPLETE, RESULT COUNT: " . count($branches)); - error_log(print_r($branches, true)); + //error_log("REQUEST COMPLETE, RESULT COUNT: " . count($branches)); // check each result to see if sufficient quantity exists to meet request foreach ($branches as $branch) { - error_log("CHECKING BRANCH " . print_r($branch, true)); - // 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; + if (isset($branch['BranchCode'])) { + // filter out branch if it does not have sufficient inventory + if ($branch['Quantity'] < $items[$branch['SapCode']] && + !isset($hubs_to_filter[$branch['BranchCode']]) + ) { + error_log("FILTERING BRANCH WITH NO INVENTORY: " . $branch['BranchCode']); + $hubs_to_filter[$branch['BranchCode']] = true; + } else { + // save inventory count so we don't have to recheck later + $qtys[$branch['BranchCode']] = $branch['Quantity']; + } } } - error_log("COMPLETED BRANCH CHECKS"); - // remove filtered hubs from list foreach ($hubs as $hub_data) { $hub_obj = $hub_data['hub']; + $branch_code = $hub_data['hub']->getBranchCode(); // check if we are filtering this hub - if (isset($hubs_to_filter[$hub_data['hub']->getBranchCode()])) { + if (isset($hubs_to_filter[$branch_code])) { // 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 { + } else if (!empty($branch_code) && isset($qtys[$branch_code])) { + // include inventory in hub data + $hub_data['inventory'] = $qtys[$branch_code]; + + // we only include branches with branch codes and quantities $results[] = $hub_data; } } - error_log("COMPLETED HUB FILTERING, TOTAL RESULTS: " . count($results)); - // return filtered hubs return $results; @@ -494,6 +504,7 @@ class HubSelector 'distance' => $dist, 'duration' => 0, 'jo_count' => 0, + 'inventory' => 0, ]; } else