| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\HRM; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handle the form submissions |
| 7 |
* |
| 8 |
* Although our most of the forms uses ajax and popup, some |
| 9 |
* are needed to submit via regular form submits. This class |
| 10 |
* Handles those form submission in this module |
| 11 |
* |
| 12 |
* @package WP ERP |
| 13 |
* @subpackage HRM |
| 14 |
*/ |
| 15 |
class Form_Handler { |
| 16 |
|
| 17 |
/** |
| 18 |
* Hook 'em all |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action( 'erp_action_hr-leave-assign-policy', array( $this, 'leave_entitlement' ) ); |
| 22 |
add_action( 'erp_action_hr-leave-req-new', array( $this, 'leave_request' ) ); |
| 23 |
|
| 24 |
// permission |
| 25 |
add_action( 'erp_action_erp-hr-employee-permission', array( $this, 'employee_permission' ) ); |
| 26 |
|
| 27 |
add_action( 'admin_init', array( $this, 'leave_request_status_change' ) ); |
| 28 |
add_action( 'admin_init', array( $this, 'handle_employee_status_update' ) ); |
| 29 |
add_action( 'admin_init', array( $this, 'handle_leave_calendar_filter' ) ); |
| 30 |
|
| 31 |
$hr_management = sanitize_title( __( 'HR Management', 'erp' ) ); |
| 32 |
add_action( "load-{$hr_management}_page_erp-hr-employee", array( $this, 'employee_bulk_action' ) ); |
| 33 |
add_action( "load-{$hr_management}_page_erp-hr-designation", array( $this, 'designation_bulk_action' ) ); |
| 34 |
add_action( "load-{$hr_management}_page_erp-hr-depts", array( $this, 'department_bulk_action' ) ); |
| 35 |
add_action( "load-{$hr_management}_page_erp-hr-reporting", array( $this, 'reporting_bulk_action' ) ); |
| 36 |
|
| 37 |
$leave = sanitize_title( __( 'Leave', 'erp' ) ); |
| 38 |
add_action( 'load-toplevel_page_erp-leave', array( $this, 'leave_request_bulk_action' ) ); |
| 39 |
add_action( "load-{$leave}_page_erp-leave-assign", array( $this, 'entitlement_bulk_action' ) ); |
| 40 |
add_action( "load-{$leave}_page_erp-holiday-assign", array( $this, 'holiday_action' ) ); |
| 41 |
add_action( "load-{$leave}_page_erp-leave-policies", array( $this, 'leave_policies' ) ); |
| 42 |
add_action( "load-leaves_page_erp-hr-reporting", array( $this, 'reporting_leaves_bulk_action' ) ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Hnadle leave calendar filter |
| 48 |
* |
| 49 |
* @since 0.1 |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function handle_leave_calendar_filter() { |
| 54 |
if ( ! isset( $_POST['erp_leave_calendar_filter'] ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
$designation = isset( $_POST['designation'] ) ? $_POST['designation'] : ''; |
| 58 |
$department = isset( $_POST['department'] ) ? $_POST['department'] : ''; |
| 59 |
$url = admin_url( "admin.php?page=erp-leave-calendar&designation=$designation&department=$department" ); |
| 60 |
wp_redirect( $url ); |
| 61 |
exit(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check is current page actions |
| 66 |
* |
| 67 |
* @since 0.1 |
| 68 |
* |
| 69 |
* @param integer $page_id |
| 70 |
* @param integer $bulk_action |
| 71 |
* |
| 72 |
* @return boolean |
| 73 |
*/ |
| 74 |
public function verify_current_page_screen( $page_id, $bulk_action ) { |
| 75 |
|
| 76 |
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! isset( $_GET['page'] ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
if ( $_GET['page'] != $page_id ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $bulk_action ) ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Handle leave policies bulk action |
| 93 |
* |
| 94 |
* @since 0.1 |
| 95 |
* |
| 96 |
* @return void [redirection] |
| 97 |
*/ |
| 98 |
public function leave_policies() { |
| 99 |
// Check nonce validaion |
| 100 |
if ( ! $this->verify_current_page_screen( 'erp-leave-policies', 'bulk-leave_policies' ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// Check permission |
| 105 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 106 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'trash' ) { |
| 110 |
|
| 111 |
if ( isset( $_POST['policy_id'] ) ) { |
| 112 |
erp_hr_leave_policy_delete( $_POST['policy_id'] ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Handle entitlement bulk actions |
| 121 |
* |
| 122 |
* @since 0.1 |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function entitlement_bulk_action() { |
| 127 |
if ( ! $this->verify_current_page_screen( 'erp-leave-assign', 'bulk-entitlements' ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// Check permission |
| 132 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 133 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
$employee_table = new \WeDevs\ERP\HRM\Entitlement_List_Table(); |
| 137 |
$action = $employee_table->current_action(); |
| 138 |
|
| 139 |
if ( $action ) { |
| 140 |
$redirect = remove_query_arg( array( |
| 141 |
'_wp_http_referer', |
| 142 |
'_wpnonce', |
| 143 |
'filter_entitlement' |
| 144 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 145 |
|
| 146 |
if ( $action == 'filter_entitlement' ) { |
| 147 |
wp_redirect( $redirect ); |
| 148 |
exit(); |
| 149 |
} |
| 150 |
|
| 151 |
if ( $action == 'entitlement_delete' ) { |
| 152 |
if ( isset( $_GET['entitlement_id'] ) && ! empty( $_GET['entitlement_id'] ) ) { |
| 153 |
foreach ( $_GET['entitlement_id'] as $key => $ent_id ) { |
| 154 |
$entitlement_data = \WeDevs\ERP\HRM\Models\Leave_Entitlement::select( 'user_id', 'policy_id' )->find( $ent_id )->toArray(); |
| 155 |
erp_hr_delete_entitlement( $ent_id, $entitlement_data['user_id'], $entitlement_data['policy_id'] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
wp_redirect( $redirect ); |
| 160 |
exit(); |
| 161 |
|
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Leave request bulk actions |
| 168 |
* |
| 169 |
* @since 1.0 |
| 170 |
* |
| 171 |
* @return void redirect |
| 172 |
*/ |
| 173 |
public function leave_request_bulk_action() { |
| 174 |
// Check nonce validaion |
| 175 |
if ( ! $this->verify_current_page_screen( 'erp-leave', 'bulk-leaves' ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
// Check permission |
| 180 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 181 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 182 |
} |
| 183 |
|
| 184 |
$leave_request_table = new \WeDevs\ERP\HRM\Leave_Requests_List_Table(); |
| 185 |
$action = $leave_request_table->current_action(); |
| 186 |
|
| 187 |
if ( $action ) { |
| 188 |
|
| 189 |
$redirect = remove_query_arg( array( |
| 190 |
'_wp_http_referer', |
| 191 |
'_wpnonce', |
| 192 |
'action', |
| 193 |
'action2', |
| 194 |
'paged', |
| 195 |
'filter_by_year' |
| 196 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 197 |
|
| 198 |
switch ( $action ) { |
| 199 |
|
| 200 |
case 'delete' : |
| 201 |
|
| 202 |
if ( isset( $_GET['request_id'] ) && ! empty( $_GET['request_id'] ) ) { |
| 203 |
foreach ( $_GET['request_id'] as $key => $request_id ) { |
| 204 |
\WeDevs\ERP\HRM\Models\Leave_request::find( $request_id )->delete(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
wp_redirect( $redirect ); |
| 209 |
exit(); |
| 210 |
|
| 211 |
case 'approved' : |
| 212 |
if ( isset( $_GET['request_id'] ) && ! empty( $_GET['request_id'] ) ) { |
| 213 |
foreach ( $_GET['request_id'] as $key => $request_id ) { |
| 214 |
erp_hr_leave_request_update_status( $request_id, 1 ); |
| 215 |
|
| 216 |
$approved_email = wperp()->emailer->get_email( 'Approved_Leave_Request' ); |
| 217 |
|
| 218 |
if ( is_a( $approved_email, '\WeDevs\ERP\Email' ) ) { |
| 219 |
$approved_email->trigger( $request_id ); |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
wp_redirect( $redirect ); |
| 226 |
exit(); |
| 227 |
|
| 228 |
case 'reject' : |
| 229 |
if ( isset( $_GET['request_id'] ) && ! empty( $_GET['request_id'] ) ) { |
| 230 |
foreach ( $_GET['request_id'] as $key => $request_id ) { |
| 231 |
erp_hr_leave_request_update_status( $request_id, 3 ); |
| 232 |
|
| 233 |
$rejected_email = wperp()->emailer->get_email( 'Rejected_Leave_Request' ); |
| 234 |
|
| 235 |
if ( is_a( $rejected_email, '\WeDevs\ERP\Email' ) ) { |
| 236 |
$rejected_email->trigger( $request_id ); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
wp_redirect( $redirect ); |
| 242 |
exit(); |
| 243 |
|
| 244 |
case 'pending': |
| 245 |
if ( isset( $_GET['request_id'] ) && ! empty( $_GET['request_id'] ) ) { |
| 246 |
foreach ( $_GET['request_id'] as $key => $request_id ) { |
| 247 |
erp_hr_leave_request_update_status( $request_id, 2 ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
wp_redirect( $redirect ); |
| 252 |
exit(); |
| 253 |
|
| 254 |
case 'filter_by_year': |
| 255 |
wp_redirect( $redirect ); |
| 256 |
exit(); |
| 257 |
|
| 258 |
case 'search_request': |
| 259 |
wp_redirect( $redirect ); |
| 260 |
exit(); |
| 261 |
|
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Handle Employee Bulk actions |
| 269 |
* |
| 270 |
* @since 0.1 |
| 271 |
* |
| 272 |
* @return void [redirection] |
| 273 |
*/ |
| 274 |
public function employee_bulk_action() { |
| 275 |
// Nonce validation |
| 276 |
if ( ! $this->verify_current_page_screen( 'erp-hr-employee', 'bulk-employees' ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
// Check permission if not hr manager then go out from here |
| 281 |
if ( ! current_user_can( 'erp_view_list' ) ) { |
| 282 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 283 |
} |
| 284 |
|
| 285 |
$employee_table = new \WeDevs\ERP\HRM\Employee_List_Table(); |
| 286 |
$action = $employee_table->current_action(); |
| 287 |
|
| 288 |
if ( $action ) { |
| 289 |
|
| 290 |
$redirect = remove_query_arg( array( |
| 291 |
'_wp_http_referer', |
| 292 |
'_wpnonce', |
| 293 |
'filter_employee' |
| 294 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 295 |
|
| 296 |
switch ( $action ) { |
| 297 |
|
| 298 |
case 'delete' : |
| 299 |
|
| 300 |
if ( isset( $_GET['employee_id'] ) && ! empty( $_GET['employee_id'] ) ) { |
| 301 |
erp_employee_delete( $_GET['employee_id'], false ); |
| 302 |
} |
| 303 |
|
| 304 |
wp_redirect( $redirect ); |
| 305 |
exit(); |
| 306 |
|
| 307 |
case 'permanent_delete' : |
| 308 |
if ( isset( $_GET['employee_id'] ) && ! empty( $_GET['employee_id'] ) ) { |
| 309 |
erp_employee_delete( $_GET['employee_id'], true ); |
| 310 |
} |
| 311 |
|
| 312 |
wp_redirect( $redirect ); |
| 313 |
exit(); |
| 314 |
|
| 315 |
case 'restore' : |
| 316 |
if ( isset( $_GET['employee_id'] ) && ! empty( $_GET['employee_id'] ) ) { |
| 317 |
erp_employee_restore( $_GET['employee_id'] ); |
| 318 |
} |
| 319 |
|
| 320 |
wp_redirect( $redirect ); |
| 321 |
exit(); |
| 322 |
|
| 323 |
case 'filter_employee': |
| 324 |
wp_redirect( $redirect ); |
| 325 |
exit(); |
| 326 |
|
| 327 |
case 'employee_search': |
| 328 |
$redirect = remove_query_arg( array( 'employee_search' ), $redirect ); |
| 329 |
wp_redirect( $redirect ); |
| 330 |
exit(); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Handle designation bulk action |
| 337 |
* |
| 338 |
* @since 0.1 |
| 339 |
* |
| 340 |
* @return void [redirection] |
| 341 |
*/ |
| 342 |
public function designation_bulk_action() { |
| 343 |
if ( ! $this->verify_current_page_screen( 'erp-hr-designation', 'bulk-designations' ) ) { |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
// Check permission if not hr manager then go out from here |
| 348 |
if ( ! current_user_can( erp_hr_get_manager_role() ) ) { |
| 349 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 350 |
} |
| 351 |
|
| 352 |
$employee_table = new \WeDevs\ERP\HRM\Designation_List_Table(); |
| 353 |
$action = $employee_table->current_action(); |
| 354 |
|
| 355 |
if ( $action ) { |
| 356 |
|
| 357 |
$redirect = remove_query_arg( array( |
| 358 |
'_wp_http_referer', |
| 359 |
'_wpnonce', |
| 360 |
'action', |
| 361 |
'action2' |
| 362 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 363 |
|
| 364 |
switch ( $action ) { |
| 365 |
|
| 366 |
case 'designation_delete' : |
| 367 |
|
| 368 |
if ( isset( $_GET['desig'] ) && ! empty( $_GET['desig'] ) ) { |
| 369 |
$not_deleted_item = erp_hr_delete_designation( $_GET['desig'] ); |
| 370 |
} |
| 371 |
|
| 372 |
if ( ! empty ( $not_deleted_item ) ) { |
| 373 |
$redirect = add_query_arg( array( 'desig_delete' => implode( ',', $not_deleted_item ) ), $redirect ); |
| 374 |
} |
| 375 |
|
| 376 |
wp_redirect( $redirect ); |
| 377 |
exit(); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Department handle bulk action |
| 384 |
* |
| 385 |
* @since 0.1 |
| 386 |
* |
| 387 |
* @return void [redirection] |
| 388 |
*/ |
| 389 |
public function department_bulk_action() { |
| 390 |
// Check nonce validation |
| 391 |
if ( ! $this->verify_current_page_screen( 'erp-hr-depts', 'bulk-departments' ) ) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
// Check permission if not hr manager then go out from here |
| 396 |
if ( ! current_user_can( erp_hr_get_manager_role() ) ) { |
| 397 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
$employee_table = new \WeDevs\ERP\HRM\Department_List_Table(); |
| 402 |
$action = $employee_table->current_action(); |
| 403 |
|
| 404 |
if ( $action ) { |
| 405 |
|
| 406 |
$redirect = remove_query_arg( array( |
| 407 |
'_wp_http_referer', |
| 408 |
'_wpnonce', |
| 409 |
'action', |
| 410 |
'action2' |
| 411 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 412 |
$resp = []; |
| 413 |
|
| 414 |
switch ( $action ) { |
| 415 |
|
| 416 |
case 'delete_department' : |
| 417 |
|
| 418 |
if ( isset( $_GET['department_id'] ) && $_GET['department_id'] ) { |
| 419 |
foreach ( $_GET['department_id'] as $key => $dept_id ) { |
| 420 |
$resp[] = erp_hr_delete_department( $dept_id ); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if ( in_array( false, $resp ) ) { |
| 425 |
$redirect = add_query_arg( array( 'department_delete' => 'item_deleted' ), $redirect ); |
| 426 |
} |
| 427 |
|
| 428 |
wp_redirect( $redirect ); |
| 429 |
exit(); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Remove all holiday |
| 436 |
* |
| 437 |
* @since 0.1 |
| 438 |
* |
| 439 |
* @return void |
| 440 |
*/ |
| 441 |
public function holiday_action() { |
| 442 |
// Check nonce validation |
| 443 |
if ( ! $this->verify_current_page_screen( 'erp-holiday-assign', 'bulk-holiday' ) ) { |
| 444 |
return; |
| 445 |
} |
| 446 |
|
| 447 |
// Check permission |
| 448 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 449 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 450 |
} |
| 451 |
|
| 452 |
$this->remove_holiday( $_GET ); |
| 453 |
|
| 454 |
$query_arg = add_query_arg( array( |
| 455 |
's' => $_GET['s'], |
| 456 |
'from' => $_GET['from'], |
| 457 |
'to' => $_GET['to'] |
| 458 |
), $_GET['_wp_http_referer'] ); |
| 459 |
wp_redirect( $query_arg ); |
| 460 |
exit(); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Handle hoiday remove functionality |
| 465 |
* |
| 466 |
* @since 0.1 |
| 467 |
* |
| 468 |
* @param array $get |
| 469 |
* |
| 470 |
* @return boolean |
| 471 |
*/ |
| 472 |
public function remove_holiday( $get ) { |
| 473 |
|
| 474 |
// Check permission |
| 475 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 476 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 477 |
} |
| 478 |
|
| 479 |
if ( isset( $get['action'] ) && $get['action'] == 'trash' ) { |
| 480 |
if ( isset( $get['holiday_id'] ) ) { |
| 481 |
erp_hr_delete_holidays( $get['holiday_id'] ); |
| 482 |
|
| 483 |
return true; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
if ( isset( $get['action2'] ) && $get['action2'] == 'trash' ) { |
| 488 |
if ( isset( $get['holiday_id'] ) ) { |
| 489 |
erp_hr_delete_holidays( $get['holiday_id'] ); |
| 490 |
|
| 491 |
return true; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Add entitlement with leave policies to employees |
| 500 |
* |
| 501 |
* @since 0.1 |
| 502 |
* |
| 503 |
* @return void |
| 504 |
*/ |
| 505 |
public function leave_entitlement() { |
| 506 |
|
| 507 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-hr-leave-assign' ) ) { |
| 508 |
die( __( 'Something went wrong!', 'erp' ) ); |
| 509 |
} |
| 510 |
|
| 511 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 512 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 513 |
} |
| 514 |
|
| 515 |
$affected = 0; |
| 516 |
$errors = array(); |
| 517 |
$employees = array(); |
| 518 |
$cur_year = (int) date( 'Y' ); |
| 519 |
$page_url = admin_url( 'admin.php?page=erp-leave-assign&tab=assignment' ); |
| 520 |
|
| 521 |
$is_single = ! isset( $_POST['assignment_to'] ); |
| 522 |
$leave_policy = isset( $_POST['leave_policy'] ) ? intval( $_POST['leave_policy'] ) : '-1'; |
| 523 |
$leave_period = isset( $_POST['leave_period'] ) ? $_POST['leave_period'] : '-1'; |
| 524 |
$single_employee = isset( $_POST['single_employee'] ) ? intval( $_POST['single_employee'] ) : '-1'; |
| 525 |
$location = isset( $_POST['location'] ) ? intval( $_POST['location'] ) : '-1'; |
| 526 |
$department = isset( $_POST['department'] ) ? intval( $_POST['department'] ) : '-1'; |
| 527 |
$comment = isset( $_POST['comment'] ) ? wp_kses_post( $_POST['comment'] ) : '-1'; |
| 528 |
|
| 529 |
if ( ! $leave_policy ) { |
| 530 |
$errors[] = 'invalid-policy'; |
| 531 |
} |
| 532 |
|
| 533 |
if ( ! in_array( $leave_period, array( $cur_year - 1, $cur_year, $cur_year + 1 ) ) ) { |
| 534 |
$errors[] = 'invalid-period'; |
| 535 |
} |
| 536 |
|
| 537 |
if ( $is_single && ! $single_employee ) { |
| 538 |
$errors[] = 'invalid-employee'; |
| 539 |
} |
| 540 |
|
| 541 |
// bail out if error found |
| 542 |
if ( $errors ) { |
| 543 |
$first_error = reset( $errors ); |
| 544 |
$redirect_to = add_query_arg( array( 'error' => $first_error ), $page_url ); |
| 545 |
wp_safe_redirect( $redirect_to ); |
| 546 |
exit; |
| 547 |
} |
| 548 |
|
| 549 |
// fetch employees if not single |
| 550 |
if ( ! $is_single ) { |
| 551 |
|
| 552 |
$employees = erp_hr_get_employees( array( |
| 553 |
'location' => $location, |
| 554 |
'department' => $department, |
| 555 |
'number' => '-1', |
| 556 |
) ); |
| 557 |
} else { |
| 558 |
|
| 559 |
$user = get_user_by( 'id', $single_employee ); |
| 560 |
$emp = new \stdClass(); |
| 561 |
$emp->id = $user->ID; |
| 562 |
$emp->display_name = $user->display_name; |
| 563 |
|
| 564 |
$employees[] = $emp; |
| 565 |
} |
| 566 |
|
| 567 |
if ( $employees ) { |
| 568 |
$from_date = $leave_period; |
| 569 |
$to_date = date( 'Y-m-t H:i:s', strtotime( '+11 month', strtotime( $leave_period ) ) ); |
| 570 |
$policy = erp_hr_leave_get_policy( $leave_policy ); |
| 571 |
|
| 572 |
if ( ! $policy ) { |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
foreach ( $employees as $employee ) { |
| 577 |
$data = array( |
| 578 |
'user_id' => $employee->id, |
| 579 |
'policy_id' => $leave_policy, |
| 580 |
'days' => $policy->value, |
| 581 |
'from_date' => $from_date, |
| 582 |
'to_date' => $to_date, |
| 583 |
'comments' => $comment, |
| 584 |
'status' => 1 |
| 585 |
); |
| 586 |
|
| 587 |
$inserted = erp_hr_leave_insert_entitlement( $data ); |
| 588 |
|
| 589 |
if ( ! is_wp_error( $inserted ) ) { |
| 590 |
$affected += 1; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
$redirect_to = add_query_arg( array( 'affected' => $affected ), $page_url ); |
| 595 |
wp_safe_redirect( $redirect_to ); |
| 596 |
exit; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Submit a new leave request |
| 602 |
* |
| 603 |
* @since 0.1 |
| 604 |
* |
| 605 |
* @return void |
| 606 |
*/ |
| 607 |
public function leave_request() { |
| 608 |
|
| 609 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-leave-req-new' ) ) { |
| 610 |
die( __( 'Something went wrong!', 'erp' ) ); |
| 611 |
} |
| 612 |
|
| 613 |
if ( ! current_user_can( 'erp_leave_create_request' ) ) { |
| 614 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 615 |
} |
| 616 |
|
| 617 |
$employee_id = isset( $_POST['employee_id'] ) ? intval( $_POST['employee_id'] ) : 0; |
| 618 |
$leave_policy = isset( $_POST['leave_policy'] ) ? intval( $_POST['leave_policy'] ) : 0; |
| 619 |
|
| 620 |
// @todo: date format may need to be changed when partial leave introduced |
| 621 |
$start_date = isset( $_POST['leave_from'] ) ? sanitize_text_field( $_POST['leave_from'] . ' 00:00:00' ) : date_i18n( 'Y-m-d 00:00:00' ); |
| 622 |
$end_date = isset( $_POST['leave_to'] ) ? sanitize_text_field( $_POST['leave_to'] . ' 23:59:59' ) : date_i18n( 'Y-m-d 23:59:59' ); |
| 623 |
|
| 624 |
$leave_reason = isset( $_POST['leave_reason'] ) ? strip_tags( $_POST['leave_reason'] ) : ''; |
| 625 |
|
| 626 |
$insert = erp_hr_leave_insert_request( array( |
| 627 |
'user_id' => $employee_id, |
| 628 |
'leave_policy' => $leave_policy, |
| 629 |
'start_date' => $start_date, |
| 630 |
'end_date' => $end_date, |
| 631 |
'reason' => $leave_reason |
| 632 |
) ); |
| 633 |
|
| 634 |
if ( ! is_wp_error( $insert ) ) { |
| 635 |
$redirect_to = admin_url( 'admin.php?page=erp-leave&view=new&msg=submitted' ); |
| 636 |
} else { |
| 637 |
$redirect_to = admin_url( 'admin.php?page=erp-leave&view=new&msg=error' ); |
| 638 |
} |
| 639 |
|
| 640 |
wp_redirect( $redirect_to ); |
| 641 |
exit; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Leave Request Status change |
| 646 |
* |
| 647 |
* @since 0.1 |
| 648 |
* |
| 649 |
* @return void |
| 650 |
*/ |
| 651 |
public function leave_request_status_change() { |
| 652 |
|
| 653 |
// If not leave bulk action then go out from here |
| 654 |
if ( ! isset( $_GET['leave_action'] ) ) { |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
// Verify the nonce validation |
| 659 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'erp-hr-leave-req-nonce' ) ) { |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
// Check permission if not have then bell out :) |
| 664 |
if ( ! current_user_can( 'erp_leave_manage' ) ) { |
| 665 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 666 |
} |
| 667 |
|
| 668 |
$action = $_GET['leave_action']; |
| 669 |
$stauses = array( |
| 670 |
'delete', |
| 671 |
'reject', |
| 672 |
'approve', |
| 673 |
'pending' |
| 674 |
); |
| 675 |
|
| 676 |
if ( ! in_array( $action, $stauses ) ) { |
| 677 |
return; |
| 678 |
} |
| 679 |
|
| 680 |
if ( empty( $_GET['id'] ) ) { |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
$request_id = absint( $_GET['id'] ); |
| 685 |
$status = null; |
| 686 |
|
| 687 |
switch ( $action ) { |
| 688 |
case 'delete': |
| 689 |
\WeDevs\ERP\HRM\Models\Leave_request::find( $_GET['id'] )->delete(); |
| 690 |
break; |
| 691 |
|
| 692 |
case 'reject': |
| 693 |
$status = 3; |
| 694 |
break; |
| 695 |
|
| 696 |
case 'approve': |
| 697 |
$status = 1; |
| 698 |
break; |
| 699 |
|
| 700 |
case 'pending': |
| 701 |
$status = 2; |
| 702 |
break; |
| 703 |
} |
| 704 |
|
| 705 |
if ( null !== $status ) { |
| 706 |
erp_hr_leave_request_update_status( $request_id, $status ); |
| 707 |
} |
| 708 |
|
| 709 |
// redirect the user back |
| 710 |
$redirect_to = remove_query_arg( array( 'status' ), admin_url( 'admin.php?page=erp-leave' ) ); |
| 711 |
$redirect_to = add_query_arg( array( 'status' => $status ), $redirect_to ); |
| 712 |
|
| 713 |
wp_redirect( $redirect_to ); |
| 714 |
exit; |
| 715 |
} |
| 716 |
|
| 717 |
/** |
| 718 |
* Employee Status Update |
| 719 |
* |
| 720 |
* @since 0.1 |
| 721 |
* |
| 722 |
* @return void |
| 723 |
*/ |
| 724 |
public function handle_employee_status_update() { |
| 725 |
// If not submit this form then return |
| 726 |
if ( ! isset( $_POST['employee_status'] ) ) { |
| 727 |
return; |
| 728 |
} |
| 729 |
|
| 730 |
// Nonce validaion |
| 731 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'wp-erp-hr-employee-update-nonce' ) ) { |
| 732 |
return; |
| 733 |
} |
| 734 |
|
| 735 |
// Check permission |
| 736 |
if ( ! current_user_can( erp_hr_get_manager_role() ) ) { |
| 737 |
wp_die( __( 'You do not have sufficient permissions to do this action', 'erp' ) ); |
| 738 |
} |
| 739 |
|
| 740 |
if ( $_POST['employee_status'] == 'terminated' ) { |
| 741 |
\WeDevs\ERP\HRM\Models\Employee::where( 'user_id', '=', $_POST['user_id'] )->update( [ |
| 742 |
'status' => $_POST['employee_status'], |
| 743 |
'termination_date' => current_time( 'mysql' ) |
| 744 |
] ); |
| 745 |
} else { |
| 746 |
\WeDevs\ERP\HRM\Models\Employee::where( 'user_id', '=', $_POST['user_id'] )->update( [ |
| 747 |
'status' => $_POST['employee_status'], |
| 748 |
'termination_date' => '' |
| 749 |
] ); |
| 750 |
} |
| 751 |
|
| 752 |
wp_redirect( $_POST['_wp_http_referer'] ); |
| 753 |
exit(); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* Employee Permission Management |
| 758 |
* |
| 759 |
* @since 0.1 |
| 760 |
* |
| 761 |
* @return void |
| 762 |
*/ |
| 763 |
public function employee_permission() { |
| 764 |
|
| 765 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'wp-erp-hr-employee-permission-nonce' ) ) { |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
$hr_manager_role = erp_hr_get_manager_role(); |
| 770 |
|
| 771 |
if ( ! current_user_can( $hr_manager_role ) ) { |
| 772 |
wp_die( __( 'Permission Denied!', 'erp' ) ); |
| 773 |
} |
| 774 |
|
| 775 |
$employee_id = isset( $_POST['employee_id'] ) ? absint( $_POST['employee_id'] ) : 0; |
| 776 |
$enable_manager = isset( $_POST['enable_manager'] ) ? filter_var( $_POST['enable_manager'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 777 |
|
| 778 |
$user = get_user_by( 'id', $employee_id ); |
| 779 |
|
| 780 |
if ( $enable_manager && ! user_can( $user, $hr_manager_role ) ) { |
| 781 |
|
| 782 |
$user->add_role( $hr_manager_role ); |
| 783 |
|
| 784 |
} else if ( ! $enable_manager && user_can( $user, $hr_manager_role ) ) { |
| 785 |
|
| 786 |
$user->remove_role( $hr_manager_role ); |
| 787 |
} |
| 788 |
|
| 789 |
do_action( 'erp_hr_after_employee_permission_set', $_POST, $user ); |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Reporting Form Submit Handler |
| 794 |
* |
| 795 |
* @since 0.1 |
| 796 |
* |
| 797 |
* @return void |
| 798 |
*/ |
| 799 |
public function reporting_bulk_action() { |
| 800 |
|
| 801 |
if ( isset( $_REQUEST['filter_headcount'] ) ) { |
| 802 |
|
| 803 |
if ( ! $this->verify_current_page_screen( 'erp-hr-reporting', 'epr-rep-headcount' ) ) { |
| 804 |
return; |
| 805 |
} |
| 806 |
|
| 807 |
$redirect = remove_query_arg( array( |
| 808 |
'_wp_http_referer', |
| 809 |
'_wpnonce', |
| 810 |
'filter_headcount' |
| 811 |
), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 812 |
|
| 813 |
wp_redirect( $redirect ); |
| 814 |
} |
| 815 |
|
| 816 |
if ( isset( $_REQUEST['filter_leave_report'] ) ) { |
| 817 |
|
| 818 |
if ( ! $this->verify_current_page_screen( 'erp-hr-reporting', 'epr-rep-leaves' ) ) { |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
$redirect = remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'filter_leave_report' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 823 |
|
| 824 |
wp_redirect( $redirect ); |
| 825 |
} |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
} |
| 830 |
|
| 831 |
new Form_Handler(); |
| 832 |
|