timesheetController.php 15 KB

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