From 1227e03e1a6d9a2d51e2c6910302f4d2f901e443 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sat, 10 Oct 2020 00:52:45 +0800 Subject: [PATCH] Add inventory count to hub table in job order processing form #519 --- src/Controller/JobOrderController.php | 7 +-- .../JobOrderHandler/CMBJobOrderHandler.php | 2 +- .../JobOrderHandler/ResqJobOrderHandler.php | 49 ++++++++++++++++++- src/Service/JobOrderHandlerInterface.php | 2 +- src/Service/MotivConnector.php | 2 + templates/job-order/form.html.twig | 2 + 6 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index f23fc73c..1549f48a 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -22,6 +22,8 @@ use App\Service\MapTools; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\InventoryManager; +use App\Service\RiderTracker; +use App\Service\MotivConnector; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -30,7 +32,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Doctrine\ORM\EntityManagerInterface; -use App\Service\RiderTracker; use Catalyst\MenuBundle\Annotation\Menu; @@ -309,13 +310,13 @@ class JobOrderController extends Controller /** * @Menu(selected="jo_proc") */ - public function processingForm(MapTools $map_tools, $id, JobOrderHandlerInterface $jo_handler, GISManagerInterface $gis) + public function processingForm(MapTools $map_tools, $id, JobOrderHandlerInterface $jo_handler, GISManagerInterface $gis, MotivConnector $motiv) { $this->denyAccessUnlessGranted('jo_proc.list', null, 'No access.'); try { - $params = $jo_handler->initializeProcessingForm($id, $map_tools); + $params = $jo_handler->initializeProcessingForm($id, $map_tools, $motiv); } catch (AccessDeniedHttpException $e) { diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 862d431b..1dc1e735 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -1627,7 +1627,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface } // initialize dispatch/processing job order form - public function initializeProcessingForm($id, $map_tools) + public function initializeProcessingForm($id, $map_tools, $motiv) { $em = $this->em; diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index e6cc6ec8..9694a2ba 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -1600,7 +1600,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } // initialize dispatch/processing job order form - public function initializeProcessingForm($id, $map_tools) + public function initializeProcessingForm($id, $map_tools, $motiv) { $em = $this->em; @@ -1671,6 +1671,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $params['hubs'] = []; + $branch_codes = []; + $inv_data = []; + // format duration and distance into friendly time foreach ($hubs as $hub) { // duration @@ -1711,13 +1714,55 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } } - $params['hubs'][] = $hub; + // handle inventory data + $bcode = $hub['hub']->getBranchCode(); + $hub['inventory'] = 0; + if ($bcode != '') + { + $branch_codes[] = $bcode; + $inv_data[$bcode] = [ + 'hub_id' => $hub_id, + 'branch_code' => $bcode, + 'inventory' => 0, + ]; + } + + $params['hubs'][$hub_id] = $hub; } $params['obj'] = $obj; // get template to display $params['template'] = $this->getTwigTemplate('jo_processing_form'); + // get battery (if any) + $skus = []; + $invoice = $obj->getInvoice(); + $inv_items = $invoice->getItems(); + foreach ($inv_items as $inv_item) + { + $batt = $inv_item->getBattery(); + if ($batt == null) + continue; + + $skus[] = $batt->getSapCode(); + } + + // get inventory + $mres = $motiv->getInventory($branch_codes, $skus); + foreach ($mres as $mres_item) + { + $bcode = $mres_item['BranchCode']; + $inv_count = $mres_item['Quantity']; + if (isset($inv_data[$bcode])) + { + $hub_id = $inv_data[$bcode]['hub_id']; + + $params['hubs'][$hub_id]['inventory'] = $inv_count; + } + } + + error_log(print_r($mres, true)); + return $params; } diff --git a/src/Service/JobOrderHandlerInterface.php b/src/Service/JobOrderHandlerInterface.php index 590a0926..59aea943 100644 --- a/src/Service/JobOrderHandlerInterface.php +++ b/src/Service/JobOrderHandlerInterface.php @@ -67,7 +67,7 @@ interface JobOrderHandlerInterface public function initializeAllForm(int $id); // initialize dispatch/processing job order form - public function initializeProcessingForm(int $id, MapTools $map_tools); + public function initializeProcessingForm(int $id, MapTools $map_tools, $motiv); // initialize assign job order form public function initializeAssignForm(int $id); diff --git a/src/Service/MotivConnector.php b/src/Service/MotivConnector.php index 263cb0d0..f98e1b16 100644 --- a/src/Service/MotivConnector.php +++ b/src/Service/MotivConnector.php @@ -31,6 +31,8 @@ class MotivConnector $body_text = json_encode($body); $res = $this->curlPost('InventoryService', $body_text); + + return json_decode($res, true); } protected function curlPost($url, $body) diff --git a/templates/job-order/form.html.twig b/templates/job-order/form.html.twig index bba70b39..a6063f63 100644 --- a/templates/job-order/form.html.twig +++ b/templates/job-order/form.html.twig @@ -575,6 +575,7 @@ --> Available Riders Jobs For Assignment + Inventory Contact Numbers Action @@ -596,6 +597,7 @@ --> {{ hub.rider_count }} {{ hub.jo_count }} + {{ hub.inventory|default('0') }} {{ hub.hub.getContactNumbers|replace({"\n": ', '}) }} {% if hub.flag_rejected %}