Merge branch '314-cmb-add-hub-distance-to-customer-on-one-step-hub-table' into '270-final-cmb-fixes'

Resolve "CMB - add hub distance to customer on one step hub table"

See merge request jankstudio/resq!358
This commit is contained in:
Kendrick Chan 2020-01-28 07:39:51 +00:00
commit 8d45b8e698
7 changed files with 268 additions and 135 deletions

2
README.md Normal file
View file

@ -0,0 +1,2 @@
MQTT format for location updates:
`<latitude>:<longitude>`

2
composer.lock generated
View file

@ -1,7 +1,7 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b101ecfbc1f6f2270f0e8ad326035b7e",

View file

@ -314,6 +314,7 @@ class HubController extends Controller
'name' => $hub->getName(),
'branch' => $hub->getBranch(),
'cnum' => $hub->getContactNumbers(),
'distance' => $hub_res['distance'],
];
}

View file

@ -690,7 +690,7 @@ class RAPIController extends Controller
$wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class);
}
// send mqtt event (fulfilled)
// send mqtt event (fulfilled)
$rider = $this->session->getRider();
$image_url = $req->getScheme() . '://' . $req->getHttpHost() . $req->getBasePath() . '/assets/images/user.gif';
if ($rider->getImageFile() != null)
@ -994,77 +994,4 @@ class RAPIController extends Controller
->setWarrantyExpiration($warr_date);
}
protected function createWarranty($jo)
{
$em = $this->getDoctrine()->getManager();
$warranty = new Warranty();
$warranty_class = $jo->getWarrantyClass();
$first_name = $jo->getCustomer()->getFirstName();
$last_name = $jo->getCustomer()->getLastName();
$mobile_number = $jo->getCustomer()->getPhoneMobile();
// check if date fulfilled is null
if ($jo->getDateFulfill() == null)
$date_create = $jo->getDateCreate();
else
$date_create = $jo->getDateFulfill();
// normalize the plate number
$plate_number = $this->normalizePlateNumber($jo->getCustomerVehicle()->getPlateNumber());
// get battery and its warranty periods
$warranty_period = 0;
$invoice_items = $jo->getInvoice()->getItems();
foreach ($invoice_items as $item)
{
if ($item->getBattery() != null)
{
$battery = $item->getBattery();
$warranty->setBatteryModel($battery->getModel());
$warranty->setBatterySize($battery->getSize());
if ($warranty_class == WarrantyClass::WTY_PRIVATE)
$warranty_period = $battery->getWarrantyPrivate();
else if ($warranty_class == WarrantyClass::WTY_COMMERCIAL)
$warranty_period = $battery->getWarrantyCommercial();
else if ($warranty_class == WarrantyClass::WTY_TNV)
$warranty_period = $battery->getWarrantyTnv();
}
}
// compute expiry date
$expiry_date = $this->computeDateExpire($date_create, $warranty_period);
$warranty->setWarrantyClass($warranty_class)
->setFirstName($first_name)
->setLastName($last_name)
->setMobileNumber($mobile_number)
->setDatePurchase($date_create)
->setDateExpire($expiry_date)
->setPlateNumber($plate_number);
$em->persist($warranty);
$em->flush();
}
protected function normalizePlateNumber($plate_number)
{
// make it upper case
$plate_number = trim(strtoupper($plate_number));
// remove special characters and spaces
$plate_number = preg_replace('/[^A-Za-z0-9]/', '', $plate_number);
//error_log('plate number ' . $plate_number);
return $plate_number;
}
protected function computeDateExpire($date_create, $warranty_period)
{
$expire_date = clone $date_create;
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
return $expire_date;
}
}

View file

@ -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);
}
}

View file

