timesheetModel.php 8.0 KB

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