timesheetController.php 15 KB

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