reportsController.php 15 KB

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