<?php
error_reporting(0);
class Pay_loan extends CI_Controller {

public function __construct(){

parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->library('session');
$this->load->model('department_model');
$this->load->helper(array('form', 'url'));
 $this->load->library('table');
 $this->load->model('page_model');
 $this->load->library('pagination');


}


public function category(){
        

$this->load->database(); //load library database
    
$getTotalbank= $this->page_model->getTotalbank();
$perPage = 20;  
$page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
         
	 
 $config['base_url'] = base_url().'pay_loan/category';
$config['total_rows'] = $getTotalbank;
$config['per_page'] = $perPage;
$config['uri_segment'] = 3;
$config['use_page_numbers'] = true;
		
        $this->pagination->initialize($config);
	
        $data['pagination'] = $this->pagination->create_links();

        $data['category'] = $this->department_model->get_payloan_category($perPage, $page*$perPage);        
        $data['pagination'] = $this->pagination->create_links();

         $data['id']='';
        $data ['id']=$this->uri->segment(3);
       
        $this->load->view("pay_loan_category.php",$data);
     
}
        public function add(){
         $id_post=$this->input->post('id');
        
         $type=$this->input->post('cat');
         $b_type=$this->department_model->get_buisness_type();
          $data = array(
             'category' =>$type,
              'status' =>1,
             
              'buisness_id'=>$b_type,
            );
         
          $value='';
          
        
            if($id_post){
                $this -> db -> where('id', $id_post);
                $this -> db -> update('pay_loan_category',$data);
            }
            else{
                $p_type=$this->db->query("select category from pay_loan_category where category='$type' and status=1 and buisness_id='$b_type'");
              foreach ($p_type->result() as $key => $value)
             $value=$value->type;
             if($value){
             
          
             echo '<script>alert("Type Already exit!");</script>';
   
              }  
            else{
                $this->db->insert('pay_loan_category', $data);
            }
                 
            }
           
        
         
        
         
        redirect('pay_loan/category', 'refresh');
    }
    public function edit(){
          
        $b_type= $this->department_model->get_buisness_type();
        $type='';
           
            
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
        
          $type=$this->db->query("select * from pay_loan_category b  where b.id  = '$id_post' and b.buisness_id='$b_type' ",false);
          foreach ($type->result() as $key => $value){
           
              $type=$value->category;
             
              
          }
          $result = array(
            'type' => $type,
              
              'id'=>$id_post,
              
        );
     
   echo json_encode($result);
         
    }
  public function catdelete(){
      $id_post = $this->input->post('uristring'); 
       
      
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('pay_loan_category');
       
 
    }
     public function totalexpense(){
        $filtervalue=$this->input->post('uristring');
        
        $filter=$this->department_model->payloanfilter($filtervalue); 
        echo json_encode($filter);
    }
    public function newcheck(){
         $date=date("Y-m-d");
          $b_type= $this->department_model->get_buisness_type();
         $type=$this->db->query("select * from bank_name  where  status=1 and buisness_id='$b_type'
                                      ");
                 $html='';
                     foreach ($type->result() as $key => $type){
                      
                   $html.='<div id="checkboxdiv" style="display:inline-block;vertical-align:top;max-width:100px"><input  type="checkbox" id="myCheck'.$type->id.'" name="select_bank['.$type->id.']" onclick="pbankFunction('.$type->id.')" value="'.$type->id.'"  >'.$type->type.' </br>
                   
                      <input id="text'.$type->id.'" type="number" name="bank_card['.$type->id.']" class="banktext bankclass" style="display:none; width:100%" value="" onKeyPress="expensebankvalue('.$type->id.')" onKeyUp="expensebankvalue('.$type->id.')" step=any></br>
                       <input id="bank'.$type->id.'" type="hidden"  class="banktext class" style="display:none; width:100%" value="" ></br></div>    <input type="hidden" id="bankvalue'.$type->id.'">';
                      }
        $result=array(
        'date'=>$date,
        'html'=>$html,
        'checked'=>11);
         echo json_encode($result);
    }
    public function show(){
      $b_type= $this->department_model->get_buisness_type();
       $html='';
        $type='';
            $cash_sale='';
            $bank_card='';
            $ticket='';
            $total='';
            $totalbank=0.0;
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
        

          $query = $this->db->query("SELECT * FROM bank_name where status=1 and record=1 and buisness_id='$b_type' ");
           $row= $query->num_rows();
         
        $bankname=$this->db->query("select * from bank_name  where   status=1 
                                     and record=1 and buisness_id='$b_type' ");
         $type=$this->db->query("select * from pay_loan b  where b.id  = '$id_post' and buisness_type='$b_type' and status=1 ",false);
          foreach ($type->result() as $key => $value){
            $type=$value->type;
              
              $total=$value->total;
              $note=$value->note;
               $date= date("Y-m-d",strtotime($value->date));
            
            $bank_card=(unserialize($value->bank_info));
             
            foreach ($bankname->result() as $key => $bank){                              
                        
         if(!isset($bank_card[$bank->id])) {
            $bank_card[$bank->id] = 0;
                 }
                                           
                                       
          $totalbank+=$bank_card[$bank->id];
            if($bank_card[$bank->id]){
             $chk="checked";
             $style="";
             }
             else{
                $chk="";
             $style="display:none";
                                                
                 }
                 $html.= '<div id="checkboxdivedit"  class="checkboxdivedit" style="display:inline-block;vertical-align:top;max-width:100px"><input  type="checkbox" id="myCheck'.$bank->id.'" name="select_bank['.$bank->id.']" onclick="pbankFunction('.$bank->id.')" value="'.$bank->id.'" '.$chk.'>'.$bank->type.' </br>
                   <input id="text'.$bank->id.'"  style="'.$style.'" type="number" name="bank_card['.$bank->id.']" class="banktext paybankclass" value="'.$bank_card[$bank->id].'"  onKeyPress="workercalculate()" onKeyUp="workercalculate()" step=any></br></div>';
                                              
          
                                 
                                        
                                    } 
          }
        
    
        
          $result = array(
            'type' => $type,
              'html'=>$html,
              'total' => $total,
              'note'=>$note,
              'id'=>$id_post,
              'date'=>$date,
              
        );
     
   echo json_encode($result);
         
       
    }
   public function create(){
      
        
        
      $b_type=$this->department_model->get_buisness_type();

                    

        $active = $this->uri->segment(3);

        $filterac=$this->input->post('tabsearchpagi');

        if($filterac){

          $active=$filterac;  

        }

       

        if(!$active and  !$filterac){

            $active=0;

            

        }

       
         $pagelimited=$this->db->query("select page_limit from page_limit where page='pay_loan' and buisness_id='$b_type' ");

          foreach ($pagelimited->result() as $key => $pagevalue)

              $value=$pagevalue->page_limit;

          

        if(isset($value)){

          $perPage = $value;  

        }

        else{

          $perPage = 20;  

        }

		$getTotal = $this->page_model->getTotalpayloan($active);

		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
         $data['tabpagi'] = $active;
		  $config['base_url'] = base_url().'pay_loan/create/'.$active;

		$config['total_rows'] = $getTotal;

		$config['per_page'] = $perPage;

		$config['uri_segment'] = 4;

		$config['use_page_numbers'] = true;



		$this->pagination->initialize($config);


         $data['limit']=$perPage;

          $data['start']=$page*$perPage;

        $data['deptlist'] = $this->department_model->get_payloan_list($perPage, $page*$perPage,$active,1);           

       
       $data['cashbank']=$this->department_model->cash_bank();
        $data['category'] = $this->department_model->get_payloan_category(); 

        $data["pagination_link"] =  $this->pagination->create_links();

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

        $data['id']='';

        $data ['id']=$this->uri->segment(3);

         $data['id_post'] = $this->input->post('uristring');

        $data['from']='';

        $data['to']='';
        
        $data['from']=$this->input->post('from');

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

        $this->load->view('pay_loan_list',$data); 
   }
     public function pagelimit(){

        $b_type=$this->department_model->get_buisness_type();

       $page= $this->input->post('limit');

        $limit=$this->db->query("select * from page_limit where page='$page' and buisness_id='$b_type'");

          foreach ($limit->result() as $key => $value)

              $pagelimit=$value->page_limit;

        $result=array('limit'=>$pagelimit);

        echo json_encode($result);

    }
      
  public function value(){
      error_reporting(0);
       $bankrowvalue=$this->department_model->bank_row();
       $data_bank =  $this->input->post('select_bank[]');
     $data1_card = $this->input->post('bank_card[]');
    $expensetype=$this->department_model->get_payloantab_type($this->input->post('select'));
     $b_type= $this->department_model->get_buisness_type();
       $bankcount=$this->department_model->get_bankcount();  
    $edit_id=$this->input->post('edit_id');
    foreach ($bankcount as $key => $bank){
               
         $value [$data_bank[$bank->id]]=$data1_card[$bank->id];
         }
        $a=(serialize($value));
    
    if($edit_id){
        $data = array(
   'date' => $this->input->post('date'),
   'type' => $this->input->post('select'),
   'bank_info'=>$a,
    'note'=>$this->input->post('note'),
   'total' => $this->input->post('total'),
    'status'=>1,
    'buisness_type'=>$b_type,
  );
    foreach ($data1_card as $bank_id => $bank_value){
               
     if ($bank_value >0 ){
   
              $purchaseall_detail[] =array(
                  
                   'pay_loan_id' =>$edit_id,
                   'sale_type' =>'bank',
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                  'date'=>$this->input->post('date'),
                  'type'=>$expensetype,
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
          
      
           
          
         }
        $this -> db -> where('pay_loan_id', $edit_id);
        $this -> db -> delete('sale_purchases');
             
            
        }   
        
          $this -> db -> where('id', $edit_id);
             $this -> db -> update('pay_loan',$data);
            if($purchaseall_detail){
              $this -> db -> insert_batch('sale_purchases', $purchaseall_detail);  
            }

          $url = 'pay_loan/create';
            echo'
           <script>
          window.location.href = "'.base_url().'/'.$url.'";
           </script>';       
       }
 
   else{   
    $type = $this->input->post('select');
    $date = $this->input->post('date');  
    $data = array(
   'date' => $this->input->post('date'),
   'type' => $type,
   'bank_info'=>$a,
    'note'=>$this->input->post('note'),
    'total' => $this->input->post('total'),
    'status'=>1,
    'buisness_type'=>$b_type,
  );  
       
       $this->db->insert('pay_loan', $data);
    
    $pur_id=$this->db->insert_id();
   foreach ($data1_card as $bank_id => $bank_value){
               
     if ($bank_value >0 ){
        $this->db->set('record',1,false);
        $this -> db -> where('id', $bank_id );
        $this -> db -> update('bank_name');
         
        
               
              $purchaseall_detail =array(
                   'pay_loan_id' =>$pur_id,
                   'sale_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$expensetype,
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                   'status'=>1,
                   'buisness_id'=>$b_type,
               );
          
        
           $this->db->insert('sale_purchases',$purchaseall_detail); 
          
         }
             
            
        }
       
   }
   redirect('pay_loan/create', 'refresh');
    
  }
  
    
      public function delete(){

       

        

       $id_post = $this->input->post('uristring'); 

       

    if($id_post){

         $this->db->set('status',0,false);

        $this -> db -> where('id', $id_post);

        $this -> db -> update('pay_loan');   

        $this->db->set('status',0,false);

        $this -> db -> where('pay_loan_id', $id_post);

        $this -> db -> update('sale_purchases');

         

        

    }

          $result = array(

            'id' => $id_post

   

        );

     echo json_encode($result);
 

    }
    
public function trash(){
     $b_type=$this->department_model->get_buisness_type();

                       

       $active = $this->uri->segment(3);

       

        $getTotalwholesale = $this->page_model->getTotalpayloantrash($active);

                      
         $pagelimited=$this->db->query("select page_limit from page_limit where page='loan' and buisness_id='$b_type' ");

          foreach ($pagelimited->result() as $key => $pagevalue)

              $value=$pagevalue->page_limit;

          

        if(isset($value)){

          $perPage = $value;  

        }

        else{

          $perPage = 20;  

        }

		

		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;

        $data['tabpagi'] = $active;

		

		  $config['base_url'] = base_url().'pay_loan/trash/'.$active;

		$config['total_rows'] = $getTotalwholesale;

		$config['per_page'] = $perPage;

		$config['uri_segment'] = 4;

		$config['use_page_numbers'] = true;




		$this->pagination->initialize($config);



         $data['limit']=$perPage;

          $data['start']=$page*$perPage;

        

        $data['deptlist'] = $this->department_model->get_payloan_list($perPage, $page*$perPage,$active,0);           


        $data['cashbank']=$this->department_model->cash_bank();
        $data['pagination'] = $this->pagination->create_links();

        $data['from']='';

        $data['to']='';

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

        $data['tabsearch']=$active;

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

       $this->load->view('pay_loan_trash',$data);
}
     public function trash_recover(){

          $id_post = $this->input->post('uristring'); 


        if($id_post){

        $this->db->set('status',1,false);

        $this -> db -> where('id', $id_post);

        $this -> db -> update('pay_loan');


         $this->db->set('status',1,false);

        $this -> db -> where('pay_loan_id', $id_post);

        $this -> db -> update('sale_purchases');   

        }

        

    }

    public function trash_delete(){

           $id_post = $this->input->post('uristring'); 

        if($id_post){   

        $this -> db -> where('id', $id_post);

        $this -> db -> delete('pay_loan');
               
        $this -> db -> where('pay_loan_id', $id_post);

        $this -> db -> delete('sale_purchases');
   

        }

      }

}

?>
