| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\HRM; |
| 3 |
|
| 4 |
use \WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
use \WeDevs\ERP\HRM\Models\announcment; |
| 6 |
use \WeDevs\ERP\HRM\Models\employee; |
| 7 |
use \WeDevs\ERP\HRM\Models\Dependents; |
| 8 |
use \WeDevs\ERP\HRM\Models\Designation; |
| 9 |
|
| 10 |
/** |
| 11 |
* Ajax handler |
| 12 |
* |
| 13 |
* @package WP-ERP |
| 14 |
*/ |
| 15 |
class Hr_Log { |
| 16 |
|
| 17 |
use Hooker; |
| 18 |
|
| 19 |
/** |
| 20 |
* Load autometically when class inistantiate |
| 21 |
* |
| 22 |
* @since 0.1 |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
|
| 28 |
// Employee |
| 29 |
$this->action( 'erp_hr_employee_new', 'create_employee', 10, 2 ); |
| 30 |
$this->action( 'erp_hr_delete_employee', 'delete_employee', 10 ); |
| 31 |
$this->action( 'erp_hr_employee_update', 'update_employee', 10, 2 ); |
| 32 |
|
| 33 |
// Employee experience |
| 34 |
$this->action( 'erp_hr_employee_experience_new', 'create_experience' ); |
| 35 |
$this->action( 'erp_hr_employee_experience_delete', 'delete_experience' ); |
| 36 |
|
| 37 |
// Employee education |
| 38 |
$this->action( 'erp_hr_employee_education_create', 'create_education' ); |
| 39 |
$this->action( 'erp_hr_employee_education_delete', 'delete_education' ); |
| 40 |
|
| 41 |
// Employee dependents |
| 42 |
$this->action( 'erp_hr_employee_dependents_create', 'create_dependents' ); |
| 43 |
$this->action( 'erp_hr_employee_dependents_delete', 'delete_dependents' ); |
| 44 |
|
| 45 |
// Employee employment status |
| 46 |
$this->action( 'erp_hr_employee_employment_history_create', 'create_employment_status' ); |
| 47 |
$this->action( 'erp_hr_employee_employment_history_delete', 'delete_employment_status' ); |
| 48 |
|
| 49 |
// Employee compensation |
| 50 |
$this->action( 'erp_hr_employee_compensation_history_create', 'create_compensation' ); |
| 51 |
$this->action( 'erp_hr_employee_compensation_history_delete', 'delete_compensation' ); |
| 52 |
|
| 53 |
// Employee job info |
| 54 |
$this->action( 'erp_hr_employee_job_history_create', 'create_job_info' ); |
| 55 |
$this->action( 'erp_hr_employee_job_history_delete', 'delete_job_info' ); |
| 56 |
|
| 57 |
// Department |
| 58 |
$this->action( 'erp_hr_dept_new', 'create_department', 10, 2 ); |
| 59 |
$this->action( 'erp_hr_dept_delete', 'delete_department', 10 ); |
| 60 |
$this->action( 'erp_hr_dept_before_updated', 'update_department', 10, 2 ); |
| 61 |
|
| 62 |
// Designation |
| 63 |
$this->action( 'erp_hr_desig_new', 'create_designation', 10, 2 ); |
| 64 |
$this->action( 'erp_hr_desig_delete', 'delete_designation', 10 ); |
| 65 |
$this->action( 'erp_hr_desig_before_updated', 'update_designation', 10, 2 ); |
| 66 |
|
| 67 |
//Leave Policy |
| 68 |
$this->action( 'erp_hr_leave_policy_new', 'create_policy', 10, 2 ); |
| 69 |
$this->action( 'erp_hr_leave_policy_delete', 'delete_policy', 10 ); |
| 70 |
$this->action( 'erp_hr_leave_before_policy_updated', 'update_policy', 10, 2 ); |
| 71 |
|
| 72 |
//Leave Request |
| 73 |
$this->action( 'erp_hr_leave_new', 'create_leave_request', 10, 3 ); |
| 74 |
|
| 75 |
//Entitlement |
| 76 |
$this->action( 'erp_hr_leave_insert_new_entitlement', 'create_entitlement', 10, 2 ); |
| 77 |
|
| 78 |
//Holiday |
| 79 |
$this->action( 'erp_hr_new_holiday', 'create_holiday', 10, 2 ); |
| 80 |
$this->action( 'erp_hr_leave_holiday_delete', 'delete_holiday', 10 ); |
| 81 |
$this->action( 'erp_hr_before_update_holiday', 'update_holiday', 10, 2 ); |
| 82 |
|
| 83 |
// Announcement |
| 84 |
$this->action( 'transition_post_status', 'announcment_log', 10, 3 ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get different array from two array |
| 89 |
* |
| 90 |
* @since 0.1 |
| 91 |
* |
| 92 |
* @param array $new_data |
| 93 |
* @param array $old_data |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function get_array_diff( $new_data, $old_data, $is_seriazie = false ) { |
| 98 |
|
| 99 |
$old_value = $new_value = []; |
| 100 |
$changes_key = array_keys( array_diff_assoc( $new_data, $old_data ) ); |
| 101 |
|
| 102 |
foreach ( $changes_key as $key => $change_field_key ) { |
| 103 |
$old_value[$change_field_key] = $old_data[$change_field_key]; |
| 104 |
$new_value[$change_field_key] = $new_data[$change_field_key]; |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! $is_seriazie ) { |
| 108 |
return [ |
| 109 |
'new_val' => $new_value ? base64_encode( maybe_serialize( $new_value ) ) : '', |
| 110 |
'old_val' => $old_value ? base64_encode( maybe_serialize( $old_value ) ) : '' |
| 111 |
]; |
| 112 |
} else { |
| 113 |
return [ |
| 114 |
'new_val' => $new_value, |
| 115 |
'old_val' => $old_value |
| 116 |
]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Add log when new employee created |
| 122 |
* |
| 123 |
* @since 0.1 |
| 124 |
* |
| 125 |
* @param integer $emp_id |
| 126 |
* @param array $fields |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function create_employee( $emp_id, $fields ) { |
| 131 |
erp_log()->add([ |
| 132 |
'sub_component' => 'employee', |
| 133 |
'message' => sprintf( __( '<strong>%s</strong> employee has been created', 'erp' ), $fields['personal']['first_name'] ), |
| 134 |
'created_by' => get_current_user_id() |
| 135 |
]); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Add log when employee deleted |
| 140 |
* |
| 141 |
* @since 0.1 |
| 142 |
* |
| 143 |
* @param integer $emp_id |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function delete_employee( $emp_id ) { |
| 148 |
|
| 149 |
if ( ! $emp_id ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $emp_id ) ); |
| 154 |
|
| 155 |
erp_log()->add([ |
| 156 |
'sub_component' => 'employee', |
| 157 |
'message' => sprintf( __( '<strong>%s</strong> employee has been deleted', 'erp' ), $employee->get_full_name() ), |
| 158 |
'created_by' => get_current_user_id(), |
| 159 |
'changetype' => 'delete', |
| 160 |
]); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Add log when employee updated |
| 165 |
* |
| 166 |
* @since 0.1 |
| 167 |
* |
| 168 |
* @param integer $emp_id |
| 169 |
* @param array $fields |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function update_employee( $emp_id, $fields ) { |
| 174 |
if ( ! $emp_id ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
//$old_employee_data = \WeDevs\ERP\HRM\Models\Employee::find( $emp_id )->toArray(); |
| 179 |
//$employee = new \WeDevs\ERP\HRM\Employee( intval( $emp_id ) ); |
| 180 |
//unset( $old_employee_data['created_at'], $old_employee_data['updated_at'] ); |
| 181 |
|
| 182 |
//$changes = $this->get_array_diff( $fields, $old_employee_data ); |
| 183 |
|
| 184 |
//if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 185 |
//$message = false; |
| 186 |
//} else { |
| 187 |
$message = sprintf( __( '<strong>%s</strong> employee has been edited', 'erp' ), $fields['personal']['first_name'] ); |
| 188 |
//} |
| 189 |
|
| 190 |
if ( $message ) { |
| 191 |
erp_log()->add([ |
| 192 |
'sub_component' => 'employee', |
| 193 |
'message' => $message, |
| 194 |
'created_by' => get_current_user_id(), |
| 195 |
'changetype' => 'edit', |
| 196 |
'old_value' => '',//$changes['old_val'], |
| 197 |
'new_value' => ''//$changes['new_val'] |
| 198 |
]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Add log when new employee experience created |
| 204 |
* |
| 205 |
* @since 0.1 |
| 206 |
* |
| 207 |
* @param array $fields |
| 208 |
* |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function create_experience( $fields ) { |
| 212 |
|
| 213 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $fields['user_id'] ) ); |
| 214 |
|
| 215 |
erp_log()->add([ |
| 216 |
'sub_component' => 'employee', |
| 217 |
'message' => sprintf( __( '<strong>%s</strong> employee experience has been created', 'erp' ), $employee->get_full_name() ), |
| 218 |
'created_by' => get_current_user_id() |
| 219 |
]); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Add log when employee experience deleted |
| 224 |
* |
| 225 |
* @since 0.1 |
| 226 |
* |
| 227 |
* @param integer $exp_id |
| 228 |
* |
| 229 |
* @return void |
| 230 |
*/ |
| 231 |
public function delete_experience( $exp_id ) { |
| 232 |
|
| 233 |
if ( ! $exp_id ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
$exp = \WeDevs\ERP\HRM\Models\Work_Experience::find( $exp_id )->toArray(); |
| 238 |
|
| 239 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $exp['employee_id'] ) ); |
| 240 |
|
| 241 |
erp_log()->add([ |
| 242 |
'sub_component' => 'employee', |
| 243 |
'message' => sprintf( __( '<strong>%s</strong> employee experience has been deleted', 'erp' ), $employee->get_full_name() ), |
| 244 |
'created_by' => get_current_user_id(), |
| 245 |
'changetype' => 'delete', |
| 246 |
]); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Add log when new employee education created |
| 251 |
* |
| 252 |
* @since 0.1 |
| 253 |
* |
| 254 |
* @param array $fields |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public function create_education( $fields ) { |
| 259 |
|
| 260 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $fields['user_id'] ) ); |
| 261 |
|
| 262 |
erp_log()->add([ |
| 263 |
'sub_component' => 'employee', |
| 264 |
'message' => sprintf( __( '<strong>%s</strong> employee education has been created', 'erp' ), $employee->get_full_name() ), |
| 265 |
'created_by' => get_current_user_id() |
| 266 |
]); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Add log when employee education deleted |
| 271 |
* |
| 272 |
* @since 0.1 |
| 273 |
* |
| 274 |
* @param integer $edu_id |
| 275 |
* |
| 276 |
* @return void |
| 277 |
*/ |
| 278 |
public function delete_education( $edu_id ) { |
| 279 |
|
| 280 |
if ( ! $edu_id ) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
$exp = \WeDevs\ERP\HRM\Models\Education::find( $edu_id )->toArray(); |
| 285 |
|
| 286 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $exp['employee_id'] ) ); |
| 287 |
|
| 288 |
erp_log()->add([ |
| 289 |
'sub_component' => 'employee', |
| 290 |
'message' => sprintf( __( '<strong>%s</strong> employee education has been deleted', 'erp' ), $employee->get_full_name() ), |
| 291 |
'created_by' => get_current_user_id(), |
| 292 |
'changetype' => 'delete', |
| 293 |
]); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Add log when new employee dependents created |
| 298 |
* |
| 299 |
* @since 0.1 |
| 300 |
* |
| 301 |
* @param array $fields |
| 302 |
* |
| 303 |
* @return void |
| 304 |
*/ |
| 305 |
public function create_dependents( $fields ) { |
| 306 |
|
| 307 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $fields['user_id'] ) ); |
| 308 |
|
| 309 |
erp_log()->add([ |
| 310 |
'sub_component' => 'employee', |
| 311 |
'message' => sprintf( __( '<strong>%s</strong> employee dependents has been created', 'erp' ), $employee->get_full_name() ), |
| 312 |
'created_by' => get_current_user_id() |
| 313 |
]); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Add log when employee dependents deleted |
| 318 |
* |
| 319 |
* @since 0.1 |
| 320 |
* |
| 321 |
* @param integer $dep_id |
| 322 |
* |
| 323 |
* @return void |
| 324 |
*/ |
| 325 |
public function delete_dependents( $dep_id ) { |
| 326 |
|
| 327 |
if ( ! $dep_id ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
$dep = \WeDevs\ERP\HRM\Models\Dependents::find( $dep_id )->toArray(); |
| 332 |
|
| 333 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $dep['employee_id'] ) ); |
| 334 |
|
| 335 |
erp_log()->add([ |
| 336 |
'sub_component' => 'employee', |
| 337 |
'message' => sprintf( __( '<strong>%s</strong> employee dependents has been deleted', 'erp' ), $employee->get_full_name() ), |
| 338 |
'created_by' => get_current_user_id(), |
| 339 |
'changetype' => 'delete', |
| 340 |
]); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Add log when new employee employment status created |
| 345 |
* |
| 346 |
* @since 0.1 |
| 347 |
* |
| 348 |
* @param array $fields |
| 349 |
* |
| 350 |
* @return void |
| 351 |
*/ |
| 352 |
public function create_employment_status( $eid ) { |
| 353 |
|
| 354 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $eid ) ); |
| 355 |
|
| 356 |
erp_log()->add([ |
| 357 |
'sub_component' => 'employee', |
| 358 |
'message' => sprintf( __( '<strong>%s</strong> employee employment status has been created', 'erp' ), $employee->get_full_name() ), |
| 359 |
'created_by' => get_current_user_id() |
| 360 |
]); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Add log when employee employment status deleted |
| 365 |
* |
| 366 |
* @since 0.1 |
| 367 |
* |
| 368 |
* @param integer $history_id |
| 369 |
* |
| 370 |
* @return void |
| 371 |
*/ |
| 372 |
public function delete_employment_status( $history_id ) { |
| 373 |
|
| 374 |
if ( ! $history_id ) { |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
global $wpdb; |
| 379 |
$query = "SELECT user_id |
| 380 |
FROM {$wpdb->prefix}erp_hr_employee_history |
| 381 |
WHERE id=".$history_id; |
| 382 |
$user_id = $wpdb->get_var($query); |
| 383 |
|
| 384 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $user_id ) ); |
| 385 |
|
| 386 |
erp_log()->add([ |
| 387 |
'sub_component' => 'employee', |
| 388 |
'message' => sprintf( __( '<strong>%s</strong> employee employment status has been deleted', 'erp' ), $employee->get_full_name() ), |
| 389 |
'created_by' => get_current_user_id(), |
| 390 |
'changetype' => 'delete', |
| 391 |
]); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Add log when new employee compensation status created |
| 396 |
* |
| 397 |
* @since 0.1 |
| 398 |
* |
| 399 |
* @param array $emp_id |
| 400 |
* |
| 401 |
* @return void |
| 402 |
*/ |
| 403 |
public function create_compensation( $emp_id ) { |
| 404 |
|
| 405 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $emp_id ) ); |
| 406 |
|
| 407 |
erp_log()->add([ |
| 408 |
'sub_component' => 'employee', |
| 409 |
'message' => sprintf( __( '<strong>%s</strong> employee compensation has been created', 'erp' ), $employee->get_full_name() ), |
| 410 |
'created_by' => get_current_user_id() |
| 411 |
]); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Add log when employee compensation deleted |
| 416 |
* |
| 417 |
* @since 0.1 |
| 418 |
* |
| 419 |
* @param integer $history_id |
| 420 |
* |
| 421 |
* @return void |
| 422 |
*/ |
| 423 |
public function delete_compensation( $history_id ) { |
| 424 |
|
| 425 |
if ( ! $history_id ) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
global $wpdb; |
| 430 |
$query = "SELECT user_id |
| 431 |
FROM {$wpdb->prefix}erp_hr_employee_history |
| 432 |
WHERE id=".$history_id; |
| 433 |
$user_id = $wpdb->get_var($query); |
| 434 |
|
| 435 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $user_id ) ); |
| 436 |
|
| 437 |
erp_log()->add([ |
| 438 |
'sub_component' => 'employee', |
| 439 |
'message' => sprintf( __( '<strong>%s</strong> employee compensation has been deleted', 'erp' ), $employee->get_full_name() ), |
| 440 |
'created_by' => get_current_user_id(), |
| 441 |
'changetype' => 'delete', |
| 442 |
]); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Add log when new employee job info created |
| 447 |
* |
| 448 |
* @since 0.1 |
| 449 |
* |
| 450 |
* @param array $emp_id |
| 451 |
* |
| 452 |
* @return void |
| 453 |
*/ |
| 454 |
public function create_job_info( $emp_id ) { |
| 455 |
|
| 456 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $emp_id ) ); |
| 457 |
|
| 458 |
erp_log()->add([ |
| 459 |
'sub_component' => 'employee', |
| 460 |
'message' => sprintf( __( '<strong>%s</strong> employee job info has been created', 'erp' ), $employee->get_full_name() ), |
| 461 |
'created_by' => get_current_user_id() |
| 462 |
]); |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Add log when employee job info deleted |
| 467 |
* |
| 468 |
* @since 0.1 |
| 469 |
* |
| 470 |
* @param integer $history_id |
| 471 |
* |
| 472 |
* @return void |
| 473 |
*/ |
| 474 |
public function delete_job_info( $history_id ) { |
| 475 |
|
| 476 |
if ( ! $history_id ) { |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
global $wpdb; |
| 481 |
$query = "SELECT user_id |
| 482 |
FROM {$wpdb->prefix}erp_hr_employee_history |
| 483 |
WHERE id=".$history_id; |
| 484 |
$user_id = $wpdb->get_var($query); |
| 485 |
|
| 486 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $user_id ) ); |
| 487 |
|
| 488 |
erp_log()->add([ |
| 489 |
'sub_component' => 'employee', |
| 490 |
'message' => sprintf( __( '<strong>%s</strong> employee job info has been deleted', 'erp' ), $employee->get_full_name() ), |
| 491 |
'created_by' => get_current_user_id(), |
| 492 |
'changetype' => 'delete', |
| 493 |
]); |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Add log when new department created |
| 498 |
* |
| 499 |
* @since 0.1 |
| 500 |
* |
| 501 |
* @param integer $dept_id |
| 502 |
* @param array $fields |
| 503 |
* |
| 504 |
* @return void |
| 505 |
*/ |
| 506 |
public function create_department( $dept_id, $fields ) { |
| 507 |
erp_log()->add([ |
| 508 |
'sub_component' => 'department', |
| 509 |
'message' => sprintf( __( '<strong>%s</strong> department has been created', 'erp' ), $fields['title'] ), |
| 510 |
'created_by' => get_current_user_id() |
| 511 |
]); |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* Add log when department deleted |
| 516 |
* |
| 517 |
* @since 0.1 |
| 518 |
* |
| 519 |
* @param integer $dept_id |
| 520 |
* |
| 521 |
* @return void |
| 522 |
*/ |
| 523 |
public function delete_department( $dept_id ) { |
| 524 |
|
| 525 |
if ( ! $dept_id ) { |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
$department = new \WeDevs\ERP\HRM\Department( intval( $dept_id ) ); |
| 530 |
|
| 531 |
erp_log()->add([ |
| 532 |
'sub_component' => 'department', |
| 533 |
'message' => sprintf( __( '<strong>%s</strong> department has been deleted', 'erp' ), $department->title ), |
| 534 |
'created_by' => get_current_user_id(), |
| 535 |
'changetype' => 'delete', |
| 536 |
]); |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Add log when udpate department |
| 541 |
* |
| 542 |
* @since 0.1 |
| 543 |
* |
| 544 |
* @param integer $dept_id |
| 545 |
* @param array $fields |
| 546 |
* |
| 547 |
* @return void |
| 548 |
*/ |
| 549 |
public function update_department( $dept_id, $fields ) { |
| 550 |
|
| 551 |
if ( ! $dept_id ) { |
| 552 |
return; |
| 553 |
} |
| 554 |
|
| 555 |
$old_department = \WeDevs\ERP\HRM\Models\Department::find( $dept_id )->toArray(); |
| 556 |
unset( $old_department['created_at'], $old_department['updated_at'] ); |
| 557 |
|
| 558 |
$changes = $this->get_array_diff( $fields, $old_department, true ); |
| 559 |
|
| 560 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 561 |
$message = false; |
| 562 |
} else { |
| 563 |
array_walk ( $changes, function ( &$key ) { |
| 564 |
if ( isset( $key['lead'] ) ) { |
| 565 |
if( $key['lead'] ) { |
| 566 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $key['lead'] ) ); |
| 567 |
$key['department_lead'] = $employee->get_full_name(); |
| 568 |
} else { |
| 569 |
$key['department_lead'] = __( 'No deparment leader', 'erp' ); |
| 570 |
} |
| 571 |
unset( $key['lead'] ); |
| 572 |
} |
| 573 |
|
| 574 |
if ( isset( $key['parent'] ) ) { |
| 575 |
if( $key['parent'] ) { |
| 576 |
$department = new \WeDevs\ERP\HRM\Department( intval( $key['parent'] ) ); |
| 577 |
$key['parent_department'] = $department->title; |
| 578 |
} else { |
| 579 |
$key['parent_department'] = __( 'No Parent Department', 'erp' ); |
| 580 |
} |
| 581 |
unset( $key['parent'] ); |
| 582 |
} |
| 583 |
} ); |
| 584 |
|
| 585 |
$message = sprintf( __( '<strong>%s</strong> department has been edited', 'erp' ), $old_department['title'] ); |
| 586 |
} |
| 587 |
|
| 588 |
if ( $message ) { |
| 589 |
erp_log()->add([ |
| 590 |
'sub_component' => 'department', |
| 591 |
'message' => $message, |
| 592 |
'created_by' => get_current_user_id(), |
| 593 |
'changetype' => 'edit', |
| 594 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 595 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 596 |
]); |
| 597 |
} |
| 598 |
|
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* Add log when new designation created |
| 603 |
* |
| 604 |
* @since 0.1 |
| 605 |
* |
| 606 |
* @param integer $desig_id |
| 607 |
* @param array $fields |
| 608 |
* |
| 609 |
* @return void |
| 610 |
*/ |
| 611 |
public function create_designation( $desig_id, $fields ) { |
| 612 |
erp_log()->add([ |
| 613 |
'sub_component' => 'designation', |
| 614 |
'message' => sprintf( __( '<strong>%s</strong> designation has been created', 'erp' ), $fields['title'] ), |
| 615 |
'created_by' => get_current_user_id() |
| 616 |
]); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Add log when department deleted |
| 621 |
* |
| 622 |
* @since 0.1 |
| 623 |
* |
| 624 |
* @param integer $dept_id |
| 625 |
* |
| 626 |
* @return void |
| 627 |
*/ |
| 628 |
public function delete_designation( $desig ) { |
| 629 |
if ( ! $desig ) { |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
erp_log()->add([ |
| 634 |
'sub_component' => 'designation', |
| 635 |
'message' => sprintf( __( '<strong>%s</strong> designation has been deleted', 'erp' ), $desig->title ), |
| 636 |
'created_by' => get_current_user_id(), |
| 637 |
'changetype' => 'delete', |
| 638 |
]); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Add log when designation updated |
| 643 |
* |
| 644 |
* @since 0.1 |
| 645 |
* |
| 646 |
* @param integer $desig_id |
| 647 |
* @param array $fields |
| 648 |
* |
| 649 |
* @return void |
| 650 |
*/ |
| 651 |
public function update_designation( $desig_id, $fields ) { |
| 652 |
if ( ! $desig_id ) { |
| 653 |
return; |
| 654 |
} |
| 655 |
|
| 656 |
$old_desig = \WeDevs\ERP\HRM\Models\Designation::find( $desig_id )->toArray(); |
| 657 |
unset( $old_desig['created_at'], $old_desig['updated_at'] ); |
| 658 |
|
| 659 |
$changes = $this->get_array_diff( $fields, $old_desig ); |
| 660 |
|
| 661 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 662 |
$message = false; |
| 663 |
} else { |
| 664 |
$message = sprintf( __( '<strong>%s</strong> designation has been edited', 'erp' ), $old_desig['title'] ); |
| 665 |
} |
| 666 |
|
| 667 |
if ( $message ) { |
| 668 |
erp_log()->add([ |
| 669 |
'sub_component' => 'designation', |
| 670 |
'message' => $message, |
| 671 |
'created_by' => get_current_user_id(), |
| 672 |
'changetype' => 'edit', |
| 673 |
'old_value' => $changes['old_val'], |
| 674 |
'new_value' => $changes['new_val'] |
| 675 |
]); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Logging for creating policy |
| 681 |
* |
| 682 |
* @since 0.1 |
| 683 |
* |
| 684 |
* @param integer $policy_id |
| 685 |
* @param array $fields |
| 686 |
* |
| 687 |
* @return void |
| 688 |
*/ |
| 689 |
public function create_policy( $policy_id, $fields ) { |
| 690 |
|
| 691 |
if ( ! $policy_id ) { |
| 692 |
return; |
| 693 |
} |
| 694 |
|
| 695 |
erp_log()->add([ |
| 696 |
'sub_component' => 'leave', |
| 697 |
'message' => sprintf( __( '<strong>%s</strong> policy has been created', 'erp' ), $fields['name'] ), |
| 698 |
'created_by' => get_current_user_id(), |
| 699 |
'changetype' => 'add', |
| 700 |
]); |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* Adding log when policy deleted |
| 705 |
* |
| 706 |
* @since 0.1 |
| 707 |
* @since 1.2.0 Using $policy eloquent object instead of $policy_id |
| 708 |
* |
| 709 |
* @param object $policy |
| 710 |
* |
| 711 |
* @return void |
| 712 |
*/ |
| 713 |
public function delete_policy( $policy ) { |
| 714 |
if ( ! $policy ) { |
| 715 |
return; |
| 716 |
} |
| 717 |
|
| 718 |
erp_log()->add([ |
| 719 |
'sub_component' => 'leave', |
| 720 |
'message' => sprintf( __( '<strong>%s</strong> policy has been deleted', 'erp' ), $policy->name ), |
| 721 |
'created_by' => get_current_user_id(), |
| 722 |
'changetype' => 'delete', |
| 723 |
]); |
| 724 |
} |
| 725 |
|
| 726 |
/** |
| 727 |
* Add log when udpate policy |
| 728 |
* |
| 729 |
* @since 0.1 |
| 730 |
* |
| 731 |
* @param integer $policy_id |
| 732 |
* @param array $fields |
| 733 |
* |
| 734 |
* @return void |
| 735 |
*/ |
| 736 |
public function update_policy( $policy_id, $fields ) { |
| 737 |
|
| 738 |
if ( ! $policy_id ) { |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
$old_policy = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy_id )->toArray(); |
| 743 |
unset( $old_policy['created_at'], $old_policy['updated_at'], $fields['instant_apply'] ) ; |
| 744 |
|
| 745 |
$old_policy['effective_date'] = erp_format_date( $old_policy['effective_date'], 'Y-m-d' ); |
| 746 |
$fields['effective_date'] = erp_format_date( $fields['effective_date'], 'Y-m-d' ); |
| 747 |
|
| 748 |
if ( isset( $fields['activate'] ) && $fields['activate'] == 1 ) { |
| 749 |
unset( $fields['execute_day'], $old_policy['execute_day'] ); |
| 750 |
} |
| 751 |
|
| 752 |
$changes = $this->get_array_diff( $fields, $old_policy, true ); |
| 753 |
|
| 754 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 755 |
$message = false; |
| 756 |
} else { |
| 757 |
array_walk ( $changes, function ( &$key ) { |
| 758 |
|
| 759 |
if ( isset( $key['color'] ) ) { |
| 760 |
$key['calender_color'] = sprintf( '<div style="width:60px; height:20px; background-color:%s"></div>', $key['color'] ); |
| 761 |
unset( $key['color'] ); |
| 762 |
} |
| 763 |
|
| 764 |
if ( isset( $key['department'] ) ) { |
| 765 |
if ( $key['department'] == '-1' ) { |
| 766 |
$key['department'] = __( 'All Department', 'erp' ); |
| 767 |
} else { |
| 768 |
$department = new \WeDevs\ERP\HRM\Department( intval( $key['department'] ) ); |
| 769 |
$key['department'] = $department->title; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
if ( isset( $key['designation'] ) ) { |
| 774 |
if ( $key['designation'] == '-1' ) { |
| 775 |
$key['designation'] = __( 'All Designation', 'erp' ); |
| 776 |
} else { |
| 777 |
$designation = new \WeDevs\ERP\HRM\Designation( intval( $key['designation'] ) ); |
| 778 |
$key['designation'] = $designation->title; |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
if ( isset( $key['location'] ) ) { |
| 783 |
if ( $key['location'] == '-1' ) { |
| 784 |
$key['location'] = __( 'All Location', 'erp' ); |
| 785 |
} else { |
| 786 |
$location = erp_company_get_location_dropdown_raw(); |
| 787 |
$key['location'] = $location[$key['location']]; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
if ( isset( $key['gender'] ) ) { |
| 792 |
$gender = erp_hr_get_genders( __( 'All', 'erp' ) ); |
| 793 |
$key['gender'] = $gender[$key['gender']]; |
| 794 |
} |
| 795 |
|
| 796 |
if ( isset( $key['marital'] ) ) { |
| 797 |
$marital = erp_hr_get_marital_statuses( __( 'All', 'erp' ) ); |
| 798 |
$key['marital'] = $marital[$key['marital']]; |
| 799 |
} |
| 800 |
|
| 801 |
if ( isset( $key['activate'] ) ) { |
| 802 |
$activate = array( '1' => __( 'Immediately apply after hiring', 'erp' ), '2' => __( 'Apply after X days from hiring', 'erp' ), '3' => __( 'Manually', 'erp' ) ); |
| 803 |
|
| 804 |
if ( $key['activate'] == 2 ) { |
| 805 |
$key['activation'] = str_replace( 'X', $key['execute_day'], $activate[$key['activate']] ); |
| 806 |
} else { |
| 807 |
$key['activation'] = $activate[$key['activate']]; |
| 808 |
} |
| 809 |
|
| 810 |
unset( $key['activate'] ); |
| 811 |
unset( $key['execute_day'] ); |
| 812 |
} |
| 813 |
|
| 814 |
if ( isset( $key['effective_date'] ) ) { |
| 815 |
$key['policy_effective_date'] = erp_format_date( $key['effective_date'] ); |
| 816 |
unset( $key['effective_date'] ); |
| 817 |
} |
| 818 |
|
| 819 |
} ); |
| 820 |
|
| 821 |
$message = sprintf( __( '<strong>%s</strong> policy has been edited', 'erp' ), $old_policy['name'] ); |
| 822 |
} |
| 823 |
|
| 824 |
if ( $message ) { |
| 825 |
erp_log()->add([ |
| 826 |
'sub_component' => 'leave', |
| 827 |
'message' => $message, |
| 828 |
'created_by' => get_current_user_id(), |
| 829 |
'changetype' => 'edit', |
| 830 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 831 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 832 |
]); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
/** |
| 837 |
* Add log when someone take leave |
| 838 |
* |
| 839 |
* @since 0.1 |
| 840 |
* |
| 841 |
* @param integer $request_id |
| 842 |
* @param array $request |
| 843 |
* @param array $leaves |
| 844 |
* |
| 845 |
* @return void |
| 846 |
*/ |
| 847 |
public function create_leave_request( $request_id, $request, $leaves ) { |
| 848 |
|
| 849 |
if ( ! $request_id ) { |
| 850 |
return; |
| 851 |
} |
| 852 |
|
| 853 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $request['user_id'] ) ); |
| 854 |
|
| 855 |
$message = sprintf( __( '<strong>%s</strong> took leave from <strong>%s</strong> to <strong>%s</strong> for <strong>%d</strong> days', 'erp' ), |
| 856 |
$employee->get_full_name(), |
| 857 |
erp_format_date( $request['start_date'] ), |
| 858 |
erp_format_date( $request['end_date'] ), |
| 859 |
$request['days'] |
| 860 |
); |
| 861 |
|
| 862 |
erp_log()->add([ |
| 863 |
'sub_component' => 'leave', |
| 864 |
'message' => $message, |
| 865 |
'created_by' => get_current_user_id(), |
| 866 |
'changetype' => 'add', |
| 867 |
]); |
| 868 |
} |
| 869 |
|
| 870 |
/** |
| 871 |
* Add log when entitlement created |
| 872 |
* |
| 873 |
* @since 0.1 |
| 874 |
* |
| 875 |
* @param integer $entitlement_id |
| 876 |
* @param array $fields |
| 877 |
* |
| 878 |
* @return void |
| 879 |
*/ |
| 880 |
public function create_entitlement( $entitlement_id, $fields ) { |
| 881 |
|
| 882 |
if ( ! $entitlement_id ) { |
| 883 |
return; |
| 884 |
} |
| 885 |
|
| 886 |
$message = sprintf( '%s <strong>%s</strong>', __( 'A new entitlement has been created for', 'erp' ), erp_hr_get_employee_name( intval( $fields['user_id'] ) ) ); |
| 887 |
|
| 888 |
erp_log()->add([ |
| 889 |
'sub_component' => 'leave', |
| 890 |
'message' => $message, |
| 891 |
'created_by' => get_current_user_id(), |
| 892 |
'changetype' => 'add', |
| 893 |
]); |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* Create hoiliday log |
| 898 |
* |
| 899 |
* @since 0.1 |
| 900 |
* |
| 901 |
* @param integer $holiday_id |
| 902 |
* @param array $fields |
| 903 |
* |
| 904 |
* @return void |
| 905 |
*/ |
| 906 |
public function create_holiday( $holiday_id, $fields ) { |
| 907 |
|
| 908 |
if ( ! $holiday_id ) { |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
erp_log()->add([ |
| 913 |
'sub_component' => 'leave', |
| 914 |
'message' => sprintf( __( 'A new holiday named <strong>%s</strong> has been created', 'erp' ), $fields['title'] ), |
| 915 |
'created_by' => get_current_user_id(), |
| 916 |
'changetype' => 'add', |
| 917 |
]); |
| 918 |
} |
| 919 |
|
| 920 |
/** |
| 921 |
* Delete holiday log insert |
| 922 |
* |
| 923 |
* @since 0.1 |
| 924 |
* |
| 925 |
* @param integer $holiday_id |
| 926 |
* |
| 927 |
* @return void |
| 928 |
*/ |
| 929 |
public function delete_holiday( $holiday_id ) { |
| 930 |
|
| 931 |
if ( ! $holiday_id ) { |
| 932 |
return; |
| 933 |
} |
| 934 |
|
| 935 |
$holiday = \WeDevs\ERP\HRM\Models\Leave_Holiday::find( $holiday_id ); |
| 936 |
|
| 937 |
if ( !$holiday ) { |
| 938 |
return; |
| 939 |
} |
| 940 |
|
| 941 |
erp_log()->add([ |
| 942 |
'sub_component' => 'leave', |
| 943 |
'message' => sprintf( __( '<strong>%s</strong> holiday has been deleted', 'erp' ), $holiday->title ), |
| 944 |
'created_by' => get_current_user_id(), |
| 945 |
'changetype' => 'delete', |
| 946 |
]); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Add log when edit holiday |
| 951 |
* |
| 952 |
* @since 0.1 |
| 953 |
* |
| 954 |
* @param integer $holiday_id |
| 955 |
* @param array $fields |
| 956 |
* |
| 957 |
* @return void |
| 958 |
*/ |
| 959 |
public function update_holiday( $holiday_id, $fields ) { |
| 960 |
|
| 961 |
if ( ! $holiday_id ) { |
| 962 |
return; |
| 963 |
} |
| 964 |
|
| 965 |
$old_holiday = \WeDevs\ERP\HRM\Models\Leave_Holiday::find( $holiday_id )->toArray(); |
| 966 |
unset( $old_holiday['created_at'], $old_holiday['updated_at'] ); |
| 967 |
|
| 968 |
$old_holiday['start'] = erp_format_date( $old_holiday['start'], 'Y-m-d' ); |
| 969 |
$old_holiday['end'] = erp_format_date( $old_holiday['end'], 'Y-m-d' ); |
| 970 |
|
| 971 |
$fields['start'] = erp_format_date( $fields['start'], 'Y-m-d' ); |
| 972 |
$fields['end'] = erp_format_date( $fields['end'], 'Y-m-d' ); |
| 973 |
|
| 974 |
$changes = $this->get_array_diff( $fields, $old_holiday, true ); |
| 975 |
|
| 976 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 977 |
$message = false; |
| 978 |
} else { |
| 979 |
array_walk ( $changes, function ( &$key ) { |
| 980 |
if ( isset( $key['start'] ) ) { |
| 981 |
$key['start_date'] = erp_format_date( $key['start'] ); |
| 982 |
unset( $key['start'] ); |
| 983 |
} |
| 984 |
|
| 985 |
if ( isset( $key['end'] ) ) { |
| 986 |
$key['end_date'] = erp_format_date( $key['end'] ); |
| 987 |
unset( $key['end'] ); |
| 988 |
} |
| 989 |
} ); |
| 990 |
$message = sprintf( __( '<strong>%s</strong> holiday has been edited', 'erp' ), $old_holiday['title'] ); |
| 991 |
} |
| 992 |
|
| 993 |
if ( $message ) { |
| 994 |
erp_log()->add([ |
| 995 |
'sub_component' => 'leave', |
| 996 |
'message' => $message, |
| 997 |
'created_by' => get_current_user_id(), |
| 998 |
'changetype' => 'edit', |
| 999 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 1000 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 1001 |
]); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* Add log when announcement create or edit |
| 1007 |
* |
| 1008 |
* @since 0.1 |
| 1009 |
* |
| 1010 |
* @param string $new_status |
| 1011 |
* @param string $old_status |
| 1012 |
* @param object $post |
| 1013 |
* |
| 1014 |
* @return void |
| 1015 |
*/ |
| 1016 |
public function announcment_log( $new_status, $old_status, $post ) { |
| 1017 |
if ( 'erp_hr_announcement' != $post->post_type ) { |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if ( 'publish' !== $new_status ) { |
| 1022 |
return; |
| 1023 |
} |
| 1024 |
|
| 1025 |
$overview = add_query_arg( array( 'page' => 'erp-hr' ), admin_url('admin.php') ); |
| 1026 |
|
| 1027 |
if ( 'publish' === $old_status ) { |
| 1028 |
$message = sprintf( __( "<strong>%s</strong> announcement has been edited", 'erp' ), $post->post_title ); |
| 1029 |
$change_type = 'edit'; |
| 1030 |
} else { |
| 1031 |
$message = sprintf( __( "<strong>%s</strong> announcement has been created", 'erp' ), $post->post_title ); |
| 1032 |
$change_type = 'add'; |
| 1033 |
} |
| 1034 |
|
| 1035 |
erp_log()->add([ |
| 1036 |
'sub_component' => 'announcement', |
| 1037 |
'message' => $message, |
| 1038 |
'created_by' => get_current_user_id(), |
| 1039 |
'changetype' => $change_type, |
| 1040 |
]); |
| 1041 |
|
| 1042 |
} |
| 1043 |
|
| 1044 |
} |
| 1045 |
|