/** * Apply entitlement yearly * * @since 0.1 * * @return void */ function erp_hr_apply_entitlement_yearly() { $financial_start_date = erp_financial_start_date(); $financial_end_date = erp_financial_end_date(); $before_financial_start_date = date('Y-m-01 H:i:s', strtotime('-1 year', strtotime($financial_start_date))); $before_financial_end_date = date('Y-m-t H:i:s', strtotime('+11 month', strtotime($before_financial_start_date))); $entitlement = new \WeDevs\ERP\HRM\Models\Leave_Entitlement(); $entitlement = $entitlement->where(function ($condition) use($before_financial_start_date, $before_financial_end_date) { $condition->where('from_date', '>=', $before_financial_start_date); $condition->where('to_date', '<=', $before_financial_end_date); }); $entitlements = $entitlement->get()->toArray(); foreach ($entitlements as $key => $entitlement) { $policy = array('user_id' => $entitlement['user_id'], 'policy_id' => $entitlement['policy_id'], 'days' => $entitlement['days'], 'from_date' => erp_financial_start_date(), 'to_date' => erp_financial_end_date(), 'comments' => $entitlement['comments']); erp_hr_leave_insert_entitlement($policy); } }
/** * Add entitlement with leave policies to employees * * @return void */ public function leave_entitlement() { if (!wp_verify_nonce($_POST['_wpnonce'], 'erp-hr-leave-assign')) { die(__('Something went wrong!', 'wp-erp')); } $affected = 0; $errors = array(); $employees = array(); $cur_year = (int) date('Y'); $page_url = admin_url('admin.php?page=erp-leave-assign&tab=assignment'); $is_single = !isset($_POST['assignment_to']); $leave_policy = isset($_POST['leave_policy']) ? intval($_POST['leave_policy']) : 0; $leave_period = isset($_POST['leave_period']) ? intval($_POST['leave_period']) : 0; $single_employee = isset($_POST['single_employee']) ? intval($_POST['single_employee']) : 0; $location = isset($_POST['location']) ? intval($_POST['location']) : 0; $department = isset($_POST['department']) ? intval($_POST['department']) : 0; $comment = isset($_POST['comment']) ? wp_kses_post($_POST['comment']) : 0; if (!$leave_policy) { $errors[] = 'invalid-policy'; } if (!in_array($leave_period, array($cur_year, $cur_year + 1))) { $errors[] = 'invalid-period'; } if ($is_single && !$single_employee) { $errors[] = 'invalid-employee'; } // bail out if error found if ($errors) { $first_error = reset($errors); $redirect_to = add_query_arg(array('error' => $first_error), $page_url); wp_safe_redirect($redirect_to); exit; } // fetch employees if not single if (!$is_single) { $company_id = erp_get_current_company_id(); $employees = erp_hr_get_employees(array('company_id' => $company_id, 'location' => $location, 'department' => $department)); } else { $user = get_user_by('id', $single_employee); $emp = new \stdClass(); $emp->user_id = $user->ID; $emp->display_name = $user->display_name; $employees[] = $emp; } if ($employees) { $from_date = $leave_period . '-01-01'; $to_date = $leave_period . '-12-31'; $policy = erp_hr_leave_get_policy($leave_policy); if (!$policy) { return; } foreach ($employees as $employee) { if (!erp_hr_leave_has_employee_entitlement($employee->user_id, $leave_policy, $leave_period)) { $data = array('user_id' => $employee->user_id, 'policy_id' => $leave_policy, 'days' => $policy->value, 'from_date' => $from_date, 'to_date' => $to_date, 'comments' => $comment, 'status' => 1); $inserted = erp_hr_leave_insert_entitlement($data); if (!is_wp_error($inserted)) { $affected += 1; } } } $redirect_to = add_query_arg(array('affected' => $affected), $page_url); wp_safe_redirect($redirect_to); exit; } }