timeEntryModel.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. class timeEntryModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $date;
  7. private $inTime;
  8. private $inTimeRaw;
  9. private $roundedInTime;
  10. private $outTime;
  11. private $outTimeRaw;
  12. private $roundedOutTime;
  13. private $lessTime;
  14. private $codeId;
  15. private $codeName;
  16. private $timeWorked;
  17. /**
  18. * @return mixed
  19. */
  20. public function getId()
  21. {
  22. return $this->id;
  23. }
  24. /**
  25. * @param mixed $id
  26. */
  27. public function setId($id)
  28. {
  29. $this->id = $id;
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function getDate()
  35. {
  36. return $this->date;
  37. }
  38. /**
  39. * @param mixed $date
  40. */
  41. public function setDate($date)
  42. {
  43. $this->date = $date;
  44. }
  45. /**
  46. * @return mixed
  47. */
  48. public function getInTime()
  49. {
  50. return $this->inTime;
  51. }
  52. /**
  53. * @param mixed $inTime
  54. */
  55. public function setInTime($inTime)
  56. {
  57. $this->inTime = $inTime;
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getOutTime()
  63. {
  64. return $this->outTime;
  65. }
  66. /**
  67. * @param mixed $outTime
  68. */
  69. public function setOutTime($outTime)
  70. {
  71. $this->outTime = $outTime;
  72. }
  73. /**
  74. * @return mixed
  75. */
  76. public function getInTimeRaw()
  77. {
  78. return $this->inTimeRaw;
  79. }
  80. /**
  81. * @param mixed $inTimeRaw
  82. */
  83. public function setInTimeRaw($inTimeRaw)
  84. {
  85. $this->inTimeRaw = $inTimeRaw;
  86. }
  87. /**
  88. * @return mixed
  89. */
  90. public function getOutTimeRaw()
  91. {
  92. return $this->outTimeRaw;
  93. }
  94. /**
  95. * @param mixed $outTimeRaw
  96. */
  97. public function setOutTimeRaw($outTimeRaw)
  98. {
  99. $this->outTimeRaw = $outTimeRaw;
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function getLessTime()
  105. {
  106. return $this->lessTime;
  107. }
  108. /**
  109. * @param mixed $lessTime
  110. */
  111. public function setLessTime($lessTime)
  112. {
  113. $this->lessTime = $lessTime;
  114. }
  115. /**
  116. * @return mixed
  117. */
  118. public function getCodeId()
  119. {
  120. return $this->codeId;
  121. }
  122. /**
  123. * @param mixed $codeId
  124. */
  125. public function setCodeId($codeId)
  126. {
  127. $this->codeId = $codeId;
  128. }
  129. /**
  130. * @return mixed
  131. */
  132. public function getCodeName()
  133. {
  134. return $this->codeName;
  135. }
  136. /**
  137. * @param mixed $codeName
  138. */
  139. public function setCodeName($codeName)
  140. {
  141. $this->codeName = $codeName;
  142. }
  143. /**
  144. * @return mixed
  145. */
  146. public function getTimeWorked()
  147. {
  148. return $this->timeWorked;
  149. }
  150. /**
  151. * @param mixed $timeWorked
  152. */
  153. public function setTimeWorked($timeWorked)
  154. {
  155. $this->timeWorked = $timeWorked;
  156. }
  157. /**
  158. * @return mixed
  159. */
  160. public function getRoundedInTime()
  161. {
  162. return $this->roundedInTime;
  163. }
  164. /**
  165. * @param mixed $roundedInTime
  166. */
  167. public function setRoundedInTime($roundedInTime)
  168. {
  169. $this->roundedInTime = $roundedInTime;
  170. }
  171. /**
  172. * @return mixed
  173. */
  174. public function getRoundedOutTime()
  175. {
  176. return $this->roundedOutTime;
  177. }
  178. /**
  179. * @param mixed $roundedOutTime
  180. */
  181. public function setRoundedOutTime($roundedOutTime)
  182. {
  183. $this->roundedOutTime = $roundedOutTime;
  184. }
  185. function __construct($id = null)
  186. {
  187. $this->db = Staple_DB::get();
  188. if($id !== null)
  189. {
  190. $sql = "SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."'";
  191. if($this->db->query($sql)->fetch_row() > 0)
  192. {
  193. $query = $this->db->query($sql);
  194. $result = $query->fetch_assoc();
  195. //Set ID and Date
  196. $this->setId($result['id']);
  197. $this->setDate(date("m/d/Y",$result['inTime']));
  198. //Set inTime
  199. $inTime = new DateTime();
  200. $inTime->setTimestamp($result['inTime']);
  201. $this->setInTime($inTime->format('h:i A'));
  202. $this->setInTimeRaw($result['inTime']);
  203. $this->setRoundedInTime($this->nearestQuarterHour($result['inTime']));
  204. //Out Time
  205. $outTime = new DateTime();
  206. $outTime->setTimestamp($result['outTime']);
  207. $this->setOutTime($outTime->format('h:i A'));
  208. $this->setOutTimeRaw($result['outTime']);
  209. $this->setRoundedOutTime($this->nearestQuarterHour($result['outTime']));
  210. $this->setLessTime($result['lessTime']);
  211. //Calculate Time Worked
  212. switch($result['lessTime'])
  213. {
  214. case 60:
  215. $lessTime = 1;
  216. break;
  217. case 30:
  218. $lessTime = 0.5;
  219. break;
  220. case 15:
  221. $lessTime = 0.25;
  222. break;
  223. default:
  224. $lessTime = 0;
  225. }
  226. //Total Worked Time
  227. $dateTime1 = new DateTime($this->roundedInTime);
  228. $dateTime2 = new DateTime($this->roundedOutTime);
  229. $interval = $dateTime1->diff($dateTime2);
  230. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  231. if($timeWorked !== 0)
  232. {
  233. $this->setTimeWorked($timeWorked);
  234. }
  235. else
  236. {
  237. $this->setTimeWorked(0);
  238. }
  239. //Get Code Information
  240. $code = new codeModel();
  241. $this->setCodeId($result['codeId']);
  242. $code->load($result['codeId']);
  243. $this->setCodeName($code->getName());
  244. return true;
  245. }
  246. }
  247. }
  248. function remove($id)
  249. {
  250. $this->db = Staple_DB::get();
  251. if($id !== null)
  252. {
  253. $auth = Staple_Auth::get();
  254. $user = new userModel($auth->getAuthId());
  255. $userId = $user->getId();
  256. $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
  257. if($this->db->query($sql))
  258. {
  259. return true;
  260. }
  261. }
  262. }
  263. function save()
  264. {
  265. $this->db = Staple_DB::get();
  266. $auth = Staple_Auth::get();
  267. $user = new userModel($auth->getAuthId());
  268. $userId = $user->getId();
  269. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  270. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  271. if($this->getId() == NULL)
  272. {
  273. //Insert new item
  274. $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId)
  275. VALUES (
  276. '".$this->db->real_escape_string($userId)."',
  277. '".$this->db->real_escape_string($inTime)."',
  278. '".$this->db->real_escape_string($outTime)."',
  279. '".$this->db->real_escape_string($this->getLessTime())."',
  280. '".$this->db->real_escape_string($this->getCodeId())."'
  281. )";
  282. }
  283. else
  284. {
  285. //Update item
  286. $sql = "UPDATE timeEntries SET
  287. userId='".$this->db->real_escape_string($userId)."',
  288. inTime='".$this->db->real_escape_string($inTime)."',
  289. outTime='".$this->db->real_escape_string($outTime)."',
  290. lessTime='".$this->db->real_escape_string($this->getLessTime())."',
  291. codeId='".$this->db->real_escape_string($this->getCodeId())."'
  292. WHERE id='".$this->db->real_escape_string($this->getId())."'
  293. ";
  294. }
  295. $query = $this->db->query($sql);
  296. if($query === true)
  297. {
  298. return true;
  299. }
  300. }
  301. function nearestQuarterHour($time)
  302. {
  303. //$time = strtotime($time);
  304. $round = 15*60;
  305. $rounded = round($time/$round)*$round;
  306. return date("g:i A", $rounded);
  307. }
  308. function timeToDecimal($time)
  309. {
  310. $timeArr = explode(':', $time);
  311. $hours = $timeArr[0]*1;
  312. $minutes = $timeArr[1]/60;
  313. $dec = $hours + $minutes;
  314. if($dec > 0)
  315. {
  316. return round($dec,2);
  317. }
  318. else
  319. {
  320. return 0;
  321. }
  322. }
  323. }
  324. ?>