| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get company work days |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @since 1.1.14 Using settings saved in ERP Settings > HR > Workdays tab |
| 8 |
* |
| 9 |
* @return array |
| 10 |
*/ |
| 11 |
function erp_hr_get_work_days() { |
| 12 |
$default = [ |
| 13 |
'mon' => 8, |
| 14 |
'tue' => 8, |
| 15 |
'wed' => 8, |
| 16 |
'thu' => 8, |
| 17 |
'fri' => 8, |
| 18 |
'sat' => 0, |
| 19 |
'sun' => 0 |
| 20 |
]; |
| 21 |
|
| 22 |
$option_key = 'erp_settings_erp-hr_workdays'; |
| 23 |
|
| 24 |
$wizard_settings = get_option( $option_key, $default ); |
| 25 |
|
| 26 |
return [ |
| 27 |
'mon' => get_option( 'mon', $wizard_settings['mon'] ), |
| 28 |
'tue' => get_option( 'tue', $wizard_settings['tue'] ), |
| 29 |
'wed' => get_option( 'wed', $wizard_settings['wed'] ), |
| 30 |
'thu' => get_option( 'thu', $wizard_settings['thu'] ), |
| 31 |
'fri' => get_option( 'fri', $wizard_settings['fri'] ), |
| 32 |
'sat' => get_option( 'sat', $wizard_settings['sat'] ), |
| 33 |
'sun' => get_option( 'sun', $wizard_settings['sun'] ) |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get working day without off day |
| 39 |
* |
| 40 |
* @since 0.1 |
| 41 |
* |
| 42 |
* @param string $start_date |
| 43 |
* @param string $end_date |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
function erp_hr_get_work_days_without_off_day( $start_date, $end_date ) { |
| 48 |
|
| 49 |
$between_dates = erp_extract_dates( $start_date, $end_date ); |
| 50 |
|
| 51 |
if ( is_wp_error( $between_dates ) ) { |
| 52 |
return $between_dates; |
| 53 |
} |
| 54 |
|
| 55 |
$dates = array( 'days' => array(), 'total' => 0 ); |
| 56 |
$work_days = erp_hr_get_work_days(); |
| 57 |
$holiday_exist = erp_hr_leave_get_holiday_between_date_range( $start_date, $end_date ); |
| 58 |
|
| 59 |
foreach ( $between_dates as $date ) { |
| 60 |
|
| 61 |
$key = strtolower( date( 'D', strtotime( $date ) ) ); |
| 62 |
$is_holidy = ( $work_days[ $key ] == 0 ) ? true : false; |
| 63 |
|
| 64 |
if ( ! $is_holidy ) { |
| 65 |
$is_holidy = in_array( $date, $holiday_exist ) ? true : false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! $is_holidy ) { |
| 69 |
|
| 70 |
$dates['days'][] = array( |
| 71 |
'date' => $date, |
| 72 |
'count' => (int) ! $is_holidy |
| 73 |
); |
| 74 |
|
| 75 |
$dates['total'] += 1; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $dates; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get working day with off day |
| 84 |
* |
| 85 |
* @since 0.1 |
| 86 |
* |
| 87 |
* @param string $start_date |
| 88 |
* @param string $end_date |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
function erp_hr_get_work_days_between_dates( $start_date, $end_date ) { |
| 93 |
|
| 94 |
$between_dates = erp_extract_dates( $start_date, $end_date ); |
| 95 |
|
| 96 |
if ( is_wp_error( $between_dates ) ) { |
| 97 |
return $between_dates; |
| 98 |
} |
| 99 |
|
| 100 |
$dates = array( 'days' => array(), 'total' => 0 ); |
| 101 |
$work_days = erp_hr_get_work_days(); |
| 102 |
$holiday_exist = erp_hr_leave_get_holiday_between_date_range( $start_date, $end_date ); |
| 103 |
|
| 104 |
foreach ( $between_dates as $date ) { |
| 105 |
|
| 106 |
$key = strtolower( date( 'D', strtotime( $date ) ) ); |
| 107 |
$is_holidy = ( $work_days[ $key ] == '0' ) ? true : false; |
| 108 |
|
| 109 |
if ( ! $is_holidy ) { |
| 110 |
$is_holidy = in_array( $date, $holiday_exist ) ? true : false; |
| 111 |
} |
| 112 |
|
| 113 |
$dates['days'][] = array( |
| 114 |
'date' => $date, |
| 115 |
'count' => (int) ! $is_holidy |
| 116 |
); |
| 117 |
|
| 118 |
if ( ! $is_holidy ) { |
| 119 |
$dates['total'] += 1; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $dates; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Sort parents before children |
| 129 |
* |
| 130 |
* @since 1.0 |
| 131 |
* |
| 132 |
* @param array $objects input objects with attributes 'id' and 'parent' |
| 133 |
* @param array $result (optional, reference) internal |
| 134 |
* @param integer $parent (optional) internal |
| 135 |
* @param integer $depth (optional) internal |
| 136 |
* |
| 137 |
* @return array output |
| 138 |
*/ |
| 139 |
function erp_parent_sort( array $objects, array &$result = array(), $parent = 0, $depth = 0 ) { |
| 140 |
foreach ( $objects as $key => $object ) { |
| 141 |
if ( $object->parent == $parent ) { |
| 142 |
$object->depth = $depth; |
| 143 |
array_push( $result, $object ); |
| 144 |
unset( $objects[ $key ] ); |
| 145 |
erp_parent_sort( $objects, $result, $object->id, $depth + 1 ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $result; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Check today's birthday through schedule job. |
| 154 |
* |
| 155 |
* @since 1.1 |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
function erp_hr_schedule_check_todays_birthday() { |
| 160 |
$birthdays = erp_hr_get_todays_birthday(); |
| 161 |
|
| 162 |
// Do the action if someone's birthday today run only in cron |
| 163 |
if ( defined( 'DOING_CRON' ) && DOING_CRON && ! empty( $birthdays ) ) { |
| 164 |
do_action( 'erp_hr_happened_birthday_today_all', $birthdays ); |
| 165 |
|
| 166 |
foreach ( $birthdays as $birthday ) { |
| 167 |
do_action( 'erp_hr_happened_birthday_today', $birthday->user_id ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return $birthdays; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Prevent redirect to woocommerce my account page |
| 176 |
* |
| 177 |
* @param boolean $prevent_access |
| 178 |
* |
| 179 |
* @since 1.1.18 |
| 180 |
* |
| 181 |
* @return boolean |
| 182 |
*/ |
| 183 |
function erp_hr_wc_prevent_admin_access( $prevent_access ) { |
| 184 |
if ( current_user_can( erp_hr_get_manager_role() ) || current_user_can( erp_hr_get_employee_role() ) ) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
return $prevent_access; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Redirect hr role based user to their page |
| 193 |
* |
| 194 |
* @since 1.2.5 |
| 195 |
* |
| 196 |
* @param $redirect_to |
| 197 |
* @param $roles |
| 198 |
* |
| 199 |
* @return string |
| 200 |
*/ |
| 201 |
function erp_hr_login_redirect( $redirect_to, $roles ) { |
| 202 |
$hr_role = erp_hr_get_manager_role(); |
| 203 |
$employee_role = erp_hr_get_employee_role(); |
| 204 |
|
| 205 |
if ( in_array( $hr_role, $roles ) || in_array( $employee_role, $roles ) ) { |
| 206 |
$redirect_to = get_admin_url( null, 'admin.php?page=erp-hr' ); |
| 207 |
} |
| 208 |
|
| 209 |
return $redirect_to; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Filter collection by date |
| 214 |
* |
| 215 |
* @since 1.3.0 |
| 216 |
* |
| 217 |
* @param \Illuminate\Database\Eloquent\Builder $collection |
| 218 |
* @param $date |
| 219 |
* @param $field string |
| 220 |
* |
| 221 |
* @return \Illuminate\Database\Eloquent\Builder |
| 222 |
*/ |
| 223 |
function erp_hr_filter_collection_by_date( $collection, $date, $field = 'created_at' ) { |
| 224 |
if ( $collection && is_array( $date ) ) { |
| 225 |
$default = [ |
| 226 |
'Y' => null, |
| 227 |
'm' => null, |
| 228 |
'd' => null, |
| 229 |
]; |
| 230 |
$parsed_date = wp_parse_args( $date, $default ); |
| 231 |
|
| 232 |
if ( ! empty( $date['Y'] ) ) { |
| 233 |
$collection = $collection->whereYear( $field, '=', intval( $parsed_date['Y'] ) ); |
| 234 |
} |
| 235 |
if ( ! empty( $date['m'] ) ) { |
| 236 |
$collection = $collection->whereMonth( $field, '=', intval( $parsed_date['m'] ) ); |
| 237 |
} |
| 238 |
if ( ! empty( $date['d'] ) ) { |
| 239 |
$collection = $collection->whereDay( $field, '=', intval( $parsed_date['d'] ) ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return $collection; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* get the list of email who disabled notification from wperp |
| 248 |
* |
| 249 |
* @since 1.3.10 |
| 250 |
* |
| 251 |
* @return array |
| 252 |
* |
| 253 |
*/ |
| 254 |
function erp_hr_get_disabled_notification_users() { |
| 255 |
$user_emails = wp_cache_get( 'erp_hr_get_disabled_notification_users', 'erp' ); |
| 256 |
if ( false == $user_emails ) { |
| 257 |
$user_query = new WP_User_Query( [ |
| 258 |
'fields' => [ 'user_email' ], |
| 259 |
'meta_key' => 'erp_hr_disable_notification', |
| 260 |
'meta_value' => 'on' |
| 261 |
] ); |
| 262 |
$user_emails = []; |
| 263 |
if ( $user_query->get_total() ) { |
| 264 |
$user_emails = wp_list_pluck( $user_query->get_results(), 'user_email' ); |
| 265 |
} |
| 266 |
|
| 267 |
wp_cache_add( 'erp_hr_get_disabled_notification_users', $user_emails, 'erp' ); |
| 268 |
} |
| 269 |
|
| 270 |
return $user_emails; |
| 271 |
} |
| 272 |
/** |
| 273 |
* exclude recipients per profile settings |
| 274 |
* |
| 275 |
* @since 1.3.10 |
| 276 |
* |
| 277 |
* @param $recipients |
| 278 |
* |
| 279 |
* @return mixed |
| 280 |
* |
| 281 |
*/ |
| 282 |
function erp_hr_exclude_recipients( $recipients ) { |
| 283 |
$disabled_notification_user = erp_hr_get_disabled_notification_users(); |
| 284 |
if ( ! empty( $disabled_notification_user ) ) { |
| 285 |
if ( is_string( $recipients ) ) { |
| 286 |
$recipients = explode( ',', $recipients ); |
| 287 |
} |
| 288 |
$recipients = array_map( 'trim', $recipients ); |
| 289 |
$recipients = array_map( 'strtolower', $recipients ); |
| 290 |
$disabled_notification_user = array_map( 'strtolower', $disabled_notification_user ); |
| 291 |
$recipients = array_diff($recipients, $disabled_notification_user); |
| 292 |
} |
| 293 |
|
| 294 |
return $recipients; |
| 295 |
} |
| 296 |
|