The client(Enlighten Smiles) would like to calculate the Internal return date and update them on the voucher record. The return date is calculated by using the current date and deployment parameters based on impression type.
/**
* 1.8.9
* Calculates the Internal Return Date
* @param {bool} isExpress
* @param {bool} exemptDayResult
* @param {number} impressionType
* @return {String} newDate - date is returned as a string
*/
function calculateInternalReturnDate(isExpress, exemptDayResult, impressionType)
{
var newDate = new Date();
var datetext = '';
var count = 0;
var exemptToDatetext = '';
var tmpdate = "";
var tmpDateText = '';
try
{
datetext = nlapiDateToString(newDate);
if (isExpress == 'T')
{
count = EXPRESS_INTERNAL_RETURN_DATE;
}
else
{
if(impressionType == DIGITAL_IMPRESSION){
count = DIGITAL_INTERNAL_RETURN_DATE;
}else{
count = STD_INTERNAL_RETURN_DATE;
}
}
for (var i = 0; i < count; i++)
{
tmpdate = nlapiAddDays(newDate, i);
tmpDateText = nlapiDateToString(tmpdate);
//If date is on a weekend...
if (tmpdate.getDay() == '0' || tmpdate.getDay() == '6')
{
count++;
continue;
}
//if array and not empty
if (exemptDayResult instanceof Array && exemptDayResult.length > 0)
{
//Checks throught all exempt days and makes sure the new date is not equal to it
for (var j = 0; j < exemptDayResult.length; j++)
{
exemptToDatetext = (exemptDayResult[j].getValue('custrecord_exempt_day'));
if(exemptToDatetext == tmpDateText)
{
count++;
}
}
}
}
newDate = nlapiAddDays(newDate, count);
while(isDateExempt(newDate, exemptDayResult) == true)
{
newDate = nlapiAddDays(newDate, 1);
}
newDate = nlapiDateToString(newDate);
}
catch(e)
{
errorHandler('calculateInternalReturnDate', e);
}
return newDate;
}
/**
* 1.8.9
* Determines if the newDate can be used as return date
* @param {Date} newDate
* @param {searchResult} exemptDayResult
* @returns {Boolean}
*/
function isDateExempt(newDate, exemptDayResult)
{
var exemptToDatetext = '';
var dateIsExempted = false;
var counter = 0;
try
{
if(newDate.getDay() == SATURDAY_DAY_NUMBER)
{
dateIsExempted = true;
//If date is on a weekend...
}
else if (newDate.getDay() == SUNDAY_DAY_NUMBER)
{
dateIsExempted = true;
}
else if (exemptDayResult instanceof Array && exemptDayResult.length > 0)
{
//Checks throught all exempt days and makes sure the new date is not equal to it
for (counter = 0; counter < exemptDayResult.length; counter++)
{
exemptToDatetext = (exemptDayResult[counter].getValue('custrecord_exempt_day'));
if(exemptToDatetext == nlapiDateToString(newDate))
{
dateIsExempted = true;
break;
}
}
}
}
catch(e)
{
errorHandler('isDateExempt', e);
}
return dateIsExempted;
}
EXPRESS_INTERNAL_RETURN_DATE,DIGITAL_IMPRESSION,DIGITAL_INTERNAL_RETURN_DATE and STD_INTERNAL_RETURN_DATE are deployment parameter set in the account