timeEntryModel.php 16 KB

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