<?php
class UnpaidCommission extends OWN_Controller{
	function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('employeemodel/employeemodel','employeemodel');
        $this->load->model('unpaidcommissionmodel/unpaidcommissionmodel','unpaidcommissionmodel');
    }

    public function index(){
    	$admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
  	    	$data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
  	    	$this->load->view('common/header');
	        $this->load->view('common/menu');
	        $this->load->view('admin/unpaidcommission/Unpaid_Commission',$data);
	        $this->load->view('common/footer');
    	}
    	else{
            redirect(base_url('admin','refresh'));
        }
	}


	public function insert_commission()
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnSave']))
            {
                $this->load->helper(array('url','form'));
                $this->load->library('form_validation');
                $config = array(
                  array(
                      'field' => 'data[date]',
                      'label' => 'Date',
                      'rules' => 'required|xss_clean|trim'
                      ),
                  
                   array(
                      'field' => 'data[employee_id]',
                      'label' => 'Employee Id',
                      'rules' => 'required|xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[commission_type]',
                      'label' => 'Commission',
                      'rules' => 'required|xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[Amount]',
                      'label' => ' Amount ',
                      'rules' => 'required|xss_clean|trim|numeric'
                      )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == FALSE) {
                    $errormessage['errormessage'] = "Commissin Info Missing......!";
                    $this->session->set_userdata($errormessage);
                    $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/unpaidcommission/Unpaid_Commission',$data);
                    $this->load->view('common/footer');
                }
                else
                {
                    $data = $this->input->post('data', TRUE);
                    //print_r($data);die();
                    if(!empty($data)){
                    	$insert=$this->unpaidcommissionmodel->save_unPaidCommission($data);
                    	if(!empty($insert)){
                    		$message['message']="Unpaid Commissin saved Successfully saved......!";
		                    $this->session->set_userdata($message);
		                    redirect(admin_url().'/unpaidCommission/index','refresh');
                    	}else{
                    		$errormessage['errormessage']="Expense Not Saved......!";
		                    $this->session->set_userdata($errormessage);
		                    redirect(admin_url().'/expensesetup/index','refresh');
                    	}
                    }
                 } 
            }
          }else{
          	redirect(base_url('admin','refresh'));
          } 
      }

      public function view_unpaidCommission(){
      		$admin_id=$this->session->userdata('admin_id');
	        if ((!empty($admin_id)) || ($admin_id !="")) 
	        {
	        	$data['UnpaidCommissionInfo']=$this->unpaidcommissionmodel->getCommissionInfo();
	        	if(!empty($data['UnpaidCommissionInfo'])){
	        		$this->load->view('common/header');
    		        $this->load->view('common/menu');
    		        $this->load->view('admin/unpaidcommission/view_Unpaid_Commission',$data);
    		        $this->load->view('common/footer');
	        	}else{
	        		$errormessage['errormessage']="In This Time No unpaidcommission available......!";
                	$this->session->set_userdata($errormessage);
         			$this->load->view('common/header');
	                $this->load->view('common/menu');
	                $this->load->view('dashbord');
	                $this->load->view('common/footer');
	        	}
	        }else{
	        	redirect(base_url('admin','refresh'));
	        }
      }


      public function delete_unPaidCommission($id)
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $execution=$this->unpaidcommissionmodel->delete_unpaidCommission($id);
            if(!empty($execution))
            {
                $message['message']="Data Deleted.......!";
                $this->session->set_userdata($message);
                redirect('admin/unpaidCommission/view_unpaidCommission','refresh');
            }
            else
            {
                $errormessage['errormessage'] = "Not Deleted ......!";
                $this->session->set_userdata($errormessage);
                redirect('admin/unpaidCommission/view_unpaidCommission','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
   }

   public function edit_unPaidCommission($id)
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            $data['UnpaidCommissionInfo']=$this->unpaidcommissionmodel->getCommissionInfoByid($id);
            if(!empty($data['UnpaidCommissionInfo']))
            {
            	//print_r($data);die();
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/unpaidcommission/edit_unpaidcommission',$data);
                $this->load->view('common/footer');
            }
            else
            {
                 $errormessage['errormessage'] = "Invalid Data......!";
                 $this->session->set_userdata($errormessage);
               		$this->load->view('common/header');
	                $this->load->view('common/menu');
	                $this->load->view('dashbord');
	                $this->load->view('common/footer');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
   }


   public function update_unPaidCommission()
	   {
	      $admin_id=$this->session->userdata('admin_id');
	        if ((!empty($admin_id)) || ($admin_id !="")) 
	        {
	            if(isset($_POST['btnUpdate']))
	            {
	                $this->load->helper(array('url','form'));
	                $this->load->library('form_validation');
	                $config = array(
	                  array(
	                      'field' => 'data[date]',
	                      'label' => 'Date',
	                      'rules' => 'required|xss_clean|trim'
	                      ),
	                      array(
	                      'field' => 'data[commission_type]',
	                      'label' => 'Commission Type',
	                      'rules' => 'required|xss_clean|trim'
	                      ),
	                      array(
	                      'field' => 'data[Amount]',
	                      'label' => ' Salary Amount ',
	                      'rules' => 'required|xss_clean|trim|numeric'
	                      ),
	                      array(
	                      'field' => 'data[employee_id]',
	                      'label' => 'employee_id ',
	                      'rules' => 'required|xss_clean|trim'
	                      )
	                );
	                $this->form_validation->set_rules($config);
	                if ($this->form_validation->run() == FALSE) {
	                    $errormessage['errormessage'] = " Salary Info Missing......!";
	                    $this->session->set_userdata($errormessage);
	                    //$data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
	                    $this->load->view('common/header');
	                    $this->load->view('common/menu');
	                    $this->load->view('admin/expense/editSalaryExpense',$data);
	                    $this->load->view('common/footer');
	                }
	                else
	                {
	                    $data = $this->input->post('data', TRUE);
	                    $data['id']=$this->input->post('id', TRUE);
	                    //print_r($data);die();
	                    if(!empty($data))
	                    {
	                        $execution=$this->unpaidcommissionmodel->updateUnpaidCommission($data);
	                        if(!empty($execution))
	                        {
	                            $message['message']="Data Updated.......!";
	                             $this->session->set_userdata($message);
	                             redirect('admin/unpaidCommission/view_unpaidCommission','refresh');
	                        }
	                        else
	                        {
	                            $errormessage['errormessage'] = " Data Not Updated......!";
	                            $this->session->set_userdata($errormessage);
	                            redirect('admin/unpaidCommission/view_unpaidCommission','refresh');
	                        }
	                    }
	                    else
	                    {
	                      $errormessage['errormessage'] = " Salary Info Missing......!";
	                      $this->session->set_userdata($errormessage);
	                      redirect('admin/unpaidCommission/view_unpaidCommission','refresh');
	                    }
	                }
	            }
	            else
	            {
	                  $this->load->view('common/header');
	                  $this->load->view('common/menu');
	                  $this->load->view('dashbord');
	                  $this->load->view('common/footer');
	            }
	        }
	        else
	        {
	            redirect(base_url('admin','refresh'));
	        }
	   }
  }
?>