fleet-contractor/Site/AssetView/js/adj_odometer.js
2023-04-28 12:22:26 +08:00

167 lines
6.8 KiB
JavaScript

//************************Add Odometer**************************************//
var isAddOdo = true;
var primarydatadourceADOdo;
function openAddOdometer() {
primarydatadourceADOdo = undefined;
var datetime = customerdatetime.split(' ');
var time = datetime[0];
var st = datetime[1].split(':');
var hours = st[0].length == 1 ? "0" + st[0] : st[0];
var minutes = st[1];
$('#dialogadd_odometer').val('');
$('#dialogadd_sel_odometeruom').val(systemunitofodometer);
$('#dialogadd_odometertimezone').val(customertimezone ? customertimezone : "UTC");
$('#dialogadd_odometerdate').val(time);
$('#dialogadd_timehour').val(hours);
$('#dialogadd_timeminute').val(minutes);
$('#dialogadd_notes').val('');
if (assetdata.TelematicsEnabled && !assetdata.CurrentOdometer.IsEmpty &&
(assetdata.CurrentOdometer.DataSource.toLowerCase() == "calamp" || assetdata.CurrentOdometer.DataSource.toLowerCase() == "pedigree")) {
isAddOdo = false;
primarydatadourceADOdo = assetdata.CurrentOdometer.DataSource;
}
else
isAddOdo = true;
$('#dialog_addodometer .dialog-title span.title').text(isAddOdo ? GetTextByKey("P_MV_ADDODOMETER", 'Add Odometer') : GetTextByKey("P_MV_ADJUSTODOMETER", 'Adjust Odometer'));
showmaskbg(true);
$('#dialog_addodometer')
.attr('act', 'add')
.css({
'top': (document.documentElement.clientHeight - $('#dialog_addodometer').height()) / 3,
'left': (document.documentElement.clientWidth - $('#dialog_addodometer').width()) / 2
})
.showDialogfixed();
$('#dialogadd_odometer').focus();
}
function OnAddOdometer() {
$('#addodomask').show();
var alerttitle = isAddOdo ? GetTextByKey("P_MV_ADDODOMETER", 'Add Odometer') : GetTextByKey("P_MV_ADJUSTODOMETER", 'Adjust Odometer');
var item = {
'CustomerID': contractorid,
'AssetID': assetid,
'Odometer': $('#dialogadd_odometer').val(),
'UOM': $('#dialogadd_sel_odometeruom').val(),
'OdometerDate': $('#dialogadd_odometerdate').val(),
'Notes': $('#dialogadd_notes').val(),
'DataSource': primarydatadourceADOdo
};
if (item.Odometer !== "") {
if (isNaN(item.Odometer)) {
showAlert(GetTextByKey("P_MV_ODOMETERFORMATERROR", 'Odometer format error.'), alerttitle);
$('#addodomask').hide();
return;
}
else {
if (item.Odometer <= 0) {
showAlert(GetTextByKey("P_MV_ODOMETERMUSTBEGREATERTHAN0", 'ODOMeter must be greater than 0.'), alerttitle);
$('#addodomask').hide();
return;
}
}
}
else {
showAlert(GetTextByKey("P_MV_ODOMETRCANNOTBEEMPTY", "Odometer cannot be empty."), alerttitle);
$('#addodomask').hide();
return;
}
if (item.OdometerDate === "") {
showAlert(GetTextByKey("P_MV_ODOMETRDATACANNOTBEEMPTY", "Odometer date cannot be empty."), alerttitle);
$('#addodomask').hide();
return;
}
var offset = $('#dialogadd_odometertimezone').find("option:selected").attr("offset");
item.OffsetMinute = offset;
var timezonetext = $('#dialogadd_enginehourstimezone').find("option:selected").text();
var hour = $('#dialogadd_timehour').val();
var minute = $('#dialogadd_timeminute').val();
item.OdometerDate = item.OdometerDate.replace("-", "/") + " " + hour + ":" + minute + ":" + "00";
var time = hour + ":" + minute
var alertmsg = "";
if (isAddOdo) {
alertmsg = GetTextByKey("P_MV_UPDATETHEODOMETER", "Do you want to update the odometer to reflect: {0} {1} at {2} {3}?").replace('{0}', item.Odometer).replace('{1}', item.UOM).replace('{2}', time).replace('{3}', timezonetext);
}
else {
alertmsg = GetTextByKey("P_MV_UPDATETHEODOMETER1", "If you proceed, odometer will be updated to reflect: {0} {1} at {2} <br/> {3}.<br/><br/> Note that this asset is telematic enabled and the primary data source is {4}.<br/><br/>Would you like to proceed?").replace('{0}', item.Odometer).replace('{1}', item.UOM).replace('{2}', time).replace('{3}', timezonetext).replace('{4}', assetdata.CurrentOdometer.DataSourceName);
}
showConfirm1(alertmsg, alerttitle, function () {
$('#addodomask').hide();
if (isAddOdo)
AddManuallyInputOdometer(item, alerttitle);
else
CheckOdometerMinnimumTime(item, alerttitle);
}, function () {
$('#addodomask').hide();
});
}
function AddManuallyInputOdometer(item, alerttitle) {
showloading(true);
var param = JSON.stringify(item);
param = htmlencode(param);
devicerequest("AddManuallyInputOdometer", param, function (data) {
showloading(false);
if (data !== 'OK') {
showAlert(data, alerttitle);
} else {
loadAssetInfo();
showAlert(GetTextByKey("P_MV_ODOMETERUPDATESUCCESSFUL", "Odometer Update Successful."), alerttitle);
}
$('#dialog_addodometer').hideDialog();
$('#addodomask').hide();
}, function (err) {
showloading(false);
showAlert(GetTextByKey("P_MV_FAILEDTOADDODOMETER", 'Failed to add Odometer.'), alerttitle);
$('#addodomask').hide();
});
}
function CheckOdometerMinnimumTime(item, alerttitle) {
var param = JSON.stringify(item);
param = htmlencode(param);
devicerequest("CheckOdometerMinnimumTime", param, function (data) {
if (data > 0) {
if (data == 1)
showAlert(GetTextByKey("P_MA_CHECKODOMETERMINNIMUMTIME", "The adjustment cannot be completed as provided. The odometer reading date provided cannot be prior to initial telematic data available for the asset."), alerttitle);
else
showAlert(GetTextByKey("P_MA_CHECKODOMETERMINNIMUMTIME1", "The adjustment cannot be completed as provided. The odometer reading date provided must be prior to or equal to the latest telematic data available for the asset."), alerttitle);
$('#addodomask').hide();
return;
} else {
SaveAdjustOdometer(item, alerttitle);
}
}, function (err) {
});
}
function SaveAdjustOdometer(item, alerttitle) {
showloading(true);
var param = JSON.stringify(item);
param = htmlencode(param);
devicerequest("SaveAdjustOdometer", param, function (data) {
showloading(false);
if (data !== 'OK') {
showAlert(data, alerttitle);
} else {
loadAssetInfo();
showAlert(GetTextByKey("P_MV_ODOMETERUPDATESUCCESSFUL", "Odometer Update Successful."), alerttitle);
}
$('#dialog_addodometer').hideDialog();
$('#addodomask').hide();
}, function (err) {
showloading(false);
showAlert(GetTextByKey("P_MV_FAILEDTOADJUSTODOMETER", 'Failed to adjust Odometer.'), alerttitle);
$('#addodomask').hide();
});
}