<?php
error_reporting(0);
class 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().'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();

        //call the model function to get the department data
        $data['category'] = $this->department_model->get_loan_category($perPage, $page*$perPage);        

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

         $data['id']='';
        $data ['id']=$this->uri->segment(3);
       
        $this->load->view("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('loan_category',$data);
            }
            else{
                $p_type=$this->db->query("select category from 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('loan_category', $data);
            }
                 
            }
           
        
         
        
         
        redirect('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 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 delete(){
      $id_post = $this->input->post('uristring'); 
       
      
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('loan_category');
       
 
    }
   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='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->getTotalloan($active);

		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
         $data['tabpagi'] = $active;
		  $config['base_url'] = base_url().'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_loan_list($perPage, $page*$perPage,$active,1);           

       

        $data['category'] = $this->department_model->get_loan_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('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 pur_edit(){

          

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

              $type='';

             $option='';

             $pay_total='';

            $total_order='';

            $due='';

            $total='';

            $html='';

         $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 loan   where id  = '$id_post' and buisness_type='$b_type' and status=1 ");
        $totalbank=0.0;
          foreach ($type->result() as $key => $value){

           

     
               $pay_total=$value->pay_total;

              $total_order=$value->total_loan;

              $due=$value->due_balance;

              $option=$value->type;

               $invoice=$value->note;

              $type= $value->pay_type;

               $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 bankclass" value="'.$bank_card[$bank->id].'"  onKeyPress="bankvalue()" onKeyUp="bankvalue()" step=any></br></div>  ';
              

             }

          }

        


          $result = array(

            'date' => $date,

              //'cash' => $pay_type,

              'option'=>$option,

              'type'=>$type,

              'pay_total' => $pay_total,

              'total_order' => $total_order,

              'due' => $due,

              'id'=>$id_post,

              'html'=>$html,

              'invoice'=>$invoice,

              

        );

     

   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[]');

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

    $bankcount=$this->department_model->get_bankcount();
     $selected=$this->input->post('select');
      $whtype=$this->department_model->get_loan_option_name($selected);
    

          $p_edit=$this->input->post('pur_edit');

      

         if($this->input->post('bank')!=null){

            $paytotal=$this->input->post('bank');

       }

       else{

          $paytotal=00; 

       }

        $value=array();

       

        foreach ($bankcount as $key => $bank){

               

         $value [$data_bank[$bank->id]]=$data1_card[$bank->id];

         }

        $a=(serialize($value));

        if($p_edit){

        $data = array(

 //'user_id' =>$this->session->userdata('user_id'),

   'date' => $this->input->post('date'),

   'type' => $this->input->post('select'),

   'pay_type' => $this->input->post('pay_type'),

   'pay_total' => $paytotal,

    'bank_info' => $a,

    'note' =>  $this->input->post('invoice'),

   'total_loan' => $this->input->post('ticket'),

    'due_balance' => $this->input->post('total'),

     'status' => 1,

  );

        

    foreach ($data1_card as $bank_id => $bank_value){
            

     if ($bank_value >0 ){       

              $purchaseall_detail[] =array(

                  

                   'loan_id' =>$p_edit,

                   'sale_type' =>'bank',

                   'bank_id'=>$bank_id,

                   'amount'=>$bank_value,

                  'date'=>$this->input->post('date'),

                  'type'=>$whtype,

                   'status'=>1,

                  'buisness_id'=>$b_type,

               );
          

         }

             

          $this -> db -> where('loan_id', $p_edit);

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

             

        }   

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

           $this -> db -> update('loan',$data);

           if($purchaseall_detail){

            $this -> db -> insert_batch('sale_purchases', $purchaseall_detail);

            } 

          $url = 'loan/create';

           echo'

           <script>

          window.location.href = "'.base_url().'/'.$url.'";

           </script>';  

      }

     if (!$p_edit){  


          $value=array();
 

         for($i=0; $i<=count($data1_card);$i++){
              

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

        $this -> db -> where('id', $data_bank[$i] );

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


           }

 
      $data = array(

  //'user_id' => $this->input->post('id'),

   'date' => $this->input->post('date'),

   'type' => $this->input->post('select'),

   'pay_type' => $this->input->post('pay_type'),

    'bank_info' => $a,

 'note' =>  $this->input->post('invoice'),

   'pay_total' => $paytotal,

   'total_loan' => $this->input->post('ticket'),

    'due_balance' => $this->input->post('total'),

    'status' => 1,

    'buisness_type' => $b_type,

  );  

       $this->db->insert('loan', $data);

   

       $pur_id=$this->db->insert_id();

 


     foreach ($data1_card as $bank_id => $bank_value){

               

     if ($bank_value >0 ){

              $purchaseall_detail =array(

                   'loan_id' =>$pur_id,

                   'sale_type' =>'bank',

                  'date'=>$this->input->post('date'),

                  'type'=>$whtype,

                   'bank_id'=>$bank_id,

                   'amount'=>$bank_value,

                   'status'=>1,

                  'buisness_id'=>$b_type,

               );


           $this->db->insert('sale_purchases',$purchaseall_detail); 

          

         }   

        }

      redirect('loan/create', 'refresh');

      }

    
  }
 
    
      public function value_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('loan');   

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

        $this -> db -> where('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->getTotalloantrash($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().'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_loan_list($perPage, $page*$perPage,$active,0);           



        $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('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('loan');


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

        $this -> db -> where('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('loan');
               
        $this -> db -> where('loan_id', $id_post);

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

        }

      }

}

?>
