(description) * @version GIT: $Id$ * @since Version 0.0.1 * @filesource * */ class Leave extends Admin_Controller { /** * [__construct description] * * @method __construct */ public function __construct() { // To inherit directly the attributes of the parent class. parent::__construct(); ### Syntax : $this->load->model('Model Path' , 'Alias name'); ### The second parameter (optional) is used to call the method. $this->load->model('MY_Model','MY_Model'); $this->load->model('Leave_model','Leave_model'); $this->load->model('Employee/Employee_model','Emp_model'); $this->load->model('Audit/Audit_model','audit_model'); $this->load->helper(array('form', 'url')); } public function index() { $this->layout->set('users', "gowtham"); $this->layout->buffer('main_content', 'Asset/index'); $this->layout->render('Employee_Layout'); } public function leaveList() { $business_id = user()->business_id; $leave_list = $this->Leave_model->get_leave_with_emp_name(user()->business_id); $leave_type = $this->Leave_model->get_leave_type($business_id); $decline_dropdown = $this->Leave_model->decline_dropdown($business_id); $this->layout->set('tableData', $leave_list); $this->layout->set('leave_type', $leave_type); $this->layout->set('dropdown', $decline_dropdown); $this->layout->buffer('main_content', 'Leave/leave_list'); $this->layout->render('Employee_Layout'); } public function applyLeave($update = null) { $business_id = user()->business_id; $user_id = user()->id; $emp = $this->Emp_model->get_employee(user()->business_id); $leave_master = $this->Leave_model->get_leave_master($business_id); $leavetype = $this->Leave_model->get_leave_master($business_id); $this->layout->set('emp', $emp); $this->layout->set('leave_master', $leave_master); $this->layout->set('leavetype', $leavetype); if($update == null) $this->session->set_flashdata('error', 0); $this->layout->buffer('main_content', 'Leave/apply_leave'); $this->layout->render('Employee_Layout'); } public function get_emp_leave_count() { $business_id = user()->business_id; $emp_id = $this->input->get('emp_id'); $type_id = $this->input->get('type_id'); $emp_leave_count = $this->Leave_model->get_count($emp_id, $business_id, $type_id); echo json_encode($emp_leave_count); } public function get_leave_master_count() { $business_id = user()->business_id; $type_id = $this->input->get('type_id'); // $year = $this->input->get('fromDate'); // $emp_id = $this->input->get('emp_id'); $emp_leave_count = $this->Leave_model->get_leave_master_count($type_id, $business_id); echo json_encode($emp_leave_count); } public function get_emp_name() { $emp = $this->Leave_model->get_employee(user()->business_id); $html = ''; foreach($emp as $val) { if($val->user_id != user()->id) { $html .=''; } } echo json_encode($html); } public function createLeave() { //permission type create part if(!empty($this->input->post('from_time'))) { $perm_data = $this->input->post(); if(isset($perm_data['emp_id'])){ $emp_id =$this->input->post('emp_id'); }else{ $emp_id = user()->id; } $perm_data['emp_id'] = $emp_id; if($perm_data['manager']==""){ if(employee()->manager == null) { $manager = 0; } else { $manager = employee()->manager; } }else{ $manager = $this->input->post('manager'); } $perm_data['manager'] = $manager; $perm_data['business_id'] = user()->business_id; $perm_data['status'] = 'pending'; $perm_data['created_date'] = date('d-m-Y'); $perm_data['created_time'] = date('H:i A'); unset($perm_data['from_date']); unset($perm_data['to_date']); $table="leave_list"; // print_r($perm_data);die; $create_permission = $this->MY_Model->insert($perm_data,$table); //for notification if($create_permission){ $code = 'LEAVE_PERMISSION'; $type = 'Email'; $this->notification->sendNotification($create_permission, $code, $type); } $this->session->set_flashdata('Success', '1'); redirect('Leave/LeaveHistory'); } //end of permission //Leave create part $data = $this->input->post(); unset($data['perm_date']); unset($data['from_time']); unset($data['to_time']); if(isset($data['emp_id'])){ $emp_id =$this->input->post('emp_id'); }else{ $emp_id = user()->id; } $data['emp_id'] = $emp_id; if($data['manager']==""){ if(employee()->manager == null) { $manager = 0; } else { $manager = employee()->manager; } }else{ $manager = $this->input->post('manager'); } $data['manager'] = $manager; $data['business_id'] = user()->business_id; $data['status'] = 'pending'; $data['created_date'] = date('d-m-Y'); $data['created_time'] = date('H:i A'); $leave_year = date('Y', strtotime($this->input->post('from_date'))); //get employee's leave count $emp_leave_count = $this->Leave_model->get_leave( $emp_id , user()->business_id , "emp_leave_days", $leave_year, $this->input->post('type')); $leave_master_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master"); // count number of days between two dates $days_between = ceil(abs(strtotime($this->input->post('to_date')) - strtotime($this->input->post('from_date'))) / 86400); $data['no_of_days'] = $days_between + 1; $noofdays = $days_between + 1; //this part is create and update leave data if (count($emp_leave_count) != 0) { if($noofdays > $emp_leave_count[0]->remaining_days) { $this->session->set_flashdata('error', '1'); redirect('Leave/applyLeave/'.'update'); } else { //this part is update if date inculeded in emp_leave_days table $data['leave_bal'] = $emp_leave_count[0]->remaining_days; $days['remaining_days'] = $emp_leave_count[0]->remaining_days; $this->Leave_model->update_leave($days, $emp_id , 'emp_leave_days', $leave_year, $this->input->post('type')); } } else { if($noofdays > $leave_master_count[0]->days) { $this->session->set_flashdata('error', '1'); redirect('Leave/applyLeave/'.'update'); } else { $all_leave_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master"); $data['leave_bal'] = $all_leave_count[0]->days; $days['remaining_days'] = $all_leave_count[0]->days; $days['business_id'] = user()->business_id; $days['emp_id'] = $emp_id; $days['total_days'] = $all_leave_count[0]->days; $days['type'] = $all_leave_count[0]->id; $days['year'] = $leave_year; $this->MY_Model->insert($days,'emp_leave_days'); } } $get_emp_data = $this->Leave_model->get_emp_data_by_emp_id($emp_id); // print_r($get_emp_data[0]->j_date); die; $from_date = new DateTime($this->input->post('from_date')); $j_date = new DateTime($get_emp_data[0]->j_date); $to_date = new DateTime($this->input->post('to_date')); if ($from_date >= $j_date && $from_date <= $to_date) { $table="leave_list"; $create_leave = $this->MY_Model->insert($data,$table); } else { $this->session->set_flashdata('error', '1'); redirect('Leave/applyLeave/'.'update'); } if($create_leave){ $code = 'APPLY_LEAVE'; $type = 'Email'; $this->notification->sendNotification($create_leave, $code, $type); } //audit if($create_leave) { //set id $ table name for audit history $this->audit->set('id',$create_leave); $this->audit->set('table',$table); //fetch new_data after post data insert $this->audit->fetch('new_data'); //audit history generation for insert $test = $this->audit->create_history('insert'); } $this->session->set_flashdata('Success', '1'); redirect('Leave/LeaveHistory'); } public function edit_leave_function() { $id = $this->input->get('id'); $emp_leave_count = $this->Leave_model->get("emp_leave_days"); $emp = $this->Emp_model->get_employee(user()->business_id); $business_id = user()->business_id; $leavetype = $this->Leave_model->getLeaveType($business_id); $editleave = $this->Leave_model->editleave($id); // print_r($leavetype); // print_r($id); // print_r($editleave); // // // print_r($emp); // die; $this->layout->set('emp', $emp); $this->layout->set('editleave', $editleave); $this->layout->set('count', $emp_leave_count); $this->layout->set('leavetype', $leavetype); $this->layout->buffer('main_content', 'Leave/edit_leave'); $this->layout->render('Employee_Layout'); } public function editleave() { if($this->input->post('type') === '1') { $action = $this->input->post(); unset($action['from_date']); unset($action['to_date']); $id = $this->input->post('id'); if(isset($action['emp_id'])){ $emp_id =$this->input->post('emp_id'); }else{ $emp_id = user()->id; } $action['emp_id'] = $emp_id; if($action['manager']){ $manager = $this->input->post('manager'); }else{ $manager = employee()->manager; } $action['manager'] = $manager; $action['business_id'] = user()->business_id; $action['status'] = 'pending'; $action['created_date'] = date('d-m-Y'); $action['created_time'] = date('H:i A'); $table="leave_list"; $create_permission = $this->MY_Model->edit_option($action, $id, $table); $this->session->set_flashdata('update', '0'); redirect('Leave/LeaveHistory'); } $action = $this->input->post(); $id = $this->input->post('id'); if(isset($action['emp_id'])){ $emp_id =$this->input->post('emp_id'); }else{ $emp_id = user()->id; } $action['emp_id'] = $emp_id; $action['business_id'] = user()->business_id; $action['status'] = 'pending'; $leave_year = date('Y', strtotime($this->input->post('from_date'))); $emp_leave_count = $this->Leave_model->get_leave( $emp_id , user()->business_id , "emp_leave_days", $leave_year, $this->input->post('type')); $leave_master_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master"); // count number of days between two dates $days_between = ceil(abs(strtotime($this->input->post('to_date')) - strtotime($this->input->post('from_date'))) / 86400); $action['no_of_days'] = $days_between + 1; $noofdays = $days_between + 1; $leave_id = $this->input->post('id'); if (count($emp_leave_count) != 0) { if($noofdays > $emp_leave_count[0]->remaining_days) { $this->session->set_flashdata('error', '1'); redirect('Leave/edit_leave_function?id='.md5($leave_id)); } else { //this part is update if date inculeded in emp_leave_days table $action['leave_bal'] = $emp_leave_count[0]->remaining_days; $days['remaining_days'] = $emp_leave_count[0]->remaining_days; $this->Leave_model->update_leave($days, $emp_id , 'emp_leave_days', $leave_year, $this->input->post('type')); } } else { if($noofdays > $leave_master_count[0]->days) { $this->session->set_flashdata('error', '1'); redirect('Leave/edit_leave_function?id='.md5($leave_id)); } else { $all_leave_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master"); $data['leave_bal'] = $all_leave_count[0]->days; $days['remaining_days'] = $all_leave_count[0]->days; $days['business_id'] = user()->business_id; $days['emp_id'] = $emp_id; $days['total_days'] = $all_leave_count[0]->days; $days['type'] = $all_leave_count[0]->id; $days['year'] = $leave_year; $this->MY_Model->insert($days,'emp_leave_days'); } } $get_from_date = $this->Leave_model->get_from_date($emp_id, $this->input->post('from_date'), $this->input->post('to_date')); // echo "
";
        // print_r($get_from_date); die();

        if($get_from_date == 0)
        {
            $table="leave_list";
            $update_leave = $this->MY_Model->edit_option($action,$id,$table);
            $this->session->set_flashdata('update', '0');
             redirect('Leave/LeaveHistory');
        }
        else
        {
             $this->session->set_flashdata('error', '1');
             redirect('Leave/applyLeave/'.'update');
        }


       }

    public function get_leave_count()
    {
        $id = $this->input->get('id');
        $type_id = $this->input->get('type_id');
        $year = date('Y');
        $emp_leave_count = $this->Leave_model->get_leave_based_on_tpye($id, $type_id, user()->business_id, "emp_leave_days", $year);
        echo json_encode($emp_leave_count);



    }

    public function get_all_leave_count()
    {
        $id = $this->input->get('type_id');
        $all_leave_count = $this->Leave_model->get_id( $id , user()->business_id , "leave_master");
        echo json_encode($all_leave_count);

    }

    public function getDeleteConfirmationPopup()
    {
        $data = array();
        $htmlPage = $this->load->view('confirmation_popup.php',$data,true);
        echo json_encode($htmlPage);
    }

    public function getDeclineConfirmationPopup()
    {
        $data = array();
        $htmlPage = $this->load->view('confirmation_popup_2.php',$data,true);
        echo json_encode($htmlPage);
    }

    public function ApproveLeave()
    {
       $id=$this->input->post('id');
       $business_id = user()->business_id;
       $emp_id = $this->Leave_model->get_emp_id($id);
       $no_of_days = $this->Leave_model->get_Leave_nodays($id,);
       $leave_year = date('Y', strtotime($no_of_days->from_date));
       $leave_type = $this->Leave_model->get_leave_data_using_leave_id($id);

        if($leave_type->leave_code !== 'permission')
        {
            $remaining_days = $this->Leave_model->remaining_days($emp_id, user()->business_id, $leave_year, $no_of_days->type);
            $balance_days = $remaining_days[0]->remaining_days - $no_of_days->no_of_days;

            $data['leave_bal'] = $balance_days;
            $days['remaining_days'] = $balance_days;
            $this->Leave_model->emp_leave_days_update($emp_id, $days, $leave_year, $business_id, $no_of_days->type);
        }

           $data['status'] = 'Approved';
           $data['approved_by']= user()->id;
           $data['approved_at'] = date('d-m-Y H:i:s');
           $leave_approve = $this->Leave_model->LeaveApprove($id, $data);
           if($leave_approve){

            if($leave_type->leave_code === 'permission'){
                $code = 'APPROVE_PERMISSION';
            }
            else{
                $code = 'APPROVE_LEAVE';
            }
                $type = 'Email';
                $this->notification->sendNotification($id, $code, $type);
            }
           redirect('Leave/leaveList');

    }

    public function getManagerConfirmationPopup()
    {
        $data = array();
        $htmlPage = $this->load->view('confirmation_popup.php',$data,true);
        echo json_encode($htmlPage);
    }
      public function ManagerLeaveApprove()
    {
       $id=$this->input->post('id');
       $business_id = user()->business_id;
       $emp_id = $this->Leave_model->get_emp_id($id);
       $no_of_days = $this->Leave_model->get_Leave_nodays($id,);
       $leave_year = date('Y', strtotime($no_of_days->from_date));
       $leave_type = $this->Leave_model->get_leave_data_using_leave_id($id);

            $remaining_days = $this->Leave_model->remaining_days($emp_id, user()->business_id, $leave_year, $no_of_days->type);
            $balance_days = $remaining_days[0]->remaining_days - $no_of_days->no_of_days;

            $data['leave_bal'] = $balance_days;
            $days['remaining_days'] = $balance_days;
            $this->Leave_model->emp_leave_days_update($emp_id, $days, $leave_year, $business_id, $no_of_days->type);


       $data['status'] = 'Approved';
       $data['approved_by']= user()->name;
       $data['approved_at'] = date('d-m-Y H:i:s');
       $leave_approve = $this->Leave_model->LeaveApprove($id, $data);
       if($leave_approve){

        if($leave_type->leave_code === 'permission'){
            $code = 'APPROVE_PERMISSION';
        }
        else{
            $code = 'APPROVE_LEAVE';
        }
            $type = 'Email';
            $this->notification->sendNotification($id, $code, $type);
        }
       redirect('Leave/manager_approvel');
    }

    public function Decline()
    {
      $id=$this->input->post('id');
      $data['status'] = 'Declined';
      $data['declined_by']= user()->name;
      $data['declined_at'] = date('d-m-Y H:i:s');
      $data['DReason'] = $this->input->post('declinedata');
      $leave_decline = $this->Leave_model->LeaveDecline($id, $data);
      $leave_type = $this->Leave_model->get_leave_data_using_leave_id($id);
      if($leave_decline){
      if($leave_type->leave_code === 'permission'){
          $code = 'DECLINE_PERMISSION';
      }
      else{
          $code = 'DECLINE_LEAVE';
      }
          $type = 'Email';
          $return = $this->notification->sendNotification($id, $code, $type);

        }
        redirect('Leave/leaveList');

    }

    public function managerDecline()
    {
      $id=$this->input->post('id');
      $data['status'] = 'Declined';
      $data['declined_by']= user()->name;
      $data['declined_at'] = date('d-m-Y H:i:s');
      $data['DReason'] = $this->input->post('declinedata');
      $leave_decline = $this->Leave_model->LeaveDecline($id, $data);
      $leave_type = $this->Leave_model->get_leave_data_using_leave_id($id);
      if($leave_decline){
      if($leave_type->leave_code === 'permission'){
          $code = 'DECLINE_PERMISSION';
      }
      else{
          $code = 'DECLINE_LEAVE';
      }
          $type = 'Email';
          $return = $this->notification->sendNotification($id, $code, $type);

        }
    redirect('Leave/manager_approvel');

    }


    public function cancled()
    {
      $id=$this->input->get('id');
      $data['status'] = 'canceled';
      $data['cancled_at'] = date('d-m-Y H:i:s');
      $this->Leave_model->Leavecancle($id, $data);
      $this->session->set_flashdata('Success', 'Leave canceled Successfully');
      redirect('Leave/LeaveHistory');
    }

    public function LeaveHistory()
    {
        $business_id = user()->business_id;
        $leave_list = $this->Leave_model->get_leave_with_emp_name(user()->business_id);
        $leave_type = $this->Leave_model->get_leave_type($business_id);
        $this->layout->set('tableData', $leave_list);
        $this->layout->set('leave_type', $leave_type);
        $this->layout->buffer('main_content', 'Leave/leavehistory');
        $this->layout->render('Employee_Layout');
    }


    public function manager_approvel()
    {

        $business_id = user()->business_id;
        $user_id = user()->id;
        $manager = $this->Leave_model->under_emp_manager($user_id, $business_id);
        $decline_dropdown = $this->Leave_model->decline_dropdown($business_id);
        $leave_list = $this->Leave_model->get_all_leave_request_by_manager_id(user()->business_id, user()->id);
        $leave_list_with_delegation = $this->Leave_model->get_all_leave_request_by_manager_id_with_delegation(user()->business_id,  user()->id);
        if($leave_list_with_delegation == NULL){
            $leave_list_with_delegation = [];
        }
        $mergedArray = array_merge($leave_list, $leave_list_with_delegation);
        $this->layout->set('tableData', $mergedArray);
        $this->layout->set('dropdown', $decline_dropdown);
        $this->layout->set('manager', $manager);
        $this->layout->buffer('main_content', 'Leave/manager_approvel');
        $this->layout->render('Employee_Layout');
    }



}