<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class salary extends CI_Controller {

    private $userInfo = array();
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model("user_model");
        $this->load->model("Salary_model","sm");
         $this->load->model("teacher_model");
        $this->user_model->check_login("admin");

        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
    }
    
    public function adjust($succuss='')
      {
		if($succuss=='success') {
			$data['success']=true;
		}else {
			$data['success']=false;
		}
		if($this->user_model->is_super())    
            $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0'")->result_array();
        else
            {$branch=$this->user_model->getbranch();
            $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0' AND `id`='$branch'")->result_array();}
        $this->user_model->check_permissions("Voucher/arrears_form");
        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        
        //===========================================
        
        //calling views
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('salary/arrears_form',$data);

      }
      
      public function adjust_teacher(){
   
     $branch=$this->user_model->getbranch();
    $this->db->select('teacher.id,teacher.firstname,teacher.lastname,adjust.both_id,adjust.amount');    
                $this->db->from('teacher');
              $this->db->join('adjust', 'adjust.both_id=teacher.id','LEFT');
                $this->db->where('teacher.status',0)->where('teacher.is_delete',0)
                ->where('teacher.branch',$branch);
               
                $query = $this->db->get()->result_array();
                // var_dump($query);die;
                $return="";
                $i=1;
                foreach ($query as $key => $value) {
                    $return .= "<tr>";
                    $return .= "<td>".$i++."</td>";
                    $return .= "<td><input type='hidden' name='t_ids[]' value='$value[id]'>".$value['id']."</td>";
                    $return .= "<td>".$value['firstname'].$value['lastname']."</td>";
                    $amount = empty($value['amount']) ? '0': $value['amount'];
                    $return .='<td><input type="text" class="form-control col-md-1 col-sm-2 col-xs-2" name="arrear[]" value="'.$amount.'" required="required"/>';
                        $return .= "</td>";
                        $return .= "</tr>"; 
                        }
                        echo $return;
}

public function adjust_staff(){
   $branch=$this->user_model->getbranch();
    $this->db->select('staff.id,staff.firstname,staff.lastname,adjust.both_id,,adjust.amount');    
                $this->db->from('staff');
                 $this->db->join('adjust', 'adjust.both_id=staff.id','LEFT');
                $this->db->where('staff.status',0)->where('staff.is_delete',0)
                ->where('staff.branch',$branch);
               
                $query = $this->db->get()->result_array();
                $return="";
                $i=1;
                foreach ($query as $key => $value) {
                    $return .= "<tr>";
                    $return .= "<td>".$i++."</td>";
                    $return .= "<td><input type='hidden' name='t_ids[]' value='$value[id]'>".$value['id']."</td>";
                    $return .= "<td>".$value['firstname'].$value['lastname']."</td>";
                    $amount = empty($value['amount']) ? '0': $value['amount'];
                    $return .='<td><input type="text" class="form-control col-md-1 col-sm-2 col-xs-2" name="arrear[]" value="'.$amount.'" required="required"/>';
                        $return .= "</td>";
                        $return .= "</tr>"; 
                        }
                        echo $return;
}

	public function getArrearemp()
	{
	   // echo "<pre>";print_r($this->input->post());exit;
		$type=$this->input->post('type',true);
		$for_date=$this->input->post('for_date',true);
		$arrear=$this->input->post('arrear',true);
		$t_ids=$this->input->post('t_ids',true);
		$data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		
		foreach($arrear as $key=>$value){
            $array = array(
                'both_id'=>$t_ids[$key],
                'ref'=>$type,    
                'amount'=>$value,    
                'month'=>date('Y-m-d',strtotime($for_date)),    
            );
            $this->db->insert('adjust',$array);
        }
        
    	redirect('Salary/adjust','refresh');
	}

	public function getArrearStudent()
	{
	   // echo "<pre>";print_r($this->input->post());exit;
		$branch=$this->input->post('branch',true);
		$class=$this->input->post('class',true);
		$section=$this->input->post('section',true);
		$for_date=$this->input->post('for_date',true);
		$tag_date=$this->input->post('tag_date',true);
		$arrear=$this->input->post('arrear',true);
		$data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		//var_dump($grno);die();
		$this->db->where('class_id',$class);
		$this->db->where('section_id',$section);
		$this->db->delete('arrears');
		
		foreach($arrear as $key=>$value){
            $array = array(
                'promotion_id'=>$key,    
                'class_id'=>$class,    
                'section_id'=>$section,    
                'amount'=>$value,    
                'add_month'=>date('Y-m-d',strtotime($for_date)),    
                'for_month'=>date('Y-m-d',strtotime($tag_date)),    
                'status'=>($value==0) ? '1':'0',
            );
            $this->db->insert('arrears',$array);
        }
        
    	redirect('Voucher/arrears_form','refresh');
	}

    
    public function increments($id=0)
        {
         $this->user_model->check_permissions("salary/increments");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        if($id>0){
            $data['edit']=$this->db->where('id',$id)->get('increments')->row_array();
        }else{
            $data['edit']=null;
        }
        $data['view']=$this->db->where('is_del',0)->get('increments')->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/increments",$data);
        $this->load->view("footer");
        }
        
         public function saveincrements()
        {
             if($this->input->post()){

            $data=$this->input->post();
              if($data['id']>0){
                  
                  $prev_slary=$this->db->select('*')
                                      ->from('increments')
                                      ->where('id',$data['id'])
                                      ->get()->row()->basic_salary;
                                      
                  $field = array('refrence' =>$data['type'] ,
                             'amount'=>$data['amount'] ,  
                             'month'=>$data['month'] ,  
                             'bothid'=>$data['emp']
                             );
                  
                  $this->db->where('id',$data['id']);
                $bool=$this->db->update('increments',$field);
                 $new_salary=$prev_slary+$data['amount'];
                  $up_data=array(
                           'salery'=>$new_salary
                           );
                   if($data['type'] =="teacher"){
                      
                      //  $this->db->where('id',$data['emp'])
                      //           ->update('teacher',$up_data);
                                
                   }else
                   {
                     //  $this->db->where('id',$data['emp'])
                      //           ->update('staff',$up_data);
                   }
                   
            if($bool==true){
              
                redirect('salary/increments','refresh');
            }else{
                echo "<h1>Error Ocurred while Saving Data</h1>";
            }
            
              }//end update
              else{
                  
           if($data['type'] =="teacher"){
                $get_salary=$this->db->select('*')
                                     ->from('teacher')
                                     ->where('id',$data['emp'])
                                     ->get()->row()->salery;
               
            }else
            {
                $get_salary=$this->db->select('*')
                                     ->from('staff')
                                     ->where('id',$data['emp'])
                                     ->get()->row()->salery;
            }
            
            $field = array('refrence' =>$data['type'] ,
                             'amount'=>$data['amount'] ,  
                             'month'=>$data['month'] ,  
                             'bothid'=>$data['emp'] ,  
                             'basic_salary'=>$get_salary
                             );
                  
                
                $booll=$this->db->insert('increments',$field);
           if($booll==true){
              
                redirect('salary/increments','refresh');
            }else{
                echo "<h1>Error Ocurred while Saving Data</h1>";
            }
        }
        
             }
        }

        
         public function actionsincre($ref,$id)
    {
        if($ref=="edit" && $id!=0){
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
       $emp=$this->sm->idincrements($id);
       
        $data['emp']=$emp;
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/incrementsedit",$data);
        $this->load->view("footer");

        }else if($ref=="del" && $id!=0){
           
           $dat = array(
               'is_del'=>1
             );
             $check=$this->sm->Update("increments",$dat,"id",$id);
            if($check==true){
                 redirect('salary/increments','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }
    }
    
      public function loan()
    {
        $this->user_model->check_permissions("salary/index");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        
        $branchid=$this->user_model->getbranch();
        $data['loan'] = $this->db->where('is_delete',0)->where('branch',$branchid)->get('loan_def')->result_array();
        
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/loan",$data);
        $this->load->view("footer");
    }
    
     public function getln()
    {
        $branchid=$this->user_model->getbranch();
        
                    $data= $this->db->where('is_delete',0)->where('branch',$branchid)->get('loan_def')->result_array();
                        if($data!=null){
                        foreach ($data as $key => $value) 
                        {
                        echo "<option value='".$value['id']."'>".$value['name']." </option>";
                        }
                        }else{
                            echo "";
                        }

                 
                 
      }
            
     public function act_dec_loan($t="")
    {
        $branchid=$this->user_model->getbranch();
         $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get($t)->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive_loan($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active_loan($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
        //return $data;
    }

public function save_loan($name,$des)
    {
           $branch=$this->user_model->getbranch();
            $myname=rawurldecode($name);
            
    $search=$this->db->select('*')
                     ->from('loan_def')
                     ->where('name',$myname)
                     ->where('is_delete',0)
                     ->where('branch',$branchid)
                     ->get()->result_array();
                if(empty($search)) 
                {    
           $dat = array(
                'name' =>rawurldecode($name),
            'branch'=>rawurldecode($branch),
           'description'=>rawurldecode($des)
             );
            $check=$this->sm->create("loan_def",$dat);
            if($check==true){
                $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('loan_def')->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive_loan($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active_loan($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
                
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
                }else{
                echo "false";
                }

    }
    
    public function update_loan($id,$name,$des)
{
  $data=array(
      'name'=>rawurldecode($name),
      'description'=>rawurldecode($des)
  );
 
  $query=$this->db->where('id',$id)
                  ->update('loan_def',$data);
                 
$branchid=$this->user_model->getbranch();
 $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('loan_def')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive_loan($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active_loan($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }


  }
}

 public function deactive_loan($id="")
   {
            $update_data=array(
                           'is_delete'=>1
                       );
                       $this->db->where('id',$id);
           
             $this->db->update('loan_def',$update_data);
        
        $branchid=$this->user_model->getbranch();   
        $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('loan_def')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive_loan($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active_loan($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }
            
              else
              {
                  echo "fales";
              }
        
              
   }
   
    public function active_loan($id="")
   {
            $update_data=array(
                           'is_delete'=>0
                       );
                       $this->db->where('id',$id);
                      $this->db->update('loan_def',$update_data);
        
        $branchid=$this->user_model->getbranch();              
        $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('loan_def')->result_array();
        $record=$data['mydata'];
               
       if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive_loan($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active_loan($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                
              }
              else
              {
                  echo "fales";
              }
             
   }
   
     public function get_loan_byid($id){
      $query=$this->db->select('*')
                     ->from('loan_def')
                     ->where('id',$id)
                     ->get()->row_array();
                echo json_encode($query);
   }
   
   public function load_teacher_loan(){
       $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('loan_assign')
                    ->join('teacher','teacher.id=loan_assign.bothid')
                    ->where('type',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('loan_def')
                           ->where('id',$value['loan_type'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[per_month]</td>
             <td>$value[recovery]</td>
             "; 
           echo  "
        <td><a href='".base_url()."salary/actions_loan/dell/".$value['id']."'  ><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_loan/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>";
                      
          echo  "</tr>";
                    }
         }  
}

public function load_staff_loan(){
    $ref="staff";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('loan_assign')
                    ->join('staff','staff.id=loan_assign.bothid')
                    ->where('type',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('loan_def')
                           ->where('id',$value['loan_type'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[per_month]</td>
             <td>$value[recovery]</td>
             "; 
             
                     
          echo  "</tr>";
                    }
         }  
}

public function load_staff_loan_byid($id){
    $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('loan_assign')
                    ->where('type',$ref)
                    ->where('bothid',$id)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from('staff')
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                     $query2=$this->db->select('*')
                           ->from('loan_def')
                           ->where('id',$value['loan_type'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[per_month]</td>
             <td>$value[recovery]</td>
             "; 
                echo  "
        <td><a href='".base_url()."salary/actions_loan/dell/".$value['id']."'  ><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_loan/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>";
          echo  "</tr>";
                    }
         }  
}

public function load_teacher_loan_byid($id){
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('loan_assign')
                    ->where('type',$ref)
                    ->where('bothid',$id)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from('teacher')
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                     $query2=$this->db->select('*')
                           ->from('loan_def')
                           ->where('id',$value['loan_type'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[per_month]</td>
             <td>$value[recovery]</td>
             "; 
                 echo  "
        <td><a href='".base_url()."salary/actions_loan/dell/".$value['id']."'  ><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_loan/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>";
          echo  "</tr>";
                    }
         }  
}

 public function actions_loan($ref,$id)
    {
        if($ref=="edit" && $id!=0){
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $emp=$this->sm->id_loan($id);
      //var_dump($emp);
       // die();
        $data['emp']=$emp;
        //$data['loan'] = $this->db->where('name','personal loan')->get('deduction_def')->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/edit_loan",$data);
        $this->load->view("footer");

        }else if($ref=="dell" && $id!=0){
           
           $dat = array(
               'is_delete'=>1
             );
             //$check=$this->sm->Update("advance",$dat,"id",$id);
             $query=$this->db->where('id',$id)
                             ->update('loan_assign',$dat);
            
                 redirect('salary/loan','refresh');
              
        }
    }
    
    public function update_loan_in()
    {

        if($this->input->post()){
        $data=$this->input->post();
         
             $date = date("Y-m-d");
             $id=$this->input->post("loanid",true);
            $dat = array(
             'type'=>$data['type'],
             'bothid' =>$data['bothid'] ,
            'amount'=>$data['amount'],
            'month'=>$data['month'], 
            'loan_type'=>$data['ln_typ'],
            'amount'=>$data['amount'],
            'per_month'=>$data['monthly_amount'],
            'recovery'=>$data['rec_month']
             );
           //  var_dump($dat);
           //  die();
             //$check=$this->sm->Update("advance",$dat,"id",$id);
             $query=$this->db->where('id',$id)
                            ->update('loan_assign',$dat);
                 redirect('salary/loan','refresh');
               
              

        }
    }
    
    public function assignloan()
{
    $search_query=$this->db->select('*')
                           ->from('loan_assign')
                           ->where('type',$this->input->post('type'))
                           ->where('bothid',$this->input->post('emp'))
                           ->where('status',0)
                           ->where('is_delete',0)
                           ->get()->result_array();
                if(empty($search_query))
   {         
  $data=array(
     'type'=>$this->input->post('type'),
     'bothid'=>$this->input->post('emp'),
     'month'=>$this->input->post('month'),
     'loan_type'=>$this->input->post('loantype'),
     'amount'=>$this->input->post('amount'),
     'per_month'=>$this->input->post('monthly_amount'),
     'recovery'=>$this->input->post('rec_month')
     );

     $this->db->insert('loan_assign',$data);
     }
     else{
      $this->session->set_flashdata('error_msg','This employee already take loan which is un paid...');
     }
     redirect('salary/loan');
}

   public function expense()
    {
        $this->user_model->check_permissions("salary/expense");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/personal_expense",$data);
        $this->load->view("footer");
    }

 public function allexpense()
    {
        $branchid=$this->user_model->getbranch();
        $data['mydata']=$this->db->where('branch',$branchid)->where('is_delete',0)->get('personal_expense')->result_array();
          $record=$data['mydata'];

      if(!empty($record))
         {
        
     foreach ($record as $key => $value) {
         echo "
         <div style='padding-bottom:10px;' class='row'>
         <input type='hidden' value='' name='hid$value[id]' id='hid$value[id]'>

         <div class='col-md-4 col-sm-4 col-xs-2'>
 <input type='checkbox'  name='allowance_id[]'  value='$value[id]'  class='allowance_id'> $value[name] 
                          </div>  

         <div class='col-md-3 col-sm-3 col-xs-3'>
                          <input type='text' class='form-control'  placeholder='IN RS'  onclick='Rup($value[id],event)' id='allowance_rup$value[id]' name='allowance_amount2[$value[id]]' >
                    </div> 

                 </div>                            
         ";
            }

         }else
         {
             echo "record not found";
         }
    }
    
    public function act_dec_expense($t="")
    {
        $branchid=$this->user_model->getbranch();
         $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get($t)->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
        //return $data;
    }

 public function save_expense($name,$des)
    {
           $branch=$this->user_model->getbranch();
            $myname=rawurldecode($name);
    $search=$this->db->select('*')
                     ->from('personal_expense')
                     ->where('name',$myname)
                     ->where('branch',$branch)
                     ->where('is_delete',0)
                     ->get()->result_array();
                if(empty($search)) 
                {
           $dat = array(
                'name' =>rawurldecode($name),
            'branch'=>rawurldecode($branch),
           'description'=>rawurldecode($des)
             );
            $check=$this->sm->create("personal_expense",$dat);
            if($check==true){
                $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branch)->get('personal_expense')->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
                }else
                {
                 echo "false";
                }

    }
    
    public function update_expense($id,$name,$des)
{
  $data=array(
      'name'=>rawurldecode($name),
      'description'=>rawurldecode($des)
  );
 
  $query=$this->db->where('id',$id)
                  ->update('personal_expense',$data);
                 
$branchid=$this->user_model->getbranch();
 $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('personal_expense')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }


  }
}

public function deactive_expense($id="")
   {
       $branchid=$this->user_model->getbranch();
            $update_data=array(
                           'is_delete'=>1
                       );
                       $this->db->where('id',$id);
           
             $this->db->update('personal_expense',$update_data);
           
           $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('personal_expense')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }
            
              else
              {
                  echo "fales";
              }
        
              
   }

 public function active_expense($id="")
   {
            $update_data=array(
                           'is_delete'=>0
                       );
                       $this->db->where('id',$id);
                      $this->db->update('personal_expense',$update_data);
            
            $branchid=$this->user_model->getbranch();          
         $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('personal_expense')->result_array();
         $record=$data['mydata'];
               
       if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                
              }
              else
              {
                  echo "fales";
              }
             
   }
   
    public function get_record_expense($id){
      $query=$this->db->select('*')
                     ->from('personal_expense')
                     ->where('id',$id)
                     ->get()->row_array();
                echo json_encode($query);
   }

public function load_teacher_expense(){
    $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('expense_assign')
                    ->join('teacher','teacher.id=expense_assign.bothid')
                    ->where('refrence',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
        echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('personal_expense')
                           ->where('id',$value['expense_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

public function load_staff_expense(){
    $branch=$this->user_model->getbranch();
    $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('expense_assign')
                    ->join('staff','staff.id=expense_assign.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('personal_expense')
                           ->where('id',$value['expense_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

public function teacher_expense_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from('expense_assign')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                   
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                     $query2=$this->db->select('*')
                           ->from('personal_expense')
                           ->where('id',$value['expense_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

 public function saveteacherexpense()
    { 

        
            $data=$this->input->post();
           
           //var_dump('<pre>',$data);
           //die();
                       $dat=array(
                    'bothid'=>$data['emp'],
                    'refrence'=>$data['type']
                );
               
           $query=$this->db->select('*')
                          ->from('expense_assign')
                          ->where('bothid',$data['emp'])
                          ->where('refrence',$data['type'])
                          ->get()->result_array();
                     if(!empty($query)) 
                     {
                           $up_data=array(
                             'status'=>1
                              );
                 $this->db->where('bothid',$data['emp'])
                  ->where('refrence',$data['type'])
                  ->update('expense_assign',$up_data); 
                         
                     }  
                    if(!empty($data['allowance_id']))
                    {
            foreach ($data['allowance_id'] as $key => $value) {
                $insert=$dat;
                $insert['expense_id']=$value;
               
                 if(empty($data['allowance_amount2'][$value]))
                {
                    $data['allowance_amount2'][$value]=0;
                }
                $insert['rupess']=$data['allowance_amount2'][$value];
               //$insert['month']=date('Y-m');
              $insert['month']=$this->input->post('month');
            
                    $this->db->insert('expense_assign',$insert);
            
            }
            
            redirect('salary/expense');
            }else{
             
             $this->session->set_flashdata('error','Please Select Expanse Type...');
             redirect('salary/expense');
            }

    }
    
    public function hitt_att()
    {
        $staffs=$this->db->select('*')
                         ->from('staff')
                         ->where('branch',2)
                         ->where('status',0)
                         ->where('is_delete',0)
                         ->get()->result_array();
        var_dump('<pre>',$staffs);
        die();
    }


    public function day()
    {
    
       $this->user_model->check_permissions("salary/day");
       $data['menu'] = $this->load_model->menu();
       $data['base_url'] = base_url();
       $data['userInfo'] = $this->userInfo;
        
       $branch=$this->user_model->getbranch();
       $data['lev_types']=$this->db->select('*')
                               ->from('leave_types')
                               ->where('branch',$branch)
                               ->order_by('id',"ASC")
                               ->where('name!=',"A")
                               ->get()->result_array();
                               
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/day",$data);
        $this->load->view("footer");
    }
    
    public function saveday()
{
if($this->input->post())
{

    $type=$this->input->post('ref');
    $month=$this->input->post('month');
   $check=$this->input->post('check',true);
  if(empty($check))
  {
      $this->session->set_flashdata('errmsg','Please select employee for deduction..');
      redirect('Salary/day');
  }
   $ab=$this->input->post('absnt');
   $per_day=$this->input->post('per_day');
   $total=$this->input->post('total');
   $bouns=$this->input->post('bouns');
   
  foreach($check as $key=>$value){
     
      $student_att=array(
                		'type'=>$type,
                		'bothid'=>$value,
                         'month'=>$month,
                         'absent_days'=>$ab[$key],
                         'deduction_day'=>$per_day[$key],
                         'deduct_amount'=>$total[$key],
                         'bounes'=>$bouns[$key],
                         'status'=>0,
                         'is_delete'=>0
                		);
                       // var_dump($pr[$key]);var_dump($ab[$key]);var_dump($per_day[$key]);var_dump($total[$key]);
                        //die();
  //Check Existing data
  $check_data=$this->db->select('*')
                       ->from('days_deduction')
                       ->where('type',$type)
                       ->where('bothid',$value)
                       ->where('month',$month)
                       ->where('status',0)
                       ->where('is_delete',0)
                       ->get()->result_array();
              if(empty($check_data))
              {
                  $this->db->insert('days_deduction',$student_att);
              }else
              {
                $up_data=array(
                   'is_delete'=>1
                );
                $this->db->where('type',$type)
                         ->where('bothid',$value)
                         ->where('month',$month)
                         ->where('status',0)
                         ->update('days_deduction',$up_data);
                         $this->db->insert('days_deduction',$student_att);
              }         

  }

redirect('salary/day');

}


}


     public function getsal($type="",$month,$id=0,$day)
                          {
                          $org_mnth=$month;
                          $re=0;
                                        $year=date('Y');
                                        $m = date('m');
                                        $month=substr($month,-2);
                                        $d= cal_days_in_month(CAL_GREGORIAN,$month,$year);
                                        
                                        $this->db->select('salery')->from('teacher');
                                        $this->db->where('is_delete',0)->where('status',0)->where('id',$id);                                        
                                        $re=$this->db->get()->row_array()['salery'];
       
                                   //Increments
                                    $increments=$this->db->select('sum(amount) as amount')
                                                                   ->from('increments')
                                                                   ->where('refrence',"teacher")
                                                                   ->where('bothid',$id)
                                                                   ->where('month <=',$org_mnth)
                                                                   ->where('is_del',0)
                                                                   ->get()->row_array()['amount'];
                                            if(!empty($increments)) 
                                            {
                                                $re=$re+$increments;
                                            }else
                                            {
                                                $re=$re;
                                            }


                                        if($d!=0)
                                        {
                                            $sal_day = $re/$d;//one day salary
                                            $sal = $day*$sal_day;
                                           
                                            $amount=round($sal, 2);
                                            $sal_day=round($sal_day, 2);
                                            $data=array(
                                                'per_day'=>$sal_day,
                                                'amount'=>$amount
                                            );
                                           echo json_encode($data);
                                           
                                       }
                                          else{
                                            return false;
                                          }

            }
    
     public function getsal_staff($type="",$month,$id=0,$day)
                          {
                            $org_mnth=$month;
                             $re=0;
                                        $year=date('Y');
                                        $m = date('m');
                                        $month=substr($month,-2);
                                        $d= cal_days_in_month(CAL_GREGORIAN,$month,$year);
                                        
                                        $this->db->select('salery')->from('staff');
                                        $this->db->where('is_delete',0)->where('status',0)->where('id',$id);                                        
                                        $re=$this->db->get()->row_array()['salery'];

                                        //Increments
                                     $increments=$this->db->select('sum(amount) as amount')
                                                                   ->from('increments')
                                                                   ->where('refrence',"staff")
                                                                   ->where('bothid',$id)
                                                                   ->where('month <=',$org_mnth)
                                                                   ->where('is_del',0)
                                                                   ->get()->row_array()['amount'];
                                            if(!empty($increments)) 
                                            {
                                                $re=$re+$increments;
                                            }else
                                            {
                                                $re=$re;
                                            }
          

                                        if($d!=0)
                                        {
                                           $sal_day = $re/$d;//one day salary
                                            $sal = $day*$sal_day;
                                           // $sal = $sal *$sal_day;
                                           // $sal=$d-$day;
                                            $amount=round($sal, 2);
                                            $sal_day=round($sal_day, 2);
                                            $data=array(
                                                'per_day'=>$sal_day,
                                                'amount'=>$amount
                                            );
                                           echo json_encode($data);
                                           
                                       }
                                          else{
                                            return false;
                                          }
 }
 
 public function deduction_view_teacher($month,$typ)
    {
     
          $branch=$this->user_model->getbranch();
        $data['cls']=$this->db->query("SELECT * FROM `days_deduction` inner join teacher on teacher.id=days_deduction.bothid
      where teacher.branch='$branch' and teacher.status=0 and teacher.is_delete=0  and days_deduction.is_delete=0  and days_deduction.type='$typ' and days_deduction.month='$month'")->result_array();
       
       $lev_types=$this->db->select('*')
                               ->from('leave_types')
                               ->where('branch',$branch)
                               ->where('name!=',"A")
                               ->order_by('id',"ASC")
                               ->get()->result_array();
                               
      if(!empty($data['cls']))
     {  
          $acc_sum=0;
           $acc_absnt=0;
           $no_mark=0;
         $i=0;        
  foreach ($data['cls'] as $key => $value) 
        {        
     $i++;
    echo "<tr>
        <td>$i</td>";

        echo"<td>$value[firstname] $value[lastname]</td>";
         if(!empty($lev_types))
        {
            
            foreach($lev_types as $lev)
            {
              
                    $fetch_att_teacher=$this->db->select('count(id) as id')
                                            ->from('teacherattendance')
                                            ->where('teacher_id',$value['id'])
                                            ->where('left(date,7)',$month)
                                            ->where('status_id',$lev['id'])
                                            ->where('is_deleted',0)
                                            ->get()->row_array()['id'];
                
  
               
         echo" <td>$fetch_att_teacher</td>";  
            }
        }
         echo "<td>$value[absent_days]</td>
         <td>$value[deduction_day]</td>
         <td>$value[deduct_amount]</td>
        
             </tr>"; 
          
            }


             }
             echo "record not found..";
    }
    
     public function deduction_view_staff($month,$typ)
    {
     
          $branch=$this->user_model->getbranch();
        $data['cls']=$this->db->query("SELECT * FROM `days_deduction` inner join staff on staff.id=days_deduction.bothid
      where staff.branch='$branch' and staff.status=0 and staff.is_delete=0  and days_deduction.is_delete=0  and days_deduction.type='$typ' and days_deduction.month='$month'")->result_array();
      
      $lev_types=$this->db->select('*')
                               ->from('leave_types')
                               ->where('branch',$branch)
                               ->where('name!=',"A")
                               ->order_by('id',"ASC")
                               ->get()->result_array();
                               
      if(!empty($data['cls']))
     {       
          $acc_sum=0;
           $acc_absnt=0;
           $no_mark=0;
         $i=0;        
  foreach ($data['cls'] as $key => $value) 
        {        
     $i++;
    echo "<tr>
        <td>$i</td>";
    echo"
        <td>$value[firstname] $value[lastname]</td>";
         
            if(!empty($lev_types))
        {
            
            foreach($lev_types as $lev)
            {
              
                    $fetch_att_teacher=$this->db->select('count(id) as id')
                                            ->from('staffatt')
                                            ->where('staff_id',$value['id'])
                                            ->where('left(date,7)',$month)
                                            ->where('status_id',$lev['id'])
                                            ->where('is_deleted',0)
                                            ->get()->row_array()['id'];
                
  
               
         echo" <td>$fetch_att_teacher</td>";  
            }
        }
        
         echo"<td>$value[absent_days]</td>
         <td>$value[deduction_day]</td>
         <td>$value[deduct_amount]</td>
         </tr>"; 
          
            }


             }
             echo "record not found..";
    }
    
    public function find_days($month)
{
     $year=date('Y');
     $month=substr($month,-2);
     $d= cal_days_in_month(CAL_GREGORIAN,$month,$year);
     echo json_encode($d);
}


    //+++++++++++++++++Defination++++++++++++++++++++++++++++++
     public function def($id=0)
    {
        $this->user_model->check_permissions("salary/def");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        if($id>0){
            $data['edit']=$this->db->where('id',$id)->get('deduction_def')->row_array();
        }else{
            $data['edit']=null;
        }
        $data['view']=$this->db->where('is_delete',0)->get('deduction_def')->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/deduction_def",$data);
        $this->load->view("footer");
    }
    
    public function savedef()
    {
        if($this->input->post()){

            $data=$this->input->post();
            $flag=empty($this->input->post('flag'))? 0 :$this->input->post('flag');
            $field = array('name' =>$data['name'],
                             'amount'=>$data['amount'],
                             'flag'=>$flag  
                    );
                    
            if($data['id']>0){
                
                $this->db->where('id',$data['id']);
                $bool=$this->db->update('deduction_def',$field);
            
                
            }else{
                $bool=$this->db->insert('deduction_def',$field);
            }
            if($bool==true){
                redirect('salary/def','refresh');
            }else{
                echo "<h1>Error Ocurred while Saving Data</h1>";
            }
        }
    }
    public function status($id=0)
    {
        $field = array('is_delete' =>1);
        $this->db->where('id',$id);
        $this->db->update('deduction_def',$field);
        redirect('salary/def','refresh');
    }
    

//+++++++++++++++++Defination++++++++++++++++++++++++++++++
    public function index()
    {
        $this->user_model->check_permissions("salary/index");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
       
        
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/advance",$data);
        $this->load->view("footer");
    }
    
    public function load_teacher_advance(){
    $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                            ->from('advance')
                            ->join('teacher','teacher.id=advance.bothid')
                            ->where('type',$ref)
                            ->where('advance.is_delete',0)
                            ->where('teacher.branch',$branch)
                            ->where('teacher.status',0)
                            ->where('teacher.is_delete',0)
                            ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>
             <td>$value[amount]</td>
              <td>$value[month]</td>
             <td>$value[description]</td> "; 
          echo  "
        <td><a href='".base_url()."salary/actions_advance/dell/".$value['id']."'  ><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_advance/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>
          </tr>";
                    }
         }  
}

public function load_staff_advance(){
    $ref="staff";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('advance')
                    ->join('staff','staff.id=advance.bothid')
                     ->where('type',$ref)
                    ->where('advance.is_delete',0)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            
             echo "
             <td>$value[firstname] $value[lastname]</td>
             <td>$value[amount]</td>
             <td>$value[month]</td>
             <td>$value[description]</td>
             
             "; 
                      
          echo  "
        <td><a href='".base_url()."salary/actions_advance/dell/".$value['id']."'><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_advance/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>
          </tr>";
                    }
         }  
}

public function teacher_salary($id)
{
    $salery=$this->db->select('*')
                    ->from('teacher')
                    ->where('id',$id)
                    ->get()->row()->salery;
                   
                echo $salery;    
}

public function teacher_salary2($id)
{
      $increments=$this->db->select('sum(amount) as amount')
                                       ->from('increments')
                                       ->where('refrence',"teacher")
                                       ->where('bothid',$id)
                                       ->where('is_del',0)
                                       ->get()->row_array()['amount'];
                if(!empty($increments)) 
                {
                    $increment=$increments;
                }else
                {
                    $increment=0;
                }
                
    $salery=$this->db->select('*')
                    ->from('teacher')
                    ->where('id',$id)
                    ->get()->row()->salery;
                   
                echo $salery+$increment;    
}

public function teacher_salaryinc($id,$month)
{
     $increments=$this->db->select('sum(amount) as amount')
                                       ->from('increments')
                                       ->where('refrence',"teacher")
                                       ->where('bothid',$id)
                                       ->where('month <=',$month)
                                       ->where('is_del',0)
                                       ->get()->row_array()['amount'];
                if(!empty($increments)) 
                {
                    $increment=$increments;
                }else
                {
                    $increment=0;
                }

    $salery=$this->db->select('*')
                    ->from('teacher')
                    ->where('id',$id)
                    ->get()->row()->salery;
                   
                echo $salery+$increment;    
}

public function staff_pf($id)
{
    $salery=$this->db->select('total')
                    ->from('provident_fund')
                    ->where('bothid',$id)
                    ->where('status',0)
                    ->where('refrence',"staff")
                    ->get()->row_array()['total'];
                   
                echo $salery;    
}

public function saveprovident_return()
{
    $type=$this->input->post('type');
    $emp=$this->input->post('emp');
    $month=$this->input->post('month');
    $total=$this->input->post('basic_salary');
    $allowance_amount22=$this->input->post('allowance_amount22');
    $after=$total-$allowance_amount22;
    $data=array(
        'bothid'=>$emp,
        'refrence'=>$type,
        'month'=>$month,
        'rupess'=>$allowance_amount22,
        'total_before'=>$total,
        'total_after'=>$after
        );
    $res=$this->db->insert('return_pf',$data);
    if($res){
        $up_data=array(
            'total'=>$after
            );
            $this->db->where('bothid',$emp)
                     ->where('refrence',$type)
                     ->where('status',0)
                     ->update('provident_fund',$up_data);
             $this->session->set_flashdata('errormsgded','Data Successfully Saved...');   
             redirect('salary/Return_provident_fund');
    }
}

public function saveprovident_add()
{
    $type=$this->input->post('type');
    $emp=$this->input->post('emp');
    $month=$this->input->post('month');
    $allowance_amount22=$this->input->post('allowance_amount22');
    $allowance_amount221=$this->input->post('allowance_amount221');
    $old=$this->db->select('total')
                  ->from('provident_fund')
                  ->where('refrence',$type)
                  ->where('bothid',$emp)
                  ->where('status',0)
                  ->get()->result_array()['total'];
                  
                  if(empty($old))
                  {
                      $create_emp=array(
                           'bothid'=>$emp,
                           'refrence'=>$type,
                           'month'=>$month,
                           'percentage'=>0,
                           'rupess'=>0,
                           'status'=>0,
                           'org_percentage'=>0,
                           'org_rupess'=>0,
                           'total'=>0
                          );
                          $this->db->insert('provident_fund',$create_emp);
                      $old=0;
                  }else
                  {
                      $old=$old;
                  }
    $after=$old+$allowance_amount22+$allowance_amount221;
    $data=array(
        'bothid'=>$emp,
        'refrence'=>$type,
        'month'=>$month,
        'amount'=>$allowance_amount22,
        'amount_org'=>$allowance_amount221
        );
    $res=$this->db->insert('Add_PF',$data);
    if($res){
        $up_data=array(
            'total'=>$after
            );
            $this->db->where('bothid',$emp)
                     ->where('refrence',$type)
                     ->where('status',0)
                     ->update('provident_fund',$up_data);
             $this->session->set_flashdata('errormsgded','Data Successfully Saved...');   
             redirect('salary/Add_PF');
    }
}

public function teacher_pf($id)
{
    $salery=$this->db->select('total')
                    ->from('provident_fund')
                    ->where('bothid',$id)
                    ->where('status',0)
                     ->where('refrence',"teacher")
                     ->get()->row_array()['total'];
                   
                echo $salery;    
}

public function teacher_pfadd($id)
{
    $salery=$this->db->select('amount')
                    ->from('Add_PF')
                    ->where('bothid',$id)
                     ->where('refrence',"teacher")
                     ->get()->row_array()['amount'];
                   
                echo $salery;    
}

public function staff_pfadd($id)
{
    $salery=$this->db->select('amount')
                    ->from('Add_PF')
                    ->where('bothid',$id)
                    ->where('refrence',"staff")
                    ->get()->row_array()['amount'];
                   
                echo $salery;    
}

public function check_pf($month,$ref,$emp)
{
  $salery=$this->db->select('*')
                    ->from('return_pf')
                    ->where('bothid',$emp)
                    ->where('month',$month)
                     ->where('refrence',$ref)
                     ->get()->result_array();
         if(!empty($salery))
         {
             echo "1";
         }
         else
         {
             echo "0";
         }
}

public function teacher_advnc($val)
{
     $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('advance')
                    ->where('type',$ref)
                    ->where('is_delete',0)
                    ->where('bothid',$val)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from('teacher')
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>
             <td>$value[amount]</td>
             <td>$value[description]</td>
             <td>$value[month]</td>
             "; 
                    
          echo  "
        <td><a href='".base_url()."salary/actions_advance/dell/".$value['id']."'><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_advance/edit/".$value['id']."' ><i class='fa fa-pencil'></i></a>
             </td>
          </tr>";
                    }
         }  
}

public function staff_salary($id)
{
    $salery=$this->db->select('*')
                    ->from('staff')
                    ->where('id',$id)
                    ->get()->row()->salery;
                echo $salery;    
}

public function staff_salary2($id)
{
    $increments=$this->db->select('sum(amount) as amount')
                                       ->from('increments')
                                       ->where('refrence',"teacher")
                                       ->where('bothid',$id)
                                       ->where('is_del',0)
                                       ->get()->row_array()['amount'];
                            if(!empty($increments)) 
                            {
                                $increment=$increments;
                            }else
                            {
                                $increment=0;
                            }
                
    $salery=$this->db->select('*')
                    ->from('staff')
                    ->where('id',$id)
                    ->get()->row()->salery;
                echo $salery+$increment;    
}

public function staff_salaryinc($id,$month)
{
     $increments=$this->db->select('sum(amount) as amount')
                                       ->from('increments')
                                       ->where('refrence',"teacher")
                                       ->where('bothid',$id)
                                       ->where('month <=',$month)
                                       ->where('is_del',0)
                                       ->get()->row_array()['amount'];
                if(!empty($increments)) 
                {
                    $increment=$increments;
                }else
                {
                    $increment=0;
                }
    $salery=$this->db->select('*')
                    ->from('staff')
                    ->where('id',$id)
                    ->get()->row()->salery;
                echo $salery+$increment;    
}

public function staff_advnc($val)
{
     $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('advance')
                    ->where('type',$ref)
                    ->where('is_delete',0)
                    ->where('bothid',$val)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from('staff')
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>
             <td>$value[amount]</td>
             <td>$value[description]</td>
             <td>$value[month]</td>
             ";         
          echo  "
        <td><a href='".base_url()."salary/actions_advance/dell/".$value['id']."'><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actions_advance/edit/".$value['id']."' ><i class='fa fa-pencil'></i></a>
             </td>
          </tr>";
                    }
         }  
}

public function get_alladvance($mnth,$ref,$bothid)
{
    $salery=$this->db->select('sum(amount) as total')
                    ->from('advance')
                    ->where('month',$mnth)
                    ->where('type',$mnth)
                    ->where('bothid',$bothid)
                    ->get()->row_array()['total'];
                echo $salery;    
}

public function edit_adv($id)
{
    $query=$this->db->select('*')
                   ->from('advance')
                   ->where('id',$id)
                   ->get()->row_array();
                   echo json_encode($query);
}

public function dell_adv($id)
{
    $data=array(
      'is_delete'=>1
    );
    $query=$this->db->where('id',$id)
                    ->update('advance',$data);
    redirect('Salary/loan','refresh');
}

 public function actions_advance($ref,$id)
    {
        if($ref=="edit" && $id!=0){
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $emp=$this->sm->id_advance($id);
      //var_dump($emp);
       // die();
        $data['emp']=$emp;
        $data['loan'] = $this->db->where('name','personal loan')->get('deduction_def')->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/edit_sal",$data);
        $this->load->view("footer");

        }else if($ref=="dell" && $id!=0){
           
           $dat = array(
               'is_delete'=>1
             );
             //$check=$this->sm->Update("advance",$dat,"id",$id);
             $query=$this->db->where('id',$id)
                             ->update('advance',$dat);
            
                 redirect('salary/index','refresh');
              
        }
    }
    
    public function update()
    {

        if($this->input->post()){
        $data=$this->input->post();
         
             $date = date("Y-m-d");
             $id=$this->input->post("advanceid",true);
            $dat = array(
             'type'=>$data['type'],
             'bothid' =>$data['bothid'] ,
            'amount'=>$data['amount'],
            'month'=>$data['month'], 
            'description'=>$data['des'],
            
             );
           //  var_dump($dat);
           //  die();
             //$check=$this->sm->Update("advance",$dat,"id",$id);
             $query=$this->db->where('id',$id)
                            ->update('advance',$dat);
                 redirect('salary/index','refresh');
               
              

        }
    }

public function update_branch()
{
    $all_rec=$this->db->select('*')
                      ->from('advance')
                      ->where('is_delete',0)
                      ->where('type',"staff")
                      ->get()->result_array();
        
        var_dump('<pre>',$all_rec);
            die();   
        // foreach($all_rec as $row)     
        // {
        //     $get_branch=$this->db->select('*')
        //                          ->from('staff')
        //                          ->where('id',$row['bothid'])
        //                          ->get()->row_array()['branch'];
        //             $data=array(
        //                 'branch'=>$get_branch
        //                 );   
        //             $this->db->where('id',$row['id'])
        //                      ->update('advance',$data);
        // }
                   
}

  public function save_advance()
    {
        
        $branchid=$this->user_model->getbranch();
             $data=array(
                'type'=>$this->input->post('type'),
                'bothid'=>$this->input->post('emp'),
                'amount'=>$this->input->post('adv_amount'),
                'description'=>$this->input->post('des'),
                'month'=>$this->input->post('month'),
                'branch'=>$branchid,
               
             );
            $this->db->insert('advance',$data);
            redirect('salary/index');
    }

    public function saveadv()
    {

        if($this->input->post()){
            $data=$this->input->post();
           
            $date = date("Y-m-d");
            $dat = array(
                'bothid' =>$data['emp'] ,
            'refrence'=>$data['type'],
            'month'=>$data['month'],
            'Amount'=>$data['amount'],
            'loantype'=>$data['loantype'],
            'date'=>$date
             );
            $this->sm->create("advancesal",$dat);
            // $id=$this->db->insert_id();
             $dat1=array(
                 'bothid' =>$data['emp'] ,
            'refrence'=>$data['type'],
            'month'=>$data['month'],
             );
              $this->sm->create("deduction",$dat1);
                        
              $id=$this->db->insert_id();
             
             $dat2=array(
             'typeid'=>$data['loantype'],
             'remaning'=>$data['amount'],   
             'deductid'=>$id                    
             );
            
             $this->sm->create("deduction_history",$dat2);
 //           if($check==true && $check1==true && $check2==true){
                 redirect('salary/index','refresh');
                
        }
    }
    public function actions($ref,$id)
    {
        if($ref=="edit" && $id!=0){
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $emp=$this->sm->id($id);
        $data['emp']=$emp;
        $data['loan'] = $this->db->where('name','personal loan')->get('deduction_def')->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/edit_sal",$data);
        $this->load->view("footer");

        }else if($ref=="del" && $id!=0){
           
           $dat = array(
               'is_delete'=>1
             );
             $check=$this->sm->Update("advancesal",$dat,"id",$id);
            if($check==true){
                 redirect('salary/index','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }
    }
     
    //Allonce functions
    
    public function allonce()
    {
        $this->user_model->check_permissions("salary/allonce");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/allonce",$data);
        $this->load->view("footer");
    }
    
     public function allallowncee()
    {
        $branchid=$this->user_model->getbranch();
        $data['mydata']=$this->db->where('branch',$branchid)->where('is_delete',0)->get('allonce')->result_array();
          $record=$data['mydata'];

      if(!empty($record))
         {
        
     foreach ($record as $key => $value) {
         echo "
         <div style='padding-bottom:10px;' class='row'>
         <input type='hidden' value='' name='hid$value[id]' id='hid$value[id]'>

         <div class='col-md-4 col-sm-4 col-xs-2'>
 <input type='checkbox'  name='allowance_id[]'  value='$value[id]'  class='allowance_id'> $value[name] 
                          </div> 

               <div class='col-md-3 col-sm-3 col-xs-3'>
                          <input type='text' class='form-control' placeholder='IN %' onclick='Per($value[id],event)' id='allowance_per$value[id]' name='allowance_amount1[$value[id]]' >
                    </div>  

         <div class='col-md-3 col-sm-3 col-xs-3'>
                          <input type='text' class='form-control'  placeholder='IN RS'  onclick='Rup($value[id],event)' id='allowance_rup$value[id]' name='allowance_amount2[$value[id]]' >
                    </div> 

                 </div>                            
         ";
            }

         }else
         {
             echo "record not found";
         }
    }
    
    public function act_dec($t="")
    {
        $branchid=$this->user_model->getbranch();
         $this->db->select('*');
         $this->db->order_by('id','desc');
        $data['mydata']=$this->db->where('branch',$branchid)->get($t)->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
        //return $data;
    }
    
    public function save_allonce($name,$des)
    {
    $myname=rawurldecode($name);
      $branchid=$this->user_model->getbranch();
    $search=$this->db->select('*')
                     ->from('allonce')
                     ->where('name',$myname)
                     ->where('is_delete',0)
                     ->order_by('id','desc')
                     ->where('branch',$branchid)
                     ->get()->result_array();
                    // var_dump($name);
                    // die();
      if(empty($search)) 
          {        
           $branch=$this->user_model->getbranch();
           $dat = array(
                'name' =>rawurldecode($name),
            'branch'=>rawurldecode($branch),
           'description'=>rawurldecode($des)
             );
            $check=$this->sm->create("allonce",$dat);
            if($check==true){
                $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('allonce')->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
                
                }else
                {
                 echo "false";
                }

    }
    
    public function update_allonceee($id,$name,$des)
{
  $data=array(
      'name'=>rawurldecode($name),
      'description'=>rawurldecode($des)
  );
 
  $query=$this->db->where('id',$id)
                  ->update('allonce',$data);
                 
$branchid=$this->user_model->getbranch();
 $this->db->select('*');
 $this->db->order_by('id','desc');
        $data['mydata']=$this->db->where('branch',$branchid)->get('allonce')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }


  }
}

 public function deactive($id="")
   {
            $update_data=array(
                           'is_delete'=>1
                       );
                       $this->db->where('id',$id);
           
             $this->db->update('allonce',$update_data);
           
        $branchid=$this->user_model->getbranch();
        $this->db->select('*');
        $this->db->order_by('id','desc');
        $data['mydata']=$this->db->where('branch',$branchid)->get('allonce')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }
            
              else
              {
                  echo "fales";
              }
        
              
   }
   
    public function active($id="")
   {
            $update_data=array(
                           'is_delete'=>0
                       );
                       $this->db->where('id',$id);
                      $this->db->update('allonce',$update_data);
                      
        $branchid=$this->user_model->getbranch();              
        $this->db->select('*');
        $this->db->order_by('id','desc');
        $data['mydata']=$this->db->where('branch',$branchid)->get('allonce')->result_array();
        $record=$data['mydata'];
               
       if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                
              }
              else
              {
                  echo "fales";
              }
             
   }

 public function get_record($id){
      $query=$this->db->select('*')
                     ->from('allonce')
                     ->where('id',$id)
                     ->get()->row_array();
                echo json_encode($query);
   }
   
   public function load_teacher_allownce(){
    $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('assign_allowance')
                    ->join('teacher','teacher.id=assign_allowance.bothid')
                    ->where('refrence',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
          
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
          $query2=$this->db->select('*')
                           ->from('allonce')
                           ->where('id',$value['allowance_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

 public function load_staff_allownce(){
     $branch=$this->user_model->getbranch();
    $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('assign_allowance')
                    ->join('staff','staff.id=assign_allowance.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
       echo "<td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('allonce')
                           ->where('id',$value['allowance_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

 public function teacher_allownce_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from('assign_allowance')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
         $res=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row();
                           
       echo "<tr>
            <td>$i</td>";
            // $query=$this->db->select('*')
            //               ->from($ref)
            //               ->where('id',$value['bothid'])
            //               ->get()->row()->firstname;
             echo "
             <td>$res->firstname $res->lastname</td>"; 
                     $query2=$this->db->select('*')
                           ->from('allonce')
                           ->where('id',$value['allowance_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

     public function actionallonce($ref,$id)
    {
        if($ref=="edit" && $id!=0){
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['edit_allonce']=$this->sm->id_('allonce',$id,"id");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/allonce_edit",$data);
        $this->load->view("footer");

        }else if($ref=="del" && $id!=0){
           
           $dat = array(
               'is_delete'=>1
             );
             $check=$this->sm->Update("allonce",$dat,"id",$id);
            if($check==true){
                 redirect('salary/allonce','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }
    }
    public function update_allonce()
    {

        if($this->input->post()){
            $data=$this->input->post();
             
             
             $id=$this->input->post("allonceid",true);
            $dat = array(
                'name' =>$data['alloncename'] ,
            'amount'=>$data['allonceammount'],
           'description'=>$data['des']
             );
             $check=$this->sm->Update("allonce",$dat,"id",$id);
            if($check==true){
                 redirect('salary/allonce','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }

        }
    }


    public function saveteacherallowance()
    { 
          $data=$this->input->post();
           //var_dump('<pre>',$data);
           //die();
                       $dat=array(
                    'bothid'=>$data['emp'],
                    'refrence'=>$data['type'],
                    'month'=>$data['month']
                );
               
           $query=$this->db->select('*')
                          ->from('assign_allowance')
                          ->where('bothid',$data['emp'])
                          ->where('refrence',$data['type'])
                          ->get()->result_array();
                     if(!empty($query)) 
                     {
                           $up_data=array(
                             'status'=>1
                              );
                 $this->db->where('bothid',$data['emp'])
                  ->where('refrence',$data['type'])
                  ->update('assign_allowance',$up_data); 
                         
                     }  
               if(!empty($data['allowance_id']))
                    {        
                      
            foreach ($data['allowance_id'] as $key => $value) {
                $insert=$dat;
                $insert['allowance_id']=$value;
                if($data['allowance_amount1'][$value]==0 && $data['allowance_amount2'][$value]==0)
                {
                 $this->session->set_flashdata('errormsg2','Allowance Assign is always grater than zero..');
                
                }else
                {
                if(empty($data['allowance_amount1'][$value]))
                {
                    $data['allowance_amount1'][$value]=0;
                }
                 if(empty($data['allowance_amount2'][$value]))
                {
                    $data['allowance_amount2'][$value]=0;
                }
                $insert['percentage']=$data['allowance_amount1'][$value];
                $insert['rupess']=$data['allowance_amount2'][$value];
               //$insert['month']=date('Y-m');
              
            
                    $this->db->insert('assign_allowance',$insert);
            }
            }
            }else{
             
             $this->session->set_flashdata('errormsg','Please Select Allowance Type...');
            
            }
            redirect('salary/allonce');

    }
    
  public function deduction_view()
    {
        $this->user_model->check_permissions("salary/allonce");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
           
        $data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/all_deductions",$data);
        $this->load->view("footer");
    }
   
     public function provident_fund()
    {
        $this->user_model->check_permissions("salary/allonce");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        //$data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/provident_fund",$data);
        $this->load->view("footer");
    }
    
     public function  Return_provident_fund()
    {
        $this->user_model->check_permissions("salary/allonce");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        //$data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/Return_provident_fund",$data);
        $this->load->view("footer");
    }
    
       public function  Add_PF()
    {
        $this->user_model->check_permissions("salary/allonce");
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        //$data['allownce']=$this->teacher_model->allallownce();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("salary/add_pf",$data);
        $this->load->view("footer");
    }
    
     public function alldeductions()
    {
        $branchid=$this->user_model->getbranch();
        $data['mydata']=$this->db->where('branch',$branchid)->where('is_delete',0)->get('deductions')->result_array();
          $record=$data['mydata'];

      if(!empty($record))
         {
        
     foreach ($record as $key => $value) {
         echo "
         <div style='padding-bottom:10px;' class='row'>
         <input type='hidden' value='' name='hid$value[id]' id='hid$value[id]'>

         <div class='col-md-4 col-sm-4 col-xs-2'>
 <input type='checkbox'  name='allowance_id[]'  value='$value[id]'  class='allowance_id'> $value[name] 
                          </div> 

               <div class='col-md-3 col-sm-3 col-xs-3'>
                          <input type='text' class='form-control' placeholder='IN %' onclick='Per($value[id],event)' id='allowance_per$value[id]' name='allowance_amount1[$value[id]]' >
                    </div>  

         <div class='col-md-3 col-sm-3 col-xs-3'>
                          <input type='text' class='form-control'  placeholder='IN RS'  onclick='Rup($value[id],event)' id='allowance_rup$value[id]' name='allowance_amount2[$value[id]]' >
                    </div> 

                 </div>                            
         ";
            }

         }else
         {
             echo "record not found";
         }
    }
    
    public function act_dec_deductions($t="")
    {
        $branchid=$this->user_model->getbranch();
         $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get($t)->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }else{
                    echo "record not found...";
                }
        //return $data;
    }
    
     public function save_deductions($name,$des)
    {
    $branchid=$this->user_model->getbranch();
    $myname=rawurldecode($name);
    $search=$this->db->select('*')
                     ->from('deductions')
                     ->where('name',$myname)
                     ->where('branch',$branchid)
                     ->where('is_delete',0)
                     ->get()->result_array();
               if(empty($search)) 
               {       
           $branch=$this->user_model->getbranch();
           $dat = array(
                'name' =>rawurldecode($name),
            'branch'=>rawurldecode($branch),
           'description'=>rawurldecode($des)
             );
            $check=$this->sm->create("deductions",$dat);
            if($check==true){
                $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('deductions')->result_array();
         $record=$data['mydata'];

        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }
                else{
                    echo "record not found...";
                }
                }
                
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
                }else
                {
                 echo "false";
                }

    }
    
    public function update_deductions($id,$name,$des)
{
  $data=array(
      'name'=>rawurldecode($name),
      'description'=>rawurldecode($des)
  );
 
  $query=$this->db->where('id',$id)
                  ->update('deductions',$data);
                 
  $branchid=$this->user_model->getbranch();
 $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('deductions')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }


  }
}

 public function deactive_deduction($id="")
   {
            $update_data=array(
                           'is_delete'=>1
                       );
                       $this->db->where('id',$id);
           
             $this->db->update('deductions',$update_data);
           
             $branchid=$this->user_model->getbranch();
           $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('deductions')->result_array();
         $record=$data['mydata'];
              
        if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                }
            
              else
              {
                  echo "fales";
              }
        
              
   }


public function active_deductions($id="")
   {
            $update_data=array(
                           'is_delete'=>0
                       );
                       $this->db->where('id',$id);
                      $this->db->update('deductions',$update_data);
            
        $branchid=$this->user_model->getbranch();          
        $this->db->select('*');
        $data['mydata']=$this->db->where('branch',$branchid)->get('deductions')->result_array();
         $record=$data['mydata'];
               
       if(!empty($record))
         {
            
     $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
            echo "<tr>
            <td>$i</td>
            <td>$value[name]</td>
            <td>$value[description]</td>
             <td><a href='' onclick='edit($value[id],event)'><i class='fa fa-pencil'></i></a> 
                                         |";
                if($value['is_delete']=='0'){
                    echo "
<a id='deactive' href='' onclick='deactive($value[id],event)'  style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Actived&nbsp;<span class='label label-info'>Click for Deactive</span>
          
          </a>";
                  }else{
echo "
 <a id='active' href='' onclick='active($value[id],event)'
     style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactiveted&nbsp;<span class='label label-info'>Click for Active</span>
                                    </a>   
";

                      }
echo "
                </td>
            </tr>";
     }
    
                
              }
              else
              {
                  echo "fales";
              }
             
   }
   
    public function get_record_dedctions($id){
      $query=$this->db->select('*')
                     ->from('deductions')
                     ->where('id',$id)
                     ->get()->row_array();
                echo json_encode($query);
   }
   
public function load_teacher_deductions(){
    $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('deductions_assigned')
                    ->join('teacher','teacher.id=deductions_assigned.bothid')
                    ->where('refrence',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
      
       echo "<tr>
            <td>$i</td>";
          
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('deductions')
                           ->where('id',$value['deduction_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}


public function load_teacher_provident(){
    $branch=$this->user_model->getbranch();
    $ref="teacher";
    $data['mydata']=$this->db->select('*')
                    ->from('provident_fund')
                    ->join('teacher','teacher.id=provident_fund.bothid')
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                    
        $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
        
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
          echo "
            <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             <td>$value[total]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

public function load_teacher_retpf(){
    $ref="teacher";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('return_pf')
                    ->join('teacher','teacher.id=return_pf.bothid')
                    ->where('refrence',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.is_delete',0)
                    ->where('teacher.status',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
          
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     
                    echo "
            
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             <td>$value[total_before]</td>
             <td>$value[total_after]</td>
             "; 
          echo  "</tr>";
                    }
         }  
}

public function load_staff_retpf(){
    $ref="staff";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('return_pf')
                    ->join('staff','staff.id=return_pf.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                    
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
           echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     
                    echo "
            
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             <td>$value[total_before]</td>
             <td>$value[total_after]</td>
             "; 
          echo  "</tr>";
                    }
         }  
}

public function load_teacher_addpf(){
    $ref="teacher";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('Add_PF')
                    ->join('teacher','teacher.id=Add_PF.bothid')
                    ->where('refrence',$ref)
                    ->where('teacher.branch',$branch)
                    ->where('teacher.status',0)
                    ->where('teacher.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
            echo "
            <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[amount_org]</td>"; 
          echo  "</tr>";
                    }
         }  
}

public function load_staff_addpf(){
    $ref="teacher";
    $branch=$this->user_model->getbranch();
    $data['mydata']=$this->db->select('*')
                    ->from('Add_PF')
                    ->join('staff','staff.id=Add_PF.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>";  
            echo "
            <td>$value[month]</td>
             <td>$value[amount]</td>
              <td>$value[amount_org]</td>
           
             "; 
          echo  "</tr>";
                    }
         }  
}

public function load_staff_provident(){
    $branch=$this->user_model->getbranch();
    $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('provident_fund')
                    ->join('staff','staff.id=provident_fund.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     
                    echo "
            
             <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             <td>$value[total]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

public function load_staff_deductions(){
    $branch=$this->user_model->getbranch();
    $ref="staff";
    $data['mydata']=$this->db->select('*')
                    ->from('deductions_assigned')
                    ->join('staff','staff.id=deductions_assigned.bothid')
                    ->where('refrence',$ref)
                    ->where('staff.branch',$branch)
                    ->where('staff.status',0)
                    ->where('staff.is_delete',0)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
             echo "
             <td>$value[firstname] $value[lastname]</td>"; 
                     $query2=$this->db->select('*')
                           ->from('deductions')
                           ->where('id',$value['deduction_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
}

public function teacher_deduction_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from('deductions_assigned')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                     $query2=$this->db->select('*')
                           ->from('deductions')
                           ->where('id',$value['deduction_id'])
                           ->get()->row()->name;  
                    echo "
             <td>$query2</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

public function teacher_provident_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from('provident_fund')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                 
                    echo "
            
             <td>$value[month]</td>
             <td>$value[percentage]</td>
             <td>$value[rupess]</td>
              <td>$value[total]</td>
             "; 
              if($value['status']=='0'){
                  echo "
                     <td>Active</td>
                  ";
              } 
              else{
                  echo "
                     <td>Previous</td>
                  ";
              }         
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

public function teacher_retpf_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from(' return_pf')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                 
                    echo "
            
             <td>$value[month]</td>
             <td>$value[rupess]</td>
             <td>$value[total_before]</td>
              <td>$value[total_after]</td>
             "; 
                    
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

public function teacher_addpf_selected($id,$ref){
    $data['mydata']=$this->db->select('*')
                    ->from('Add_PF')
                    ->where('bothid',$id)
                    ->where('refrence',$ref)
                    ->get()->result_array();
                $record=$data['mydata'];  
     if(!empty($record))
         {
              $i=0; 
     foreach ($record as $key => $value) {
         ++$i;
       echo "<tr>
            <td>$i</td>";
            $query=$this->db->select('*')
                           ->from($ref)
                           ->where('id',$value['bothid'])
                           ->get()->row()->firstname;
             echo "
             <td>$query</td>"; 
                 
                    echo "
            
             <td>$value[month]</td>
             <td>$value[amount]</td>
             <td>$value[amount_org]</td>
             
             "; 
                    
          echo  "</tr>";
                    }
         }  
         else
         {
             echo " ";
         }
}

public function saveteacherdeductions()
    { 

        
            $data=$this->input->post();
            $mnth=$data['month'];

                $dat=array(
                    'bothid'=>$data['emp'],
                    'refrence'=>$data['type'],
                     'month'=>$mnth
                );
               
           $query=$this->db->select('*')
                          ->from('deductions_assigned')
                          ->where('bothid',$data['emp'])
                          ->where('refrence',$data['type'])
                          ->get()->result_array();
                     if(!empty($query)) 
                     {
                           $up_data=array(
                             'status'=>1
                              );
                 $this->db->where('bothid',$data['emp'])
                  ->where('refrence',$data['type'])
                  ->update('deductions_assigned',$up_data); 
                         
                     } 
                     if(!empty($data['allowance_id']))
                    {  
                      
            foreach ($data['allowance_id'] as $key => $value) {
                $insert=$dat;
                $insert['deduction_id']=$value;
                 if($data['allowance_amount1'][$value]==0 && $data['allowance_amount2'][$value]==0)
                {
                 $this->session->set_flashdata('errormsg2ded','Deduction Assign is always grater than zero..');
                
                }else
                {
                if(empty($data['allowance_amount1'][$value]))
                {
                    $data['allowance_amount1'][$value]=0;
                }
                 if(empty($data['allowance_amount2'][$value]))
                {
                    $data['allowance_amount2'][$value]=0;
                }
                $insert['percentage']=$data['allowance_amount1'][$value];
                $insert['rupess']=$data['allowance_amount2'][$value];
               //$insert['month']=date('Y-m');
              
            
                    $this->db->insert('deductions_assigned',$insert);
            }
            }
             }else{
             
             $this->session->set_flashdata('errormsgded','Please Select Deduction Type...');
            
            }
            redirect('salary/deduction_view');

    }
    
    public function saveprovident()
    { 
 $data=$this->input->post();
 
 $salery=$this->input->post('basic_salary');
 
            $dat=array(
                    'bothid'=>$data['emp'],
                    'refrence'=>$data['type'],
                     'month'=>$data['month']
                );
         $query=$this->db->select('*')
                          ->from('provident_fund')
                          ->where('bothid',$data['emp'])
                          ->where('refrence',$data['type'])
                          ->get()->result_array();
                     if(!empty($query)) 
                     {
                           $up_data=array(
                             'status'=>1
                              );
                 $this->db->where('bothid',$data['emp'])
                  ->where('refrence',$data['type'])
                  ->update('provident_fund',$up_data); 
                         
                     } 
               $insert=$dat;
               if(empty($data['allowance_amount1']))
                {
                    $data['allowance_amount1']=0;
                }
                 if(empty($data['allowance_amount2']))
                {
                    $data['allowance_amount2']=0;
                }
                
                 if(empty($data['allowance_amount11']))
                {
                    $data['allowance_amount11']=0;
                }
                 if(empty($data['allowance_amount22']))
                {
                    $data['allowance_amount22']=0;
                }
                $insert['percentage']=$data['allowance_amount1'];
                $insert['rupess']=$data['allowance_amount2'];
                $insert['org_percentage']=$data['allowance_amount11'];
                $insert['org_rupess']=$data['allowance_amount22'];
                if($data['allowance_amount1']==0)
                {
                    $emp_amount=$data['allowance_amount2'];  
                }else
                {
                    $rato=$salery/100;
                    $emp_amount=$rato * $data['allowance_amount1'];
                }
                
                if($data['allowance_amount11']==0)
                {
                    $org_amount=$data['allowance_amount22'];  
                }else
                {
                    $rato=$salery/100;
                    $org_amount=$rato * $data['allowance_amount11'];
                }
              $net=$emp_amount+$org_amount;
              $insert['	total']=$net;
                 $this->db->insert('provident_fund',$insert);
         
           redirect('salary/provident_fund');
      }
    
    public function Assign()
    {
        $this->user_model->check_permissions("salary/Assign");
        $data=$this->sm->up();
        $data['allonce']=$this->sm->all("allonce");
       $this->sm->down("salary/assignallonce",$data);
    }
    public function saveasignalloance()
    {
        if($this->input->post()){
            $data=$this->input->post();
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                $column = array('bothid' =>$v['employee']['id'] ,
                            'refrence'=>$v['employee']['type'],
                            'allowance_id'=>$v['allowance']['id'],
                            'amount'=>$v['allowance']['amount'],
                            'month'=>$v['allowance']['month']
                            );
                   $check= $this->sm->create("assign_allowance",$column);  
            }
        }
        if($check==true){
            redirect("salary/Assign","refresh");
        }else{echo "<h1>ni howa :'(</h1>";}
        }
    }
    public function Teacher_Allowance()
    {
        $this->user_model->check_permissions("Salary/Teacher_Allowance");
        $limit=10;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->teacher_data($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count('assign_allowance','teacher');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/Teacher_Allowance/");
        $this->sm->down("salary/teacher_allowance_view",$data);
    }
    public function Staff_Allowance()
    {   
        $this->user_model->check_permissions("Salary/Staff_Allowance");
        $limit=10;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['offset']=$offset;
        $count=$this->sm->count('assign_allowance','staff');
        $data['employee']=$this->sm->staff_data($limit,$offset);
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/staff_Allowance/");
        $this->sm->down("salary/staff_alloance_view",$data);
    }
    public function Action_Allonce($ref="",$id=0)
    {
        if($ref=="edit"){
            $data=$this->sm->up();
            $data['allonce']=$this->sm->all("allonce");
            $data['edit']=$this->sm->id_('assign_allowance',$id,"id");
            $this->sm->down("salary/edit_allowance",$data);
        }else if($ref=="del"){
                 $dat = array(
               'is_delete'=>1
             );
             $check=$this->sm->Update("assign_allowance",$dat,"id",$id);
            if($check==true){
                 redirect('salary/assign','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }else{echo "galat tareqa";}
    }
    public function Update_assign()
    { 
        if($this->input->post()){
            $data=$this->input->post();
            $id=$this->input->post('id',true);
            $column = array('bothid' =>$data['empid'] ,
                            'refrence'=>$data['type'],
                            'allowance_id'=>$data['allonce'],
                            'amount'=>$data['amount'],
                            'month'=>$data['month']
                            );
                $this->sm->update("assign_allowance",$column,'id',$id);  
        }
        if($data['type']=="teacher"){
            redirect("salary/Teacher_Allowance","refresh");
        }else{redirect("salary/Staff_Allowance","refresh");}

        
       
    }

    //++++++++++++++++++++++++++++++++++++Deduction Salary++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   public function Deduction()
    {
        $this->user_model->check_permissions("salary/Deduction");
        $data=$this->sm->up();
         
       $this->sm->down("salary/deduction",$data);
    }
    public function savededuction()
    {
         if($this->input->post()){
            $data=$this->input->post();
            

           $this->sm->savededuction($data);
           redirect("salary/teacher_deduct_view","refresh");
//            die;
//        if($check==true && $data['type']=="teacher"){
//            
//            redirect("salary/teacher_deduct_view","refresh");
//        }else if($check==true && $data['type']=="staff"){redirect("salary/staff_deduct_view","refresh");}else{echo "<h1>ni howa :'(</h1>";}
//        }
         }
    }
    public function teacher_deduct_view()
    {
        $this->user_model->check_permissions("salary/teacher_deduct_view");
        $limit=10;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->teacher_deduction($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count('deduction','teacher');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/teacher_deduct_view/");
        $this->sm->down("salary/teacher_deduct_view",$data);
    }
    public function staff_deduct_view()
    {
         $this->user_model->check_permissions("salary/staff_deduct_view");
        $limit=10;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->staff_deduction($limit,$offset);
        
        $data['offset']=$offset;
        $count=$this->sm->count('deduction','staff');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/staff_deduct_view/");
        $this->sm->down("salary/staff_deduct_view",$data);
    }
    public function Action_Deduction($ref="",$id=0)
    {
        if($ref=="edit"){
            $data=$this->sm->up();
            $data['edit']=$this->sm->id_('deduction',$id,"id");
            $this->sm->down("salary/deduct_edit",$data);
        }else if($ref=="del"){
                 $dat = array(
               'is_delete'=>1
             );
             $check=$this->sm->Update("deduction",$dat,"id",$id);
            if($check==true){
                 redirect('salary/Deduction','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }else{echo "galat tareqa";}
    }
    public function Update_deduction()
    { 
        if($this->input->post()){
            $data=$this->input->post();
            
            $id=$this->input->post('id',true);
            $column = array('bothid' =>$data['empid'] ,
                            'refrence'=>$data['type'],
                            'reason'=>$data['reason'],
                            'amount'=>$data['amount'],
                            'month'=>$data['month']
                            );
                $this->sm->update("deduction",$column,'id',$id);
        }
        if($data['type']=="teacher"){
            redirect("salary/teacher_deduct_view","refresh");
        }else{redirect("salary/staff_deduct_view","refresh");}
    }
    //++++++++++++++++++++++++++++++++++++Security Cut++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     public function Security()
    {
        $this->user_model->check_permissions("salary/Security");
        $data=$this->sm->up();
       $this->sm->down("salary/Security",$data);
    } 
    public function savesecurity()
    {
         if($this->input->post()){
            $data=$this->input->post();
          
            $ref;
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                $ref=$v['employee']['type'];
                $column = array('bothid' =>$v['employee']['id'] ,
                            'refrence'=>$v['employee']['type'],
                            'security_amount'=>$v['security']['amount'],
                            'detuct_amount'=>$v['security']['amountcut'],
                            'remendar_amount'=>$v['security']['remandar'],
                            'month'=>$v['security']['month']
                            );
                   $check= $this->sm->create("security_deduct",$column);  
            }
        }
        if($check==true && $ref=="teacher"){
            
            redirect("salary/Security","refresh");
            
            
        }else if($check==true && $ref=="staff"){redirect("salary/staff_deduct_view","refresh");
        }else{echo "<h1>ni howa :'(</h1>";}
        }
    }
    public function Teacher_security_View()
    {
        $this->user_model->check_permissions("salary/Teacher_security_View");
         $limit=20;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->teacher_security_view($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count('security_deduct','teacher');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/Teacher_security_View/");
        $this->sm->down("salary/Teacher_security_View",$data);
    }
    public function Staff_security_View()
    {
        $this->user_model->check_permissions("salary/Staff_security_View");
         $limit=10;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->staff_security_view($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count('security_deduct','staff');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/Staff_security_View/");
        $this->sm->down("salary/Staff_security_View",$data);
    }
    public function Action_security($ref="",$id=0)
    {
        if($ref=="edit"){
            $data=$this->sm->up();
            $data['edit']=$this->sm->id_('security_deduct',$id,"id");
            $this->sm->down("salary/security_edit",$data);
        }else if($ref=="del"){
                 $dat = array(
               'is_delete'=>1
             );
             $check=$this->sm->Update("security_deduct",$dat,"id",$id);
            if($check==true){
                 redirect('salary/security','refresh');
                }
                else{
                    echo "<h1>ni howa hay :'(</h1>";
                }
        }else{echo "galat tareqa";}
    }
    public function Update_security($value='')
    {
       if($this->input->post()){
            $data=$this->input->post();
            
            $id=$this->input->post('id',true);
            $column = array('bothid' =>$data['empid'] ,
                            'refrence'=>$data['type'],
                            'security_amount'=>$data['amount'],
                            'detuct_amount'=>$data['amountcut'],
                            'remendar_amount'=>$data['rem'],
                            'month'=>$data['month']
                            );
                $this->sm->update("security_deduct",$column,'id',$id);
       }
        if($data['type']=="teacher"){
            redirect("salary/Teacher_security_View","refresh");
        }else{redirect("salary/staff_security_View","refresh");}
       
    }
//++++++++++++++++++++++++++++++++++++Create Salary++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    public function creat_salry()
    {
        $this->user_model->check_permissions("salary/creat_salry");
        $data=$this->sm->up();
       $this->sm->down("salary/create_sal",$data);
    }
    
    
     public function NextPage()
    {   
        if($this->input->post()){
        $mydata=$this->input->post();
        //var_dump($mydata);
       // die();
        //Add for pagenation
        $type=$mydata['type'];
        
        $month=$mydata['month'];
         
       
        $data=$this->sm->up();
        
        $field=$this->input->post();
        
        
       //$both=$field['bothid'];
      
        $d=$this->sm->get('salary',$month,$type);

        $date=date('Y-m');

        $current= date_parse($date);
         $sel=date_parse($month);

        //var_dump(($sel['year'] <=$current['year']) && ($sel['month'] <=$current['month']) || (count($d) != 0) );die();
     
if($month<=$date|| (count($d) != 0) ){
        $data['type']=$type;
        $data['typ']=$type;
        $data['month']=$month;
         $data['mnth_sl']=$month;
        $data['allonce']=$this->sm->all('allonce');
       
     
        $data['employee']=$this->sm->all_emp_monthly($type,$month);
        // var_dump('<pre>',$data['employee']);
        // die();
        $data['assignallonce']=$this->sm->allallonce($type);
       $data['increment']=$this->sm->increment_all($type,$month);
        $data['deduction']=$this->sm->deduction_all($type,$month);
         
       // $data['deduction_def']=$this->sm->all('deduction_def');
       $data['deduction_def']=$this->sm->all('deductions');
    //     var_dump('<pre>',$data['deduction_def']);
    //   die();
       $data['loan_def']=$this->sm->all('loan_def');
      
       $data['personal_exp']=$this->sm->all('personal_expense');
      // $data['days']=$this->sm->all('advance');
    
        //var_dump('<pre>',$data['advnce']);
        //die();
   
        $this->sm->down("salary/Next_salary",$data);
        }else{
           $this->sm->down("salary/back",$data);
        
            }
        }
        else{
       
        echo "<h1>could not Permision Go Back</h1>";
          
        $this->sm->down("salary/back",$data);
      
        }
        


    }
    
    
    
    
    public function save_salary($value='')
    {
        $data=$this->input->post();
        $uid= $this->user_model->userInfo("id")['id'];
        // $branch=$this->user_model->getbranch();
        
        $type=$data['type'];
        $month=$data['month'];
        $date=date('Y,m,d');
        if(empty($data['empid']))
        {
            $this->session->set_flashdata('errmsg2','Please select employee for create salary');
             redirect('Salary/creat_salry');
        }
       
        foreach ($data['empid'] as $key => $value) {
            
            if($type == 'teacher'){
                $branch = @$this->db->query("SELECT `branch` FROM `teacher` WHERE `id`='$value' AND `is_delete`='0'")->result_array()[0]['branch'];
            }
            else{
                $branch = @$this->db->query("SELECT `branch` FROM `staff` WHERE `id`='$value' AND `is_delete`='0'")->result_array()[0]['branch'];
                
            }
            
            $field = array('bothid' =>$value ,
                            'refrence'=>$type,
                            'branch'=>$branch,
                            'total_amount'=>$data['total'][$value],
                            'month'=>$month,                            
                            'user_id'=>$uid,
                             'create_date'=>$date
                             );
                   //check existing date_add
                   $query_salary=$this->db->select('*')
                                          ->from('salary')  
                                          ->where('bothid',$value)  
                                          ->where('refrence',$type) 
                                          ->where('branch',$branch) 
                                          ->where('is_paid',0) 
                                          ->where('month',$month) 
                                          ->where('is_delete',0) 
                                          ->get()->result_array();
                       if(!empty($query_salary))
                       {
                           $data_up=array(
                            'is_delete'=>1
                           );
                           $this->db->where('bothid',$value) 
                                    ->where('refrence',$type) 
                                    ->where('branch',$branch)
                                     ->where('is_paid',0) 
                                     ->where('month',$month)
                                     ->update('salary',$data_up);
                                     $check=$this->sm->create('salary',$field); 
                       }else
                       {
                       $check=$this->sm->create('salary',$field); 
                       }
                // $check=$this->sm->create('salary',$field);            
                   }
                   if($check==true && $type=="teacher"){
                       redirect('salary/view_salary','refresh');
                   }else{redirect('salary/view_salary','refresh');}

    }
    public function Teachersalary_view()
    {
        $this->user_model->check_permissions("salary/Teachersalary_view");
        $limit=200;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->Teacher_salary($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count_salery('salary','teacher');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/Teachersalary_view/");
        $this->sm->down("salary/teacher_salary_view",$data);
    }
    public function staff_salary_view()
    {
        $this->user_model->check_permissions("salary/staff_salary_view");
         $limit=200;
        $offset=$this->uri->segment(3)?$this->uri->segment(3): 0;
        $data=$this->sm->up();
        $data['employee']=$this->sm->staff_salary($limit,$offset);
        $data['offset']=$offset;
        $count=$this->sm->count_salery('salary','staff');
        $data['page_links']=$this->sm->dopaginate($count,$limit,3,"salary/staff_salary_view/");
        $this->sm->down("salary/staff_salary_view",$data);
    }
   
    public function teacher_sal_search($v='')
    {
        $re=$this->sm->Teacher_salary1($v);
        echo json_encode($re);
    }
    public function staff_sal_search($v='')
    {
        $re=$this->sm->staff_salary1($v);
        echo json_encode($re);
    }
      public function Action_salary($id=0)
    {
        $data=$this->sm->up();
        $ret=$this->sm->id_('salary',$id,'id');
        $branch=$this->user_model->getbranch();
        $data['branch']=$this->sm->id_('branch',$branch,'id');
        $ref=$ret['refrence'];
        $data['ttyp']=$ref;
        $data['stat']=$ret['is_paid'];
        //var_dump($data['stat']);
       // die();
        $data['create_date']=$ret['create_date'];
        $month=$ret['month'];
        $data['sal_id']=$id;
        $bothid=$ret['bothid'];
        $data['bothid']=$bothid;
        $data['month']=$month;
        $data['refrence']=$ret['refrence'];
        $data['invoice']=$ret['id'];
       // var_dump('<pre>',$data['invoice']);
       // die();
       $data['employee']=$this->sm->emp_id_actdec($ref,$bothid);
       // $data['employee']=$this->sm->emp_id($ref,$bothid);
         $data['increment']=$this->sm->increment_all_id($ref,$month,$bothid);
        $data['allonce']=$this->sm->all('allonce');
        $data['assignallonce']=$this->sm->allallonce($ref);
       // $data['deduction_def']=$this->sm->all('deduction_def');
        //Add new
        $data['loan_def']=$this->sm->all('loan_def');
      
        //$data['deduction']=$this->sm->deduction_all($type,$month);
       $data['deduction_def']=$this->sm->all('deductions');

       $data['personal_exp']=$this->sm->all('personal_expense');
       //End
        $this->sm->down("salary/history",$data);
    }
     public function view_salary()
    {
       $this->user_model->check_permissions("salary/view_salary");
       $data=$this->sm->up();
       $this->sm->down("salary/view",$data);
    }    
    
    public function my_view_salary(){
       $this->user_model->check_permissions("salary/my_view_salary");
       $data=$this->sm->up();
       $this->sm->down("salary/my_view_salary",$data);
    }
    public function salary_view()
    {
       if($this->input->post()){
        $data=$this->input->post();
        $data=$this->sm->up();
        $field=$this->input->post();
        $type=$field['type'];
        $month=$field['month'];
        $d=$this->sm->get('salary',$month,$type);
        $date=date('Y-m');
        $current= date_parse($date);
        $sel=date_parse($month);
        $data['type']=$type;
        $data['month']=$month;
        $data['employee']=$this->sm->salary_emp_actdec($type,$month);
       // $data['employee']=$this->sm->salary_emp($type,$month);
        if(!empty($data['employee']))
        {
        $data['allonce']=$this->sm->all('allonce');
        $data['assignallonce']=$this->sm->allallonce($type);
        $data['deduction']=$this->sm->deduction_all($type,$month);
        
        $data['increment']=$this->sm->increment_all($type,$month);

        $data['loan_def']=$this->sm->all('loan_def');
        $data['deduction_def']=$this->sm->all('deductions');
        // var_dump('<pre>',$data['deduction_def']);
        // die();
        $data['personal_exp']=$this->sm->all('personal_expense');
        $this->sm->down("salary/salary_view",$data);
            
        }else
        {
               $this->session->set_flashdata('error_msg','Salary Not Created For This Month..');
     
                redirect('salary/view_salary');

        }
      
       }
       }
    public function my_salary_view(){
       if($this->input->post()){
            $data=$this->input->post();
            $data=$this->sm->up();
            $field=$this->input->post();
            $type=$field['type'];
            $month=$field['month'];
            $d=$this->sm->get('salary',$month,$type);
            $date=date('Y-m');
            $current= date_parse($date);
            $sel=date_parse($month);
            $data['type']=$type;
            $data['month']=$month;
            $data['employee']=$this->sm->salary_emp_actdec($type,$month, true);
           // $data['employee']=$this->sm->salary_emp($type,$month);
            if(!empty($data['employee']))
            {
                $data['allonce']=$this->sm->all('allonce');
                $data['assignallonce']=$this->sm->allallonce($type);
                $data['deduction']=$this->sm->deduction_all($type,$month);
                
                $data['increment']=$this->sm->increment_all($type,$month);
        
                $data['loan_def']=$this->sm->all('loan_def');
                $data['deduction_def']=$this->sm->all('deductions');
                // var_dump('<pre>',$data['deduction_def']);
                // die();
                $data['personal_exp']=$this->sm->all('personal_expense');
                $this->sm->down("salary/my_salary_view",$data);
                
            }
            else{
                   $this->session->set_flashdata('error_msg','Salary Not Created For This Month..');
         
                    redirect('salary/my_view_salary');
    
            }
      
       }
    }
       

       
        public function paid_emp()
       {
          
           $employees_pay=$this->input->post('employees_pay');
           $type=$this->input->post('type');
           $month=$this->input->post('month');
          
           $branch=$this->user_model->getbranch();
	          if(!empty($employees_pay))
	          {
	              foreach($employees_pay as $ab)
	              {
	             $id=$ab;
	             $checker=$this->db->select('*')
	                               ->from('salary')
	                               ->where('id',$id)
	                               ->where('is_paid',0)
	                               ->where('is_delete',0)
	                               ->get()->row_array();
	           if(!empty($checker))
	           {
	               $this->sm->update_salary($id);
                  $get_loan=$this->session->userdata('paid_loan');
                  $get_data=$this->db->select('*')
                                      ->from('salary')
                                      ->where('id',$id)
                                      ->get()->row_array();
                                $bothid=$get_data['bothid'];  
                                $month=$get_data['month'];
                                $type=$ref=$get_data['refrence']; 
                   
                    //UPDATE TABLE
                        $get_data_up=$this->db->select('*')
                                      ->from('loan_assign')
                                      ->where('type',$ref)
                                      ->where('bothid',$bothid)
                                      ->where('status',0)
                                      ->where('is_delete',0)
                                       ->get()->row_array();
                        $my_id=$get_data_up['id'];
                        $paid=$get_data_up['total_paid'];
                        $amount=$get_data_up['amount'];
                        $total_paid=$paid+$get_loan;
                        if($amount==$total_paid){
                         $up_dat=array(
                         'total_paid'=>$total_paid,
                         'status'=>1
                         );
                         $this->db->where('id',$my_id)
                              ->update('loan_assign',$up_dat);
                        }else
                        {
                          $up_date=array(
                         'total_paid'=>$total_paid
                         
                         );
                         $this->db->where('id',$my_id)
                              ->update('loan_assign',$up_date);
                        }
	           }
             }
	                //Posted On Function
	                
	                    $data=$this->sm->up();
                        
                      
                       
                        $d=$this->sm->get('salary',$month,$type);
                        $date=date('Y-m');
                        $current= date_parse($date);
                        $sel=date_parse($month);
                        $data['type']=$type;
                        $data['month']=$month;
                        $data['employee']=$this->sm->salary_emp_actdec($type,$month);
                        
                        if(!empty($data['employee']))
                        {
                        $data['allonce']=$this->sm->all('allonce');
                        $data['assignallonce']=$this->sm->allallonce($type);
                        $data['deduction']=$this->sm->deduction_all($type,$month);
                        $data['increment']=$this->sm->increment_all($type,$month);
                
                        $data['loan_def']=$this->sm->all('loan_def');
                        $data['deduction_def']=$this->sm->all('deductions');
                        $data['personal_exp']=$this->sm->all('personal_expense');
                         $this->session->set_flashdata('suc_msg',"Salary release successfully....");
                         $this->session->set_flashdata('err_msg');
                        $this->sm->down("salary/salary_view",$data);
                        }else
                        {
                               $this->session->set_flashdata('error_msg','Salary Not Created For This Month..');
                     
                                redirect('salary/view_salary');
                
                        }
	          }else
	          {
	              //Posted On Function
	                
	                    $data=$this->sm->up();
                        
                      
                       
                        $d=$this->sm->get('salary',$month,$type);
                        $date=date('Y-m');
                        $current= date_parse($date);
                        $sel=date_parse($month);
                        $data['type']=$type;
                        $data['month']=$month;
                        $data['employee']=$this->sm->salary_emp_actdec($type,$month);
                        
                        if(!empty($data['employee']))
                        {
                        $data['allonce']=$this->sm->all('allonce');
                        $data['assignallonce']=$this->sm->allallonce($type);
                        $data['deduction']=$this->sm->deduction_all($type,$month);
                        $data['increment']=$this->sm->increment_all($type,$month);
                
                        $data['loan_def']=$this->sm->all('loan_def');
                        $data['deduction_def']=$this->sm->all('deductions');
                        $data['personal_exp']=$this->sm->all('personal_expense');
                         $this->session->set_flashdata('err_msg',"Please Select Employee....");
                         $this->session->set_flashdata('suc_msg');
                        $this->sm->down("salary/salary_view",$data);
                        }else
                        {
                               $this->session->set_flashdata('error_msg','Salary Not Created For This Month..');
                     
                                redirect('salary/view_salary');
                
                        }
	          }
          }
          
   
       
  public function is_paid($id=0)
       {
           $id=$this->input->post('id');
           $branch=$this->user_model->getbranch();
           
           $checker=$this->db->select('*')
	                               ->from('salary')
	                               ->where('id',$id)
	                               ->where('is_paid',0)
	                               ->where('is_delete',0)
	                               ->get()->row_array();
	          if(!empty($checker))
	          {
	              $this->sm->update_salary($id);
                        $get_loan=$this->session->userdata('paid_loan');
                        $get_data=$this->db->select('*')
                                      ->from('salary')
                                      ->where('id',$id)
                                      ->get()->row_array();
                                $bothid=$get_data['bothid'];  
                                $ref=$get_data['refrence'];     
                             
                        //UPDATE TABLE
                        $get_data_up=$this->db->select('*')
                                      ->from('loan_assign')
                                      ->where('type',$ref)
                                      ->where('bothid',$bothid)
                                      ->where('status',0)
                                      ->where('is_delete',0)
                                       ->get()->row_array();
                        $my_id=$get_data_up['id'];
                        $paid=$get_data_up['total_paid'];
                        $amount=$get_data_up['amount'];
                        $total_paid=$paid+$get_loan;
                        if($amount==$total_paid){
                         $up_dat=array(
                         'total_paid'=>$total_paid,
                         'status'=>1
                         );
                         $this->db->where('id',$my_id)
                              ->update('loan_assign',$up_dat);
                        }else
                        {
                          $up_date=array(
                         'total_paid'=>$total_paid
                         
                         );
                         $this->db->where('id',$my_id)
                              ->update('loan_assign',$up_date);
                        }
                        $submitted_at=date('Y-m-d');
	          }
                      
                        
                        $this->session->set_flashdata('succ',"Salary release successfully....");
                        redirect("salary/Action_salary/".$id,"refresh"); 
        
       
       }
       
       	public function update_account($submitted_at="",$fee_deposit_type="")
	{
	   $branch=$this->user_model->getbranch();
	    if(!empty($submitted_at) && !empty($fee_deposit_type))
	    {
	        $sum_amount=$this->db->select('sum(total_amount) as total_amount')
	                             ->from('salary')
	                             ->where('paid_type',$fee_deposit_type)
	                             ->where('is_paid_date',$submitted_at)
	                             ->where('is_paid',1)
	                             ->where('branch',$branch)
	                             ->get()->row_array()['total_amount'];
	   
	           if($fee_deposit_type=="cash")
	           {
	           
	               $check_voucher=$this->db->select('*')
	                                       ->from('transction')
	                                       ->where('head',6)
	                                       ->where('salary_voucher',1)
	                                       ->where('date',$submitted_at)
	                                       ->where('has_cr',0)
	                                       ->where('branch',$branch)
	                                       ->get()->row_array();
	               if(empty($check_voucher)) 
	               {
	                   $curdate= date('Y-m-d H:i:s');
	                   $branch=$this->user_model->getbranch();
	                   $t = microtime(true);
                       $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                       $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                       $trans_id=$date->getTimestamp();
                       $id=$this->user_model->userInfo('id')['id'];
                       $type="cash payment";
                        $name=$this->db->query("SELECT voucher_no FROM `transction` where type='$type' and branch=$branch order by id desc limit 1 ")->row_array()['voucher_no']; 
                        if($name==null)
                        {
                            $name=1;
                        } 
                        else{
                            $name=$name;
                        preg_match_all('!\d+!', $name, $matches);
                        $name=$matches[0][0];
                        $name++;
                        }
                        $voucher_no="CP".$name;
                    
                       $hard_fir=array(
                        'date'=>$submitted_at,
                        'created_date'=>$curdate,
                        'type'=>"cash payment",
                        'head' =>6,
                        'head_name' =>"Cash in hand",
                        'narration' =>"Salaries Paid ".$submitted_at,
                        'program' =>0,
                        'project' =>0,
                        'program_name' =>"Education",
                        'project_name' =>"School",
                        'amount' =>$sum_amount,
                        'created_by'=>$id ,
                        'paid_from'=>"Salary",
                        'branch' =>$branch,
                        'balance' =>0,
                        'after_transction'=>$sum_amount,
                        'transction_id'=>$trans_id,
                        'voucher_no'=>$voucher_no,
                        'has_cr'=>0,
                        'salary_voucher'=>1
                         );
                  
                        $this->db->insert('transction',$hard_fir);
                        
                        //Second Effect
                         $hard_sec=array(
                        'date'=>$submitted_at,
                        'created_date'=>$curdate,
                        'type'=>"cash payment",
                        'head' =>513,
                        'head_name' =>"Staff Salries",
                        'narration' =>"Salaries Paid ".$submitted_at,
                        'program' =>0,
                        'project' =>0,
                        'program_name' =>"Education",
                        'project_name' =>"School",
                        'amount' =>$sum_amount,
                        'created_by'=>$id ,
                        'paid_from'=>"Salary",
                        'branch' =>$branch,
                        'balance' =>0,
                        'after_transction'=>$sum_amount,
                        'transction_id'=>$trans_id,
                        'voucher_no'=>$voucher_no,
                        'has_cr'=>1,
                        'salary_voucher'=>1
                         );
                         $this->db->insert('transction',$hard_sec);
	               }else
	               {
	                   //Update Record
	                   $voucher_no=$check_voucher['voucher_no'];
	                   
	                   $up_data=array(
	                       'amount'=>$sum_amount
	                       );
	                       $this->db->where('head',6)
	                                ->where('voucher_no',$voucher_no)
	                                ->where('date',$submitted_at)
	                                ->where('salary_voucher',1)
	                                ->where('branch',$branch)
	                                ->update('transction',$up_data);
	                      
	                       $this->db->where('head',513)
	                                ->where('voucher_no',$voucher_no)
	                                ->where('date',$submitted_at)
	                                ->where('salary_voucher',1)
	                                ->where('branch',$branch)
	                                ->update('transction',$up_data);             
	               }
	           }else
	           {
	               //Banks
	               $branch=$this->user_model->getbranch();
	               $primary_bank=$this->db->select('*')
	                                       ->from('bank_def')
	                                       ->where('branch',$branch)
	                                       ->where('salary_primary',1)
	                                       ->where('is_active',1)
	                                       ->get()->row_array();
	                       if(!empty($primary_bank))
	                       {
	                        $account_bank=$this->db->select('*')
        	                                       ->from('account_heads')
        	                                       ->where('name',$primary_bank['b_name'])
        	                                       ->where('parent',5)
        	                                       ->where('has_child',0)
        	                                       ->get()->row_array();
        	                      if(!empty($account_bank)) 
        	                      {
        	                          $check_voucher=$this->db->select('*')
                    	                                       ->from('transction')
                    	                                       ->where('head',$account_bank['id'])
                    	                                       ->where('salary_voucher',1)
                    	                                       ->where('date',$submitted_at)
                    	                                       ->where('has_cr',0)
                    	                                       ->where('branch',$branch)
                    	                                       ->get()->row_array();  
                    	                   
                    	               if(empty($check_voucher)) 
                    	                   {
                    	                          $curdate= date('Y-m-d H:i:s');
                            	                   $branch=$this->user_model->getbranch();
                            	                   $t = microtime(true);
                                                   $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                                                   $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                                                   $trans_id=$date->getTimestamp();
                                                   $id=$this->user_model->userInfo('id')['id'];
                                                   $type="bank payment";
                                                    $num=$this->db->query("SELECT voucher_no FROM `transction` where type='$type' and branch=$branch order by id desc limit 1 ")->row_array()['voucher_no']; 
                                                    if($num==null)
                                                    {
                                                        $num=1;
                                                    } 
                                                    else{
                                                    preg_match_all('!\d+!', $num, $matches);
                                                    $num=$matches[0][0];
                                                    $num++;
                                                    }
                                                    $voucher_no="BP".$num;
                    	                       
                    	                       $bnk_fir=array(
                                                    'date'=>$submitted_at,
                                                    'created_date'=>$curdate,
                                                    'type'=>"bank payment",
                                                    'head' =>$account_bank['id'],
                                                    'head_name' =>$account_bank['name'],
                                                    'narration' =>"Salaries Paid ".$submitted_at,
                                                    'program' =>0,
                                                    'project' =>0,
                                                    'program_name' =>"Education",
                                                    'project_name' =>"School",
                                                    'amount' =>$sum_amount,
                                                    'created_by'=>$id ,
                                                    'paid_from'=>"Salary",
                                                    'branch' =>$branch,
                                                    'balance' =>0,
                                                    'after_transction'=>0,
                                                    'transction_id'=>$trans_id,
                                                    'voucher_no'=>$voucher_no,
                                                    'chq'=>0,
                                                    'has_cr'=>0,
                                                    'salary_voucher'=>1
                                                    
                                                     );
                                             
                                                  $this->db->insert('transction',$bnk_fir); 
                                                  
                                                  $bnk_sec=array(
                                                    'date'=>$submitted_at,
                                                    'created_date'=>$curdate,
                                                    'type'=>"bank payment",
                                                    'head' =>513,
                                                    'head_name' =>"Staff Salries",
                                                    'narration' =>"Salary Paid ".$submitted_at,
                                                    'program' =>0,
                                                    'project' =>0,
                                                    'program_name' =>"Education",
                                                    'project_name' =>"School",
                                                    'amount' =>$sum_amount,
                                                    'created_by'=>$id ,
                                                    'paid_from'=>"Salary",
                                                    'branch' =>$branch,
                                                    'balance' =>0,
                                                    'after_transction'=>0,
                                                    'transction_id'=>$trans_id,
                                                    'voucher_no'=>$voucher_no,
                                                    'chq'=>0,
                                                    'has_cr'=>1,
                                                    'salary_voucher'=>1
                                                    
                                                     );
                                             
                                                  $this->db->insert('transction',$bnk_sec); 
                    	                   }else
                    	                   {
                    	                       //Update Record
                    	                        $voucher_no=$check_voucher['voucher_no'];
	                   
                        	                   $up_data=array(
                        	                       'amount'=>$sum_amount
                        	                       );
                        	                       $this->db->where('head',$account_bank['id'])
                        	                                ->where('voucher_no',$voucher_no)
                        	                                ->where('date',$submitted_at)
                        	                                ->where('salary_voucher',1)
                        	                                ->where('branch',$branch)
                        	                                ->update('transction',$up_data);
                        	                      
                        	                       $this->db->where('head',513)
                        	                                ->where('voucher_no',$voucher_no)
                        	                                ->where('date',$submitted_at)
                        	                                ->where('salary_voucher',1)
                        	                                ->where('branch',$branch)
                        	                                ->update('transction',$up_data);
                    	                       
                    	                   }
        	                      }
        	                                     
	                       }
	                                       
	               
	           }
	    }
	}
	
       public function printview($type,$month){
          $branch=$this->user_model->getBranch();
          $data['type']=$type;
        $data['month']=$month;
         $data['b_header']= $this->db->query("SELECT * FROM `branch` WHERE is_delete='0' AND id='$branch'")->row_array();
      //   $data['allonce']=$this->sm->get_assign_allonce($type,$month);
        $data['employee']=$this->sm->salary_emp($type,$month);
        $data['advance']=$this->sm->advance_all($type,$month);
        $data['deduction']=$this->sm->deduction_all($type,$month);
        $data['security']=$this->sm->security_all($type,$month);
         $this->load->view("salary/print_detail_head",$data);
        }
        
        
        public function getadvance($type="",$month='0000-00',$id=0)
        {
            //$this->db->select('sum(amount) as total')->where('month',$month)->where('refrence',$type)->where('bothid',$id)->where('is_delete',0);
            //$this->db->select('sum(amount) as total')->where('refrence',$type)->where('bothid',$id)->where('is_delete',0);
            //$advance=$this->db->get('advancesal')->row_array()['total'];
           
            $deductions_defs = $this->db->where('flag',1)->where('is_delete',0)->get('deduction_def')->result_array();
            $deduction_remaining = array();
            foreach ($deductions_defs as $key=>$value) {
                $this->db->select('deduction_history.remaning');
                $this->db->join('deduction_history','deduction.id=deduction_history.deductid');
                $this->db->where('deduction.is_delete',0)->where('deduction.refrence',$type)->where('deduction.bothid',$id);
                $this->db->where('deduction_history.typeid',$value['id']);
                $this->db->order_by('deduction_history.id DESC');
                $res=$this->db->get('deduction')->row_array()['remaning'];
                if(!empty($res)){
                    $deduction_remaining[$value['id']]=$res;
                }
            }
//            var_dump($deduction_remaining);die;
            

//            $date=date("Y-m", strtotime("-1 months"));
//            $this->db->where('month',$date)->where('refrence',$type)->where('bothid',$id)->where('is_delete',0);
//            $remaining=$this->db->get('deduction')->result_array();
            
           
           
            
            $type=$this->db->where('is_delete',0)->get('deduction_def')->result_array();
           
            
//            if(count($type)>0)
//                {
//            
//
//               //var_dump("<pre>",$types);die();
//                //foreach ($remaining as $key => $va) {
//                foreach ($type as $key => $value) {
//                    
//                    if($value['id']==@$remaining[$key]['typeid']){
//                        $deductions_defs['amount']=$value['amount'];
//
//                        $data[$key]['name']=$value['name'];
//                        if($value['flag']==0){
//                    $data[$key]['amount']=($value['amount']+$advance)-$remaining[$key]['paid'];
//                }
//                else{
//                    $data[$key]['amount']=$value['amount']+$remaining[$key]['paid'];
//                    
//                    }
//                    }else{
//                        
//                        
//                    $data[$key]['name']=$value['name'];
//                    if($value['flag']==0){
//                    $data[$key]['amount']=$deduction_remaining[$value['id']];}else{
//                    $data[$key]['amount']=$value['amount'];
//                    
//                    
//                    }
//                }
//                
//                }
//                //}
//            }else{
                foreach ($type as $key => $value) {
                    
                    $data[$key]['name']=$value['name'];
                    $data[$key]['id']=$value['id'];
                    if($value['flag']==0){
                       // $deduction_remaining[$value['name']]
                    //$data[$key]$deductions_defs['amount']=$value['amount'];
                        $data[$key]['amount']=$value['amount'];
                    }else{
                        if(!empty($deduction_remaining[$value['id']])){
                            $data[$key]['amount']=$deduction_remaining[$value['id']];
                        }
                        else{
                            unset($data[$key]);
                            continue;
                        }
                    }
                //}
            }

            echo json_encode($data);
        }
        public function getemp_increment($value='')
    {
        if(!empty($value))
             {
                 if($value=="teacher")
                 {
                     $data=$this->sm->increment_teacher($value);
                      $return="";
                        $i=1;
                 foreach ($data as $key => $value) {

                         $return .= "<tr>";
               $return .= "<td>".$i++."</td>";
               $return .= "<td>".$value['firstname']."".$value['lastname']." </td>";
               $return .= "<td>".$value['refrence']." </td>";
               $return .= "<td>".$value['Amount']." </td>";
             $return .= "<td>".$value['month']." </td>";
             $return .= "<td><a href='".base_url()."salary/actionsincre/del/".$value['id']."'><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actionsincre/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>";
            $return .= "</tr>";
        
                 }
                 echo $return;
            
                 }else if($value=="staff"){
                     $data=$this->sm->increment_staff($value);
                      $return="";
                        $i=1;
                 foreach ($data as $key => $value) {

                         $return .= "<tr>";
               $return .= "<td>".$i++."</td>";
               $return .= "<td>".$value['firstname']."".$value['lastname']." </td>";
               $return .= "<td>".$value['refrence']." </td>";
               $return .= "<td>".$value['Amount']." </td>";
             $return .= "<td>".$value['month']." </td>";
             $return .= "<td><a href='".base_url()."salary/actionsincre/del/".$value['id']."'><i class='fa fa-trash'></i></a>
             <a href='".base_url()."salary/actionsincre/edit/".$value['id']."'><i class='fa fa-pencil'></i></a>
             </td>";
            $return .= "</tr>";
        
                 }
                 echo $return;
                 }else{
                     echo "";
                 }
             }
    }
     public function getemp_advance1($val,$ref='')
    {
        if(!empty($ref && $val))
             {
                  if($ref=="teacher" && $val!=0)
                 {
                     $data=$this->sm->increment1_teacher($ref,$val);
                     
                     echo json_encode($data);
                 }else if($ref=="staff" && $val!=0){
                     $data=$this->sm->increment1_staff($ref,$val);
                     echo json_encode($data);
                 }else{
                     echo "";
                 }
             }
    }
    
    public function check_increment($mnth,$ref,$emp)
{
    $salery=$this->db->select('*')
                    ->from('increments')
                    ->where('month',$mnth)
                    ->where('refrence',$ref)
                    ->where('bothid',$emp)
                    ->where('is_del',0)
                    ->get()->result_array();
                if(!empty($salery))
                {
                echo "true";
                }   
                else{
                echo "false";
                }   
}
        
  public function slry_deduction()
    {
        $this->user_model->check_permissions("salary/slry_deduction");
        $data=$this->sm->up();
         
       $this->sm->down("salary/deduction_view",$data);
    }
    
     public function monthly_deductions_teacher()
        {
            //pagination

             $config=[
            'base_url'=>base_url('Salary/slry_deduction'),
            'per_page'=>10,
            'total_rows'=>$this->sm->num_rows("teacher"),
            'full_tag_open'=>"<ul class='pagination'>",
            'full_tag_close'=>"</ul>",
             'last_link'=>'Last',
             'first_link'=>'first',
            'first_tag_open'=>'<li>',
            'first_tag_close'=>'</li>',
            'last_tag_open'=>'<li>',
            'last_tag_close'=>'</li>',
             'next_tag_open'=>'<li>',
            'next_tag_close'=>'</li>',
             'prev_tag_open'=>'<li>',
             'prev_tag_close'=>'</li>',
             'num_tag_open'=>'<li>',
             'num_tag_close'=>'</li>',
             'cur_tag_open'=>"<li class='active'><a>",
             'cur_tag_close'=>'</a></li>',
            
        ];
   $this->pagination->initialize($config);
$ofset=$this->uri->segment(3);
if(empty($ofset))
{
    $ofset=0;
}
else{
    $ofset=$ofset;
}

            //End pagination
            $branchid=$this->user_model->getbranch();
           $query=$this->db->select('*')
                           ->from('teacher')
                           ->where('branch',$branchid)
                           ->where('status',0)
                           ->Limit($config['per_page'],$ofset)
                           ->get()->result_array();
               if(!empty($query))
                   {
                       $i=0;
             foreach ($query as $key => $value) {
                   ++$i;
                echo "<tr'>
            <td>$i</td>
            <td>$value[firstname]</td>
            <td>$value[designation]</td>
            <td>View Deduction</td>
            </tr>";
                   }            
        }

        }
        
         public function monthly_deductions_staff($month,$type)
        {
            $branchid=$this->user_model->getbranch();
           $query=$this->db->select('*')
                           ->from('staff')
                           ->where('branch',$branchid)
                           ->where('status',0)
                           ->get()->result_array();
               if(!empty($query))
                   {
                       $i=0;
             foreach ($query as $key => $value) {
                   ++$i;
                echo "<tr'>
            <td>$i</td>
            <td>$value[firstname]</td>
            <td>$value[designation]</td>
            <td>View Deduction</td>
            </tr>";
                   }            
        }

        }
        
         public function savededuction_type($type="")
    {
        if($this->input->post('type'))
        {
             $branchid=$this->user_model->getbranch();
          $data=$this->sm->up();
         $type=$this->input->post('type');
        $data['type']=$type;
         $month=$this->input->post('month');
      $data['month']=$month;

            //End pagination
      if($type=="teacher"){
          $branchid=$this->user_model->getbranch();
           $ref="teacher";
          $data['record']=$this->db->select('*')
                          ->from('teacher')
                         
                           ->where('teacher.branch',$branchid)
                          ->where('teacher.status',0)
                          ->where('teacher.is_delete',0)
                          ->get()->result_array();
                      $this->sm->down("salary/show_emp",$data);    
                    // $this->load->view("salary/show_emp",$data);
                      // $data['record']=$this->db->select('*')
                         //  ->from('teacher')
                           //   ->join('salary', 'salary.bothid !=teacher.id')
                          // ->where('teacher.branch',$branchid)
                         //  ->where('teacher.status',0)
                         //  ->where('salary.refrence',$ref)
                        //  ->where('salary.month',$month)
                         //  ->Limit($config['per_page'],$ofset)
                         //  ->get()->result_array();
                      // var_dump('<pre>',$data['record']);
                      // die(); 
      }else
      {
             $branchid=$this->user_model->getbranch();
           $data['record']=$this->db->select('*')
                           ->from('staff')
                           ->where('branch',$branchid)
                           ->where('status',0)
                           ->where('staff.is_delete',0)
                          ->get()->result_array();
         $this->sm->down("salary/show_emp",$data);
        // $this->load->view("salary/show_emp",$data);
      } 
        }
        else{
           
echo "Data not posted";
 
        }
         
     
      
            
    }
    
    public function get_ded($id,$type,$month)
    {
        $get_salary=0;
        if($type=="teacher")
        {
            $slry=$this->db->select('salery')
                       ->from('teacher')
                       ->where('id',$id)
                       ->get()->row()->salery;
         $get_salary=$slry;
        }
          if($type=="staff")
        {
            $slry=$this->db->select('salery')
                       ->from('staff')
                       ->where('id',$id)
                       ->get()->row()->salery;
         $get_salary=$slry;
        }
         $query=$this->db->select('*')
                       ->from('deductions_assigned')
                       ->join('deductions', 'deductions.id = deductions_assigned.deduction_id')
                       ->where('deductions_assigned.refrence',$type)
                       ->where('deductions_assigned.bothid',$id)
                       ->where('deductions_assigned.status',0)
                       ->where('deductions_assigned.is_delete',0)
                       ->get()->result_array();
if(!empty($query))
{
$i=0;
   foreach($query as $key => $value)
        {
            $i++;
echo "
<tr>
<td>$value[name]</td>";
   if($value['percentage']>0)
          {
            
            $c=$value['percentage']/100; 
            $ans=$c*$get_salary;
            echo "<td><input type='text' value='$ans' id='ded_total$i' readonly></td>";
          }
          else{
              
              echo " <td><input class='num' type='text' value='$value[rupess]' id='ded_total$i' readonly></td>";
          }
          echo "<td> <input type='text'  onkeyup='check_value($i,event)' name='pay_ded' id='ded$i'> </td>";
          echo "<td> <input type='hidden' name='pay_amount' value='$value[deduction_id]' id='ded_id$i'> </td>
          <td> <input type='hidden'  id='ded_amnt$i'> </td>";
          echo "</tr>";
        }

}

     

//loan


    $loan=$this->db->select('*')
                       ->from('loan_assign')
                       ->join('loan_def', 'loan_def.id = loan_assign.loan_type')
                       ->where('loan_assign.type',$type)
                       ->where('loan_assign.bothid',$id)
                       //->where('loan_assign.month >=',$month)
                       ->where('loan_assign.status',0)
                       ->where('loan_assign.is_delete',0)
                       ->get()->result_array();
             if(!empty($loan))
             {
    
$i=0;
 foreach($loan as $key2 => $value2)
        {
        if($value2['total_paid']+$value2['per_month']<=$value2['amount'])
        {
           $value2['per_month']=$value2['per_month'];
        }else
        {
          $value2['per_month']=$value2['amount']-$value2['total_paid'];
        }
$i++;
echo "<tr>
<td>$value2[name]</td>;
<td><input type='text'  value='$value2[per_month]' id='loan_total$i' readonly></td>;
<td> <input type='text' onkeyup='check_value_loan($i,event)' name='pay_loan' id='loan$i'> </td>
<td> <input type='hidden'  value='$value2[loan_type]' id='loan_id$i'> </td>
<td> <input type='hidden' value='loan' id='loan_type'> </td>
";
     echo " 
</tr>
";    
        }

}          



  //End loan

  //Advances



    $advance=$this->db->select('*')
                       ->from('advance')
                       ->where('advance.type',$type)
                       ->where('advance.bothid',$id)
                       ->where('advance.month',$month)
                       ->where('advance.status',0)
                       ->where('advance.is_delete',0)
                       ->get()->result_array();
             if(!empty($advance))  {


$adv_amount=0;
 foreach($advance as $key3 => $value3)
        {

$adv_amount=$value3['amount'];
         
        }
        echo "<tr>
<td>Advance</td>;
<td><input type='text' value='$adv_amount'  id='adv_total$id' readonly></td>;
<td> <input type='text' onkeyup='check_value_adv($id,event)' name='pay_adv' id='adv$id'> </td>
";

echo " 
</tr>
";

             }        


  //End Advances   

  //Personal Expense

    $expn=$this->db->select('*')
                       ->from('expense_assign')
                        ->join('personal_expense', 'personal_expense.id = expense_assign.expense_id')
                       ->where('expense_assign.refrence',$type)
                       ->where('expense_assign.bothid',$id)
                       ->where('expense_assign.month',$month)
                       ->where('expense_assign.status',0)
                       ->where('expense_assign.is_delete',0)
                       ->get()->result_array();
 if(!empty($expn))  {
$i=0;
 foreach($expn as $key4 => $value4)
        {
            $i++;
echo "<tr>
<td>$value4[name]</td>;
<td><input type='text' value='$value4[rupess]' id='exp_total$i' readonly></td>;
<td> <input type='text' onkeyup='check_value_loan($i,event)' name='pay_exp' id='exp$i'> </td>
<td> <input type='hidden' value='$value4[expense_id]' id='exp_id$i'> </td>
<td> <input type='hidden' value='expense' id='exp_type'> </td>
";
     echo " 
</tr>
";    
        }




             }        


  //End expense  

   //Days deductions

    $days_ded=$this->db->select('*')
                       ->from('days_deduction')
                       ->where('days_deduction.type',$type)
                       ->where('days_deduction.bothid',$id)
                       ->where('days_deduction.month',$month)
                       ->where('days_deduction.status',0)
                       ->where('days_deduction.is_delete',0)
                       ->get()->result_array();
 if(!empty($days_ded))  {

 foreach($days_ded as $key5 => $value5)
        {
echo "<tr>
<td>Days</td>;
<td><input type='text' value='$value5[deduct_amount]' id='days_total$id' readonly></td>;
<td> <input type='text' onkeyup='check_value_days($id,event)' name='pay_days' id='day$id'> </td>
";
    echo " 

</tr>
";     
        }




             }        


  //End days Deductions 
  
echo "<tr>
<td></td>
<td><input type='button' class='btn btn-lg btn-success' onclick='fun($id)' value='Save' ></td>
</tr>";

                    
    } 
    
   public function savefinal_deductions()
{
    if(isset($_POST['deduction'])){
        $type=$_POST['ref'];
        
        $month=$_POST['month'];
         $bothid=$_POST['both_id'];
        // var_dump($type); var_dump($month); var_dump($bothid);
        //die();
         $total_deductions=$_POST['total_ded_amount'];
         $monthly_deductions=$_POST['deduction'];
         $monthly_deductions_ids=$_POST['deduction_ids'];
    $result =count($monthly_deductions);
$i=0;
$k=0;
$t=0;
for($i=0; $i<$result; $i++ )
  {
 for($j=$k; $j<$result; $j++)
  {
      for($l=$t; $l<$result; $l++)
      {
         
 $rem=$total_deductions[$i]-$monthly_deductions[$i];
 if($monthly_deductions[$i]!=""){
      $data=array(
         'refrence'=>$type,
         'bothid'=>$bothid,
         'deduction_id'=>$monthly_deductions_ids[$i],
         'deduction_amount'=>$total_deductions[$i],
         'pay_ded'=>$monthly_deductions[$i],
         'remaining_ded'=>$rem,
         'month'=>$month
     );
//check existing data
$query_data=$this->db->select('*')
                      ->from('monthly_deductions')
                      ->where('refrence',$type)
                      ->where('bothid',$bothid)
                      ->where('deduction_id',$monthly_deductions_ids[$i])
                      ->where('month',$month)
                      ->where('status',0)
                      ->where('is_delete',0)
                      ->get()->result_array();
              if(!empty($query_data))
              {
                 $data_up=array(
                   'is_delete'=>1
                 );
                 $this->db->where('refrence',$type)
                          ->where('bothid',$bothid)
                          ->where('deduction_id',$monthly_deductions_ids[$i])
                          ->where('month',$month)
                          ->where('status',0)
                          ->update('monthly_deductions',$data_up);
                   $this->db->insert('monthly_deductions',$data);
              }else
              {
              $this->db->insert('monthly_deductions',$data);
              }        
    
 }
 
         $t=$l;
         break;
      }
         $k=$j;
         break;
    }//inner loop
        
   }//outer loop
    }
    
    //Get Loans

    if(isset($_POST['Loans'])){
        $type=$_POST['ref'];
        
        $month=$_POST['month'];
         $bothid=$_POST['both_id'];
        // var_dump($type); var_dump($month); var_dump($bothid);
        //die();
         $total_loan_amount=$_POST['total_loan_amount'];
         $monthly_Loans=$_POST['Loans'];
         $monthly_Loan_ids=$_POST['Loan_ids'];
        
    $result =count($monthly_Loans);
$i=0;
$k=0;
$t=0;
for($i=0; $i<$result; $i++ )
  {
 for($j=$k; $j<$result; $j++)
  {
      for($l=$t; $l<$result; $l++)
      {
         
 $rem=$total_loan_amount[$i]-$monthly_Loans[$i];
  if($monthly_Loans[$i] !=""){
     $data=array(
         'refrence'=>$type,
         'bothid'=>$bothid,
         'loan_id'=>$monthly_Loan_ids[$i],
         'loan_amount'=>$total_loan_amount[$i],
         'pay_loan'=>$monthly_Loans[$i],
         'remaining_loan'=>$rem,
         'month'=>$month
     );

              //check existing data
$query_data=$this->db->select('*')
                      ->from('monthly_loans')
                      ->where('refrence',$type)
                      ->where('bothid',$bothid)
                      ->where('loan_id',$monthly_Loan_ids[$i])
                      ->where('month',$month)
                      ->where('status',0)
                      ->where('is_delete',0)
                      ->get()->result_array();
              if(!empty($query_data))
              {
                 $data_up=array(
                   'is_delete'=>1
                 );
                 $this->db->where('refrence',$type)
                          ->where('bothid',$bothid)
                          ->where('loan_id',$monthly_Loan_ids[$i])
                          ->where('month',$month)
                          ->where('status',0)
                          ->update('monthly_loans',$data_up);
                   $this->db->insert('monthly_loans',$data);
              }else
              {
              $this->db->insert('monthly_loans',$data);
              } 
              
              
              //Update Loan Table
              
             
 //$this->db->insert('monthly_loans',$data);
  }

         $t=$l;
         break;
      }
         $k=$j;
         break;
    }//inner loop
        
   }//outer loop

    }

    //Get Avance

    if(isset($_POST['advance'])){
        $type=$_POST['ref'];
        
        $month=$_POST['month'];
         $bothid=$_POST['both_id'];
        // var_dump($type); var_dump($month); var_dump($bothid);
        //die();
         $total_adv_amount=$_POST['adv_total_amount'];
         $monthly_adv=$_POST['advance'];
         
 $rem=$total_adv_amount-$monthly_adv;
  if($monthly_adv !=""){
     $data=array(
         'refrence'=>$type,
         'bothid'=>$bothid,
         'adv_amount'=>$total_adv_amount,
         'pay_adv'=>$monthly_adv,
         'remaining_adv'=>$rem,
         'month'=>$month
     );

//check existing data
$query_advance=$this->db->select('*')
                      ->from('monthly_advance')
                      ->where('refrence',$type)
                      ->where('bothid',$bothid)
                      ->where('month',$month)
                      ->where('status',0)
                      ->where('is_delete',0)
                      ->get()->result_array();
              if(!empty($query_advance))
              {
                 $data_advnc=array(
                   'is_delete'=>1
                 );
                 $this->db->where('refrence',$type)
                          ->where('bothid',$bothid)
                          ->where('month',$month)
                          ->where('status',0)
                          ->update('monthly_advance',$data_advnc);
                   $this->db->insert('monthly_advance',$data);
              }else
              {
              $this->db->insert('monthly_advance',$data);
              } 

 //$this->db->insert('monthly_advance',$data);
  }
    }

    //Get Days


 if(isset($_POST['day'])){
        $type=$_POST['ref'];
        
        $month=$_POST['month'];
         $bothid=$_POST['both_id'];
        // var_dump($type); var_dump($month); var_dump($bothid);
        //die();
         $total_day_amount=$_POST['day_total'];
         $monthly_day=$_POST['day'];
         
 $rem=$total_day_amount-$monthly_day;
  if($monthly_day !=""){
     $data=array(
         'refrence'=>$type,
         'bothid'=>$bothid,
         'days_amount'=>$total_day_amount,
         'pay_days'=>$monthly_day,
         'remaining_days'=>$rem,
         'month'=>$month
     );

//check existing data
$query_day=$this->db->select('*')
                      ->from('monthly_days')
                      ->where('refrence',$type)
                      ->where('bothid',$bothid)
                      ->where('month',$month)
                      ->where('status',0)
                      ->where('is_delete',0)
                      ->get()->result_array();
              if(!empty($query_day))
              {
                 $data_day=array(
                   'is_delete'=>1
                 );
                 $this->db->where('refrence',$type)
                          ->where('bothid',$bothid)
                          ->where('month',$month)
                          ->where('status',0)
                          ->update('monthly_days',$data_day);
                   $this->db->insert('monthly_days',$data);
              }else
              {
              $this->db->insert('monthly_days',$data);
              }

 //$this->db->insert('monthly_days',$data);
  }
    }





//Get Expense

 if(isset($_POST['Expense'])){
        $type=$_POST['ref'];
        
        $month=$_POST['month'];
         $bothid=$_POST['both_id'];
        // var_dump($type); var_dump($month); var_dump($bothid);
        //die();
         $total_exp_amount=$_POST['Expense_amount'];
         $monthly_exp=$_POST['Expense'];
         $monthly_exp_ids=$_POST['Expense_ids'];
        
    $result =count($monthly_exp);
$i=0;
$k=0;
$t=0;
for($i=0; $i<$result; $i++ )
  {
 for($j=$k; $j<$result; $j++)
  {
      for($l=$t; $l<$result; $l++)
      {
         
 $rem=$total_exp_amount[$i]-$monthly_exp[$i];
  if($monthly_exp[$i] !=""){
     $data=array(
         'refrence'=>$type,
         'bothid'=>$bothid,
         'exp_id'=>$monthly_exp_ids[$i],
         'exp_amount'=>$total_exp_amount[$i],
         'pay_exp'=>$monthly_exp[$i],
         'remaining_exp'=>$rem,
         'month'=>$month
     );

$query_expnc=$this->db->select('*')
                      ->from('monthly_expense')
                      ->where('refrence',$type)
                      ->where('bothid',$bothid)
                      ->where('exp_id',$monthly_exp_ids[$i])
                      ->where('month',$month)
                      ->where('status',0)
                      ->where('is_delete',0)
                      ->get()->result_array();
              if(!empty($query_expnc))
              {
                 $data_expnc=array(
                   'is_delete'=>1
                 );
                 $this->db->where('refrence',$type)
                          ->where('bothid',$bothid)
                          ->where('exp_id',$monthly_exp_ids[$i])
                          ->where('month',$month)
                          ->where('status',0)
                          ->update('monthly_expense',$data_expnc);
                   $this->db->insert('monthly_expense',$data);
              }else
              {
              $this->db->insert('monthly_expense',$data);
              }  

 //$this->db->insert('monthly_expense',$data);
  }

         $t=$l;
         break;
      }
         $k=$j;
         break;
    }//inner loop
        
   }//outer loop

    }


} 





    
    
    
        
        
        
        
        
        

}