Tuesday, January 18, 2011

Calculate business response time - using Javascript

Problem:
Figure out when we must respond to a customer based on their service plan level. The response time must only include business hours.

Example, customer A calls in with a support problem on Friday at 4 PM. Their service plan level calls for an 8 business hour response time. Our business hours are 8 am - 5 pm. getFollowUpBy(<8-hour serviceplan>) will return the next Monday 3 PM.


Source:
/**
 * @author Mac
 */

function getNextBizDay(d)
{
    //Return the next business day
    var nextBizDay = new Date();
    var day = d.getDay();
    var addDays = 0;
    if(day < 5) //Monday through Thursday, add
    addDays = 1;
    else if (day == 5) //Friday 1=Sat, 2=Sun, 3=Mon
    addDays = 3;
    else //Saturday
    addDays = 2;
    nextBizDay.setDate(d.getDate() + addDays);
    nextBizDay.setHours(8);
    nextBizDay.setMinutes(0);
    nextBizDay.setSeconds(0);
    nextBizDay.setMilliseconds(0);
    return nextBizDay;
}


function getFollowUpBy(serviceplan)
{
    var mils = 3600000;
    switch(serviceplan)
    {
        case 3:
            mils *= 8;
            break;
        case 4:
            mils *= 3;
            break;
        case 5:
            mils *= 1;
            break;
        default:
            return null;//This means they dont have a service plan, or non-support service plan
    }
  
    var d = new Date();
    var eod = new Date(); //End Of (business) Day
    eod.setHours(17);
    eod.setMinutes(0);
    eod.setSeconds(0);
    eod.setMilliseconds(0);
  
    var nextBizDay = getNextBizDay(d);
  
    var remainingMils = eod.getTime() - d.getTime();
    if(remainingMils > 0)
    {
        if(mils > remainingMils) //Time left over
        {
            nextBizDay.setTime(nextBizDay.getTime() + (mils - remainingMils));
        }
        else //apply all mils to today
        {
            nextBizDay.setTime(d.getTime() + mils);
          
        }
    }
    else
    {
        nextBizDay.setTime(nextBizDay.getTime() + mils);
    }  
    return nextBizDay;
}

No comments:

Post a Comment

There was an error in this gadget