PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/Requests.php

https://gitlab.com/nicolerey/PPLI-payroll
PHP | 228 lines | 210 code | 18 blank | 0 comment | 22 complexity | 0d56d611fc5c3e9b514c62b026745832 MD5 | raw file
  1. <?php
  2. class Requests extends HR_Controller
  3. {
  4. protected $active_nav;
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('Request_model', 'request');
  9. $this->load->model('Employee_model', 'employee');
  10. }
  11. public function view_all()
  12. {
  13. $extras = [];
  14. $this->active_nav = NAV_VIEW_REQUESTS;
  15. $department_id = $this->employee->get_department(user_id());
  16. $status = $this->input->get('status');
  17. if(!in_array($status, ['p', 'a', 'da'])){
  18. $status = 'p';
  19. }
  20. switch($status){
  21. case 'p':
  22. $extras['request_status'] = 'Pending';
  23. $this->active_subnav = SUBNAV_PENDING_REQUESTS;
  24. break;
  25. case 'a':
  26. $extras['request_status'] = 'Approved';
  27. $this->active_subnav = SUBNAV_APPROVED_REQUESTS;
  28. break;
  29. case 'da':
  30. $extras['request_status'] = 'Discarded';
  31. $this->active_subnav = SUBNAV_DISCARDED_REQUESTS;
  32. break;
  33. }
  34. $extras['items'] = $this->request->get_by_department($department_id, $status);
  35. $this->generate_page('requests/listing', $extras);
  36. }
  37. public function track()
  38. {
  39. $this->active_nav = NAV_TRACK_REQUESTS;
  40. $this->generate_page('requests/listing', [
  41. 'request_status' => 'Track',
  42. 'items' => $this->employee->get_filed_requests(user_id()),
  43. 'edit' => TRUE
  44. ]);
  45. }
  46. public function edit($id)
  47. {
  48. $this->active_nav = NAV_TRACK_REQUESTS;
  49. $this->generate_page('requests/listing', [
  50. 'data' => TRUE
  51. ]);
  52. }
  53. public function view($id = FALSE)
  54. {
  55. if(!$id || !$request = $this->request->get($id)){
  56. show_404();
  57. }
  58. $extras = [];
  59. $this->active_nav = NAV_VIEW_REQUESTS;
  60. switch($request['status']){
  61. case 'p':
  62. $extras['request_status'] = 'Pending';
  63. $this->active_subnav = SUBNAV_PENDING_REQUESTS;
  64. break;
  65. case 'a':
  66. $extras['request_status'] = 'Approved';
  67. $this->active_subnav = SUBNAV_APPROVED_REQUESTS;
  68. break;
  69. case 'da':
  70. $extras['request_status'] = 'Discarded';
  71. $this->active_subnav = SUBNAV_DISCARDED_REQUESTS;
  72. break;
  73. default:
  74. $extras['request_status'] = 'Track';
  75. $this->active_subnav = NAV_TRACK_REQUESTS;
  76. break;
  77. }
  78. $extras['title'] = 'View request';
  79. $extras['data'] = $request;
  80. $this->import_page_script('file-request.js');
  81. $this->generate_page('requests/view', $extras);
  82. }
  83. public function file_request()
  84. {
  85. $this->active_nav = NAV_FILE_REQUEST;
  86. $this->import_plugin_script(['bootstrap-datepicker/js/bootstrap-datepicker.min.js', 'moment.js']);
  87. $this->import_page_script('file-request.js');
  88. $this->generate_page('requests/file-request', [
  89. 'title' => 'File new request',
  90. 'data' => [],
  91. 'sick' => $this->employee->get_remaining_sick_leaves(user_id()),
  92. 'menstruation' => $this->employee->get_remaining_menstruation_leaves(user_id())
  93. ]);
  94. }
  95. public function store()
  96. {
  97. $this->output->set_content_type('json');
  98. $this->_perform_validation(MODE_CREATE);
  99. if(!$this->form_validation->run()){
  100. $this->output->set_output(json_encode([
  101. 'result' => FALSE,
  102. 'messages' => array_values($this->form_validation->error_array())
  103. ]));
  104. return;
  105. }
  106. if($this->request->create($this->_format_data())){
  107. $this->output->set_output(json_encode(['result' => TRUE]));
  108. }else{
  109. $this->output->set_output(json_encode(['result' => FALSE, 'messages' => ['Cannot file request. Please try again later.']]));
  110. }
  111. }
  112. public function update($id = FALSE)
  113. {
  114. $this->output->set_content_type('json');
  115. if(!$id || !$request = $this->request->get($id)){
  116. $this->output->set_output(json_encode([
  117. 'result' => FALSE,
  118. 'messages' => ['Please select a request to update!']
  119. ]));
  120. return;
  121. }
  122. $this->_perform_validation(MODE_EDIT);
  123. if(!$this->form_validation->run()){
  124. $this->output->set_output(json_encode([
  125. 'result' => FALSE,
  126. 'messages' => array_values($this->form_validation->error_array())
  127. ]));
  128. return;
  129. }
  130. $data = [];
  131. if(role_is('sv')){
  132. $data['status'] = $this->input->post('status');
  133. }
  134. if(role_is('re') && $request['status'] === 'a'){
  135. $data['is_acknowledged'] = $this->input->post('is_acknowledged') ? 1 : 0;
  136. }
  137. if($this->request->update($id, $data)){
  138. $this->output->set_output(json_encode(['result' => TRUE]));
  139. }else{
  140. $this->output->set_output(json_encode(['result' => FALSE, 'messages' => ['Cannot update request. Please try again later.']]));
  141. }
  142. }
  143. public function _perform_validation($mode)
  144. {
  145. if($mode === MODE_CREATE){
  146. $this->form_validation->set_rules('date_start', 'date start', 'required|callback__validate_date');
  147. $this->form_validation->set_rules('date_end', 'date end', 'required|callback__validate_date');
  148. $this->form_validation->set_rules('custom_type_name', 'leave title', 'callback__validate_custom_type_name');
  149. $this->form_validation->set_rules('content', 'request content', 'required');
  150. $this->form_validation->set_rules('type', 'request type', 'required|in_list[matpat,vl,o,sl,wml]|callback__validate_leave_type', ['required' => 'Please select a valid leave type.']);
  151. }else{
  152. $this->form_validation->set_rules('status', 'status', 'in_list[a,da,p]|callback__validate_status', ['in_list' => 'Please select a valid %s.']);
  153. }
  154. }
  155. public function _format_data()
  156. {
  157. $this->load->model('Employee_model', 'employee');
  158. $data = [
  159. 'datetime_filed' => date('Y-m-d H:i:s'),
  160. 'date_start' => format_date($this->input->post('date_start')),
  161. 'date_end' => format_date($this->input->post('date_end')),
  162. 'custom_type_name' => $this->input->post('type') === 'o' ? $this->input->post('custom_type_name') : NULL,
  163. 'content' => $this->input->post('content'),
  164. 'type' => $this->input->post('type'),
  165. 'status' => 'p',
  166. 'sender_id' => user_id(),
  167. 'department_id' => $this->employee->get_department(user_id())
  168. ];
  169. if(($data['date_start'] === $data['date_end']) && in_array($this->input->post('halfday'), ['am', 'pm'])){
  170. $data['halfday'] = $this->input->post('halfday');
  171. }
  172. return $data;
  173. }
  174. public function _validate_leave_type($type)
  175. {
  176. $start_leave = date_create($this->input->post('date_start'));
  177. $end_leave = date_create($this->input->post('date_end'));
  178. $duration = date_diff($end_leave, $start_leave)->format('%a')+1;
  179. if(intval($duration) === 1 && in_array($this->input->post('halfday'), ['am', 'pm'])){
  180. $duration = 0.5;
  181. }
  182. if($type === 'sl'){
  183. $remaining = $this->employee->get_remaining_sick_leaves(user_id());
  184. $this->form_validation->set_message('_validate_leave_type', "Not enought credit for sick leave. Remaining: {$remaining}");
  185. return $remaining >= $duration;
  186. }else if($type === 'wml'){
  187. $remaining = $this->employee->get_remaining_menstruation_leaves(user_id());
  188. $this->form_validation->set_message('_validate_leave_type', "Not enought credit for menstruation leave. Remaining: {$remaining}");
  189. return $remaining >= $duration;
  190. }
  191. }
  192. public function _validate_date($val)
  193. {
  194. $this->form_validation->set_message('_validate_date', 'Please input a valid %s');
  195. return is_valid_date($val, 'm/d/Y');
  196. }
  197. public function _validate_custom_type_name($name)
  198. {
  199. $this->form_validation->set_message('_validate_custom_type_name', 'Please fill up the %s');
  200. if($this->input->post('type') === 'o'){
  201. return trim($name);
  202. }
  203. return TRUE;
  204. }
  205. public function _validate_status($val)
  206. {
  207. $this->form_validation->set_message('_validate_status', 'You are not allowed to update requests.');
  208. return trim($val) ? role_is('sv') : TRUE;
  209. }
  210. }