2014-09-11 62 views
1

我目前正在开展一个项目,并遇到一个我目前无法解决的挑战。我尝试了很长时间,努力并最终把我的场景放在这里,因为我可以得到一些有用的提示。另外,请耐心等待,因为我将详细介绍这个问题。根据不同的工资率格式计算工资

因此,我有一个项目,其中包含根据他们的班次时间(时间为24小时制)计算其员工工资的模块。现在,按照周日(周一至周五)的付款时间,付款率不同,付款率分为两部分:Week DayWeek Night.而且,weekend(星期六&星期日)的付款率也与正常日不同。最后,有public holidays,他们也有不同的薪酬比其他事件。下表列出了工资率的总结:

------------------------------------------ 
|Event   | Shift Times    | 
------------------------------------------ 
|Week Day  | 0600-1800    | 
------------------------------------------ 
|Week Night | 1800-0600    | 
------------------------------------------ 
|Weekend  | All day weekend rate  | 
------------------------------------------- 
|Public holiday|All day public holiday rate| 
-------------------------------------------+ 

现在,这看起来很简单,如果你要只为特别的日子分配工资标准,如移的公众假期,然后分配给公众假期率,但问题是这不是我的项目中的情况。

它是如何工作

现在,这种转变通常是12小时长的和如一个员工开始于0900并完成对Monday2100两种不同的工资水平之间可以下降。因此,计算小时数为week day工资率为9小时,week night工资率为3小时。同样,员工也可以按照三种不同的工资率进行工作,例如:周日晚上从2200开始,在Monday早上完成0900。因此,工资率小时为weekend,工作时间为2小时,周日晚上开始班次,2小时后周末结束。因此,从00000600(6小时),薪资费率将以week night费率为基础,其余时间在周薪之内。

现在好消息是我能够做到此为止。一切都很完美。虽然我已经把很多条件放在里面,但我认为这不是一个好习惯,但是暂时我想让它运行。 Public Holiday发生时会出现问题。现在公众假期是最困难的部分,因为它可以在任何一天(周和周末)出现,并且必须在每种情况下重新检查,并且必须进行时间的增加和减少,这些时间太复杂并且不起作用。如果整个班次都是在公共假期,我能够完成这部分工作。但是,如果它下降超过一个薪酬类别,那么我很难解决这个问题。由于代码太复杂,您可能会感到困惑,因此我不会分享公共假期的代码。但是,我将分享其他付费率格式正常工作的代码。

注意 我会建议你当我想你的建议不从我的代码得到任何想法,如果你看一下我的代码,你可能会遇到的想法。

