timesheetController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class timesheetController extends Staple_Controller
  3. {
  4. public function _start()
  5. {
  6. }
  7. public function index($year = null, $month = null)
  8. {
  9. //Typecast variables
  10. $month = (int) $month;
  11. $year = (int) $year;
  12. //Build new insert form
  13. $form = new insertTimeForm();
  14. //Check for form submission
  15. if($form->wasSubmitted())
  16. {
  17. //Add submitted data to the form
  18. $form->addData($_POST);
  19. //Check form validation
  20. if($form->validate())
  21. {
  22. //Export form data into an array
  23. $data = $form->exportFormData();
  24. //Check if dates are within the current pay period.
  25. $startMonth = date('m',strtotime('last month'));
  26. if($startMonth == 1)
  27. {
  28. $startYear = date('Y',strtotime('last year'));
  29. }
  30. else
  31. {
  32. $startYear = date('Y');
  33. }
  34. $endMonth = date('m');
  35. $endYear = date('Y');
  36. $startDate= strtotime($startMonth.'/26/'.$startYear);
  37. $endDate = strtotime($endMonth.'/25/'.$endYear);
  38. $userDate = strtotime($data['date']);
  39. if($userDate >= $startDate && $userDate <= $endDate)
  40. {
  41. //Date is within pay period
  42. //Compare in Times and out Times.
  43. if(strtotime($data['inTime']) < strtotime($data['outTime']))
  44. {
  45. //Create a new entry object
  46. $entry = new timeEntryModel();
  47. $entry->setDate($data['date']);
  48. $entry->setInTime($data['inTime']);
  49. $entry->setOutTime($data['outTime']);
  50. $entry->setLessTime($data['lessTime']);
  51. $entry->setCodeId($data['code']);
  52. if($entry->save())
  53. {
  54. $form = new insertTimeForm();
  55. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  56. $this->view->insertTimeForm = $form;
  57. }
  58. else
  59. {
  60. $message = $entry->save()->getMessage();
  61. $form->errorMessage = array($message);
  62. $this->view->insertTimeForm = $form;
  63. }
  64. }
  65. else
  66. {
  67. //Send form with error message back.
  68. $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  69. $this->view->insertTimeForm = $form;
  70. }
  71. }
  72. else
  73. {
  74. //Send form with error message back.
  75. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  76. $this->view->insertTimeForm = $form;
  77. }
  78. }
  79. else
  80. {
  81. $this->view->insertTimeForm = $form;
  82. }
  83. }
  84. else
  85. {
  86. $this->view->insertTimeForm = $form;
  87. }
  88. //Set year and month variables if undefined.
  89. if($year == null)
  90. {
  91. $date = new DateTime();
  92. $year = $date->format('Y');
  93. }
  94. if($month == null)
  95. {
  96. $date = new DateTime();
  97. $month = $date->format('m');
  98. }
  99. //Load timesheet for user.
  100. $timesheet = new timesheetModel($year,$month);
  101. //Pass timesheet object to view
  102. $this->view->timesheet = $timesheet;
  103. //Check for unvalidated entries
  104. $i = 0;
  105. foreach($timesheet->getEntries() as $entry)
  106. {
  107. if($entry->batchId == $timesheet->getBatch())
  108. {
  109. $i++;
  110. }
  111. }
  112. if($i > 0)
  113. {
  114. $this->view->needsValidation = true;
  115. }
  116. else
  117. {
  118. $this->view->needsValidation = false;
  119. }
  120. $changeYearForm = new changeYearForm();
  121. $this->view->changeYearForm = $changeYearForm;
  122. }
  123. public function remove($id)
  124. {
  125. if($id != null)
  126. {
  127. //Confirm entry for user
  128. $timesheet = new timesheetModel();
  129. if($timesheet->exists($id))
  130. {
  131. //Delete Item
  132. if($timesheet->remove($id))
  133. {
  134. $this->view->message = "Entry removed.";
  135. }
  136. else
  137. {
  138. $this->view->message = "ERROR: Could not remove entry.";
  139. }
  140. }
  141. else
  142. {
  143. header("location: ".$this->_link(array('timesheet'))."");
  144. }
  145. }
  146. else
  147. {
  148. header("location: ".$this->_link(array('timesheet'))."");
  149. }
  150. }
  151. public function edit($id = null)
  152. {
  153. if($id != null)
  154. {
  155. $entry = new timeEntryModel($id);
  156. if($entry)
  157. {
  158. $form = new editTimeForm();
  159. $form->setAction($this->_link(array('timesheet','edit',$id)));
  160. //$form->addData();
  161. $this->view->form = $form;
  162. }
  163. else
  164. {
  165. echo "Entry loaded";
  166. //header("location: ".$this->_link(array('timesheet'))."");
  167. }
  168. }
  169. else
  170. {
  171. echo "ERROR: Unable to load entry";
  172. //header("location: ".$this->_link(array('timesheet'))."");
  173. }
  174. }
  175. public function changeyear()
  176. {
  177. $form = new changeYearForm();
  178. if($form->wasSubmitted())
  179. {
  180. $form->addData($_POST);
  181. if($form->validate())
  182. {
  183. $data = $form->exportFormData();
  184. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  185. }
  186. else
  187. {
  188. header("location: ".$this->_link(array('timesheet'))."");
  189. }
  190. }
  191. else
  192. {
  193. header("location: ".$this->_link(array('timesheet'))."");
  194. }
  195. }
  196. public function validate($year, $month)
  197. {
  198. $timesheet = new timesheetModel($year,$month);
  199. //Get Current Batch ID
  200. $auth = Staple_Auth::get();
  201. $user = new userModel($auth->getAuthId());
  202. $batchId = $user->getBatchId();
  203. //Check for unvalidated entries
  204. $i = 0;
  205. foreach($timesheet->getEntries() as $entry)
  206. {
  207. if($entry->batchId == $timesheet->getBatch())
  208. {
  209. $i++;
  210. }
  211. }
  212. if($i > 0)
  213. {
  214. $this->view->timesheet = $timesheet;
  215. $form = new validateTimeSheetForm();
  216. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  217. if($form->wasSubmitted())
  218. {
  219. $timesheet->validate($batchId);
  220. header("location:".$this->_link(array('timesheet'))."");
  221. }
  222. else
  223. {
  224. $this->view->form = $form;
  225. $this->view->needsValidation = false;
  226. }
  227. }
  228. else
  229. {
  230. $this->view->needsValidation = false;
  231. $this->view->timesheet = array();
  232. }
  233. }
  234. }
  235. ?>