$(function () { InitPMGridData(); $('#dialog_pm').dialog(function () { $("#pmschedulelistmask").hide(); }); $('#dialog_pmschedulelist').dialog(function () { showmaskbg(false); GetPMInfo(); }); if (!IsDealer && CanEditPM) $("#btnaddplan").show(); }); function showPMSchedules() { showmaskbg(true); $('#dialog_pmschedulelist').css({ 'top': (document.documentElement.clientHeight - $('#dialog_pmschedulelist').height()) / 3, 'left': (document.documentElement.clientWidth - $('#dialog_pmschedulelist').width()) / 2 }).showDialogfixed(); SetAssetItem(assetdata); getAssetPMSchedules(); } function SetAssetItem(asset) { if (isNaN(asset.EngineHours)) asset.EngineHours = asset.CurrentHours.Corrected if (isNaN(asset.Odometer)) { asset.Odometer = asset.CurrentOdometer.Corrected; asset.OdometerUnits = asset.CurrentOdometer.UOM; } $("#dialog_pm").data("asset", asset); } function showConfirmsub(msg, title, fok, fcancel) { $("#pmschedulelistmask").show(); _dialog.showConfirmOKCancel(msg, title, function (e) { $("#pmschedulelistmask").hide(); if (typeof fok === 'function') { fok(e); } }, function () { if (fcancel) fcancel(); $("#pmschedulelistmask").hide(); }); } /****** PM******/ var pmschedules var grid_pmschedules; function InitPMGridData() { grid_pmschedules = createGridView('#pmschedulelist'); var list_columns = [ { name: 'Selected', caption: "", valueIndex: 'Selected', type: 3, css: { 'width': 45, 'text-align': 'center' } }, { name: 'PmScheduleName', caption: GetTextByKey("P_MV_SCHEDULENAME", "Schedule Name"), valueIndex: 'PmScheduleName', css: { 'width': 200, 'text-align': 'left' } }, { name: 'StartValue', caption: GetTextByKey("P_MV_INITIALSERVICEVALUE", "Initial Service Value"), valueIndex: 'StartValue', css: { 'width': 120, 'text-align': 'right' } }, { name: 'LastAlertTimeString', caption: GetTextByKey("P_MV_LASTSERVICEDATE", "Last Service Date"), valueIndex: 'LastAlertTimeString', css: { 'width': 120, 'text-align': 'left' } }, { name: 'ServiceName', caption: GetTextByKey("P_MV_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(true); } else { var item = this; var msg = GetTextByKey("P_MV_REMOVEASSETFROMSCHEDULE", "Do you want to remove this asset from the schedule?"); if (item.UnMaintainedAlert && item.UnMaintainedAlert > 0) { var msg = GetTextByKey("P_MV_REMOVEASSETFROMSCHEDULE_TIPS", "Select OK below will result in the deletion of existing unassigned PM alerts for this asset.
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."); } showConfirmsub(msg, GetTextByKey("P_MV_REMOVEASSET", 'Remove Asset'), function () { removeAssetFromPMSchedule(item); }, function () { item.Selected = true; grid_pmschedules.reload(true); }); } } }; } columns.push(col); } grid_pmschedules.canMultiSelect = false; grid_pmschedules.columns = columns; grid_pmschedules.init(); } function getAssetPMSchedules() { $("#pmschedulelistmask").show(); assetrequest1('GetPMSchedulesByAsset', assetid, function (data) { $("#pmschedulelistmask").hide(); if (typeof (data) === "string") { showAlert(data, GetTextByKey("P_MV_ERROR", 'Error')); return; } if (data && data.length > 0) { pmschedules = data; ShowPMSchedules(data); } }, function () { $("#pmschedulelistmask").hide(); }); } 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; rows.push(r); } 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) { $("#pmschedulelistmask").show(); assetrequest1('AddAssetToPMSchedule', JSON.stringify(item), function (data) { if (data === "OK") { $("#pmschedulelistmask").hide(); $('#dialog_pm').hideDialog(); //showAlert(data, 'Saved Successfully.'); getAssetPMSchedules(); } else showAlert(data, GetTextByKey("P_MV_ERROR", 'Error')); }, function () { $("#pmschedulelistmask").hide(); }); } function removeAssetFromPMSchedule(schedule) { var assetid = $("#dialog_pm").data("asset").ID; var scheduleid = schedule.PmScheduleID; $("#pmschedulelistmask").show(); assetrequest1('RemoveAssetFromPMSchedule', JSON.stringify([assetid, scheduleid]), function (data) { $("#pmschedulelistmask").hide(); if (data === "OK") { //showAlert(data, 'Removed Successfully.'); getAssetPMSchedules(); } else showAlert(data, GetTextByKey("P_MV_ERROR", 'Error')); }, function () { $("#pmschedulelistmask").hide(); }); } function showSetPMDialog(pmschedule) { $("#pmschedulelistmask").show(); //$('#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_MV_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?"); contentctrl.append($("
").text(text)); var radio1 = $(""); contentctrl.append(radio1); var tag = hasAvailableIntervals(pmschedule); if (tag) text = getFirstLineText(pmschedule); else text = GetTextByKey("P_MV_NOAVAILABLEINTERVALS", "No available intervals."); contentctrl.append($("
").text(text)); if (tag) { var radio2 = $(""); contentctrl.append(radio2); text = GetTextByKey("P_MV_ONEORMORESERVICESWASMISSEDALERTMEFOR", "One or more services was missed. Alert me for "); contentctrl.append($("").text(text)); var selInterval = $("").prop("disabled", true); contentctrl.append(selInterval); contentctrl.append($("" + GetTextByKey("P_MV_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($("").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_MV_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?"); contentctrl.append($("
").text(text)); var radio1 = $(""); contentctrl.append(radio1); var tag = hasAvailableIntervals(pmschedule); if (tag) text = getFirstLineText(pmschedule); else text = GetTextByKey("P_MV_NOAVAILABLEINTERVALS", "No available intervals."); contentctrl.append($("
").text(text)); if (tag) { var radio2 = $(""); contentctrl.append(radio2); text = GetTextByKey("P_MV_CALCULATEBASEDUPONLASTSERVICETHE", "Calculate based upon last service. The "); contentctrl.append($("").text(text)); var selInterval = $("").prop("disabled", true); contentctrl.append(selInterval); contentctrl.append($("" + GetTextByKey("P_MV_SERVICEWASPERFORMEDAT", " service was performed at ") + "")); var txtStartValue = $("").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($("").val(interval).text(interval)); } //for (var i = 0; i < pmschedule.AllIntervals.length; i++) { // var interval = pmschedule.AllIntervals[i]; // selInterval.append($("").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_MV_WHENWOULDYOULIKETHEFIRSTALERT", "When would you like the first alert?"); contentctrl.append($("
").text(text)); var radio1 = $(""); contentctrl.append(radio1); var tag = hasAvailableIntervals(pmschedule); if (tag) text = GetTextByKey("P_MV_TRIGGERANALERTASIFSERVICEISDUETODAY", "Trigger an alert as if service is due today."); else text = GetTextByKey("P_MV_NOAVAILABLEINTERVALS", "No available intervals."); contentctrl.append($("
").text(text)); if (tag) { var radio2 = $(""); contentctrl.append(radio2); text = GetTextByKey("P_MV_CALCULATEBASEDUPONLASTSERVICETHELASTSERVICEDATAWAS", "Calculate based upon last service. The last service date was "); contentctrl.append($("").text(text)); var dateparent = $(""); contentctrl.append(dateparent); var txtLastServiceDate = $("").prop("disabled", true); txtLastServiceDate.datetimepicker({ timepicker: false, format: 'm/d/Y', enterLikeTab: false }).text(currentdate); dateparent.append(txtLastServiceDate); contentctrl.append($("" + GetTextByKey("P_MV_FOR", " for ") + "")); var selInterval = $("").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($("").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_MV_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_MV_BEFORETHEASSETREADING", " before the asset reading ") + value + " " + unit; text += GetTextByKey("P_MV_FOR", " for ") + nextInterval.ServiceName + "."; } } return text; }