timesheetController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. //Date is within pay period
  40. if($userDate >= $startDate && $userDate <= $endDate)
  41. {
  42. //Compare in Times and out Times.
  43. if(strtotime($data['inTime']) < strtotime($data['outTime']))
  44. {
  45. //Create a new entry object and set properties
  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. //Save entry data to table.
  53. if($entry->save())
  54. {
  55. //Return a new time form with success message
  56. $form = new insertTimeForm();
  57. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  58. $this->view->insertTimeForm = $form;
  59. }
  60. else
  61. {
  62. //Return the same form with a warning message
  63. $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
  64. $form->errorMessage = array($message);
  65. $this->view->insertTimeForm = $form;
  66. }
  67. }
  68. else
  69. {
  70. //Return the same form with error message.
  71. $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  72. $this->view->insertTimeForm = $form;
  73. }
  74. }
  75. else
  76. {
  77. //Return the same form with error message.
  78. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  79. $this->view->insertTimeForm = $form;
  80. }
  81. }
  82. else
  83. {
  84. //Return form with invalid data.
  85. $this->view->insertTimeForm = $form;
  86. }
  87. }
  88. else
  89. {
  90. //Return form
  91. $this->view->insertTimeForm = $form;
  92. }
  93. //Set year and month variables if undefined.
  94. if($year == null)
  95. {
  96. $date = new DateTime();
  97. $year = $date->format('Y');
  98. }
  99. if($month == null)
  100. {
  101. $date = new DateTime();
  102. $month = $date->format('m');
  103. }
  104. //Load timesheet for user.
  105. $timesheet = new timesheetModel($year,$month);
  106. //Pass timesheet object to view
  107. $this->view->timesheet = $timesheet;
  108. //Check for unvalidated entries
  109. $i = 0;
  110. foreach($timesheet->getEntries() as $entry)
  111. {
  112. if($entry->batchId == $timesheet->getBatch())
  113. {
  114. $i++;
  115. }
  116. }
  117. if($i > 0)
  118. {
  119. $this->view->needsValidation = true;
  120. }
  121. else
  122. {
  123. $this->view->needsValidation = false;
  124. }
  125. $changeYearForm = new changeYearForm();
  126. $this->view->changeYearForm = $changeYearForm;
  127. }
  128. public function remove($id)
  129. {
  130. if($id != null)
  131. {
  132. //Confirm entry for user
  133. $timesheet = new timesheetModel();
  134. if($timesheet->exists($id))
  135. {
  136. //Delete Item
  137. if($timesheet->remove($id))
  138. {
  139. $this->view->message = "Entry removed.";
  140. }
  141. else
  142. {
  143. $this->view->message = "ERROR: Could not remove entry.";
  144. }
  145. }
  146. else
  147. {
  148. header("location: ".$this->_link(array('timesheet'))."");
  149. }
  150. }
  151. else
  152. {
  153. header("location: ".$this->_link(array('timesheet'))."");
  154. }
  155. }
  156. public function edit($id = null)
  157. {
  158. if($id != null)
  159. {
  160. $entry = new timeEntryModel($id);
  161. print_r($entry);
  162. }
  163. else
  164. {
  165. echo "ERROR: Unable to load entry";
  166. //header("location: ".$this->_link(array('timesheet'))."");
  167. }
  168. }
  169. public function changeyear()
  170. {
  171. $form = new changeYearForm();
  172. if($form->wasSubmitted())
  173. {
  174. $form->addData($_POST);
  175. if($form->validate())
  176. {
  177. $data = $form->exportFormData();
  178. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  179. }
  180. else
  181. {
  182. header("location: ".$this->_link(array('timesheet'))."");
  183. }
  184. }
  185. else
  186. {
  187. header("location: ".$this->_link(array('timesheet'))."");
  188. }
  189. }
  190. public function validate($year, $month)
  191. {
  192. $timesheet = new timesheetModel($year,$month);
  193. //Get Current Batch ID
  194. $auth = Staple_Auth::get();
  195. $user = new userModel($auth->getAuthId());
  196. $batchId = $user->getBatchId();
  197. //Check for unvalidated entries
  198. $i = 0;
  199. foreach($timesheet->getEntries() as $entry)
  200. {
  201. if($entry->batchId == $timesheet->getBatch())
  202. {
  203. $i++;
  204. }
  205. }
  206. if($i > 0)
  207. {
  208. $this->view->timesheet = $timesheet;
  209. $form = new validateTimeSheetForm();
  210. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  211. if($form->wasSubmitted())
  212. {
  213. $timesheet->validate($batchId);
  214. header("location:".$this->_link(array('timesheet'))."");
  215. }
  216. else
  217. {
  218. $this->view->form = $form;
  219. $this->view->needsValidation = false;
  220. }
  221. }
  222. else
  223. {
  224. $this->view->needsValidation = false;
  225. $this->view->timesheet = array();
  226. }
  227. }
  228. }
  229. ?>