timesheetController.php 16 KB

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