function getHoursBreakDown($time1, $time2){ 

    $hoursForShift = array(
     "weekday" => 0, 
     "weeknight" => 0, 
     "saturday" => 0, 
     "sunday" => 0, 
     "public" => 0 
    ); 

    $t1 = strtotime($time1); 
    $t2 = strtotime($time2); 

    $t1_Day = (int) date("N", $t1); 
    $t2_Day = (int) date("N", $t2); 

    $t1_Hour = (int) date("G", $t1); 
    $t2_Hour = (int) date("G", $t2); 

    $t1_Date = (int) date("j", $t1); 
    $t2_Date = (int) date("j", $t2); 

    $t1_Month = (int) date("n", $t1); 
    $t2_Month = (int) date("n", $t2); 

    $t1_Minutes = round(date("i", $t1)/60, 2); // to substitute, extra 
    $t2_Minutes = round(date("i", $t2)/60, 2); // to add, lost 

    // Working days 
    if ($t1_Day<6) { 


     if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 

     for($i = $t1_Hour; $i< $t2_Hour; $i++){ 


      if (($i>= 6 && $i < 18) || ($t2_Day < 6 && $i >= 30)) { 
       $hoursForShift['weekday']++; 

      } else if ($t2_Day < 6 || $i < 24) { 
       $hoursForShift['weeknight']++; 

      } else if ($t2_Day == 6) { 
       $hoursForShift['saturday']++; 

      } else if ($t2_Day == 7) { 
       $hoursForShift['sunday']++; 
      } 
     } 

     $t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour; 

     // Deducting extra minutes from sign in time 
     if  ($t1_Hour >= 6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes; 
     else if ($t1_Hour < 6 || $t1_Hour >= 18) $hoursForShift['weeknight'] -= $t1_Minutes; 

     // Adding lost minutes to sign out 
     if  ($t2_Day < 6 && ($t2_Hour >= 6 && $t2_Hour < 18)){ $hoursForShift['weekday'] += $t2_Minutes; } 
     else if ($t2_Day < 6 && ($t2_Hour < 6 || $t2_Hour >= 18)) $hoursForShift['weeknight'] += $t2_Minutes; 

     // Adding lost minutes to sign out if, sign out on weekend 
     if  ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes; 

    } else { 
     //Weekends 

     if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 

     for($i = $t1_Hour; $i< $t2_Hour; $i++){ 

      if ($t2_Day == 1 && ($i%24) < 12) { 
       $hoursForShift['weeknight']++; 

      } else if ($t1_Day == 6 && $i < 24) { 
       $hoursForShift['saturday']++; 

      } else if ($t1_Day == 7 || ($t1_Day == 6 && $i >= 24)) { 
       $hoursForShift['sunday']++; 
      } 
     } 

     if ($t2_Day == 1) $hoursForShift['weeknight'] -= $t1_Minutes; 
     if ($t2_Day == 6) $hoursForShift['saturday'] -= $t1_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes; 

     if ($t2_Day == 1) $hoursForShift['weeknight'] += $t2_Minutes; 
     else if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes; 
    } 
} 

好处是,没有什么是从数据库中走出来。这个问题是基于完全的头脑计算和技术,并且使用纯粹的PHP。

请高度赞赏这方面的任何帮助。 谢谢。

+0

付薪率你的意思**每小时**? – 2014-09-12 15:38:04

+0

@CSᵠ:是小时。 – 2014-09-13 08:23:17

回答

2

Huff !!经过漫长而艰苦的工作,并最终整天拉我的头发,我能够找出问题的解决方案。以下功能是我正在寻找的问题的所有答案。

function getHoursBreakDown($time1, $time2){ 

    $hoursForShift = array(
     "weekday" => 0, 
     "weeknight" => 0, 
     "saturday" => 0, 
     "sunday" => 0, 
     "public" => 0 
    ); 

    $t1 = strtotime($time1); 
    $t2 = strtotime($time2); 

    $t1_Day = (int) date("N", $t1); 
    $t2_Day = (int) date("N", $t2); 

    $t1_Hour = (int) date("G", $t1); 
    $t2_Hour = (int) date("G", $t2); 

    $t1_Date = (int) date("j", $t1); 
    $t2_Date = (int) date("j", $t2); 

    $t1_Month = (int) date("n", $t1); 
    $t2_Month = (int) date("n", $t2); 

    $t1_Year = (int) date("Y", $t1); 
    $t2_Year = (int) date("Y", $t2); 

    $t1_Minutes = round(date("i", $t1)/60, 2); // to substitute, extra 
    $t2_Minutes = round(date("i", $t2)/60, 2); // to add, lost 

    $start=explode(' ', $time1); 
    $finish=explode(' ', $time2); 

     $publicHolidays = array(
     "2014-01-01", 
     "2014-01-27", 
     "2014-04-18", 
     "2014-04-19", 
     "2014-04-21", 
     "2014-04-25", 
     "2014-06-09", 
     "2014-08-17", 
     "2014-10-06", 
     "2014-11-14", 
     "2014-12-25", 
     "2014-12-26" 

    ); 


    // Working days 
    if ($t1_Day<6) { 

     if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 

     for($i = $t1_Hour; $i< $t2_Hour; $i++){ 


      if (($i>= 6 && $i < 18) || ($t2_Day < 6 && $i >= 30 && $i< 42)) { 
       $hoursForShift['weekday']++; 

      } else if ($t2_Day < 6 || $i < 24) { 
       $hoursForShift['weeknight']++; 

      } else if ($t2_Day == 6) { 
       $hoursForShift['saturday']++; 

      } else if ($t2_Day == 7) { 
       $hoursForShift['sunday']++; 
      } 
     } 

     $t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour; 

     // Deducting extra minutes from sign in time 
     if((!in_array($start[0], $publicHolidays)) && (!in_array($finish[0], $publicHolidays))) { 
     if  ($t1_Hour >= 6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes; 
     else if ($t1_Hour < 6 || $t1_Hour >= 18) $hoursForShift['weeknight'] -= $t1_Minutes; 

     // Adding lost minutes to sign out 
     if  ($t2_Day < 6 && ($t2_Hour >= 6 && $t2_Hour < 18)){ $hoursForShift['weekday'] += $t2_Minutes; } 
     else if ($t2_Day < 6 && ($t2_Hour < 6 || $t2_Hour >= 18)) $hoursForShift['weeknight'] += $t2_Minutes; 

     // Adding lost minutes to sign out if, sign out on weekend 
     if  ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes; 
    } 

    } else if($t1_Day>=6) { 
     //Weekends 

     if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 

     for($i = $t1_Hour; $i< $t2_Hour; $i++){ 


      if ($t2_Day == 1 && $i>=24 && $i<30) { 
       $hoursForShift['weeknight']++; 

      }else if ($t2_Day == 1 && $i>29 && $i<42) { 
       $hoursForShift['weekday']++; 

      }else if ($t1_Day == 6 && $i < 24) { 
       $hoursForShift['saturday']++; 

      } else if ($t1_Day == 7 || ($t1_Day == 6 && $i >= 24)) { 
       $hoursForShift['sunday']++; 
      } 
     } 


     if((!in_array($start[0], $publicHolidays)) && (!in_array($finish[0], $publicHolidays))) { 
     $t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour; 

     if ($t1_Day == 1 && $t1_Hour>=0 && $t1_Hour<6) $hoursForShift['weeknight'] -= $t1_Minutes; 
     else if ($t1_Day == 1 && $t1_Hour>=6 && $t1_Hour<18) $hoursForShift['weekday'] -= $t1_Minutes; 
     else if ($t1_Day == 6) $hoursForShift['saturday'] -= $t1_Minutes; 
     else if ($t1_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes; 

     if ($t2_Day == 1 && $t2_Hour<6 || $t2_Day == 1 && $t2_Hour>=18) $hoursForShift['weeknight'] += $t2_Minutes; 
     else if ($t2_Day == 1 && $t2_Hour>=6 && $t2_Hour < 18) $hoursForShift['weekday'] += $t2_Minutes; 
     else if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes; 
    } 

    } 
     // If the start and end date is public holiday 
     if((in_array($start[0], $publicHolidays)) && (in_array($finish[0], $publicHolidays))) 

     { 

      $hoursForShift['public'] = $hoursForShift['weekday']; 
      $hoursForShift['weekday'] = 0; 

      $hoursForShift['public'] += $hoursForShift['weeknight']; 
      $hoursForShift['weeknight'] = 0; 

      $hoursForShift['public'] += $hoursForShift['saturday']; 
      $hoursForShift['saturday'] = 0; 

      $hoursForShift['public'] += $hoursForShift['sunday']; 
      $hoursForShift['sunday'] = 0; 

      $hoursForShift['public'] -= $t1_Minutes; 
      $hoursForShift['public'] += $t2_Minutes; 

      //If start date is on public holiday but the end date 
     }else if ((in_array($start[0], $publicHolidays)) && (! in_array($finish[0], $publicHolidays))) { 


      if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 


      for($i = $t1_Hour; $i<24; $i++){ 

      if($t1_Day<6){  


      if (($i>=6 && $i <18)) { 
       $hoursForShift['weekday']--; 
       $hoursForShift['public']++; 

      } else if ($i >=18 && $i <24) { 

       $hoursForShift['weeknight']--; 
       $hoursForShift['public']++; 

      } 
      else if ($i>0 && $i<=6) { 
       $hoursForShift['weeknight']--; 
       $hoursForShift['public']++; 
      } 
      } else if ($t1_Day == 6) { 

       $hoursForShift['saturday']--; 
       $hoursForShift['public']++; 

      } else if ($t1_Day == 7) { 

       $hoursForShift['sunday']--; 
       $hoursForShift['public']++; 
      } 
     } 

     $t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour; 

     $hoursForShift['public'] -= $t1_Minutes; 


     if ($t2_Day <6 && $t2_Hour>0 && $t2_Hour<6 || $t2_Day <6 && $t2_Hour>=18) $hoursForShift['weeknight'] += $t2_Minutes; 
     else if ($t2_Day <6 && $t2_Hour>=6 && $t2_Hour < 18) $hoursForShift['weekday'] += $t2_Minutes; 
     else if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes; 
     else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes; 

     //If the start date is not on a public holiday but end date is. 

     }else if ((! in_array($start[0], $publicHolidays)) && (in_array($finish[0], $publicHolidays))) { 


      if ($t1_Hour > $t2_Hour) $t2_Hour += 24; 


      for($i = 25; $i<= $t2_Hour; $i++){ 

      if($t2_Day<6){  

      if ($i>24 && $i <=30) { 
       $hoursForShift['weeknight']--; 
       $hoursForShift['public']++; 

      } else if ($i >=30 && $i < 42) { 

       $hoursForShift['weekday']--; 
       $hoursForShift['public']++; 

      } 

      } else if ($t2_Day == 6) { 

       $hoursForShift['saturday']--; 
       $hoursForShift['public']++; 

      } else if ($t2_Day == 7) { 

       $hoursForShift['sunday']--; 
       $hoursForShift['public']++; 
      } 
     } 


     $hoursForShift['public'] += $t2_Minutes; 


     if ($t1_Day <6 && $t1_Hour>0 && $t1_Hour<6 || $t1_Day <6 && $t1_Hour>=18) $hoursForShift['weeknight'] -= $t1_Minutes; 
     else if ($t1_Day <6 && $t1_Hour>=6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes; 
     else if ($t1_Day == 6) $hoursForShift['saturday'] -= $t1_Minutes; 
     else if ($t1_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes; 


     } 


    return $hoursForShift; 
    }