timesheetModel.php 7.7 KB

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