timeEntryModel.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. private $userId;
  22. private $timestamp;
  23. private $note;
  24. /**
  25. * @return mixed
  26. */
  27. public function getId()
  28. {
  29. return $this->id;
  30. }
  31. /**
  32. * @param mixed $id
  33. */
  34. public function setId($id)
  35. {
  36. $this->id = $id;
  37. }
  38. /**
  39. * @return mixed
  40. */
  41. public function getDate()
  42. {
  43. return $this->date;
  44. }
  45. /**
  46. * @param mixed $date
  47. */
  48. public function setDate($date)
  49. {
  50. $this->date = $date;
  51. }
  52. /**
  53. * @return mixed
  54. */
  55. public function getFullDate()
  56. {
  57. return $this->fullDate;
  58. }
  59. /**
  60. * @param mixed $fullDate
  61. */
  62. public function setFullDate($fullDate)
  63. {
  64. $this->fullDate = $fullDate;
  65. }
  66. /**
  67. * @return mixed
  68. */
  69. public function getInTime()
  70. {
  71. return $this->inTime;
  72. }
  73. /**
  74. * @param mixed $inTime
  75. */
  76. public function setInTime($inTime)
  77. {
  78. $this->inTime = $inTime;
  79. }
  80. /**
  81. * @return mixed
  82. */
  83. public function getInTimeRaw()
  84. {
  85. return $this->inTimeRaw;
  86. }
  87. /**
  88. * @param mixed $inTimeRaw
  89. */
  90. public function setInTimeRaw($inTimeRaw)
  91. {
  92. $this->inTimeRaw = $inTimeRaw;
  93. }
  94. /**
  95. * @return mixed
  96. */
  97. public function getRoundedInTime()
  98. {
  99. return $this->roundedInTime;
  100. }
  101. /**
  102. * @param mixed $roundedInTime
  103. */
  104. public function setRoundedInTime($roundedInTime)
  105. {
  106. $this->roundedInTime = $roundedInTime;
  107. }
  108. /**
  109. * @return mixed
  110. */
  111. public function getInTimeDate()
  112. {
  113. return $this->inTimeDate;
  114. }
  115. /**
  116. * @param mixed $inTimeDate
  117. */
  118. public function setInTimeDate($inTimeDate)
  119. {
  120. $this->inTimeDate = $inTimeDate;
  121. }
  122. /**
  123. * @return mixed
  124. */
  125. public function getOutTime()
  126. {
  127. return $this->outTime;
  128. }
  129. /**
  130. * @param mixed $outTime
  131. */
  132. public function setOutTime($outTime)
  133. {
  134. $this->outTime = $outTime;
  135. }
  136. /**
  137. * @return mixed
  138. */
  139. public function getOutTimeRaw()
  140. {
  141. return $this->outTimeRaw;
  142. }
  143. /**
  144. * @param mixed $outTimeRaw
  145. */
  146. public function setOutTimeRaw($outTimeRaw)
  147. {
  148. $this->outTimeRaw = $outTimeRaw;
  149. }
  150. /**
  151. * @return mixed
  152. */
  153. public function getRoundedOutTime()
  154. {
  155. return $this->roundedOutTime;
  156. }
  157. /**
  158. * @param mixed $roundedOutTime
  159. */
  160. public function setRoundedOutTime($roundedOutTime)
  161. {
  162. $this->roundedOutTime = $roundedOutTime;
  163. }
  164. /**
  165. * @return mixed
  166. */
  167. public function getOutTimeDate()
  168. {
  169. return $this->outTimeDate;
  170. }
  171. /**
  172. * @param mixed $outTimeDate
  173. */
  174. public function setOutTimeDate($outTimeDate)
  175. {
  176. $this->outTimeDate = $outTimeDate;
  177. }
  178. /**
  179. * @return mixed
  180. */
  181. public function getLessTime()
  182. {
  183. return $this->lessTime;
  184. }
  185. /**
  186. * @param mixed $lessTime
  187. */
  188. public function setLessTime($lessTime)
  189. {
  190. $this->lessTime = $lessTime;
  191. }
  192. /**
  193. * @return mixed
  194. */
  195. public function getCodeId()
  196. {
  197. return $this->codeId;
  198. }
  199. /**
  200. * @param mixed $codeId
  201. */
  202. public function setCodeId($codeId)
  203. {
  204. $this->codeId = $codeId;
  205. }
  206. /**
  207. * @return mixed
  208. */
  209. public function getCodeName()
  210. {
  211. return $this->codeName;
  212. }
  213. /**
  214. * @param mixed $codeName
  215. */
  216. public function setCodeName($codeName)
  217. {
  218. $this->codeName = $codeName;
  219. }
  220. /**
  221. * @return mixed
  222. */
  223. public function getTimeWorked()
  224. {
  225. return $this->timeWorked;
  226. }
  227. /**
  228. * @param mixed $timeWorked
  229. */
  230. public function setTimeWorked($timeWorked)
  231. {
  232. $this->timeWorked = $timeWorked;
  233. }
  234. /**
  235. * @return mixed
  236. */
  237. public function getBatchId()
  238. {
  239. return $this->batchId;
  240. }
  241. /**
  242. * @param mixed $batchId
  243. */
  244. public function setBatchId($batchId)
  245. {
  246. $this->batchId = $batchId;
  247. }
  248. /**
  249. * @return mixed
  250. */
  251. public function getUserId()
  252. {
  253. return $this->userId;
  254. }
  255. /**
  256. * @param mixed $userId
  257. */
  258. public function setUserId($userId)
  259. {
  260. $this->userId = $userId;
  261. }
  262. /**
  263. * @return mixed
  264. */
  265. public function getTimestamp()
  266. {
  267. return $this->timestamp;
  268. }
  269. /**
  270. * @param mixed $timestamp
  271. */
  272. public function setTimestamp($timestamp)
  273. {
  274. $this->timestamp = $timestamp;
  275. }
  276. /**
  277. * @return mixed
  278. */
  279. public function getNote()
  280. {
  281. return $this->note;
  282. }
  283. /**
  284. * @param mixed $note
  285. */
  286. public function setNote($note)
  287. {
  288. $this->note = $note;
  289. }
  290. function __construct($id = null)
  291. {
  292. $this->db = Staple_DB::get();
  293. if($id !== null)
  294. {
  295. $sql = "SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."'";
  296. if($this->db->query($sql)->fetch_row() > 0)
  297. {
  298. $query = $this->db->query($sql);
  299. $result = $query->fetch_assoc();
  300. //Set ID and Date
  301. $this->setId($result['id']);
  302. $this->setBatchId($result['batchId']);
  303. $this->setDate(date("m/d/Y",$result['inTime']));
  304. $this->setFullDate(date("l, F jS Y",$result['inTime']));
  305. //Set inTime
  306. $inTime = new DateTime();
  307. $inTime->setTimestamp($result['inTime']);
  308. $this->setInTime($inTime->format('g:i A'));
  309. $this->setInTimeRaw($result['inTime']);
  310. $this->setRoundedInTime($this->nearestQuarterHour($result['inTime']));
  311. $this->setInTimeDate(date("Y-m-d", $result['inTime']));
  312. //Out Time
  313. $outTime = new DateTime();
  314. $outTime->setTimestamp($result['outTime']);
  315. $this->setOutTime($outTime->format('g:i A'));
  316. $this->setOutTimeRaw($result['outTime']);
  317. $this->setRoundedOutTime($this->nearestQuarterHour($result['outTime']));
  318. $this->setOutTimeDate(date("Y-m-d", $result['outTime']));
  319. $this->setLessTime($result['lessTime']);
  320. //Calculate Time Worked
  321. switch($result['lessTime'])
  322. {
  323. case 60:
  324. $lessTime = 1;
  325. break;
  326. case 30:
  327. $lessTime = 0.5;
  328. break;
  329. case 15:
  330. $lessTime = 0.25;
  331. break;
  332. default:
  333. $lessTime = 0;
  334. }
  335. //Total Worked Time
  336. $dateTime1 = new DateTime($this->roundedInTime);
  337. $dateTime1->setDate(date('Y',strtotime($this->inTimeDate)), date('m',strtotime($this->inTimeDate)), date('d',strtotime($this->inTimeDate)));
  338. $dateTime2 = new DateTime($this->roundedOutTime);
  339. $dateTime2->setDate(date('Y',strtotime($this->outTimeDate)), date('m',strtotime($this->outTimeDate)), date('d',strtotime($this->outTimeDate)));
  340. $interval = $dateTime1->diff($dateTime2);
  341. $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
  342. if($timeWorked !== 0)
  343. {
  344. $this->setTimeWorked($timeWorked);
  345. }
  346. else
  347. {
  348. $this->setTimeWorked(0);
  349. }
  350. //Get Code Information
  351. $code = new codeModel();
  352. $this->setCodeId($result['codeId']);
  353. $code->load($result['codeId']);
  354. $this->setCodeName($code->getName());
  355. $this->setUserId($result['userId']);
  356. $this->setTimestamp($result['timestamp']);
  357. $this->setNote($result['note']);
  358. return true;
  359. }
  360. }
  361. }
  362. function remove($id)
  363. {
  364. $this->db = Staple_DB::get();
  365. if($id !== null)
  366. {
  367. $auth = Staple_Auth::get();
  368. $user = new userModel($auth->getAuthId());
  369. $userId = $user->getId();
  370. $accountLevel = $user->getAuthLevel();
  371. $entry = new timeEntryModel($id);
  372. $fullDate = $entry->getFullDate();
  373. $inTime = $entry->getInTime();
  374. $outTime = $entry->getOutTime();
  375. $effectedUserId = $entry->getUserId();
  376. $effectedUser = new userModel();
  377. $account = $effectedUser->userInfo($effectedUserId);
  378. //Check for admin account delete
  379. if($accountLevel >= 900)
  380. {
  381. $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."'";
  382. //AND userId <> '".$this->db->real_escape_string($userId)."'
  383. if($this->db->query($sql))
  384. {
  385. $audit = new auditModel();
  386. $audit->setUserId($account['id']);
  387. $audit->setAction('Admin Entry Remove');
  388. $audit->setItem($user->getUsername()." removed entry for ".$fullDate." In Time: ".$inTime." Out Time: ".$outTime."");
  389. $audit->save();
  390. return true;
  391. }
  392. }
  393. else
  394. {
  395. //Check if validated
  396. if($this->validated($id))
  397. {
  398. $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
  399. if($this->db->query($sql))
  400. {
  401. return true;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. function save()
  408. {
  409. $this->db = Staple_DB::get();
  410. $auth = Staple_Auth::get();
  411. $user = new userModel($auth->getAuthId());
  412. $userId = $user->getId();
  413. $batchId = $user->getBatchId();
  414. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  415. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  416. if(strtotime($this->getDate()." ".$this->getInTime()) > strtotime($this->getDate()." ".$this->getOutTime()))
  417. {
  418. $outTime = strtotime($this->getDate()." 12:00 AM")+86400;
  419. }
  420. if($this->id == NULL)
  421. {
  422. if($this->_overlap($inTime,$outTime))
  423. {
  424. //Insert new item
  425. $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId, batchId)
  426. VALUES (
  427. '" . $this->db->real_escape_string($userId) . "',
  428. '" . $this->db->real_escape_string($inTime) . "',
  429. '" . $this->db->real_escape_string($outTime) . "',
  430. '" . $this->db->real_escape_string($this->getLessTime()) . "',
  431. '" . $this->db->real_escape_string($this->getCodeId()) . "',
  432. '" . $this->db->real_escape_string($batchId) . "'
  433. )";
  434. $query = $this->db->query($sql);
  435. if ($query === true)
  436. {
  437. return true;
  438. }
  439. }
  440. }
  441. else
  442. {
  443. if($this->_overlap($inTime,$outTime,$this->getId()))
  444. {
  445. //Update item
  446. $sql = "UPDATE timeEntries SET
  447. inTime='" . $this->db->real_escape_string($inTime) . "',
  448. outTime='" . $this->db->real_escape_string($outTime) . "',
  449. lessTime='" . $this->db->real_escape_string($this->getLessTime()) . "',
  450. codeId='" . $this->db->real_escape_string($this->getCodeId()) . "',
  451. batchId='" . $this->db->real_escape_string($batchId) . "'
  452. WHERE id='" . $this->db->real_escape_string($this->id) . "'
  453. ";
  454. $query = $this->db->query($sql);
  455. if ($query === true)
  456. {
  457. return true;
  458. }
  459. }
  460. }
  461. }
  462. function nearestQuarterHour($time,$string = null)
  463. {
  464. //$time = strtotime($time);
  465. $round = 15*60;
  466. $rounded = round($time/$round)*$round;
  467. if($string == 1)
  468. {
  469. return $rounded;
  470. }
  471. else
  472. {
  473. return date("g:i A", $rounded);
  474. }
  475. }
  476. function timeToDecimal($time)
  477. {
  478. $timeArr = explode(':', $time);
  479. $hours = $timeArr[0]*1;
  480. $minutes = $timeArr[1]/60;
  481. $dec = $hours + $minutes;
  482. if($dec > 0)
  483. {
  484. return round($dec,2);
  485. }
  486. else
  487. {
  488. return 0;
  489. }
  490. }
  491. function _overlap($inTime,$outTime,$id = null)
  492. {
  493. //Checks to see if the times entered fit within any other time entry for that user.
  494. $this->db = Staple_DB::get();
  495. $auth = Staple_Auth::get();
  496. $user = new userModel($auth->getAuthId());
  497. $userId = $user->getId();
  498. /*
  499. $dateString = strtotime(date("Y-m-d", $inTime));
  500. $nextDateString = $dateString + 86400;
  501. */
  502. $date = new DateTime();
  503. $dateString = $inTime;
  504. $nextDateString = $date->setTimestamp($inTime)->setTime(23,59,59);
  505. $nextDateString = $nextDateString->format('U');
  506. //Find the earliest time for the given date.
  507. $sql = "
  508. 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
  509. ";
  510. $query = $this->db->query($sql);
  511. $result = $query->fetch_assoc();
  512. $firstInTime = $result['inTime'];
  513. //Find the latest time for the given date.
  514. $sql = "
  515. 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
  516. ";
  517. if($this->db->query($sql)->num_rows > 0)
  518. {
  519. $query = $this->db->query($sql);
  520. $result = $query->fetch_assoc();
  521. $lastOutTime = $result['outTime'];
  522. }
  523. else
  524. {
  525. $lastOutTime = null;
  526. }
  527. if($id == null)
  528. {
  529. $sql = "SELECT inTime, outTime FROM timeEntries WHERE userId = '".$this->db->real_escape_string($userId)."'";
  530. }
  531. else
  532. {
  533. $sql = "SELECT inTime, outTime FROM timeEntries WHERE userId = '".$this->db->real_escape_string($userId)."' AND id <> '".$this->db->real_escape_string($id)."'";
  534. }
  535. $query = $this->db->query($sql);
  536. $data = array();
  537. while($result = $query->fetch_assoc())
  538. {
  539. $data[] = $result;
  540. }
  541. $overlap = 0;
  542. foreach($data as $entry)
  543. {
  544. if($inTime == $entry['inTime'] && $outTime == $entry['outTime'])
  545. {
  546. $overlap++;
  547. }
  548. if($inTime > $entry['inTime'] && $inTime < $entry['outTime'])
  549. {
  550. $overlap++;
  551. }
  552. if($outTime > $entry['inTime'] && $outTime < $entry['outTime'])
  553. {
  554. $overlap++;
  555. }
  556. if($inTime < $firstInTime && $outTime > $lastOutTime)
  557. {
  558. //$overlap++;
  559. }
  560. }
  561. if($overlap > 0)
  562. {
  563. return false;
  564. }
  565. else
  566. {
  567. return true;
  568. }
  569. }
  570. function validated($id,$uid = null)
  571. {
  572. if($uid == null)
  573. {
  574. $auth = Staple_Auth::get();
  575. $user = new userModel($auth->getAuthId());
  576. $userId = $user->getId();
  577. $batchId = $user->getBatchId();
  578. }
  579. else
  580. {
  581. $user = new userModel();
  582. $info = $user->userInfo($uid);
  583. $userId = $info['id'];
  584. $batchId = $info['batchId'];
  585. }
  586. $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)."'";
  587. if($this->db->query($sql)->num_rows > 0)
  588. {
  589. return true;
  590. }
  591. else
  592. {
  593. return false;
  594. }
  595. }
  596. function adminSave()
  597. {
  598. if(isset($this->userId))
  599. {
  600. //Check for current account.
  601. $currentUser = new userModel();
  602. if($this->userId != $currentUser->getId())
  603. {
  604. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  605. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  606. $sql = "
  607. INSERT INTO timeEntries
  608. (userId,inTime,outTime,lessTime,codeId,note,batchId)
  609. VALUES (
  610. '".$this->db->real_escape_string($this->userId)."',
  611. '".$this->db->real_escape_string($inTime)."',
  612. '".$this->db->real_escape_string($outTime)."',
  613. '".$this->db->real_escape_string($this->lessTime)."',
  614. '".$this->db->real_escape_string($this->codeId)."',
  615. '".$this->db->real_escape_string($this->note)."',
  616. '".$this->db->real_escape_string("ADMIN ADD")."'
  617. )
  618. ";
  619. if($this->db->query($sql))
  620. {
  621. $user = new userModel();
  622. $audit = new auditModel();
  623. $audit->setUserId($this->userId);
  624. $audit->setAction('Admin Entry Add');
  625. $audit->setItem($user->getUsername()." added entry for ".$this->getDate().". In Time: ".$this->inTime."/Out Time: ".$this->outTime."");
  626. $audit->save();
  627. return true;
  628. }
  629. }
  630. }
  631. }
  632. }
  633. ?>