timesheetController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. $startMonth = date('m',strtotime('last month'));
  245. if($startMonth == 1)
  246. {
  247. $startYear = date('Y',strtotime('last year'));
  248. }
  249. else
  250. {
  251. $startYear = date('Y');
  252. }
  253. $date = new DateTime();
  254. $endMonth = $date->modify('+1 month')->format('m');
  255. $endYear = $date->format('Y');
  256. $startDate= strtotime($startMonth.'/26/'.$startYear);
  257. $endDate = strtotime($endMonth.'/25/'.$endYear);
  258. $userDate = strtotime($data['date']);
  259. //Date is within pay period
  260. if($userDate >= $startDate && $userDate <= $endDate)
  261. {
  262. //Create a new entry object and set properties
  263. $entry = new timeEntryModel();
  264. $entry->setId($id);
  265. $entry->setDate($data['date']);
  266. $entry->setInTime($data['inTime']);
  267. $entry->setOutTime($data['outTime']);
  268. $entry->setLessTime($data['lessTime']);
  269. $entry->setCodeId($data['code']);
  270. //Save entry data to table.
  271. if($entry->save())
  272. {
  273. //Return a new time form with success message
  274. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  275. $this->view->form = $form;
  276. }
  277. else
  278. {
  279. //Return the same form with a warning message
  280. $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.";
  281. $form->errorMessage = array($message);
  282. $this->view->form = $form;
  283. }
  284. }
  285. else
  286. {
  287. //Return the same form with error message.
  288. $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
  289. $this->view->form = $form;
  290. }
  291. }
  292. else
  293. {
  294. //Return form with invalid data.
  295. $this->view->form = $form;
  296. }
  297. }
  298. else
  299. {
  300. //Return form
  301. $this->view->form = $form;
  302. }
  303. }
  304. else
  305. {
  306. header("location: ".$this->_link(array('timesheet'))."");
  307. }
  308. }
  309. public function changeyear()
  310. {
  311. $form = new changeYearForm();
  312. if($form->wasSubmitted())
  313. {
  314. $form->addData($_POST);
  315. if($form->validate())
  316. {
  317. $data = $form->exportFormData();
  318. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  319. }
  320. else
  321. {
  322. header("location: ".$this->_link(array('timesheet'))."");
  323. }
  324. }
  325. else
  326. {
  327. header("location: ".$this->_link(array('timesheet'))."");
  328. }
  329. }
  330. public function validate($year, $month)
  331. {
  332. $timesheet = new timesheetModel($year,$month);
  333. //Get Current Batch ID
  334. $auth = Staple_Auth::get();
  335. $user = new userModel($auth->getAuthId());
  336. $batchId = $user->getBatchId();
  337. //Check for unvalidated entries within the current pay period.
  338. $i = 0;
  339. foreach($timesheet->getEntries() as $entry)
  340. {
  341. if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
  342. {
  343. if($entry->batchId == $timesheet->getBatch())
  344. {
  345. $i++;
  346. }
  347. }
  348. }
  349. if($i > 0)
  350. {
  351. $this->view->timesheet = $timesheet;
  352. $form = new validateTimeSheetForm();
  353. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  354. if($form->wasSubmitted())
  355. {
  356. if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
  357. {
  358. $timesheet->validate($batchId);
  359. header("location:" . $this->_link(array('timesheet')) . "");
  360. }
  361. }
  362. else
  363. {
  364. $this->view->form = $form;
  365. $this->view->needsValidation = false;
  366. }
  367. }
  368. else
  369. {
  370. $this->view->needsValidation = false;
  371. $this->view->timesheet = array();
  372. }
  373. }
  374. /* TODO REMOVE
  375. public function unlocked()
  376. {
  377. $form = new unlockDatesForm();
  378. if($form->wasSubmitted())
  379. {
  380. $form->addData($_POST);
  381. if($form->validate())
  382. {
  383. $data = $form->exportFormData();
  384. }
  385. else
  386. {
  387. $this->view->form = $form;
  388. }
  389. }
  390. else
  391. {
  392. $this->view->form = $form;
  393. }
  394. }
  395. */
  396. public function admininsert()
  397. {
  398. if($this->accountLevel >= 900)
  399. {
  400. $form = new insertTimeForm();
  401. $form->admin(1);
  402. if($form->wasSubmitted())
  403. {
  404. $form->addData($_POST);
  405. if($form->validate())
  406. {
  407. $data = $form->exportFormData();
  408. //Create a new entry object and set properties
  409. $entry = new timeEntryModel();
  410. $entry->setDate($data['date']);
  411. $entry->setInTime($data['inTime']);
  412. $entry->setOutTime($data['outTime']);
  413. $entry->setLessTime($data['lessTime']);
  414. $entry->setCodeId($data['code']);
  415. $entry->setUserId($data['account']);
  416. $entry->setNote($data['note']);
  417. //Save entry data to table.
  418. if($entry->adminSave())
  419. {
  420. //Return a new time form with success message
  421. $form = new insertTimeForm();
  422. $form->admin(1);
  423. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  424. $this->view->form = $form;
  425. }
  426. else
  427. {
  428. //Return the same form with a warning message
  429. $message = "<i class=\"fa fa-warning\"></i> Administrative action not allowed on your own timesheet.";
  430. $form->errorMessage = array($message);
  431. $this->view->form = $form;
  432. }
  433. }
  434. else
  435. {
  436. $this->view->form = $form;
  437. }
  438. }
  439. else
  440. {
  441. $this->view->form = $form;
  442. }
  443. }
  444. else
  445. {
  446. header("location: ".$this->_link(array('index'))."");
  447. }
  448. }
  449. }
  450. ?>