timesheetModel.php 11 KB

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