<?php
error_reporting(0);
class Expense 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 activepagination(){
   
     $b_type=$this->department_model->get_buisness_type();
         $tabid=$this->input->post('expensetabid');
        $page=$this->input->post('expensepage');
        $pagination=$this->db->query("select * from pagination where page='$page' and buisness_id='$b_type' ");
          foreach ($pagination->result() as $key => $value)
              $value=$value->page;
        $data=array('page'=>$page,
                   'active'=>$tabid,
                   'buisness_id'=>$b_type,);
        if($value){
           
            $this -> db -> where('page', $page );
        $this -> db -> update('pagination',$data);
        }
        else{
           $this -> db ->insert('pagination',$data); 
        }
    $result=array('success'=>$tabid);
    echo json_encode($result);

}
public function search(){
    $b_type=$this->department_model->get_buisness_type();
     $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
        

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

  $active = $this->uri->segment(3);
        $filterac=$this->input->post('tabsearchpagi');
       
      
        if($filterac){
          $active=$filterac;  
        }
       
        if(!$active and  !$filterac){
            $active=0;
            
        }
    
        $getTotalexpense = $this->page_model->getTotalexpense($active);
                         $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='expense' and buisness_id='$b_type' ");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($value){
          $perPage = $value;  
        }
        else{
          $perPage = 2;  
        }
		
		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
					 $data['tabpagi'] = $active;
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		  $config['base_url'] = base_url().'expense/search/'.$active;
		$config['total_rows'] = $getTotalexpense;
		$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['tabtype'] = $this->department_model->get_expensetab_type($active);
        
       $data["pagination_link"] =  $this->pagination->create_links();
  
      $data['tabsearch']=$this->input->post('tabsearch');
    
        $data['deptlist'] = $this->department_model->get_expense_list($perPage, $page*$perPage,$active);           

        $data['pagination'] = $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['to']=$this->input->post('to');
        $data['bb']=$this->input->post('from');
      $data['cashbank']=$this->department_model->cash_bank();
        $this->load->view("expense.php",$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 totalexpense(){
        $filtervalue=$this->input->post('uristring');
        
        $filter=$this->department_model->expensefilter($filtervalue); 
        echo json_encode($filter);
    }
   public function value(){
        $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_expensetab_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(
  'user_id' => $this->input->post('id'),
   '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(
                  
                   'expense_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('expense_id', $edit_id);
        $this -> db -> delete('sale_purchases');
             
            
        }   
        
          $this -> db -> where('id', $edit_id);
             $this -> db -> update('expense',$data);
            if($purchaseall_detail){
              $this -> db -> insert_batch('sale_purchases', $purchaseall_detail);  
            }
            
           
        
          $url = 'expense/search';
            echo'
           <script>
          window.location.href = "'.base_url().'/'.$url.'";
           </script>';       
}
 
   
   else{
       
        $type = $this->input->post('select');
       $date = $this->input->post('date');
       
      $data = array(
  'user_id' => $this->input->post('id'),
   '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('expense', $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(
                   'expense_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('expense/search', 'refresh');
    }
  

 
     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();
       
        $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 expense 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>
                                    ';
                                              
                                     
                                       
        
                                 
                                        
                                    }
             
            
              
          }
        
       /* $expensetype=$this->db->query("select * from expense_type b  where b.type  = '$type' and buisness_type='$b_type' and status=1 ",false);
          foreach ($expensetype->result() as $key => $valuetype){
              $expensetypeid=$valuetype->id;
          }
        */
        
          $result = array(
            'type' => $type,
              'html'=>$html,
              'total' => $total,
              'note'=>$note,
              'id'=>$id_post,
              'date'=>$date,
              
        );
     
   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('expense');
        
          
          
                 $this->db->set('status',0,false);
        $this -> db -> where('expense_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 expense   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 cat_edit(){
         $id ['id']=$this->uri->segment(3);
        $this->load->view('cat_edit',$id);
    }
   
   
    public function trash(){
    

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


//$num_rows=$this->db->count_all("users");
                      $b_type=$this->department_model->get_buisness_type();
                       $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
                           $active = $this->uri->segment(3);
        
               $data['tabsearch']=$active ;
                 $this->load->database(); //load library database

    
                         $getTotalexpense = $this->page_model->getTotalexpensetrash($active);
                         $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='expense' and buisness_id='$b_type' ");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($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().'expense/trash/'.$active;
		$config['total_rows'] = $getTotalexpense;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['use_page_numbers'] = true;


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

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

        //call the model function to get the department data
        $data['deptlist'] = $this->department_model->get_expense_trash($perPage, $page*$perPage,$active);        
        $data['pagination'] = $this->pagination->create_links();
         $data['from']='';
        $data['to']='';
        $data['from']=$this->input->post('from');
         $data['cashbank']=$this->department_model->cash_bank();
        $data['to']=$this->input->post('to');
        $data['bb']=$this->input->post('from');
        $data['tabsearch']=$active;
       $this->load->view('expense_trash',$data);
  }
   

      public function type(){


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


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

       $b_type= $this->department_model->get_buisness_type();
       $query = $this->db->query("SELECT * FROM expense_type where status=1 and buisness_type='$b_type' ");
        $row= $query->num_rows();
    
          $perPage = 20;  
      
		
		 $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
					
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'expense/type';
		$config['total_rows'] = $row;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = true;
		
		
		//Pagination Container tag
		$config['full_tag_open'] = '<div style="margin:50px 30%;">';
		$config['full_tag_close'] = '</div>';
		
		//First and last Link Text
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		
		//First tag 
		$config['first_tag_open'] = '<span class="pagination-first-tag">';
		$config['first_tag_close'] = '</span>';
		
		//Last tag 
		$config['last_tag_open'] = '<span class="pagination-last-tag">';
		$config['last_tag_close'] = '</span>';
		
		
		//Next and Prev Link
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		
		
		//Next and Prev Link Styling
		$config['next_tag_open'] = '<span class="pagination-next-tag">';
		$config['next_tag_close'] = '</span>';
		
		$config['prev_tag_open'] = '<span class="pagination-prev-tag">';
		$config['prev_tag_close'] = '</span>';
		
		
		//Current page tag
		$config['cur_tag_open'] = '<strong class="pagination-current-tag">';
		$config['cur_tag_close'] = '</strong>';

		
		$config['num_tag_open'] = '<span class="pagination-number">';
		$config['num_tag_close'] = '</span>';



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

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

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

        $this->load->view('expense_type',$data);
    }
       public function cat_value(){
$b_type= $this->department_model->get_buisness_type();
         $type=$this->input->post('cat');
          $data = array(
  'user_id' =>$this->session->userdata('user_id'),
   
   'type' =>$type,
              
              'buisness_type'=>$b_type,
              'status' =>1,
  
       );
          $value='';
          $p_type=$this->db->query("select type from expense_type where type='$type' and buisness_type='$b_type' and status=1");
          foreach ($p_type->result() as $key => $value)
              $value=$value->type;
          if($value){
             
          
             echo '<script>alert("Type Already exit!");</script>';
   
        }   
        else{
          $this->db->insert('expense_type', $data);  
        }
         
        
         
        redirect('expense/type', '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 expense_type b  where b.id  = '$id_post' and buisness_type='$b_type' ",false);
          foreach ($type->result() as $key => $value){
           
              $type=$value->type;
              
              
          }
          $result = array(
            'type' => $type,
              
              'id'=>$id_post,
              
        );
     
   echo json_encode($result);
         
    }
     public function typeupdate(){
           $id_post='';
         $id_post=$this->input->post('id');
        
        $data = array(
 'user_id' =>$this->session->userdata('user_id'),
   
   'type' => $this->input->post('cat'),
   
  );
        $prename=  $this->page_model->get_preexpensetype($id_post);
         $this -> db -> where('id', $id_post);
        $this -> db -> update('expense_type',$data);
        redirect('expense/type', 'refresh');
     }
    public function type_delete(){
      $id_post = $this->input->post('uristring'); 
        $this->db->set('status',0,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('expense_type');
       
 
    }
     public function trash_delete(){
           $id_post = $this->input->post('uristring'); 
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('expense');
         $this -> db -> where('expense_id', $id_post);
        $this -> db -> delete('sale_purchases');
      }
    public function trash_recover(){
          $id_post = $this->input->post('uristring'); 
        $this->db->set('status',1,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('expense');
        
        $this->db->set('status',1,false);
        $this -> db -> where('expense_id', $id_post);
        $this -> db -> update('sale_purchases');
    }
}

?>
