reportModel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class reportModel extends Staple_Model
  3. {
  4. private $db;
  5. private $timesheets;
  6. function __construct($year, $month)
  7. {
  8. $this->db = Staple_DB::get();
  9. $staffIds = $this->getStaffIds();
  10. $data = array();
  11. foreach($staffIds as $key => $value)
  12. {
  13. $data[$value] = $this->getTimesheet($key, $year, $month);
  14. }
  15. echo "<pre>";
  16. print_r($data);
  17. echo "</pre>";
  18. $this->timesheets = $data;
  19. }
  20. function getStaffIds()
  21. {
  22. $auth = Staple_Auth::get();
  23. $user = new userModel($auth->getAuthId());
  24. $userId = $user->getId();
  25. $authLevel = $user->getAuthLevel();
  26. if($authLevel >= 900)
  27. {
  28. $sql = "
  29. SELECT id, firstName, lastName FROM accounts WHERE 1 ORDER BY lastName ASC
  30. ";
  31. }
  32. else
  33. {
  34. $sql = "
  35. SELECT id, firstName, lastName FROM accounts WHERE supervisorId = '".$this->db->real_escape_string($userId)."' ORDER BY lastName ASC
  36. ";
  37. }
  38. $query = $this->db->query($sql);
  39. while($result = $query->fetch_assoc())
  40. {
  41. $data[$result['id']] = $result['lastName'].", ".$result['firstName'];
  42. }
  43. return $data;
  44. }
  45. function getTimesheet($userId, $year, $month)
  46. {
  47. $currentDate = new DateTime();
  48. $currentDate->setDate($year, $month, 1);
  49. $currentYear = $currentDate->format('Y');
  50. $currentMonth = $currentDate->format('m');
  51. $currentMonthText = $currentDate->format('F');
  52. $startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
  53. $startDateTimeString = strtotime($startDate);
  54. $currentDate->setDate($year, $month, 1);
  55. $endDate = $currentDate->modify('+25 day')->format('Y-m-d');
  56. $endDateTimeString = strtotime($endDate);
  57. $sql = "
  58. SELECT id FROM timeEntries WHERE inTime > $startDateTimeString AND inTime < $endDateTimeString AND userId = $userId ORDER BY inTime ASC;
  59. ";
  60. $data = array();
  61. $query = $this->db->query($sql);
  62. while($result = $query->fetch_assoc())
  63. {
  64. $data[] = $this->calculateEntry($result['id']);
  65. }
  66. return $data;
  67. }
  68. function calculateEntry($id)
  69. {
  70. $sql = "
  71. SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
  72. ";
  73. $query = $this->db->query($sql);
  74. $result = $query->fetch_assoc();
  75. print_r($result);
  76. //Set inTime
  77. $inTime = new DateTime();
  78. $inTime->setTimestamp($result['inTime']);
  79. $roundedInTime = $this->nearestQuarterHour($result['inTime']);
  80. $inTimeRaw = $result['inTime'];
  81. $inTimeDate = date("Y-m-d", $result['inTime']);
  82. //Out Time
  83. $outTime = new DateTime();
  84. $outTime->setTimestamp($result['outTime']);
  85. $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
  86. $outTimeRaw = $result['outTime'];
  87. $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
  88. $outTimeDate = date("Y-m-d", $result['outTime']);
  89. $lessTime = $result['lessTime'];
  90. //Calculate Time Worked
  91. switch($result['lessTime'])
  92. {
  93. case 60:
  94. $lessTime = 1;
  95. break;
  96. case 30:
  97. $lessTime = 0.5;
  98. break;
  99. case 15:
  100. $lessTime = 0.25;
  101. break;
  102. default:
  103. $lessTime = 0;
  104. }
  105. //Total Worked Time
  106. $dateTime1 = new DateTime($roundedInTime);
  107. $dateTime1->setDate(date('Y',strtotime($inTimeDate)), date('m',strtotime($inTimeDate)), date('d',strtotime($inTimeDate)));
  108. $dateTime2 = new DateTime($roundedOutTime);
  109. $dateTime2->setDate(date('Y',strtotime($outTimeDate)), date('m',strtotime($outTimeDate)), date('d',strtotime($outTimeDate)));
  110. $interval = $dateTime1->diff($dateTime2);
  111. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  112. if($timeWorked !== 0)
  113. {
  114. $timeWorked = $timeWorked;
  115. }
  116. else
  117. {
  118. $timeWorked = 0;
  119. }
  120. //Get Code Information
  121. $code = new codeModel();
  122. $codeId = $result['codeId'];
  123. $code->load($result['codeId']);
  124. $codeName = $code->getName();
  125. $data['date'] = date('Y-m-d', $inTimeRaw);
  126. $data['inTime'] = $inTimeRaw;
  127. $data['outTime'] = $outTimeRaw;
  128. $data['lessTime'] = $lessTime;
  129. $data['timeWorked'] = $timeWorked;
  130. $data['code'] = $codeName;
  131. return $data;
  132. }
  133. function nearestQuarterHour($time)
  134. {
  135. //$time = strtotime($time);
  136. $round = 15*60;
  137. $rounded = round($time/$round)*$round;
  138. return date("g:i A", $rounded);
  139. }
  140. function timeToDecimal($time)
  141. {
  142. $timeArr = explode(':', $time);
  143. $hours = $timeArr[0]*1;
  144. $minutes = $timeArr[1]/60;
  145. $dec = $hours + $minutes;
  146. if($dec > 0)
  147. {
  148. return round($dec,2);
  149. }
  150. else
  151. {
  152. return 0;
  153. }
  154. }
  155. }