timesheetModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. class timesheetModel extends Staple_Model
  3. {
  4. private $db;
  5. private $currentYear;
  6. private $currentMonth;
  7. private $currentMonthText;
  8. private $startDate;
  9. private $startDateTimeString;
  10. private $endDate;
  11. private $endDateTimeString;
  12. private $nextMonth;
  13. private $nextMonthText;
  14. private $nextYear;
  15. private $previousMonth;
  16. private $previousMonthText;
  17. private $previousYear;
  18. private $userId;
  19. private $batch;
  20. private $entries;
  21. private $totals;
  22. private $review;
  23. /**
  24. * @return string
  25. */
  26. public function getCurrentYear()
  27. {
  28. return $this->currentYear;
  29. }
  30. /**
  31. * @param string $currentYear
  32. */
  33. public function setCurrentYear($currentYear)
  34. {
  35. $this->currentYear = $currentYear;
  36. }
  37. /**
  38. * @return string
  39. */
  40. public function getCurrentMonth()
  41. {
  42. return $this->currentMonth;
  43. }
  44. /**
  45. * @param string $currentMonth
  46. */
  47. public function setCurrentMonth($currentMonth)
  48. {
  49. $this->currentMonth = $currentMonth;
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getCurrentMonthText()
  55. {
  56. return $this->currentMonthText;
  57. }
  58. /**
  59. * @param string $currentMonthText
  60. */
  61. public function setCurrentMonthText($currentMonthText)
  62. {
  63. $this->currentMonthText = $currentMonthText;
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getStartDate()
  69. {
  70. return $this->startDate;
  71. }
  72. /**
  73. * @param string $startDate
  74. */
  75. public function setStartDate($startDate)
  76. {
  77. $this->startDate = $startDate;
  78. }
  79. /**
  80. * @return int
  81. */
  82. public function getStartDateTimeString()
  83. {
  84. return $this->startDateTimeString;
  85. }
  86. /**
  87. * @param int $startDateTimeString
  88. */
  89. public function setStartDateTimeString($startDateTimeString)
  90. {
  91. $this->startDateTimeString = $startDateTimeString;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getEndDate()
  97. {
  98. return $this->endDate;
  99. }
  100. /**
  101. * @param string $endDate
  102. */
  103. public function setEndDate($endDate)
  104. {
  105. $this->endDate = $endDate;
  106. }
  107. /**
  108. * @return int
  109. */
  110. public function getEndDateTimeString()
  111. {
  112. return $this->endDateTimeString;
  113. }
  114. /**
  115. * @param int $endDateTimeString
  116. */
  117. public function setEndDateTimeString($endDateTimeString)
  118. {
  119. $this->endDateTimeString = $endDateTimeString;
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getNextMonth()
  125. {
  126. return $this->nextMonth;
  127. }
  128. /**
  129. * @param string $nextMonth
  130. */
  131. public function setNextMonth($nextMonth)
  132. {
  133. $this->nextMonth = $nextMonth;
  134. }
  135. /**
  136. * @return string
  137. */
  138. public function getNextMonthText()
  139. {
  140. return $this->nextMonthText;
  141. }
  142. /**
  143. * @param string $nextMonthText
  144. */
  145. public function setNextMonthText($nextMonthText)
  146. {
  147. $this->nextMonthText = $nextMonthText;
  148. }
  149. /**
  150. * @return string
  151. */
  152. public function getNextYear()
  153. {
  154. return $this->nextYear;
  155. }
  156. /**
  157. * @param string $nextYear
  158. */
  159. public function setNextYear($nextYear)
  160. {
  161. $this->nextYear = $nextYear;
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function getPreviousMonth()
  167. {
  168. return $this->previousMonth;
  169. }
  170. /**
  171. * @param string $previousMonth
  172. */
  173. public function setPreviousMonth($previousMonth)
  174. {
  175. $this->previousMonth = $previousMonth;
  176. }
  177. /**
  178. * @return string
  179. */
  180. public function getPreviousMonthText()
  181. {
  182. return $this->previousMonthText;
  183. }
  184. /**
  185. * @param string $previousMonthText
  186. */
  187. public function setPreviousMonthText($previousMonthText)
  188. {
  189. $this->previousMonthText = $previousMonthText;
  190. }
  191. /**
  192. * @return string
  193. */
  194. public function getPreviousYear()
  195. {
  196. return $this->previousYear;
  197. }
  198. /**
  199. * @param string $previousYear
  200. */
  201. public function setPreviousYear($previousYear)
  202. {
  203. $this->previousYear = $previousYear;
  204. }
  205. /**
  206. * @return mixed
  207. */
  208. public function getBatch()
  209. {
  210. return $this->batch;
  211. }
  212. /**
  213. * @param mixed $batch
  214. */
  215. public function setBatch($batch)
  216. {
  217. $this->batch = $batch;
  218. }
  219. /**
  220. * @return array
  221. */
  222. public function getEntries()
  223. {
  224. return $this->entries;
  225. }
  226. /**
  227. * @param array $entries
  228. */
  229. public function setEntries($entries)
  230. {
  231. $this->entries = $entries;
  232. }
  233. /**
  234. * @return mixed
  235. */
  236. public function getTotals()
  237. {
  238. return $this->totals;
  239. }
  240. /**
  241. * @param mixed $totals
  242. */
  243. public function setTotals($totals)
  244. {
  245. $this->totals = $totals;
  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 getReview()
  265. {
  266. return $this->review;
  267. }
  268. /**
  269. * @param mixed $review
  270. */
  271. public function setReview($review)
  272. {
  273. $this->review = $review;
  274. }
  275. function __construct($year, $month, $uid = null)
  276. {
  277. $this->db = Staple_DB::get();
  278. if($uid == null)
  279. {
  280. //Get batchID
  281. $user = new userModel();
  282. $this->batch = $user->getBatchId();
  283. }
  284. else
  285. {
  286. $user = new userModel($uid);
  287. }
  288. //Current Dates
  289. $currentDate = new DateTime();
  290. $currentDate->setTime(0,0,0);
  291. $currentDate->setDate($year, $month, 1);
  292. $this->currentYear = $currentDate->format('Y');
  293. $this->currentMonth = $currentDate->format('m');
  294. $this->currentMonthText = $currentDate->format('F');
  295. $this->startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
  296. $this->startDateTimeString = $currentDate->format('U');
  297. $currentDate->setDate($year, $month, 1);
  298. $currentDate->modify('+24 day');
  299. $currentDate->setTime(23,59,59);
  300. $this->endDate = $currentDate->format('Y-m-d');
  301. $this->endDateTimeString = $currentDate->format('U');
  302. //Previous Dates
  303. $previousDate = new DateTime();
  304. $previousDate->setTime(0,0,0);
  305. $previousDate->setDate($year, $month, 1);
  306. $previousDate->modify('-1 month');
  307. $this->previousMonth = $previousDate->format('m');
  308. $this->previousMonthText = $previousDate->format('F');
  309. $previousDate->setDate($year, $month, 1);
  310. $previousDate->modify('-1 year');
  311. $this->previousYear = $previousDate->format('Y');
  312. //Future Dates
  313. $furtureDate = new DateTime();
  314. $furtureDate->setTime(23,59,59);
  315. $furtureDate->setDate($year, $month, 1);
  316. $furtureDate->modify('+1 month');
  317. $this->nextMonth = $furtureDate->format('m');
  318. $this->nextMonthText = $furtureDate->format('F');
  319. $furtureDate->setDate($year, $month, 1);
  320. $furtureDate->modify('+1 year');
  321. $this->nextYear = $furtureDate->format('Y');
  322. //Time Entries
  323. $this->entries = $this->timeEntries($uid);
  324. $timeCode = new codeModel();
  325. //Get time code totals
  326. $totals = array();
  327. foreach ($timeCode->allCodes() as $code)
  328. {
  329. $codeId = $timeCode->getIdFor($code);
  330. $totals[$code] = $this->calculatedTotals($codeId['id'],$this->startDate,$this->endDate,$uid);
  331. }
  332. $totals['Total Time'] = array_sum($totals);
  333. $this->setTotals($totals);
  334. $this->userId = $user->getId();
  335. }
  336. function validate($batchId)
  337. {
  338. //Generate a new Batch ID for the user.
  339. if($this->genSetNewBatch())
  340. {
  341. //TODO need to log how and when the timesheet validated.
  342. return true;
  343. }
  344. }
  345. function timeEntries($uid = null)
  346. {
  347. $user = new userModel();
  348. if($uid != null)
  349. {
  350. $account = $user->userInfo($uid);
  351. $userId = $account['id'];
  352. }
  353. else
  354. {
  355. //Get user ID from Auth
  356. $userId = $user->getId();
  357. }
  358. $sql = "SELECT id FROM timeEntries WHERE inTime BETWEEN $this->startDateTimeString AND $this->endDateTimeString AND userId = $userId ORDER BY inTime ASC";
  359. if($this->db->query($sql)->num_rows > 0)
  360. {
  361. $query = $this->db->query($sql);
  362. while($result = $query->fetch_assoc())
  363. {
  364. $entry = new timeEntryModel($result['id']);
  365. $data[] = $entry;
  366. }
  367. return $data;
  368. }
  369. else
  370. {
  371. return array();
  372. }
  373. }
  374. /* TODO deprecate
  375. function payPeriodCalculatedTotals($startDate, $endDate)
  376. {
  377. //Get user ID from Auth
  378. $user = new userModel();
  379. $userId = $user->getId();
  380. $sql = "SELECT ROUND((TIME_TO_SEC(SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 00:00:00') AND userId = $userId";
  381. if($this->db->query($sql)->num_rows > 0)
  382. {
  383. $query = $this->db->query($sql);
  384. $result = $query->fetch_assoc();
  385. return round($result['totalTime'],2);
  386. }
  387. else
  388. {
  389. return 0;
  390. }
  391. }
  392. */
  393. function calculatedTotals($code,$startDate,$endDate,$uid=null)
  394. {
  395. //Get user ID from Auth
  396. $user = new userModel();
  397. if($uid == null)
  398. {
  399. $userId = $user->getId();
  400. }
  401. else
  402. {
  403. $account = $user->userInfo($uid);
  404. $userId = $account['id'];
  405. }
  406. $sql = "SELECT codeId, inTime, outTime, lessTime FROM timeEntries WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 23:59:59') AND userId = $userId AND codeId = $code;";
  407. if($this->db->query($sql)->fetch_row() > 0)
  408. {
  409. $query = $this->db->query($sql);
  410. $total = 0;
  411. while($result = $query->fetch_assoc())
  412. {
  413. $inTime = $result['inTime'];
  414. $outTime = $result['outTime'];
  415. switch($result['lessTime'])
  416. {
  417. case 60:
  418. $lessTime = 1;
  419. break;
  420. case 30:
  421. $lessTime = 0.5;
  422. break;
  423. case 15:
  424. $lessTime = 0.25;
  425. break;
  426. default:
  427. $lessTime = 0;
  428. }
  429. $roundedInTime = $this->nearestQuarterHour($inTime);
  430. $roundedOutTime = $this->nearestQuarterHour($outTime);
  431. $lapse = $roundedOutTime - $roundedInTime;
  432. $lapseHours = gmdate ('H:i', $lapse);
  433. $decimalHours = $this->timeToDecimal($lapseHours);
  434. $total = $total + $decimalHours;
  435. $total = $total - $lessTime;
  436. $code = new codeModel();
  437. $codeId = $code->getIdFor("Unpaid Leave");
  438. if($codeId['id'] == $result['codeId'])
  439. {
  440. $total = (-1)*$total;
  441. }
  442. }
  443. return $total;
  444. }
  445. else
  446. {
  447. return 0;
  448. }
  449. }
  450. function nearestQuarterHour($time)
  451. {
  452. $round = 15*60;
  453. $rounded = round($time/$round)*$round;
  454. return $rounded;
  455. }
  456. function timeToDecimal($time)
  457. {
  458. $timeArr = explode(':', $time);
  459. $hours = $timeArr[0]*1;
  460. $minutes = $timeArr[1]/60;
  461. $dec = $hours + $minutes;
  462. if($dec > 0)
  463. {
  464. return round($dec,2);
  465. }
  466. else
  467. {
  468. return 0;
  469. }
  470. }
  471. function genSetNewBatch()
  472. {
  473. $this->db = Staple_DB::get();
  474. $user = new userModel();
  475. $userId = $user->getId();
  476. $oldKey = $user->getBatchId();
  477. $key = sha1(time().$user->getUsername().rand(999,9999999999));
  478. //Check if key exists
  479. $sql = "SELECT id FROM accounts WHERE batchId = '".$this->db->real_escape_string($key)."'";
  480. if($this->db->query($sql)->fetch_row() > 0)
  481. {
  482. //Key already in use
  483. return false;
  484. }
  485. else
  486. {
  487. //Set new key in user account
  488. $sql = "UPDATE accounts SET batchId='".$this->db->real_escape_string($key)."' WHERE id=$userId";
  489. if($this->db->query($sql))
  490. {
  491. //Log Audit
  492. $audit = new auditModel();
  493. $audit->setAction('Timesheet Validation');
  494. $audit->setUserId($userId);
  495. $audit->setItem('Batch: '.$oldKey);
  496. $audit->save();
  497. return true;
  498. }
  499. else
  500. {
  501. return false;
  502. }
  503. }
  504. }
  505. }
  506. ?>