reportsController.php 15 KB

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