reportModel.php 5.6 KB

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