<?php
error_reporting(0);
class Purchases 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 index()
{
    

}
public function activepurchasespagination(){
     $b_type=$this->department_model->get_buisness_type();
         $tabid=$this->input->post('purchasestabid');
        $page=$this->input->post('purchasespage');
        $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 home(){
     
    $b_type=$this->department_model->get_buisness_type();
    $data['pay']=0.0;
    $data['order']=0.0;
    $data['due']=0.0;
    $data['msg']='';
       //load library database



 $this->load->database();
   $active = $this->uri->segment(3);
        $filterac=$this->input->post('tabsearchpagi');
       
      
        if($filterac){
          $active=$filterac;  
        }
       
        if(!$active and  !$filterac){
            $active=0;
            
        }
          $getTotalpurchases= $this->page_model->getTotalpurchases($active);
        
  
		
      
         $pagelimit=$this->db->query("select * from page_limit where page='home' and buisness_id='$b_type'");
          foreach ($pagelimit->result() as $key => $pagevalue)
               $limited=$pagevalue->page_limit;
        
        if($limited){
          $perPage = $limited;  
        }
        else{
          $perPage = 2;  
        }
		
		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
          $data['tabpagi'] = $active;
		
         $config['base_url'] = base_url().'purchases/home/'.$active;
		$config['total_rows'] = $getTotalpurchases;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['use_page_numbers'] = true;
		

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

        $data['deptlist'] = $this->department_model->get_purchase_list($perPage, $page*$perPage,$active);           
 
      
        $data["pagination_link"] =  $this->pagination->create_links();
    
        $data['limit']=$perPage;
         $data['start']=$page*$perPage;
        $data['from']='';
        $data['to']='';
        $data['tabsearch']=$this->input->post('tabsearch');
        $data['catmark']=$this->department_model->purtabmark();
        $data['tabtype'] = $this->department_model->get_purtab_type($active); 
  
        $data['from']=$this->input->post('from');
        
    
        $data['to']=$this->input->post('to');
   
           $this->load->view("purchases.php",$data);
   
     
}
    /* public function wholesaletotal(){
         $filter=$this->input->post('uristring');
        $saleprofit= $this->department_model->get_totalwholesale($filter);
         echo  json_encode($saleprofit);
     }*/
   
   public function totalpurchases(){
        $filter=$this->input->post('uristring');
         $active=$this->input->get('active');
       $calculate=$this->department_model->get_purchases_total($filter,$active);
       echo json_encode($calculate);
        }
    public function value(){
        $bankrowvalue=$this->department_model->bank_row();
        $b_type=$this->department_model->get_buisness_type();
        $data_bank =  $this->input->post('select_bank[]');
     $data1_card = $this->input->post('bank_card[]');
    $prtype=$this->department_model->get_purtab_type($this->input->post('select'));
        $p_edit=$this->input->post('pur_edit');
      $paytotal=$this->input->post('bank');
        $bankcount=$this->department_model->get_bankcount();
         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,
    'invoice_number' =>  $this->input->post('invoice'),
   'total_order' => $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(
                  
                   'purchases_id' =>$p_edit,
                   'purchases_type' =>'bank',
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                  'date'=>$this->input->post('date'),
                  'type'=>$prtype,
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
         }
             
          $this -> db -> where('purchases_id', $p_edit);
        $this -> db -> delete('sale_purchases');  
        }   
        
         $this -> db -> where('id', $p_edit);
        $this -> db -> update('purchases',$data);
           
           if($purchaseall_detail){
                 $this -> db -> insert_batch('sale_purchases', $purchaseall_detail);
            } 
          
           
           
        $url = 'purchases/home';
       echo'
           <script>
          window.location.href = "'.base_url().$url.'";
           </script>';
        
    }
     if (!$p_edit){  
 
      
      $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,
    'invoice_number' =>  $this->input->post('invoice'),
   'pay_total' => $paytotal,
   'total_order' => $this->input->post('ticket'),
    'due_balance' => $this->input->post('total'),
    'status' => 1,
    'buisness_type' => $b_type,
  );  
     $this->db->insert('purchases', $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(
                   'purchases_id' =>$pur_id,
                   'purchases_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$prtype,
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
          
        
          
          $this->db->insert('purchases_details',$purchaseall_detail); 
           $this->db->insert('sale_purchases',$purchaseall_detail); 
          
         }
             
            
        }
        
     redirect('purchases/home', 'refresh');
      }
    }
    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 bankvalue(){
       
         $b_type= $this->department_model->get_buisness_type();
        $bank_id = $this->input->post('uristring');
       $total_amount_bank=0.0;
        $pur_banksum=0.0;
         $query = $this->db->query("SELECT * FROM bank_name where status=1 and id='$bank_id' and record=1 and buisness_id='$b_type' ");
       foreach ($query->result() as $key => $typen){
            $typen=$typen->type;
       }
        $banksum=0.0;
         $query2 = $this->db->query("SELECT * FROM sale_purchases where status=1 and bank_id='$bank_id' and buisness_id='$b_type' ");
       foreach ($query2->result() as $key => $value){
            $banksum+=$value->amount;
           $pur_banksum+=$value->pur_amount;
       }
       
        /* $query3 = $this->db->query("SELECT * FROM purchases_details where status=1 and bank_id='$bank_id' ");
       foreach ($query3->result() as $key => $pur_value){
            $pur_banksum+=$pur_value->amount;
       }*/
        $total_amount_bank=$banksum- $pur_banksum;
      
        
        $data=array(
            'sum'=>number_format($total_amount_bank,2),
            'id'=>$bank_id,
            'type'=>$typen,
            'sale_bank'=>number_format($banksum,2),
        );
       echo json_encode($data); 
    }
     public function cash(){
        $cash = $this->input->post('category');
         $salecashsum=0.0;
         if($cash=='cash'){
             $query2 = $this->db->query("SELECT * FROM purchases where status=1 and pay_type='cash' ");
         }
         if($cash=='ticket'){
             $query2 = $this->db->query("SELECT * FROM purchases where status=1 and pay_type='ticket' ");
         }
         
       foreach ($query2->result() as $key => $value){
            $salecashsum+=$value->pay_total;
       }
        $cashsum=0.0;
         $query2 = $this->db->query("SELECT * FROM sale_details where status=1 and sale_type='$cash' ");
       foreach ($query2->result() as $key => $value){
            $cashsum+=$value->amount;
       }
         $cashamount=$cashsum-$salecashsum;
        $data=array(
           
            'cash'=>$cashamount,
            'pur_cash'=>$salecashsum,
        );
       echo json_encode($data); 
    }
    public function paybankvalue(){
        $b_type= $this->department_model->get_buisness_type();
        $bank_id = $this->input->post('uristring');
       
         $query = $this->db->query("SELECT * FROM bank_name where status=1 and id='$bank_id' and record=1 and buisness_id='$b_type'");
       foreach ($query->result() as $key => $typen){
            $typen=$typen->type;
       }
       
        $sum=0.0;
        $count=0;
         $type=$this->db->query("select * from sale  where  buisness_type='$b_type' and status=1 ");
          foreach ($type->result() as $key => $value){
        
              $bank_card=(unserialize($value->bank_info));
              $count+=count($bank_card);
             
              // echo json_encode($count);
        //exit;
            
          }
       //echo json_encode($count);
       // exit;
       foreach ($type->result() as $key => $value){
        
              $bank_card=(unserialize($value->bank_info));
        for ($n = 0; $n<$count; ++$n) 
                            
                    { 
                   
                   if($bank_id==$n){
                       $sum+=$bank_card[$n];
                       
                       
                   }
               } 
       }
        
        $data=array(
            'sum'=>$sum,
            'id'=>$bank_id,
            'type'=>$typen,
        );
       echo json_encode($data); 
    }
     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 purchases   where id  = '$id_post' and buisness_type='$b_type' and status=1 ",false);

        $totalbank=0.0;

          foreach ($type->result() as $key => $value){

           

             // $type=$value->type;

             $total=$value->total;

              $ticket=$value->ticket;

              $cash_sale=$value->cash_sale;

               $pay_total=$value->pay_total;

              $total_order=$value->total_order;

              $due=$value->due_balance;

              $option=$value->type;

               $date= date("Y-m-d",strtotime($value->date));

               $invoice=$value->invoice_number;

              $type=$value->pay_type;

            $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>

                                    ';

                                              

                                       

                                       

        

                                  

                                        

                                    }

             

            

              

          }

        

     /*$prtype=$this->db->query("select * from purchases_type b  where type  = '$option' and buisness_type='$b_type' and status=1 ",false);

          foreach ($prtype->result() as $key => $prtype){

              $prtype=$prtype->id;

          }*/

        

          $result = array(

            'date' => $date,

              //'cash' => $pay_type,

             

              'type'=>$type,

              'category'=>$option,

              'pay_total' => $pay_total,

              'total_order' => $total_order,

              'due' => $due,

              'id'=>$id_post,

              'html'=>$html,

              'invoice'=>$invoice,

              

        );

     

   echo json_encode($result);

         

       

    

    }

    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="bankvalue('.$type->id.')" onKeyUp="bankvalue('.$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='';

        $pay_type='';

        $order=0.0;

        $t_order=0.0;
        $due_balance=0.0;
        $d_balance=0.0;

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

        

        $id_post1 = $this->input->post('type');

        

          $type=$this->db->query("select * from purchases b  where b.type  = '$id_post' and buisness_type='$b_type' and status=1 ",false);

          foreach ($type->result() as $key => $value){

             $type=$value->pay_type;

              $id=$value->id;

              $order+=round($value->pay_total,2);

              $t_order+=round($value->total_order,2);

              $dubalance+=round($value->due_balance,2);

              

              

          }

        $dbalance= ($order)-($t_order);

        //$due=$order-$t_order;

          $result = array(

              

            'res' => round($order,2),

              'res1' => round($t_order,2),

              'res2' => round($dbalance,2),

              'res3' => $id_post,

              'id'=>$id,

              'res5'=>$type,

              

        );

     

   echo json_encode($result);

         

       

    }
    public function pay(){
          $b_type= $this->department_model->get_buisness_type();
        $bankcount=$this->department_model->get_bankcount();
      $total=$this->input->post('totaldu');
        
     $bankval=$this->input->post('bank');
     $ticket=$this->input->post('ticket');
        
    $data_bank =  $this->input->post('select_bank[]');
     $data1_card = $this->input->post('bank_card[]');     
        
        $selected=$this->input->post('select');
        $pay=$this->input->post('pay_type');
       $prtype=$this->department_model->get_purtab_type( $selected);
       $value=array();
        foreach ($bankcount as $key => $bank){
               
         $value [$data_bank[$bank->id]]=$data1_card[$bank->id];
             $this->db->set('record',1,false);
        $this -> db -> where('id', $data_bank[$bank->id] );
        $this -> db -> update('bank_name');
         }
         
     
          $a=(serialize($value));
        
       // $select = str_replace(' ', '', $selected);
       //$pay_type = str_replace(' ', '_', $pay);
     
      $data = array(
 // 'user_id' =>$this->session->userdata('user_id'),
   'date' => $this->input->post('date'),
   'type' => $selected,
   'pay_type' => $pay,
   'pay_total' => $bankval,
    'due_balance'=> $bankval,
   'bank_info' => $a,
    'buisness_type' => $b_type,
    'status' => 1,
          
          
       );
        
         $this->db->insert('purchases', $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(
                   'purchases_id' =>$pur_id,
                   'purchases_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$prtype,
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                   'status'=>1,
                  'buisness_id' => $b_type,
               );
          
        
          
          $this->db->insert('sale_purchases',$purchaseall_detail); 
           $this->db->insert('purchases_details',$purchaseall_detail);
         }
             
            
        }
        
         
       
        redirect('purchases/home', 'refresh');
    }
    public function update(){
        $id_post='';
         $id_post=$this->input->post('edit_purchases');
        $data = array(
 'user_id' =>$this->session->userdata('user_id'),
   'date' => $this->input->post('date'),
   'type' => $this->input->post('type'),
   'pay_type' => $this->input->post('cash'),
   'pay_total' => $this->input->post('bank'),
   'total_order' => $this->input->post('ticket'),
    'due_balance' => $this->input->post('total'),
            'status' => 1,
  );
        
         $this -> db -> where('id', $id_post);
        $this -> db -> update('purchases',$data);
        redirect('purchases/home', 'refresh');
        
    }
     public function category(){
              
 $this->load->database(); //load library database




          $getTotalpurchases= $this->page_model->getTotalpurchasescat();
          $perPage = 20;  
       
		
		 $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
       
		  $config['base_url'] = base_url().'purchases/category';
		$config['total_rows'] = $getTotalpurchases;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = true;
		
		


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

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

        $data['pagination'] = $this->pagination->create_links();
     
    
         $this->load->view('category',$data);
     }
    public function delete(){
          $id_post = $this->input->post('uristring'); 
        $this->db->set('status',0,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('purchases_type');
    }
    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('purchases');
        
          
                 $this->db->set('status',0,false);
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> update('purchases_details');
         
      
                 $this->db->set('status',0,false);
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> update('sale_purchases');

            
        }
       
        
         $b_type= $this->department_model->get_buisness_type();
       $status=1;
        $pay=0.0;
        $order=0.0;
        $due=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 purchases   where status='$status' and buisness_type='$b_type'   order by id   limit ".$limit." ",false);
          foreach ($type->result() as $key => $value){
           
             
              $pay+=$value->pay_total;
              $order+=$value->total_order;
              $due+=$value->due_balance;
              
              
          }
          $result = array(
            'pay' => $pay,
              'order' => $order,
              'due' => $due,
             
              
        );
     
   echo json_encode($result);
         
        
    }
    public function cat_edit(){
         $id ['id']=$this->uri->segment(3);
        $this->load->view('cat_edit',$id);
    }
    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,
              'status' =>  1,
              'buisness_type' =>  $b_type,
              
  
       );
        $p_type='';
     $value='';
          $p_type=$this->db->query("select type from purchases_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("Category Already exit!");</script>';
   
        }   
        else{
           $this->db->insert('purchases_type', $data);  
        }
        
         
       
        redirect('purchases/category', 'refresh');
    }
    public function cat_update(){
         $id_post='';
         $id_post=$this->input->post('id');
        $data = array(
        'user_id' =>$this->session->userdata('user_id'),
   
       'type' => $this->input->post('cat'),
        );
        
         $this -> db -> where('id', $id_post);
        $this -> db -> update('purchases_type',$data);
        redirect('purchases/category', 'refresh');
    }
    public function trash(){
         $this->load->library('table');

 $this->load->library('pagination');

 $this->load->helper('form');

 $this->load->helper('url');

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

$active = $this->uri->segment(3);
//$num_rows=$this->db->count_all("users");

  $b_type=$this->department_model->get_buisness_type();
   
       
       
        
       
        if(!$active){
            $active=0;
            
        }
     
        $getTotalsale = $this->page_model->getTotalpurchasestrash($active); 
    
        
        
                          $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
  
		
      
         $pagelimit=$this->db->query("select * from page_limit where page='home' and buisness_id='$b_type' ");
          foreach ($pagelimit->result() as $key => $pagevalue)
               $limited=$pagevalue->page_limit;
       
        if($limited){
          $perPage = $limited;  
        }
        else{
          $perPage = 20;  
        }
		
		 $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().'purchases/trash/'.$active;;
		$config['total_rows'] = $getTotalsale;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['use_page_numbers'] = true;
		

		$this->pagination->initialize($config);
	
        
        $data['deptlist'] = $this->department_model->get_ptrash($perPage, $page*$perPage,$active);           

        $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('purchases_trash',$data);

    }
     public function trash_delete(){
           $id_post = $this->input->post('uristring'); 

        if($id_post){
            
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('purchases');
        
           
                
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> delete('purchases_details');
          
      
                
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> delete('sale_purchases');
    
            
        }
      }
     public function alltrashdelete(){
           $id_post = $this->input->post('uristring'); 
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('purchases');
      }
    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('purchases');
        
                 $this->db->set('status',1,false);
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> update('purchases_details');
          
          
                 $this->db->set('status',1,false);
        $this -> db -> where('purchases_id', $id_post);
        $this -> db -> update('sale_purchases');
         
            
        }
        
    }
     public function catedit(){
          
       $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 purchases_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);
         
    }
}

?>
