PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.10 1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 All 144 releases
erp / modules / hrm / includes / functions-employee.php

functions-employee.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at modules/hrm/includes/functions-employee.php

1,004 lines 27.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Delete an employee if removed from WordPress usre table
5 *
6 * @param int the user id
7 *
8 * @return void
9 */
10 function erp_hr_employee_on_delete( $user_id, $hard = 0 ) {
11 global $wpdb;
12
13 $user = get_user_by( 'id', $user_id );
14
15 if ( ! $user ) {
16 return;
17 }
18
19 $role = reset( $user->roles );
20
21 if ( 'employee' == $role ) {
22 \WeDevs\ERP\HRM\Models\Employee::where( 'user_id', $user_id )->withTrashed()->forceDelete();
23 }
24 }
25
26 /**
27 * Create a new employee
28 *
29 * @param array arguments
30 *
31 * @return int employee id
32 */
33 function erp_hr_employee_create( $args = array() ) {
34 $employee = new \WeDevs\ERP\HRM\Employee( null );
35 $result = $employee->create_employee( $args );
36
37 if ( is_wp_error( $result ) ) {
38 return $result->get_error_message();
39 }
40
41 return $result->user_id;
42 }
43
44 /**
45 * Get all employees from a company
46 *
47 * @param int $company_id company id
48 * @param bool $no_object if set true, Employee object will be
49 * returned as array. $wpdb rows otherwise
50 *
51 * @return array the employees
52 */
53 function erp_hr_get_employees( $args = array() ) {
54 global $wpdb;
55
56 $defaults = array(
57 'number' => 20,
58 'offset' => 0,
59 'orderby' => 'hiring_date',
60 'order' => 'DESC',
61 'no_object' => false,
62 'count' => false
63 );
64
65 $args = wp_parse_args( $args, $defaults );
66 $where = array();
67
68 $employee_tbl = $wpdb->prefix . 'erp_hr_employees';
69 $employees = \WeDevs\ERP\HRM\Models\Employee::select( array( $employee_tbl . '.user_id', 'display_name' ) )
70 ->leftJoin( $wpdb->users, $employee_tbl . '.user_id', '=', $wpdb->users . '.ID' );
71
72 if ( isset( $args['designation'] ) && $args['designation'] != '-1' ) {
73 $employees = $employees->where( 'designation', $args['designation'] );
74 }
75
76 if ( isset( $args['department'] ) && $args['department'] != '-1' ) {
77 $employees = $employees->where( 'department', $args['department'] );
78 }
79
80 if ( isset( $args['location'] ) && $args['location'] != '-1' ) {
81 $employees = $employees->where( 'location', $args['location'] );
82 }
83
84 if ( isset( $args['type'] ) && $args['type'] != '-1' ) {
85 $employees = $employees->where( 'type', $args['type'] );
86 }
87
88 if ( isset( $args['status'] ) && ! empty( $args['status'] ) ) {
89 if ( $args['status'] == 'trash' ) {
90 $employees = $employees->onlyTrashed();
91 } else {
92 if ( $args['status'] != 'all' ) {
93 $employees = $employees->where( 'status', $args['status'] );
94 }
95 }
96 } else {
97 $employees = $employees->where( 'status', 'active' );
98 }
99
100 if ( isset( $args['s'] ) && ! empty( $args['s'] ) ) {
101 $arg_s = $args['s'];
102 $employees = $employees->where( 'display_name', 'LIKE', "%$arg_s%" );
103 }
104
105 if ( 'employee_name' === $args['orderby'] ) {
106 $employees = $employees->leftJoin( $wpdb->usermeta . ' as umeta', function ( $join ) use ( $wpdb, $employee_tbl ) {
107 $join->on( $employee_tbl . '.user_id', '=', 'umeta.user_id' )
108 ->where( 'umeta.meta_key', '=', 'first_name' );
109 } );
110
111 $args['orderby'] = 'umeta.meta_value';
112 }
113
114 $cache_key = 'erp-get-employees-' . md5( serialize( $args ) );
115 $results = wp_cache_get( $cache_key, 'erp' );
116 $users = array();
117
118 // Check if want all data without any pagination
119 if ( $args['number'] != '-1' && ! $args['count'] ) {
120 $employees = $employees->skip( $args['offset'] )->take( $args['number'] );
121 }
122
123 // Check if args count true, then return total count customer according to above filter
124 if ( $args['count'] ) {
125 return $employees->count();
126 }
127
128 if ( false === $results ) {
129
130 $results = $employees
131 ->orderBy( $args['orderby'], $args['order'] )
132 ->get()
133 ->toArray();
134
135 $results = erp_array_to_object( $results );
136 wp_cache_set( $cache_key, $results, 'erp', HOUR_IN_SECONDS );
137 }
138
139 if ( $results ) {
140 foreach ( $results as $key => $row ) {
141
142 if ( true === $args['no_object'] ) {
143 $users[] = $row;
144 } else {
145 $users[] = new \WeDevs\ERP\HRM\Employee( intval( $row->user_id ) );
146 }
147 }
148 }
149
150 return $users;
151 }
152
153
154 /**
155 * Get all employees from a company
156 *
157 * @param int $company_id company id
158 * @param bool $no_object if set true, Employee object will be
159 * returned as array. $wpdb rows otherwise
160 *
161 * @return array the employees
162 */
163 function erp_hr_count_employees() {
164
165 $where = array();
166
167 $employee = new \WeDevs\ERP\HRM\Models\Employee();
168
169 if ( isset( $args['designation'] ) && ! empty( $args['designation'] ) ) {
170 $designation = array( 'designation' => $args['designation'] );
171 $where = array_merge( $designation, $where );
172 }
173
174 if ( isset( $args['department'] ) && ! empty( $args['department'] ) ) {
175 $department = array( 'department' => $args['department'] );
176 $where = array_merge( $where, $department );
177 }
178
179 if ( isset( $args['location'] ) && ! empty( $args['location'] ) ) {
180 $location = array( 'location' => $args['location'] );
181 $where = array_merge( $where, $location );
182 }
183
184 if ( isset( $args['status'] ) && ! empty( $args['status'] ) ) {
185 $status = array( 'status' => $args['status'] );
186 $where = array_merge( $where, $status );
187 }
188
189 $counts = $employee->where( $where )->count();
190
191 return $counts;
192 }
193
194
195 /**
196 * Get Employee status count
197 *
198 * @since 0.1
199 *
200 * @return array
201 */
202 function erp_hr_employee_get_status_count() {
203 global $wpdb;
204
205 $statuses = array( 'all' => __( 'All', 'erp' ) ) + erp_hr_get_employee_statuses();
206 $counts = array();
207
208 foreach ( $statuses as $status => $label ) {
209 $counts[ $status ] = array( 'count' => 0, 'label' => $label );
210 }
211
212 $cache_key = 'erp-hr-employee-status-counts';
213 $results = wp_cache_get( $cache_key, 'erp' );
214
215 if ( false === $results ) {
216
217 $employee = new \WeDevs\ERP\HRM\Models\Employee();
218 $db = new \WeDevs\ORM\Eloquent\Database();
219
220 $results = $employee->select( array( 'status', $db->raw( 'COUNT(id) as num' ) ) )
221 ->where( 'status', '!=', '0' )
222 ->groupBy( 'status' )
223 ->get()->toArray();
224
225 wp_cache_set( $cache_key, $results, 'erp' );
226 }
227
228 foreach ( $results as $row ) {
229 if ( array_key_exists( $row['status'], $counts ) ) {
230 $counts[ $row['status'] ]['count'] = (int) $row['num'];
231 }
232
233 $counts['all']['count'] += (int) $row['num'];
234 }
235
236 return $counts;
237 }
238
239 /**
240 * Count trash employee
241 *
242 * @since 0.1
243 *
244 * @return int [no of trash employee]
245 */
246 function erp_hr_count_trashed_employees() {
247 $employee = new \WeDevs\ERP\HRM\Models\Employee();
248
249 return $employee->onlyTrashed()->count();
250 }
251
252 /**
253 * Employee Restore from trash
254 *
255 * @since 0.1
256 *
257 * @param array|int $employee_ids
258 *
259 * @return void
260 */
261 function erp_employee_restore( $employee_ids ) {
262 if ( empty( $employee_ids ) ) {
263 return;
264 }
265
266 if ( is_array( $employee_ids ) ) {
267 foreach ( $employee_ids as $key => $user_id ) {
268 \WeDevs\ERP\HRM\Models\Employee::withTrashed()->where( 'user_id', $user_id )->restore();
269 }
270 }
271
272 if ( is_int( $employee_ids ) ) {
273 \WeDevs\ERP\HRM\Models\Employee::withTrashed()->where( 'user_id', $employee_ids )->restore();
274 }
275 }
276
277 /**
278 * Employee Delete
279 *
280 * @since 1.0.0
281 * @since 1.2.0 After delete an employee, remove HR roles instead of
282 * remove the related wp user
283 *
284 * @param array|int $employee_ids
285 * @param $force boolean
286 * @return void
287 */
288 function erp_employee_delete( $employee_ids, $force = false ) {
289 if ( empty( $employee_ids ) ) {
290 return;
291 }
292
293 $employees = [];
294
295 if ( is_array( $employee_ids ) ) {
296 foreach ( $employee_ids as $key => $user_id ) {
297 $employees[] = $user_id;
298 }
299 } else if ( is_int( $employee_ids ) ) {
300 $employees[] = $employee_ids;
301 }
302
303 // still do we have any ids to delete?
304 if ( ! $employees ) {
305 return;
306 }
307
308 // seems like we got some
309 foreach ( $employees as $employee_wp_user_id ) {
310
311 do_action( 'erp_hr_delete_employee', $employee_wp_user_id, $force );
312
313 if ( $force ) {
314
315 // find leave entitlements and leave requests and delete them as well
316 \WeDevs\ERP\HRM\Models\Leave_request::where( 'user_id', '=', $employee_wp_user_id )->delete();
317 \WeDevs\ERP\HRM\Models\Leave_Entitlement::where( 'user_id', '=', $employee_wp_user_id )->delete();
318 \WeDevs\ERP\HRM\Models\Education::where( 'employee_id', '=', $employee_wp_user_id )->delete();
319 \WeDevs\ERP\HRM\Models\Performance::where( 'employee_id', '=', $employee_wp_user_id )->delete();
320 \WeDevs\ERP\HRM\Models\Work_Experience::where( 'employee_id', '=', $employee_wp_user_id )->delete();
321 \WeDevs\ERP\HRM\Models\Employee_History::where( 'user_id', '=', $employee_wp_user_id )->delete();
322 \WeDevs\ERP\HRM\Models\Employee_Note::where( 'user_id', '=', $employee_wp_user_id )->delete();
323 \WeDevs\ERP\HRM\Models\Announcement::where( 'user_id', '=', $employee_wp_user_id )->delete();
324
325 \WeDevs\ERP\HRM\Models\Employee::where( 'user_id', $employee_wp_user_id )->withTrashed()->forceDelete();
326 $wp_user = get_userdata( $employee_wp_user_id );
327 $wp_user->remove_role( erp_hr_get_manager_role() );
328 $wp_user->remove_role( erp_hr_get_employee_role() );
329
330 //finally remove from wordpress user
331 $remove_wp_user = get_option('erp_hrm_remove_wp_user', 'no');
332 if('yes' == $remove_wp_user ){
333 $current_user = get_current_user_id();
334 wp_delete_user($employee_wp_user_id, $current_user);
335 }
336
337 } else {
338 \WeDevs\ERP\HRM\Models\Employee::where( 'user_id', $employee_wp_user_id )->delete();
339 }
340
341 do_action( 'erp_hr_after_delete_employee', $employee_wp_user_id, $force );
342 }
343
344 }
345
346 /**
347 * Get Todays Birthday
348 *
349 * @since 0.1
350 * @since 1.1.14 Add where condition to remove terminated employees
351 *
352 * @return object collection of user_id
353 */
354 function erp_hr_get_todays_birthday() {
355
356 $db = new \WeDevs\ORM\Eloquent\Database();
357
358 return erp_array_to_object( \WeDevs\ERP\HRM\Models\Employee::select( 'user_id' )
359 ->where( $db->raw( "DATE_FORMAT( `date_of_birth`, '%m %d' )" ), \Carbon\Carbon::today()->format( 'm d' ) )
360 ->where( 'termination_date', '0000-00-00' )
361 ->get()
362 ->toArray() );
363 }
364
365 /**
366 * Get next seven days birthday
367 *
368 * @since 0.1
369 * @since 1.1.14 Add where condition to remove terminated employees
370 *
371 * @return object user_id, date_of_birth
372 */
373 function erp_hr_get_next_seven_days_birthday() {
374
375 $db = new \WeDevs\ORM\Eloquent\Database();
376
377 return erp_array_to_object( \WeDevs\ERP\HRM\Models\Employee::select( array( 'user_id', 'date_of_birth' ) )
378 ->where( $db->raw( "DATE_FORMAT( `date_of_birth`, '%m %d' )" ), '>', \Carbon\Carbon::today()->format( 'm d' ) )
379 ->where( $db->raw( "DATE_FORMAT( `date_of_birth`, '%m %d' )" ), '<=', \Carbon\Carbon::tomorrow()->addWeek()->format( 'm d' ) )
380 ->where( 'termination_date', '0000-00-00' )
381 ->get()
382 ->toArray() );
383 }
384
385 /**
386 * Get the raw employees dropdown
387 *
388 * @param int company id
389 *
390 * @return array the key-value paired employees
391 */
392 function erp_hr_get_employees_dropdown_raw( $exclude = null ) {
393 $employees = erp_hr_get_employees( [ 'number' => - 1, 'no_object' => true ] );
394 $dropdown = array( 0 => __( '- Select Employee -', 'erp' ) );
395
396 if ( $employees ) {
397 foreach ( $employees as $key => $employee ) {
398 if ( $exclude && $employee->user_id == $exclude ) {
399 continue;
400 }
401
402 $dropdown[ $employee->user_id ] = $employee->display_name;
403 }
404 }
405
406 return $dropdown;
407 }
408
409 /**
410 * Get company employees dropdown
411 *
412 * @param int company id
413 * @param string selected department
414 *
415 * @return string the dropdown
416 */
417 function erp_hr_get_employees_dropdown( $selected = '' ) {
418 $employees = erp_hr_get_employees_dropdown_raw();
419 $dropdown = '';
420
421 if ( $employees ) {
422 foreach ( $employees as $key => $title ) {
423 $dropdown .= sprintf( "<option value='%s'%s>%s</option>\n", $key, selected( $selected, $key, false ), $title );
424 }
425 }
426
427 return $dropdown;
428 }
429
430 /**
431 * Get the registered employee statuses
432 *
433 * @return array the employee statuses
434 */
435 function erp_hr_get_employee_statuses() {
436 $statuses = array(
437 'active' => __( 'Active', 'erp' ),
438 'terminated' => __( 'Terminated', 'erp' ),
439 'deceased' => __( 'Deceased', 'erp' ),
440 'resigned' => __( 'Resigned', 'erp' )
441 );
442
443 return apply_filters( 'erp_hr_employee_statuses', $statuses );
444 }
445
446 /**
447 * Get the registered employee statuses
448 *
449 * @return array the employee statuses
450 */
451 function erp_hr_get_employee_statuses_icons( $selected = null ) {
452 $statuses = apply_filters( 'erp_hr_employee_statuses_icons', array(
453 'active' => sprintf( '<span class="erp-tips dashicons dashicons-yes" title="%s"></span>', __( 'Active', 'erp' ) ),
454 'terminated' => sprintf( '<span class="erp-tips dashicons dashicons-dismiss" title="%s"></span>', __( 'Terminated', 'erp' ) ),
455 'deceased' => sprintf( '<span class="erp-tips dashicons dashicons-marker" title="%s"></span>', __( 'Deceased', 'erp' ) ),
456 'resigned' => sprintf( '<span class="erp-tips dashicons dashicons-warning" title="%s"></span>', __( 'Resigned', 'erp' ) )
457 ) );
458
459 if ( $selected && array_key_exists( $selected, $statuses ) ) {
460 return $statuses[ $selected ];
461 }
462
463 return false;
464 }
465
466
467 /**
468 * Get the registered employee statuses
469 *
470 * @return array the employee statuses
471 */
472 function erp_hr_get_employee_types() {
473 $types = array(
474 'permanent' => __( 'Full Time', 'erp' ),
475 'parttime' => __( 'Part Time', 'erp' ),
476 'contract' => __( 'On Contract', 'erp' ),
477 'temporary' => __( 'Temporary', 'erp' ),
478 'trainee' => __( 'Trainee', 'erp' )
479 );
480
481 return apply_filters( 'erp_hr_employee_types', $types );
482 }
483
484 /**
485 * Get the registered employee hire sources
486 *
487 * @return array the employee hire sources
488 */
489 function erp_hr_get_employee_sources() {
490 $sources = array(
491 'direct' => __( 'Direct', 'erp' ),
492 'referral' => __( 'Referral', 'erp' ),
493 'web' => __( 'Web', 'erp' ),
494 'newspaper' => __( 'Newspaper', 'erp' ),
495 'advertisement' => __( 'Advertisement', 'erp' ),
496 'social' => __( 'Social Network', 'erp' ),
497 'other' => __( 'Other', 'erp' ),
498 );
499
500 return apply_filters( 'erp_hr_employee_sources', $sources );
501 }
502
503 /**
504 * Get marital statuses
505 *
506 * @return array all the statuses
507 */
508 function erp_hr_get_marital_statuses( $select_text = null ) {
509
510 if ( $select_text ) {
511 $statuses = array(
512 '-1' => $select_text,
513 'single' => __( 'Single', 'erp' ),
514 'married' => __( 'Married', 'erp' ),
515 'widowed' => __( 'Widowed', 'erp' )
516 );
517 } else {
518 $statuses = array(
519 'single' => __( 'Single', 'erp' ),
520 'married' => __( 'Married', 'erp' ),
521 'widowed' => __( 'Widowed', 'erp' )
522 );
523 }
524
525 return apply_filters( 'erp_hr_marital_statuses', $statuses );
526 }
527
528 /**
529 * Get Terminate Type
530 *
531 * @return array all the type
532 */
533 function erp_hr_get_terminate_type( $selected = null ) {
534 $type = apply_filters( 'erp_hr_terminate_type', [
535 'voluntary' => __( 'Voluntary', 'erp' ),
536 'involuntary' => __( 'Involuntary', 'erp' )
537 ] );
538
539 if ( $selected ) {
540 return ( isset( $type[ $selected ] ) ) ? $type[ $selected ] : '';
541 }
542
543 return $type;
544 }
545
546 /**
547 * Get Terminate Reason
548 *
549 * @return array all the reason
550 */
551 function erp_hr_get_terminate_reason( $selected = null ) {
552 $reason = apply_filters( 'erp_hr_terminate_reason', [
553 'attendance' => __( 'Attendance', 'erp' ),
554 'better_employment' => __( 'Better Employment Conditions', 'erp' ),
555 'career_prospect' => __( 'Career Prospect', 'erp' ),
556 'death' => __( 'Death', 'erp' ),
557 'desertion' => __( 'Desertion', 'erp' ),
558 'dismissed' => __( 'Dismissed', 'erp' ),
559 'dissatisfaction' => __( 'Dissatisfaction with the job', 'erp' ),
560 'higher_pay' => __( 'Higher Pay', 'erp' ),
561 'other_employement' => __( 'Other Employment', 'erp' ),
562 'personality_conflicts' => __( 'Personality Conflicts', 'erp' ),
563 'relocation' => __( 'Relocation', 'erp' ),
564 'retirement' => __( 'Retirement', 'erp' ),
565 ] );
566
567 if ( $selected ) {
568 return ( isset( $reason[ $selected ] ) ) ? $reason[ $selected ] : '';
569 }
570
571 return $reason;
572 }
573
574 /**
575 * Get Terminate Reason
576 *
577 * @return array all the reason
578 */
579 function erp_hr_get_terminate_rehire_options( $selected = null ) {
580 $reason = apply_filters( 'erp_hr_terminate_rehire_option', array(
581 'yes' => __( 'Yes', 'erp' ),
582 'no' => __( 'No', 'erp' ),
583 'upon_review' => __( 'Upon Review', 'erp' )
584 ) );
585
586 if ( $selected ) {
587 return ( isset( $reason[ $selected ] ) ) ? $reason[ $selected ] : '';
588 }
589
590 return $reason;
591 }
592
593 /**
594 * Employee terminated action
595 *
596 * @since 1.0.0
597 *
598 * @param $data
599 *
600 * @return $this|string|\WP_Error
601 */
602 function erp_hr_employee_terminate( $data ) {
603 if ( ! $data['user_id'] ) {
604 return new WP_Error( 'no-user-id', 'No User id found' );
605 }
606
607 $employee = new \WeDevs\ERP\HRM\Employee( intval( $data['user_id'] ) );
608 $result = $employee->terminate( $data );
609
610 if ( is_wp_error( $result ) ) {
611 return $result->get_error_message();
612 }
613
614 return $result;
615 }
616
617 /**
618 * Get marital statuses
619 *
620 * @return array all the statuses
621 */
622 function erp_hr_get_genders( $select_text = null ) {
623
624 if ( $select_text ) {
625 $genders = array(
626 '-1' => $select_text,
627 'male' => __( 'Male', 'erp' ),
628 'female' => __( 'Female', 'erp' ),
629 'other' => __( 'Other', 'erp' )
630 );
631 } else {
632 $genders = array(
633 'male' => __( 'Male', 'erp' ),
634 'female' => __( 'Female', 'erp' ),
635 'other' => __( 'Other', 'erp' )
636 );
637 }
638
639 return apply_filters( 'erp_hr_genders', $genders );
640 }
641
642 /**
643 * Get marital statuses
644 *
645 * @return array all the statuses
646 */
647 function erp_hr_get_pay_type() {
648 $types = array(
649 'hourly' => __( 'Hourly', 'erp' ),
650 'daily' => __( 'Daily', 'erp' ),
651 'weekly' => __( 'Weekly', 'erp' ),
652 'biweekly' => __( 'Biweekly', 'erp' ),
653 'monthly' => __( 'Monthly', 'erp' ),
654 'contract' => __( 'Contract', 'erp' ),
655 );
656
657 return apply_filters( 'erp_hr_pay_type', $types );
658 }
659
660 /**
661 * Get marital statuses
662 *
663 * @return array all the statuses
664 */
665 function erp_hr_get_pay_change_reasons() {
666 $reasons = array(
667 'promotion' => __( 'Promotion', 'erp' ),
668 'performance' => __( 'Performance', 'erp' ),
669 'increment' => __( 'Increment', 'erp' )
670 );
671
672 return apply_filters( 'erp_hr_pay_change_reasons', $reasons );
673 }
674
675 /**
676 * Add a new item in employee history table
677 *
678 * @param array $args
679 *
680 * @return array|bool|string|\WP_Error
681 */
682 function erp_hr_employee_add_history( $args = array() ) {
683
684 if ( ! $args['user_id'] ) {
685 return new WP_Error( 'no-user-id', 'No User id found' );
686 }
687 $employee = new \WeDevs\ERP\HRM\Employee( intval( $args['user_id'] ) );
688 $result = $employee->create_or_update_history( $args );
689
690 if ( is_wp_error( $result ) ) {
691 return $result->get_error_message();
692 }
693
694 return $result;
695 }
696
697 /**
698 * Remove an item from the history
699 *
700 * @param int $history_id
701 *
702 * @return bool
703 */
704 function erp_hr_employee_remove_history( $history_id ) {
705 global $wpdb;
706
707 return $wpdb->delete( $wpdb->prefix . 'erp_hr_employee_history', array( 'id' => $history_id ) );
708 }
709
710 /**
711 * Individual employee url
712 *
713 * @param int employee id
714 *
715 * @return string url of the employee details page
716 */
717 function erp_hr_url_single_employee( $employee_id, $tab = null ) {
718 if ( $tab ) {
719 $tab = '&tab=' . $tab;
720 }
721
722 $user = wp_get_current_user();
723
724 if ( in_array( 'employee', (array) $user->roles ) ) {
725 $url = admin_url( 'admin.php?page=erp-hr-my-profile&action=view&id=' . $employee_id . $tab );
726 } else {
727 $url = admin_url( 'admin.php?page=erp-hr-employee&action=view&id=' . $employee_id . $tab );
728 }
729
730 return apply_filters( 'erp_hr_url_single_employee', $url, $employee_id );
731 }
732
733 /**
734 * Individual employee tab url
735 *
736 * @param string $tab
737 * @param int employee id
738 *
739 * @since 1.1.10
740 *
741 * @return string
742 */
743 function erp_hr_employee_tab_url( $tab, $employee_id ) {
744 $emp_url = erp_hr_url_single_employee( intval( $employee_id ) );
745 $tab_url = add_query_arg( array( 'tab' => $tab ), $emp_url );
746
747 return apply_filters( 'erp_hr_employee_tab_url', $tab_url, $tab, $employee_id );
748 }
749
750 /**
751 * Get Employee Announcement List
752 *
753 * @since 0.1
754 *
755 * @param integer $user_id
756 *
757 * @return array
758 */
759 function erp_hr_employee_dashboard_announcement( $user_id ) {
760 global $wpdb;
761
762 return erp_array_to_object( \WeDevs\ERP\HRM\Models\Announcement::join( $wpdb->posts, 'post_id', '=', $wpdb->posts . '.ID' )
763 ->where( 'user_id', '=', $user_id )
764 ->orderby( $wpdb->posts . '.post_date', 'desc' )
765 ->take( 8 )
766 ->get()
767 ->toArray() );
768 }
769
770 /**
771 * [erp_hr_employee_single_tab_general description]
772 *
773 * @return void
774 */
775 function erp_hr_employee_single_tab_general( $employee ) {
776 include WPERP_HRM_VIEWS . '/employee/tab-general.php';
777 }
778
779 /**
780 * [erp_hr_employee_single_tab_job description]
781 *
782 * @return void
783 */
784 function erp_hr_employee_single_tab_job( $employee ) {
785 include WPERP_HRM_VIEWS . '/employee/tab-job.php';
786 }
787
788 /**
789 * [erp_hr_employee_single_tab_leave description]
790 *
791 * @return void
792 */
793 function erp_hr_employee_single_tab_leave( $employee ) {
794 include WPERP_HRM_VIEWS . '/employee/tab-leave.php';
795 }
796
797 /**
798 * [erp_hr_employee_single_tab_notes description]
799 *
800 * @return void
801 */
802 function erp_hr_employee_single_tab_notes( $employee ) {
803 include WPERP_HRM_VIEWS . '/employee/tab-notes.php';
804 }
805
806 /**
807 * [erp_hr_employee_single_tab_performance description]
808 *
809 * @return void
810 */
811 function erp_hr_employee_single_tab_performance( $employee ) {
812 include WPERP_HRM_VIEWS . '/employee/tab-performance.php';
813 }
814
815 /**
816 * [erp_hr_employee_single_tab_permission description]
817 *
818 * @return void
819 */
820 function erp_hr_employee_single_tab_permission( $employee ) {
821 include WPERP_HRM_VIEWS . '/employee/tab-permission.php';
822 }
823
824 /**
825 * Get employee's available history module
826 *
827 * @since 1.3.0
828 *
829 * @return array
830 */
831 function erp_hr_employee_history_modules() {
832 $modules = array(
833 'employment',
834 'compensation',
835 'job',
836 );
837
838 return apply_filters( 'erp_hr_employee_history_modules', $modules );
839 }
840
841 /**
842 * Translate generic module data to readable format
843 *
844 * @param array $history
845 * @param bool $inserting if inserting data then true
846 *
847 * @return array|WP_Error
848 */
849 function erp_hr_translate_employee_history( array $history = array(), $inserting = false ) {
850 $available_modules = erp_hr_employee_history_modules();
851
852 if ( empty( $history['module'] ) || ! in_array( $history['module'], $available_modules ) ) {
853 return new \WP_Error( 'invalid-module-type', __( 'Unsupported module type', 'erp' ) );
854 }
855
856 $translators = array(
857 'employment' => array(
858 'date' => 'date',
859 'type' => 'type',
860 'comment' => 'comment',
861 ),
862 'compensation' => array(
863 'date' => 'date',
864 'comment' => 'comment',
865 'category' => 'pay_type',
866 'type' => 'pay_rate',
867 'data' => 'reason',
868 ),
869 'job' => array(
870 'date' => 'date',
871 'comment' => 'designation',
872 'category' => 'department',
873 'data' => 'reporting_to',
874 'type' => 'location',
875 )
876 );
877
878 $translators = apply_filters( 'erp_hr_translatable_employee_history_module_params', $translators );
879
880 $translator = $translators[ $history['module'] ];
881
882 if ( $inserting ) {
883 $translator = array_flip( $translator );
884 }
885
886 $formatted_history = array();
887 foreach ( $translator as $key => $val ) {
888 $formatted_history[ $val ] = '';
889
890 if ( ! empty( $history[ $key ] ) ) {
891 if ( ! $inserting ) {
892 $formatted_history['id'] = ! empty( $history['id'] ) ? intval( $history['id'] ) : null;
893 }
894 $formatted_history['module'] = $history['module'];
895 $formatted_history[ $val ] = $history[ $key ];
896 }
897 }
898
899 return $formatted_history;
900 }
901
902 /**
903 * control user data visibility
904 *
905 * @since 1.3.0
906 *
907 * @param $data
908 * @param $user_id (of browsing user)
909 *
910 * @return array;
911 */
912 function erp_hr_control_restricted_data( $data, $user_id ) {
913 global $current_user;
914 if ( ( ! current_user_can( erp_hr_get_manager_role() ) && ( $current_user->ID !== $user_id ) ) ) {
915 $restricted = [
916 'pay_rate',
917 'pay_type',
918 'hiring_source',
919 'hiring_date',
920 ];
921
922 return array_merge( $data, $restricted );
923 }
924
925 return array();
926 }
927
928 /**
929 * Get employee full name
930 *
931 * @since 1.3.2
932 *
933 * @param $user_id
934 *
935 * @return string
936 *
937 */
938 function erp_hr_get_employee_name( $user_id ) {
939 if ( ! $user_id instanceof WP_User ) {
940 $user = new WP_User( $user_id );
941 }
942
943 $name = array();
944 if ( $user->first_name ) {
945 $name[] = $user->first_name;
946 }
947
948 if ( $user->middle_name ) {
949 $name[] = $user->middle_name;
950 }
951
952 if ( $user->last_name ) {
953 $name[] = $user->last_name;
954 }
955
956 return implode( ' ', $name );
957
958 }
959
960 /**
961 * Get employee details admin url
962 *
963 * @since 1.3.11
964 *
965 * @param $user_id
966 *
967 * @return string
968 *
969 */
970 function erp_hr_get_details_url( $user_id ) {
971 return admin_url( 'admin.php?page=erp-hr-employee&action=view&id=' . $user_id );
972 }
973
974 /**
975 * Get employee single url
976 *
977 * @since 1.3.11
978 *
979 * @param $user_id
980 *
981 * @return string
982 *
983 */
984 function erp_hr_get_single_link( $user_id ) {
985 return sprintf( '<a href="%s">%s</a>', erp_hr_get_details_url( $user_id ), erp_hr_get_employee_name($user_id) );
986 }
987
988 /**
989 * check if employee exist by email
990 *
991 * @since 1.3.12
992 *
993 * @param $email
994 *
995 * @return array
996 *
997 */
998 function erp_is_employee_exist( $email, $user_id ) {
999 global $wpdb;
1000 $user_email = sanitize_email($email);
1001 $sql = "select user.ID from {$wpdb->prefix}erp_hr_employees as employee inner join {$wpdb->prefix}users as user on user.ID=employee.user_id where user.user_email='{$user_email}' AND user.ID !='{$user_id}'";
1002 return $wpdb->get_col( $sql );
1003 }
1004