timesheetModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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, $user = null)
  261. {
  262. $this->db = Staple_DB::get();
  263. if($user == null)
  264. {
  265. //Get batchID
  266. $user = new userModel();
  267. $this->batch = $user->getBatchId();
  268. }
  269. else
  270. {
  271. $user = new userModel($user);
  272. }
  273. //Current Dates
  274. $currentDate = new DateTime();
  275. $currentDate->setDate($year, $month, 1);
  276. $this->currentYear = $currentDate->format('Y');
  277. $this->currentMonth = $currentDate->format('m');
  278. $this->currentMonthText = $currentDate->format('F');
  279. $this->startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
  280. $this->startDateTimeString = strtotime($this->startDate);
  281. $currentDate->setDate($year, $month, 1);
  282. $this->endDate = $currentDate->modify('+24 day')->format('Y-m-d');
  283. $this->endDateTimeString = strtotime($this->endDate);
  284. //Previous Dates
  285. $previousDate = new DateTime();
  286. $previousDate->setDate($year, $month, 1);
  287. $previousDate->modify('-1 month');
  288. $this->previousMonth = $previousDate->format('m');
  289. $this->previousMonthText = $previousDate->format('F');
  290. $previousDate->setDate($year, $month, 1);
  291. $previousDate->modify('-1 year');
  292. $this->previousYear = $previousDate->format('Y');
  293. //Future Dates
  294. $furtureDate = new DateTime();
  295. $furtureDate->setDate($year, $month, 1);
  296. $furtureDate->modify('+1 month');
  297. $this->nextMonth = $furtureDate->format('m');
  298. $this->nextMonthText = $furtureDate->format('F');
  299. $furtureDate->setDate($year, $month, 1);
  300. $furtureDate->modify('+1 year');
  301. $this->nextYear = $furtureDate->format('Y');
  302. //Time Entries
  303. $this->entries = $this->timeEntries($this->startDate, $this->endDate);
  304. $timeCode = new codeModel();
  305. //Get time code totals
  306. $totals = array();
  307. foreach ($timeCode->allCodes() as $code)
  308. {
  309. $codeId = $timeCode->getIdFor($code);
  310. $totals[$code] = $this->calculatedTotals($codeId['id'],$this->startDate,$this->endDate);
  311. }
  312. $totals['Total Time'] = array_sum($totals);
  313. $this->setTotals($totals);
  314. $this->userId = $user->getId();
  315. }
  316. function validate($batchId)
  317. {
  318. //Generate a new Batch ID for the user.
  319. if($this->genSetNewBatch())
  320. {
  321. //TODO need to log how and when the timesheet validated.
  322. return true;
  323. }
  324. }
  325. function timeEntries($startDate,$endDate)
  326. {
  327. //Get user ID from Auth
  328. $user = new userModel();
  329. $userId = $user->getId();
  330. $sql = "SELECT id FROM timeEntries WHERE inTime BETWEEN $this->startDateTimeString AND $this->endDateTimeString AND userId = $userId ORDER BY inTime ASC";
  331. if($this->db->query($sql)->num_rows > 0)
  332. {
  333. $query = $this->db->query($sql);
  334. while($result = $query->fetch_assoc())
  335. {
  336. $entry = new timeEntryModel($result['id']);
  337. $data[] = $entry;
  338. }
  339. return $data;
  340. }
  341. else
  342. {
  343. return array();
  344. }
  345. }
  346. /* TODO deprecate
  347. function payPeriodCalculatedTotals($startDate, $endDate)
  348. {
  349. //Get user ID from Auth
  350. $user = new userModel();
  351. $userId = $user->getId();
  352. $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";
  353. if($this->db->query($sql)->num_rows > 0)
  354. {
  355. $query = $this->db->query($sql);
  356. $result = $query->fetch_assoc();
  357. return round($result['totalTime'],2);
  358. }
  359. else
  360. {
  361. return 0;
  362. }
  363. }
  364. */
  365. function calculatedTotals($code,$startDate,$endDate)
  366. {
  367. //Get user ID from Auth
  368. $user = new userModel();
  369. $userId = $user->getId();
  370. //$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 23:59:59') AND userId = $userId AND codeId = $code;";
  371. $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;";
  372. if($this->db->query($sql)->fetch_row() > 0)
  373. {
  374. $query = $this->db->query($sql);
  375. $total = 0;
  376. while($result = $query->fetch_assoc())
  377. {
  378. $inTime = $result['inTime'];
  379. $outTime = $result['outTime'];
  380. switch($result['lessTime'])
  381. {
  382. case 60:
  383. $lessTime = 1;
  384. break;
  385. case 30:
  386. $lessTime = 0.5;
  387. break;
  388. case 15:
  389. $lessTime = 0.25;
  390. break;
  391. default:
  392. $lessTime = 0;
  393. }
  394. $roundedInTime = $this->nearestQuarterHour($inTime);
  395. $roundedOutTime = $this->nearestQuarterHour($outTime);
  396. $lapse = $roundedOutTime - $roundedInTime;
  397. $lapseHours = gmdate ('H:i', $lapse);
  398. $decimalHours = $this->timeToDecimal($lapseHours);
  399. $total = $total + $decimalHours;
  400. $total = $total - $lessTime;
  401. }
  402. return $total;
  403. }
  404. else
  405. {
  406. return 0;
  407. }
  408. }
  409. function nearestQuarterHour($time)
  410. {
  411. //$time = strtotime($time);
  412. $round = 15*60;
  413. $rounded = round($time/$round)*$round;
  414. return $rounded;
  415. }
  416. function timeToDecimal($time)
  417. {
  418. $timeArr = explode(':', $time);
  419. $hours = $timeArr[0]*1;
  420. $minutes = $timeArr[1]/60;
  421. $dec = $hours + $minutes;
  422. if($dec > 0)
  423. {
  424. return round($dec,2);
  425. }
  426. else
  427. {
  428. return 0;
  429. }
  430. }
  431. function genSetNewBatch()
  432. {
  433. $this->db = Staple_DB::get();
  434. $user = new userModel();
  435. $userId = $user->getId();
  436. $oldKey = $user->getBatchId();
  437. $key = sha1(time().$user->getUsername().rand(999,9999999999));
  438. //Check if key exists
  439. $sql = "SELECT id FROM accounts WHERE batchId = '".$this->db->real_escape_string($key)."'";
  440. if($this->db->query($sql)->fetch_row() > 0)
  441. {
  442. //Key already in use
  443. return false;
  444. }
  445. else
  446. {
  447. //Set new key in user account
  448. $sql = "UPDATE accounts SET batchId='".$this->db->real_escape_string($key)."' WHERE id=$userId";
  449. if($this->db->query($sql))
  450. {
  451. //Log Audit
  452. $audit = new auditModel();
  453. $audit->setAction('Timesheet Validation');
  454. $audit->setUserId($userId);
  455. $audit->setItem('Batch: '.$oldKey);
  456. $audit->save();
  457. return true;
  458. }
  459. else
  460. {
  461. return false;
  462. }
  463. }
  464. }
  465. }
  466. ?>