2023-04-28 12:22:26 +08:00

325 lines
12 KiB
JavaScript

var _selectedRental = null;
$(function () {
$('#dialog_rentaldate').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_projectreturndate').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_returndate').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_rentaltermbillingdate').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]);
}
});
});
function SetMachineRental(rental) {
//if (IsAdmin)
// $('#btnManageRentalHistory').css('display', '');
//else
// $('#btnManageRentalHistory').css('display', 'none');
//if (rental != null) {
// $('#tab_rentalconnect :input[type="text"]').attr('disabled', false);
// $('#dialog_outside').attr('disabled', false);
// $('#dialog_termunit').attr('disabled', false);
// $('#dialog_comments').attr('disabled', false);
//}
//else {
// $('#tab_rentalconnect :input[type="text"]').attr('disabled', true);
// $('#dialog_outside').attr('disabled', true);
// $('#dialog_termunit').attr('disabled', true);
// $('#dialog_comments').attr('disabled', true);
// $('#btnManageRentalHistory').css('display', 'none');
//}
if (rental != null) {
//if (rental.data)
// rental = rental.data;
$('#tab_rentalconnect').data('rentalid', rental.RentalID);
$('#dialog_outside').val(rental.Outside);
$('#dialog_vendor').val(rental.Vendor);
$('#dialog_rentalrate').val(rental.RentalRate);
$('#dialog_term').val(rental.Term);
$('#dialog_termunit').val(rental.TermUnit);
$('#dialog_rentaldate').val(rental.RentalDateStr);
$('#dialog_projectreturndate').val(rental.ProjectReturnDateStr);
$('#dialog_returndate').val(rental.ReturnDateStr);
$('#dialog_ponumber').val(rental.PONumber);
$('#dialog_comments').val(rental.Comments);
$('#dialog_rentaltermbillingdate').val(rental.RentalTermBillingDateStr);
$('#dialog_billingcycledays').val(rental.BillingCycleDays < 0 ? "" : rental.BillingCycleDays);
$('#dialog_insuredvalue').val(rental.InsuredValue);
}
else {
$('#tab_rentalconnect').data('rentalid', '');
$('#dialog_outside').val('');
$('#dialog_vendor').val('');
$('#dialog_rentalrate').val('');
$('#dialog_term').val('');
$('#dialog_termunit').val('');
$('#dialog_rentaldate').val('');
$('#dialog_projectreturndate').val('');
$('#dialog_returndate').val('');
$('#dialog_ponumber').val('');
$('#dialog_comments').val('');
$('#dialog_rentaltermbillingdate').val('');
$('#dialog_billingcycledays').val('');
$('#dialog_insuredvalue').val('');
}
}
function getRentalInput(alerttitle) {
var rentalid = $('#tab_rentalconnect').data('rentalid');
if (rentalid === "")
rentalid = -1;
var rental = {
'RentalID': rentalid,
'MachineID': machineid,
'Outside': $('#dialog_outside').val(),
'Vendor': $('#dialog_vendor').val(),
'RentalRate': $('#dialog_rentalrate').val(),
'Term': $('#dialog_term').val(),
'TermUnit': $('#dialog_termunit').val(),
'RentalDate': $('#dialog_rentaldate').val(),
'RentalDateStr': $('#dialog_rentaldate').val(),
'ProjectReturnDate': $('#dialog_projectreturndate').val(),
'ProjectReturnDateStr': $('#dialog_projectreturndate').val(),
'ReturnDate': $('#dialog_returndate').val(),
'ReturnDateStr': $('#dialog_returndate').val(),
'PONumber': $('#dialog_ponumber').val(),
'Comments': $('#dialog_comments').val(),
'RentalTermBillingDate': $('#dialog_rentaltermbillingdate').val(),
'BillingCycleDays': $('#dialog_billingcycledays').val(),
'InsuredValue': $('#dialog_insuredvalue').val()
};
if (rental.RentalRate !== "" && isNaN(rental.RentalRate)) {
showAlert(GetTextByKey("P_MA_RENTALRATEFORMATERROR", 'Rental Rate format error.'), alerttitle);
return false;
}
if (rental.RentalRate === "") {
rental.RentalRate = 0;
}
if (rental.Term === "") {
rental.Term = 0;
}
if (isNaN(rental.Term) || !IsInteger.test(rental.Term) || eval(rental.Term) < 0) {
showAlert(GetTextByKey("P_MA_RENTALTERMMUSTBEANINTEGEREQUALTOORGREATERTHAN0", 'Rental Term must be an integer equal to or greater than 0. '), alerttitle);
return false;
}
if (rental.RentalDate.length <= 0) {
showAlert(GetTextByKey("P_MA_RENTALDATECANNOTBEEMPTY", 'Rental Date cannot be empty.'), alerttitle);
return false;
}
if (rental.BillingCycleDays !== "") {
if (isNaN(rental.BillingCycleDays) || !IsInteger.test(rental.BillingCycleDays) || eval(rental.BillingCycleDays) < 0) {
showAlert(GetTextByKey("P_MA_THENUMBEROFDAYSINBILLINGCYCLEMUSTBEANINTEGEREQUALTOORGREATERTHAN0", 'The Number of Days in Billing Cycle must be an integer equal to or greater than 0.'), alerttitle);
return false;
}
} else
rental.BillingCycleDays = -1;
if (rental.InsuredValue !== "" && isNaN(rental.InsuredValue)) {
showAlert(GetTextByKey("P_MR_INSUREDVALUEFORMATERROR", 'Insured Value format error.'), alerttitle);
return false;
}
if (rental.InsuredValue === "") {
rental.InsuredValue = 0;
}
return rental;
}
var IsInteger = /^[0-9]+$/;
function SaveRental() {
if (!rentalChanged) return;
var alerttitle = "";
var rentalid = $('#tab_rentalconnect').data('rentalid');
if (rentalid === "")
rentalid = -1;
if (rentalid > 0)
alerttitle = GetTextByKey("P_MA_EDITRENTAL", "Edit Rental");
else
alerttitle = GetTextByKey("P_MA_ADDRENTAL", "Add Rental");
var rental = getRentalInput(alerttitle);
if (!rental) return;
if (!machineid) {
OnSave(0, false, function () {
getRentals(machineid);
});
return;
}
else {
DoSaveRental(rental, alerttitle);
}
}
function DoSaveRental(item, alerttitle) {
var rentaldate = new Date(item.RentalDate.replace("-", "/"));
var pjdate = new Date(item.ProjectReturnDate.replace("-", "/"));
var returndate = new Date(item.ReturnDate.replace("-", "/"));
if (rentaldate > pjdate) {
showAlert(GetTextByKey("P_MA_PROJRETURNDATEMUSTBELATERTHANRENTALDATE", "Proj. Return Date must be later than than Rental Date."), alerttitle);
return false;
}
if (rentaldate > returndate) {
showAlert(GetTextByKey("P_MA_RETURNDATEMUSTBELATERTHANRENTALDATE", "Return Date must be later than Rental Date."), alerttitle);
return false;
}
showloading(true);
var param = JSON.stringify(item);
param = htmlencode(param);
devicerequest("SaveRental", contractorid + String.fromCharCode(170) + param, function (data) {
showloading(false);
if (typeof (data) === "string") {
if (data === "Rental dates entered overlap with another entry. Please adjust the dates.")
data = GetTextByKey("P_MA_RENTALDATESENTEREDOVERLAPWITHANOTHERENTRY", "Rental dates entered overlap with another entry. Please adjust the dates.");
showAlert(data, GetTextByKey("P_MA_SAVERENTAL", 'Save Rental'));
} else {
rentalChanged = false;
item.RentalID = data;
$('#tab_rentalconnect').data('rentalid', item.RentalID);
showAlert(GetTextByKey("P_MA_SAVSUCCESSFULLY", 'Saved successfully.'), GetTextByKey("P_MA_SAVERENTAL", 'Save Rental'));
getRentals(machineid);
}
}, function (err) {
console.log(err);
showloading(false);
showAlert(GetTextByKey("P_MA_FAILEDTOSAVERENTAL", 'Failed to save Rental.'), GetTextByKey("P_MA_SAVERENTAL", 'Save Rental'));
});
}
function CancelRental() {
SetMachineRental(_selectedRental);
}
function jumpToAddRental() {
window.open("AddRental.aspx?cid=" + contractorid + "&mid=" + machineid);
}
function getRentals(machineid) {
if (!machineid)
return;
_selectedRental = null;
$("#rentalListDiv").hide();
showLoading();
devicerequest("SearchRentalsByAsset", JSON.stringify([contractorid, machineid]), function (data) {
$('#tbody_rentals').empty();
if (typeof (data) === "string") {
showAlert(data, GetTextByKey("P_MA_ERROR", 'Error'));
return;
}
if (data && data.length > 0) {
rentalsdata = data;
$("#rentalListDiv").show();
}
else
rentalsdata = [];
sortTableData($('#tbRentals'), rentalsdata);
showRentals(rentalsdata);
SetMachineRental(_selectedRental);
hideLoading();
}, function (err) {
hideLoading();
});
}
function rentalsTrClick() {
var rental = $(this).data('rental');
SetMachineRental(rental);
_selectedRental = rental;
$('#tbody_rentals tr').removeClass('selected');
$(this).addClass('selected');
}
function showRentals(data) {
var trs = [];
for (var i = 0; i < data.length; i++) {
var rental = data[i];
var tr = $('<tr></tr>').data('rental', rental).click(rentalsTrClick);
if (rental.Selected) {
_selectedRental = rental;
tr.addClass('selected');
}
tr.append($('<td class="machinetd" style="width: 10%;"></td>').text(rental.RentalDateStr));
tr.append($('<td class="machinetd" style="width: 12%;"></td>').text(rental.ProjectReturnDateStr));
tr.append($('<td class="machinetd" style="width: 12%;"></td>').text(rental.ReturnDateStr));
//tr.append($('<td class="machinetd" style="width: 10%;"></td>').html(replaceHtmlText(rental.Outside)));
tr.append($('<td class="machinetd" style="width: 12%;"></td>').html(replaceHtmlText(rental.Vendor)));
tr.append($('<td class="machinetd" style="width: 10%;"></td>').text(rental.RentalRate));
tr.append($('<td class="machinetd" style="width: 10%;"></td>').text(rental.Term));
tr.append($('<td class="machinetd" style="width: 10%;"></td>').text(getLangTermUnit(rental.TermUnit)));
//tr.append($('<td style="width: 24%;"></td>').attr('title', rental.Comments).html(replaceHtmlText(rental.Comments)));
trs.push(tr);
}
$('#tbody_rentals').append(trs);
}
function getLangTermUnit(unit) {
var langunit = unit
if (unit === "Hourly")
langunit = GetTextByKey("P_MA_HOURLY", "Hourly");
else if (unit === "Daily")
langunit = GetTextByKey("P_MA_DAILY", "Daily");
else if (unit === "Weekly")
langunit = GetTextByKey("P_MA_WEEKLY", "Weekly");
else if (unit === "Monthly")
langunit = GetTextByKey("P_MA_MONTHLY", "Monthly");
else if (unit === "Annually")
langunit = GetTextByKey("P_MA_ANNUALLY", "Annually");
return langunit;
}
function ManageRentalHistory() {
window.open("MachineDeviceManagement.aspx?cid=" + contractorid + "&mid=" + machineid + "#nav_managrentals");
}
function AddRental() {
showRentalConfirm(GetTextByKey("P_MA_WOULDSAVECHANGES", 'Would you like to save changes?'), GetTextByKey("P_MA_RENTAL", 'Rental'),
function () {
SaveRental();//Edit
}, function () {
$('#tab_rentalconnect').data('rentalid', '');
SaveRental(); //Add
}, null);
}