reportModel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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[$result['id']] = $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. $timestamp = $result['timestamp'];
  101. //Calculate Time Worked
  102. switch($result['lessTime'])
  103. {
  104. case 60:
  105. $lessTime = 1;
  106. break;
  107. case 30:
  108. $lessTime = 0.5;
  109. break;
  110. case 15:
  111. $lessTime = 0.25;
  112. break;
  113. default:
  114. $lessTime = 0;
  115. }
  116. //Total Worked Time
  117. $dateTime1 = new DateTime($roundedInTime);
  118. $dateTime1->setDate(date('Y',strtotime($inTimeDate)), date('m',strtotime($inTimeDate)), date('d',strtotime($inTimeDate)));
  119. $dateTime2 = new DateTime($roundedOutTime);
  120. $dateTime2->setDate(date('Y',strtotime($outTimeDate)), date('m',strtotime($outTimeDate)), date('d',strtotime($outTimeDate)));
  121. $interval = $dateTime1->diff($dateTime2);
  122. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  123. if($timeWorked !== 0)
  124. {
  125. $timeWorked = $timeWorked;
  126. }
  127. else
  128. {
  129. $timeWorked = 0;
  130. }
  131. //Get Code Information
  132. $code = new codeModel();
  133. $codeId = $result['codeId'];
  134. $code->load($result['codeId']);
  135. $codeName = $code->getName();
  136. $data['date'] = date('Y-m-d', $inTimeRaw);
  137. $data['inTime'] = $inTimeRaw;
  138. $data['outTime'] = $outTimeRaw;
  139. $data['lessTime'] = $lessTime;
  140. $data['timeWorked'] = $timeWorked;
  141. $data['code'] = $codeName;
  142. $data['timestamp'] = $timestamp;
  143. //Get the user of the entry.
  144. $entry = new timeEntryModel($id);
  145. if($entry->validated($id,$result['userId']))
  146. {
  147. $data['validated'] = 0;
  148. }
  149. else
  150. {
  151. $data['validated'] = 1;
  152. }
  153. return $data;
  154. }
  155. function nearestQuarterHour($time)
  156. {
  157. //$time = strtotime($time);
  158. $round = 15*60;
  159. $rounded = round($time/$round)*$round;
  160. return date("g:i A", $rounded);
  161. }
  162. function timeToDecimal($time)
  163. {
  164. $timeArr = explode(':', $time);
  165. $hours = $timeArr[0]*1;
  166. $minutes = $timeArr[1]/60;
  167. $dec = $hours + $minutes;
  168. if($dec > 0)
  169. {
  170. return round($dec,2);
  171. }
  172. else
  173. {
  174. return 0;
  175. }
  176. }
  177. function weekly()
  178. {
  179. }
  180. }