timesheetModel.php 8.9 KB

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