reportModel.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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, $inactive = null)
  21. {
  22. $this->db = Staple_DB::get();
  23. if($inactive != null)
  24. {
  25. $staffIds = $this->getStaffIds(1);
  26. }
  27. else
  28. {
  29. $staffIds = $this->getStaffIds();
  30. }
  31. $data = array();
  32. if(count($staffIds) > 0)
  33. {
  34. foreach($staffIds as $key => $value)
  35. {
  36. $data[$value] = $this->getTimesheet($key, $year, $month);
  37. $data[$value]['id'] = $key;
  38. }
  39. }
  40. $this->timesheets = $data;
  41. }
  42. function getStaffIds($inactive = null)
  43. {
  44. $auth = Staple_Auth::get();
  45. $user = new userModel($auth->getAuthId());
  46. $userId = $user->getId();
  47. $authLevel = $user->getAuthLevel();
  48. $data = array();
  49. if($authLevel >= 900)
  50. {
  51. if($inactive == 1)
  52. {
  53. $sql = "
  54. SELECT id, firstName, lastName FROM accounts WHERE status = 0 ORDER BY lastName ASC
  55. ";
  56. }
  57. else
  58. {
  59. $sql = "
  60. SELECT id, firstName, lastName FROM accounts WHERE status = 1 ORDER BY lastName ASC
  61. ";
  62. }
  63. }
  64. else
  65. {
  66. if($inactive == 1)
  67. {
  68. $sql = "
  69. SELECT id, firstName, lastName FROM accounts WHERE status = 0 AND supervisorId = '" . $this->db->real_escape_string($userId) . "' ORDER BY lastName ASC
  70. ";
  71. }
  72. else
  73. {
  74. $sql = "
  75. SELECT id, firstName, lastName FROM accounts WHERE status = 1 AND supervisorId = '" . $this->db->real_escape_string($userId) . "' ORDER BY lastName ASC
  76. ";
  77. }
  78. }
  79. $query = $this->db->query($sql);
  80. while($result = $query->fetch_assoc())
  81. {
  82. $data[$result['id']] = $result['lastName'].", ".$result['firstName'];
  83. }
  84. return $data;
  85. }
  86. function getTimesheet($userId, $year, $month)
  87. {
  88. $currentDate = new DateTime();
  89. $currentDate->setDate($year, $month, 1);
  90. $currentYear = $currentDate->format('Y');
  91. $currentMonth = $currentDate->format('m');
  92. $currentMonthText = $currentDate->format('F');
  93. $startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
  94. $startDateTimeString = strtotime($startDate);
  95. $currentDate->setDate($year, $month, 1);
  96. $endDate = $currentDate->modify('+25 day')->format('Y-m-d');
  97. $endDateTimeString = strtotime($endDate);
  98. $sql = "
  99. SELECT id FROM timeEntries WHERE inTime > $startDateTimeString AND inTime < $endDateTimeString AND userId = $userId ORDER BY inTime ASC;
  100. ";
  101. $data = array();
  102. $query = $this->db->query($sql);
  103. while($result = $query->fetch_assoc())
  104. {
  105. $data[$result['id']] = $this->calculateEntry($result['id']);
  106. }
  107. return $data;
  108. }
  109. function calculateEntry($id)
  110. {
  111. $sql = "
  112. SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
  113. ";
  114. $query = $this->db->query($sql);
  115. $result = $query->fetch_assoc();
  116. //Set inTime
  117. $inTime = new DateTime();
  118. $inTime->setTimestamp($result['inTime']);
  119. $roundedInTime = $this->nearestQuarterHour($result['inTime']);
  120. $inTimeRaw = $result['inTime'];
  121. $inTimeDate = date("Y-m-d", $result['inTime']);
  122. //Out Time
  123. $outTime = new DateTime();
  124. $outTime->setTimestamp($result['outTime']);
  125. $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
  126. $outTimeRaw = $result['outTime'];
  127. $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
  128. $outTimeDate = date("Y-m-d", $result['outTime']);
  129. $lessTime = $result['lessTime'];
  130. $timestamp = $result['timestamp'];
  131. $note = $result['note'];
  132. //Calculate Time Worked
  133. switch($result['lessTime'])
  134. {
  135. case 60:
  136. $lessTime = 1;
  137. break;
  138. case 30:
  139. $lessTime = 0.5;
  140. break;
  141. case 15:
  142. $lessTime = 0.25;
  143. break;
  144. default:
  145. $lessTime = 0;
  146. }
  147. //Total Worked Time
  148. $dateTime1 = new DateTime($roundedInTime);
  149. $dateTime1->setDate(date('Y',strtotime($inTimeDate)), date('m',strtotime($inTimeDate)), date('d',strtotime($inTimeDate)));
  150. $dateTime2 = new DateTime($roundedOutTime);
  151. $dateTime2->setDate(date('Y',strtotime($outTimeDate)), date('m',strtotime($outTimeDate)), date('d',strtotime($outTimeDate)));
  152. $interval = $dateTime1->diff($dateTime2);
  153. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  154. if($timeWorked !== 0)
  155. {
  156. $timeWorked = $timeWorked;
  157. }
  158. else
  159. {
  160. $timeWorked = 0;
  161. }
  162. //Get Code Information
  163. $code = new codeModel();
  164. $codeId = $result['codeId'];
  165. $code->load($result['codeId']);
  166. $codeName = $code->getName();
  167. $data['date'] = date('Y-m-d', $inTimeRaw);
  168. $data['inTime'] = $inTimeRaw;
  169. $data['outTime'] = $outTimeRaw;
  170. $data['lessTime'] = $lessTime;
  171. $data['timeWorked'] = $timeWorked;
  172. $data['code'] = $codeName;
  173. $data['timestamp'] = $timestamp;
  174. $data['note'] = $note;
  175. //Get the user of the entry.
  176. $entry = new timeEntryModel($id);
  177. if($entry->validated($id,$result['userId']))
  178. {
  179. $data['validated'] = 0;
  180. }
  181. else
  182. {
  183. $data['validated'] = 1;
  184. }
  185. return $data;
  186. }
  187. function nearestQuarterHour($time)
  188. {
  189. //$time = strtotime($time);
  190. $round = 15*60;
  191. $rounded = round($time/$round)*$round;
  192. return date("g:i A", $rounded);
  193. }
  194. function timeToDecimal($time)
  195. {
  196. $timeArr = explode(':', $time);
  197. $hours = $timeArr[0]*1;
  198. $minutes = $timeArr[1]/60;
  199. $dec = $hours + $minutes;
  200. if($dec > 0)
  201. {
  202. return round($dec,2);
  203. }
  204. else
  205. {
  206. return 0;
  207. }
  208. }
  209. function weekly()
  210. {
  211. }
  212. function payPeriodTotals($year, $month)
  213. {
  214. //Get all users
  215. $users = new userModel();
  216. $accounts = $users->listAll();
  217. $data = array();
  218. $date = new DateTime();
  219. $date->setTime(0,0,0);
  220. $date->setDate($year,$month,26);
  221. $date->modify('-1 month');
  222. $startDate = $date->format('U');
  223. $date->modify('+1 month -1 day');
  224. $date->setTime(23,59,59);
  225. $endDate = $date->format('U');
  226. foreach($accounts as $account)
  227. {
  228. $userId = $account['id'];
  229. $userName = $account['lastName'].", ".$account['firstName'];
  230. $sql = "
  231. 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;
  232. ";
  233. $query = $this->db->query($sql);
  234. $data2 = array();
  235. $setDate = 0;
  236. $timeWorked = 0;
  237. while($result = $query->fetch_assoc())
  238. {
  239. $day = new DateTime();
  240. $day->setTimestamp($result['inTime']);
  241. $date = $day->format('Y-m-d');
  242. $entry = $this->calculateEntry($result['id']);
  243. if($date == $setDate)
  244. {
  245. $timeWorked = $timeWorked + $entry['timeWorked'];
  246. }
  247. else
  248. {
  249. $timeWorked = $entry['timeWorked'];
  250. }
  251. $data2[$date] = $timeWorked;
  252. $setDate = $date;
  253. }
  254. $data[$userName] = $data2;
  255. }
  256. return $data;
  257. }
  258. function payroll($year, $month)
  259. {
  260. $users = new userModel();
  261. $accounts = $users->listAll();
  262. $data = array();
  263. $date = new DateTime();
  264. $date->setTime(0,0,0);
  265. $date->setDate($year,$month,26);
  266. $date->modify('-1 month');
  267. $startDate = $date->format('U');
  268. $date->modify('+1 month -1 day');
  269. $date->setTime(23,59,59);
  270. $endDate = $date->format('U');
  271. foreach($accounts as $account)
  272. {
  273. $userId = $account['id'];
  274. $userName = $account['lastName'] . ", " . $account['firstName'];
  275. $sql = "
  276. 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;
  277. ";
  278. $query = $this->db->query($sql);
  279. $data2 = array();
  280. $setCode = 0;
  281. $timeWorked = 0;
  282. while($result = $query->fetch_assoc())
  283. {
  284. $entry = $this->calculateEntry($result['id']);
  285. $code = $entry['code'];
  286. if($code == $setCode)
  287. {
  288. $timeWorked = $timeWorked + $entry['timeWorked'];
  289. }
  290. else
  291. {
  292. $timeWorked = $entry['timeWorked'];
  293. }
  294. $data2[$code] = $timeWorked;
  295. $setCode = $code;
  296. }
  297. $data[$userName] = $data2;
  298. }
  299. return $data;
  300. }
  301. }