timesheetModel.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. class timesheetModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $userId;
  7. private $date;
  8. private $inTime;
  9. private $outTime;
  10. private $lessTime;
  11. private $codeId;
  12. /**
  13. * @return mixed
  14. */
  15. public function getId()
  16. {
  17. return $this->id;
  18. }
  19. /**
  20. * @param mixed $id
  21. */
  22. public function setId($id)
  23. {
  24. $this->id = $id;
  25. }
  26. /**
  27. * @return mixed
  28. */
  29. public function getUserId()
  30. {
  31. return $this->userId;
  32. }
  33. /**
  34. * @param mixed $userId
  35. */
  36. public function setUserId($userId)
  37. {
  38. $this->userId = $userId;
  39. }
  40. /**
  41. * @return mixed
  42. */
  43. public function getDate()
  44. {
  45. return $this->date;
  46. }
  47. /**
  48. * @param mixed $date
  49. */
  50. public function setDate($date)
  51. {
  52. $this->date = $date;
  53. }
  54. /**
  55. * @return mixed
  56. */
  57. public function getInTime()
  58. {
  59. return $this->inTime;
  60. }
  61. /**
  62. * @param mixed $inTime
  63. */
  64. public function setInTime($inTime)
  65. {
  66. $this->inTime = $inTime;
  67. }
  68. /**
  69. * @return mixed
  70. */
  71. public function getOutTime()
  72. {
  73. return $this->outTime;
  74. }
  75. /**
  76. * @param mixed $outTime
  77. */
  78. public function setOutTime($outTime)
  79. {
  80. $this->outTime = $outTime;
  81. }
  82. /**
  83. * @return mixed
  84. */
  85. public function getLessTime()
  86. {
  87. return $this->lessTime;
  88. }
  89. /**
  90. * @param mixed $lessTime
  91. */
  92. public function setLessTime($lessTime)
  93. {
  94. $this->lessTime = $lessTime;
  95. }
  96. /**
  97. * @return mixed
  98. */
  99. public function getCodeId()
  100. {
  101. return $this->codeId;
  102. }
  103. /**
  104. * @param mixed $codeId
  105. */
  106. public function setCodeId($codeId)
  107. {
  108. $this->codeId = $codeId;
  109. }
  110. function __construct()
  111. {
  112. $this->db = Staple_DB::get();
  113. }
  114. function load()
  115. {
  116. $authId = Staple_Auth::get()->getAuthId();
  117. //Get User ID.
  118. if(isset($authId))
  119. {
  120. $sql = "SELECT id FROM accounts WHERE username = '".$this->db->real_escape_string($authId)."'";
  121. if($this->db->query($sql)->fetch_row() > 0)
  122. {
  123. $query = $this->db->query($sql);
  124. $result = $query->fetch_assoc();
  125. $this->setUserId($result['id']);
  126. }
  127. if(isset($this->userId))
  128. {
  129. $sql = "SELECT * FROM timeEntries WHERE userId = '" . $this->db->real_escape_string($this->userId) . "' ORDER BY inTime ASC";
  130. if ($this->db->query($sql)->fetch_row() > 0)
  131. {
  132. $query = $this->db->query($sql);
  133. $data = array();
  134. while ($row = $query->fetch_assoc())
  135. {
  136. $data[] = $row;
  137. }
  138. foreach($data as $entry)
  139. {
  140. $code = new codeModel();
  141. $code->load($entry['codeId']);
  142. $codeName = $code->getName();
  143. $data2['id'] = $entry['id'];
  144. //Set Date Object
  145. $date = new DateTime();
  146. $date->setTimestamp($entry['inTime']);
  147. //Date
  148. $data2['date']['ymd'] = $date->format("Y-m-d");
  149. //Formatted Date
  150. $data2['date']['formatted'] = $date->format("F jS, Y");
  151. //Day
  152. $data2['date']['dayOfWeek'] = $date->format("l");
  153. //Day Abbreviated
  154. $data2['date']['dayShort'] = $date->format("D.");
  155. //MonthYear
  156. $data2['date']['my'] = $date->format("m/y");
  157. //MonthDay
  158. $data2['date']['md'] = $date->format("m/d");
  159. //DateMonthYear
  160. $data2['date']['mdy'] = $date->format("m/d/y");
  161. //In Time
  162. $data2['rawInTime'] = $date->format("g:i A");
  163. $data2['roundedInTime'] = $this->nearestQuarterHour($date->format("g:i A"));
  164. //Out Time
  165. $date->setTimestamp($entry['outTime']);
  166. $data2['rawOutTime'] = $date->format("g:i A");
  167. $data2['roundedOutTime'] = $this->nearestQuarterHour($date->format("g:i A"));
  168. //Less Time
  169. $data2['lessTime'] = $entry['lessTime'];
  170. switch($entry['lessTime'])
  171. {
  172. case 60:
  173. $lessTime = 1;
  174. break;
  175. case 30:
  176. $lessTime = 0.5;
  177. break;
  178. case 15:
  179. $lessTime = 0.25;
  180. break;
  181. default:
  182. $lessTime = 0;
  183. }
  184. //Total Worked Time
  185. $dateTime1 = new DateTime($data2['roundedInTime']);
  186. $dateTime2 = new DateTime($data2['roundedOutTime']);
  187. $interval = $dateTime1->diff($dateTime2);
  188. $data2['timeWorked'] = $interval->h.":".$interval->i;
  189. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  190. if($timeWorked > 0)
  191. {
  192. $data2['timeWorkedDec'] = $timeWorked;
  193. }
  194. else
  195. {
  196. $data2['timeWorkedDec'] = 0;
  197. }
  198. $data2['code'] = $codeName;
  199. $data3[] = $data2;
  200. }
  201. return $data3;
  202. }
  203. else
  204. {
  205. return array();
  206. }
  207. }
  208. }
  209. }
  210. private function nearestQuarterHour($time)
  211. {
  212. $time = strtotime($time);
  213. $round = 15*60;
  214. $rounded = round($time/$round)*$round;
  215. return date("g:i A", $rounded);
  216. }
  217. private function timeToDecimal($time)
  218. {
  219. $timeArr = explode(':', $time);
  220. $hours = $timeArr[0]*1;
  221. $minutes = $timeArr[1]/60;
  222. $dec = $hours + $minutes;
  223. if($dec > 0)
  224. {
  225. return round($dec,2);
  226. }
  227. else
  228. {
  229. return 0;
  230. }
  231. }
  232. function save()
  233. {
  234. $id = $this->getId();
  235. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  236. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  237. if($id == NULL)
  238. {
  239. //Insert new item
  240. $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId)
  241. VALUES (
  242. '".$this->db->real_escape_string($this->getUserId())."',
  243. '".$this->db->real_escape_string($inTime)."',
  244. '".$this->db->real_escape_string($outTime)."',
  245. '".$this->db->real_escape_string($this->getLessTime())."',
  246. '".$this->db->real_escape_string($this->getCodeId())."'
  247. )";
  248. }
  249. else
  250. {
  251. //Update item
  252. $sql = "UPDATE timeEntries SET
  253. userId='".$this->db->real_escape_string($this->getUserId())."',
  254. inTime='".$this->db->real_escape_string($inTime)."',
  255. outTime='".$this->db->real_escape_string($outTime)."',
  256. lessTime='".$this->db->real_escape_string($this->getLessTime())."',
  257. codeId='".$this->db->real_escape_string($this->getCodeId())."'
  258. WHERE id='".$this->db->real_escape_string($id)."'
  259. ";
  260. }
  261. $query = $this->db->query($sql);
  262. if($query === true)
  263. {
  264. return true;
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. }
  271. }
  272. ?>