MMCT TEAM
Server IP : 103.191.208.50  /  Your IP : 216.73.216.53
Web Server : LiteSpeed
System : Linux orion.herosite.pro 4.18.0-553.53.1.lve.el8.x86_64 #1 SMP Wed May 28 17:01:02 UTC 2025 x86_64
User : celkcksm ( 1031)
PHP Version : 7.4.33
Disable Function : show_source, system, shell_exec, passthru, popen, exec
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/celkcksm/ecampus.ncriptech.com/application/controllers/admin/hrm/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/celkcksm/ecampus.ncriptech.com/application/controllers/admin/hrm/Employee.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * 
 */
class Employee  extends BaseAdminController
{
    protected $inst_id='';
    protected $inst_parent_id='';
    protected $inst_code='';
    protected $inst_type='';
    protected $hrms_access='';
    protected $hrms_add_access='';
    protected $hrms_edit_access='';
    protected $hrms_delete_access='';
    protected $hrms_view_access='';
    protected $user_id='';

    function __construct()
    {
        parent::__construct();

        $institute_data=check_institute($this->data['userdata']);

        $this->institute_data=$institute_data;

        //print_obj($institute_data);die;

        $this->inst_id=$institute_data['institute_id'];

        $this->inst_parent_id=$institute_data['inst_parent_id'];
        $this->inst_code=$institute_data['inst_code'];
        $this->inst_type=$institute_data['inst_type'];

        $this->hrms_access=$institute_data['hrms_access'];

        $this->hrms_view_access=$institute_data['user_can_view_emp'];
        $this->hrms_add_access=$institute_data['user_can_add_emp'];
        $this->hrms_edit_access=$institute_data['user_can_edit_emp'];
        $this->hrms_delete_access=$institute_data['user_can_delete_emp'];

        $this->user_id=$this->institute_data['user_id'];

        $this->load->model(array('hrm/employee_model'=>'em','hrm/departments_model'=>'dm','hrm/designation_model'=>'des','hrm/roles_model'=>'hrmrm'));
    }

    function index(){
        if($this->data['userdata']->user_type=='student' || $this->data['userdata']->user_type=='agent'){
            redirect($this->data['base_url']);
        }

        if(session_userdata('isAdminLoggedin')){
            $add_access= check_access_control($this->data['userdata'],'hrm_employee','add');

            $this->data['add_access']=$add_access;

            $this->data['page_title']='Employees';

            if($this->hrms_access=='yes'){

                $employee_page_access= check_access_control($this->data['userdata'],'hrm_employee','view');

                if($employee_page_access=='yes'){
                    //monthly_salary_cal_days_setting=>array('actual_days','organisation_days')
                    //monthly_pay_day=>array('last_working_day','selected_working_day')

                    //monthly_pay_day_fails_day=>array('+1 day','-1 day')

                    $payschedulr=array(
                        'workdays'=>array('1','2','3','4','5'),
                        'monthly_salary_cal_days_setting'=>'actual_days',
                        'monthly_salary_cal_days_setting'=>'26',
                        'monthly_pay_day'=>'selected_working_day',
                        'monthly_pay_day_fails_day'=>'-1 day'
                    );

                    $this->data['employee_can_be_uploaded']=$this->institute_data['employee_data_can_be_uploaded'];
                    $this->data['employee_upload_limit']=$this->institute_data['employee_upload_limit'];

                    $view='hrm/employees/vw_employees';
                }else{
                    $view='hrm/vw_permission_denied';
                }
                
            }else{
                $view='hrm/vw_permission_denied';
            }

            $this->theme->title($this->data['page_title'])->load($view, $this->data);
        }else{

            redirect($this->data['base_url']);
        }
    }


    function indexAddEditEmployee($emp_id=null){
       
        if(session_userdata('isAdminLoggedin')){

            $this->data['page_title']='Employees';

            if($this->hrms_access=='yes'){
                $employee_page_access= check_access_control($this->data['userdata'],'hrm_employee',['add','view']);

                if($employee_page_access=='yes'){
                    $user_id=$this->data['userdata']->user_id;
                    $inst_id=$this->inst_id;
                    $inst_type=$this->inst_type;

                    $_departments=array();
                    $_designations=array();
                    $employee_data=array();
                    $_roles=array();
                    $states=array();
                    $address_data=array();
                    $academic_details=array();
                    $professional_details=array();
                    $docs=array();

                    if(!empty($emp_id)){
                        $emp_id=decode_data($emp_id);
                        $employee_data=$this->em->_get_employee(array('emp_id'=>$emp_id));

                        //print_obj($employee_data);

                        $address_data=$this->em->get_employee_address(array('address_inst_id'=>$inst_id,'address_inst_type'=>$inst_type,'address_emp_id'=>$employee_data->emp_user_id));

                        $academic_details=$this->em->get_employee_academics_data(array('academic_inst_id'=>$inst_id,'academic_inst_type'=>$inst_type,'academic_emp_id'=>$employee_data->emp_user_id),FALSE);

                        $professional_details=$this->em->get_employee_professional_data(array('prof_inst_id'=>$inst_id,'prof_inst_type'=>$inst_type,'prof_emp_id'=>$employee_data->emp_user_id),FALSE);

                        $docs=$this->em->_get_employee_docs(array('doc_inst_id'=>$inst_id,'doc_inst_type'=>$inst_type,'doc_emp_id'=>$employee_data->emp_user_id),FALSE);
                    }

                    $departments=$this->dm->get_department(array('department_institute_id'=>$inst_id,'department_institute_type'=>$inst_type,'department_status'=>'active'),FALSE);

                    if(!empty($departments)){
                        foreach ($departments as $key => $value) {
                            $_departments[]=array(
                                'department_id'=>$value->department_id,
                                'department_name'=>$value->department_name,
                                'selected'=>(!empty($employee_data) && ($employee_data->emp_dept_id===$value->department_id))?'selected':''
                            );
                        }
                    }

                    $designations=$this->des->get_designation(array('designation_inst_id'=>$inst_id,'designation_inst_type'=>$inst_type,'designation_status'=>'active'),FALSE);

                    if(!empty($designations)){
                        foreach ($designations as $key => $value) {
                            $_designations[]=array(
                                'designation_id'=>$value->designation_id,
                                'designation_name'=>$value->designation_name,
                                'selected'=>(!empty($employee_data) && ($employee_data->emp_desig_id===$value->designation_id))?'selected':''
                            );
                        }
                    }


                    $gender_arr=array('male'=>'Male','female'=>'Female','other'=>'Other');

                    foreach ($gender_arr as $key => $value) {
                        $_genders[]=array(
                            'gender_value'=>$key,
                            'gender_name'=>$value,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_gender===$key))?'selected':''
                        );
                    }


                    $marital_status_arr=array('1'=>'Single','2'=>'Married','3'=>'Unmarried','4'=>'Widowed','5'=>'Divorced','6'=>'Other');

                    foreach ($marital_status_arr as $key => $value) {
                        $_marital_status[]=array(
                            'marital_status_value'=>$key,
                            'marital_status_name'=>$value,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_marital_status==$key))?'selected':''
                        );
                    }

                    $national_id_types_arr=array('1'=>'Aadhar Card','2'=>'Voter card','3'=>'Passport','4'=>'Driving License');

