timesheetController.php 16 KB

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