Fenux.Net - The Life of a Geek
Posted on 11/11/2006 4:51 pm in code.NET

After about an hour of working through the logic in my Payroll History program, I finally found [story:vsdotnet2003sp1sucks the bug that was causing it to report the wrong goal number of hours for each employee]. It was a small mistake in the code, but a huge logical mistake. Then I discovered an even bigger one. To calculate the Goal Hours, we take the number of pay periods (which are two weeks) in the year and multiply it by 80.

To calculate the number of pay periods in the year, we use this formula:

yearstart = {@yearstart}
yearend = {@yearend}

// Find the number of days between yearstart and yearend and add one to the result.
// The +1 lets us count the first day, which we would otherwise be excluding. days = DateDiff("d", yearstart, yearend) + 1
// Perform an integer division on it, so we can ignore the remainder. formula = days 14

The first mistake was that I’d excluded the +1, so if you printed the report on payday, it would give you results that were too small because of the rounding involved.

To get the year end, you just have to use the current day if you’re in the year you’re printing, but otherwise, you use December 31st. Getting the year start is more difficult. This is where the big logic error came in.

// start with Janurary first of the year in question
yearstart = DateSerial(ToNumber({payperiod.year}), 1, 1)

// unless the employee started after the first of the year.
if ({@hiredthisyear}) Then yearstart = DateValue({employee.fdthir})

// Then find the number of days between that day and the start of a known pay period before the
// software was written.
days = DateDiff("d", DateSerial(2001,6,1), yearstart)

// Now subtract the number of days that difference is from the start of a payperiod from the
// year start day.
days = 0 - (days mod 14)
formula = DateAdd("d", days, yearstart)

When I first wrote the formula, I was adding 14 – days mod 14 to the year start date and adding 1 to the number of payperiods I calculated. It only worked right when you had a year start date in the middle of the year, which was the case I was trying to fix at the time. However, it gave a result that was always a pay period off for people who’d been there the whole year. Now it properly counts that pay period that technically started before the year did since it’s the date you get paid that matters.

Post a Comment

Most Recent Comments

Creative Commons License  Subscribe with Bloglines  Get Daily Wisdom!
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
© 2000-2012 Jason Burgess