reportModel.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. $note = $result['note'];
  102. //Calculate Time Worked
  103. switch($result['lessTime'])
  104. {
  105. case 60:
  106. $lessTime = 1;
  107. break;
  108. case 30:
  109. $lessTime = 0.5;
  110. break;
  111. case 15:
  112. $lessTime = 0.25;
  113. break;
  114. default:
  115. $lessTime = 0;
  116. }
  117. //Total Worked Time
  118. $dateTime1 = new DateTime($roundedInTime);
  119. $dateTime1->setDate(date('Y',strtotime($inTimeDate)), date('m',strtotime($inTimeDate)), date('d',strtotime($inTimeDate)));
  120. $dateTime2 = new DateTime($roundedOutTime);
  121. $dateTime2->setDate(date('Y',strtotime($outTimeDate)), date('m',strtotime($outTimeDate)), date('d',strtotime($outTimeDate)));
  122. $interval = $dateTime1->diff($dateTime2);
  123. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  124. if($timeWorked !== 0)
  125. {
  126. $timeWorked = $timeWorked;
  127. }
  128. else
  129. {
  130. $timeWorked = 0;
  131. }
  132. //Get Code Information
  133. $code = new codeModel();
  134. $codeId = $result['codeId'];
  135. $code->load($result['codeId']);
  136. $codeName = $code->getName();
  137. $data['date'] = date('Y-m-d', $inTimeRaw);
  138. $data['inTime'] = $inTimeRaw;
  139. $data['outTime'] = $outTimeRaw;
  140. $data['lessTime'] = $lessTime;
  141. $data['timeWorked'] = $timeWorked;
  142. $data['code'] = $codeName;
  143. $data['timestamp'] = $timestamp;
  144. $data['note'] = $note;
  145. //Get the user of the entry.
  146. $entry = new timeEntryModel($id);
  147. if($entry->validated($id,$result['userId']))
  148. {
  149. $data['validated'] = 0;
  150. }
  151. else
  152. {
  153. $data['validated'] = 1;
  154. }
  155. return $data;
  156. }
  157. function nearestQuarterHour($time)
  158. {
  159. //$time = strtotime($time);
  160. $round = 15*60;
  161. $rounded = round($time/$round)*$round;
  162. return date("g:i A", $rounded);
  163. }
  164. function timeToDecimal($time)
  165. {
  166. $timeArr = explode(':', $time);
  167. $hours = $timeArr[0]*1;
  168. $minutes = $timeArr[1]/60;
  169. $dec = $hours + $minutes;
  170. if($dec > 0)
  171. {
  172. return round($dec,2);
  173. }
  174. else
  175. {
  176. return 0;
  177. }
  178. }
  179. function weekly()
  180. {
  181. }
  182. function payPeriodTotals($year, $month)
  183. {
  184. //Get all users
  185. $users = new userModel();
  186. $accounts = $users->listAll();
  187. $data = array();
  188. $date = new DateTime();
  189. $date->setTime(0,0,0);
  190. $date->setDate($year,$month,26);
  191. $date->modify('-1 month');
  192. $startDate = $date->format('U');
  193. $date->modify('+1 month -1 day');
  194. $date->setTime(23,59,59);
  195. $endDate = $date->format('U');
  196. foreach($accounts as $account)
  197. {
  198. $userId = $account['id'];
  199. $userName = $account['lastName'].", ".$account['firstName'];
  200. $sql = "
  201. SELECT * FROM timeEntries WHERE inTime >= '".$this->db->real_escape_string($startDate)."' AND inTime <= '".$this->db->real_escape_string($endDate)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY inTime ASC;
  202. ";
  203. $query = $this->db->query($sql);
  204. $data2 = array();
  205. $setDate = 0;
  206. $timeWorked = 0;
  207. while($result = $query->fetch_assoc())
  208. {
  209. $day = new DateTime();
  210. $day->setTimestamp($result['inTime']);
  211. $date = $day->format('Y-m-d');
  212. $entry = $this->calculateEntry($result['id']);
  213. if($date == $setDate)
  214. {
  215. $timeWorked = $timeWorked + $entry['timeWorked'];
  216. }
  217. else
  218. {
  219. $timeWorked = $entry['timeWorked'];
  220. }
  221. $data2[$date] = $timeWorked;
  222. $setDate = $date;
  223. }
  224. $data[$userName] = $data2;
  225. }
  226. return $data;
  227. }
  228. function payroll($year, $month)
  229. {
  230. $users = new userModel();
  231. $accounts = $users->listAll();
  232. $data = array();
  233. $date = new DateTime();
  234. $date->setTime(0,0,0);
  235. $date->setDate($year,$month,26);
  236. $date->modify('-1 month');
  237. $startDate = $date->format('U');
  238. $date->modify('+1 month -1 day');
  239. $date->setTime(23,59,59);
  240. $endDate = $date->format('U');
  241. foreach($accounts as $account)
  242. {
  243. $userId = $account['id'];
  244. $userName = $account['lastName'] . ", " . $account['firstName'];
  245. $sql = "
  246. SELECT * FROM timeEntries WHERE inTime >= '" . $this->db->real_escape_string($startDate) . "' AND inTime <= '" . $this->db->real_escape_string($endDate) . "' AND userId = '" . $this->db->real_escape_string($userId) . "' ORDER BY inTime ASC;
  247. ";
  248. $query = $this->db->query($sql);
  249. $data2 = array();
  250. $setCode = 0;
  251. $timeWorked = 0;
  252. while($result = $query->fetch_assoc())
  253. {
  254. $entry = $this->calculateEntry($result['id']);
  255. $code = $entry['code'];
  256. if($code == $setCode)
  257. {
  258. $timeWorked = $timeWorked + $entry['timeWorked'];
  259. }
  260. else
  261. {
  262. $timeWorked = $entry['timeWorked'];
  263. }
  264. $data2[$code] = $timeWorked;
  265. $setCode = $code;
  266. }
  267. $data[$userName] = $data2;
  268. }
  269. return $data;
  270. }
  271. }