timesheetController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $data['inTime'] = $entry->getInTime();
  162. $data['outTime'] = $entry->getOutTime();
  163. $data['date'] = $entry->getDate();
  164. $data['lessTime'] = $entry->getLessTime();
  165. $data['code'] = $entry->getCodeId();
  166. $form = new editTimeForm();
  167. $form->setAction($this->_link(array('timesheet','edit',$id)));
  168. $form->addData($data);
  169. //Check for form submission
  170. if($form->wasSubmitted())
  171. {
  172. //Add submitted data to the form
  173. $form->addData($_POST);
  174. //Check form validation
  175. if($form->validate())
  176. {
  177. //Export form data into an array
  178. $data = $form->exportFormData();
  179. //Check if dates are within the current pay period.
  180. $startMonth = date('m',strtotime('last month'));
  181. if($startMonth == 1)
  182. {
  183. $startYear = date('Y',strtotime('last year'));
  184. }
  185. else
  186. {
  187. $startYear = date('Y');
  188. }
  189. $endMonth = date('m');
  190. $endYear = date('Y');
  191. $startDate= strtotime($startMonth.'/26/'.$startYear);
  192. $endDate = strtotime($endMonth.'/25/'.$endYear);
  193. $userDate = strtotime($data['date']);
  194. //Date is within pay period
  195. if($userDate >= $startDate && $userDate <= $endDate)
  196. {
  197. //Compare in Times and out Times.
  198. if(strtotime($data['inTime']) < strtotime($data['outTime']))
  199. {
  200. //Create a new entry object and set properties
  201. $entry = new timeEntryModel();
  202. $entry->setId($id);
  203. $entry->setDate($data['date']);
  204. $entry->setInTime($data['inTime']);
  205. $entry->setOutTime($data['outTime']);
  206. $entry->setLessTime($data['lessTime']);
  207. $entry->setCodeId($data['code']);
  208. //Save entry data to table.
  209. if($entry->save())
  210. {
  211. //Return a new time form with success message
  212. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  213. $this->view->form = $form;
  214. }
  215. else
  216. {
  217. //Return the same form with a warning message
  218. $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.";
  219. $form->errorMessage = array($message);
  220. $this->view->form = $form;
  221. }
  222. }
  223. else
  224. {
  225. //Return the same form with error message.
  226. $form->errorMessage = array("<i class='fa fa-warning'></i> <b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  227. $this->view->form = $form;
  228. }
  229. }
  230. else
  231. {
  232. //Return the same form with error message.
  233. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  234. $this->view->form = $form;
  235. }
  236. }
  237. else
  238. {
  239. //Return form with invalid data.
  240. $this->view->form = $form;
  241. }
  242. }
  243. else
  244. {
  245. //Return form
  246. $this->view->form = $form;
  247. }
  248. }
  249. else
  250. {
  251. header("location: ".$this->_link(array('timesheet'))."");
  252. }
  253. }
  254. public function changeyear()
  255. {
  256. $form = new changeYearForm();
  257. if($form->wasSubmitted())
  258. {
  259. $form->addData($_POST);
  260. if($form->validate())
  261. {
  262. $data = $form->exportFormData();
  263. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  264. }
  265. else
  266. {
  267. header("location: ".$this->_link(array('timesheet'))."");
  268. }
  269. }
  270. else
  271. {
  272. header("location: ".$this->_link(array('timesheet'))."");
  273. }
  274. }
  275. public function validate($year, $month)
  276. {
  277. $timesheet = new timesheetModel($year,$month);
  278. //Get Current Batch ID
  279. $auth = Staple_Auth::get();
  280. $user = new userModel($auth->getAuthId());
  281. $batchId = $user->getBatchId();
  282. //Check for unvalidated entries
  283. $i = 0;
  284. foreach($timesheet->getEntries() as $entry)
  285. {
  286. if($entry->batchId == $timesheet->getBatch())
  287. {
  288. $i++;
  289. }
  290. }
  291. if($i > 0)
  292. {
  293. $this->view->timesheet = $timesheet;
  294. $form = new validateTimeSheetForm();
  295. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  296. if($form->wasSubmitted())
  297. {
  298. $timesheet->validate($batchId);
  299. header("location:".$this->_link(array('timesheet'))."");
  300. }
  301. else
  302. {
  303. $this->view->form = $form;
  304. $this->view->needsValidation = false;
  305. }
  306. }
  307. else
  308. {
  309. $this->view->needsValidation = false;
  310. $this->view->timesheet = array();
  311. }
  312. }
  313. }
  314. ?>