reportModel.php 9.9 KB

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