timesheetController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. class timesheetController extends Staple_Controller
  3. {
  4. private $userId;
  5. private $accountLevel;
  6. public function _start()
  7. {
  8. $auth = Staple_Auth::get();
  9. $user = new userModel();
  10. $user->userInfo($auth->getAuthId());
  11. $this->userId = $user->getId();
  12. $this->accountLevel = $user->getAuthLevel();
  13. }
  14. public function index($year = null, $month = null)
  15. {
  16. //Typecast variables
  17. $month = (int) $month;
  18. $year = (int) $year;
  19. //Build new insert form
  20. $form = new insertTimeForm();
  21. //Check for form submission
  22. if($form->wasSubmitted())
  23. {
  24. //Add submitted data to the form
  25. $form->addData($_POST);
  26. //Check form validation
  27. if($form->validate())
  28. {
  29. //Export form data into an array
  30. $data = $form->exportFormData();
  31. //Check if dates are within the current pay period.
  32. $date = new DateTime();
  33. if($date->format('d') > 25)
  34. {
  35. $date->modify('+1 month');
  36. }
  37. $maxDate = $date->setDate($date->format('Y'),$date->format('m'),25)->setTime(23,59,59)->getTimestamp();
  38. $minDate = $date->modify('-1 month +1 day')->setTime(0,0,0)->getTimestamp();
  39. $userDate = strtotime($data['date']);
  40. //Date is within pay period
  41. if($userDate >= $minDate && $userDate <= $maxDate)
  42. {
  43. //Create a new entry object and set properties
  44. $entry = new timeEntryModel();
  45. $entry->setDate($data['date']);
  46. $entry->setInTime($data['inTime']);
  47. $entry->setOutTime($data['outTime']);
  48. $entry->setLessTime($data['lessTime']);
  49. $entry->setCodeId($data['code']);
  50. //Save entry data to table.
  51. if($entry->save())
  52. {
  53. //Return a new time form with success message
  54. $form = new insertTimeForm();
  55. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  56. $this->view->insertTimeForm = $form;
  57. }
  58. else
  59. {
  60. //Return the same form with a warning message
  61. $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
  62. $form->errorMessage = array($message);
  63. $this->view->insertTimeForm = $form;
  64. }
  65. }
  66. else
  67. {
  68. //Return the same form with error message.
  69. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  70. $this->view->insertTimeForm = $form;
  71. }
  72. }
  73. else
  74. {
  75. //Return form with invalid data.
  76. $this->view->insertTimeForm = $form;
  77. }
  78. }
  79. else
  80. {
  81. //Return form
  82. $this->view->insertTimeForm = $form;
  83. }
  84. //Set year and month variables if undefined.
  85. if($year == null)
  86. {
  87. $date = new DateTime();
  88. $year = $date->format('Y');
  89. }
  90. if($month == null)
  91. {
  92. $date = new DateTime();
  93. if($date->format("j") >= 26)
  94. {
  95. $month = $date->modify('+1 month')->format('m');
  96. }
  97. else
  98. {
  99. $month = $date->format('m');
  100. }
  101. }
  102. //Load timesheet for user.
  103. $timesheet = new timesheetModel($year,$month);
  104. //Pass timesheet object to view
  105. $this->view->timesheet = $timesheet;
  106. //Check for unvalidated entries
  107. $i = 0;
  108. foreach($timesheet->getEntries() as $entry)
  109. {
  110. if($entry->batchId == $timesheet->getBatch())
  111. {
  112. $i++;
  113. }
  114. }
  115. if($i > 0)
  116. {
  117. $this->view->needsValidation = true;
  118. }
  119. else
  120. {
  121. $this->view->needsValidation = false;
  122. }
  123. $changeYearForm = new changeYearForm();
  124. $this->view->changeYearForm = $changeYearForm;
  125. }
  126. public function remove($id = null)
  127. {
  128. if($id != null)
  129. {
  130. //Confirm entry for user
  131. $timeEntry = new timeEntryModel($id);
  132. if($timeEntry->getId() !== NULL)
  133. {
  134. //Remove Entry
  135. if($timeEntry->remove($timeEntry->getId()))
  136. {
  137. $this->view->message = "<i class=\"fa fa-check\"></i> Removed successfully.";
  138. }
  139. else
  140. {
  141. $this->view->message = "ERROR: Cannot remove entry.";
  142. }
  143. }
  144. else
  145. {
  146. header("location: ".$this->_link(array('timesheet'))."");
  147. }
  148. }
  149. else
  150. {
  151. header("location: ".$this->_link(array('timesheet'))."");
  152. }
  153. }
  154. public function edit($id = null)
  155. {
  156. if($id != null)
  157. {
  158. $entry = new timeEntryModel($id);
  159. $data['inTime'] = $entry->getInTime();
  160. $data['outTime'] = $entry->getOutTime();
  161. $data['date'] = $entry->getDate();
  162. $data['lessTime'] = $entry->getLessTime();
  163. $data['code'] = $entry->getCodeId();
  164. $this->view->id = $entry->getId();
  165. $form = new editTimeForm();
  166. $form->setAction($this->_link(array('timesheet','edit',$id)));
  167. $form->addData($data);
  168. //Check for form submission
  169. if($form->wasSubmitted())
  170. {
  171. //Add submitted data to the form
  172. $form->addData($_POST);
  173. //Check form validation
  174. if($form->validate())
  175. {
  176. //Export form data into an array
  177. $data = $form->exportFormData();
  178. //Check if dates are within the current pay period.
  179. $startMonth = date('m',strtotime('last month'));
  180. if($startMonth == 1)
  181. {
  182. $startYear = date('Y',strtotime('last year'));
  183. }
  184. else
  185. {
  186. $startYear = date('Y');
  187. }
  188. $endMonth = date('m');
  189. $endYear = date('Y');
  190. $startDate= strtotime($startMonth.'/26/'.$startYear);
  191. $endDate = strtotime($endMonth.'/25/'.$endYear);
  192. $userDate = strtotime($data['date']);
  193. //Date is within pay period
  194. if($userDate >= $startDate && $userDate <= $endDate)
  195. {
  196. //Create a new entry object and set properties
  197. $entry = new timeEntryModel();
  198. $entry->setId($id);
  199. $entry->setDate($data['date']);
  200. $entry->setInTime($data['inTime']);
  201. $entry->setOutTime($data['outTime']);
  202. $entry->setLessTime($data['lessTime']);
  203. $entry->setCodeId($data['code']);
  204. //Save entry data to table.
  205. if($entry->save())
  206. {
  207. //Return a new time form with success message
  208. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  209. $this->view->form = $form;
  210. }
  211. else
  212. {
  213. //Return the same form with a warning message
  214. $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.";
  215. $form->errorMessage = array($message);
  216. $this->view->form = $form;
  217. }
  218. }
  219. else
  220. {
  221. //Return the same form with error message.
  222. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  223. $this->view->form = $form;
  224. }
  225. }
  226. else
  227. {
  228. //Return form with invalid data.
  229. $this->view->form = $form;
  230. }
  231. }
  232. else
  233. {
  234. //Return form
  235. $this->view->form = $form;
  236. }
  237. }
  238. else
  239. {
  240. header("location: ".$this->_link(array('timesheet'))."");
  241. }
  242. }
  243. public function changeyear()
  244. {
  245. $form = new changeYearForm();
  246. if($form->wasSubmitted())
  247. {
  248. $form->addData($_POST);
  249. if($form->validate())
  250. {
  251. $data = $form->exportFormData();
  252. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  253. }
  254. else
  255. {
  256. header("location: ".$this->_link(array('timesheet'))."");
  257. }
  258. }
  259. else
  260. {
  261. header("location: ".$this->_link(array('timesheet'))."");
  262. }
  263. }
  264. public function validate($year, $month)
  265. {
  266. $timesheet = new timesheetModel($year,$month);
  267. //Get Current Batch ID
  268. $auth = Staple_Auth::get();
  269. $user = new userModel($auth->getAuthId());
  270. $batchId = $user->getBatchId();
  271. //Check for unvalidated entries within the current pay period.
  272. $i = 0;
  273. foreach($timesheet->getEntries() as $entry)
  274. {
  275. if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
  276. {
  277. if($entry->batchId == $timesheet->getBatch())
  278. {
  279. $i++;
  280. }
  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. if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
  291. {
  292. $timesheet->validate($batchId);
  293. header("location:" . $this->_link(array('timesheet')) . "");
  294. }
  295. }
  296. else
  297. {
  298. $this->view->form = $form;
  299. $this->view->needsValidation = false;
  300. }
  301. }
  302. else
  303. {
  304. $this->view->needsValidation = false;
  305. $this->view->timesheet = array();
  306. }
  307. }
  308. /* TODO REMOVE
  309. public function unlocked()
  310. {
  311. $form = new unlockDatesForm();
  312. if($form->wasSubmitted())
  313. {
  314. $form->addData($_POST);
  315. if($form->validate())
  316. {
  317. $data = $form->exportFormData();
  318. }
  319. else
  320. {
  321. $this->view->form = $form;
  322. }
  323. }
  324. else
  325. {
  326. $this->view->form = $form;
  327. }
  328. }
  329. */
  330. public function admininsert()
  331. {
  332. if($this->accountLevel >= 900)
  333. {
  334. $form = new insertTimeForm();
  335. $form->admin(1);
  336. if($form->wasSubmitted())
  337. {
  338. $form->addData($_POST);
  339. if($form->validate())
  340. {
  341. $data = $form->exportFormData();
  342. //Create a new entry object and set properties
  343. $entry = new timeEntryModel();
  344. $entry->setDate($data['date']);
  345. $entry->setInTime($data['inTime']);
  346. $entry->setOutTime($data['outTime']);
  347. $entry->setLessTime($data['lessTime']);
  348. $entry->setCodeId($data['code']);
  349. $entry->setUserId($data['account']);
  350. $entry->setNote($data['note']);
  351. //Save entry data to table.
  352. if($entry->adminSave())
  353. {
  354. //Return a new time form with success message
  355. $form = new insertTimeForm();
  356. $form->admin(1);
  357. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  358. $this->view->form = $form;
  359. }
  360. else
  361. {
  362. //Return the same form with a warning message
  363. $message = "<i class=\"fa fa-warning\"></i> Administrative action not allowed on your own timesheet.";
  364. $form->errorMessage = array($message);
  365. $this->view->form = $form;
  366. }
  367. }
  368. else
  369. {
  370. $this->view->form = $form;
  371. }
  372. }
  373. else
  374. {
  375. $this->view->form = $form;
  376. }
  377. }
  378. else
  379. {
  380. header("location: ".$this->_link(array('index'))."");
  381. }
  382. }
  383. }
  384. ?>