timesheetModel.php 9.1 KB

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