timesheetModel.php 8.6 KB

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