timesheetController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. $date = new DateTime();
  26. if($date->format('d') > 25)
  27. {
  28. $date->modify('+1 month');
  29. }
  30. $maxDate = $date->setDate($date->format('Y'),$date->format('m'),25)->setTime(23,59,59)->getTimestamp();
  31. $minDate = $date->modify('-1 month +1 day')->setTime(0,0,0)->getTimestamp();
  32. $userDate = strtotime($data['date']);
  33. //Date is within pay period
  34. if($userDate >= $minDate && $userDate <= $maxDate)
  35. {
  36. //Create a new entry object and set properties
  37. $entry = new timeEntryModel();
  38. $entry->setDate($data['date']);
  39. $entry->setInTime($data['inTime']);
  40. $entry->setOutTime($data['outTime']);
  41. $entry->setLessTime($data['lessTime']);
  42. $entry->setCodeId($data['code']);
  43. //Save entry data to table.
  44. if($entry->save())
  45. {
  46. //Return a new time form with success message
  47. $form = new insertTimeForm();
  48. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  49. $this->view->insertTimeForm = $form;
  50. }
  51. else
  52. {
  53. //Return the same form with a warning message
  54. $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
  55. $form->errorMessage = array($message);
  56. $this->view->insertTimeForm = $form;
  57. }
  58. }
  59. else
  60. {
  61. //Return the same form with error message.
  62. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  63. $this->view->insertTimeForm = $form;
  64. }
  65. }
  66. else
  67. {
  68. //Return form with invalid data.
  69. $this->view->insertTimeForm = $form;
  70. }
  71. }
  72. else
  73. {
  74. //Return form
  75. $this->view->insertTimeForm = $form;
  76. }
  77. //Set year and month variables if undefined.
  78. if($year == null)
  79. {
  80. $date = new DateTime();
  81. $year = $date->format('Y');
  82. }
  83. if($month == null)
  84. {
  85. $date = new DateTime();
  86. if($date->format("j") >= 26)
  87. {
  88. $month = $date->modify('+1 month')->format('m');
  89. }
  90. else
  91. {
  92. $month = $date->format('m');
  93. }
  94. }
  95. //Load timesheet for user.
  96. $timesheet = new timesheetModel($year,$month);
  97. //Pass timesheet object to view
  98. $this->view->timesheet = $timesheet;
  99. //Check for unvalidated entries
  100. $i = 0;
  101. foreach($timesheet->getEntries() as $entry)
  102. {
  103. if($entry->batchId == $timesheet->getBatch())
  104. {
  105. $i++;
  106. }
  107. }
  108. if($i > 0)
  109. {
  110. $this->view->needsValidation = true;
  111. }
  112. else
  113. {
  114. $this->view->needsValidation = false;
  115. }
  116. $changeYearForm = new changeYearForm();
  117. $this->view->changeYearForm = $changeYearForm;
  118. }
  119. public function remove($id = null)
  120. {
  121. if($id != null)
  122. {
  123. //Confirm entry for user
  124. $timeEntry = new timeEntryModel($id);
  125. if($timeEntry->getId() !== NULL)
  126. {
  127. //Remove Entry
  128. if($timeEntry->remove($timeEntry->getId()))
  129. {
  130. $this->view->message = "<i class=\"fa fa-check\"></i> Removed successfully.";
  131. }
  132. else
  133. {
  134. $this->view->message = "ERROR: Could not remove entry.";
  135. }
  136. }
  137. else
  138. {
  139. header("location: ".$this->_link(array('timesheet'))."");
  140. }
  141. }
  142. else
  143. {
  144. header("location: ".$this->_link(array('timesheet'))."");
  145. }
  146. }
  147. public function edit($id = null)
  148. {
  149. if($id != null)
  150. {
  151. $entry = new timeEntryModel($id);
  152. $data['inTime'] = $entry->getInTime();
  153. $data['outTime'] = $entry->getOutTime();
  154. $data['date'] = $entry->getDate();
  155. $data['lessTime'] = $entry->getLessTime();
  156. $data['code'] = $entry->getCodeId();
  157. $this->view->id = $entry->getId();
  158. $form = new editTimeForm();
  159. $form->setAction($this->_link(array('timesheet','edit',$id)));
  160. $form->addData($data);
  161. //Check for form submission
  162. if($form->wasSubmitted())
  163. {
  164. //Add submitted data to the form
  165. $form->addData($_POST);
  166. //Check form validation
  167. if($form->validate())
  168. {
  169. //Export form data into an array
  170. $data = $form->exportFormData();
  171. //Check if dates are within the current pay period.
  172. $startMonth = date('m',strtotime('last month'));
  173. if($startMonth == 1)
  174. {
  175. $startYear = date('Y',strtotime('last year'));
  176. }
  177. else
  178. {
  179. $startYear = date('Y');
  180. }
  181. $endMonth = date('m');
  182. $endYear = date('Y');
  183. $startDate= strtotime($startMonth.'/26/'.$startYear);
  184. $endDate = strtotime($endMonth.'/25/'.$endYear);
  185. $userDate = strtotime($data['date']);
  186. //Date is within pay period
  187. if($userDate >= $startDate && $userDate <= $endDate)
  188. {
  189. //Compare in Times and out Times.
  190. //if(strtotime($data['inTime']) < strtotime($data['outTime']))
  191. //{
  192. //Create a new entry object and set properties
  193. $entry = new timeEntryModel();
  194. $entry->setId($id);
  195. $entry->setDate($data['date']);
  196. $entry->setInTime($data['inTime']);
  197. $entry->setOutTime($data['outTime']);
  198. $entry->setLessTime($data['lessTime']);
  199. $entry->setCodeId($data['code']);
  200. //Save entry data to table.
  201. if($entry->save())
  202. {
  203. //Return a new time form with success message
  204. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  205. $this->view->form = $form;
  206. }
  207. else
  208. {
  209. //Return the same form with a warning message
  210. $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. If you are updating an already existing entry, remove that entry and submit a new one.";
  211. $form->errorMessage = array($message);
  212. $this->view->form = $form;
  213. }
  214. //}
  215. //else
  216. //{
  217. //Return the same form with error message.
  218. // $form->errorMessage = array("<i class='fa fa-warning'></i> <b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  219. // $this->view->form = $form;
  220. //}
  221. }
  222. else
  223. {
  224. //Return the same form with error message.
  225. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  226. $this->view->form = $form;
  227. }
  228. }
  229. else
  230. {
  231. //Return form with invalid data.
  232. $this->view->form = $form;
  233. }
  234. }
  235. else
  236. {
  237. //Return form
  238. $this->view->form = $form;
  239. }
  240. }
  241. else
  242. {
  243. header("location: ".$this->_link(array('timesheet'))."");
  244. }
  245. }
  246. public function changeyear()
  247. {
  248. $form = new changeYearForm();
  249. if($form->wasSubmitted())
  250. {
  251. $form->addData($_POST);
  252. if($form->validate())
  253. {
  254. $data = $form->exportFormData();
  255. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  256. }
  257. else
  258. {
  259. header("location: ".$this->_link(array('timesheet'))."");
  260. }
  261. }
  262. else
  263. {
  264. header("location: ".$this->_link(array('timesheet'))."");
  265. }
  266. }
  267. public function validate($year, $month)
  268. {
  269. $timesheet = new timesheetModel($year,$month);
  270. //Get Current Batch ID
  271. $auth = Staple_Auth::get();
  272. $user = new userModel($auth->getAuthId());
  273. $batchId = $user->getBatchId();
  274. //Check for unvalidated entries
  275. $i = 0;
  276. foreach($timesheet->getEntries() as $entry)
  277. {
  278. if($entry->batchId == $timesheet->getBatch())
  279. {
  280. $i++;
  281. }
  282. }
  283. if($i > 0)
  284. {
  285. $this->view->timesheet = $timesheet;
  286. $form = new validateTimeSheetForm();
  287. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  288. if($form->wasSubmitted())
  289. {
  290. $timesheet->validate($batchId);
  291. header("location:".$this->_link(array('timesheet'))."");
  292. }
  293. else
  294. {
  295. $this->view->form = $form;
  296. $this->view->needsValidation = false;
  297. }
  298. }
  299. else
  300. {
  301. $this->view->needsValidation = false;
  302. $this->view->timesheet = array();
  303. }
  304. }
  305. }
  306. ?>