<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Page_model extends CI_Model {

	public function __construct(){
        
		parent::__construct();
	}
	function buisness_type(){
          $b_type='';
        $user_id=$this->session->userdata('user_id');
               $bus_type=$this->db->query("select * from buisness_type  where user_id='$user_id'
    ");
  $data='';
 $row = $bus_type->row();
    return $b_type=$row->buisness_id;
    }
	function getTotalsaletab($tabid){
        $b_type= $this->buisness_type();
       
        
      
        
		$query = "SELECT * FROM sale where status=1  and buisness_type='$b_type' and type='$tabid' ";
        $query2 = $this->db->query($query);
         $tabrow= $query2->num_rows();
        return  $tabrow;
    }
	function getTotalCustomers()
	{
		return $this->db->count_all_results('sale_purchases');
	}
	
	
	public function getCustomersByLimit($limit, $start) 
    {
        $this->db->limit($limit, $start);
        $query = $this->db->get("sale_purchases");
 
        if ($query->num_rows() > 0) 
        {
            return $query->result_array();
        }
 
        return false;
    }
    function getTotalresult()
	{
       $b_type= $this->buisness_type();
        
		$query = "SELECT * FROM sale_purchases where   status=1 and buisness_id='$b_type'  ";
        $query2 = $this->db->query($query);
 $tabrow= $query2->num_rows();
        return  $tabrow;
	}
     function getTotalsale($active)
	{
          $b_type= $this->buisness_type();
        
           
        
         $query = "SELECT * FROM sale where status=1  and buisness_type='$b_type'  ";   
         if($active){
             
                 $query .= " AND type='$active'  " ;    
                    
                }
		
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
	}
    function getTotalpurchasescat(){
        $b_type= $this->buisness_type();
      
          $query = "SELECT * FROM purchases_type where status=1  and buisness_type='$b_type'  ";   
        
		
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
    }
     function getTotalbank(){
        $b_type= $this->buisness_type();
      
          $query = "SELECT * FROM bank_name where status=1  and buisness_id='$b_type'  ";   
        
		
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
    }
     function getTotalwholesale($active)
	{
          $b_type= $this->buisness_type();
         
          $query = "SELECT * FROM wholesale where status=1  and buisness_type='$b_type'  ";   
         if($active){
            $query .= " and type='$active'  ";  
        }
		
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
	}
    function getTotalpurchases($active)
	{
        $b_type= $this->buisness_type();
          
        $query = "select * from purchases where status=1 and buisness_type='$b_type'   " ;    
          if($active){
            $query .= " and type='$active'  ";  
        }          
            
        
		
        $query2 = $this->db->query($query);
         $tabrow= $query2->num_rows();
        return  $tabrow;
	}
     function getTotalworker($active)
	{
        $b_type= $this->buisness_type();
      
       
           $query = "SELECT * FROM worker where status=1  and buisness_type='$b_type'  ";  
         if($active){
            $query .= " and name='$active'  ";  
        }
		
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
	}
    
         function getTotalsaletrash($active)
	{
              $b_type= $this->buisness_type();
        
         
               
       
         $query = "SELECT * FROM sale where status=0  and buisness_type='$b_type'  ";   
      
		if($active){
            $query .= " and type='$active'  ";  
        }
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
    }
     function getTotalworkertrash()
	{
          $b_type= $this->buisness_type();
        
       
         $query = "SELECT * FROM worker where status=0  and buisness_type='$b_type'  ";   
      
		if($active){
            $query .= " and name='$active'  ";  
        }
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
     }
    function getTotalwholesaletrash()
	{
          $b_type= $this->buisness_type();
        
       
         $query = "SELECT * FROM wholesale where status=0  and buisness_type='$b_type'  ";   
         if($active){
            $query .= " and type='$active'  ";  
        }
		
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
     }
    function getTotalexpensetrash($active)
	{
          $b_type= $this->buisness_type();
        
       
         $query = "SELECT * FROM expense where status=0  and buisness_type='$b_type'  ";   
         if($active){
            $query .= " and type='$active'  ";  
        }
		
        $query2 = $this->db->query($query);
        $tabrow= $query2->num_rows();
        return  $tabrow;
     }
    function getTotalpurchasestrash($active)
	         {
              $b_type= $this->buisness_type(); 
              $query = "SELECT * FROM purchases where status=0  and buisness_type='$b_type'  ";   
             if($active){
                $query .= " and type='$active'  ";  
                    
                }
		
        $query2 = $this->db->query($query);
          $tabrow= $query2->num_rows();
        return  $tabrow;
    }
    function getTotalexpense($active)
	{
        $b_type= $this->buisness_type();
         
               
        
           $query = "SELECT * FROM expense where status=1  and buisness_type='$b_type'  "; 
        if($active){
            $query .= " and type='$active'  ";  
        }
		
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
	}
     function getTotaltransfer()
	{
        $b_type= $this->buisness_type();
        
		$query = "SELECT * FROM transfer where status=1  and buisness_type='$b_type'  ";
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
	}
     function getTotalinvoice()
	{
        $b_type= $this->buisness_type();
        
		$query = "SELECT * FROM invoice where status=1  and buisness_type='$b_type'  ";
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
	}
    function activepage()
	{
         $b_type='';
        $user_id=$this->session->userdata('user_id');
               $bus_type=$this->db->query("select * from buisness_type  where user_id='$user_id'
    ");
  $data='';
 $row = $bus_type->row();
    $b_type=$row->buisness_id;
		$page=$this->db->query("select * from pagination  where page=1 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}	
    function saleactivepage()
	{
         $pageid=0;
         $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=2 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}	
    function saletrashactivepage()
	{
         $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=22 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}	
    function expenseactivepage()
	{
         $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=6 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}	
    function wholesaleactivepage()
	{
         $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=4 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}	
       function purchasesactivepage()
	{
      $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=3 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}
        function workeractivepage()
	{
      $b_type= $this->buisness_type();
		$page=$this->db->query("select * from pagination  where page=5 and buisness_id='$b_type' 
    ");
  $data='';
  foreach ($page->result() as $key => $activeid){
    $pageid=$activeid->active;

  }
    return  $pageid; 
}
     function get_precat($id){
      $b_type= $this->buisness_type();
		$page=$this->db->query("select * from purchases_type  where id='$id' and buisness_type='$b_type' 
    ");
     $result=$page->result();
     
      $cat=$result[0]->type;
     $type=$this->input->post('cat');
        $listtype=array('type' => $type);
        
         $this -> db -> where('type', $cat);
   
        $this -> db -> update('purchases',$listtype);

$sql = "UPDATE sale_purchases set type =  '$type'  WHERE purchases_id !=0  and type='$cat'  and buisness_id='$b_type' " ;
$this->db->query($sql);
     
   
   
     
 } 
     function get_prewholecat($id){
      $b_type= $this->buisness_type();
		$page=$this->db->query("select * from wholesale_category  where id='$id' and buisness_type='$b_type' 
    ");
     $result=$page->result();
     
      $cat=$result[0]->category;
     $type=$this->input->post('cat');
        $listtype=array('type' => $type); 
        
         $this -> db -> where('type', $cat);
   
        $this -> db -> update('wholesale',$listtype);

$sql = "UPDATE sale_purchases set type =  '$type'  WHERE wholesale_id !=0  and type='$cat' and buisness_id='$b_type' " ;
$this->db->query($sql);
     
   
   
     
 } 
    function get_preworkername($id){
        $b_type= $this->buisness_type();
		$page=$this->db->query("select * from worker_name  where id='$id' and buisness_type='$b_type' 
    ");
     $result=$page->result();
     
      $cat=$result[0]->name;
     $type=$this->input->post('name');
        $listtype=array('name' => $type); 
        
         $this -> db -> where('name', $cat);
   
        $this -> db -> update('worker',$listtype);

$sql = "UPDATE sale_purchases set type =  '$type'  WHERE worker_id !=0  and type='$cat' and buisness_id='$b_type' " ;
$this->db->query($sql);
     
        
    }
    function get_preexpensetype($id){
         $b_type= $this->buisness_type();
		$page=$this->db->query("select * from expense_type  where id='$id' and buisness_type='$b_type' 
    ");
     $result=$page->result();
     
      $cat=$result[0]->type;
     $type=$this->input->post('cat');
        $listtype=array('type' => $type); 
        
         $this -> db -> where('type', $cat);
   
        $this -> db -> update('expense',$listtype);

$sql = "UPDATE sale_purchases set type =  '$type'  WHERE expense_id !=0  and type='$cat' and buisness_id='$b_type' " ;
$this->db->query($sql);
    }
    function get_presaletype($id){
        $b_type= $this->buisness_type();
		$page=$this->db->query("select * from sale_type  where id='$id' and buisness_type='$b_type' 
    ");
     $result=$page->result();
     
      $cat=$result[0]->type;
     $type=$this->input->post('cat');
        $listtype=array('type' => $type); 
        
         $this -> db -> where('type', $cat);
   
        $this -> db -> update('sale',$listtype);

$sql = "UPDATE sale_purchases set type =  '$type'  WHERE sale_id !=0  and type='$cat' and buisness_id='$b_type' " ;
$this->db->query($sql);
        
    }
    function getTotaltrashinvoice(){
        $b_type= $this->buisness_type();
        
		$query = "SELECT * FROM invoice where status=0  and buisness_type='$b_type'  ";
        $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;
    }
    
    public function getTotalloan($active){
        $b_type= $this->buisness_type();
        $query = "SELECT * FROM loan where status=1  and buisness_type='$b_type'  "; 
        if($active){
            $query .= " and type='$active'  ";  
        }
       $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;  
    }
     public function getTotalloantrash($active){
        $b_type= $this->buisness_type();
        $query = "SELECT * FROM loan where status=0  and buisness_type='$b_type'  "; 
        if($active){
            $query .= " and type='$active'  ";  
        }
       $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;  
    }
    public function getTotalpayloantrash($active){
           $b_type= $this->buisness_type();
        $query = "SELECT * FROM pay_loan where status=0  and buisness_type='$b_type'  "; 
        if($active){
            $query .= " and type='$active'  ";  
        }
       $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;  
    }
    public function getTotalpayloan($active){
         $b_type= $this->buisness_type();
        $query = "SELECT * FROM pay_loan where status=1  and buisness_type='$b_type'  "; 
        if($active){
            $query .= " and type='$active'  ";  
        }
       $query2 = $this->db->query($query);
       $tabrow= $query2->num_rows();
        return  $tabrow;  
    }
}