From 0f245e9038bf77485f1abfb9f06fcd0e1c9d47ab Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 15 Mar 2021 08:32:37 +0000 Subject: [PATCH] Fixes to issues found during testing. #543 --- src/Controller/APIController.php | 28 ++++++++-- src/Ramcar/HubCriteria.php | 8 +-- src/Service/HubSelector.php | 93 +++++++++++++++++++++++++------- src/Service/InventoryManager.php | 1 + 4 files changed, 102 insertions(+), 28 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 8f268309..919c2b1e 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -26,6 +26,7 @@ use App\Ramcar\TradeInType; use App\Ramcar\JOEventType; use App\Ramcar\AdvanceOrderSlot; use App\Ramcar\AutoAssignStatus; +use App\Ramcar\HubCriteria; use App\Service\InvoiceGeneratorInterface; use App\Service\RisingTideGateway; @@ -35,6 +36,7 @@ use App\Service\RiderTracker; use App\Service\MapTools; use App\Service\InventoryManager; use App\Service\RiderAssignmentHandlerInterface; +use App\Service\HubSelector; use App\Entity\MobileSession; use App\Entity\Customer; @@ -2330,7 +2332,7 @@ class APIController extends Controller implements LoggedController public function newRequestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo, MapTools $map_tools, InventoryManager $im, MQTTClient $mclient, - RiderAssignmentHandlerInterface $rah) + RiderAssignmentHandlerInterface $rah, HubSelector $hub_select) { // check required parameters and api key $required_params = [ @@ -2515,24 +2517,40 @@ class APIController extends Controller implements LoggedController // check if hub is null if ($hub == null) { + // TODO: set this properly. This is test data + $hub_criteria = new HubCriteria(); + $hub_criteria->setPoint($jo->getCoordinates()) + ->setLimitResults(1) + ->setLimitDistance(5) + ->setInventoryCheck(true) + ->setJoType($jo->getServiceType()) + ->setDateTime(new DateTime()); + + // add battery to items + $sku = $batt->getSAPCode(); + if (!empty($sku)) + $hub_criteria->addItem($batt->getSAPCode(), 1); + // find nearest hub 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); - $nearest_hub = $this->findNearestHub($jo, $em, $map_tools); + //$nearest_hub = $this->findNearestHub($jo, $em, $map_tools); + $nearest_hubs = $hub_select->find($hub_criteria); } else { $nearest_hub = $this->findNearestHub($jo, $em, $map_tools); + $nearest_hubs = $hub_select->find($hub_criteria); } - if (!empty($nearest_hub)) + if (!empty($nearest_hubs)) { //error_log('found nearest hub ' . $nearest_hub->getID()); // assign rider - $available_riders = $nearest_hub->getAvailableRiders(); + $available_riders = $nearest_hubs[0]['hub']->getAvailableRiders(); if (count($available_riders) > 0) { $assigned_rider = null; @@ -2553,7 +2571,7 @@ class APIController extends Controller implements LoggedController $assigned_rider = $available_riders[0]; //error_log('found rider ' . $assigned_rider->getID()); - $jo->setHub($nearest_hub); + $jo->setHub($nearest_hubs[0]['hub']); $jo->setRider($assigned_rider); $jo->setStatus(JOStatus::ASSIGNED); $jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED); diff --git a/src/Ramcar/HubCriteria.php b/src/Ramcar/HubCriteria.php index 001b4cd3..a57b4b99 100644 --- a/src/Ramcar/HubCriteria.php +++ b/src/Ramcar/HubCriteria.php @@ -14,7 +14,7 @@ class HubCriteria protected $flag_inventory_check; // flag if we need to check for inventory 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[id] = quantity to check for and has_inventory is true + protected $items; // array of items: items[sku] = quantity to check for public function __construct() { @@ -89,10 +89,10 @@ class HubCriteria return $this->date_time; } - public function addItem($id, $quantity) + public function addItem($sku, $quantity) { - // at this point, id is assumed to be a valid item - $this->items[$id] = $quantity; + // at this point, sku is assumed to be a valid item + $this->items[$sku] = $quantity; return $this; } diff --git a/src/Service/HubSelector.php b/src/Service/HubSelector.php index 2a9f7a80..c5403dd2 100644 --- a/src/Service/HubSelector.php +++ b/src/Service/HubSelector.php @@ -52,13 +52,13 @@ class HubSelector $filtered_hubs = $hubs_jo_type; // inventory filter - $hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $has_inventory, + $hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items); $filtered_hubs = $hubs_inventory; // round robin filter - $hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs); - $filtered_hubs = $hubs_round_robin; + //$hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs); + //$filtered_hubs = $hubs_round_robin; // max results filter $hubs_max_result = $this->filterHubsByMaxResults($filtered_hubs, $limit_results); @@ -73,6 +73,9 @@ class HubSelector { if (empty($hubs)) return $hubs; + + // for now only + return $hubs; } protected function filterHubsByMaxResults($hubs, $limit_result) @@ -81,6 +84,14 @@ class HubSelector return $hubs; if (empty($limit_result)) return $hubs; + + $results = []; + for ($i = 0; $i < $limit_result; $i++) + { + $results[] = $hubs[$i]; + } + + return $results; } protected function filterHubsByJoType($hubs, $jo_type) @@ -91,13 +102,21 @@ class HubSelector return $hubs; $results = []; - foreach ($hubs as $hub) + foreach ($hubs as $hub_data) { - $has_jo_type = false; + $hub = $hub_data['hub']; + + // TODO: for now, have this return true + $has_jo_type = true; // check if hub offers the jo_type // TODO: add service to hub if ($has_jo_type) - $results[] = $hub; + $results[] = [ + 'hub' => $hub, + 'db_distance' => $hub_data['db_distance'], + 'distance' => $hub_data['distance'], + 'duration' => $hub_data['duration'], + ]; } return $results; @@ -113,27 +132,34 @@ class HubSelector $results = []; - foreach ($hubs as $hub) + foreach ($hubs as $hub_data) { // go through each hub's opening times to check if hub is open // for the specified time // get hub opening and closing times - $time_open = date("H:i:s", $hub->getTimeOpen()); - $time_close = date("H:i:s", $hub->getTimeClose()); + $hub = $hub_data['hub']; - $filter_time = date("H:i:s", $date_time); + $time_open = $hub->getTimeOpen()->format("H:i:s"); + $time_close = $hub->getTimeClose()->format("H:i:s"); + + $filter_time = $date_time->format("H:i:s"); if (($filter_time >= $time_open) && ($filter_time <= $time_close)) { - $results[] = $hub; + $results[] = [ + 'hub' => $hub, + 'db_distance' => $hub_data['db_distance'], + 'distance' => $hub_data['distance'], + 'duration' => $hub_data['duration'], + ]; } } return $results; } - protected function filterHubsByInventory($hubs, $has_inventory, $jo_type, $items) + protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items) { if (empty($hubs)) return $hubs; @@ -144,21 +170,33 @@ class HubSelector $results = []; if ($flag_inventory_check) { - foreach ($hubs as $hub) + foreach ($hubs as $hub_data) { + $hub = $hub_data['hub']; + if ($jo_type == ServiceType::BATTERY_REPLACEMENT_NEW) { // call inventory $has_items = $this->checkInventory($items, $hub); if ($has_items) - $results[] = $hub; + $results[] = [ + 'hub' => $hub, + 'db_distance' => $hub_data['db_distance'], + 'distance' => $hub_data['distance'], + 'duration' => $hub_data['duration'], + ]; } if ($jo_type == ServiceType::BATTERY_REPLACEMENT_WARRANTY) { // call inventory $has_items = $this->checkInventory($items, $hub); if ($has_items) - $results[] = $hub; + $results[] = [ + 'hub' => $hub, + 'db_distance' => $hub_data['db_distance'], + 'distance' => $hub_data['distance'], + 'duration' => $hub_data['duration'], + ]; } } } @@ -214,19 +252,36 @@ class HubSelector { // check if hub has all items $skus = []; - $hubs[] = $hub; + $branch_codes[] = $hub->getBranchCode(); $result = false; - foreach ($items as $item) + foreach ($items as $key=> $value) { // add sap code of item/battery into array since // getBranchesInventory takes in an array of hubs/branches // and an array of skus - $sku[] = $item->getSAPCode(); + // $items as format: $items[sku] = quantity + $skus[] = $key; } // call InventoryManager's getBranchesInventory to check if hub has all items - $result = $this->im->getBranchesInventory($hubs, $skus); + $branches_with_items = $this->im->getBranchesInventory($branch_codes, $skus); + + if (!empty($branches_with_items)) + { + // check if branch has enough quantity for item + foreach ($branches_with_items as $branch) + { + // get quantity from call + $qty_available = $branch['Quantity']; + + // get the quantity request + $sku_requested = $branch['SapCode']; + $qty_requested = $items[$sku_requested]; + if ($qty_available >= $qty_requested) + $result = true; + } + } // return true or false return $result; diff --git a/src/Service/InventoryManager.php b/src/Service/InventoryManager.php index 4dc5a8b1..f0669559 100644 --- a/src/Service/InventoryManager.php +++ b/src/Service/InventoryManager.php @@ -71,6 +71,7 @@ class InventoryManager // check if the response is empty $results = []; + //error_log($response); if (!empty($response)) $results = json_decode($response, true);