timesheetController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = new insertTimeForm();
  213. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  214. $this->view->form = $form;
  215. }
  216. else
  217. {
  218. //Return the same form with a warning message
  219. $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
  220. $form->errorMessage = array($message);
  221. $this->view->form = $form;
  222. }
  223. }
  224. else
  225. {
  226. //Return the same form with error message.
  227. $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  228. $this->view->form = $form;
  229. }
  230. }
  231. else
  232. {
  233. //Return the same form with error message.
  234. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  235. $this->view->form = $form;
  236. }
  237. }
  238. else
  239. {
  240. //Return form with invalid data.
  241. $this->view->form = $form;
  242. }
  243. }
  244. else
  245. {
  246. //Return form
  247. $this->view->form = $form;
  248. }
  249. }
  250. else
  251. {
  252. header("location: ".$this->_link(array('timesheet'))."");
  253. }
  254. }
  255. public function changeyear()
  256. {
  257. $form = new changeYearForm();
  258. if($form->wasSubmitted())
  259. {
  260. $form->addData($_POST);
  261. if($form->validate())
  262. {
  263. $data = $form->exportFormData();
  264. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  265. }
  266. else
  267. {
  268. header("location: ".$this->_link(array('timesheet'))."");
  269. }
  270. }
  271. else
  272. {
  273. header("location: ".$this->_link(array('timesheet'))."");
  274. }
  275. }
  276. public function validate($year, $month)
  277. {
  278. $timesheet = new timesheetModel($year,$month);
  279. //Get Current Batch ID
  280. $auth = Staple_Auth::get();
  281. $user = new userModel($auth->getAuthId());
  282. $batchId = $user->getBatchId();
  283. //Check for unvalidated entries
  284. $i = 0;
  285. foreach($timesheet->getEntries() as $entry)
  286. {
  287. if($entry->batchId == $timesheet->getBatch())
  288. {
  289. $i++;
  290. }
  291. }
  292. if($i > 0)
  293. {
  294. $this->view->timesheet = $timesheet;
  295. $form = new validateTimeSheetForm();
  296. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  297. if($form->wasSubmitted())
  298. {
  299. $timesheet->validate($batchId);
  300. header("location:".$this->_link(array('timesheet'))."");
  301. }
  302. else
  303. {
  304. $this->view->form = $form;
  305. $this->view->needsValidation = false;
  306. }
  307. }
  308. else
  309. {
  310. $this->view->needsValidation = false;
  311. $this->view->timesheet = array();
  312. }
  313. }
  314. }
  315. ?>