Payroll Calendar

Posted by Paulh0763 on 31-Mar-2015 14:29

I have a Setup Calendar object and when I create payroll it is not doing them correctly for semi-monthly and monthly. I thought I figured it out but no luck. If someone can pinpoint what is wrong I would appreciate all input. Here is the code and the output.

Thanks! Big Smile

//Statically set the number of years you want payroll to be setup for when this trigger runs.
var YEARS_OF_PAYROLL = 2;

//Get the holiday dates
var holidays = rbv_api.selectQuery("SELECT bank_holiday, dueDate from holiday", 1000);

//If check date is on holiday
var checkHoliday   = ('{!check_date_on_holida#code}' === 'B') ? -1 : 1;

//If check date is on Saturday
var checkSaturday = ('{!check_date_on_saturd#code}' === 'F') ? -1 : 2;

//If check date is on Sunday
var checkSunday   = ('{!check_date_on_sunday#code}' === 'F') ? -2 : 1;

//Variable to hold frequency 
var frequency = {!payroll_frequency#code};

//Function to set day or month depending on frequency
var setter = (frequency === 52) ? "setDate" : (frequency === 26) ? "setDate" : (frequency === 24) ? "setMonth" : (frequency === 12) ? "setMonth" : (frequency === 4) ? "setMonth" : "setMonth";

//Function to get day or month depending on setter
var getter = setter.replace("set","get");

//Multiplier, dependent on frequency
var multiplier = (frequency === 52) ? 7 : (frequency === 26) ? 14 : (frequency === 24) ? 1 : (frequency === 12) ? 1  : (frequency === 4) ? 3 : 4;

//Calculate the number of records to create in Payroll Calendar
var numOfPayrolls = (frequency === 24) ? (12 * YEARS_OF_PAYROLL) : (frequency * YEARS_OF_PAYROLL);

//Map to hold one record.
var data = {};

//Initialize dates
var start_date = '{!period_start_date#js}', 
    end_date   = '{!period_end_date#js}', 
    call_date  = '{!call_in_date#js}',
    check_date = '{!first_check_date#js}';


/**************Create records****************************/
for(var i = 0; i < numOfPayrolls; i++)
{
   data = determinePayrollDates(i);
   rbv_api.createRecord("payroll_calendar", data);
}
/********************************************************/



/**************Create second set if Semi-Monthly*********/
if(frequency === 24)
{
  start_date = '{!period_2_start_date#js}', 
  end_date   = '{!period_2_end_date#js}', 
  call_date  = '{!call_in_date_2#js}',
  check_date = '{!period_2_check_date#js}';
  
  for(var i = 0; i < numOfPayrolls; i++)
  {
     data = determinePayrollDates(i);
     rbv_api.createRecord("payroll_calendar", data);
  }
}
/*******************************************************/



/****************Determine Payroll Dates*****************/
function determinePayrollDates(index)
{
   //Map to hold one record
   var data = {};

   var d = new Date(end_date);
   var n = d.getMonth();

   startDate = new Date(start_date);
   startDate[setter](startDate[getter]() + (multiplier*index));
   
   endDate = new Date(end_date);
   endDate[setter](endDate[getter]() + (multiplier*index));

  var quartEnd = new Date(endDate);
  quartEnd.setDate(quartEnd.getDate() - 2);
  quartEnd.setDate(0);

   var tempEnd = new Date(endDate);
   tempEnd.setDate(tempEnd.getDate() + 17);
   tempEnd.setDate(0);

   var thirtyEnd = new Date(endDate);
   thirtyEnd.setDate(thirtyEnd.getDate() - 2);
   thirtyEnd.setDate(0);


   var tempStart = new Date(startDate);
   tempStart.setDate(1);
   var semiMonthCheck = frequency === 24 && startDate.toString() !== tempStart.toString();

   if(multiplier === 1 && n === 1 && endDate.getDate() < tempEnd.getDate() && (semiMonthCheck || frequency === 12)) {
     endDate.setDate(tempEnd.getDate() + 17);
     endDate.setDate(0)
   }
   if(multiplier === 3 && endDate.getDate() < quartEnd.getDate()) {
     endDate.setDate(quartEnd.getDate() - 2);
     endDate.setDate(0);
   }

   if(multiplier === 1 && (n === 3 || n === 5 || n === 8 || n === 10) && endDate.getDate() < thirtyEnd.getDate() && (semiMonthCheck || frequency === 12)) {
     endDate.setDate(thirtyEnd.getDate() - 2);
     endDate.setDate(0)
   }
   rbv_api.println("date after= " +endDate);

   var callDate  = new Date(call_date);
   callDate[setter](callDate[getter]() + (multiplier*index));

   var checkDate = new Date(check_date);
   checkDate[setter](checkDate[getter]() + (multiplier*index));

   adjustForHolidaysWeekends(checkDate, data);

   data.R7041490          = {!R5585065#id}; 
   data.R7802784          = {!id};
   data.period_start_date = startDate;
   data.period_end_date   = endDate;
   data.call_in_date      = callDate;
   data.check_date        = checkDate;

   return data;
}
/*********************************************************/



/********Adjust checkdate for holidays and weekends*******/
function adjustForHolidaysWeekends(checkDate, data)
{
  //Check for holidays
   for(var i = 0; i < holidays.length; i++)
       if((holidaysIdea[1].getMonth() == checkDate.getMonth()) && (holidaysIdea[1].getDate() == checkDate.getDate()))
         switch(checkDate.getDay())
         {
           case 5:
              if(checkHoliday === -1)
                checkDate.setDate(checkDate.getDate() + checkHoliday);
              else
                checkDate.setDate(checkDate.getDate() + checkHoliday+2);
              data.adjusted_due_to_holi = true;
              break;
           case 1:
             if(checkHoliday === -1)
                checkDate.setDate(checkDate.getDate() + checkHoliday-2);
              else
                checkDate.setDate(checkDate.getDate() + checkHoliday);
             data.adjusted_due_to_holi = true;
             break;
           case 6:
           case 0:
             break;
           default:
             checkDate.setDate(checkDate.getDate() + checkHoliday);
             data.adjusted_due_to_holi = true;
             break;
         }

   switch(checkDate.getDay())
   {
       case 6:
            checkDate.setDate(checkDate.getDate() + checkSaturday);
            data.adjusted_due_to_week = true;
            break;
       case 0:
            checkDate.setDate(checkDate.getDate() + checkSunday);
            data.adjusted_due_to_week = true;
            break;
    }
}
/******************************************************/

date after= Thu Jan 15 2015 00:00:00 GMT-0500 (EST)
date after= Sun Feb 15 2015 00:00:00 GMT-0500 (EST)
date after= Sun Mar 15 2015 00:00:00 GMT-0400 (EDT)
date after= Wed Apr 15 2015 00:00:00 GMT-0400 (EDT)
date after= Fri May 15 2015 00:00:00 GMT-0400 (EDT)
date after= Mon Jun 15 2015 00:00:00 GMT-0400 (EDT)
date after= Wed Jul 15 2015 00:00:00 GMT-0400 (EDT)
date after= Sat Aug 15 2015 00:00:00 GMT-0400 (EDT)
date after= Tue Sep 15 2015 00:00:00 GMT-0400 (EDT)
date after= Thu Oct 15 2015 00:00:00 GMT-0400 (EDT)
date after= Sun Nov 15 2015 00:00:00 GMT-0500 (EST)
date after= Tue Dec 15 2015 00:00:00 GMT-0500 (EST)
date after= Fri Jan 15 2016 00:00:00 GMT-0500 (EST)
date after= Mon Feb 15 2016 00:00:00 GMT-0500 (EST)
date after= Tue Mar 15 2016 00:00:00 GMT-0400 (EDT)
date after= Fri Apr 15 2016 00:00:00 GMT-0400 (EDT)
date after= Sun May 15 2016 00:00:00 GMT-0400 (EDT)
date after= Wed Jun 15 2016 00:00:00 GMT-0400 (EDT)
date after= Fri Jul 15 2016 00:00:00 GMT-0400 (EDT)
date after= Mon Aug 15 2016 00:00:00 GMT-0400 (EDT)
date after= Thu Sep 15 2016 00:00:00 GMT-0400 (EDT)
date after= Sat Oct 15 2016 00:00:00 GMT-0400 (EDT)
date after= Tue Nov 15 2016 00:00:00 GMT-0500 (EST)
date after= Thu Dec 15 2016 00:00:00 GMT-0500 (EST)
date after= Sat Jan 31 2015 00:00:00 GMT-0500 (EST)
date after= Tue Mar 03 2015 00:00:00 GMT-0500 (EST)
date after= Tue Mar 31 2015 00:00:00 GMT-0400 (EDT)
date after= Fri May 01 2015 00:00:00 GMT-0400 (EDT)
date after= Sun May 31 2015 00:00:00 GMT-0400 (EDT)
date after= Wed Jul 01 2015 00:00:00 GMT-0400 (EDT)
date after= Fri Jul 31 2015 00:00:00 GMT-0400 (EDT)
date after= Mon Aug 31 2015 00:00:00 GMT-0400 (EDT)
date after= Thu Oct 01 2015 00:00:00 GMT-0400 (EDT)
date after= Sat Oct 31 2015 00:00:00 GMT-0400 (EDT)
date after= Tue Dec 01 2015 00:00:00 GMT-0500 (EST)
date after= Thu Dec 31 2015 00:00:00 GMT-0500 (EST)
date after= Sun Jan 31 2016 00:00:00 GMT-0500 (EST)
date after= Wed Mar 02 2016 00:00:00 GMT-0500 (EST)
date after= Thu Mar 31 2016 00:00:00 GMT-0400 (EDT)
date after= Sun May 01 2016 00:00:00 GMT-0400 (EDT)
date after= Tue May 31 2016 00:00:00 GMT-0400 (EDT)
date after= Fri Jul 01 2016 00:00:00 GMT-0400 (EDT)
date after= Sun Jul 31 2016 00:00:00 GMT-0400 (EDT)
date after= Wed Aug 31 2016 00:00:00 GMT-0400 (EDT)
date after= Sat Oct 01 2016 00:00:00 GMT-0400 (EDT)
date after= Mon Oct 31 2016 00:00:00 GMT-0400 (EDT)
date after= Thu Dec 01 2016 00:00:00 GMT-0500 (EST)
date after= Sat Dec 31 2016 00:00:00 GMT-0500 (EST)



All Replies

Posted by Godfrey Sorita on 02-Apr-2015 15:48
Hi Paul,

Please log a support case for this question/problem. A Progress TSE will be assisting you in debugging and pointing the cause of the error.


Regards,
Godfrey
This thread is closed