@ -380,17 +380,22 @@
<tr>
<th>Hub</th>
<th>Branch</th>
<!--
<th class="text-right">Distance</th>
<th class="text-right">Travel Time</th>
<th class="text-right">Available Riders</th>
<th class="text-right">Jobs For Assignment</th>
-->
<th>Contact Numbers</th>
<th>Distance in KM</th>
<th>Action</th>
</tr>
</thead>
<tbody id="nearest_hubs">
<!-- {% if mode in ['onestep-edit'] %}
{% for hub in hubs %}
<tr data-id="{{ hub.hub.getID }}"{{ obj.getHub and obj.getHub.getID == hub.hub.getID ? ' class="m-table__row--primary"' }}>
<td>{{ hub.hub.getName }}</td>
<td>{{ hub.hub.getBranch }}</td>
<td>{{ hub.hub.getContactNumbers }}</td>
<td></span></td>
<td></td>
{% endfor %}
{% endif %} -->
</tbody>
</table>
</div>
@ -422,6 +427,24 @@
</tr>
</thead>
<tbody id="riders">
{% if mode in ['onestep-edit'] %}
{% set avail_riders = obj.getHub.getAvailableRiders|default([]) %}
<tr class="placeholder-row{{ obj.getHub and avail_riders|length > 0 ? ' hide' }}">
<td colspan="5">
No riders available.
</td>
</tr>
{% if obj.getHub %}
{% for rider in avail_riders %}
<tr data-id="{{ rider.getID }}"{{ obj.getRider and obj.getRider.getID == rider.getID ? ' class="m-table__row--primary"' }}">
<td>{{ rider.getFirstName }}</td>
<td>{{ rider.getLastName }}</td>
<td>{{ rider.getContactNumber }}</td>
<td>{{ rider.getPlateNumber }}</td>
{% endfor %}
{% endif %}
{% endif %}
</tbody>
</table>
</div>
@ -562,6 +585,9 @@
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
{% if ftags.set_map_coordinate and is_granted('joborder.cancel') and not obj.isCancelled %}
<a href="{{ url('jo_cancel', {'id': obj.getID}) }}" class="btn btn-danger btn-cancel-job-order">Cancel Job Order</a>
{% endif %}
<a href="{{ return_url }}" class="btn btn-secondary">Back</a>
</div>
</div>
@ -584,6 +610,8 @@
$(function() {
var form_in_process = false;
var selected_hub = '';
var selected_rider = '';
// openstreet maps stuff
// TODO: move this to a service
@ -604,7 +632,7 @@ $(function() {
});
var icon_hub = L.divIcon({
className: 'map-div-icon',
html: "<div style='background-color:#00FF00;' class='marker-pin'></div><i class='fa fa-home awesome'>",
html: "<div style='background-color:#FFFF00;' class='marker-pin'></div><i class='fa fa-home awesome'>",
iconSize: [39, 42],
iconAnchor: [15, 42]
});
@ -623,25 +651,58 @@ $(function() {
var marker = L.marker([lat, lng], { icon: icon_customer });
// 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 mode in ['onestep'] %}
// 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);
hub_table += '<tr data-id=' + hub['id'] + '>';
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
hub_table += '<tr data-id=' + hub['id'] + '>';
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td>' + hub['distance'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
$('#nearest_hubs').html(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 += '<tr data-id=' + hub['id'] + ' class="m-table__row--primary"' + '>';
}
else {
hub_table += '<tr data-id=' + hub['id'] + '>';
}
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td>' + hub['distance'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
$('#nearest_hubs').html(hub_table);
});
{% endif %}
// add marker to layer group
markerLayerGroup.addLayer(marker);
@ -706,7 +767,6 @@ $(function() {
});
$(function() {
var selected_hub = "";
$('#hubs-table').on('click', 'tr', function() {
var id = $(this).data('id');
@ -723,6 +783,10 @@ $(function() {
selected_hub = id;
$('#hub-field').val(selected_hub);
// clear rider field
$('#rider-field').val('');
selected_rider = '';
// get riders of hub
// get hub riders ajax
// TODO: add latitude and longitude of delivery location to ajax request
@ -758,7 +822,6 @@ $(function() {
});
$(function() {
var selected_rider = '';
$('#rider-table').on('click', 'tr', function() {
var id = $(this).data('id');
@ -777,9 +840,29 @@ $(function() {
var lat = {{ obj.getCoordinates.getLatitude }};
var lng = {{ obj.getCoordinates.getLongitude }};
selected_hub = '{{ obj.getHub ? obj.getHub.getID: "" }}';
$('#hub-field').val(selected_hub);
selected_rider = '{{ obj.getRider ? obj.getRider.getID: "" }}';
$('#rider-field').val(selected_rider);
selectPoint(lat, lng);
// TODO: find a way to highlight the set hub
// need to put selected rider on map. selected_hub is already on map because of selectPoint
riderLayerGroup.clearLayers();
$.getJSON("{{ url('hub_riders') }}?id=" + selected_hub, function(data) {
var riders = data['riders'];
for (i in riders) {
var rider = riders[i];
if (selected_rider == rider['id']) {
var rider_lat = rider['location'][0];
var rider_lng = rider['location'][1];
var rider_marker = L.marker([rider_lat, rider_lng], { icon: icon_rider_available });
riderLayerGroup.addLayer(rider_marker);
}
}
});
{% endif %}
{% if mode in ['update-processing', 'update-reassign-hub'] %}

View file

@ -36,7 +36,9 @@
<div class="m-form__section m-form__section--first">
<div class="form-group m-form__group row">
<div class="col-lg-6">
<label data-field="customer_vehicle">Select a vehicle:</label>
<label data-field="customer_vehicle">Select a vehicle:
<span style="color:red"> *</span>
</label>
<select class="form-control m-select2" id="customer-vehicle" name="customer_vehicle"></select>
<div class="form-control-feedback hide" data-field="customer_vehicle"></div>
</div>
@ -326,7 +328,9 @@
</div>
<div class="form-group m-form__group row">
<div class="col-lg-6">
<label data-field="delivery_address">Delivery Address</label>
<label data-field="delivery_address">Delivery Address
<span style="color:red"> *</span>
</label>
<textarea name="delivery_address" class="form-control m-input" rows="4">{{ obj.getDeliveryAddress }}</textarea>
<div class="form-control-feedback hide" data-field="delivery_address"></div>
</div>
@ -338,7 +342,9 @@
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<label name="coordinates" data-field="coordinates">Coordinates</label>
<label name="coordinates" data-field="coordinates">Coordinates
<span style="color:red"> *</span>
</label>
<div class="form-control-feedback hide" data-field="coordinates"></div>
<input type="hidden" id="map_lat" name="coord_lat" value="">
<input type="hidden" id="map_lng" name="coord_lng" value="">
@ -363,7 +369,9 @@
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<label name="hub" data-field="hub">Click on a row to select a hub</label>
<label name="hub" data-field="hub">Click on a row to select a hub
<span style="color:red"> *</span>
</label>
<div class="form-control-feedback hide" data-field="hub"></div>
<input type="hidden" id="hub-field" name="hub_id" value="">
<div class="table-frame" data-name="hub">
@ -372,17 +380,22 @@
<tr>
<th>Hub</th>
<th>Branch</th>
<!--
<th class="text-right">Distance</th>
<th class="text-right">Travel Time</th>
<th class="text-right">Available Riders</th>
<th class="text-right">Jobs For Assignment</th>
-->
<th>Contact Numbers</th>
<th>Distance in KM</th>
<th>Action</th>
</tr>
</thead>
<tbody id="nearest_hubs">
<!-- {% if mode in ['onestep-edit'] %}
{% for hub in hubs %}
<tr data-id="{{ hub.hub.getID }}"{{ obj.getHub and obj.getHub.getID == hub.hub.getID ? ' class="m-table__row--primary"' }}>
<td>{{ hub.hub.getName }}</td>
<td>{{ hub.hub.getBranch }}</td>
<td>{{ hub.hub.getContactNumbers }}</td>
<td></span></td>
<td></td>
{% endfor %}
{% endif %} -->
</tbody>
</table>
</div>
@ -397,7 +410,9 @@
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<label name="rider" data-field="rider">Click on a row to select a rider</label>
<label name="rider" data-field="rider">Click on a row to select a rider
<span style="color:red"> *</span>
</label>
<div class="form-control-feedback hide" data-field="rider"></div>
<input type="hidden" id="rider-field" name="rider_id" value="">
<div class="table-frame" data-name="rider">
@ -412,6 +427,24 @@
</tr>
</thead>
<tbody id="riders">
{% if mode in ['onestep-edit'] %}
{% set avail_riders = obj.getHub.getAvailableRiders|default([]) %}
<tr class="placeholder-row{{ obj.getHub and avail_riders|length > 0 ? ' hide' }}">
<td colspan="5">
No riders available.
</td>
</tr>
{% if obj.getHub %}
{% for rider in avail_riders %}
<tr data-id="{{ rider.getID }}"{{ obj.getRider and obj.getRider.getID == rider.getID ? ' class="m-table__row--primary"' }}">
<td>{{ rider.getFirstName }}</td>
<td>{{ rider.getLastName }}</td>
<td>{{ rider.getContactNumber }}</td>
<td>{{ rider.getPlateNumber }}</td>
{% endfor %}
{% endif %}
{% endif %}
</tbody>
</table>
</div>
@ -552,6 +585,9 @@
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
{% if ftags.set_map_coordinate and is_granted('joborder.cancel') and not obj.isCancelled %}
<a href="{{ url('jo_cancel', {'id': obj.getID}) }}" class="btn btn-danger btn-cancel-job-order">Cancel Job Order</a>
{% endif %}
<a href="{{ return_url }}" class="btn btn-secondary">Back</a>
</div>
</div>
@ -574,6 +610,8 @@
$(function() {
var form_in_process = false;
var selected_hub = '';
var selected_rider = '';
// openstreet maps stuff
// TODO: move this to a service
@ -594,7 +632,7 @@ $(function() {
});
var icon_hub = L.divIcon({
className: 'map-div-icon',
html: "<div style='background-color:#00FF00;' class='marker-pin'></div><i class='fa fa-home awesome'>",
html: "<div style='background-color:#FFFF00;' class='marker-pin'></div><i class='fa fa-home awesome'>",
iconSize: [39, 42],
iconAnchor: [15, 42]
});
@ -613,25 +651,58 @@ $(function() {
var marker = L.marker([lat, lng], { icon: icon_customer });
// 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 mode in ['onestep'] %}
// 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);
hub_table += '<tr data-id=' + hub['id'] + '>';
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
hub_table += '<tr data-id=' + hub['id'] + '>';
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td>' + hub['distance'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
$('#nearest_hubs').html(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 += '<tr data-id=' + hub['id'] + ' class="m-table__row--primary"' + '>';
}
else {
hub_table += '<tr data-id=' + hub['id'] + '>';
}
hub_table += '<td>' + hub['name'] + '</td>';
hub_table += '<td>' + hub['branch'] + '</td>';
hub_table += '<td>' + hub['cnum'] + '</td>';
hub_table += '<td>' + hub['distance'] + '</td>';
hub_table += '<td></td>';
hub_table += '</tr>';
}
$('#nearest_hubs').html(hub_table);
});
{% endif %}
// add marker to layer group
markerLayerGroup.addLayer(marker);
@ -696,7 +767,6 @@ $(function() {
});
$(function() {
var selected_hub = "";
$('#hubs-table').on('click', 'tr', function() {
var id = $(this).data('id');
@ -713,6 +783,10 @@ $(function() {
selected_hub = id;
$('#hub-field').val(selected_hub);
// clear rider field
$('#rider-field').val('');
selected_rider = '';
// get riders of hub
// get hub riders ajax
// TODO: add latitude and longitude of delivery location to ajax request
@ -748,7 +822,6 @@ $(function() {
});
$(function() {
var selected_rider = '';
$('#rider-table').on('click', 'tr', function() {
var id = $(this).data('id');
@ -767,9 +840,29 @@ $(function() {
var lat = {{ obj.getCoordinates.getLatitude }};
var lng = {{ obj.getCoordinates.getLongitude }};
selected_hub = '{{ obj.getHub ? obj.getHub.getID: "" }}';
$('#hub-field').val(selected_hub);
selected_rider = '{{ obj.getRider ? obj.getRider.getID: "" }}';
$('#rider-field').val(selected_rider);
selectPoint(lat, lng);
// TODO: find a way to highlight the set hub
// need to put selected rider on map. selected_hub is already on map because of selectPoint
riderLayerGroup.clearLayers();
$.getJSON("{{ url('hub_riders') }}?id=" + selected_hub, function(data) {
var riders = data['riders'];
for (i in riders) {
var rider = riders[i];
if (selected_rider == rider['id']) {
var rider_lat = rider['location'][0];
var rider_lng = rider['location'][1];
var rider_marker = L.marker([rider_lat, rider_lng], { icon: icon_rider_available });
riderLayerGroup.addLayer(rider_marker);
}
}
});
{% endif %}
{% if mode in ['update-processing', 'update-reassign-hub'] %}