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

public function __construct(){

        parent::__construct();
    $this->load->model('page_model');
  			$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->library('pagination');


}

public function index()
{
    

}

public function transfer(){
     
        

 $this->load->database(); //load library database

 $getTotaltransfer = $this->page_model->getTotaltransfer();
                         $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
  
		
      
         $limit=$this->db->query("select page_limit from page_limit where page='transfer' ");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($value){
          $perPage = $value;  
        }
        else{
          $perPage = 2;  
        }
		
		 $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
					 $data['tabpagi'] = $active;
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'balance/transfer';
		$config['total_rows'] = $getTotaltransfer;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = true;
		
		
		//Pagination Container tag
		



		$this->pagination->initialize($config);
	
	
        $data['deptlist'] = $this->department_model->get_transfer_list($perPage, $page*$perPage);           
           $data['pagination'] = $this->pagination->create_links();
           $data["pagination_link"] =  $this->pagination->create_links();

         $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['bank'] = $this->department_model->get_bankname();
        $data['to']=$this->input->post('to');
        $data['bb']=$this->input->post('from');
        $this->load->view("transfer.php",$data);
     
}
   
   public function value(){
       
       $data_bank =  $this->input->post('select_bank[]');
     $data1_card = $this->input->post('bank_card[]');
    $bank_from=$this->input->post('bank_from');
    $toid=$this->input->post('select');
        $bankcount=$this->department_model->get_bankcount();
     
       $bankto=$this->department_model->get_banktotransfer($toid);
       
     
       
     $b_type= $this->department_model->get_buisness_type();
       
    $edit_id=$this->input->post('edit_id');
  /* foreach ($bankcount as $key => $bank){
               
         $value [$data_bank[$bank->id]]=$data1_card[$bank->id];
         }*/
         $a=(serialize($data1_card));
  
    if($edit_id){
        
        
       $data=array(
   'date' => $this->input->post('date'),
    'bank_from'=>$bank_from,
   'bank_to' => $this->input->post('select'),
          'amount'=>$this->input->post('total'),
  'bank_info'=>$a,
    'total' => $this->input->post('total'),
          'status'=>1,
          'buisness_type'=>$b_type,
  );
           $purchaseall_detail = array();
     
          foreach ($data1_card as $bank_id => $bank_value){
            
     if ($bank_value >0 ){
        
         
        
              $purchaseall_detail[] =array(
                  
                   'transfer_id' =>$edit_id,
                   'sale_type' =>'bank',
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$this->input->post('total'),
                  'date'=>$this->input->post('date'),
                  'type'=>$this->input->post('select'),
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
         
          
    
              //$where = " 'transfer_id',$edit_id  AND 'bank_id',$bank_id ";
             $this -> db -> where('transfer_id',$edit_id   );
         
            $this -> db -> delete('sale_purchases');
          
         }
             
            
        }   
        
     
        
         $transfer_detail =array(
                   'transfer_id' =>$edit_id,
                   'sale_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$bank_from,
                   'bank_id'=>$bankto,
                   'amount'=>$this->input->post('total'),
                   'status'=>1,
             'buisness_id'=>$b_type,
               );
        
       
        $this -> db -> insert('sale_purchases',$transfer_detail);
          $this -> db -> where('id', $edit_id);
        $this -> db -> update('transfer',$data);
           if($purchaseall_detail){
              $this -> db -> insert_batch('sale_purchases',$purchaseall_detail); 
           }
            
           
        
            
          $url = 'Balance/transfer';
       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'),
    'bank_from'=>$bank_from,
   'bank_to' => $type,
   'amount'=>$this->input->post('total'),
      'bank_info'=>$a,
      'total' => $this->input->post('total'),
          'status'=>1,
          'buisness_type'=>$b_type,
  );  
       
       $this->db->insert('transfer', $data);
       
                $id=$this->db->query("select id from transfer ");
 
  foreach ($id->result() as $key => $purchaseid){
    $pur_id=$purchaseid->id;

  }
             foreach ($data1_card as $bank_id => $bank_value){
               
     if ($bank_value >0 ){
        
         
        
               
              $purchaseall_detail =array(
                   'transfer_id' =>$pur_id,
                   'sale_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$type,
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$this->input->post('total'),
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
          
         $transfer_detail =array(
                   'transfer_id' =>$pur_id,
                   'sale_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$bank_from,
                   'bank_id'=>$bankto,
                   'amount'=>$this->input->post('total'),
                   'status'=>1,
             'buisness_id'=>$b_type,
               );
          
        
          
        
           $this->db->insert('sale_purchases',$purchaseall_detail);
         $this->db->insert('sale_purchases',$transfer_detail); 
          
         }
             
            
        }
         
       
       
       
   }
 

  
redirect('Balance/transfer', 'refresh');
    }
  


     public function newcheck(){ 
          $b_type= $this->department_model->get_buisness_type();
         $date=date("Y-m-d");
         $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="transferbankvalue('.$type->id.')" onKeyUp="transferbankvalue('.$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();
       
        $type='';
            $cash_sale='';
            $bank_card='';
            $ticket='';
            $total='';
            
         $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 transfer b  where b.id  = '$id_post' and buisness_type='$b_type' and status=1 ",false);
          foreach ($type->result() as $key => $value){
            $type=$value->bank_to;
              $date= date("Y-m-d",strtotime($value->date));
              $total=$value->total;
             
              
            
            $bank_card=(unserialize($value->bank_info));
             
                                    foreach ($bankname->result() as $key => $bank){
                                        
                        
                                  
                                           
                                      
                                      $totalbank+=$bank_card[$bank->id];
                                            if($bank_card[$bank->id]){
                                                $chk="checked";
                                               $style="";
                                                $bankid=$bank->id;
                                                $banktype=$bank->type;
                                            }
                                            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="text" name="bank_card['.$bank->id.']" class="banktext bankclass" value="'.$bank_card[$bank->id].'"  onKeyPress="transferbankvalue('.$bank->id.')" onKeyUp="transferbankvalue('.$bank->id.')""></br></div>
                                    ';
                                              
                                      
                                       
        
                                 
                                        
                                    }
             
            
              
          }
        
        
     
        
          $result = array(
              'date'=>$date,
            'type' => $banktype,
              'bank_to'=>$type,
              'html'=>$html,
              'total' => $total,
              'id'=>$id_post,
              'bank_id'=>$bankid,
              
        );
     
   echo json_encode($result);
         
       
    }
    

 
    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('transfer');
        
          
            
        $this->db->set('status',0,false);
        $this -> db -> where('transfer_id', $id_post);
        $this -> db -> update('sale_purchases');
            
      }
             
       
   $b_type= $this->department_model->get_buisness_type();
       
       $status=1;
        $type='';
            $cash_sale=0.0;
            $bank_card=0.0;
            $ticket=0.0;
            $total=0.0;
        $limit=0;
             $limit=$this->db->query("select page_limit from page_limit");
          foreach ($limit->result() as $key => $value)
        $value=$value->page_limit;  
        $limit=$value-1;
        
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
        
          $type=$this->db->query("select * from transfer   where status='$status' and buisness_type='$b_type'   order by id   limit ".$limit." ");
          foreach ($type->result() as $key => $value){
           
             
              $cash_sale+=$value->cash_pay;
              $bank_card+=$value->bank_card;
              $ticket+=$value->cheque_pay;
              $total+=$value->total;
              
          }
          $result = array(
            'type' => $type,
              'cash' => $cash_sale,
              'bank' => $bank_card,
              'ticket' => $ticket,
              'total' => $total,
              'id'=>$id_post,
              
        );
     
   echo json_encode($result);
         
    
 
    }

    
    public function trash(){
    

 $this->load->database(); //load library database


//$num_rows=$this->db->count_all("users");

 $config['base_url'] = base_url().'balance/trash';
      $b_type='';
         $bus_type=$this->db->query("select * from buisness_type  
           ");
          
        $row = $bus_type->row();
       $b_type=$row->buisness_id;
       $query = $this->db->query("SELECT * FROM transfer where status=0 and buisness_type='$b_type' ");
       $row= $query->num_rows();
      $config['total_rows'] = ($row+$row);


 $config['use_page_numbers'] = TRUE;

 $config['full_tag_open'] = '<ul class="pagination">';

 $config['full_tag_close'] = '</ul>';

 $config['prev_link'] = '&laquo;';

 $config['prev_tag_open'] = '<li>';

 $config['prev_tag_close'] = '</li>';

 $config['next_tag_open'] = '<li>';

 $config['next_tag_close'] = '</li>';

 $config['cur_tag_open'] = '<li class="active"><a href="#">';

 $config['cur_tag_close'] = '</a></li>';

 $config['num_tag_open'] = '<li>';

 $config['num_tag_close'] = '</li>';

$config['next_link'] = '&raquo;';
        //pagination settings
        $seting=$this->input->post('seting');
       
        $config['base_url'] = site_url('balance/trash');
       
       
        $limit=$this->db->query("select page_limit from page_limit");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($value){
          $config['per_page'] = $value;  
        }
        else{
           $config['per_page'] = "3";    
        }
           
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = floor($choice);
        $this->pagination->initialize($config);
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) :0;

        //call the model function to get the department data
        $data['deptlist'] = $this->department_model->get_transfer_trash($config["per_page"], $data['page']);        
        $data['pagination'] = $this->pagination->create_links();
         $data['from']='';
        $data['to']='';
        $data['from']=$this->input->post('from');
        
        $data['to']=$this->input->post('to');
        $data['bb']=$this->input->post('from');
       $this->load->view('transfer_trash',$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 trash_delete(){
           $id_post = $this->input->post('uristring'); 
         //$query = $this->db->query("SELECT * FROM bank_name where status=1   ");
   
        if($id_post){
            
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('transfer');
        
          
           
                 
        $this -> db -> where('transfer_id', $id_post);
        $this -> db -> delete('sale_purchases');
            
        }
      }
    public function trash_recover(){
          $id_post = $this->input->post('uristring'); 
        
        
         $query = $this->db->query("SELECT * FROM sale_purchases where status=1   ");
       $row= $query->num_rows();
        if($id_post){
            
       $this->db->set('status',1,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('transfer');
        
          
            for ($i = 0; $i <$row+1; $i++) {
                 
         $this->db->set('status',1,false);
        $this -> db -> where('transfer_id', $id_post);
        $this -> db -> update('sale_purchases');
        
            } 
        }
    }
}

?>
