timesheetController.php 12 KB

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