timesheetController.php 16 KB

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