timesheetModel.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 $vacationUsed;
  21. private $normalWorked;
  22. private $sickUsed;
  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 float|int
  235. */
  236. public function getVacationUsed()
  237. {
  238. return $this->vacationUsed;
  239. }
  240. /**
  241. * @param float|int $vacationUsed
  242. */
  243. public function setVacationUsed($vacationUsed)
  244. {
  245. $this->vacationUsed = $vacationUsed;
  246. }
  247. /**
  248. * @return float|int
  249. */
  250. public function getNormalWorked()
  251. {
  252. return $this->normalWorked;
  253. }
  254. /**
  255. * @param float|int $normalWorked
  256. */
  257. public function setNormalWorked($normalWorked)
  258. {
  259. $this->normalWorked = $normalWorked;
  260. }
  261. /**
  262. * @return float|int
  263. */
  264. public function getSickUsed()
  265. {
  266. return $this->sickUsed;
  267. }
  268. /**
  269. * @param float|int $sickUsed
  270. */
  271. public function setSickUsed($sickUsed)
  272. {
  273. $this->sickUsed = $sickUsed;
  274. }
  275. function __construct($year, $month)
  276. {
  277. $this->db = Staple_DB::get();
  278. //Get batchID
  279. $user = new userModel();
  280. $this->batch = $user->getBatchId();
  281. //Current Dates
  282. $currentDate = new DateTime();
  283. $currentDate->setDate($year, $month, 1);
  284. $this->currentYear = $currentDate->format('Y');
  285. $this->currentMonth = $currentDate->format('m');
  286. $this->currentMonthText = $currentDate->format('F');
  287. $this->startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
  288. $this->startDateTimeString = strtotime($this->startDate);
  289. $currentDate->setDate($year, $month, 1);
  290. $this->endDate = $currentDate->modify('+24 day')->format('Y-m-d');
  291. $this->endDateTimeString = strtotime($this->endDate);
  292. //Previous Dates
  293. $previousDate = new DateTime();
  294. $previousDate->setDate($year, $month, 1);
  295. $previousDate->modify('-1 month');
  296. $this->previousMonth = $previousDate->format('m');
  297. $this->previousMonthText = $previousDate->format('F');
  298. $previousDate->setDate($year, $month, 1);
  299. $previousDate->modify('-1 year');
  300. $this->previousYear = $previousDate->format('Y');
  301. //Future Dates
  302. $furtureDate = new DateTime();
  303. $furtureDate->setDate($year, $month, 1);
  304. $furtureDate->modify('+1 month');
  305. $this->nextMonth = $furtureDate->format('m');
  306. $this->nextMonthText = $furtureDate->format('F');
  307. $furtureDate->setDate($year, $month, 1);
  308. $furtureDate->modify('+1 year');
  309. $this->nextYear = $furtureDate->format('Y');
  310. //Time Entries
  311. $this->entries = $this->entries($this->startDate, $this->endDate);
  312. $timeCode = new codeModel();
  313. //Vacation Total
  314. $code = $timeCode->getIdFor('vacation');
  315. $this->vacationUsed = $this->calculatedTotals($code['id'],$this->startDate, $this->endDate);
  316. //Normal Total
  317. $code = $timeCode->getIdFor('normal');
  318. $this->normalWorked = $this->calculatedTotals($code['id'],$this->startDate, $this->endDate);
  319. //Sick Total
  320. $code = $timeCode->getIdFor('sick');
  321. $this->sickUsed = $this->calculatedTotals($code['id'],$this->startDate,$this->endDate);
  322. }
  323. function validate($batchId)
  324. {
  325. //Generate a new Batch ID for the user.
  326. if($this->genSetNewBatch())
  327. {
  328. //TODO need to log how and when the timesheet validated.
  329. return true;
  330. }
  331. }
  332. function entries($startDate,$endDate)
  333. {
  334. //Get user ID from Auth
  335. $user = new userModel();
  336. $userId = $user->getId();
  337. $sql = "SELECT id FROM timeEntries WHERE inTime BETWEEN $this->startDateTimeString AND $this->endDateTimeString AND userId = $userId ORDER BY inTime ASC";
  338. if($this->db->query($sql)->num_rows > 0)
  339. {
  340. $query = $this->db->query($sql);
  341. while($result = $query->fetch_assoc())
  342. {
  343. $entry = new timeEntryModel($result['id']);
  344. $data[] = $entry;
  345. }
  346. return $data;
  347. }
  348. else
  349. {
  350. return array();
  351. }
  352. }
  353. function calculatedTotals($code,$startDate,$endDate)
  354. {
  355. //Get user ID from Auth
  356. $user = new userModel();
  357. $userId = $user->getId();
  358. $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;";
  359. if($this->db->query($sql)->num_rows > 0)
  360. {
  361. $query = $this->db->query($sql);
  362. $result = $query->fetch_assoc();
  363. return round($result['totalTime'],2);
  364. }
  365. else
  366. {
  367. return 0;
  368. }
  369. }
  370. function genSetNewBatch()
  371. {
  372. $this->db = Staple_DB::get();
  373. $user = new userModel();
  374. $userId = $user->getId();
  375. $key = sha1(time().$user->getUsername().rand(999,9999999999));
  376. //Check if key exists
  377. $sql = "SELECT id FROM accounts WHERE batchId = '".$this->db->real_escape_string($key)."'";
  378. if($this->db->query($sql)->fetch_row() > 0)
  379. {
  380. //Key already in use
  381. return false;
  382. }
  383. else
  384. {
  385. //Set new key in user account
  386. $sql = "UPDATE accounts SET batchId='".$this->db->real_escape_string($key)."' WHERE id=$userId";
  387. if($this->db->query($sql))
  388. {
  389. return true;
  390. }
  391. else
  392. {
  393. return false;
  394. }
  395. }
  396. }
  397. }
  398. ?>