550 lines
21 KiB
JavaScript
550 lines
21 KiB
JavaScript
$(function () {
|
||
InitPMGridData();
|
||
|
||
$('#dialog_pm').dialog(function () {
|
||
showmaskbg(false);
|
||
});
|
||
|
||
});
|
||
|
||
function SetAssetItem(asset) {
|
||
$("#dialog_pm").data("asset", asset);
|
||
}
|
||
|
||
function showConfirmOKCancel(msg, title, fok, fcancel) {
|
||
showmaskbg(true);
|
||
_dialog.showConfirmOKCancel(msg, title, function (e) {
|
||
showmaskbg(false);
|
||
if (typeof fok === 'function') {
|
||
fok(e);
|
||
}
|
||
}, function () {
|
||
if (fcancel)
|
||
fcancel();
|
||
showmaskbg(false);
|
||
});
|
||
}
|
||
|
||
/****** PM******/
|
||
var pmschedules
|
||
var grid_pmschedules;
|
||
function InitPMGridData() {
|
||
grid_pmschedules = new GridView('#pmschedulelist');
|
||
grid_pmschedules.lang = {
|
||
all: GetTextByKey("P_GRID_ALL", "(All)"),
|
||
ok: GetTextByKey("P_GRID_OK", "OK"),
|
||
reset: GetTextByKey("P_GRID_RESET", "Reset")
|
||
};
|
||
var list_columns = [
|
||
{ name: 'Selected', caption: "", valueIndex: 'Selected', type: 3, css: { 'width': 45, 'text-align': 'center' } },
|
||
{ name: 'PmScheduleName', caption: GetTextByKey("P_MA_SCHEDULENAME", "Schedule Name"), valueIndex: 'PmScheduleName', css: { 'width': 200, 'text-align': 'left' } },
|
||
{ name: 'StartValue', caption: GetTextByKey("P_MA_INITIALSERVICEVALUE", "Initial Service Value"), valueIndex: 'StartValue', css: { 'width': 120, 'text-align': 'right' } },
|
||
{ name: 'LastAlertTimeString', caption: GetTextByKey("P_MA_LASTSERVICEDATE", "Last Service Date"), valueIndex: 'LastAlertTimeString', css: { 'width': 120, 'text-align': 'left' } },
|
||
{ name: 'ServiceName', caption: GetTextByKey("P_MA_LASTSERVICEPERFORMED", "Last Service Performed"), valueIndex: 'ServiceName', css: { 'width': 140, 'text-align': 'left' } }
|
||
];
|
||
var columns = [];
|
||
// head
|
||
for (var hd in list_columns) {
|
||
var col = {};
|
||
col.name = list_columns[hd].name;
|
||
col.caption = list_columns[hd].caption;
|
||
col.visible = true;
|
||
col.sortable = true;
|
||
col.width = list_columns[hd].css.width;
|
||
col.align = list_columns[hd].css["text-align"]
|
||
col.key = list_columns[hd].valueIndex;
|
||
if (list_columns[hd].type) {
|
||
col.type = list_columns[hd].type;
|
||
}
|
||
if (col.name === "Selected") {
|
||
col.events = {
|
||
onchange: function () {
|
||
if (this.Selected) {
|
||
showSetPMDialog(this);
|
||
this.Selected = false;//弹出对话框并取消勾选,因为此时机器并没有真正加入到计划,在对话框OK刷新列表
|
||
grid_pmschedules.reload();
|
||
}
|
||
else {
|
||
var item = this;
|
||
var msg = GetTextByKey("P_MA_REMOVEASSETFROMSCHEDULE", "Do you want to remove this asset from the schedule?");
|
||
if (item.UnMaintainedAlert && item.UnMaintainedAlert > 0) {
|
||
var msg = GetTextByKey("P_MA_REMOVEASSETFROMSCHEDULE_TIPS", "Select OK below will result in the deletion of existing unassigned PM alerts for this asset.<br/> If you do not want those alerts deleted, select CANCEL and assign appropriate alerts to a maintenance record or work order in asset health prior to deletion/removal.");
|
||
}
|
||
showConfirmOKCancel(msg, GetTextByKey("P_MA_REMOVEASSET", 'Remove Asset'), function () {
|
||
removeAssetFromPMSchedule(item);
|
||
}, function () {
|
||
item.Selected = true;
|
||
grid_pmschedules.reload();
|
||
});
|
||
}
|
||
}
|
||
};
|
||
}
|
||
columns.push(col);
|
||
}
|
||
grid_pmschedules.canMultiSelect = false;
|
||
grid_pmschedules.columns = columns;
|
||
grid_pmschedules.init();
|
||
}
|
||
|
||
function getAssetPMSchedules() {
|
||
showLoading();
|
||
assetrequest('GetPMSchedulesByAsset', machineid, function (data) {
|
||
hideLoading();
|
||
if (typeof (data) === "string") {
|
||
showAlert(data, GetTextByKey("P_MA_ERROR", 'Error'));
|
||
return;
|
||
}
|
||
if (data && data.length > 0) {
|
||
pmschedules = data;
|
||
ShowPMSchedules(data);
|
||
}
|
||
}, function () {
|
||
hideLoading();
|
||
});
|
||
}
|
||
|
||
function ShowPMSchedules() {
|
||
var rows = [];
|
||
for (var i = 0; i < pmschedules.length; i++) {
|
||
var r = pmschedules[i];
|
||
if (r.PmScheduleType === "HM" || r.PmScheduleType === "PM")
|
||
r.StartValue = r.StartHours;
|
||
else if (r.PmScheduleType === "ADM" || r.PmScheduleType === "RDM")
|
||
r.StartValue = r.StartOdometer;
|
||
else
|
||
r.StartValue = r.StartDateString;
|
||
|
||
var fr = { Values: r };
|
||
rows.push(fr);
|
||
}
|
||
grid_pmschedules.setData(rows);
|
||
}
|
||
|
||
function reshowpmgrid() {
|
||
setTimeout(function () {
|
||
$("#pmschedulelist").css("height", $(window).height() - $("#pmschedulelist").offset().top - 10);
|
||
grid_pmschedules && grid_pmschedules.resize();
|
||
});
|
||
}
|
||
|
||
function addAssetToPMSchedule(item) {
|
||
showLoading();
|
||
assetrequest('AddAssetToPMSchedule', JSON.stringify(item), function (data) {
|
||
hideLoading();
|
||
if (data === "OK") {
|
||
$('#dialog_pm').hideDialog();
|
||
showmaskbg(false);
|
||
getAssetPMSchedules();
|
||
}
|
||
else
|
||
showAlert(data, GetTextByKey("P_MA_ERROR", 'Error'));
|
||
}, function () {
|
||
hideLoading();
|
||
});
|
||
}
|
||
|
||
function removeAssetFromPMSchedule(schedule) {
|
||
var assetid = $("#dialog_pm").data("asset").ID;
|
||
var scheduleid = schedule.PmScheduleID;
|
||
showLoading();
|
||
assetrequest('RemoveAssetFromPMSchedule', JSON.stringify([assetid, scheduleid]), function (data) {
|
||
hideLoading();
|
||
if (data === "OK") {
|
||
//showAlert(data, 'Removed Successfully.');
|
||
getAssetPMSchedules();
|
||
}
|
||
else
|
||
showAlert(data, GetTextByKey("P_MA_ERROR", 'Error'));
|
||
}, function () {
|
||
hideLoading();
|
||
});
|
||
}
|
||
|
||
function showSetPMDialog(pmschedule) {
|
||
showmaskbg(true);
|
||
//$('#dialog_pm .dialog-title span.title').text('Set PM Schedule');
|
||
if (pmschedule) {
|
||
var contentctrl = $("#dialog_pm .dialog-content");
|
||
contentctrl.empty();
|
||
//scheduletype === "HM" || scheduletype === "RDM" || scheduletype === "TBM"
|
||
if (pmschedule.PmScheduleType === "PM" || pmschedule.PmScheduleType === "ADM") {
|
||
createAbsoluteContent(contentctrl, pmschedule);
|
||
}
|
||
else if (pmschedule.PmScheduleType === "HM" || pmschedule.PmScheduleType === "RDM") {
|
||
createRelativeContent(contentctrl, pmschedule);
|
||
}
|
||
else if (pmschedule.PmScheduleType === "TBM") {
|
||
createTBMContent(contentctrl, pmschedule);
|
||
}
|
||
}
|
||
|
||
$('#dialog_pm').css({
|
||
'top': (document.documentElement.clientHeight - $('#dialog_adjustenginehours').height()) / 3,
|
||
'left': (document.documentElement.clientWidth - $('#dialog_adjustenginehours').width()) / 2
|
||
}).showDialogfixed();
|
||
}
|
||
|
||
function createAbsoluteContent(contentctrl, pmschedule) {
|
||
var text = GetTextByKey("P_MA_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
var radio1 = $("<input type='radio' name='pm' checked='checked' />");
|
||
contentctrl.append(radio1);
|
||
|
||
var tag = hasAvailableIntervals(pmschedule);
|
||
if (tag)
|
||
text = getFirstLineText(pmschedule);
|
||
else
|
||
text = GetTextByKey("P_MA_NOAVAILABLEINTERVALS", "No available intervals.");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
if (tag) {
|
||
var radio2 = $("<input type='radio' name='pm' />");
|
||
contentctrl.append(radio2);
|
||
text = GetTextByKey("P_MA_ONEORMORESERVICESWASMISSEDALERTMEFOR", "One or more services was missed. Alert me for ");
|
||
contentctrl.append($("<span></span>").text(text));
|
||
var selInterval = $("<select></select>").prop("disabled", true);
|
||
contentctrl.append(selInterval);
|
||
contentctrl.append($("<span> </span>").text(GetTextByKey("P_MA_NOW", " now.")));
|
||
|
||
if (pmschedule.Intervals) {
|
||
for (var i = 0; i < pmschedule.Intervals.length; i++) {
|
||
var interval = pmschedule.Intervals[i];
|
||
if (!interval.Recurring) continue;//暂不考虑非周期性Service
|
||
selInterval.append($("<option></option>").val(interval.PmIntervalID).text(interval.ServiceName));
|
||
}
|
||
}
|
||
|
||
radio1.change(enableInput);
|
||
radio2.change(enableInput);
|
||
function enableInput() {
|
||
if (radio1.prop("checked"))
|
||
selInterval.prop("disabled", true);
|
||
else
|
||
selInterval.prop("disabled", false);
|
||
}
|
||
}
|
||
|
||
$("#btnSetPMSchedule").unbind().click(function () {
|
||
var asset = $("#dialog_pm").data("asset");
|
||
var item = {};
|
||
item.AssetID = asset.ID;
|
||
item.PmScheduleID = pmschedule.PmScheduleID;
|
||
//item.PMType = pmschedule.PmScheduleType;
|
||
|
||
if (pmschedule.PmScheduleType === "PM")
|
||
item.StartHours = asset.EngineHours;
|
||
else {
|
||
var unit = pmschedule.PmScheduleUom;
|
||
var value = asset.Odometer;
|
||
if (value > 0 && unit && asset.OdometerUnits
|
||
&& unit.toLowerCase().charAt(0) != asset.OdometerUnits.toLowerCase().charAt(0)) {
|
||
if (unit.toLowerCase().startsWith("m"))
|
||
value = value * 0.6213712;
|
||
else
|
||
value = value / 0.6213712;
|
||
}
|
||
item.StartOdometer = Math.round(value);
|
||
}
|
||
|
||
if (radio2 && radio2.prop("checked")) {
|
||
item.SelectedIntervalID = selInterval.val();
|
||
}
|
||
|
||
addAssetToPMSchedule(item);
|
||
});
|
||
}
|
||
|
||
function createRelativeContent(contentctrl, pmschedule) {
|
||
var text = GetTextByKey("P_MA_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
var radio1 = $("<input type='radio' name='pm' checked='checked' />");
|
||
contentctrl.append(radio1);
|
||
|
||
var tag = hasAvailableIntervals(pmschedule);
|
||
if (tag)
|
||
text = getFirstLineText(pmschedule);
|
||
else
|
||
text = GetTextByKey("P_MA_NOAVAILABLEINTERVALS", "No available intervals.");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
if (tag) {
|
||
var radio2 = $("<input type='radio' name='pm' />");
|
||
contentctrl.append(radio2);
|
||
text = GetTextByKey("P_MA_CALCULATEBASEDUPONLASTSERVICETHE", "Calculate based upon last service. The ");
|
||
contentctrl.append($("<span></span>").text(text));
|
||
var selInterval = $("<select></select>").prop("disabled", true);
|
||
contentctrl.append(selInterval);
|
||
contentctrl.append($("<span></span>").text(GetTextByKey("P_MA_SERVICEWASPERFORMEDAT", " service was performed at ")));
|
||
var txtStartValue = $("<input type='text' style='width:80px;' />").prop("disabled", true);
|
||
contentctrl.append(txtStartValue);
|
||
|
||
if (pmschedule.AllIntervals) {
|
||
var asset = $("#dialog_pm").data("asset");
|
||
var value = 0;
|
||
if (pmschedule.PmScheduleType === "HM")
|
||
value = Math.round(asset.EngineHours);
|
||
else
|
||
value = Math.round(asset.Odometer);
|
||
if (isNaN(value))
|
||
value = 0;
|
||
|
||
var tempIntervals = getIntervalValues(value, pmschedule.AllIntervals);
|
||
for (var i = 0; i < tempIntervals.length; i++) {
|
||
var interval = tempIntervals[i];
|
||
selInterval.append($("<option></option>").val(interval).text(interval));
|
||
}
|
||
//for (var i = 0; i < pmschedule.AllIntervals.length; i++) {
|
||
// var interval = pmschedule.AllIntervals[i];
|
||
// selInterval.append($("<option></option>").val(interval).text(interval));
|
||
//}
|
||
}
|
||
|
||
radio1.change(enableInput);
|
||
radio2.change(enableInput);
|
||
function enableInput() {
|
||
if (radio1.prop("checked")) {
|
||
txtStartValue.prop("disabled", true);
|
||
selInterval.prop("disabled", true);
|
||
}
|
||
else {
|
||
txtStartValue.prop("disabled", false);
|
||
selInterval.prop("disabled", false);
|
||
}
|
||
}
|
||
}
|
||
|
||
$("#btnSetPMSchedule").unbind().click(function () {
|
||
var asset = $("#dialog_pm").data("asset");
|
||
var item = {};
|
||
item.AssetID = asset.ID;
|
||
item.PmScheduleID = pmschedule.PmScheduleID;
|
||
//item.PMType = pmschedule.PmScheduleType;
|
||
if (radio1.prop("checked")) {
|
||
if (pmschedule.PmScheduleType === "HM")
|
||
item.StartHours = asset.EngineHours;
|
||
else {
|
||
var unit = pmschedule.PmScheduleUom;
|
||
var value = asset.Odometer;
|
||
if (value > 0 && unit && asset.OdometerUnits
|
||
&& unit.toLowerCase().charAt(0) != asset.OdometerUnits.toLowerCase().charAt(0)) {
|
||
if (unit.toLowerCase().startsWith("m"))
|
||
value = value * 0.6213712;
|
||
else
|
||
value = value / 0.6213712;
|
||
}
|
||
item.StartOdometer = Math.round(value);
|
||
}
|
||
}
|
||
else {
|
||
if (pmschedule.PmScheduleType === "HM") {
|
||
item.StartHours = txtStartValue.val();
|
||
if (item.StartHours === "" || isNaN(item.StartHours))
|
||
item.StartHours = asset.EngineHours;
|
||
}
|
||
else {
|
||
item.StartOdometer = txtStartValue.val();
|
||
if (item.StartOdometer === "" || isNaN(item.StartOdometer)) {
|
||
var unit = pmschedule.PmScheduleUom;
|
||
var value = asset.Odometer;
|
||
if (value > 0 && unit && asset.OdometerUnits
|
||
&& unit.toLowerCase().charAt(0) != asset.OdometerUnits.toLowerCase().charAt(0)) {
|
||
if (unit.toLowerCase().startsWith("m"))
|
||
value = value * 0.6213712;
|
||
else
|
||
value = value / 0.6213712;
|
||
}
|
||
item.StartOdometer = Math.round(value);
|
||
}
|
||
}
|
||
|
||
item.StartIntervalValue = selInterval.val();
|
||
}
|
||
|
||
addAssetToPMSchedule(item);
|
||
});
|
||
}
|
||
|
||
function createTBMContent(contentctrl, pmschedule) {
|
||
var text = GetTextByKey("P_MA_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
var radio1 = $("<input type='radio' name='pm' checked='checked' />");
|
||
contentctrl.append(radio1);
|
||
|
||
var tag = hasAvailableIntervals(pmschedule);
|
||
if (tag)
|
||
text = GetTextByKey("P_MA_TRIGGERANALERTASIFSERVICEISDUETODAY", "Trigger an alert as if service is due today.");
|
||
else
|
||
text = GetTextByKey("P_MA_NOAVAILABLEINTERVALS", "No available intervals.");
|
||
contentctrl.append($("<span></span><br />").text(text));
|
||
|
||
if (tag) {
|
||
var radio2 = $("<input type='radio' name='pm' />");
|
||
contentctrl.append(radio2);
|
||
text = GetTextByKey("P_MA_CALCULATEBASEDUPONLASTSERVICETHELASTSERVICEDATAWAS", "Calculate based upon last service. The last service date was ");
|
||
contentctrl.append($("<span></span>").text(text));
|
||
|
||
var dateparent = $("<span></span>");
|
||
contentctrl.append(dateparent);
|
||
var txtLastServiceDate = $("<input type='text' style='width:80px;' />").prop("disabled", true);
|
||
txtLastServiceDate.datetimepicker({
|
||
timepicker: false,
|
||
format: 'm/d/Y',
|
||
enterLikeTab: false
|
||
}).text(currentdate);
|
||
dateparent.append(txtLastServiceDate);
|
||
|
||
contentctrl.append($("<span></span>").text(GetTextByKey("P_MA_FOR", " for ")));
|
||
var selInterval = $("<select></select>").prop("disabled", true);
|
||
contentctrl.append(selInterval);
|
||
|
||
if (pmschedule.Intervals) {
|
||
for (var i = 0; i < pmschedule.AllIntervals.length; i++) {
|
||
var interval = pmschedule.AllIntervals[i];
|
||
selInterval.append($("<option></option>").val(interval).text(interval));
|
||
}
|
||
}
|
||
|
||
radio1.change(enableInput);
|
||
radio2.change(enableInput);
|
||
function enableInput() {
|
||
if (radio1.prop("checked")) {
|
||
txtLastServiceDate.prop("disabled", true);
|
||
selInterval.prop("disabled", true);
|
||
}
|
||
else {
|
||
txtLastServiceDate.prop("disabled", false);
|
||
selInterval.prop("disabled", false);
|
||
}
|
||
}
|
||
}
|
||
|
||
$("#btnSetPMSchedule").unbind().click(function () {
|
||
var asset = $("#dialog_pm").data("asset");
|
||
var item = {};
|
||
item.AssetId = asset.ID;
|
||
item.PmScheduleID = pmschedule.PmScheduleID;
|
||
//item.PMType = pmschedule.PmScheduleType;
|
||
if (radio1.prop("checked")) {
|
||
item.StartDate = currentdate;
|
||
}
|
||
else {
|
||
item.StartDate = txtLastServiceDate.val();
|
||
item.StartIntervalValue = selInterval.val();
|
||
if (item.StartDate == "")
|
||
item.StartDate = "1900-01-01";
|
||
}
|
||
|
||
addAssetToPMSchedule(item);
|
||
});
|
||
}
|
||
|
||
function hasAvailableIntervals(pmschedule) {
|
||
if (!pmschedule.Intervals || pmschedule.Intervals.length == 0)
|
||
return false;
|
||
|
||
for (var i = 0; i < pmschedule.Intervals.length; i++) {
|
||
var ii = pmschedule.Intervals[i];
|
||
if (ii.Recurring)
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function getIntervalValues(currentValue, allIntervals) {
|
||
var result = [];//取当前值的前两个和后10个Interval
|
||
if (allIntervals && allIntervals.length > 0) {
|
||
var maxInterval = allIntervals[allIntervals.length - 1];
|
||
var pervoid = parseInt(currentValue / maxInterval);
|
||
var remain = currentValue % maxInterval;
|
||
if (remain > 0)
|
||
pervoid++;
|
||
var maxpervoid = pervoid + 1;
|
||
var minpervoid = pervoid - Math.ceil(10 / allIntervals.length) - 1;//10表示向后取10
|
||
if (minpervoid < 0)
|
||
minpervoid = 0;
|
||
|
||
for (var pi = maxpervoid; pi >= minpervoid; pi--) {
|
||
for (var i = allIntervals.length - 1; i >= 0; i--) {
|
||
var interval = allIntervals[i];
|
||
var tempinterval = pi * maxInterval + interval;
|
||
result.push(tempinterval);
|
||
if (tempinterval >= currentValue) {
|
||
if (result.length > 2)
|
||
result.splice(0, 1);
|
||
}
|
||
else if (result.length == 12)
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
function getFirstLineText(pmschedule) {
|
||
var text = "";
|
||
if (pmschedule.Intervals && pmschedule.Intervals.length > 0) {
|
||
var asset = $("#dialog_pm").data("asset");
|
||
var unit = "";
|
||
var value = 0;
|
||
if (pmschedule.PmScheduleType === "PM" || pmschedule.PmScheduleType === "HM") {
|
||
unit = "hours";
|
||
value = Math.round(asset.EngineHours);
|
||
}
|
||
else {
|
||
unit = pmschedule.PmScheduleUom;
|
||
value = asset.Odometer;
|
||
if (value > 0 && unit && asset.OdometerUnits
|
||
&& unit.toLowerCase().charAt(0) != asset.OdometerUnits.toLowerCase().charAt(0)) {
|
||
if (unit.toLowerCase().startsWith("m"))
|
||
value = value * 0.6213712;
|
||
else
|
||
value = value / 0.6213712;
|
||
value = Math.round(value);
|
||
}
|
||
}
|
||
if (isNaN(value))
|
||
value = 0;
|
||
|
||
var minoffset = null;
|
||
var nextInterval = null;
|
||
for (var i = 0; i < pmschedule.Intervals.length; i++) {
|
||
var ii = pmschedule.Intervals[i];
|
||
if (!ii.Recurring) continue;//暂不考虑非周期性Service
|
||
|
||
var offset = 0;
|
||
if (value == 0)
|
||
offset = ii.Interval;
|
||
else if (value % ii.Interval > 0)
|
||
offset = ii.Interval - value % ii.Interval;
|
||
|
||
if (!nextInterval || offset < minoffset) {
|
||
nextInterval = ii;
|
||
minoffset = offset;
|
||
continue;
|
||
}
|
||
|
||
if (offset == minoffset && (ii.Priority < nextInterval.Priority
|
||
|| (ii.Priority == nextInterval.Priority && ii.Interval > nextInterval.Interval)))
|
||
nextInterval = ii;
|
||
}
|
||
|
||
if (nextInterval) {
|
||
text = GetTextByKey("P_MA_THEFIRSTALERTWILLBE", "The first alert will be ");
|
||
text += nextInterval.NotificationPeriod + " " + unit;
|
||
if (value != 0 && value % nextInterval.Interval == 0)
|
||
parseInt(value / nextInterval.Interval) * nextInterval.Interval
|
||
else
|
||
value = (parseInt(value / nextInterval.Interval) + 1) * nextInterval.Interval;
|
||
|
||
text += GetTextByKey("P_MA_BEFORETHEASSETREADING", " before the asset reading ") + value + " " + unit;
|
||
text += GetTextByKey("P_MA_FOR", " for ") + nextInterval.ServiceName + ".";
|
||
}
|
||
}
|
||
|
||
return text;
|
||
}
|