From bc8ce40b1339020e1b5b2854603ca64d4602b171 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 28 Jan 2020 07:24:14 +0000 Subject: [PATCH] Add distance from customer to hub to hub table. #314 --- src/Controller/HubController.php | 1 + src/Service/MapTools.php | 29 +++++++++- .../job-order/cmb.form.onestep.html.twig | 25 ++++++++- templates/job-order/form.onestep.html.twig | 53 ++++++++++++++----- 4 files changed, 91 insertions(+), 17 deletions(-) diff --git a/src/Controller/HubController.php b/src/Controller/HubController.php index ffce16f6..aeaaec7a 100644 --- a/src/Controller/HubController.php +++ b/src/Controller/HubController.php @@ -314,6 +314,7 @@ class HubController extends Controller 'name' => $hub->getName(), 'branch' => $hub->getBranch(), 'cnum' => $hub->getContactNumbers(), + 'distance' => $hub_res['distance'], ]; } diff --git a/src/Service/MapTools.php b/src/Service/MapTools.php index f02618ba..6aa5848c 100644 --- a/src/Service/MapTools.php +++ b/src/Service/MapTools.php @@ -84,10 +84,23 @@ class MapTools { //error_log($row[0]->getName() . ' - ' . $row['dist']); $hubs[] = $row[0]; + + // get coordinates of hub + $hub_coordinates = $row[0]->getCoordinates(); + + $cust_lat = $point->getLatitude(); + $cust_lng = $point->getLongitude(); + + $hub_lat = $hub_coordinates->getLatitude(); + $hub_lng = $hub_coordinates->getLongitude(); + + // get distance in kilometers from customer point to hub point + $dist = $this->distance($cust_lat, $cust_lng, $hub_lat, $hub_lng); + $final_data[] = [ 'hub' => $row[0], 'db_distance' => $row['dist'], - 'distance' => 0, + 'distance' => $dist, 'duration' => 0, ]; } @@ -135,4 +148,18 @@ class MapTools return $final_data; */ } + + protected function distance($lat1, $lon1, $lat2, $lon2) + { + if (($lat1 == $lat2) && ($lon1 == $lon2)) + return 0; + + $theta = $lon1 - $lon2; + $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); + $dist = acos($dist); + $dist = rad2deg($dist); + $miles = $dist * 60 * 1.1515; + + return round(($miles * 1.609344), 1); + } } diff --git a/templates/job-order/cmb.form.onestep.html.twig b/templates/job-order/cmb.form.onestep.html.twig index 8eced3cc..8e4ac45d 100644 --- a/templates/job-order/cmb.form.onestep.html.twig +++ b/templates/job-order/cmb.form.onestep.html.twig @@ -381,18 +381,21 @@ Hub Branch Contact Numbers + Distance in KM Action - {% if mode in ['onestep-edit'] %} + @@ -662,6 +665,7 @@ $(function() { hub_table += '' + hub['name'] + ''; hub_table += '' + hub['branch'] + ''; hub_table += '' + hub['cnum'] + ''; + hub_table += '' + hub['distance'] + ''; hub_table += ''; hub_table += ''; } @@ -673,13 +677,30 @@ $(function() { {% if mode in ['onestep-edit'] %} // get nearest hubs ajax + var hub_table = ''; $.getJSON("{{ url('hub_nearest') }}?lat=" + lat + "&long=" + lng, function(data) { var hubs = data['hubs']; for (i in hubs) { var hub = hubs[i]; var hub_marker = L.marker([hub['lat'], hub['long']], { icon: icon_hub }); hubLayerGroup.addLayer(hub_marker); + + if (selected_hub == hub['id']) { + hub_table += ''; + } + else { + hub_table += ''; + } + hub_table += '' + hub['name'] + ''; + hub_table += '' + hub['branch'] + ''; + hub_table += '' + hub['cnum'] + ''; + hub_table += '' + hub['distance'] + ''; + hub_table += ''; + hub_table += ''; + } + + $('#nearest_hubs').html(hub_table); }); {% endif %} diff --git a/templates/job-order/form.onestep.html.twig b/templates/job-order/form.onestep.html.twig index 120943ad..8e4ac45d 100644 --- a/templates/job-order/form.onestep.html.twig +++ b/templates/job-order/form.onestep.html.twig @@ -36,7 +36,9 @@
- +
@@ -326,7 +328,9 @@
- +
@@ -338,7 +342,9 @@
- + @@ -363,7 +369,9 @@
- +
@@ -372,25 +380,22 @@ Hub Branch - Contact Numbers + Distance in KM Action - {% if mode in ['onestep-edit'] %} +
@@ -405,7 +410,9 @@
- +
@@ -658,24 +665,42 @@ $(function() { hub_table += '' + hub['name'] + ''; hub_table += '' + hub['branch'] + ''; hub_table += '' + hub['cnum'] + ''; + hub_table += '' + hub['distance'] + ''; hub_table += ''; hub_table += ''; } $('#nearest_hubs').html(hub_table); - + }); {% endif %} {% if mode in ['onestep-edit'] %} // get nearest hubs ajax + var hub_table = ''; $.getJSON("{{ url('hub_nearest') }}?lat=" + lat + "&long=" + lng, function(data) { var hubs = data['hubs']; for (i in hubs) { var hub = hubs[i]; var hub_marker = L.marker([hub['lat'], hub['long']], { icon: icon_hub }); hubLayerGroup.addLayer(hub_marker); + + if (selected_hub == hub['id']) { + hub_table += ''; + } + else { + hub_table += ''; + } + hub_table += '' + hub['name'] + ''; + hub_table += '' + hub['branch'] + ''; + hub_table += '' + hub['cnum'] + ''; + hub_table += '' + hub['distance'] + ''; + hub_table += ''; + hub_table += ''; + } + + $('#nearest_hubs').html(hub_table); }); {% endif %}