I'm using CR 2008 with an ODBC connection to a db on SQL Server 2005. The following code does almost everything that I need. I also need to be able to calculate the average response time. I'm not sure what to use or how to convert it to what I need. My times are based on a 9 hour business day. Weekends, holidays, and non-business hours are removed. The ToText result that is displayed shows the number of days, but these are 9 hour days, not 24. CR won't let me summarize the field. Any help would be greatly appreciated.
Code:
whileprintingrecords;
datetimevar createdate:={Cases.Case_CreatedDate};
numbervar createyear:=datepart("yyyy", {Cases.Case_CreatedDate});
numbervar createmonth:=datepart("m", {Cases.Case_CreatedDate});
numbervar createday:=datepart("d", {Cases.Case_CreatedDate});
numbervar createdayofweek:=dayofweek({Cases.Case_CreatedDate});
datevar createdateonly;
datetimevar respondate:={CaseProgress.Case_TimeStamp};
numbervar respondyear:=datepart("yyyy", {CaseProgress.Case_TimeStamp});
numbervar respondmonth:=datepart("m", {CaseProgress.Case_TimeStamp});
numbervar responday:=datepart("d", {CaseProgress.Case_TimeStamp});
numbervar respondayofweek:=dayofweek({CaseProgress.Case_TimeStamp});
datevar respondateonly;
numbervar secadjust:=0; //if interval spans a weekend(s) or evenings then time is removed
numbervar daydiff:=0; //number of days between create and respond
numbervar respondtime;
numbervar Days;
numbervar Hours;
numbervar Minutes;
numbervar Seconds;
shared numbervar respondflag; //reset at the case group header level
numbervar i:=1; //for looping through the Holidays
numbervar Hol:=0;
DateVar Array Holidays;
If {CaseProgress.Case_Stage}="Responded" and respondflag=0 then
(
if createdayofweek = 7 then //if Saturday, make Monday 8am
(createdate:=dateadd("d",2,createdate);
createyear:=datepart("yyyy", createdate);
createmonth:=datepart("m", createdate);
createday:=datepart("d", createdate);
createdate := cdatetime(createyear,createmonth,createday,8,00,00);)
else if createdayofweek = 1 then //if Sunday, make Monday 8am
(createdate:=dateadd("d",1,createdate);
createyear:=datepart("yyyy", createdate);
createmonth:=datepart("m", createdate);
createday:=datepart("d", createdate);
createdate := cdatetime(createyear,createmonth,createday,8,00,00);)
else if createdayofweek = 6 and time(createdate) > time(16,59,59) then //if Friday after 5pm, make Monday 8am
(createdate:=dateadd("d",3,createdate);
createyear:=datepart("yyyy", createdate);
createmonth:=datepart("m", createdate);
createday:=datepart("d", createdate);
createdate := cdatetime(createyear,createmonth,createday,8,00,00);)
else if time(createdate) > time(16,59,59) then //if after 5pm, make next day 8am
(createdate:=dateadd("d",1,createdate);
createyear:=datepart("yyyy", createdate);
createmonth:=datepart("m", createdate);
createday:=datepart("d", createdate);
createdate := cdatetime(createyear,createmonth,createday,8,00,00);)
else if time(createdate) < time(8,00,00) then //if before 8am, make 8am
createdate := cdatetime(createyear,createmonth,createday,8,00,00);
createdateonly := Date(createdate);
if respondayofweek = 7 then //if Saturday, make Monday 8am
(respondate:=dateadd("d",2,respondate);
respondyear:=datepart("yyyy", respondate);
respondmonth:=datepart("m", respondate);
responday:=datepart("d", respondate);
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);)
else if respondayofweek = 1 then //if Sunday, make Monday 8am
(respondate:=dateadd("d",1,respondate);
respondyear:=datepart("yyyy", respondate);
respondmonth:=datepart("m", respondate);
responday:=datepart("d", respondate);
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);)
else if respondayofweek = 6 and time(respondate) > time(16,59,59) then //if Friday after 5pm, make Monday 8am
(respondate:=dateadd("d",3,respondate);
respondyear:=datepart("yyyy", respondate);
respondmonth:=datepart("m", respondate);
responday:=datepart("d", respondate);
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);)
else if time(respondate) > time(16,59,59) then //if after 5pm, make next day 8am
(respondate:=dateadd("d",1,respondate);
respondyear:=datepart("yyyy", respondate);
respondmonth:=datepart("m", respondate);
responday:=datepart("d", respondate);
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);)
else if time(respondate) < time(8,00,00) then //if before 8am, make 8am
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);
respondateonly := Date(respondate);
For i := 1 to Count(Holidays) Do
(
if createdateonly = Holidays[i] then
createdate:=dateadd("d",1,createdate);
if respondateonly = Holidays[i] then
respondate:=dateadd("d",1,respondate);
if Holidays[i] > createdateonly and Holidays[i] < respondateonly then
Hol := Hol +1;
);
createdateonly := Date(createdate);
respondateonly := Date(respondate);
daydiff := DateDiff("d", createdate, respondate);
if daydiff >= 1 then //remove the number of seconds of nonbusiness hour time
(if DateDiff("ww",createdate, respondate) <> 0 then
(daydiff := daydiff - (3*DateDiff("ww",createdate, respondate)); //remove 3 days for every weekend
secadjust := 226800 * DateDiff("ww",createdate, respondate)); //add weekend seconds (63 hrs)
secadjust := secadjust + (54000 * daydiff) + (32400 * Hol)); //add non-business hr seconds (15 hrs) and holiday seconds (9 hrs)
respondtime:=DateDiff("s", createdate, respondate)-secadjust;
respondflag:=1;
Days := Truncate (respondtime / 32400); //9 hr business day
Hours := Truncate (Remainder ( respondtime , 32400) / 3600) ;
Minutes := Truncate (Remainder ( respondtime , 3600) / 60) ;
Seconds := Remainder (respondtime , 60) ;
Totext ( Days , '##' ) + ':' +
Totext ( Hours , '00' ) + ':' +
Totext ( Minutes , '00' ) + ':' +
Totext ( Seconds , '00' )
)