Пример #1
0
/**
 * Create a new employee
 *
 * @param  array  arguments
 *
 * @return int  employee id
 */
function erp_hr_employee_create($args = array())
{
    global $wpdb;
    $defaults = array('user_email' => '', 'company_id' => erp_get_current_company_id(), 'work' => array('designation' => 0, 'department' => 0, 'location' => '', 'hiring_source' => '', 'hiring_date' => '', 'date_of_birth' => '', 'reporting_to' => 0, 'pay_rate' => '', 'pay_type' => '', 'type' => '', 'status' => ''), 'personal' => array('photo_id' => 0, 'user_id' => 0, 'first_name' => '', 'middle_name' => '', 'last_name' => '', 'other_email' => '', 'phone' => '', 'work_phone' => '', 'mobile' => '', 'address' => '', 'gender' => '', 'marital_status' => '', 'nationality' => '', 'driving_license' => '', 'hobbies' => '', 'user_url' => '', 'description' => ''));
    $posted = array_map('strip_tags_deep', $args);
    $posted = array_map('trim_deep', $posted);
    $data = wp_parse_args($posted, $defaults);
    // some basic validation
    if (empty($data['personal']['first_name'])) {
        return new WP_Error('empty-first-name', __('Please provide the first name.', 'wp-erp'));
    }
    if (empty($data['personal']['last_name'])) {
        return new WP_Error('empty-last-name', __('Please provide the last name.', 'wp-erp'));
    }
    if (!is_email($data['user_email'])) {
        return new WP_Error('invalid-email', __('Please provide a valid email address.', 'wp-erp'));
    }
    // attempt to create the user
    $password = wp_generate_password(12);
    $userdata = array('user_login' => $data['user_email'], 'user_pass' => $password, 'user_email' => $data['user_email'], 'first_name' => $data['personal']['first_name'], 'last_name' => $data['personal']['last_name'], 'display_name' => $data['personal']['first_name'] . ' ' . $data['personal']['last_name'], 'role' => 'employee');
    // if user id exists, do an update
    $user_id = isset($posted['user_id']) ? intval($posted['user_id']) : 0;
    $update = false;
    if ($user_id) {
        $update = true;
        $userdata['ID'] = $user_id;
    }
    $userdata = apply_filters('erp_hr_employee_args', $userdata);
    $user_id = wp_insert_user($userdata);
    if (is_wp_error($user_id)) {
        return $user_id;
    }
    // if reached here, seems like we have success creating the user
    $employee = new \WeDevs\ERP\HRM\Employee($user_id);
    // inserting the user for the first time
    if (!$update) {
        $work = $data['work'];
        if (!empty($work['type'])) {
            $employee->update_employment_status($work['type']);
        }
        // update compensation
        if (!empty($work['pay_rate'])) {
            $pay_type = !empty($work['pay_type']) ? $work['pay_type'] : 'monthly';
            $employee->update_compensation($work['pay_rate'], $pay_type);
        }
        // update job info
        $employee->update_job_info($work['department'], $work['designation'], $work['reporting_to'], $work['location']);
    }
    // update the erp table
    $wpdb->update($wpdb->prefix . 'erp_hr_employees', array('company_id' => (int) $data['company_id'], 'hiring_source' => $data['work']['hiring_source'], 'hiring_date' => $data['work']['hiring_date'], 'date_of_birth' => $data['work']['date_of_birth']), array('user_id' => $user_id));
    foreach ($data['personal'] as $key => $value) {
        update_user_meta($user_id, $key, $value);
    }
    if ($update) {
        do_action('erp_hr_employee_update', $user_id, $data);
    } else {
        do_action('erp_hr_employee_new', $user_id, $data);
    }
    return $user_id;
}
Пример #2
0
 /**
  * Save work days from settings
  *
  * @return void
  */
 public function save_work_days()
 {
     if (!wp_verify_nonce($_POST['_wpnonce'], 'erp-settings')) {
         die;
     }
     $company_id = erp_get_current_company_id();
     $option_key = 'erp_hr_work_days_' . $company_id;
     $days = array_map('absint', $_POST['day']);
     if (count($days) == 7) {
         update_option($option_key, $days);
     }
 }
Пример #3
0
 /**
  * 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;
     }
 }