0

I've been asked to write a web page that can work out staff paid hours, but the logic of calculating deductions and overtime is driving me mad.

Staff are paid monthly, but any absences are deducted from that weeks overtime before being deducted from pay. Most weeks this isn't an issue I can work out whether there is any OT left after absence and if the value is negative then there is no OT payment and a further reduction in paid hours.

However, I have to calculate the start of the month, which may begin mid week. This is where the logic becomes difficult, because an employee may have worked 4hrs Overtime on Wednesday August 1st (for instance), but had 7.5hrs absence on Tuesday July 31st. Obviously we need to deduct some of the overtime.

This is further complicated by the fact that we already deducted the absence from July's pay, but we need to account for the fact that some of those hours were double time hours.

I've approached this by calculating first of all the balance of Overtime - Absences in the week on each side of the month start. This can lead to four possible outcomes:

  • There is a a positive balance of OT hours in the latter part of the week and a negative balance of deductions in the previous part of the week
  • There is a a positive balance of OT hours in the previous part of the week and a negative balance of deductions in the latter part of the week
  • There is a positive or 0 balance of OT in both parts of the week
  • There is a negative or 0 balance of deductions in both parts of the week

In the final two instances, I need take no action, however in the case of the former I need to adjust this months pay to compensate for the time off in that week. Just to throw in one last curveball, overtime hours are at double time, deductions are normal time. This means that if I am absent 7.5hrs on Tues July 31st and work 4hrs OT on Weds August 1st, then the first four hours of the deduction should be at double time, leading to 11.5hrs of deductions right? Only I can't calculate it as just lose the 4hrs OT, because in July I already will have lost 7.5hrs of paid work, so I should only lose 4 paid hours, which is 2hrs of OT.

Here is my code so far, commented to the best of my ability. I am trying to work out what the values of overtime and deductions are in each instance:

//get imbalances
$pre = $preOvertime - abs($preDeductions);
$post = $postOvertime - abs($postDeductions);
/*    FOUR POSSIBILITIES:    */
// need to reduce ot hours from pre by absence hours from post to compensate
if($pre > 0 AND $post < 0){
    if(($pre - abs($post)) < 0){
        // compensating for too much time off 
        // in first partial week of month
        $weeklyminus = ??????;
        $weeklyOT = ??????;
    } else {
        // compensating for too much time off 
        // in last partial week of last month
        $weeklyminus = ??????;
        $weeklyOT = ??????;
    }
}
// need to reduce ot hours from post by absence hours from pre to compensate
if($pre < 0 AND $post > 0){
    // if paid hrs OT is less than hours absence
    if(abs($pre) < $post){
        // deduct the amount of absence in normal hours
        $weeklyminus = abs($pre); //since we already paid for it once, reduce further with single time hours
        $weeklyOT = $post; //double time hours
    } else {
        // the most you can take is $post paid normal hours
        $weeklyminus = $post; //since we already paid for it once, reduce further with single time hours
        $weeklyOT = $post; //double time hours
     }
}
// negative hours both sides, no remaining OT hours to reduce
if($pre <= 0 AND $post <= 0){
    $weeklyOT = $postOvertime;
    $weeklyminus = $postDeductions;
}
// positive hours both sides, no need to reduce hours to compensate
if($pre >= 0 AND $post >= 0){
    $weeklyOT = $postOvertime;
    $weeklyminus = $postDeductions;
}

If anyone can think of a simpler way to approach this, I would greatly appreciate it, this has been driving me crazy for some time. Part of the reason I post here is to force myself to lay it out clearly in the hopes it gives me some clarity, but honestly I am starting to feel that I cannot see the woods for the trees and need an outside eye.

Thanks in advance.

MikeK
  • 373
  • 1
  • 2
  • 16
  • Why are you considering weeks? Do this per day and then aggregate into whatever larger time periods you need. – vascowhite Aug 22 '13 at 08:15
  • vascowhite: because overtime and deductions from it are weekly, not sure how I would do this on a daily basis while still baring in mind the week start/end, and accounting for this whole first part-week in the month compensating for too much time off conundrum – MikeK Aug 22 '13 at 08:17

0 Answers0