                    foreach($national_id_types_arr as $key => $value){
                        $_national_id_types[]=array(
                            'id_type_value'=>$key,
                            'id_type_name'=>$value,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_national_id_type==$key))?'selected':''
                        );
                    }

                    //print_obj($_national_id_types);die;


                    $blood_groups=$this->sm->_get_blood_groups(null,FALSE);

                    foreach ($blood_groups as $key => $value) {
                        $_blood_groups[]=array(
                            'bllod_group_id'=>$value->blood_group_id,
                            'blood_group_name'=>$value->blood_group_name,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_blood_group===$value->blood_group_id))?'selected':''
                        );
                    }


                    $proof_types=$this->em->get_employee_proof_type(array('type_inst_id'=>$inst_id,'type_inst_type'=>$inst_type),FALSE);

                    if(!empty($proof_types)){
                        foreach ($proof_types as $key => $value) {
                            $_proof_types[]=array(
                                'proof_id'=>$value->type_id,
                                'proof_name'=>$value->type_name,
                                'selected'=>(!empty($employee_data) && ($employee_data->emp_id_proof_type===$value->type_id))?'selected':''
                            );
                        }
                    }

                    $emp_type_arr=$this->em->get_employee_types();

                    foreach ($emp_type_arr as $key => $value) {
                        $_emp_type_arr[]=array(
                            'type_value'=>$value->type_id,
                            'type_name'=>$value->type_name,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_type===$value->type_id))?'selected':''
                        );
                    }


                    $emp_account_status_arr=array('active'=>'Active','inactive'=>'Inactive');

                    foreach ($emp_account_status_arr as $key => $value) {
                        $_emp_account_status_arr[]=array(
                            'status_value'=>$key,
                            'status_name'=>$value,
                            'selected'=>(!empty($employee_data) && ($employee_data->emp_status===$key))?'selected':''
                        );
                    }



                    $users_roles=$this->hrmrm->get_role(array('role_status'=>'active','role_name!='=>'Superadmin'),FALSE);

                    if(!empty($users_roles)){
                        foreach ($users_roles as $key => $value) {
                            $_roles[]=array(
                                'role_id'=>$value->role_id,
                                'role_name'=>$value->role_name,
                                'role_diaplay_name'=>$value->role_display_name,
                                'selected'=>(!empty($employee_data) && ($employee_data->emp_role===$value->role_id))?'selected':''
                            );
                        }
                    }

                    $_states=$this->sm->_get_states(array('state_country_id'=>'1'),FALSE);

                    if(!empty($_states)){
                        foreach ($_states as $key => $value) {
                            $states[]=array(
                                'state_id'=>$value->state_id,
                                'state_name'=>$value->state_name,
                                'current_selected'=>(!empty($address_data) && ($address_data->address_state===$value->state_id))?'selected':'',
                                'permanent_selected'=>(!empty($address_data) && ($address_data->address_perm_state===$value->state_id))?'selected':'',
                            );
                        }
                    }

                    //Academic details
                    $this->data['academic_details']=$academic_details;
                    $this->data['professional_details']=$professional_details;
                    $this->data['docs']=$docs;

                    $this->data['departments']=$_departments;
                    $this->data['designations']=$_designations;
                    $this->data['genders']=$_genders;
                    $this->data['marital_status']=$_marital_status;
                    $this->data['national_id_types']=$_national_id_types;
                    $this->data['proof_types']=$_proof_types;
                    $this->data['blood_groups']=$_blood_groups;
                    $this->data['emp_type']=$_emp_type_arr;
                    $this->data['emp_status']=$_emp_account_status_arr;
                    $this->data['employee_roles']=$_roles;
                    $this->data['employee_data']=$employee_data;
                    $this->data['states']=$states;
                    $this->data['address_data']=$address_data;

                    $this->data['hrms_add_access']=$this->hrms_add_access;
                    $this->data['hrms_edit_access']=$this->hrms_edit_access;
                    $this->data['hrms_delete_access']=$this->hrms_delete_access;
                    $this->data['hrms_view_access']=$this->hrms_view_access;

                    $this->data['employee_can_be_uploaded']=$this->institute_data['employee_data_can_be_uploaded'];
                    $this->data['employee_upload_limit']=$this->institute_data['employee_upload_limit'];


                    $view='hrm/employees/vw_employees_add_edit';
                }else{
                    $view='hrm/vw_permission_denied';
                }
                
            }else{
                $view='hrm/vw_permission_denied';
            }                

            $this->theme->title($this->data['page_title'])->load($view, $this->data);

        }else{
            redirect($this->data['base_url']);
        }
    }

    public function onSearchEmployeesList(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                if($this->hrms_access=='yes'){
                    
                    $inst_id=$this->inst_id;
                    $inst_type=$this->inst_type;

                    $param['column_order'] = array(
                        null,
                        'emp_first_name',
                        'emp_mid_name',
                        'emp_last_name',
                        'emp_code'
                    );


                    $param['emp_inst_id']=$inst_id;
                    $param['emp_inst_type']=$inst_type;

                    $param['column_search'] = array('emp_first_name','emp_mid_name','emp_last_name','emp_code');
                    $param['order'] = array('emp_id' => 'DESC');
                    $posts=$this->input->post();

                    $list = $this->em->_get_employee_list($posts,$param,FALSE,$this->inst_code,FALSE);
                    
                    $data = array();
                    $no = isset($posts['start'])?$posts['start']:0;

                    $action='';

                    foreach ($list as $emp){
                        $no++;

                        $row = array();

                        if(!empty($agent_image) && is_file(FCPATH.$agent_image->media_disk_path)){
                            $image=$this->data['base_url'].$agent_image->media_disk_path;
                        }else{
                            $image=$this->data['no_image'];
                        }

                         

                        if($emp->emp_mid_name!=''){
                            $emp_name=$emp->emp_first_name.' '.$emp->emp_mid_name.' '.$emp->emp_last_name;
                        }else{
                            $emp_name=$emp->emp_first_name.' '.$emp->emp_last_name;
                        }

                        $emp_name_with_code='Code:'.$emp->emp_code.'<br>Name:'.$emp_name.'<br>Email:'.$emp->emp_email.'<br>Phone No:'.$emp->emp_ph_no;

                        $row[]  =   $no;
                        $row[]  =   '<img src="'.$image.'" width="50px" height="50px">';
                        $row[]  =   '<strong>'.$emp_name_with_code.'</strong>';

                        $edit_access= check_access_control($this->data['userdata'],'hrm_employee','edit');
                        $delete_access= check_access_control($this->data['userdata'],'hrm_employee','delete');

                        if($emp->emp_status=='active'){
                            $status='<span class="badge badge-success">Active</span>';
                        }else if($emp->emp_status=='inactive'){
                            $status='<span class="badge badge-danger">Inactive</span>';
                        }

                        $action='<div class="btn-group" role="group" aria-label="Basic example">';


                        if($edit_access=='yes'){
                            $action.='<a href="'.$this->data['base_url'].'/hrm/employees/add/'.encode_data($emp->emp_id).'" class="btn btn-xs btn-dark btn_dep_edit" data-emp_id="'.encode_data($emp->emp_id).'"><i class="fa fa-edit"></i></a>';
                        }

                        if($delete_access=='yes'){
                            $action.=' <button type="button" class="btn btn-xs btn-danger btn_emp_del" data-emp_id="'.encode_data($emp->emp_id).'"><i class="fa fa-trash"></i></button>';
                        }

                        if($edit_access=='no' && $delete_access=='no'){
                             $action.='<span class="badge badge-danger">Not permitted</span>';
                        }

                        $action.='</div>';

                        $row[]  =   $emp->role_name;

                        $row[]  =   $status;
                        $row[]  =   $action;

                        $data[] = $row; 
                    }

                    $output = array(
                        "draw" => isset($posts['draw'])?$posts['draw']:'',
                        "recordsTotal" => $this->em->_get_employee_list($posts,$param,TRUE,$this->inst_code),
                        "recordsFiltered" => $this->em->_get_employee_list($posts,$param,TRUE,$this->inst_code),
                        "data" => $data,
                    );
                }else{
                    $output['error']='Permission denied';
                }

                    
                
                echo json_encode($output);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    public function onAddEditEmployees_v1(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){
                if($this->hrms_access=='yes'){

                    $emp_id=post_data('emp_id');

                    $emp_data_type=post_data('emp_data_type');

                    $emp_first_name=post_data('emp_first_name');
                    $emp_middle_name=post_data('emp_middle_name');
                    $emp_last_name=post_data('emp_last_name');
                    $emp_dob=post_data('emp_dob');
                    $emp_doj=post_data('emp_doj');
                    $emp_dtoc=post_data('emp_dtoc');
                    $emp_email=post_data('emp_email');
                    $emp_ph_no=post_data('emp_ph_no');
                    $emp_ph_no_alter=post_data('emp_ph_no_alter');
                    $emp_whatsapp_no=post_data('emp_whatsapp_no');
                    $emp_gender=post_data('emp_gender');
                    $emp_department=post_data('emp_department');
                    $emp_designation=post_data('emp_designation');
                    $emp_office_shift=post_data('emp_office_shift');
                    $emp_attendance_type=post_data('emp_attendance_type');
                    $emp_username=post_data('emp_username');
                    $emp_role=post_data('emp_role');

                    $emp_aadhar_no=post_data('emp_aadhar_no');
                    $emp_uan_no=post_data('emp_uan_no');
                    $emp_pan_no=post_data('emp_pan_no');
                    $emp_spouse_name=post_data('emp_spouse_name');
                    $emp_mother_name=post_data('emp_mother_name');
                    $emp_father_name=post_data('emp_father_name');
                    $emp_marital_status=post_data('emp_marital_status');

                    $emp_blood_group=post_data('emp_blood_group');

                    $emp_c_o=post_data('emp_c_o');
                    $emp_city_village=post_data('emp_city_village');
                    $emp_district=post_data('emp_district');
                    $emp_p_o=post_data('emp_p_o');
                    $emp_p_s=post_data('emp_p_s');
                    $emp_state=post_data('emp_state');
                    $emp_pincode=post_data('emp_pincode');
                    $emp_address_landmark=post_data('emp_address_landmark');

                    $emp_perma_c_o=post_data('emp_perma_c_o');
                    $emp_perma_city_village=post_data('emp_perma_city_village');
                    $emp_perma_district=post_data('emp_perma_district');
                    $emp_perma_p_o=post_data('emp_perma_p_o');
                    $emp_perma_p_s=post_data('emp_perma_p_s');
                    $emp_perma_state=post_data('emp_perma_state');
                    $emp_perma_pincode=post_data('emp_perma_pincode');
                    $emp_perma_address_landmark=post_data('emp_perma_address_landmark');

                    $address_same_as=post_data('address_same_as');

                    $emp_type=post_data('emp_type');

                    $emp_status=post_data('emp_status');

                    $emp_expense_limit=post_data('emp_expense_limit');
                    $emp_do_expense=post_data('emp_do_expense');

                    $emp_expense_limit_point=post_data('emp_expense_limit_point');

                    //Basic Info
                    if($emp_data_type==='basic_info'){

                        if(empty($emp_id)){
                            $this->form_validation->set_rules('emp_password', 'Password', 'trim|min_length[5]|max_length[18]',
                                array(
                                    'min_length'    => 'Minimum 5 charachters needed for %s',
                                    'max_length'    => 'Maximum 18 charachters allowed for %s'
                                )
                            );
                        }

                        $this->form_validation->set_rules('emp_first_name', 'First Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_middle_name', 'Middle Name', 'trim|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_last_name', 'Last Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                      
                        $this->form_validation->set_rules('emp_dob', 'Date of Birth', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_doj', 'Date of joining', 'trim|required',array('required'=>'You have not provided %s'));

                        $this->form_validation->set_rules('emp_ph_no', 'Phone no', 'trim|required|numeric|exact_length[10]', array(
                            'required' => 'You have not provided %s',
                            'numeric' => 'The %s should contain only numeric characters.',
                            'exact_length' => 'The %s should be exactly 10 characters long.'
                        ));

                        $this->form_validation->set_rules('emp_email', 'Email', 'trim|required|valid_email',array('required'=>'you have not provided %s','valid_email'=>'%s is not valid.'));

                        $this->form_validation->set_rules('emp_father_name', 'Father Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_mother_name', 'Mother Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_aadhar_no', 'AADHAR No.', 'trim|required|numeric|exact_length[12]', array(
                            'required' => 'You have not provided %s',
                            'numeric' => 'The %s should contain only numeric characters.',
                            'exact_length' => 'The %s should be exactly 12 characters long.'
                        ));

                        $this->form_validation->set_rules('emp_pan_no', 'PAN No.', 'trim|required', array(
                            'required' => 'You have not provided %s',
                            'validate_pan' => 'Invalid %s. Please provide a valid PAN number (e.g., ABCDE1234F).'
                        ));

                        //print_obj($this->form_validation->run());die;

                        if ($this->form_validation->run() == FALSE){
                            $validation_error_data = strip_tags($this->form_validation->error_string());
                            $return['error']=$validation_error_data;
                        }else{
                            $total_emp=$this->em->get_total_employee(array('emp_inst_id'=>$this->inst_id));

                            $_emp_code=$this->inst_code.date('Y');

                            $emp_code=emp_code($_emp_code,($total_emp+1));

                            $perm=$this->input->post('user_permissions');

                            if(!empty($perm)){
                                foreach ($perm as $key => $value) {
                                    $d[$key]=$value;
                                }

                                $permissions=json_encode($d);
                            }else{
                                $permissions=null;
                            }

                            if(empty($emp_id)){
                                $emp_found=$this->em->get_employee(array('emp_ph_no'=>$emp_ph_no));
                                $user_found=$this->um->_get_user(array('user_name'=>$emp_ph_no));

                                if(empty($emp_found)){

                                    if(empty($user_found)){
                                        $data_to_store=compact('emp_role','emp_department','emp_code','emp_designation','emp_first_name','emp_middle_name','emp_last_name','emp_gender','emp_email','emp_ph_no','emp_ph_no_alter','emp_whatsapp_no','emp_blood_group','emp_father_name','emp_mother_name','emp_spouse_name','emp_aadhar_no','emp_pan_no','emp_uan_no','emp_marital_status','emp_type','emp_status','emp_dob','emp_doj','emp_dtoc','emp_expense_limit','emp_do_expense','emp_expense_limit_point');

                                        $emp_profile_id=$this->store_emp_data($data_to_store);

                                        if($emp_profile_id){

                                            if(!empty($data['emp_password'])){
                                                $emp_password=$data['emp_password'];
                                            }else{
                                                $emp_password='Password@123';
                                            }

                                            $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                            $emp_user_data=array(
                                                'user_name'=>$emp_username,
                                                'user_password'=>$password,
                                                'user_password_visible'=>encode_data($emp_password),
                                                'user_type'=>'employee',
                                                'user_permissions'=>$permissions,
                                                'user_status'=>$emp_status,
                                                'created_by_type'=>$this->inst_type,
                                                'created_by'=>$this->inst_id
                                            );

                                            $emp_user_id=$this->um->store_users($emp_user_data);

                                            if(!empty($emp_user_id)){
                                                $address_data=compact('emp_user_id','emp_c_o','emp_city_village','emp_perma_district','emp_perma_p_o','emp_p_o','emp_state','emp_pincode','emp_address_landmark','emp_perma_c_o','emp_perma_city_village','emp_perma_district','emp_perma_p_o','emp_perma_p_s','emp_state','emp_perma_pincode','emp_perma_address_landmark','address_same_as');

                                                $this->store_employee_address($address_data);
                                                $this->em->update_employee(array('emp_user_id'=>$emp_user_id),array('emp_id'=>$emp_profile_id));
                                                $return['success']='Employee has been registered successfully.';
                                            }else{
                                                $return['success']='Employee not registered';
                                            }

                                        }else{
                                            $return['error']='Employee data has not been saved.';
                                        }
                                    }else{
                                        $return['error']='User already found in the system with same phone no';
                                    }   

                                }else{
                                    $return['error']='Employee already found in the system with same phone no';
                                }

                            }else{
                                $emp_user_id=decode_data($emp_id);

                                if(is_numeric($emp_user_id)){

                                    $emp_found=$this->em->get_employee(array('emp_user_id'=>$emp_user_id,'emp_inst_id'=>$this->inst_id));

                                    if(!empty($emp_found)){

                                        $emp_found_with_mail=$this->em->get_employee(array('emp_email'=>$emp_email,'emp_id!='=>$emp_found->emp_id,'emp_user_id'=>$emp_user_id,'emp_inst_id'=>$this->inst_id));

                                        if(empty($emp_found_with_mail)){
                                            
                                            $emp_id=$emp_found->emp_id;

                                            $data_to_store=compact('emp_id','emp_user_id','emp_department','emp_designation','emp_role','emp_first_name','emp_middle_name','emp_last_name','emp_gender','emp_email','emp_ph_no','emp_ph_no_alter','emp_whatsapp_no','emp_blood_group','emp_father_name','emp_mother_name','emp_spouse_name','emp_aadhar_no','emp_pan_no','emp_uan_no','emp_marital_status','emp_type','emp_status','emp_dob','emp_doj','emp_dtoc','emp_expense_limit','emp_do_expense','emp_expense_limit_point');

                                            //print_obj($data_to_store);die;

                                            $updated=$this->update_employee($data_to_store);

                                            print_obj($updated);die;

                                            if($updated){
                                                if(!empty($emp_username) && !empty($emp_password)){
                                                    $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                                    $emp_user_data=array(
                                                        'user_name'=>$emp_username,
                                                        'user_password'=>$password,
                                                        'user_password_visible'=>encode_data($emp_password),
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>$permissions,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$this->user_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }else if(empty($emp_username) && !empty($emp_password)){
                                                    $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                                    $emp_user_data=array(
                                                        'user_password'=>$password,
                                                        'user_password_visible'=>encode_data($emp_password),
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>$permissions,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$this->user_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }else if(!empty($emp_username) && empty($emp_password)){

                                                    $emp_user_data=array(
                                                        'user_name'=>$emp_username,
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>$permissions,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$this->user_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }

                                                $this->em->delete_employee_address(array('address_inst_id'=>$this->inst_id,'address_inst_type'=>$this->inst_type,'address_emp_id'=>$emp_user_id));

                                                $address_data=array(
                                                    'address_inst_id'=>$this->inst_id,
                                                    'address_inst_type'=>$inst_type,
                                                    'address_emp_id'=>$emp_found->emp_user_id,
                                                    'address_co'=>$emp_c_o,
                                                    'address_city'=>$emp_city_village,
                                                    'address_district'=>$emp_perma_district,
                                                    'address_po'=>$emp_perma_p_o,
                                                    'address_ps'=>$emp_p_o,
                                                    'address_state'=>$emp_state,
                                                    'address_pincode'=>$emp_pincode,
                                                    'address_landmark'=>$emp_address_landmark,
                                                    'address_perm_co'=>$emp_perma_c_o,
                                                    'address_perm_city'=>$emp_perma_city_village,
                                                    'address_perm_district'=>$emp_perma_district,
                                                    'address_perm_po'=>$emp_perma_p_o,
                                                    'address_perm_ps'=>$emp_perma_p_s,
                                                    'address_perm_state'=>$emp_state,
                                                    'address_perm_pincode'=>$emp_perma_pincode,
                                                    'address_perm_landmark'=>$emp_perma_address_landmark,
                                                    'address_same_current'=>(isset($address_same_as))?$address_same_as:'no'
                                                );

                                                $this->em->store_employee_address($address_data);

                                                $return['success']='Employee data has been updated.';
                                            }else{
                                                $return['error']='Employee data has not been updated.';
                                            }

                                        }else{
                                            $return['error']='Another employee with same email already exists.';
                                        }

                                    }else{
                                        $return['error']='Employee data not found to update.';
                                    }

                                }else{
                                    $return['error']='Data manipulation not allowed';
                                }
                            }
                        }
                    }

                    //Academic Info


                    //Document Info


                }else{
                    $return['error']='Permission denied';
                }

                json_headers($return);
            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    public function onAddEditEmployees(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){
                if($this->hrms_access=='yes'){

                    $inst_id=$this->inst_id;
                    $inst_type=$this->inst_type;
                    $inst_code=$this->inst_code;

                    $emp_id=post_data('emp_id');

                    $emp_data_type=post_data('emp_data_type');

                    $emp_first_name=post_data('emp_first_name');
                    $emp_mid_name=post_data('emp_middle_name');
                    $emp_last_name=post_data('emp_last_name');
                    $emp_father_name=post_data('emp_father_name');
                    $emp_mother_name=post_data('emp_mother_name');
                    $emp_dob=post_data('emp_dob');
                    $emp_doj=post_data('emp_doj');
                    $emp_doc=post_data('emp_dtoc');
                    $emp_email=post_data('emp_email');
                    $emp_ph_no=post_data('emp_ph_no');
                    $emp_ph_no_alter=post_data('emp_ph_no_alter');
                    $emp_whatsapp_no=post_data('emp_whatsapp_no');
                    $emp_gender=post_data('emp_gender');
                    $emp_department=post_data('emp_department');
                    $emp_designation=post_data('emp_designation');
                    $emp_username=post_data('emp_username');
                    $emp_role=post_data('emp_role');
                    $emp_password=post_data('emp_password');
                    $emp_national_id=post_data('emp_national_id');
                    $emp_national_id_type=post_data('emp_national_id_type');
                    $emp_contract_type=post_data('emp_contract_type');
                    $emp_marital_status=post_data('emp_marital_status');
                    $emp_spouse_name=post_data('emp_spouse_name');

                    $emp_dept_id=post_data('emp_department');
                    $emp_desig_id=post_data('emp_designation');

                    $emp_blood_group=post_data('emp_blood_group');

                    $emp_type=post_data('emp_type');

                    $emp_status=post_data('emp_status');

                    $emp_expense_limit=post_data('emp_expense_limit');
                    $emp_do_expense=post_data('emp_do_expense');

                    $emp_expense_limit_point=post_data('emp_expense_limit_point');

                    //Basic Info
                    if($emp_data_type==='basic_info'){

                        if(empty($emp_id)){
                            $this->form_validation->set_rules('emp_password', 'Password', 'trim|min_length[5]|max_length[18]',
                                array(
                                    'min_length'    => 'Minimum 5 charachters needed for %s',
                                    'max_length'    => 'Maximum 18 charachters allowed for %s'
                                )
                            );
                        }

                        $this->form_validation->set_rules('emp_first_name', 'First Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_middle_name', 'Middle Name', 'trim|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_last_name', 'Last Name', 'trim|required|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'required' => 'You have not provided %s',
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_father_name', 'Middle Name', 'trim|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                        $this->form_validation->set_rules('emp_mother_name', 'Middle Name', 'trim|regex_match[/^[A-Za-z\s]{1,255}$/]', array(
                            'regex_match' => 'Invalid %s. It should contain only alphabets and white spaces, and be between 1 and 255 characters.'
                        ));

                      
                        $this->form_validation->set_rules('emp_dob', 'Date of Birth', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_doj', 'Date of joining', 'trim|required',array('required'=>'You have not provided %s'));

                        $this->form_validation->set_rules('emp_ph_no', 'Phone no', 'trim|required|numeric|exact_length[10]', array(
                            'required' => 'You have not provided %s',
                            'numeric' => 'The %s should contain only numeric characters.',
                            'exact_length' => 'The %s should be exactly 10 characters long.'
                        ));

                        $this->form_validation->set_rules('emp_email', 'Email', 'trim|required|valid_email',array('required'=>'you have not provided %s','valid_email'=>'%s is not valid.'));

                        $this->form_validation->set_rules('emp_national_id_type', 'National ID Type', 'trim|required', array(
                            'required' => 'You have not provided %s',
                        ));

                        $this->form_validation->set_rules('emp_national_id', 'National ID', 'trim|required', array(
                            'required' => 'You have not provided %s',
                        ));

                        $this->form_validation->set_rules('emp_pan_no', 'PAN No.', 'trim', array(
                            'required' => 'You have not provided %s',
                            'validate_pan' => 'Invalid %s. Please provide a valid PAN number (e.g., ABCDE1234F).'
                        ));

                        //print_obj($this->form_validation->run());die;

                        if ($this->form_validation->run() == FALSE){
                            $validation_error_data = strip_tags($this->form_validation->error_string());
                            $return['error']=$validation_error_data;
                        }else{
                            $total_emp=$this->em->get_total_employee(array('emp_inst_id'=>$this->inst_id));

                            if($total_emp<$this->institute_data['employee_upload_limit']){
                                $_emp_code=$this->inst_code.date('Y');

                                $emp_code=emp_code($_emp_code,($total_emp+1));

                                $perm=$this->input->post('hrm_role_permissions');

                                if(!empty($perm)){
                                    foreach ($perm as $key => $value) {
                                        $d[$key]=$value;
                                    }

                                    $permissions=json_encode($d);
                                }else{
                                    $permissions=null;
                                }

                                if(empty($emp_id)){
                                    $emp_found=$this->em->get_employee(array('emp_ph_no'=>$emp_ph_no));
                                    $user_found=$this->um->_get_user(array('user_name'=>$emp_ph_no));

                                    if(empty($emp_found)){

                                        if(empty($user_found)){
                                            $data_to_store=compact('inst_id','inst_type','inst_code','emp_role','emp_dept_id','emp_code','emp_desig_id','emp_first_name','emp_mid_name','emp_last_name','emp_gender','emp_email','emp_ph_no','emp_ph_no_alter','emp_whatsapp_no','emp_blood_group','emp_father_name','emp_mother_name','emp_spouse_name','emp_national_id','emp_national_id_type','emp_pan_no','emp_uan_no','emp_marital_status','emp_type','emp_status','emp_dob','emp_doj','emp_doc','emp_expense_limit','emp_do_expense','emp_expense_limit_point','emp_user_id','emp_c_o','emp_city_village','emp_perma_district','emp_perma_p_o','emp_p_o','emp_state','emp_pincode','emp_address_landmark','emp_perma_c_o','emp_perma_city_village','emp_perma_district','emp_perma_p_o','emp_perma_p_s','emp_state','emp_perma_pincode','emp_perma_address_landmark','address_same_as','emp_username','permissions','emp_status','emp_contract_type');


                                            $emp_user_id=store_employee($data_to_store);

                                            if($emp_user_id){
                                                $return['success']='Employee registered';
                                            }else{
                                                $return['error']='Employee not registered';
                                            }
                                
                                        }else{
                                            $return['error']='User already found in the system with same phone no';
                                        }   

                                    }else{
                                        $return['error']='Employee already found in the system with same phone no';
                                    }

                                }else{//new
                                    $emp_user_id=decode_data($emp_id);

                                    if(is_numeric($emp_user_id)){

                                        $emp_found=$this->em->get_employee(array('emp_user_id'=>$emp_user_id,'emp_inst_id'=>$this->inst_id));

                                        if(!empty($emp_found)){
                                            $emp_found_with_mail=$this->em->get_employee(array('emp_email'=>$emp_email,'emp_id!='=>$emp_found->emp_id,'emp_user_id'=>$emp_found->emp_user_id,'emp_inst_id'=>$this->inst_id),TRUE,$inst_code);

                                            if(empty($emp_found_with_mail)){
                                                
                                                $emp_id=$emp_found->emp_id;

                                                $data_to_store=compact('emp_id','emp_user_id','emp_department','emp_designation','emp_role','emp_first_name','emp_middle_name','emp_last_name','emp_gender','emp_email','emp_ph_no','emp_ph_no_alter','emp_whatsapp_no','emp_blood_group','emp_father_name','emp_mother_name','emp_spouse_name','emp_national_id','emp_national_id_type','emp_pan_no','emp_uan_no','emp_marital_status','emp_type','emp_status','emp_dob','emp_doj','emp_dtoc','emp_expense_limit','emp_do_expense','emp_expense_limit_point','emp_contract_type');

                                                $updated=$this->update_employee($data_to_store);

                                                if($updated){
                                                    $check_user=$this->um->get_user(array('user_name'=>$emp_username,'user_type'=>'employee'),$this->inst_type,null,$inst_code);
                                                    $emp_user_data=array(
                                                        'user_role'=>$emp_role,
                                                        'user_code'=>$inst_code,
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>$permissions,
                                                        'user_status'=>$emp_status
                                                    );

                                                    //print_obj($emp_user_data);

                                                    if(!empty($emp_username)){
                                                        $emp_user_data['user_name']=$emp_username;   
                                                    }

                                                    if(!empty($emp_password)){
                                                        $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));
                                                        $emp_user_data['user_password']=$password;
                                                        $emp_user_data['user_password_visible']=encode_data($emp_password);    
                                                    }

                                                    if(!empty($check_user)){
                                                        $emp_user_data['updated_by']=$this->inst_id;
                                                        $emp_user_data['updated_by_type']=$this->inst_type;
                                                        $emp_user_data['updated_at']=date('Y-m-d H:i:s');
                                                        $this->um->update_users($emp_user_data,array('user_id'=>$check_user->user_id),FALSE,$inst_code);
                                                        $emp_user_id=$check_user->user_id;
                                                    }else{
                                                        $emp_user_data['created_by']=$this->inst_id;
                                                        $emp_user_data['created_by_type']=$this->inst_type;
                                                        $emp_user_data['created_at']=date('Y-m-d H:i:s');
                                                        $emp_user_id= $this->um->store_users($emp_user_data,$inst_code);
                                                    }


                                                    // $data_to_store=compact('emp_id','emp_user_id','emp_department','emp_designation','emp_role','emp_first_name','emp_middle_name','emp_last_name','emp_gender','emp_email','emp_ph_no','emp_ph_no_alter','emp_whatsapp_no','emp_blood_group','emp_father_name','emp_mother_name','emp_spouse_name','emp_national_id','emp_national_id_type','emp_pan_no','emp_uan_no','emp_marital_status','emp_type','emp_status','emp_dob','emp_doj','emp_dtoc','emp_expense_limit','emp_do_expense','emp_expense_limit_point');

                                                    // $updated=update_employee($data_to_store);

                                                    /*$this->em->delete_employee_address(array('address_inst_id'=>$this->inst_id,'address_inst_type'=>$this->inst_type,'address_emp_id'=>$emp_user_id));

                                                    $address_data=array(
                                                        'address_inst_id'=>$this->inst_id,
                                                        'address_inst_type'=>$inst_type,
                                                        'address_emp_id'=>$emp_found->emp_user_id,
                                                        'address_co'=>$emp_c_o,
                                                        'address_city'=>$emp_city_village,
                                                        'address_district'=>$emp_perma_district,
                                                        'address_po'=>$emp_perma_p_o,
                                                        'address_ps'=>$emp_p_o,
                                                        'address_state'=>$emp_state,
                                                        'address_pincode'=>$emp_pincode,
                                                        'address_landmark'=>$emp_address_landmark,
                                                        'address_perm_co'=>$emp_perma_c_o,
                                                        'address_perm_city'=>$emp_perma_city_village,
                                                        'address_perm_district'=>$emp_perma_district,
                                                        'address_perm_po'=>$emp_perma_p_o,
                                                        'address_perm_ps'=>$emp_perma_p_s,
                                                        'address_perm_state'=>$emp_state,
                                                        'address_perm_pincode'=>$emp_perma_pincode,
                                                        'address_perm_landmark'=>$emp_perma_address_landmark,
                                                        'address_same_current'=>(isset($address_same_as))?$address_same_as:'no'
                                                    );

                                                    $this->em->store_employee_address($address_data);*/

                                                    $return['success']='Employee data has been updated.';
                                                }else{
                                                    $return['error']='Employee data has not been updated.';
                                                }

                                            }else{
                                                $return['error']='Another employee with same email already exists.';
                                            }

                                        }else{
                                            $return['error']='Employee data not found to update.';
                                        }

                                    }else{
                                        $return['error']='Data manipulation not allowed';
                                    }
                                }
                            }else{
                                $return['error']='You have reached the limit of '.$this->institute_data['employee_upload_limit'].' employees you can upload.';
                            }  
                        }
                    }

                    //Academic Info


                    //Document Info


                }else{
                    $return['error']='Permission denied';
                }

                json_headers($return);
            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    protected function store_emp_data($data){

        if(!empty($data['emp_dob'])){
            $dob=date('Y-m-d',strtotime($data['emp_dob']));
        }else{
            $dob=null;
        }

        if(!empty($data['emp_doj'])){
            $doj=date('Y-m-d',strtotime($data['emp_doj']));
        }else{
            $doj=null;
        }

        if(!empty($data['emp_dtoc'])){
            $dtoc=date('Y-m-d',strtotime($data['emp_dtoc']));
        }else{
            $dtoc=null;
        }

        if($data['emp_do_expense']=='yes'){
            $expense_limit=$data['emp_expense_limit'];
        }else{
            $expense_limit='0';
        }

        $data_to_store=array(
            'emp_inst_id'=>$this->inst_id,
            'emp_inst_type'=>$this->inst_type,
            'emp_role'=>$data['emp_role'],
            'emp_dept_id'=>$data['emp_department'],
            'emp_code'=>$data['emp_code'],
            'emp_desig_id'=>$data['emp_designation'],
            'emp_dept_id'=>$data['emp_department'],
            'emp_first_name'=>$data['emp_first_name'],
            'emp_mid_name'=>$data['emp_middle_name'],
            'emp_last_name'=>$data['emp_last_name'],
            'emp_gender'=>$data['emp_gender'],
            'emp_email'=>$data['emp_email'],
            'emp_ph_no'=>$data['emp_ph_no'],
            'emp_ph_no_alter'=>$data['emp_ph_no_alter'],
            'emp_whatsapp_no'=>$data['emp_whatsapp_no'],
            'emp_blood_group'=>$data['emp_blood_group'],
            'emp_father_name'=>$data['emp_father_name'],
            'emp_mother_name'=>$data['emp_mother_name'],
            'emp_spouse_name'=>$data['emp_spouse_name'],
            'emp_aadhar_no'=>$data['emp_aadhar_no'],
            'emp_pan_no'=>$data['emp_pan_no'],
            'emp_uan_no'=>$data['emp_uan_no'],
            'emp_marital_status'=>$data['emp_marital_status'],
            'emp_type'=>$data['emp_type'],
            'emp_status'=>$data['emp_status'],
            'emp_dob'=>$dob,
            'emp_doj'=>$doj,
            'emp_doc'=>$dtoc,
            'emp_do_expense'=>$data['emp_do_expense'],
            'emp_expense_limit'=>$expense_limit,
            'emp_expense_limit_point'=>$data['emp_expense_limit_point']
        );

        $emp_profile_id=$this->em->store_employee($data_to_store);

        return $emp_profile_id;
    }


    protected function store_employee_address($data){
        $address_data=array(
            'address_inst_id'=>$this->inst_id,
            'address_inst_type'=>$this->inst_type,
            'address_emp_id'=>$data['emp_user_id'],
            'address_co'=>$data['emp_c_o'],
            'address_city'=>$data['emp_city_village'],
            'address_district'=>$data['emp_perma_district'],
            'address_po'=>$data['emp_perma_p_o'],
            'address_ps'=>$data['emp_p_o'],
            'address_state'=>$data['emp_state'],
            'address_pincode'=>$data['emp_pincode'],
            'address_landmark'=>$data['emp_address_landmark'],
            'address_perm_co'=>$data['emp_perma_c_o'],
            'address_perm_city'=>$data['emp_perma_city_village'],
            'address_perm_district'=>$data['emp_perma_district'],
            'address_perm_po'=>$data['emp_perma_p_o'],
            'address_perm_ps'=>$data['emp_perma_p_s'],
            'address_perm_state'=>$data['emp_state'],
            'address_perm_pincode'=>$data['emp_perma_pincode'],
            'address_perm_landmark'=>$data['emp_perma_address_landmark'],
            'address_same_current'=>(isset($data['address_same_as']))?$data['address_same_as']:'no'
        );

        $this->em->store_employee_address($address_data);
     }


    protected function update_employee($data){
        if(!empty($data['emp_dob'])){
            $dob=date('Y-m-d',strtotime($data['emp_dob']));
        }else{
            $dob=null;
        }

        if(!empty($data['emp_doj'])){
            $doj=date('Y-m-d',strtotime($data['emp_doj']));
        }else{
            $doj=null;
        }

        if(!empty($data['emp_dtoc'])){
            $dtoc=date('Y-m-d',strtotime($data['emp_dtoc']));
        }else{
            $dtoc=null;
        }

        if($data['emp_do_expense']=='yes'){
            $expense_limit=$data['emp_expense_limit'];
        }else{
            $expense_limit='0';
        }

        $data_to_store=array(
            'emp_inst_id'=>$this->inst_id,
            'emp_dept_id'=>$data['emp_department'],                                            
            'emp_desig_id'=>$data['emp_designation'],
            'emp_role'=>$data['emp_role'],
            'emp_first_name'=>$data['emp_first_name'],
            'emp_mid_name'=>$data['emp_middle_name'],
            'emp_last_name'=>$data['emp_last_name'],
            'emp_gender'=>$data['emp_gender'],
            'emp_email'=>$data['emp_email'],
            'emp_ph_no'=>$data['emp_ph_no'],
            'emp_ph_no_alter'=>$data['emp_ph_no_alter'],
            'emp_whatsapp_no'=>$data['emp_whatsapp_no'],
            'emp_blood_group'=>$data['emp_blood_group'],
            'emp_father_name'=>$data['emp_father_name'],
            'emp_mother_name'=>$data['emp_mother_name'],
            'emp_spouse_name'=>$data['emp_spouse_name'],
            'emp_national_id'=>$data['emp_national_id'],
            'emp_national_id_type'=>$data['emp_national_id_type'],
            'emp_contract_type'=>$data['emp_contract_type'],
            'emp_pan_no'=>$data['emp_pan_no'],
            'emp_uan_no'=>$data['emp_uan_no'],
            'emp_marital_status'=>$data['emp_marital_status'],
            'emp_type'=>$data['emp_type'],
            'emp_status'=>$data['emp_status'],
            'emp_dob'=>$dob,
            'emp_doj'=>$doj,
            'emp_doc'=>$dtoc,
            'emp_do_expense'=>$data['emp_do_expense'],
            'emp_expense_limit'=>$expense_limit,
            'emp_expense_limit_point'=>$data['emp_expense_limit_point']
        );

        $updated=$this->em->update_employee($data_to_store,array('emp_id'=>$data['emp_id'],'emp_user_id'=>$data['emp_user_id'],'emp_inst_id'=>$this->inst_id));

        return $updated;
    }




    public function onAddEditEmployees_old(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                if($this->hrms_access=='yes'){
                    $parent_inst_id=$this->inst_parent_id;

                    $user_id=$this->data['userdata']->user_id;

                    $institute_data=check_institute($this->data['userdata']);
                    $inst_id=$institute_data['institute_id'];
                    $inst_type=$institute_data['inst_type'];
                    $inst_code=$institute_data['inst_code'];

                    $emp_id=post_data('emp_id');

                    $emp_data_type=post_data('emp_data_type');

                    $emp_first_name=post_data('emp_first_name');
                    $emp_middle_name=post_data('emp_middle_name');
                    $emp_last_name=post_data('emp_last_name');
                    $emp_dob=post_data('emp_dob');
                    $emp_doj=post_data('emp_doj');
                    $emp_email=post_data('emp_email');
                    $emp_ph_no=post_data('emp_ph_no');
                    $emp_ph_no_alter=post_data('emp_ph_no_alter');
                    $emp_whatsapp_no=post_data('emp_whatsapp_no');
                    $emp_gender=post_data('emp_gender');
                    $emp_department=post_data('emp_department');
                    $emp_designation=post_data('emp_designation');
                    $emp_office_shift=post_data('emp_office_shift');
                    $emp_attendance_type=post_data('emp_attendance_type');
                    $emp_username=post_data('emp_username');
                    $emp_role=post_data('emp_role');

                    $emp_password=post_data('emp_password');


                    $emp_aadhar_no=post_data('emp_aadhar_no');
                    $emp_uan_no=post_data('emp_uan_no');
                    $emp_pan_no=post_data('emp_pan_no');
                    $emp_spouse_name=post_data('emp_spouse_name');
                    $emp_mother_name=post_data('emp_mother_name');
                    $emp_father_name=post_data('emp_father_name');
                    $emp_marital_status=post_data('emp_marital_status');

                    $emp_blood_group=post_data('emp_blood_group');

                    $emp_c_o=post_data('emp_c_o');
                    $emp_city_village=post_data('emp_city_village');
                    $emp_district=post_data('emp_district');
                    $emp_p_o=post_data('emp_p_o');
                    $emp_p_s=post_data('emp_p_s');
                    $emp_state=post_data('emp_state');
                    $emp_pincode=post_data('emp_pincode');
                    $emp_address_landmark=post_data('emp_address_landmark');


                    $emp_perma_c_o=post_data('emp_perma_c_o');
                    $emp_perma_city_village=post_data('emp_perma_city_village');
                    $emp_perma_district=post_data('emp_perma_district');
                    $emp_perma_p_o=post_data('emp_perma_p_o');
                    $emp_perma_p_s=post_data('emp_perma_p_s');
                    $emp_perma_state=post_data('emp_perma_state');
                    $emp_perma_pincode=post_data('emp_perma_pincode');
                    $emp_perma_address_landmark=post_data('emp_perma_address_landmark');

                    $address_same_as=post_data('address_same_as');

                    $emp_type=post_data('emp_type');

                    $emp_status=post_data('emp_status');

                    $emp_academic=$this->input->post('emp_academic');
                    $emp_professional=$this->input->post('emp_professional');

                    $emp_docs=$this->input->post('emp_docs');

                    if($emp_data_type==='basic_info'){

                        if(empty($emp_id)){
                            $this->form_validation->set_rules('emp_password', 'Password', 'trim|min_length[5]|max_length[18]',
                                array(
                                    'min_length'    => 'Minimum 5 charachters needed for %s',
                                    'max_length'    => 'Maximum 18 charachters allowed for %s'
                                )
                            );
                        }
                       
                        $this->form_validation->set_rules('emp_first_name', 'First Name', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_last_name', 'Last Name', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_dob', 'Date of Birth', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_doj', 'Date of joining', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_ph_no', 'Phone no', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_email', 'Email', 'trim|required|valid_email',array('required'=>'you have not provided %s','valid_email'=>'%s is not valid.'));

                        $this->form_validation->set_rules('emp_father_name', 'Father Name', 'trim|required',array('required'=>'You have not provided %s'));
                        $this->form_validation->set_rules('emp_mother_name', 'Mother Name', 'trim|required',array('required'=>'You have not provided %s'));

                        if ($this->form_validation->run() == FALSE){
                            $validation_error_data = strip_tags($this->form_validation->error_string());
                            $return['error']=$validation_error_data;
                        }else{

                            $total_emp=$this->em->get_total_employee(array('emp_inst_id'=>$inst_id));

                            $_emp_code=$this->data['user_code'].date('Y');

                            $emp_code=emp_code($_emp_code,($total_emp+1));

                            if(empty($emp_id)){
                                $emp_found=$this->em->get_employee(array('emp_email'=>$emp_email));

                                if(empty($emp_found)){

                                    if(!empty($emp_password)){
                                        $emp_password=$emp_password;
                                    }else{
                                        $emp_password='Password@123';
                                    }

                                    $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                    $data_to_store=array(
                                        'emp_inst_id'=>$inst_id,
                                        'emp_inst_type'=>$inst_type,
                                        'emp_role'=>$emp_role,
                                        'emp_dept_id'=>$emp_department,
                                        'emp_code'=>$emp_code,
                                        'emp_desig_id'=>$emp_designation,
                                        'emp_first_name'=>$emp_first_name,
                                        'emp_mid_name'=>$emp_middle_name,
                                        'emp_last_name'=>$emp_last_name,
                                        'emp_gender'=>$emp_gender,
                                        'emp_email'=>$emp_email,
                                        'emp_ph_no'=>$emp_ph_no,
                                        'emp_ph_no_alter'=>$emp_ph_no_alter,
                                        'emp_whatsapp_no'=>$emp_whatsapp_no,
                                        'emp_blood_group'=>$emp_blood_group,
                                        'emp_father_name'=>$emp_father_name,
                                        'emp_mother_name'=>$emp_mother_name,
                                        'emp_spouse_name'=>$emp_spouse_name,
                                        'emp_aadhar_no'=>$emp_aadhar_no,
                                        'emp_pan_no'=>$emp_pan_no,
                                        'emp_uan_no'=>$emp_uan_no,
                                        'emp_marital_status'=>$emp_marital_status,
                                        'emp_type'=>$emp_type,
                                        'emp_status'=>$emp_status,
                                        'emp_dob'=>date('Y-m-d',strtotime($emp_dob)),
                                        'emp_doj'=>date('Y-m-d',strtotime($emp_doj))
                                    );

                                    $emp_profile_id=$this->em->store_employee($data_to_store);

                                    if($emp_profile_id){

                                        $emp_user_data=array(
                                            'user_name'=>$emp_username,
                                            'user_password'=>$password,
                                            'user_password_visible'=>encode_data($emp_password),
                                            'user_type'=>'employee',
                                            'user_permissions'=>NULL,
                                            'user_status'=>$emp_status,
                                            'created_by_type'=>$inst_type,
                                            'created_by'=>$inst_id
                                        );

                                        $emp_user_id=$this->um->store_users($emp_user_data);

                                        if(!empty($emp_user_id)){
                                            $address_data=array(
                                                'address_inst_id'=>$inst_id,
                                                'address_inst_type'=>$inst_type,
                                                'address_emp_id'=>$emp_user_id,
                                                'address_co'=>$emp_c_o,
                                                'address_city'=>$emp_city_village,
                                                'address_district'=>$emp_perma_district,
                                                'address_po'=>$emp_perma_p_o,
                                                'address_ps'=>$emp_p_o,
                                                'address_state'=>$emp_state,
                                                'address_pincode'=>$emp_pincode,
                                                'address_landmark'=>$emp_address_landmark,
                                                'address_perm_co'=>$emp_perma_c_o,
                                                'address_perm_city'=>$emp_perma_city_village,
                                                'address_perm_district'=>$emp_perma_district,
                                                'address_perm_po'=>$emp_perma_p_o,
                                                'address_perm_ps'=>$emp_perma_p_s,
                                                'address_perm_state'=>$emp_state,
                                                'address_perm_pincode'=>$emp_perma_pincode,
                                                'address_perm_landmark'=>$emp_perma_address_landmark,
                                                'address_same_current'=>(isset($address_same_as))?$address_same_as:'no'
                                            );

                                            $this->em->store_employee_address($address_data);
                                            $this->em->update_employee(array('emp_user_id'=>$emp_user_id),array('emp_id'=>$emp_profile_id));


                                            //Academic Data
                                            if(!empty($emp_academic)){
                                                foreach ($emp_academic as $key => $value) {
                                                    if(!empty($value['board']) && !empty($value['qualification']) && !empty($value['yop'])){
                                                       $_academic_data[]=array(
                                                            'academic_inst_id'=>$inst_id,
                                                            'academic_inst_type'=>$inst_type,
                                                            'academic_emp_id'=>$emp_user_id,
                                                            'academic_board'=>$value['board'],
                                                            'academic_qualification'=>$value['qualification'],
                                                            'academic_year_of_passing'=>$value['yop']
                                                        ); 
                                                    }
                                                        
                                                }

                                                if(isset($_academic_data) && !empty($_academic_data)){
                                                    $this->em->delete_employee_academics_data(array('academic_inst_id'=>$inst_id,'academic_inst_type'=>$inst_type,'academic_emp_id'=>$emp_user_id));

                                                    $this->em->store_employee_academics_data($_academic_data,TRUE);
                                                }
                                            }

                                            //Professional Data
                                            if(!empty($emp_professional)){
                                                foreach ($emp_professional as $key => $value) {
                                                    if(!empty($value['company']) && !empty($value['designation']) && !empty($value['jobrole']) && !empty($value['yoj']) && !empty($value['yol'])){
                                                        $professional_data[]=array(
                                                            'prof_inst_id'=>$inst_id,
                                                            'prof_inst_type'=>$inst_type,
                                                            'prof_emp_id'=>$emp_user_id,
                                                            'prof_company'=>$value['company'],
                                                            'prof_designation'=>$value['designation'],
                                                            'prof_job_role'=>$value['jobrole'],
                                                            'prof_year_of_joining'=>$value['yoj'],
                                                            'prof_year_of_leaving'=>$value['yol'],
                                                            'prof_last_salary'=>$value['salary']
                                                        );
                                                    }                                                    
                                                }

                                                if(isset($professional_data) && !empty($professional_data)){
                                                    $this->em->delete_employee_professional_data(array('prof_inst_id'=>$inst_id,'prof_inst_type'=>$inst_type,'prof_emp_id'=>$emp_user_id));

                                                    $this->em->store_employee_professional_data($professional_data,TRUE);
                                                }
                                            }

                                            //Documents
                                                if(!empty($emp_docs)){
                                                    $storage_dir='institutes/'.$inst_code;
                                                    
                                                    foreach ($emp_docs as $key => $value) {
                                                        $files=$_FILES['emp_docs'.$key.'doc'];

                                                        if(isset($files) && $files['error']=='0'){

                                                            $emp_doc_data=array(
                                                                'file_size'=>'1',
                                                                'file_name'=>'emp_docs'.$key.'doc',
                                                                'file_types'=>'png,jpg,jpeg,pdf,doc,docx',
                                                                'file_storage_dir'=>$storage_dir,
                                                                'file_storage_type'=>'employee_docs',
                                                                'file_storage_type_id'=>$inst_id,
                                                                'file_parent_id'=>$parent_inst_id,
                                                                'file_resize'=>false,
                                                                'file_compress'=>false,
                                                                'file_operation_type'=>'new',
                                                                'file_uploaded_by'=>$user_id
                                                            );

                                                            $file_id=$this->onUploadFiles($emp_doc_data);

                                                            if(!empty($file_id) && is_numeric($file_id)){
                                                                 $docs_data=array(
                                                                    'doc_inst_id'=>$inst_id,
                                                                    'doc_inst_type'=>$inst_type,
                                                                    'doc_emp_id'=>$emp_user_id,
                                                                    'doc_doc_type_id'=>$value['doc_type'],
                                                                    'doc_storage_id'=>$file_id,
                                                                    'doc_description'=>$value['description']
                                                                );

                                                                $this->em->store_employee_docs($docs_data);
                                                            }
                                                        }
                                                    }
                                                }


                                            $return['success']='Employee has been registered successfully.';
                                        }else{
                                            $return['error']='Employee created but employee user has not been registered.';
                                        }

                                    }else{
                                        $return['error']='Employee data has not been saved.';
                                    }
                                }else{
                                    $return['error']='Employee already found in the system with same email id';
                                }
                            }else{

                                $emp_user_id=decode_data($emp_id);

                                if(is_numeric($emp_user_id)){

                                    $emp_found=$this->em->get_employee(array('emp_user_id'=>$emp_user_id,'emp_inst_id'=>$inst_id));

                                    if(!empty($emp_found)){

                                        $emp_found_with_mail=$this->em->get_employee(array('emp_email'=>$emp_email,'emp_id!='=>$emp_found->emp_id,'emp_user_id'=>$emp_user_id,'emp_inst_id'=>$inst_id));

                                        if(empty($emp_found_with_mail)){                                    

                                            $data_to_store=array(
                                                'emp_inst_id'=>$inst_id,
                                                'emp_dept_id'=>$emp_department,                                            
                                                'emp_desig_id'=>$emp_designation,
                                                'emp_role'=>$emp_role,
                                                'emp_first_name'=>$emp_first_name,
                                                'emp_mid_name'=>$emp_middle_name,
                                                'emp_last_name'=>$emp_last_name,
                                                'emp_gender'=>$emp_gender,
                                                'emp_email'=>$emp_email,
                                                'emp_ph_no'=>$emp_ph_no,
                                                'emp_ph_no_alter'=>$emp_ph_no_alter,
                                                'emp_whatsapp_no'=>$emp_whatsapp_no,
                                                'emp_blood_group'=>$emp_blood_group,
                                                'emp_father_name'=>$emp_father_name,
                                                'emp_mother_name'=>$emp_mother_name,
                                                'emp_spouse_name'=>$emp_spouse_name,
                                                'emp_aadhar_no'=>$emp_aadhar_no,
                                                'emp_pan_no'=>$emp_pan_no,
                                                'emp_uan_no'=>$emp_uan_no,
                                                'emp_marital_status'=>$emp_marital_status,
                                                'emp_type'=>$emp_type,
                                                'emp_status'=>$emp_status,
                                                'emp_dob'=>date('Y-m-d',strtotime($emp_dob)),
                                                'emp_doj'=>date('Y-m-d',strtotime($emp_doj))
                                            );

                                            $updated=$this->em->update_employee($data_to_store,array('emp_id'=>$emp_found->emp_id,'emp_user_id'=>$emp_user_id,'emp_inst_id'=>$inst_id));

                                            if($updated){
                                                if(!empty($emp_username) && !empty($emp_password)){
                                                    $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                                    $emp_user_data=array(
                                                        'user_name'=>$emp_username,
                                                        'user_password'=>$password,
                                                        'user_password_visible'=>encode_data($emp_password),
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>NULL,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$inst_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }else if(empty($emp_username) && !empty($emp_password)){
                                                    $password=password_hash($emp_password, PASSWORD_BCRYPT, array('cost'=>12));

                                                    $emp_user_data=array(
                                                        'user_password'=>$password,
                                                        'user_password_visible'=>encode_data($emp_password),
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>NULL,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$inst_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }else if(!empty($emp_username) && empty($emp_password)){

                                                    $emp_user_data=array(
                                                        'user_name'=>$emp_username,
                                                        'user_type'=>'employee',
                                                        'user_permissions'=>NULL,
                                                        'user_status'=>$emp_status,
                                                        'updated_by_type'=>'institute',
                                                        'updated_by'=>$inst_id
                                                    );

                                                    $this->um->update_users($emp_user_data,array('user_id'=>$emp_found->emp_user_id));
                                                }

                                                $this->em->delete_employee_address(array('address_inst_id'=>$inst_id,'address_inst_type'=>$inst_type,'address_emp_id'=>$emp_user_id));

                                                $address_data=array(
                                                    'address_inst_id'=>$inst_id,
                                                    'address_inst_type'=>$inst_type,
                                                    'address_emp_id'=>$emp_found->emp_user_id,
                                                    'address_co'=>$emp_c_o,
                                                    'address_city'=>$emp_city_village,
                                                    'address_district'=>$emp_perma_district,
                                                    'address_po'=>$emp_perma_p_o,
                                                    'address_ps'=>$emp_p_o,
                                                    'address_state'=>$emp_state,
                                                    'address_pincode'=>$emp_pincode,
                                                    'address_landmark'=>$emp_address_landmark,
                                                    'address_perm_co'=>$emp_perma_c_o,
                                                    'address_perm_city'=>$emp_perma_city_village,
                                                    'address_perm_district'=>$emp_perma_district,
                                                    'address_perm_po'=>$emp_perma_p_o,
                                                    'address_perm_ps'=>$emp_perma_p_s,
                                                    'address_perm_state'=>$emp_state,
                                                    'address_perm_pincode'=>$emp_perma_pincode,
                                                    'address_perm_landmark'=>$emp_perma_address_landmark,
                                                    'address_same_current'=>(isset($address_same_as))?$address_same_as:'no'
                                                );

                                                $this->em->store_employee_address($address_data);

                                                //Academic Data
                                                if(!empty($emp_academic)){
                                                    foreach ($emp_academic as $key => $value) {
                                                        if(!empty($value['board']) && !empty($value['qualification']) && !empty($value['yop'])){
                                                           $_academic_data[]=array(
                                                                'academic_inst_id'=>$inst_id,
                                                                'academic_inst_type'=>$inst_type,
                                                                'academic_emp_id'=>$emp_found->emp_user_id,
                                                                'academic_board'=>$value['board'],
                                                                'academic_qualification'=>$value['qualification'],
                                                                'academic_year_of_passing'=>$value['yop']
                                                            ); 
                                                        }
                                                            
                                                    }

                                                    if(isset($_academic_data) && !empty($_academic_data)){
                                                        $this->em->delete_employee_academics_data(array('academic_inst_id'=>$inst_id,'academic_inst_type'=>$inst_type,'academic_emp_id'=>$emp_found->emp_user_id));

                                                        $this->em->store_employee_academics_data($_academic_data,TRUE);
                                                    }
                                                }

                                                //Professional Data
                                                if(!empty($emp_professional)){
                                                    foreach ($emp_professional as $key => $value) {
                                                        if(!empty($value['company']) && !empty($value['designation']) && !empty($value['jobrole']) && !empty($value['yoj']) && !empty($value['yol'])){
                                                            $professional_data[]=array(
                                                                'prof_inst_id'=>$inst_id,
                                                                'prof_inst_type'=>$inst_type,
                                                                'prof_emp_id'=>$emp_found->emp_user_id,
                                                                'prof_company'=>$value['company'],
                                                                'prof_designation'=>$value['designation'],
                                                                'prof_job_role'=>$value['jobrole'],
                                                                'prof_year_of_joining'=>$value['yoj'],
                                                                'prof_year_of_leaving'=>$value['yol'],
                                                                'prof_last_salary'=>$value['salary']
                                                            );
                                                        }                                                    
                                                    }


                                                    if(isset($professional_data) && !empty($professional_data)){
                                                        $this->em->delete_employee_professional_data(array('prof_inst_id'=>$inst_id,'prof_inst_type'=>$inst_type,'prof_emp_id'=>$emp_found->emp_user_id));

                                                        $this->em->store_employee_professional_data($professional_data,TRUE);
                                                    }
                                                }

                                                //Documents
                                                if(!empty($emp_docs)){
                                                    $storage_dir='institutes/'.$inst_code;
                                                    
                                                    foreach ($emp_docs as $key => $value) {
                                                        $files=$_FILES['emp_docs'.$key.'doc'];

                                                        if(isset($files) && $files['error']=='0'){

                                                            $emp_doc_data=array(
                                                                'file_size'=>'1',
                                                                'file_name'=>'emp_docs'.$key.'doc',
                                                                'file_types'=>'png,jpg,jpeg,pdf,doc,docx',
                                                                'file_storage_dir'=>$storage_dir,
                                                                'file_storage_type'=>'employee_docs',
                                                                'file_storage_type_id'=>$inst_id,
                                                                'file_parent_id'=>$parent_inst_id,
                                                                'file_resize'=>false,
                                                                'file_compress'=>false,
                                                                'file_operation_type'=>'new',
                                                                'file_uploaded_by'=>$user_id
                                                            );

                                                            $file_id=$this->onUploadFiles($emp_doc_data);

                                                            if(!empty($file_id) && is_numeric($file_id)){
                                                                $this->em->delete_employee_docs(array('doc_inst_id'=>$inst_id,'doc_inst_type'=>$inst_type,'doc_emp_id'=>$emp_found->emp_user_id));
                                                                $docs_data=array(
                                                                    'doc_inst_id'=>$inst_id,
                                                                    'doc_inst_type'=>$inst_type,
                                                                    'doc_emp_id'=>$emp_found->emp_user_id,
                                                                    'doc_doc_type_id'=>$value['doc_type'],
                                                                    'doc_storage_id'=>$file_id,
                                                                    'doc_description'=>$value['description']
                                                                );

                                                                $this->em->store_employee_docs($docs_data);
                                                            }
                                                        }
                                                    }
                                                }                                     

                                                $return['success']='Employee data has been updated.';
                                            }else{
                                                $return['error']='Employee data has not been updated.';
                                            }

                                        }else{
                                            $return['error']='Another employee with same email already exists.';
                                        }

                                    }else{
                                        $return['error']='Employee data not found to update.';
                                    }

                                }else{
                                    $return['error']='Data manipulation not allowed';
                                }
                            }
                        }     
                    }
                }else{
                    $return['error']='Permission denied';
                }
                    
                json_headers($return);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }
}

MMCT - 2023