fleet-contractor/Site/Maintenance/js/workordersendemail.js
2023-05-30 17:34:56 +08:00

411 lines
14 KiB
JavaScript

$(function () {
InitContactGridData();
InitICContactGridData();
$('#sendworkorder_search').bind('input propertychange', function () {
showContactList(false);
});
$('#sendworkorder_search').keydown(function () {
showContactList(false);
});
$('#sendinternalcomments_search').bind('input propertychange', function () {
showICContactList(false);
});
$('#sendinternalcomments_search').keydown(function () {
showICContactList(false);
});
$('#sendemailpopupdialog').dialog(function () {
showmaskbg(false);
});
$('#sendicemailpopupdialog').dialog(function () {
showmaskbg(false);
});
});
var machineContacts = [];
function getMachineContacts(assetid) {
grid_contactdt && grid_contactdt.setData([]);
grid_iccontactdt && grid_iccontactdt.setData([]);
worequest("GetMachineContacts", assetid, function (data) {
if (typeof (data) === "string") {
showAlert(data, GetTextByKey("P_WO_ERROR", 'Error'));
return;
}
machineContacts = data;
showContactList(true);
showICContactList(true);
}, function (err) {
});
}
function showContactList(newopen) {
var rows = [];
if (machineContacts && machineContacts.length > 0) {
var filter = $('#sendworkorder_search').val()?.trim()?.toLowerCase();
var assignedto = $.trim($('#dialog_assignto').val());
for (var i = 0; i < machineContacts.length; i++) {
var r = machineContacts[i];
var fr = { Values: r };
if (newopen) {
if (assignedto.toLowerCase() === r.IID.toLowerCase())
r.Email = true;
else
r.Email = false;
if (assignedto.toLowerCase() === r.IID.toLowerCase())
rows.splice(0, 0, fr);
else
rows.push(fr);
}
else {
if (!r.Text && !r.Email) {
if (r.DisplayName.toLowerCase().indexOf(filter) >= 0) {
if (assignedto.toLowerCase() === r.IID.toLowerCase())
rows.splice(0, 0, fr);
else
rows.push(fr);
}
}
else {
if (assignedto.toLowerCase() === r.IID.toLowerCase())
rows.splice(0, 0, fr);
else
rows.push(fr);
}
}
}
}
rows = rows.sort(function (a, b) { return ((b.Text || b.Email) ? 1 : 0) - ((a.Text || a.Email) ? 1 : 0) });
grid_contactdt.setData(rows);
}
var grid_contactdt;
function InitContactGridData() {
grid_contactdt = new GridView('#contactlist');
grid_contactdt.lang = {
all: GetTextByKey("P_GRID_ALL", "(All)"),
ok: GetTextByKey("P_GRID_OK", "OK"),
reset: GetTextByKey("P_GRID_RESET", "Reset")
};
var list_columns = [
{ name: 'DisplayName', caption: GetTextByKey("P_WO_CONTACTNAME", "Contact Name"), valueIndex: 'DisplayName', css: { 'width': 148, 'text-align': 'left' } },
{ name: 'ContactTypeName', caption: GetTextByKey("P_WO_CONTACTTYPE", "Contact Type"), valueIndex: 'ContactTypeName', css: { 'width': 148, 'text-align': 'left' } },
//{ name: 'Text', caption: "Text", valueIndex: 'Text', type: 3, css: { 'width': 45, 'text-align': 'center' } },
{ name: 'Email', caption: GetTextByKey("P_WO_EMAIL", "Email"), valueIndex: 'Email', type: 3, css: { 'width': 60, 'text-align': 'center' } }
];
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;
}
columns.push(col);
//if (col.name === "Text") {
// col.enabled = function (item) {
// return item.TextAddress !== '';
// };
//}
if (col.name === "Email") {
col.enabled = function (item) {
return item.ID !== '';
};
}
}
grid_contactdt.canMultiSelect = false;
grid_contactdt.columns = columns;
grid_contactdt.init();
grid_contactdt.selectedrowchanged = function (rowindex) {
var rowdata = grid_contactdt.source[rowindex];
if (rowdata) {
}
}
}
function openSendEmail() {
$('#sendworkorder_desc').val('');
$('#sendworkorder_otheremailaddress').val('');
$('#sendworkorder_search').val('');
var mid;
if (typeof editableSelectMachine != "undefined") {
var machine = editableSelectMachine.selecteditem();
if (machine)
mid = machine.Id;
}
else
mid = machineid;
if (!mid) {
showAlert(GetTextByKey("P_WO_PLEASESELECTANASSET", "Please select an Asset."), GetTextByKey("P_WO_SENDEMAIL", "Send Email"));
$('#dialog_machine').focus();
return;
}
getMachineContacts(mid);
showmaskbg(true);
$('#sendemailpopupdialog').css({
'top': (document.documentElement.clientHeight - $('#sendemailpopupdialog').height()) / 3,
'left': (document.documentElement.clientWidth - $('#sendemailpopupdialog').width()) / 2
}).showDialogfixed();
}
function hideSendEmailPopup() {
showmaskbg(false);
$('#sendemailpopupdialog').hide();
}
function CheckEmail(mail) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (mail.length == 0)
return true;
return filter.test(mail);
}
function onSendWorkOrder() {
var alerttitle = GetTextByKey("P_WO_SENDWORKORDER", "Send Work Order");
if (!workorderid || workorderid === "") {
showAlert(GetTextByKey("P_WO_SAVEWORKORDERFIRST", "Please save work order first."), alerttitle);
return;
}
var emailaddress = [];
var otheremailaddressstr = $('#sendworkorder_otheremailaddress').val();
if (otheremailaddressstr !== "") {
var address = otheremailaddressstr.split(';');
for (var i = 0; i < address.length; i++) {
if (!CheckEmail($.trim(address[i]))) {
_dialog.showAlert(GetTextByKey("P_WO_OTHERADDRESSINVALID", 'The other email address {0} is invalid.').replace('{0}', emailaddress[i]), alerttitle);
return;
}
emailaddress.push({ 'Key': '', 'Value': address[i] });
}
}
for (var i = 0; i < grid_contactdt.source.length; i++) {
var ct = grid_contactdt.source[i].Values;
if (ct.Email) {
emailaddress.push({ 'Key': ct.IID, 'Value': ct.ID });
}
}
if (emailaddress.length == 0) {
hideSendEmailPopup();
return;
}
var desc = $('#sendworkorder_desc').val();
worequest("SendWorkOrder", workorderid + String.fromCharCode(170) + JSON.stringify(emailaddress) + String.fromCharCode(170) + desc, function (data) {
if (data !== 'OK') {
showAlert(data, alerttitle);
}
else
showAlert(GetTextByKey("P_WO_MESSAGESENT", 'Message sent'), alerttitle);
hideSendEmailPopup();
}, function (err) {
_dialog.showAlert(GetTextByKey("P_WO_FAILEDSENDWORKORDER", 'Failed to send work order.'), alerttitle);
});
}
//***********************Send Internal Comments Email Begin***********************************//
var grid_iccontactdt;
function InitICContactGridData() {
grid_iccontactdt = new GridView('#iccontactlist');
grid_iccontactdt.lang = {
all: GetTextByKey("P_GRID_ALL", "(All)"),
ok: GetTextByKey("P_GRID_OK", "OK"),
reset: GetTextByKey("P_GRID_RESET", "Reset")
};
var list_columns = [
{ name: 'DisplayName', caption: GetTextByKey("P_WO_CONTACTNAME", "Contact Name"), valueIndex: 'DisplayName', css: { 'width': 148, 'text-align': 'left' } },
{ name: 'ContactTypeName', caption: GetTextByKey("P_WO_CONTACTTYPE", "Contact Type"), valueIndex: 'ContactTypeName', css: { 'width': 148, 'text-align': 'left' } },
{ name: 'Text', caption: "Text", valueIndex: 'Text', type: 3, css: { 'width': 45, 'text-align': 'center' } },
{ name: 'Email', caption: GetTextByKey("P_WO_EMAIL", "Email"), valueIndex: 'Email', type: 3, css: { 'width': 60, 'text-align': 'center' } }
];
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;
}
columns.push(col);
if (col.name === "Text") {
col.enabled = function (item) {
return item.Mobile != null && $.trim(item.Mobile).length > 0;
};
}
if (col.name === "Email") {
col.enabled = function (item) {
return item.ID !== '';
};
}
}
grid_iccontactdt.canMultiSelect = false;
grid_iccontactdt.columns = columns;
grid_iccontactdt.init();
grid_iccontactdt.selectedrowchanged = function (rowindex) {
var rowdata = grid_iccontactdt.source[rowindex];
if (rowdata) {
}
}
}
function showICContactList(newopen) {
var filter = $('#sendinternalcomments_search').val().trim().toLowerCase();
var rows = [];
if (machineContacts && machineContacts.length > 0) {
for (var i = 0; i < machineContacts.length; i++) {
var r = machineContacts[i];
if (newopen) {
r.Text = false;
r.Email = false;
rows.push({ Values: r });
}
else {
if (!r.Text && !r.Email) {
if (r.DisplayName.toLowerCase().indexOf(filter) >= 0)
rows.push({ Values: r });
}
else
rows.push({ Values: r });
}
}
}
rows = rows.sort(function (a, b) { return ((b.Text || b.Email) ? 1 : 0) - ((a.Text || a.Email) ? 1 : 0) });
grid_iccontactdt.setData(rows);
}
function openSendICEmail() {
var comment = internal?.text;
if ($.trim(comment) == "") {
return;
}
$('#sendic_otheremailaddress').val('');
$('#sendic_phonenumber').val('');
$('#sendinternalcomments_search').val('');
var mid;
if (typeof editableSelectMachine != "undefined") {
var machine = editableSelectMachine.selecteditem();
if (machine)
mid = machine.Id;
}
else
mid = machineid;
if (!mid) {
showAlert(GetTextByKey("P_WO_PLEASESELECTANASSET", "Please select an Asset."), GetTextByKey("P_WO_SENDEMAIL", "Send Email"));
$('#dialog_machine').focus();
return;
}
getMachineContacts(mid);
showmaskbg(true);
$('#sendicemailpopupdialog').css({
'top': (document.documentElement.clientHeight - $('#sendicemailpopupdialog').height()) / 3,
'left': (document.documentElement.clientWidth - $('#sendicemailpopupdialog').width()) / 2
}).showDialogfixed();
}
function hideSendICEmailPopup() {
showmaskbg(false);
$('#sendicemailpopupdialog').hide();
}
function onSendInternalComments() {
var comment = internal?.text;
if ($.trim(comment) == "") {
hideSendICEmailPopup();
return;
}
var alerttitle = GetTextByKey("P_WO_SENDINTERNALCOMMENTS", "Send Internal Comments");
var emailaddress = [];
var otheremailaddressstr = $('#sendic_otheremailaddress').val();
if (otheremailaddressstr !== "") {
var address = otheremailaddressstr.split(';');
for (var i = 0; i < address.length; i++) {
if (!CheckEmail($.trim(address[i]))) {
_dialog.showAlert(GetTextByKey("P_WO_OTHERADDRESSINVALID", 'The other email address {0} is invalid.').replace('{0}', emailaddress[i]), alerttitle);
return;
}
emailaddress.push({ 'Key': '', 'Value': address[i] });
}
}
var phonenumbers = [];
var pm = $('#sendic_phonenumber').val();
if (pm !== "") {
var pms = pm.split(';');
for (var i = 0; i < pms.length; i++) {
if (pms[i] === "")
continue;
if (!checkPhoneNumber(pms[i]) && !CheckEmail(pms[i])) {
showAlert(GetTextByKey("P_WO_THEMOBILENUMBERISINVALID", "The mobile number {0} is invalid.").replace('{0}', pms[i]), alerttitle);
return;
}
phonenumbers.push({ 'Key': '', 'Value': pms[i] });
}
}
for (var i = 0; i < grid_iccontactdt.source.length; i++) {
var ct = grid_iccontactdt.source[i].Values;
if (ct.Email) {
emailaddress.push({ 'Key': ct.IID, 'Value': ct.ID });
}
if (ct.Text && (checkPhoneNumber(ct.Mobile) || CheckEmail(ct.Mobile))) {
phonenumbers.push({ 'Key': ct.IID, 'Value': ct.Mobile });
}
}
var param = JSON.stringify([workorderid, comment, JSON.stringify(emailaddress), JSON.stringify(phonenumbers)]);
param = htmlencode(param);
worequest("SendInternalComments", param, function (data) {
if (data !== "") {
showAlert(data, GetTextByKey("P_WO_ERROR", 'Error'));
return;
}
if (typeof internal !== 'undefined') {
internal.text = '';
}
hideSendICEmailPopup();
if (typeof wowidgetobj != 'undefined')
wowidgetobj.reloaddata("InternalComments");
else
getComments();
}, function (err) {
hideSendICEmailPopup();
});
}
//***********************Send Internal Comments Email End***********************************//