reportsController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. $this->_setLayout('print');
  235. $date = new DateTime();
  236. $date->setDate($year,$month,26);
  237. $date->setTime(0,0,0);
  238. $year = $date->format('Y');
  239. $this->view->year = $year;
  240. $nextYear = $date->modify('+1 year')->format('Y');
  241. $this->view->nextYear = $nextYear;
  242. $previousYear = $date->modify('-2 year')->format('Y');
  243. $this->view->previousYear = $previousYear;
  244. $month = $date->format('m');
  245. $this->view->month = $month;
  246. $nextMonth = $date->modify('+1 month')->format('m');
  247. $this->view->nextMonth = $nextMonth;
  248. $previousMonth = $date->modify('-2 month')->format('m');
  249. $this->view->previousMonth = $previousMonth;
  250. $date->setDate($year,$month,26);
  251. $date->setTime(0,0,0);
  252. $newDate = new DateTime();
  253. switch($month)
  254. {
  255. case 1:
  256. $newDate->setDate($previousYear,$previousMonth,26);
  257. break;
  258. default:
  259. $newDate->setDate($year,$previousMonth,26);
  260. }
  261. $newDate->setTime(0,0,0);
  262. $date2 = new DateTime();
  263. $date2->setDate($year,$month,25);
  264. $date2->setTime(24,00,00);
  265. $interval = date_diff($newDate,$date2);
  266. $span = $interval->days;
  267. $this->view->span = $span;
  268. $this->view->date = $date->format("F Y");
  269. $date = new dateTime();
  270. $date->setDate($year, $month, 25);
  271. $this->view->currentDate = $date->format('Y-m-d');
  272. $this->view->previousDate = $date->modify('-1 month +1 day')->format('Y-m-d');
  273. $reports = new reportModel($year, $month);
  274. $this->view->report = $reports->payPeriodTotals($year, $month);
  275. }
  276. public function payroll($year = null, $month = null)
  277. {
  278. if($year == null)
  279. {
  280. $year = date('Y');
  281. }
  282. if($month == null)
  283. {
  284. $month = date('m');
  285. }
  286. $this->view->year = $year;
  287. $date = new DateTime();
  288. $date->setDate($year,$month,26);
  289. $date->setTime(0,0,0);
  290. $this->view->date = $date->format("F Y");
  291. $date->modify('+1 year');
  292. $this->view->nextYear = $date->format('Y');
  293. $date->modify('-2 year');
  294. $this->view->previousYear = $date->format('Y');
  295. $this->view->month = $date->format('m');
  296. $date->modify('-1 month');
  297. $this->view->previousMonth = $date->format('m');
  298. $date->modify('+2 month');
  299. $this->view->nextMonth = $date->format('m');
  300. $date2 = new DateTime();
  301. $date2->setDate($year,$month,25);
  302. $date2->setTime(24,0,0);
  303. $interval = date_diff($date,$date2);
  304. $this->view->span = $interval->days;
  305. $reports = new reportModel($year, $month);
  306. $this->view->report = $reports->payroll($year, $month);
  307. $this->view->startDate = $date->format("F jS Y");
  308. $days = $interval->days - 1;
  309. $date->modify("+$days days");
  310. $this->view->endDate = $date->format("F jS Y");
  311. $date = new dateTime();
  312. $date->setDate($year, $month, 25);
  313. $this->view->currentDate = $date->format('Y-m-d');
  314. $this->view->previousDate = $date->modify('-1 month +1 day')->format('Y-m-d');
  315. $codes = new codeModel();
  316. $this->view->codes = $codes->allCodes();
  317. }
  318. public function payrollprint($year = null, $month = null)
  319. {
  320. if($year != null || $month != null) {
  321. $this->_setLayout('print');
  322. if ($year == null) {
  323. $year = date('Y');
  324. }
  325. if ($month == null) {
  326. $month = date('m');
  327. }
  328. $this->view->year = $year;
  329. $date = new DateTime();
  330. $date->setDate($year, $month, 26);
  331. $date->setTime(0, 0, 0);
  332. $this->view->month = $date->format('m');
  333. $date->modify('-1 month');
  334. $this->view->previousMonth = $date->format('m');
  335. $date2 = new DateTime();
  336. $date2->setDate($year, $month, 25);
  337. $date2->setTime(24, 0, 0);
  338. $interval = date_diff($date, $date2);
  339. $this->view->span = $interval->days;
  340. $reports = new reportModel($year, $month);
  341. $this->view->report = $reports->payroll($year, $month);
  342. $this->view->startDate = $date->format("F jS Y");
  343. $days = $interval->days - 1;
  344. $date->modify("+$days days");
  345. $this->view->endDate = $date->format("F jS Y");
  346. $codes = new codeModel();
  347. $this->view->codes = $codes->allCodes();
  348. }
  349. else
  350. {
  351. header("location:".$this->_link(array('reports','payroll'))."");
  352. }
  353. }
  354. public function inactive($year = null, $month = null)
  355. {
  356. if ($year == null)
  357. {
  358. $date = new DateTime();
  359. if($date->format('d') >= 26)
  360. {
  361. $date->modify('+1 month');
  362. }
  363. $year = $date->format('Y');
  364. }
  365. if ($month == null)
  366. {
  367. $date = new DateTime();
  368. if($date->format('d') >= 26)
  369. {
  370. $date->modify('+1 month');
  371. }
  372. $month = $date->format('m');
  373. }
  374. $date = new DateTime();
  375. $date->setDate($year,$month,26);
  376. $date->setTime(0,0,0);
  377. $this->view->year = $date->format('Y');
  378. $this->view->date = $date->format("F Y");
  379. $date->modify('+1 year');
  380. $this->view->nextYear = $date->format('Y');
  381. $date->modify('-2 year');
  382. $this->view->previousYear = $date->format('Y');
  383. $date->modify('+1 year');
  384. $month = $date->format('m');
  385. $this->view->month = $month;
  386. $date->modify('-1 month');
  387. $this->view->previousMonth = $date->format('m');
  388. $date->modify('+2 month');
  389. $this->view->nextMonth = $date->format('m');
  390. $report = new reportModel($year, $month,1);
  391. $this->view->report = $report->getTimesheets();
  392. $this->view->accountLevel = $this->authLevel;
  393. $date = new DateTime();
  394. if($date->format('d') >= 26)
  395. {
  396. $date->modify('+1 month');
  397. }
  398. $date->setDate($year, $month, 1);
  399. $this->view->monthName = $date->format('F');
  400. $printInactiveTimeSheetForm = new printInactiveTimeSheetForm();
  401. $printInactiveTimeSheetForm->setAction($this->_link(array("reports","inactive",$year,$month)));
  402. if($printInactiveTimeSheetForm->wasSubmitted())
  403. {
  404. $printInactiveTimeSheetForm->addData($_POST);
  405. if($printInactiveTimeSheetForm->validate())
  406. {
  407. $data = $printInactiveTimeSheetForm->exportFormData();
  408. $this->layout->addScriptBlock("
  409. window.open('".$this->_link(array("reports","inactive","printpreview",$year,$month,$data['account']))."');
  410. ");
  411. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  412. }
  413. else
  414. {
  415. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  416. }
  417. }
  418. else
  419. {
  420. $this->view->printTimeSheetForm = $printInactiveTimeSheetForm;
  421. }
  422. }
  423. public function reviewed($userId = null, $year = null, $month = null)
  424. {
  425. if($userId != null || $year != null || $month != null)
  426. {
  427. $review = new reviewModel();
  428. $review->setAccountId($userId);
  429. $review->setPayPeriodMonth($month);
  430. $review->setPayPeriodYear($year);
  431. if($review->save())
  432. {
  433. header("location: ".$this->_link(array('reports',$year,$month,$userId))."");
  434. }
  435. else
  436. {
  437. $this->view->year = $year;
  438. $this->view->month = $month;
  439. $this->view->errorMessage = "Entry already marked as reviewed.";
  440. }
  441. }
  442. else
  443. {
  444. header("location: ".$this->_link(array('reports',$year,$month,$userId))."");
  445. }
  446. }
  447. }