Slight problem Calculating No. of working days...

JamieBrown
JamieBrown Member Posts: 107
Hi, I have the following code calulating the number of working days in a calendar month. This works fine apart from when the enddate = nonworking. This makes noofdays 1 too many. Would I need, if the enddate = nonworking then noofdays := noofdays -1? (Noofdays is a boolean)

As you can tell, I'm still fairly new to Navision, so I hope this makes sense...

Thanks in advance.
Form - OnInit()

d := TODAY;
datetoday := DATE2DMY(d,1);

Startdate := CALCDATE('-CM',WORKDATE); 
enddate := CALCDATE('CM',WORKDATE); 
companyinfo.GET; 
REPEAT 
nonworking := calendarMgt.CheckDateStatus(companyinfo."Base Calendar Code",Startdate,datedscr);

IF NOT nonworking THEN 
noofdaysTD := noofdaysTD + 1;

Startdate := Startdate + 1; 
UNTIL Startdate >= d;

noofdaysTD := noofdaysTD +1;

Startdate := CALCDATE('-CM',WORKDATE); 
enddate := CALCDATE('CM',WORKDATE); 
companyinfo.GET; 
REPEAT 
nonworking := calendarMgt.CheckDateStatus(companyinfo."Base Calendar Code",Startdate,datedscr);

IF NOT nonworking THEN 
noofdays := noofdays + 1; 

Startdate := Startdate + 1; 
UNTIL Startdate >= enddate;

noofdays := noofdays + 1;

dateperc := ROUND(noofdaysTD / noofdays * 10000,1);

Answers

  • Marije_Brummel
    Marije_Brummel Member, Moderators Design Patterns Posts: 4,262
    Why don't you use the date table? You can set the filters and loop through the record, seeing if it is a working day and count them.
  • David_Cox
    David_Cox Member Posts: 509
    edited 2006-09-19
    [You are adding a "non conditional" day on at the end, so you need to check if the last day is a workingday then add the extra day!

    Startdate := Startdate + 1;
    UNTIL Startdate >= enddate;

    nonworking := calendarMgt.CheckDateStatus(companyinfo."Base Calendar Code",Endingdate,datedscr);
    IF NOT nonworking THEN
    noofdays := noofdays + 1;
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • JamieBrown
    JamieBrown Member Posts: 107
    The public holidays will be entered manually into the base calendar table as non-working days.

    Basically, we're closed on Saturdays and bank holidays. So if either of these fall on the last day of the month it will report incorrectly.

    To get around the Saturday problem is straight forward with:
    sat := DATE2DWY(enddate, 1);
    If sat = 6 then begin
    noofdays := noofdays -1;
    end;
    

    However, I'm not sure how the public holidays would work? Maybe we could just live with it?
  • JamieBrown
    JamieBrown Member Posts: 107
    Sorry David, I didn't see all of your reply...

    Yes your logic was right. It works with the following:
    
    Startdate := Startdate + 1; 
    UNTIL Startdate >= enddate;
    nonworking := calendarMgt.CheckDateStatus(companyinfo."Base Calendar Code",Startdate,datedscr);
    
    IF nonworking THEN
    noofdays := noofdays -1;
    

    Thanks alot for your help.
    Jamie