| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get All Reporting Titles |
| 4 |
* |
| 5 |
* @return array |
| 6 |
*/ |
| 7 |
function erp_hr_get_reports() { |
| 8 |
|
| 9 |
$reports = [ |
| 10 |
'age-profile' => [ |
| 11 |
'title' => __( 'Age Profile', 'erp' ), |
| 12 |
'description' => __( 'Shows age breakdown data in your company in different departments.', 'erp' ) |
| 13 |
], |
| 14 |
'gender-profile' => [ |
| 15 |
'title' => __( 'Gender Profile', 'erp' ), |
| 16 |
'description' => __( 'Shows differentiation data by age in your company.', 'erp' ) |
| 17 |
], |
| 18 |
'headcount' => [ |
| 19 |
'title' => __( 'Head Count', 'erp' ), |
| 20 |
'description' => __( 'Displays actual number of individuals in your company in different departments.', 'erp' ) |
| 21 |
], |
| 22 |
'salary-history' => [ |
| 23 |
'title' => __( 'Salary History', 'erp' ), |
| 24 |
'description' => __( 'Shows Salary History of the employees of your company.', 'erp' ), |
| 25 |
], |
| 26 |
'years-of-service' => [ |
| 27 |
'title' => __( 'Years of Service', 'erp' ), |
| 28 |
'description' => __( 'Shows longevity and experience report of the employees of your company.', 'erp' ) |
| 29 |
] |
| 30 |
]; |
| 31 |
|
| 32 |
return apply_filters( 'erp_hr_reports', $reports ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get report breakdown by age |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
function get_employee_breakdown_by_age( $employees ) { |
| 41 |
|
| 42 |
$_under18 = 0; |
| 43 |
$_18_to_25 = 0; |
| 44 |
$_26_to_35 = 0; |
| 45 |
$_36_to_45 = 0; |
| 46 |
$_46_to_55 = 0; |
| 47 |
$_56_to_65 = 0; |
| 48 |
$_65plus = 0; |
| 49 |
|
| 50 |
foreach ( $employees as $employee ) { |
| 51 |
|
| 52 |
if ( !is_valid_date( $employee->date_of_birth ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
$dob = new DateTime( $employee->date_of_birth ); |
| 57 |
$now = new DateTime(); |
| 58 |
$interval = $now->diff( $dob ); |
| 59 |
$age = $interval->y; |
| 60 |
|
| 61 |
if ( $age > 0 && $age <18 ) { |
| 62 |
|
| 63 |
$_under18++; |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
if ( $age >= 18 && $age <= 25 ) { |
| 68 |
|
| 69 |
$_18_to_25++; |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if ( $age >= 26 && $age <= 35 ) { |
| 74 |
|
| 75 |
$_26_to_35++; |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
if ( $age >= 36 && $age <= 45 ) { |
| 80 |
|
| 81 |
$_36_to_45++; |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $age >= 46 && $age <= 55 ) { |
| 86 |
|
| 87 |
$_46_to_55++; |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $age >= 56 && $age <= 65 ) { |
| 92 |
|
| 93 |
$_56_to_65++; |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
if ( $age > 65 ) { |
| 98 |
|
| 99 |
$_65plus++; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$count = [ |
| 104 |
'_under_18' => $_under18, |
| 105 |
'_18_to_25' => $_18_to_25, |
| 106 |
'_26_to_35' => $_26_to_35, |
| 107 |
'_36_to_45' => $_36_to_45, |
| 108 |
'_46_to_55' => $_46_to_55, |
| 109 |
'_56_to_65' => $_56_to_65, |
| 110 |
'_65plus' => $_65plus, |
| 111 |
]; |
| 112 |
|
| 113 |
return $count; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Counts diffrenet genders in employees |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
function erp_hr_get_gender_count( $department = null ) { |
| 122 |
|
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
if ( null == $department ) { |
| 126 |
$all_user_id = $wpdb->get_col( "SELECT user_id FROM {$wpdb->prefix}erp_hr_employees" ); |
| 127 |
|
| 128 |
} else { |
| 129 |
$all_user_id = $wpdb->get_col( "SELECT user_id FROM {$wpdb->prefix}erp_hr_employees WHERE department = $department" ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( $all_user_id ) { |
| 133 |
|
| 134 |
foreach ( $all_user_id as $user_id ) { |
| 135 |
|
| 136 |
$gender_single = get_user_meta( $user_id, 'gender' ); |
| 137 |
$gender_all[] = $gender_single[0]; |
| 138 |
} |
| 139 |
|
| 140 |
$gender_counted = array_count_values( $gender_all ); |
| 141 |
} |
| 142 |
|
| 143 |
$gender['male'] = isset( $gender_counted['male'] ) ? $gender_counted['male'] : 0; |
| 144 |
$gender['female'] = isset( $gender_counted['female'] ) ? $gender_counted['female'] : 0; |
| 145 |
$gender['other'] = isset( $gender_counted['other'] ) ? $gender_counted['other'] : 0; |
| 146 |
|
| 147 |
return $gender; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Gets data for Employee Breakdown Table on ERP HR Reporting |
| 152 |
* |
| 153 |
* @return object |
| 154 |
*/ |
| 155 |
function erp_hr_get_age_breakdown_data() { |
| 156 |
|
| 157 |
$employees = new \WeDevs\ERP\HRM\Models\Employee(); |
| 158 |
$departments = erp_hr_get_departments(); |
| 159 |
$age_breakdown_data = []; |
| 160 |
$tot_under18 = 0; |
| 161 |
$tot_18_to_25 = 0; |
| 162 |
$tot_26_to_35 = 0; |
| 163 |
$tot_36_to_45 = 0; |
| 164 |
$tot_46_to_55 = 0; |
| 165 |
$tot_56_to_65 = 0; |
| 166 |
$tot_65plus = 0; |
| 167 |
|
| 168 |
foreach( $departments as $department ) { |
| 169 |
|
| 170 |
$emp_by_dept = $employees->where( 'department', $department->id )->get(); |
| 171 |
$emp_by_dept_data = get_employee_breakdown_by_age( $emp_by_dept ); |
| 172 |
|
| 173 |
$tot_under18 += $emp_by_dept_data['_under_18']; |
| 174 |
$tot_18_to_25 += $emp_by_dept_data['_18_to_25']; |
| 175 |
$tot_26_to_35 += $emp_by_dept_data['_26_to_35']; |
| 176 |
$tot_36_to_45 += $emp_by_dept_data['_36_to_45']; |
| 177 |
$tot_46_to_55 += $emp_by_dept_data['_46_to_55']; |
| 178 |
$tot_56_to_65 += $emp_by_dept_data['_56_to_65']; |
| 179 |
$tot_65plus += $emp_by_dept_data['_65plus']; |
| 180 |
|
| 181 |
$age_breakdown_data[] = [ |
| 182 |
'department' => $department->title, |
| 183 |
'_under18' => $emp_by_dept_data['_under_18'], |
| 184 |
'_18_to_25' => $emp_by_dept_data['_18_to_25'], |
| 185 |
'_26_to_35' => $emp_by_dept_data['_26_to_35'], |
| 186 |
'_36_to_45' => $emp_by_dept_data['_36_to_45'], |
| 187 |
'_46_to_55' => $emp_by_dept_data['_46_to_55'], |
| 188 |
'_56_to_65' => $emp_by_dept_data['_56_to_65'], |
| 189 |
'_65plus' => $emp_by_dept_data['_65plus'] |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
$age_breakdown_data[] = [ |
| 194 |
'department' => 'Total', |
| 195 |
'_under18' => $tot_under18, |
| 196 |
'_18_to_25' => $tot_18_to_25, |
| 197 |
'_26_to_35' => $tot_26_to_35, |
| 198 |
'_36_to_45' => $tot_36_to_45, |
| 199 |
'_46_to_55' => $tot_46_to_55, |
| 200 |
'_56_to_65' => $tot_56_to_65, |
| 201 |
'_65plus' => $tot_65plus |
| 202 |
]; |
| 203 |
|
| 204 |
$age_breakdown_data = erp_array_to_object( $age_breakdown_data ); |
| 205 |
|
| 206 |
return $age_breakdown_data; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get count Employee Breakdown Table rows on ERP HR Reporting |
| 211 |
* |
| 212 |
* @return int |
| 213 |
*/ |
| 214 |
function erp_hr_count_age_breakdown() { |
| 215 |
|
| 216 |
$count = count( erp_hr_get_departments() ); |
| 217 |
|
| 218 |
return ++$count; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get data for Gender Ratio List Table |
| 223 |
* |
| 224 |
*@return array |
| 225 |
*/ |
| 226 |
function erp_hr_get_gender_ratio_data() { |
| 227 |
|
| 228 |
$gender_count = erp_hr_get_gender_count(); |
| 229 |
$gender_total = $gender_count['male'] + $gender_count['female'] + $gender_count['other']; |
| 230 |
|
| 231 |
$gender_ratio_data = [ |
| 232 |
'male' => [ |
| 233 |
'gender' => 'Male', |
| 234 |
'count' => $gender_count['male'], |
| 235 |
'percentage' => number_format( ( $gender_count['male'] * 100 ) / $gender_total, 2 ) . '%' |
| 236 |
], |
| 237 |
'female' => [ |
| 238 |
'gender' => 'Female', |
| 239 |
'count' => $gender_count['female'], |
| 240 |
'percentage' => number_format( ( $gender_count['female'] * 100 ) / $gender_total, 2 ) . '%' |
| 241 |
], |
| 242 |
'other' => [ |
| 243 |
'gender' => 'Unspecified', |
| 244 |
'count' => $gender_count['other'], |
| 245 |
'percentage' => number_format( ( $gender_count['other'] * 100 ) / $gender_total, 2 ) . '%' |
| 246 |
], |
| 247 |
'total' => [ |
| 248 |
'gender' => 'Total', |
| 249 |
'count' => $gender_total, |
| 250 |
'percentage' => '100%' |
| 251 |
], |
| 252 |
]; |
| 253 |
|
| 254 |
$gender_ratio_data = erp_array_to_object( $gender_ratio_data ); |
| 255 |
|
| 256 |
return $gender_ratio_data; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Returns Employee headcount by date/month |
| 261 |
* |
| 262 |
* @return number |
| 263 |
*/ |
| 264 |
function erp_hr_get_headcount( $date = '', $dept = '', $query_type = '' ) { |
| 265 |
|
| 266 |
global $wpdb; |
| 267 |
|
| 268 |
$count = 0; |
| 269 |
$all_user_data = $wpdb->get_results( "SELECT user_id, department, hiring_date, termination_date FROM {$wpdb->prefix}erp_hr_employees ", ARRAY_A ); |
| 270 |
|
| 271 |
if ( 'date' == $query_type ) { |
| 272 |
|
| 273 |
$date = strtotime( $date ); |
| 274 |
|
| 275 |
foreach ( $all_user_data as $user_data ) { |
| 276 |
|
| 277 |
$date_start = strtotime( $user_data['hiring_date'] ); |
| 278 |
$date_last = '0000-00-00' == $user_data['termination_date'] ? strtotime( 'now' ) : strtotime( $user_data['termination_date'] ); |
| 279 |
|
| 280 |
if( $date >= $date_start && $date <= $date_last ) { |
| 281 |
$count++; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if ( 'month' == $query_type ) { |
| 287 |
|
| 288 |
foreach ($all_user_data as $user_data ) { |
| 289 |
|
| 290 |
if ( '0000-00-00' == $user_data['hiring_date'] ) { |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
if ( $dept && $dept != $user_data['department'] ) { |
| 295 |
continue; |
| 296 |
} |
| 297 |
|
| 298 |
$date_start = $user_data['hiring_date']; |
| 299 |
$date_last = '0000-00-00' == $user_data['termination_date'] ? current_time( 'Y-m-d' ) : $user_data['termination_date']; |
| 300 |
|
| 301 |
$start = ( new DateTime( $date_start ) )->modify( 'first day of this month' ); |
| 302 |
$end = ( new DateTime( $date_last ) )->modify( 'last day of this month' ); |
| 303 |
$interval = DateInterval::createFromDateString( '1 month' ); |
| 304 |
$period = new DatePeriod( $start, $interval, $end ); |
| 305 |
|
| 306 |
foreach ( $period as $months ) { |
| 307 |
|
| 308 |
if ( $date == $months->format('Y-m') ) { |
| 309 |
|
| 310 |
$count++; |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
return $count; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Check if a string is valid date |
| 322 |
* |
| 323 |
* @since 0.1 |
| 324 |
* |
| 325 |
* @return bool |
| 326 |
*/ |
| 327 |
function is_valid_date( $str ) { |
| 328 |
|
| 329 |
try { |
| 330 |
|
| 331 |
$dt = new DateTime( trim( $str ) ); |
| 332 |
|
| 333 |
} catch( Exception $e ) { |
| 334 |
|
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
$month = $dt->format( 'm' ); |
| 339 |
$day = $dt->format( 'd' ); |
| 340 |
$year = $dt->format( 'Y' ); |
| 341 |
|
| 342 |
if( checkdate( $month, $day, $year) ) { |
| 343 |
return true; |
| 344 |
} else { |
| 345 |
return false; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|