reportsController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. class reportsController extends Staple_Controller
  3. {
  4. private $authLevel;
  5. private $uid;
  6. public function _start()
  7. {
  8. $auth = Staple_Auth::get();
  9. $this->authLevel = $auth->getAuthLevel();
  10. $user = new userModel();
  11. $this->uid = $user->getId();
  12. if ($this->authLevel < 500) {
  13. header("location:" . $this->_link(array('index', 'index')) . "");
  14. }
  15. }
  16. public function index($year = null, $month = null)
  17. {
  18. if ($year == null) {
  19. $year = date('Y');
  20. }
  21. if ($month == null) {
  22. $month = date('m');
  23. }
  24. $report = new reportModel($year, $month);
  25. $this->view->report = $report->getTimesheets();
  26. $timesheet = new timesheetModel($year, $month);
  27. $this->view->nextMonth = $timesheet->getNextMonth();
  28. $this->view->previousMonth = $timesheet->getPreviousMonth();
  29. $this->view->year = $timesheet->getCurrentYear();
  30. $yearForm = new changeYearForm();
  31. $yearForm->setAction($this->_link(array('reports','changeyear')));
  32. $this->view->yearForm = $yearForm;
  33. $this->view->accountLevel = $this->authLevel;
  34. $date = new DateTime();
  35. $date->setDate($year, $month, 1);
  36. $this->view->month = $date->format('F');
  37. $printTimeSheetForm = new printTimeSheetForm();
  38. if($printTimeSheetForm->wasSubmitted())
  39. {
  40. $printTimeSheetForm->addData($_POST);
  41. if($printTimeSheetForm->validate())
  42. {
  43. $data = $printTimeSheetForm->exportFormData();
  44. header("location: ".$this->_link(array('reports','printpreview',$year,$month,$data['account']))."");
  45. }
  46. else
  47. {
  48. $this->view->printTimeSheetForm = $printTimeSheetForm;
  49. }
  50. }
  51. else
  52. {
  53. $this->view->printTimeSheetForm = $printTimeSheetForm;
  54. }
  55. }
  56. public function changeyear()
  57. {
  58. $form = new changeYearForm();
  59. if($form->wasSubmitted())
  60. {
  61. $form->addData($_POST);
  62. if($form->validate())
  63. {
  64. $data = $form->exportFormData();
  65. header("location: ".$this->_link(array('reports',$data['year']))."");
  66. }
  67. else
  68. {
  69. header("location: ".$this->_link(array('reports'))."");
  70. }
  71. }
  72. else
  73. {
  74. header("location: ".$this->_link(array('reports'))."");
  75. }
  76. }
  77. public function weekly()
  78. {
  79. //Weekly report form
  80. $form = new weeklyReportForm();
  81. if ($form->wasSubmitted()) {
  82. $form->addData($_POST);
  83. if ($form->validate()) {
  84. $data = $form->exportFormData();
  85. $report = new weeklyReportModel();
  86. $this->view->report = $report->timeWorked($data['account'], $data['year']);
  87. $account = new userModel();
  88. $this->view->account = $account->userInfo($data['account']);
  89. $this->view->year = $data['year'];
  90. } else {
  91. $this->view->form = $form;
  92. }
  93. } else {
  94. $this->view->form = $form;
  95. }
  96. }
  97. public function unlock()
  98. {
  99. $auth = Staple_Auth::get();
  100. $this->authLevel = $auth->getAuthLevel();
  101. if ($this->authLevel < 900)
  102. {
  103. header("location:" . $this->_link(array('index', 'index')) . "");
  104. }
  105. else
  106. {
  107. $year = date('Y');
  108. $month = date('m');
  109. $timesheets = new reportModel($year, $month);
  110. $this->view->accounts = $timesheets;
  111. }
  112. }
  113. public function unlockid($id)
  114. {
  115. $auth = Staple_Auth::get();
  116. $this->authLevel = $auth->getAuthLevel();
  117. if ($this->authLevel < 900)
  118. {
  119. header("location:" . $this->_link(array('index', 'index')) . "");
  120. }
  121. else
  122. {
  123. $unlock = new unlockModel();
  124. if ($unlock->unlock($id))
  125. {
  126. $this->view->message = "<i class='fa fa-check'></i> Time entry unlocked.";
  127. }
  128. else
  129. {
  130. $this->view->message = "<i class='fa fa-close'></i> ERROR: Unable to unlock your own time entries.";
  131. }
  132. }
  133. }
  134. public function printpreview($year,$month,$uid)
  135. {
  136. $report = new reportModel($year,$month);
  137. $user = new userModel();
  138. $account = $user->userInfo($uid);
  139. $userName = $account['lastName'].", ".$account['firstName'];
  140. $data = array();
  141. foreach($report->timesheets as $account => $entry)
  142. {
  143. if($userName == $account)
  144. {
  145. foreach($entry as $key=>$value)
  146. {
  147. if($value['code'] == 'Normal')
  148. {
  149. if(array_key_exists($value['date'],$data))
  150. {
  151. $data[$value['date']]['normal'] = $data[$value['date']]['normal'] + $value['timeWorked'];
  152. }
  153. else
  154. {
  155. $data[$value['date']]['normal'] = $value['timeWorked'];
  156. }
  157. }
  158. if($value['code'] == 'Sick')
  159. {
  160. if(array_key_exists($value['date'],$data))
  161. {
  162. $data[$value['date']]['sick'] = $data[$value['date']]['sick'] + $value['timeWorked'];
  163. }
  164. else
  165. {
  166. $data[$value['date']]['sick'] = $value['timeWorked'];
  167. }
  168. }
  169. if($value['code'] == 'Vacation')
  170. {
  171. if(array_key_exists($value['date'],$data))
  172. {
  173. $data[$value['date']]['vacation'] = $data[$value['date']]['vacation'] + $value['timeWorked'];
  174. }
  175. else
  176. {
  177. $data[$value['date']]['vacation'] = $value['timeWorked'];
  178. }
  179. }
  180. }
  181. }
  182. }
  183. $this->view->data = $data;
  184. }
  185. }