Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. <?php
  2. function labor_summary_post() {
  3. $ONEWEEKSECONDS = 604800;
  4. $ONEDAYSECONDS = 86400;
  5.  
  6. $weeklimit = 0; //weekly overtime hours threshold; 0 = no weekly OT consideration e.g. 40
  7. $daylimit = 0; //daily overtime hours threshold; 0 = no daily OT consideration e.g. 8
  8. $payrollstart = 0; //day of the week payroll starts; Sun-Sat = 0-6
  9.  
  10. $offset = 0; //time offset from UTC default in seconds
  11.  
  12. $startDate = strtotime($this->post('startDate')); //unix timestamp of query start date
  13. $endDate = strtotime($this->post('endDate')); //unix timestamp of query end date
  14.  
  15. if ($this->post('startTime')){ //optional
  16. $startTime = strtotime($this->post('startTime')); //unix timestamp of query start time
  17. $startminute = date('i', $startTime); //0-59
  18. $starthour = date('H', $startTime); //0-23
  19. $startseconds = date('s', $startTime); // 1-12
  20. }
  21. else{ //defaults to 00:00
  22. $startTime = 0;
  23. $startminute = 0;
  24. $starthour = 0;
  25. $startseconds = 0;
  26. }
  27. if($this->post('endTime')){ //end time provided
  28. $endTime = strtotime($this->post('endTime')); //unix timestamp of query end time
  29. $endminute = date('i', $endTime);
  30. $endhour = date('H', $endTime);
  31. $endseconds = date('s', $endTime);
  32. }else{ //defaults to 00:00
  33. $endTime = 0;
  34. $endminute = 0;
  35. $endhour = 0;
  36. $endseconds = 0;
  37. }
  38.  
  39. $start = $startDate + ($starthour *3600) + ($startminute *60) + $startseconds; //query start in seconds
  40. $end = $endDate + ($endhour *3600) + ($endminute *60) + $endseconds; //query end in seconds
  41. if($endTime == 0){ //only end date provided; assume end of day
  42. $end += $ONEDAYSECONDS;
  43. }
  44.  
  45. $startz = $start - $offset; //start, timezone adjusted
  46. $endz = $end - $offset; //end, timezone adjusted
  47.  
  48. //day of nearest, prior payroll start
  49. $startweekday = date('w', $startDate);
  50. if($startweekday < $payrollstart){
  51. $daystoprevpayroll = 7 - ($payrollstart - $startweekday);
  52. } else{
  53. $daystoprevpayroll = abs($payrollstart - $startweekday);
  54. }
  55. $timetoprevpayroll = $daystoprevpayroll * $ONEDAYSECONDS;
  56. $bufferedstarttime = ($startDate - $timetoprevpayroll) - $offset; //most recent payroll start
  57.  
  58. $query = //multiarray of Clock-In/Clock-Out staff records: Where bufferedstartime<=time<=endz
  59. //should exclude records with no clock out timestamp
  60. $employees = array(); //multiarray of shifts by employee
  61. $numShifts = count($query[0]); //count of shift records
  62. //iterate through shifts
  63. for($i = 0; $i < $numShifts; $i++){
  64. $shift = $query[0][$i];
  65. $staffID = $shift['StaffID']; //employee identifier
  66.  
  67. //arrange shifts by employee
  68. if(!array_key_exists($staffID, $employees)) {
  69. $employees[$staffID] = array();
  70. }
  71. array_push($employees[$staffID], $shift);
  72. }
  73.  
  74. $totalRegHours = 0; //sum of all regular hours worked by all employees
  75. $totalRegDollars = 0; //sum of all regular dollars earned by all employees
  76. $totalOverHours = 0; //sum of all overtime hours worked by all employees
  77. $totalOverDollars = 0; //sum of all overtime dollars earned by all employees
  78.  
  79. //iterate through each employee
  80. foreach ($employees as $employee){
  81. $weekindex = 1; //current week in the iteration
  82. $dailyhourtotal = 0; //sum of hours worked in current day
  83. $weeklyhourtotal = 0; //sum of hours worked in current week
  84. $prevday = -1; //tracks day of current shift
  85.  
  86. //iterate through each shift of the current employee
  87. foreach($employee as $shift){
  88. $currentday = date('d', ($shift['ClockIn'])); //index of the shift's day
  89. $clockin = $shift['ClockIn']; //stamp of shift start
  90. $clockout = $shift['ClockOut']; //stamp of shift end
  91. $duration = $clockout - $clockin; //length of shift
  92.  
  93. $regRate = $shift['RegRate']; //employee's regular pay rate for the shift
  94. $overRate = $shift['OverRate']; //employees OT rate for the shift
  95.  
  96. $include = false; //include shift in labor % calculation
  97.  
  98. //new week, if considered
  99. if($weeklimit > 0 && $clockin > ($bufferedstarttime + ($ONEWEEKSECONDS * $weekindex))){
  100. $weeklyhourtotal = 0;
  101. $weekindex += 1;
  102. }
  103.  
  104. //new day, if considered
  105. if($daylimit > 0 && $currentday != $prevday){
  106. $dailyhourtotal = 0;
  107. }
  108.  
  109. //punches don't fall in search range, but employee was punched in
  110. if($clockin <= $startz && $clockout >= $endz){
  111. $duration = $duration - ($startz - $clockin)/3600 - ($clockout - $endz)/3600;
  112. $include = true;
  113. }
  114. //shift exceeds end date, truncate length
  115. else if($clockin >= $startz && $clockin <= $endz && $clockout >= $endz){
  116. $duration = $duration - ($clockout - $endz)/3600;
  117. $include = true;
  118. }
  119. //shift starts before search and enters range
  120. else if($clockin <= $startz && $clockout >= $startz && $clockout <= $endz){
  121. $duration = $duration - ($startz - $clockin)/3600;
  122. $include = true;
  123. }
  124.  
  125. /* Calculate hours and pay */
  126.  
  127. //CASE 1: Both daily and weekly will exceed overtime
  128. if($weeklimit > 0 && $daylimit > 0
  129. && $dailyhourtotal <= $daylimit && $dailyhourtotal + $duration > $daylimit
  130. && $weeklyhourtotal <= $weeklimit && $weeklyhourtotal + $duration > $weeklimit){
  131. $dailydif = ($duration + $dailyhourtotal) - $daylimit; //day amount that exceeds OT limit
  132. $weeklydif = ($duration + $weeklyhourtotal) - $weeklimit; //week amount that exceeds OT
  133. if($dailydif >= $weeklydif //day hours remainder is greater
  134. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  135. $totalRegHours += ($daylimit - $dailyhourtotal);
  136. $totalRegDollars += ($daylimit - $dailyhourtotal) * $regRate;
  137. $totalOverHours += $dailydif;
  138. $totalOverDollars += $dailydif * $overRate;
  139. }
  140. else if($weeklydif > $dailydif
  141. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  142. $totalRegHours += $weeklimit - $weeklyhourtotal;
  143. $totalRegDollars += ($weeklimit - $weeklyhourtotal) * $regRate;
  144. $totalOverHours += $weeklydif;
  145. $totalOverDollars += $weeklydif * $overRate;
  146. }
  147. }
  148. //CASE 2: Already in weekly overtime
  149. else if($weeklimit > 0 && $weeklyhourtotal > $weeklimit
  150. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  151. $totalOverHours += $duration;
  152. $totalOverDollars += $duration * $overRate;
  153. }
  154. //CASE 3: Already in daily overtime
  155. else if($daylimit > 0 && $dailyhourtotal > $daylimit
  156. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  157. $totalOverHours += $duration;
  158. $totalOverDollars += $duration * $overRate;
  159. }
  160. //CASE 4: Will exceed weekly overtime
  161. else if($weeklimit > 0 && ($weeklyhourtotal + $duration) > $weeklimit
  162. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  163. $totalRegHours += ($weeklimit - $weeklyhourtotal);
  164. $totalRegDollars += ($weeklimit - $weeklyhourtotal) * $regRate;
  165. $totalOverHours += ($weeklyhourtotal + $duration) - $weeklimit;
  166. $totalOverDollars += (($weeklyhourtotal + $duration) - $weeklimit) * $overRate;
  167. }
  168. //CASE 5: Will exceed daily overtime
  169. else if($daylimit > 0 && ($dailyhourtotal + $duration) > $daylimit
  170. && (($clockin >= $startz && $clockout <= $endz) || $include)){
  171. $totalRegHours += ($daylimit - $dailyhourtotal);
  172. $totalRegDollars += ($daylimit - $dailyhourtotal) * $regRate;
  173. $totalOverHours += ($dailyhourtotal + $duration) - $daylimit;
  174. $totalOverDollars += (($dailyhourtotal + $duration) - $daylimit) * $overRate;
  175. }
  176. //CASE 6: No overtime
  177. else if(($clockin >= $start - $offset && $clockout <= $endz) || $include){
  178. $totalRegHours += $duration;
  179. $totalRegDollars += $duration * $regRate;
  180. }
  181. $dailyhourtotal += $duration; //update daily total w/ shift length
  182. $weeklyhourtotal += $duration; //update weekly total w/ shift length
  183. $prevday = $currentday;
  184. } //finished single shift
  185. } //finished all of a single employee's shifts
  186.  
  187. $results = array();
  188. $results['RegularHours'] = $totalRegHours;
  189. $results['RegularDollars'] = $totalRegDollars;
  190. $results['OvertimeHours'] = $totalOverHours;
  191. $results['OvertimeDollars'] = $totalOverDollars;
  192. $results['TotalDollars'] = $totalRegDollars + $totalOverDollars;
  193.  
  194. $salesquery = //DB query for total sales: Select sum(amount) From salesTbl Where: startz<=time<=endz
  195. $totalSales = $salesquery[0];
  196. $results['TotalSales'] = $totalSales;
  197. if($totalSales == 0){ //no sales; labor is 100% of cost
  198. $results['LaborPercent'] = 100;
  199. }else{
  200. $results['LaborPercent'] = (($totalRegDollars + $totalOverDollars) / $totalSales) * 100;
  201. }
  202. $this->response($results);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement