timeEntryModel.php 20 KB

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