| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\Accounting; |
| 3 |
|
| 4 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
|
| 6 |
/** |
| 7 |
* Accounting handler |
| 8 |
* |
| 9 |
* @package WP-ERP |
| 10 |
*/ |
| 11 |
class Logger { |
| 12 |
|
| 13 |
use Hooker; |
| 14 |
|
| 15 |
//'Created vendor credit for vendorname with amount $5049' |
| 16 |
|
| 17 |
/** |
| 18 |
* Load autometically when class inistantiate |
| 19 |
* |
| 20 |
* @since 0.1 |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
|
| 26 |
// Customer |
| 27 |
$this->action( 'erp_ac_after_new_customer', 'new_customer', 10, 2 ); |
| 28 |
$this->action( 'erp_ac_before_update_customer', 'update_customer' ); |
| 29 |
$this->action( 'erp_ac_new_transaction', 'new_transaction', 10, 3 ); |
| 30 |
$this->action( 'erp_ac_new_journal', 'new_journal', 10, 3 ); |
| 31 |
$this->action( 'erp_ac_new_account', 'new_account', 10, 2 ); |
| 32 |
$this->action( 'erp_ac_before_update_account', 'update_account', 10, 3 ); |
| 33 |
|
| 34 |
} |
| 35 |
//new account has been created |
| 36 |
function new_account( $insert_id, $fields ) { |
| 37 |
|
| 38 |
$url = admin_url( 'admin.php?page=erp-accounting-charts&action=view&id=' . $insert_id ); |
| 39 |
$message = sprintf( '%1$s <a href="%2$s">%3$s</a> %4$s', __( 'New account', 'erp' ), $url, $fields['name'], __( 'has been created', 'erp' ) ); |
| 40 |
|
| 41 |
erp_log()->add([ |
| 42 |
'component' => 'Accounting', |
| 43 |
'sub_component' => 'Chart of account', |
| 44 |
'old_value' => '', |
| 45 |
'new_value' => '', |
| 46 |
'message' => $message, |
| 47 |
'changetype' => 'add', |
| 48 |
'created_by' => get_current_user_id() |
| 49 |
|
| 50 |
]); |
| 51 |
} |
| 52 |
|
| 53 |
function update_account( $insert_id, $fields, $bank_details ) { |
| 54 |
if ( ! $insert_id ) return; |
| 55 |
|
| 56 |
$url = admin_url( 'admin.php?page=erp-accounting-charts&action=view&id=' . $insert_id ); |
| 57 |
$message = sprintf( '%1$s <a href="%2$s">%3$s</a> %4$s', __( 'New account', 'erp' ), $url, $fields['name'], __( 'has been created', 'erp' ) ); |
| 58 |
|
| 59 |
$bank = erp_ac_get_chart($insert_id)->toArray(); |
| 60 |
$sub_bnk_detail = []; |
| 61 |
$fild_bnk_detail = []; |
| 62 |
|
| 63 |
unset( $bank['parent'], $bank['type_id'], $bank['currency'], $bank['tax'], $bank['cash_account'] ); |
| 64 |
unset( $bank['reconcile'], $bank['system'], $fields['type_id'] ); |
| 65 |
|
| 66 |
if ( isset( $bank['bank_details'] ) && $bank['bank_details'] ) { |
| 67 |
$sub_bnk_detail = $bank['bank_details']; |
| 68 |
unset( $bank['bank_details'], $sub_bnk_detail['id'], $sub_bnk_detail['ledger_id'] ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( $bank_details ) { |
| 72 |
unset( $bank_details['id'], $bank_details['ledger_id'] ); |
| 73 |
} |
| 74 |
|
| 75 |
$field_status = $fields['active'] ? __( 'Active', 'erp' ) : __( 'Inactive', 'erp' ); |
| 76 |
$db_bank_status = $bank['active'] ? __( 'Active', 'erp' ) : __( 'Inactive', 'erp' ); |
| 77 |
|
| 78 |
unset( $fields['active'], $bank['active'] ); |
| 79 |
|
| 80 |
$fields['status'] = $field_status; |
| 81 |
$bank['status'] = $db_bank_status; |
| 82 |
|
| 83 |
$sub_bnk_detail['account_number'] = isset( $sub_bnk_detail['account_number'] ) ? $sub_bnk_detail['account_number'] : ''; |
| 84 |
$sub_bnk_detail['bank_name'] = isset( $sub_bnk_detail['bank_name'] ) ? $sub_bnk_detail['bank_name'] : ''; |
| 85 |
|
| 86 |
$fields_merge = array_merge( $fields, $bank_details ); |
| 87 |
$sub_bnk_merge = array_merge( $bank, $sub_bnk_detail ); |
| 88 |
|
| 89 |
$array_dif = $this->get_array_diff( $fields_merge, $sub_bnk_merge ); |
| 90 |
|
| 91 |
erp_log()->add([ |
| 92 |
'component' => 'Accounting', |
| 93 |
'sub_component' => 'Chart of account', |
| 94 |
'changetype' => 'edit', |
| 95 |
'old_value' => $array_dif['old_val'], |
| 96 |
'new_value' => $array_dif['new_val'], |
| 97 |
'message' => $message, |
| 98 |
'created_by' => get_current_user_id() |
| 99 |
]); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
function new_journal( $transaction_id, $args, $post ) { |
| 104 |
$url = admin_url( 'admin.php?page=erp-accounting-journal&action=view&id=' . $transaction_id ); |
| 105 |
$message = sprintf( '%1$s <a href="%2$s">%3$s</a> %4$s %5$s', |
| 106 |
__( 'Created', 'erp' ), |
| 107 |
$url, |
| 108 |
__( 'journal', 'erp' ), |
| 109 |
__( 'with amount', 'erp' ), |
| 110 |
erp_ac_get_price( $args['trans_total'] ) |
| 111 |
); |
| 112 |
|
| 113 |
erp_log()->add([ |
| 114 |
'component' => 'Accounting', |
| 115 |
'sub_component' => __( 'journal', 'erp' ), |
| 116 |
'old_value' => '', |
| 117 |
'new_value' => '', |
| 118 |
'message' => $message, |
| 119 |
'changetype' => 'add', |
| 120 |
'created_by' => get_current_user_id() |
| 121 |
|
| 122 |
]); |
| 123 |
} |
| 124 |
|
| 125 |
function new_transaction( $transaction_id, $args, $items ) { |
| 126 |
|
| 127 |
$people = erp_get_people( $args['user_id'] ); |
| 128 |
if ( is_wp_error( $people ) ) { |
| 129 |
$name = ''; |
| 130 |
} else { |
| 131 |
$people = $people ? $people : get_user_by( 'id', $args['user_id'] ); |
| 132 |
$name = isset( $people->display_name ) ? $people->display_name : $people->first_name . ' ' . $people->last_name; |
| 133 |
} |
| 134 |
|
| 135 |
$page = $args['type'] == 'sales' ? 'erp-accounting-customers' : 'erp-accounting-vendors'; |
| 136 |
$component_page = $args['type'] == 'sales' ? 'erp-accounting-sales' : 'erp-accounting-expense'; |
| 137 |
$user_url = admin_url( 'admin.php?page=' . $page . '&action=view&id=' . $args['user_id'] ); |
| 138 |
$component_url = admin_url( 'admin.php?page=' . $component_page . '&action=view&id=' . $transaction_id ); |
| 139 |
|
| 140 |
if ( $args['form_type'] == 'payment_voucher' ) { |
| 141 |
$form_type = __( 'payment voucher', 'erp' ); |
| 142 |
} else if ( $args['form_type'] == 'vendor_credit' ) { |
| 143 |
$form_type = __( 'vendor credit', 'erp' ); |
| 144 |
} else { |
| 145 |
$form_type = $args['form_type']; |
| 146 |
} |
| 147 |
|
| 148 |
$message = sprintf( '%1$s <a href="%2$s">%3$s </a> %4$s %5$s <a href="%6$s">%7$s</a> %8$s %9$s', |
| 149 |
__( 'Created', 'erp' ), |
| 150 |
$component_url, |
| 151 |
$args['type'], |
| 152 |
$form_type, |
| 153 |
__( 'for', 'erp' ), |
| 154 |
$user_url, |
| 155 |
$name, |
| 156 |
__( 'with amount', 'erp' ), |
| 157 |
erp_ac_get_price( $args['trans_total'] ) |
| 158 |
); |
| 159 |
|
| 160 |
erp_log()->add([ |
| 161 |
'component' => 'Accounting', |
| 162 |
'sub_component' => $args['type'] == 'sales' ? __( 'Sales', 'erp' ) : __( 'Expenses', 'erp' ), |
| 163 |
'old_value' => '', |
| 164 |
'new_value' => '', |
| 165 |
'message' => $message, |
| 166 |
'changetype' => 'add', |
| 167 |
'created_by' => get_current_user_id() |
| 168 |
|
| 169 |
]); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Add log when new customer created |
| 174 |
* |
| 175 |
* @since 0.1 |
| 176 |
* |
| 177 |
* @param integer $dept_id |
| 178 |
* @param array $fields |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function new_customer( $customer_id, $fields ) { |
| 183 |
|
| 184 |
$page = $fields['type'] == 'vendor' ? 'erp-accounting-vendors' : 'erp-accounting-customers'; |
| 185 |
$url = sprintf( '<a href="%1$s"><strong>%2$s</strong></a>', admin_url( 'admin.php?page='. $page .'&action=view&id=' . $customer_id ), $fields['first_name'] . ' ' . $fields['last_name'] ); |
| 186 |
$component = $fields['type'] == 'vendor' ? __( 'vendor', 'erp' ) : __( 'customer', 'erp' ); |
| 187 |
|
| 188 |
erp_log()->add([ |
| 189 |
'component' => 'Accounting', |
| 190 |
'sub_component' => $fields['type'] == 'vendor' ? __( 'Vendor', 'erp' ) : __( 'Customer', 'erp' ), |
| 191 |
'old_value' => '', |
| 192 |
'new_value' => '', |
| 193 |
'message' => esc_html( $url ) .' '. $component . __( ' has been created', 'erp' ), |
| 194 |
'changetype' => 'add', |
| 195 |
'created_by' => get_current_user_id() |
| 196 |
|
| 197 |
]); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Add log when update customer |
| 202 |
* |
| 203 |
* @since 0.1 |
| 204 |
* |
| 205 |
* @param integer $dept_id |
| 206 |
* @param array $fields |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
public function update_customer( $fields ) { |
| 211 |
$page = $fields['type'] == 'vendor' ? 'erp-accounting-vendors' : 'erp-accounting-customers'; |
| 212 |
$customer_id = isset( $fields['id'] ) ? intval( $fields['id'] ) : 0; |
| 213 |
$customer = (array) erp_get_people( $customer_id ); |
| 214 |
$component = $fields['type'] == 'vendor' ? __( 'vendor', 'erp' ) : __( 'customer', 'erp' ); |
| 215 |
|
| 216 |
if ( $customer ) { |
| 217 |
unset( $customer['created_at'], $customer['updated_at'] ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( in_array( $fields['type'], $customer['types'] ) ) { |
| 221 |
$customer['type'] = $fields['type']; |
| 222 |
} else { |
| 223 |
$customer['type'] = ''; |
| 224 |
} |
| 225 |
|
| 226 |
unset( $customer['types'] ); |
| 227 |
|
| 228 |
$changes = $this->get_array_diff( $fields, $customer ); |
| 229 |
|
| 230 |
$url = sprintf( '<a href="%1$s"><strong>%2$s</strong></a>', admin_url( 'admin.php?page='. $page .'&action=view&id=' . $fields['id'] ), $fields['first_name'] . ' ' . $fields['last_name'] ); |
| 231 |
|
| 232 |
erp_log()->add([ |
| 233 |
'component' => 'Accounting', |
| 234 |
'sub_component' => $fields['type'] == 'vendor' ? __( 'Vendor', 'erp' ) : __( 'Customer', 'erp' ), |
| 235 |
'changetype' => 'edit', |
| 236 |
'old_value' => $changes['old_val'], |
| 237 |
'new_value' => $changes['new_val'], |
| 238 |
'message' => esc_html( $url ) .' '. $component . __( ' has been updated', 'erp' ), |
| 239 |
'created_by' => get_current_user_id() |
| 240 |
]); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get different array from two array |
| 245 |
* |
| 246 |
* @since 0.1 |
| 247 |
* |
| 248 |
* @param array $new_data |
| 249 |
* @param array $old_data |
| 250 |
* |
| 251 |
* @return array |
| 252 |
*/ |
| 253 |
public function get_array_diff( $new_data, $old_data, $is_seriazie = false ) { |
| 254 |
|
| 255 |
$old_value = $new_value = []; |
| 256 |
$changes_key = array_keys( array_diff_assoc( $new_data, $old_data ) ); |
| 257 |
|
| 258 |
foreach ( $changes_key as $key => $change_field_key ) { |
| 259 |
$old_value[$change_field_key] = $old_data[$change_field_key]; |
| 260 |
$new_value[$change_field_key] = $new_data[$change_field_key]; |
| 261 |
} |
| 262 |
|
| 263 |
if ( ! $is_seriazie ) { |
| 264 |
return [ |
| 265 |
'new_val' => $new_value ? base64_encode( maybe_serialize( $new_value ) ) : '', |
| 266 |
'old_val' => $old_value ? base64_encode( maybe_serialize( $old_value ) ) : '' |
| 267 |
]; |
| 268 |
} else { |
| 269 |
return [ |
| 270 |
'new_val' => $new_value, |
| 271 |
'old_val' => $old_value |
| 272 |
]; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Add log when department deleted |
| 280 |
* |
| 281 |
* @since 0.1 |
| 282 |
* |
| 283 |
* @param integer $dept_id |
| 284 |
* |
| 285 |
* @return void |
| 286 |
*/ |
| 287 |
public function delete_department( $dept_id ) { |
| 288 |
|
| 289 |
if ( ! $dept_id ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
$department = new \WeDevs\ERP\HRM\Department( intval( $dept_id ) ); |
| 294 |
|
| 295 |
erp_log()->add([ |
| 296 |
'sub_component' => 'department', |
| 297 |
'message' => sprintf( '<strong>%s</strong> department has been deleted', $department->title ), |
| 298 |
'created_by' => get_current_user_id(), |
| 299 |
'changetype' => 'delete', |
| 300 |
]); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Add log when udpate department |
| 305 |
* |
| 306 |
* @since 0.1 |
| 307 |
* |
| 308 |
* @param integer $dept_id |
| 309 |
* @param array $fields |
| 310 |
* |
| 311 |
* @return void |
| 312 |
*/ |
| 313 |
public function update_department( $dept_id, $fields ) { |
| 314 |
|
| 315 |
if ( ! $dept_id ) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
$old_department = \WeDevs\ERP\HRM\Models\Department::find( $dept_id )->toArray(); |
| 320 |
unset( $old_department['created_at'], $old_department['updated_at'] ); |
| 321 |
|
| 322 |
$changes = $this->get_array_diff( $fields, $old_department, true ); |
| 323 |
|
| 324 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 325 |
$message = false; |
| 326 |
} else { |
| 327 |
array_walk ( $changes, function ( &$key ) { |
| 328 |
if ( isset( $key['lead'] ) ) { |
| 329 |
if( $key['lead'] ) { |
| 330 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $key['lead'] ) ); |
| 331 |
$key['department_lead'] = $employee->get_full_name(); |
| 332 |
} else { |
| 333 |
$key['department_lead'] = 'No deparment leader'; |
| 334 |
} |
| 335 |
unset( $key['lead'] ); |
| 336 |
} |
| 337 |
|
| 338 |
if ( isset( $key['parent'] ) ) { |
| 339 |
if( $key['parent'] ) { |
| 340 |
$department = new \WeDevs\ERP\HRM\Department( intval( $key['parent'] ) ); |
| 341 |
$key['parent_department'] = $department->title; |
| 342 |
} else { |
| 343 |
$key['parent_department'] = 'No Parent Department'; |
| 344 |
} |
| 345 |
unset( $key['parent'] ); |
| 346 |
} |
| 347 |
} ); |
| 348 |
|
| 349 |
$message = sprintf( '<strong>%s</strong> department has been edited', $old_department['title'] ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( $message ) { |
| 353 |
erp_log()->add([ |
| 354 |
'sub_component' => 'department', |
| 355 |
'message' => $message, |
| 356 |
'created_by' => get_current_user_id(), |
| 357 |
'changetype' => 'edit', |
| 358 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 359 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 360 |
]); |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Add log when new designation created |
| 367 |
* |
| 368 |
* @since 0.1 |
| 369 |
* |
| 370 |
* @param integer $desig_id |
| 371 |
* @param array $fields |
| 372 |
* |
| 373 |
* @return void |
| 374 |
*/ |
| 375 |
public function create_designation( $desig_id, $fields ) { |
| 376 |
erp_log()->add([ |
| 377 |
'sub_component' => 'designation', |
| 378 |
'message' => sprintf( '<strong>%s</strong> designation has been created', $fields['title'] ), |
| 379 |
'created_by' => get_current_user_id() |
| 380 |
]); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Add log when department deleted |
| 385 |
* |
| 386 |
* @since 0.1 |
| 387 |
* |
| 388 |
* @param integer $dept_id |
| 389 |
* |
| 390 |
* @return void |
| 391 |
*/ |
| 392 |
public function delete_designation( $desig ) { |
| 393 |
if ( ! $desig ) { |
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
erp_log()->add([ |
| 398 |
'sub_component' => 'designation', |
| 399 |
'message' => sprintf( '<strong>%s</strong> designation has been deleted', $desig->title ), |
| 400 |
'created_by' => get_current_user_id(), |
| 401 |
'changetype' => 'delete', |
| 402 |
]); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Add log when designation updated |
| 407 |
* |
| 408 |
* @since 0.1 |
| 409 |
* |
| 410 |
* @param integer $desig_id |
| 411 |
* @param array $fields |
| 412 |
* |
| 413 |
* @return void |
| 414 |
*/ |
| 415 |
public function update_designation( $desig_id, $fields ) { |
| 416 |
if ( ! $desig_id ) { |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
$old_desig = \WeDevs\ERP\HRM\Models\Designation::find( $desig_id )->toArray(); |
| 421 |
unset( $old_desig['created_at'], $old_desig['updated_at'] ); |
| 422 |
|
| 423 |
$changes = $this->get_array_diff( $fields, $old_desig ); |
| 424 |
|
| 425 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 426 |
$message = false; |
| 427 |
} else { |
| 428 |
$message = sprintf( '<strong>%s</strong> designation has been edited', $old_desig['title'] ); |
| 429 |
} |
| 430 |
|
| 431 |
if ( $message ) { |
| 432 |
erp_log()->add([ |
| 433 |
'sub_component' => 'designation', |
| 434 |
'message' => $message, |
| 435 |
'created_by' => get_current_user_id(), |
| 436 |
'changetype' => 'edit', |
| 437 |
'old_value' => $changes['old_val'], |
| 438 |
'new_value' => $changes['new_val'] |
| 439 |
]); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Logging for creating policy |
| 445 |
* |
| 446 |
* @since 0.1 |
| 447 |
* |
| 448 |
* @param integer $policy_id |
| 449 |
* @param array $fields |
| 450 |
* |
| 451 |
* @return void |
| 452 |
*/ |
| 453 |
public function create_policy( $policy_id, $fields ) { |
| 454 |
|
| 455 |
if ( ! $policy_id ) { |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
erp_log()->add([ |
| 460 |
'sub_component' => 'leave', |
| 461 |
'message' => sprintf( '<strong>%s</strong> policy has been created', $fields['name'] ), |
| 462 |
'created_by' => get_current_user_id(), |
| 463 |
'changetype' => 'add', |
| 464 |
]); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Adding log when policy deleted |
| 469 |
* |
| 470 |
* @since 0.1 |
| 471 |
* |
| 472 |
* @param integer $policy_id |
| 473 |
* |
| 474 |
* @return void |
| 475 |
*/ |
| 476 |
public function delete_policy( $policy_id ) { |
| 477 |
|
| 478 |
if ( ! $policy_id ) { |
| 479 |
return; |
| 480 |
} |
| 481 |
|
| 482 |
$policy = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy_id ); |
| 483 |
|
| 484 |
if ( !$policy ) { |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
erp_log()->add([ |
| 489 |
'sub_component' => 'leave', |
| 490 |
'message' => sprintf( '<strong>%s</strong> policy has been deleted', $policy->name ), |
| 491 |
'created_by' => get_current_user_id(), |
| 492 |
'changetype' => 'delete', |
| 493 |
]); |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Add log when udpate policy |
| 498 |
* |
| 499 |
* @since 0.1 |
| 500 |
* |
| 501 |
* @param integer $policy_id |
| 502 |
* @param array $fields |
| 503 |
* |
| 504 |
* @return void |
| 505 |
*/ |
| 506 |
public function update_policy( $policy_id, $fields ) { |
| 507 |
|
| 508 |
if ( ! $policy_id ) { |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
$old_policy = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy_id )->toArray(); |
| 513 |
unset( $old_policy['created_at'], $old_policy['updated_at'], $fields['instant_apply'] ) ; |
| 514 |
|
| 515 |
$old_policy['effective_date'] = erp_format_date( $old_policy['effective_date'], 'Y-m-d' ); |
| 516 |
$fields['effective_date'] = erp_format_date( $fields['effective_date'], 'Y-m-d' ); |
| 517 |
|
| 518 |
if ( isset( $fields['activate'] ) && $fields['activate'] == 1 ) { |
| 519 |
unset( $fields['execute_day'], $old_policy['execute_day'] ); |
| 520 |
} |
| 521 |
|
| 522 |
$changes = $this->get_array_diff( $fields, $old_policy, true ); |
| 523 |
|
| 524 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 525 |
$message = false; |
| 526 |
} else { |
| 527 |
array_walk ( $changes, function ( &$key ) { |
| 528 |
|
| 529 |
if ( isset( $key['color'] ) ) { |
| 530 |
$key['calender_color'] = sprintf( '<div style="width:60px; height:20px; background-color:%s"></div>', $key['color'] ); |
| 531 |
unset( $key['color'] ); |
| 532 |
} |
| 533 |
|
| 534 |
if ( isset( $key['department'] ) ) { |
| 535 |
if ( $key['department'] == '-1' ) { |
| 536 |
$key['department'] = __( 'All Department', 'erp' ); |
| 537 |
} else { |
| 538 |
$department = new \WeDevs\ERP\HRM\Department( intval( $key['department'] ) ); |
| 539 |
$key['department'] = $department->title; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
if ( isset( $key['designation'] ) ) { |
| 544 |
if ( $key['designation'] == '-1' ) { |
| 545 |
$key['designation'] = __( 'All Designation', 'erp' ); |
| 546 |
} else { |
| 547 |
$designation = new \WeDevs\ERP\HRM\Designation( intval( $key['designation'] ) ); |
| 548 |
$key['designation'] = $designation->title; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if ( isset( $key['location'] ) ) { |
| 553 |
if ( $key['location'] == '-1' ) { |
| 554 |
$key['location'] = __( 'All Location', 'erp' ); |
| 555 |
} else { |
| 556 |
$location = erp_company_get_location_dropdown_raw(); |
| 557 |
$key['location'] = $location[$key['location']]; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
if ( isset( $key['gender'] ) ) { |
| 562 |
$gender = erp_hr_get_genders( __( 'All', 'erp' ) ); |
| 563 |
$key['gender'] = $gender[$key['gender']]; |
| 564 |
} |
| 565 |
|
| 566 |
if ( isset( $key['marital'] ) ) { |
| 567 |
$marital = erp_hr_get_marital_statuses( __( 'All', 'erp' ) ); |
| 568 |
$key['marital'] = $marital[$key['marital']]; |
| 569 |
} |
| 570 |
|
| 571 |
if ( isset( $key['activate'] ) ) { |
| 572 |
$activate = array( '1' => __( 'Immediately', 'accounting'), '2' => __('After X Days', 'accounting'), '3' => __( 'Manually', 'accounting') ); |
| 573 |
|
| 574 |
if ( $key['activate'] == 2 ) { |
| 575 |
$key['activation'] = str_replace( 'X', $key['execute_day'], $activate[$key['activate']] ); |
| 576 |
} else { |
| 577 |
$key['activation'] = $activate[$key['activate']]; |
| 578 |
} |
| 579 |
|
| 580 |
unset( $key['activate'] ); |
| 581 |
unset( $key['execute_day'] ); |
| 582 |
} |
| 583 |
|
| 584 |
if ( isset( $key['effective_date'] ) ) { |
| 585 |
$key['policy_effective_date'] = erp_format_date( $key['effective_date'] ); |
| 586 |
unset( $key['effective_date'] ); |
| 587 |
} |
| 588 |
|
| 589 |
} ); |
| 590 |
|
| 591 |
$message = sprintf( '<strong>%s</strong> policy has been edited', $old_policy['name'] ); |
| 592 |
} |
| 593 |
|
| 594 |
if ( $message ) { |
| 595 |
erp_log()->add([ |
| 596 |
'sub_component' => 'leave', |
| 597 |
'message' => $message, |
| 598 |
'created_by' => get_current_user_id(), |
| 599 |
'changetype' => 'edit', |
| 600 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 601 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 602 |
]); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* Add log when someone take leave |
| 608 |
* |
| 609 |
* @since 0.1 |
| 610 |
* |
| 611 |
* @param integer $request_id |
| 612 |
* @param array $request |
| 613 |
* @param array $leaves |
| 614 |
* |
| 615 |
* @return void |
| 616 |
*/ |
| 617 |
public function create_leave_request( $request_id, $request, $leaves ) { |
| 618 |
|
| 619 |
if ( ! $request_id ) { |
| 620 |
return; |
| 621 |
} |
| 622 |
|
| 623 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $request['user_id'] ) ); |
| 624 |
|
| 625 |
$message = sprintf( '<strong>%s</strong> take leave from <strong>%s</strong> to <strong>%s</strong> about <strong>%d</strong> days', |
| 626 |
$employee->get_full_name(), |
| 627 |
erp_format_date( $request['start_date'] ), |
| 628 |
erp_format_date( $request['end_date'] ), |
| 629 |
$request['days'] |
| 630 |
); |
| 631 |
|
| 632 |
erp_log()->add([ |
| 633 |
'sub_component' => 'leave', |
| 634 |
'message' => $message, |
| 635 |
'created_by' => get_current_user_id(), |
| 636 |
'changetype' => 'add', |
| 637 |
]); |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Add log when entitlement created |
| 642 |
* |
| 643 |
* @since 0.1 |
| 644 |
* |
| 645 |
* @param integer $entitlement_id |
| 646 |
* @param array $fields |
| 647 |
* |
| 648 |
* @return void |
| 649 |
*/ |
| 650 |
public function create_entitlement( $entitlement_id, $fields ) { |
| 651 |
|
| 652 |
if ( ! $entitlement_id ) { |
| 653 |
return; |
| 654 |
} |
| 655 |
|
| 656 |
$employee = new \WeDevs\ERP\HRM\Employee( intval( $fields['user_id'] ) ); |
| 657 |
$message = sprintf( '%s <strong>%s</strong>', __( 'A new entitlement has been created for'), $employee->get_full_name() ); |
| 658 |
|
| 659 |
erp_log()->add([ |
| 660 |
'sub_component' => 'leave', |
| 661 |
'message' => $message, |
| 662 |
'created_by' => get_current_user_id(), |
| 663 |
'changetype' => 'add', |
| 664 |
]); |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* Create hoiliday log |
| 669 |
* |
| 670 |
* @since 0.1 |
| 671 |
* |
| 672 |
* @param integer $holiday_id |
| 673 |
* @param array $fields |
| 674 |
* |
| 675 |
* @return void |
| 676 |
*/ |
| 677 |
public function create_holiday( $holiday_id, $fields ) { |
| 678 |
|
| 679 |
if ( ! $holiday_id ) { |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
erp_log()->add([ |
| 684 |
'sub_component' => 'leave', |
| 685 |
'message' => sprintf( 'A new holiday named <strong>%s</strong> has been created', $fields['title'] ), |
| 686 |
'created_by' => get_current_user_id(), |
| 687 |
'changetype' => 'add', |
| 688 |
]); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Delete holiday log insert |
| 693 |
* |
| 694 |
* @since 0.1 |
| 695 |
* |
| 696 |
* @param integer $holiday_id |
| 697 |
* |
| 698 |
* @return void |
| 699 |
*/ |
| 700 |
public function delete_holiday( $holiday_id ) { |
| 701 |
|
| 702 |
if ( ! $holiday_id ) { |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
$holiday = \WeDevs\ERP\HRM\Models\Leave_Holiday::find( $holiday_id ); |
| 707 |
|
| 708 |
if ( !$holiday ) { |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
erp_log()->add([ |
| 713 |
'sub_component' => 'leave', |
| 714 |
'message' => sprintf( '<strong>%s</strong> holiday has been deleted', $holiday->title ), |
| 715 |
'created_by' => get_current_user_id(), |
| 716 |
'changetype' => 'delete', |
| 717 |
]); |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Add log when edit holiday |
| 722 |
* |
| 723 |
* @since 0.1 |
| 724 |
* |
| 725 |
* @param integer $holiday_id |
| 726 |
* @param array $fields |
| 727 |
* |
| 728 |
* @return void |
| 729 |
*/ |
| 730 |
public function update_holiday( $holiday_id, $fields ) { |
| 731 |
|
| 732 |
if ( ! $holiday_id ) { |
| 733 |
return; |
| 734 |
} |
| 735 |
|
| 736 |
$old_holiday = \WeDevs\ERP\HRM\Models\Leave_Holiday::find( $holiday_id )->toArray(); |
| 737 |
unset( $old_holiday['created_at'], $old_holiday['updated_at'] ); |
| 738 |
|
| 739 |
$old_holiday['start'] = erp_format_date( $old_holiday['start'], 'Y-m-d' ); |
| 740 |
$old_holiday['end'] = erp_format_date( $old_holiday['end'], 'Y-m-d' ); |
| 741 |
|
| 742 |
$fields['start'] = erp_format_date( $fields['start'], 'Y-m-d' ); |
| 743 |
$fields['end'] = erp_format_date( $fields['end'], 'Y-m-d' ); |
| 744 |
|
| 745 |
$changes = $this->get_array_diff( $fields, $old_holiday, true ); |
| 746 |
|
| 747 |
if ( empty( $changes['old_val'] ) && empty( $changes['new_val'] ) ) { |
| 748 |
$message = false; |
| 749 |
} else { |
| 750 |
array_walk ( $changes, function ( &$key ) { |
| 751 |
if ( isset( $key['start'] ) ) { |
| 752 |
$key['start_date'] = erp_format_date( $key['start'] ); |
| 753 |
unset( $key['start'] ); |
| 754 |
} |
| 755 |
|
| 756 |
if ( isset( $key['end'] ) ) { |
| 757 |
$key['end_date'] = erp_format_date( $key['end'] ); |
| 758 |
unset( $key['end'] ); |
| 759 |
} |
| 760 |
} ); |
| 761 |
$message = sprintf( '<strong>%s</strong> holiday has been edited', $old_holiday['title'] ); |
| 762 |
} |
| 763 |
|
| 764 |
if ( $message ) { |
| 765 |
erp_log()->add([ |
| 766 |
'sub_component' => 'leave', |
| 767 |
'message' => $message, |
| 768 |
'created_by' => get_current_user_id(), |
| 769 |
'changetype' => 'edit', |
| 770 |
'old_value' => $changes['old_val'] ? base64_encode( maybe_serialize( $changes['old_val'] ) ) : '', |
| 771 |
'new_value' => $changes['new_val'] ? base64_encode( maybe_serialize( $changes['new_val'] ) ) : '' |
| 772 |
]); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
* Add log when announcement create or edit |
| 778 |
* |
| 779 |
* @since 0.1 |
| 780 |
* |
| 781 |
* @param string $new_status |
| 782 |
* @param string $old_status |
| 783 |
* @param object $post |
| 784 |
* |
| 785 |
* @return void |
| 786 |
*/ |
| 787 |
public function announcment_log( $new_status, $old_status, $post ) { |
| 788 |
if ( 'erp_hr_announcement' != $post->post_type ) { |
| 789 |
return; |
| 790 |
} |
| 791 |
|
| 792 |
if ( 'publish' !== $new_status ) { |
| 793 |
return; |
| 794 |
} |
| 795 |
|
| 796 |
$overview = add_query_arg( array( 'page' => 'erp-hr' ), admin_url('admin.php') ); |
| 797 |
|
| 798 |
if ( 'publish' === $old_status ) { |
| 799 |
$message = sprintf( "<strong>%s</strong> announcement has been edited", $post->post_title ); |
| 800 |
$change_type = 'edit'; |
| 801 |
} else { |
| 802 |
$message = sprintf( "<strong>%s</strong> announcement has been created", $post->post_title ); |
| 803 |
$change_type = 'add'; |
| 804 |
} |
| 805 |
|
| 806 |
erp_log()->add([ |
| 807 |
'sub_component' => 'announcement', |
| 808 |
'message' => $message, |
| 809 |
'created_by' => get_current_user_id(), |
| 810 |
'changetype' => $change_type, |
| 811 |
]); |
| 812 |
|
| 813 |
} |
| 814 |
|
| 815 |
} |
| 816 |
|