timesheetModel.php 10 KB

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