$(function () { $("#dialog_segmenthour").keydown(numberinput); $("#dialog_segmentcost").keydown(numberinput); }); function numberinput(e) { var keyCode = e.which; if (keyCode === 9//tab || keyCode === 8 || keyCode === 46 //delete || keyCode === 110 || keyCode === 190//. || keyCode === 37 || keyCode === 39//left right || keyCode === 55 || keyCode === 56//home end || (keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105)) return true; else return false; } function GetSegmentDataSource(next) { worequest("GetSegmentDataSource", '', function (data) { if (data.Users && data.Users.length > 0) { userdata = data.Users; $('#dialog_segmentuser').dropdownSource(userdata); } if (data.JobSites && data.JobSites.length > 0) { var jss = [{ ID: '-1', Name: ' ', html: ' ' }]; for (var js of data.JobSites) { jss.push(js); } jobsitedata = jss; $("#dialog_segmentjobsite").dropdownSource(jobsitedata); } if (data.Components && data.Components.length > 0) { var source = []; for (var type of data.Components) { source.push({ value: type }); } components = source; $("#dialog_segmentcomponent").dropdownSource(components); } if (data.SegmentTypes && data.SegmentTypes.length > 0) { var source = []; for (var type of data.SegmentTypes) { source.push({ value: type }); } segmenttypes = source; $("#dialog_segmenttype").dropdownSource(segmenttypes); } if (next) next(); }, function (err) { console.log(err);; }); } function GetSegmentDataSource1(next) { worequest("GetSegmentDataSource1", '', function (data) { if (data.Components && data.Components.length > 0) { var source = []; for (var type of data.Components) { source.push({ value: type }); } components = source; $("#dialog_segmentcomponent").dropdownSource(components); } if (data.SegmentTypes && data.SegmentTypes.length > 0) { var source = []; for (var type of data.SegmentTypes) { source.push({ value: type }); } segmenttypes = source; $("#dialog_segmenttype").dropdownSource(segmenttypes); } if (next) next(); }, function (err) { console.log(err);; }); } function SetCompleted(index) { var date = $('#dialog_segmentcompleteddate' + index).val(); if (date.length > 0 && checkDate(date)) { $('#dialog_segmentcompleted' + index).prop('checked', true); } } var segmentdata = []; function initControl(index) { $('#dialog_segmentcompleteddate' + index).datetimepicker({ timepicker: false, format: 'm/d/Y', enterLikeTab: false, onSelectDate: function (v, inp) { var date = new DateFormatter().formatDate(v, 'm/d/Y 00:00:00'); inp.parent().data('val', [date]); } }); $('#dialog_segmentuser' + index).dropdown(userdata || [], { search: true, valueKey: 'IID', textKey: 'DisplayName' }); $("#dialog_segmentjobsite" + index).dropdown(jobsitedata || [], { search: true, valueKey: 'ID', textKey: 'Name' }); $("#dialog_segmenttype" + index).dropdown(segmenttypes || [], { input: true, maxlength: 50, textKey: 'value' }); $("#dialog_segmentcomponent" + index).dropdown(components || [], { input: true, maxlength: 40, textKey: 'value' }); } function getSegments(callbake) { segmentindex = 0; $('#tab_segments .segments_table').remove(); if (!workorderid || workorderid == "") return; worequest("GetSegments", workorderid, function (data) { if (typeof (data) === "string") { showAlert(data, GetTextByKey("P_WO_ERROR", 'Error')); return; } if (data && data.length > 0) { //$('#dialog_workordercosts').attr('disabled', true); segmentdata = data; var wocost = 0; for (var i = 0; i < segmentdata.length; i++) { wocost += segmentdata[i].Cost; showSegment(segmentdata[i]); } $('#dialog_workordercosts').val(wocost); } else { //$('#dialog_workordercosts').attr('disabled', false); if (workorderdata) $('#dialog_workordercosts').val(workorderdata.WorkOrderTotalCost == 0 ? "" : workorderdata.WorkOrderTotalCost); segmentdata = []; if (callbake) callbake(); } }, function (err) { }); } var segmentindex = 0; function showSegment(segment) { segmentindex++; var div_segments = $('#tab_segments'); var table = $('
'); table.data('index', segmentindex); var tr1 = $(''); var tr1_td1 = $('' + GetTextByKey("P_WO_SEGMENT", "Segment") + " " + segmentindex + '"'); if (!WOReadOnly) { var s_del = $(''); tr1_td1.append(s_del); } var tr1_td2 = $('"'); tr1.append(tr1_td1).append(tr1_td2); table.append(tr1); var tr = $(''); var td1 = $('' + GetTextByKey("P_WO_USER_COLON", " User:") + '"'); var td2 = $('"'); var sel_user = $('').css("width", 320); td2.append(sel_user); var td3 = $('' + GetTextByKey("P_WO_COMPLETED_COLON", "Completed:") + '"'); var td4 = $('"'); var chk_completed = $(''); td4.append(chk_completed); tr.append(td1).append(td2).append(td3).append(td4); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_HOURS_COLON", "Hours:") + '"'); td2 = $('"'); var input_hour = $('') .keydown(numberinput); td2.append(input_hour); td3 = $('' + GetTextByKey("P_WO_COMPLETEDDATE_COLON", "Completed Date:") + '"'); td4 = $('"'); var input_completeddate = $(''); td4.append(input_completeddate); tr.append(td1).append(td2).append(td3).append(td4); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_JOBSITE_COLON", "Jobsite:") + '"'); td2 = $('"'); var sel_jobsite = $('').css("width", 320); td2.append(sel_jobsite); td3 = $('' + GetTextByKey("P_WO_DESCRIPTION_COLON", "Description:") + '*"'); td4 = $('"'); var input_desc = $(''); td4.append(input_desc); tr.append(td1).append(td2).append(td3).append(td4); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_COST_COLON", "Cost:") + '"'); td2 = $('"'); var input_cost = $('') .keydown(numberinput); td2.append(input_cost); td3 = $('' + GetTextByKey("P_WO_NOTES_COLON", "Notes:") + '"'); td4 = $('"').css("vertical-align", "top"); var textarea_notes = $('') td4.append(textarea_notes); tr.append(td1).append(td2).append(td3).append(td4); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_XXXXXX_COLON", "Segment Type:") + '"'); td2 = $('"'); var sel_segmenttype = $('').css("width", 320); td2.append(sel_segmenttype); tr.append(td1).append(td2); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_COMPONENT_COLON", "Component:") + '"'); td2 = $('"'); var sel_component = $('').css("width", 320); td2.append(sel_component); tr.append(td1).append(td2); table.append(tr); tr = $(''); td1 = $('' + GetTextByKey("P_WO_XXXXXX_COLON", "Billable:") + '"'); td2 = $('"'); var chk_billable = $(''); td2.append(chk_billable); tr.append(td1).append(td2); table.append(tr); if (!WOReadOnly) { tr = $(''); td1 = $('"'); var input_save = $('').val(GetTextByKey("P_WO_SAVE", "Save")); var input_cancel = $('').val(GetTextByKey("P_WO_CANCEL", "Cancel")); td1.append(input_save).append(input_cancel); tr.append(td1); table.append(tr); } div_segments.append(table); initControl(segmentindex); $('#tabsegment_' + segmentindex).data('segment', segment); $('#dialog_segmentuser' + segmentindex).dropdownVal(segment.UserIID); $('#dialog_segmentdesc' + segmentindex).val(segment.Description); $('#dialog_segmenthour' + segmentindex).val(segment.Hours === 0 ? "" : segment.Hours); $('#dialog_segmentnotes' + segmentindex).val(segment.Notes); $('#dialog_segmentjobsite' + segmentindex).dropdownVal(segment.JobsiteID); $('#dialog_segmentcost' + segmentindex).val(segment.Cost === 0 ? "" : segment.Cost); $('#dialog_segmenttype' + segmentindex).dropdownVal(segment.SegmentType); $('#dialog_segmentcompleted' + segmentindex).prop('checked', segment.Completed); $('#dialog_segmentcompleteddate' + segmentindex).val(segment.CompletedDateStr); $('#dialog_segmentcomponent' + segmentindex).dropdownVal(segment.Component); $('#dialog_segmentbillable' + segmentindex).prop('checked', segment.Billable); } function OnAddSegment() { if (!workorderid || workorderid === "") { showAlert(GetTextByKey("P_WO_SAVEWORKORDERFIRST", "Please save work order first."), GetTextByKey("P_WO_ADDSEGMENT", "Add Segment")); return; } $('#dialog_segmentuser').dropdownVal(''); $('#dialog_segmentdesc').val(''); $('#dialog_segmenthour').val(''); $('#dialog_segmentnotes').val(''); $('#dialog_segmentjobsite').dropdownVal(''); $('#dialog_segmentcost').val(''); $('#dialog_segmentcompleted').prop('checked', false); $('#dialog_segmentcompleteddate').val(''); $('#dialog_segmenttype').dropdownVal(''); $('#dialog_segmentcomponent').dropdownVal(''); $('#dialog_segmentbillable').prop('checked', false); showPopup(); } function DeleteSegment(index) { var segment = $('#tabsegment_' + index).data('segment'); if (!segment) return; showConfirm(GetTextByKey("P_WO_DOYOUWANTTODELETETHESEGMENT", 'Do you want to delete the segment?'), GetTextByKey("P_WO_DELETESEGMENT", 'Delete Segment'), function () { worequest("DeleteSegment", segment.SegmentID, function (data) { GetSegmentDataSource1(function () { getSegments(getTotalCost); }); }, function (err) { showAlert(GetTextByKey("P_WO_FAILEDDELETESEGMENT", 'Failed to delete this segment.'), GetTextByKey("P_WO_DELETESEGMENT", 'Delete Segment')); }); }); } function CancelSegment(index) { var segment = $('#tabsegment_' + index).data('segment'); if (!segment) return; $('#dialog_segmentuser' + index).dropdownVal(segment.UserIID); $('#dialog_segmentdesc' + index).val(segment.Description); $('#dialog_segmenthour' + index).val(segment.Hours); $('#dialog_segmentnotes' + index).val(segment.Notes); $('#dialog_segmentjobsite' + index).dropdownVal(segment.JobsiteID); $('#dialog_segmentcost' + index).val(segment.Cost); $('#dialog_segmentcompleted' + index).prop('checked', segment.Completed); $('#dialog_segmentcompleteddate' + index).val(segment.CompletedDateStr); $('#dialog_segmenttype' + index).dropdownVal(segment.SegmentType); $('#dialog_segmentcomponent' + index).dropdownVal(segment.Component); $('#dialog_segmentbillable' + index).prop('checked', segment.Billable); } function SaveSegment(index) { $('#btn_savesegment' + index).attr('disabled', true); var segmentid = -1; var alerttitle = GetTextByKey("P_WO_ADDSEGMENT", "Add Segment"); if (index !== "") { var alerttitle = GetTextByKey("P_WO_EDITSEGMENT", "Edit Segment"); var segment = $('#tabsegment_' + index).data('segment'); if (!segment) return; segmentid = segment.SegmentID; } var item = { 'SegmentID': segmentid, 'WorkOrderID': workorderid, 'UserIID': $('#dialog_segmentuser' + index).dropdownVal(), 'Description': $('#dialog_segmentdesc' + index).val(), 'Hours': $('#dialog_segmenthour' + index).val(), 'Notes': $('#dialog_segmentnotes' + index).val(), 'JobsiteID': $('#dialog_segmentjobsite' + index).dropdownVal(), 'Cost': $('#dialog_segmentcost' + index).val(), 'Completed': $('#dialog_segmentcompleted' + index).prop('checked'), 'CompletedDate': $('#dialog_segmentcompleteddate' + index).val(), 'SegmentType': $('#dialog_segmenttype' + index).dropdownVal(), 'Component': $('#dialog_segmentcomponent' + index).dropdownVal(), 'Billable': $('#dialog_segmentbillable' + index).prop('checked') }; if (item.Description === "" || item.Description.length == 0) { showAlert(GetTextByKey("P_WO_DESCRIPTIONREQUIRED", 'Description is required.'), alerttitle); $('#btn_savesegment' + index).attr('disabled', false); return; } if (item.Hours !== "") { if (isNaN(item.Hours)) { showAlert(GetTextByKey("P_WO_HOURSFORMATERROR", 'Hours format error.'), alerttitle); $('#btn_savesegment' + index).attr('disabled', false); return; } else { if (item.Hours <= 0) { showAlert(GetTextByKey("P_WO_HOURSMUSTBEGREATERTHAN0", 'Hours must be greater than 0.'), alerttitle); $('#btn_savesegment' + index).attr('disabled', false); return; } } } if (item.Cost !== "") { if (isNaN(item.Cost)) { showAlert(GetTextByKey("P_WO_COSTFORMATERROR", 'Cost format error.'), alerttitle); $('#btn_savesegment' + index).attr('disabled', false); return; } else { if (item.Cost <= 0) { showAlert(GetTextByKey("P_WO_COSTMUSTBEGREATERTHAN0", 'Cost must be greater than 0.'), alerttitle); $('#btn_savesegment' + index).attr('disabled', false); return; } } } if (item.Cost === "") item.Cost = -1; if (item.Hours === "") item.Hours = -1; var param = JSON.stringify(item); param = htmlencode(param); worequest("SaveSegment", param, function (data) { if (typeof (data) === "string") { $('#btn_savesegment' + index).attr('disabled', false); showAlert(data, alerttitle); } else { $('#btn_savesegment' + index).attr('disabled', false); if (index !== "") { showAlert(GetTextByKey("P_WO_SAVSUCCESSFULLY", "Saved successfully."), alerttitle); } GetSegmentDataSource1(function () { getSegments(getTotalCost); }); hidePopup(); } }, function (err) { $('#btn_savesegment' + index).attr('disabled', false); console.log(err); showAlert(GetTextByKey("P_WO_FAILEDTOSAVESEGMENT", 'Failed to save segment.'), alerttitle); }); } function showPopup() { $('#popupmask').css('height', $('#dialog_workorder').height()); $('#popupmask').show(); $('#popupdialog').show(); $('#popupdialog').css({ "top": ($("#popupmask").height() - $('#popupdialog').height()) / 2, "left": ($("#popupmask").width() - $('#popupdialog').width()) / 2 }); $('#dialog_segmentuser').focus(); } function hidePopup() { $('#popupmask').hide(); $('#popupdialog').hide(); }