reportsController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. class reportsController extends Staple_Controller
  3. {
  4. private $authLevel;
  5. private $uid;
  6. public function _start()
  7. {
  8. $this->_setLayout('main');
  9. $auth = Staple_Auth::get();
  10. $this->authLevel = $auth->getAuthLevel();
  11. $user = new userModel();
  12. $this->uid = $user->getId();
  13. if ($this->authLevel < 500) {
  14. header("location:" . $this->_link(array('index', 'index')) . "");
  15. }
  16. }
  17. public function index($year = null, $month = null)
  18. {
  19. if ($year == null)
  20. {
  21. $date = new DateTime();
  22. if($date->format('d') >= 26)
  23. {
  24. $date->modify('+1 month');
  25. }
  26. $year = $date->format('Y');
  27. }
  28. if ($month == null)
  29. {
  30. $date = new DateTime();
  31. if($date->format('d') >= 26)
  32. {
  33. $date->modify('+1 month');
  34. }
  35. $month = $date->format('m');
  36. }
  37. $date = new DateTime();
  38. $date->setDate($year,$month,26);
  39. $date->setTime(0,0,0);
  40. $this->view->year = $date->format('Y');
  41. $this->view->date = $date->format("F Y");
  42. $date->modify('+1 year');
  43. $this->view->nextYear = $date->format('Y');
  44. $date->modify('-2 year');
  45. $this->view->previousYear = $date->format('Y');
  46. $date->modify('+1 year');
  47. $month = $date->format('m');
  48. $this->view->month = $month;
  49. $date->modify('-1 month');
  50. $this->view->previousMonth = $date->format('m');
  51. $date->modify('+2 month');
  52. $this->view->nextMonth = $date->format('m');
  53. $report = new reportModel($year, $month);
  54. $this->view->report = $report->getTimesheets();
  55. $reviewed = new reviewModel();
  56. $this->view->reviewed = $reviewed->load($year, $month);
  57. $this->view->accountLevel = $this->authLevel;
  58. $date = new DateTime();
  59. if($date->format('d') >= 26)
  60. {
  61. $date->modify('+1 month');
  62. }
  63. $date->setDate($year, $month, 1);
  64. $this->view->monthName = $date->format('F');
  65. $printActiveTimeSheetForm = new printActiveTimeSheetForm();
  66. $printActiveTimeSheetForm->setAction($this->_link(array("reports",$year,$month)));
  67. if($printActiveTimeSheetForm->wasSubmitted())
  68. {
  69. $printActiveTimeSheetForm->addData($_POST);
  70. if($printActiveTimeSheetForm->validate())
  71. {
  72. $data = $printActiveTimeSheetForm->exportFormData();
  73. $this->layout->addScriptBlock("
  74. window.open('".$this->_link(array("reports","printpreview",$year,$month,$data['account']))."');
  75. ");
  76. $this->view->printTimeSheetForm = $printActiveTimeSheetForm;
  77. }
  78. else
  79. {
  80. $this->view->printTimeSheetForm = $printActiveTimeSheetForm;
  81. }
  82. }
  83. else
  84. {
  85. $this->view->printTimeSheetForm = $printActiveTimeSheetForm;
  86. }
  87. }
  88. public function changeyear()
  89. {
  90. $form = new changeYearForm();
  91. if($form->wasSubmitted())
  92. {
  93. $form->addData($_POST);
  94. if($form->validate())
  95. {
  96. $data = $form->exportFormData();
  97. header("location: ".$this->_link(array('reports',$data['year']))."");
  98. }
  99. else
  100. {
  101. header("location: ".$this->_link(array('reports'))."");
  102. }
  103. }
  104. else
  105. {
  106. header("location: ".$this->_link(array('reports'))."");
  107. }
  108. }
  109. public function weekly()
  110. {
  111. //Weekly report form
  112. $form = new weeklyReportForm();
  113. if ($form->wasSubmitted()) {
  114. $form->addData($_POST);
  115. if ($form->validate()) {
  116. $data = $form->exportFormData();
  117. $report = new weeklyReportModel();
  118. $this->view->report = $report->timeWorked($data['account'], $data['year']);
  119. $account = new userModel();
  120. $this->view->account = $account->userInfo($data['account']);
  121. $this->view->year = $data['year'];
  122. } else {
  123. $this->view->form = $form;
  124. }
  125. } else {
  126. $this->view->form = $form;
  127. }
  128. }
  129. public function unlock()
  130. {
  131. $auth = Staple_Auth::get();
  132. $this->authLevel = $auth->getAuthLevel();
  133. if ($this->authLevel < 900)
  134. {
  135. header("location:" . $this->_link(array('index', 'index')) . "");
  136. }
  137. else
  138. {
  139. $date = new DateTime();
  140. if($date->format('d') >= 26)
  141. {
  142. $date->modify('+1 month');
  143. }
  144. $timesheets = new reportModel($date->format('Y'), $date->format('m'));
  145. $this->view->accounts = $timesheets;
  146. $this->view->dateTitle = $date->format('F')." ".$date->format('Y');
  147. }
  148. }
  149. public function unlockid($id)
  150. {
  151. $auth = Staple_Auth::get();
  152. $this->authLevel = $auth->getAuthLevel();
  153. if ($this->authLevel < 900)
  154. {
  155. header("location:" . $this->_link(array('index', 'index')) . "");
  156. }
  157. else
  158. {
  159. $unlock = new unlockModel();
  160. if ($unlock->unlock($id))
  161. {
  162. $this->view->message = "<i class='fa fa-check'></i> Time entry unlocked.";
  163. }
  164. else
  165. {
  166. $this->view->message = "<i class='fa fa-close'></i> ERROR: Unable to unlock your own time entries.";
  167. }
  168. }
  169. }
  170. public function printpreview($year,$month,$uid)
  171. {
  172. $this->_setLayout('print');
  173. $user = new userModel();
  174. $account = $user->userInfo($uid);
  175. $this->view->firstName = $account['firstName'];
  176. $this->view->lastName = $account['lastName'];
  177. $this->view->batchId = $account['batchId'];
  178. $this->view->year = $year;
  179. $this->view->month = date('F',$month);
  180. $timesheet = new timesheetModel($year, $month,$uid);
  181. $this->view->timesheet = $timesheet;
  182. }
  183. public function payperiod($year = null, $month = null)
  184. {
  185. if ($year == null) {
  186. $year = date('Y');
  187. }
  188. if ($month == null) {
  189. $month = date('m');
  190. }
  191. $date = new DateTime();
  192. $date->setDate($year,$month,26);
  193. $date->setTime(0,0,0);
  194. $year = $date->format('Y');
  195. $this->view->year = $year;
  196. $nextYear = $date->modify('+1 year')->format('Y');
  197. $this->view->nextYear = $nextYear;
  198. $previousYear = $date->modify('-2 year')->format('Y');
  199. $this->view->previousYear = $previousYear;
  200. $month = $date->format('m');
  201. $this->view->month = $month;
  202. $nextMonth = $date->modify('+1 month')->format('m');
  203. $this->view->nextMonth = $nextMonth;
  204. $previousMonth = $date->modify('-2 month')->format('m');
  205. $this->view->previousMonth = $previousMonth;
  206. $date->setDate($year,$month,26);
  207. $date->setTime(0,0,0);
  208. $newDate = new DateTime();
  209. switch($month)
  210. {
  211. case 1:
  212. $newDate->setDate($previousYear,$previousMonth,26);
  213. break;
  214. default:
  215. $newDate->setDate($year,$previousMonth,26);
  216. }
  217. $newDate->setTime(0,0,0);
  218. $date2 = new DateTime();
  219. $date2->setDate($year,$month,25);
  220. $date2->setTime(24,00,00);
  221. $interval = date_diff($newDate,$date2);
  222. $span = $interval->days;
  223. $this->view->span = $span;
  224. $this->view->date = $date->format("F Y");
  225. $date = new dateTime();
  226. $date->setDate($year, $month, 25);
  227. $this->view->currentDate = $date->format('Y-m-d');
  228. $this->view->previousDate = $date->modify('-1 month +1 day')->format('Y-m-d');
  229. $reports = new reportModel($year, $month);
  230. $this->view->report = $reports->payPeriodTotals($year, $month);
  231. }
  232. public function payperiodprint($year, $month)
  233. {
  234. if($year != null || $month != null)
  235. {
  236. $this->_setLayout('print');
  237. $this->view->year = $year;
  238. $date = new DateTime();
  239. $date->setDate($year,$month,26);
  240. $date->setTime(0,0,0);
  241. $this->view->month = $date->format('m');
  242. $date->modify('-1 month');
  243. $this->view->previousMonth = $date->format('m');
  244. $date2 = new DateTime();
  245. $date2->setDate($year,$month,25);
  246. $date2->setTime(24,0,0);
  247. $interval = date_diff($date,$date2);
  248. $this->view->span = $interval->days;
  249. $reports = new reportModel($year, $month);
  250. $this->view->report = $reports->payPeriodTotals($year, $month);
  251. $this->view->startDate = $date->format("F jS Y");
  252. $days = $interval->days - 1;
  253. $date->modify("+$days days");
  254. $this->view->endDate = $date->format("F jS Y");
  255. }
  256. else
  257. {
  258. header("location:".$this->_link(array('reports','payperiod'))."");
  259. }
  260. }
  261. public function payroll($year = null, $month = null)
  262. {
  263. if($year == null)
  264. {
  265. $year = date('Y');
  266. }
  267. if($month == null)
  268. {
  269. $month = date('m');
  270. }
  271. $this->view->year = $year;
  272. $date = new DateTime();
  273. $date->setDate($year,$month,26);
  274. $date->setTime(0,0,0);
  275. $this->view->date = $date->format("F Y");
  276. $date->modify('+1 year');
  277. $this->view->nextYear = $date->format('Y');
  278. $date->modify('-2 year');
  279. $this->view->previousYear = $date->format('Y');
  280. $this->view->month = $date->format('m');
  281. $date->modify('-1 month');
  282. $this->view->previousMonth = $date->format('m');
  283. $date->modify('+2 month');
  284. $this->view->nextMonth = $date->format('m');
  285. $date2 = new DateTime();
  286. $date2->setDate($year,$month,25);
  287. $date2->setTime(24,0,0);
  288. $interval = date_diff($date,$date2);
  289. $this->view->span = $interval->days;
  290. $reports = new reportModel($year, $month);
  291. $this->view->report = $reports->payroll($year, $month);
  292. $this->view->startDate = $date->format("F jS Y");
  293. $days = $interval->days - 1;
  294. $date->modify("+$days days");
  295. $this->view->endDate = $date->format("F jS Y");
  296. $date = new dateTime();
  297. $date->setDate($year, $month, 25);
  298. $this->view->currentDate = $date->format('Y-m-d');
  299. $this->view->previousDate = $date->modify('-1 month +1 day')->format('Y-m-d');
  300. $codes = new codeModel();
  301. $this->view->codes = $codes->allCodes();
  302. }
  303. public function payrollprint($year = null, $month = null)
  304. {
  305. if($year != null || $month != null) {
  306. $this->_setLayout('print');
  307. if ($year == null) {
  308. $year = date('Y');
  309. }
  310. if ($month == null) {
  311. $month = date('m');
  312. }
  313. $this->view->year = $year;
  314. $date = new DateTime();
  315. $date->setDate($year, $month, 26);
  316. $date->setTime(0, 0, 0);
  317. $this->view->month = $date->format('m');
  318. $date->modify('-1 month');
  319. $this->view->previousMonth = $date->format('m');
  320. $date2 = new DateTime();
  321. $date2->setDate($year, $month, 25);
  322. $date2->setTime(24, 0, 0);
  323. $interval = date_diff($date, $date2);
  324. $this->view->span = $interval->days;
  325. $reports = new reportModel($year, $month);
  326. $this->view->report = $reports->payroll($year, $month);
  327. $this->view->startDate = $date->format("F jS Y");
  328. $days = $interval->days - 1;
  329. $date->modify("+$days days");
  330. $this->view->endDate = $date->format("F jS Y");
  331. $codes = new codeModel();
  332. $this->view->codes = $codes->allCodes();
  333. }
  334. else
  335. {
  336. header("location:".$this->_link(array('reports','payroll'))."");
  337. }
  338. }
  339. public function inactive($year = null, $month = null)
  340. {
  341. if ($year == null)
  342. {
  343. $date = new DateTime();
  344. if($date->format('d') >= 26)
  345. {
  346. $date->modify('+1 month');
  347. }
  348. $year = $date->format('Y');
  349. }
  350. if ($month == null)
  351. {
  352. $date = new DateTime();
  353. if($date->format('d') >= 26)
  354. {
  355. $date->modify('+1 month');
  356. }
  357. $month = $date->format('m');
  358. }
  359. $date = new DateTime();
  360. $date->setDate($year,$month,26);
  361. $date->setTime(0,0,0);
  362. $this->view->year = $date->format('Y');
  363. $this->view->date = $date->format("F Y");
  364. $date->modify('+1 year');
  365. $this->view->nextYear = $date->format('Y');
  366. $date->modify('-2 year');
  367. $this->view->previousYear = $date->format('Y');
  368. $date->modify('+1 year');
  369. $month = $date->format('m');
  370. $this->view->month = $month;
  371. $date->modify('-1 month');
  372. $this->view->previousMonth = $date->format('m');
  373. $date->modify('+2 month');
  374. $this->view->nextMonth = $date->format('m');
  375. $report = new reportModel($year, $month,1);
  376. $this->view->report = $report->getTimesheets();
  377. $this->view->accountLevel = $this->authLevel;
  378. $date = new DateTime();
  379. if($date->format('d') >= 26)
  380. {
  381. $date->modify('+1 month');
  382. }
  383. $date->setDate($year, $month, 1);
  384. $this->view->monthName = $date->format('F');
  385. $printInactiveTimeSheetForm = new printInactiveTimeSheetForm();
  386. $printInactiveTimeSheetForm->setAction($this->_link(array("reports","inactive",$year,$month)));
  387. if($printInactiveTimeSheetForm->wasSubmitted())
  388. {
  389. $printInactiveTimeSheetForm->addData($_POST);
  390. if($printInactiveTimeSheetForm->validate())
  391. {
  392. $data = $printInactiveTimeSheetForm->exportFormData();
  393. $this->layout->addScriptBlock("
  394. window.open('".$this->_link(array("reports","inactive","printpreview",$year,$month,$data['account']))."');
  395. ");
  396. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  397. }
  398. else
  399. {
  400. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  401. }
  402. }
  403. else
  404. {
  405. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  406. }
  407. }
  408. public function reviewed($userId = null, $year = null, $month = null)
  409. {
  410. if($userId != null || $year != null || $month != null)
  411. {
  412. $review = new reviewModel();
  413. $review->setAccountId($userId);
  414. $review->setPayPeriodMonth($month);
  415. $review->setPayPeriodYear($year);
  416. if($review->save())
  417. {
  418. header("location: ".$this->_link(array('reports',$year,$month,$userId))."");
  419. }
  420. else
  421. {
  422. $this->view->year = $year;
  423. $this->view->month = $month;
  424. $this->view->errorMessage = "Entry already marked as reviewed.";
  425. }
  426. }
  427. else
  428. {
  429. header("location: ".$this->_link(array('reports',$year,$month,$userId))."");
  430. }
  431. }
  432. }