timeEntryModel.php 20 KB

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