timeEntryModel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. class timeEntryModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $date;
  7. private $fullDate;
  8. private $inTime;
  9. private $inTimeRaw;
  10. private $roundedInTime;
  11. private $outTime;
  12. private $outTimeRaw;
  13. private $roundedOutTime;
  14. private $lessTime;
  15. private $codeId;
  16. private $codeName;
  17. private $timeWorked;
  18. private $batchId;
  19. /**
  20. * @return mixed
  21. */
  22. public function getId()
  23. {
  24. return $this->id;
  25. }
  26. /**
  27. * @param mixed $id
  28. */
  29. public function setId($id)
  30. {
  31. $this->id = $id;
  32. }
  33. /**
  34. * @return mixed
  35. */
  36. public function getDate()
  37. {
  38. return $this->date;
  39. }
  40. /**
  41. * @param mixed $date
  42. */
  43. public function setDate($date)
  44. {
  45. $this->date = $date;
  46. }
  47. /**
  48. * @return mixed
  49. */
  50. public function getInTime()
  51. {
  52. return $this->inTime;
  53. }
  54. /**
  55. * @param mixed $inTime
  56. */
  57. public function setInTime($inTime)
  58. {
  59. $this->inTime = $inTime;
  60. }
  61. /**
  62. * @return mixed
  63. */
  64. public function getOutTime()
  65. {
  66. return $this->outTime;
  67. }
  68. /**
  69. * @param mixed $outTime
  70. */
  71. public function setOutTime($outTime)
  72. {
  73. $this->outTime = $outTime;
  74. }
  75. /**
  76. * @return mixed
  77. */
  78. public function getInTimeRaw()
  79. {
  80. return $this->inTimeRaw;
  81. }
  82. /**
  83. * @param mixed $inTimeRaw
  84. */
  85. public function setInTimeRaw($inTimeRaw)
  86. {
  87. $this->inTimeRaw = $inTimeRaw;
  88. }
  89. /**
  90. * @return mixed
  91. */
  92. public function getOutTimeRaw()
  93. {
  94. return $this->outTimeRaw;
  95. }
  96. /**
  97. * @param mixed $outTimeRaw
  98. */
  99. public function setOutTimeRaw($outTimeRaw)
  100. {
  101. $this->outTimeRaw = $outTimeRaw;
  102. }
  103. /**
  104. * @return mixed
  105. */
  106. public function getLessTime()
  107. {
  108. return $this->lessTime;
  109. }
  110. /**
  111. * @param mixed $lessTime
  112. */
  113. public function setLessTime($lessTime)
  114. {
  115. $this->lessTime = $lessTime;
  116. }
  117. /**
  118. * @return mixed
  119. */
  120. public function getCodeId()
  121. {
  122. return $this->codeId;
  123. }
  124. /**
  125. * @param mixed $codeId
  126. */
  127. public function setCodeId($codeId)
  128. {
  129. $this->codeId = $codeId;
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. public function getCodeName()
  135. {
  136. return $this->codeName;
  137. }
  138. /**
  139. * @param mixed $codeName
  140. */
  141. public function setCodeName($codeName)
  142. {
  143. $this->codeName = $codeName;
  144. }
  145. /**
  146. * @return mixed
  147. */
  148. public function getTimeWorked()
  149. {
  150. return $this->timeWorked;
  151. }
  152. /**
  153. * @param mixed $timeWorked
  154. */
  155. public function setTimeWorked($timeWorked)
  156. {
  157. $this->timeWorked = $timeWorked;
  158. }
  159. /**
  160. * @return mixed
  161. */
  162. public function getRoundedInTime()
  163. {
  164. return $this->roundedInTime;
  165. }
  166. /**
  167. * @param mixed $roundedInTime
  168. */
  169. public function setRoundedInTime($roundedInTime)
  170. {
  171. $this->roundedInTime = $roundedInTime;
  172. }
  173. /**
  174. * @return mixed
  175. */
  176. public function getRoundedOutTime()
  177. {
  178. return $this->roundedOutTime;
  179. }
  180. /**
  181. * @param mixed $roundedOutTime
  182. */
  183. public function setRoundedOutTime($roundedOutTime)
  184. {
  185. $this->roundedOutTime = $roundedOutTime;
  186. }
  187. /**
  188. * @return mixed
  189. */
  190. public function getBatchId()
  191. {
  192. return $this->batchId;
  193. }
  194. /**
  195. * @param mixed $batchId
  196. */
  197. public function setBatchId($batchId)
  198. {
  199. $this->batchId = $batchId;
  200. }
  201. /**
  202. * @return mixed
  203. */
  204. public function getFullDate()
  205. {
  206. return $this->fullDate;
  207. }
  208. /**
  209. * @param mixed $fullDate
  210. */
  211. public function setFullDate($fullDate)
  212. {
  213. $this->fullDate = $fullDate;
  214. }
  215. function __construct($id = null)
  216. {
  217. $this->db = Staple_DB::get();
  218. if($id !== null)
  219. {
  220. $sql = "SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."'";
  221. if($this->db->query($sql)->fetch_row() > 0)
  222. {
  223. $query = $this->db->query($sql);
  224. $result = $query->fetch_assoc();
  225. //Set ID and Date
  226. $this->setId($result['id']);
  227. $this->setBatchId($result['batchId']);
  228. $this->setDate(date("m/d/Y",$result['inTime']));
  229. $this->setFullDate(date("l, F jS Y",$result['inTime']));
  230. //Set inTime
  231. $inTime = new DateTime();
  232. $inTime->setTimestamp($result['inTime']);
  233. $this->setInTime($inTime->format('h:i A'));
  234. $this->setInTimeRaw($result['inTime']);
  235. $this->setRoundedInTime($this->nearestQuarterHour($result['inTime']));
  236. //Out Time
  237. $outTime = new DateTime();
  238. $outTime->setTimestamp($result['outTime']);
  239. $this->setOutTime($outTime->format('h:i A'));
  240. $this->setOutTimeRaw($result['outTime']);
  241. $this->setRoundedOutTime($this->nearestQuarterHour($result['outTime']));
  242. $this->setLessTime($result['lessTime']);
  243. //Calculate Time Worked
  244. switch($result['lessTime'])
  245. {
  246. case 60:
  247. $lessTime = 1;
  248. break;
  249. case 30:
  250. $lessTime = 0.5;
  251. break;
  252. case 15:
  253. $lessTime = 0.25;
  254. break;
  255. default:
  256. $lessTime = 0;
  257. }
  258. //Total Worked Time
  259. $dateTime1 = new DateTime($this->roundedInTime);
  260. $dateTime2 = new DateTime($this->roundedOutTime);
  261. $interval = $dateTime1->diff($dateTime2);
  262. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  263. if($timeWorked !== 0)
  264. {
  265. $this->setTimeWorked($timeWorked);
  266. }
  267. else
  268. {
  269. $this->setTimeWorked(0);
  270. }
  271. //Get Code Information
  272. $code = new codeModel();
  273. $this->setCodeId($result['codeId']);
  274. $code->load($result['codeId']);
  275. $this->setCodeName($code->getName());
  276. return true;
  277. }
  278. }
  279. }
  280. function remove($id)
  281. {
  282. $this->db = Staple_DB::get();
  283. if($id !== null)
  284. {
  285. $auth = Staple_Auth::get();
  286. $user = new userModel($auth->getAuthId());
  287. $userId = $user->getId();
  288. //Check if validated
  289. if($this->_validated($id))
  290. {
  291. $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
  292. if($this->db->query($sql))
  293. {
  294. return true;
  295. }
  296. }
  297. }
  298. }
  299. function save()
  300. {
  301. $this->db = Staple_DB::get();
  302. $auth = Staple_Auth::get();
  303. $user = new userModel($auth->getAuthId());
  304. $userId = $user->getId();
  305. $batchId = $user->getBatchId();
  306. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  307. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  308. if($this->id == NULL)
  309. {
  310. if($this->_overlap($inTime,$outTime))
  311. {
  312. //Insert new item
  313. $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId, batchId)
  314. VALUES (
  315. '" . $this->db->real_escape_string($userId) . "',
  316. '" . $this->db->real_escape_string($inTime) . "',
  317. '" . $this->db->real_escape_string($outTime) . "',
  318. '" . $this->db->real_escape_string($this->getLessTime()) . "',
  319. '" . $this->db->real_escape_string($this->getCodeId()) . "',
  320. '" . $this->db->real_escape_string($batchId) . "'
  321. )";
  322. $query = $this->db->query($sql);
  323. if ($query === true)
  324. {
  325. return true;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. if($this->_overlap($inTime,$outTime,$this->getId()))
  332. {
  333. //Update item
  334. $sql = "UPDATE timeEntries SET
  335. inTime='" . $this->db->real_escape_string($inTime) . "',
  336. outTime='" . $this->db->real_escape_string($outTime) . "',
  337. lessTime='" . $this->db->real_escape_string($this->getLessTime()) . "',
  338. codeId='" . $this->db->real_escape_string($this->getCodeId()) . "',
  339. batchId='" . $this->db->real_escape_string($batchId) . "'
  340. WHERE id='" . $this->db->real_escape_string($this->id) . "'
  341. ";
  342. $query = $this->db->query($sql);
  343. if ($query === true)
  344. {
  345. return true;
  346. }
  347. }
  348. }
  349. }
  350. function nearestQuarterHour($time)
  351. {
  352. //$time = strtotime($time);
  353. $round = 15*60;
  354. $rounded = round($time/$round)*$round;
  355. return date("g:i A", $rounded);
  356. }
  357. function timeToDecimal($time)
  358. {
  359. $timeArr = explode(':', $time);
  360. $hours = $timeArr[0]*1;
  361. $minutes = $timeArr[1]/60;
  362. $dec = $hours + $minutes;
  363. if($dec > 0)
  364. {
  365. return round($dec,2);
  366. }
  367. else
  368. {
  369. return 0;
  370. }
  371. }
  372. function _overlap($inTime,$outTime,$id = null)
  373. {
  374. //Checks to see if the times entered fit within any other time entry for that user.
  375. $this->db = Staple_DB::get();
  376. $auth = Staple_Auth::get();
  377. $user = new userModel($auth->getAuthId());
  378. $userId = $user->getId();
  379. $dateString = strtotime(date("Y-m-d", $inTime));
  380. $nextDateString = $dateString + 86400;
  381. //Find the earliest time for the given date.
  382. $sql = "
  383. SELECT inTime FROM timeEntries WHERE inTime > '".$this->db->real_escape_string($dateString)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY inTime ASC LIMIT 1
  384. ";
  385. $query = $this->db->query($sql);
  386. $result = $query->fetch_assoc();
  387. $firstInTime = $result['inTime'];
  388. //Find the latest time for the given date.
  389. $sql = "
  390. SELECT outTime FROM timeEntries WHERE outTime > '".$this->db->real_escape_string($dateString)."' AND outTime < '".$this->db->real_escape_string($nextDateString)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY outTime DESC LIMIT 1
  391. ";
  392. $query = $this->db->query($sql);
  393. $result = $query->fetch_assoc();
  394. $lastOutTime = $result['outTime'];
  395. if($inTime < $firstInTime && $outTime > $lastOutTime)
  396. {
  397. return false;
  398. }
  399. else
  400. {
  401. //Check if inTime and out time fall between already existing dates.
  402. $sql = "
  403. SELECT id FROM timeEntries
  404. WHERE ('".$this->db->real_escape_string($outTime)."' > inTime AND '".$this->db->real_escape_string($outTime)."' < outTime)
  405. OR ('".$this->db->real_escape_string($inTime)."' > inTime AND '".$this->db->real_escape_string($inTime)."' < outTime)
  406. AND userId = '".$this->db->real_escape_string($userId)."'
  407. ";
  408. if($this->db->query($sql)->num_rows > 0)
  409. {
  410. return false;
  411. }
  412. else
  413. {
  414. return true;
  415. }
  416. }
  417. }
  418. function _validated($id)
  419. {
  420. $auth = Staple_Auth::get();
  421. $user = new userModel($auth->getAuthId());
  422. $userId = $user->getId();
  423. $batchId = $user->getBatchId();
  424. $sql = "SELECT id FROM timeEntries WHERE userId = '".$this->db->real_escape_string($userId)."' AND batchId = '".$this->db->real_escape_string($batchId)."' AND id = '".$this->db->real_escape_string($id)."'";
  425. if($this->db->query($sql)->num_rows > 0)
  426. {
  427. return true;
  428. }
  429. else
  430. {
  431. return false;
  432. }
  433. }
  434. }
  435. ?>