Example #1
0
/**
 * After create employee apply leave policy, you can also create leave policy by passing employee id.
 *
 * @since 0.1
 *
 * @param  int $employee
 * @param  array $policies
 *
 * @return void
 */
function erp_hr_apply_new_employee_policy($employee = false, $policies = false)
{
    if (is_int($employee)) {
        $user_id = intval($employee);
        $employee_obj = new \WeDevs\ERP\HRM\Employee($user_id);
        $employee = $employee_obj->to_array();
    } else {
        $user_id = intval($employee['id']);
    }
    $department = isset($employee['work']['department']) ? $employee['work']['department'] : '';
    $designation = isset($employee['work']['designation']) ? $employee['work']['designation'] : '';
    $gender = isset($employee['personal']['gender']) ? $employee['personal']['gender'] : '';
    $location = isset($employee['work']['location']) ? $employee['work']['location'] : '';
    $marital = isset($employee['personal']['marital_status']) ? $employee['personal']['marital_status'] : '';
    $today = date('Y-m-d', strtotime(current_time('mysql')));
    if (!$policies) {
        $policies = \WeDevs\ERP\HRM\Models\Leave_Policies::where('activate', '1')->get()->toArray();
    } else {
        $policies = array($policies);
    }
    $selected_policy = [];
    foreach ($policies as $key => $policy) {
        $effective_date = date('Y-m-d', strtotime($policy['effective_date']));
        if (strtotime($effective_date) < 0 || $today < $effective_date) {
            continue;
        }
        if ($policy['department'] != '-1' && $policy['department'] != $department) {
            continue;
        }
        if ($policy['designation'] != '-1' && $policy['designation'] != $designation) {
            continue;
        }
        if ($policy['gender'] != '-1' && $policy['gender'] != $gender) {
            continue;
        }
        if ($policy['location'] != '-1' && $policy['location'] != $location) {
            continue;
        }
        if ($policy['marital'] != '-1' && $policy['marital'] != $marital) {
            continue;
        }
        $selected_policy[] = $policy;
    }
    foreach ($selected_policy as $key => $leave_policy) {
        erp_hr_apply_leave_policy($user_id, $leave_policy);
    }
}