Calculating Return Dates

The client(Enlighten Smiles) would like to calculate the 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.

/*
* @param {bool} isExpress - type of voucher
*@parsm exemptDayResult-search result containing list of exempted dates

*@param impressionType- type of impression
* @return {String} newDate - date is returned as a string
 */
function calculateRequiredDate(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_LEAD_TIME; // deployment parameter
        }
        else
        {
            if(impressionType == DIGITAL_IMPRESSION){
                count = DIGITAL_LEAD_TIME; //deployment parameter
            }
            else
            {
                if(impressType==IMPRESSION){
                count = STD_LEAD_TIME; //deployment parameter
                }
            }
        }

        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++;
                    }
                }
            }
        }
        nlapiLogExecution("DEBUG", "count", count);
        newDate = nlapiAddDays(newDate, count);
        nlapiLogExecution("DEBUG", "count", count);
        

        while(isDateExempt(newDate, exemptDayResult) == true) 
        {
            newDate = nlapiAddDays(newDate, 1);
        }
        newDate = nlapiDateToString(newDate);
        nlapiLogExecution("DEBUG", "newDate", newDate);
    }
    catch(e)
    {
        errorHandler('calculateRequiredDate', e);
    }
    return newDate;
}




Function to validate the date is an exempt day.
/**
 * Determines if the newDate can be used as return date

 * @since 1.5.0
 * @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'));
                nlapiLogExecution("DEBUG", "exemptToDatetext", exemptToDatetext);

                if(exemptToDatetext == nlapiDateToString(newDate)) 
                {
                    dateIsExempted = true;
                    break;
                }
            }
        }
    }
    catch(e)
    {
        errorHandler('isDateExempt', e);
    }

    return dateIsExempted;
}

Leave a comment

Your email address will not be published. Required fields are marked *