timeEntryModel.php 9.8 KB

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