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

class Usermodel extends CI_Model {


  function getapplytudents($email, $password)

{
//robert
   $this -> db -> select('*');

   $this -> db -> from('people');

   $this -> db -> where('email', $email);

   $this -> db -> where('indexno', ($password));

   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result();

   }

   else

   {

     return false;

   }

}




		public function search_record_plotsexpense($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM  expenses WHERE 	imgid  = '" . $this->db->escape_like_str($searchterm) . "' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	

	public function searchallplotsexpense($searchterm,$limit)
{
    $sql = "SELECT * FROM expenses
  WHERE imgid = '" . $this->db->escape_like_str($searchterm) . "'  LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	









function populateAlerts($mydate,$deliverystatus){
 
 
  $this -> db -> select('*');

   $this -> db -> from('alerts');
$this -> db -> where('duedate', $mydate);
$this -> db -> where('deliverystatus', $deliverystatus);




 $this->db->order_by('id','DESC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function populatepostimages($img_id){

if($img_id!=null){
$this->db->select(" * from  alldocuments where img_id= '$img_id'");
$this -> db -> limit(1);
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();

}else{

  return false;
}

}


}

		public function countAlerts($userid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM alerts WHERE userid='$userid'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

	
	
   public function displayallattendance($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		 $this->db->order_by('attendance.id','DESC');
		

	$this->db->join('people', 'attendance.idno = people.idno','INNER JOIN' );
		//$this->db->join('attendance', 'people.idno = attendance.idno','INNER JOIN');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   



public function searchallattendance($searchterm,$limit)
{
	
	//
    $sql = "SELECT * ,attendance.id  as id FROM attendance INNER JOIN people on  attendance.idno= people.idno  WHERE attendance.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or date LIKE '%" . $searchterm . "%'  or comment LIKE '%" . $searchterm . "%' or type LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
   		

   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	
	
	

	
	
public function searchaaccountingsales2($invoice,$companyid)
{
    $sql = "SELECT * ,sales_order.code,sales_order.code,sales_order.qty,sales_order.branch,sales_order.amount, products.producttype,sales_order.bprice,products.salesaccount,products.expenseaccount,products.assetaccount
,sales_order.id  as id,sales_order.qty  as qty  FROM sales_order INNER JOIN products on  sales_order.code= products.code  WHERE sales_order.invoice = '" . $invoice . "' and   sales_order.status ='printed' and sales_order.companyid= '$companyid' order by sales_order.id desc  LIMIT 10 OFFSET 500 " ;
   		
		

   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	
	
			
public function searchaaccountingsalesrow($invoice,$id)
{
    $sql = "SELECT sales_orderraw.id  as id, sales_orderraw.invoice,sales_orderraw.qty  as qty,sales_orderraw.bprice  as bprice,sales_orderraw.code,sales_orderraw.branch,sales_orderraw.amount,sales_orderraw.producttype,sales_orderraw.salesaccount,sales_orderraw.expenseaccount,sales_orderraw.assetaccount  FROM sales_orderraw WHERE sales_orderraw.invoice = '$invoice'  and sales_orderraw.companyid='$id' and sales_orderraw.status !='returned' " ;
   	  //  $sql = "SELECT sales_orderraw.id  as id, sales_orderraw.qty  as qty,sales_orderraw.bprice  as bprice,sales_orderraw.code,sales_orderraw.branch,sales_orderraw.amount,products.producttype,products.salesaccount,products.expenseaccount,products.assetaccount  FROM sales_orderraw INNER JOIN products on  sales_orderraw.code= products.code  WHERE sales_orderraw.invoice = '$invoice'  and sales_orderraw.companyid='$id' and sales_orderraw.status !='returned' " ;
	
		

   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
		
public function searchaaccountingsales($invoice,$id)
{
    $sql = "SELECT sales_order.id  as id, sales_order.qty  as qty,sales_order.bprice  as bprice,sales_order.code,sales_order.branch,sales_order.amount,sales_order.invoice,sales_order.producttype,sales_order.salesaccount,sales_order.expenseaccount,sales_order.assetaccount  FROM sales_order  WHERE sles_order.invoice = '$invoice'  and sales_order.companyid='$id' and sales_order.status !='returned' " ;
   		  //  $sql = "SELECT sales_order.id  as id, sales_order.qty  as qty,sales_order.bprice  as bprice,sales_order.code,sales_order.branch,sales_order.amount,producttype,products.salesaccount,products.expenseaccount,products.assetaccount  FROM sales_order INNER JOIN products on  sales_order.code= products.code  WHERE sales_order.invoice = '$invoice'  and sales_order.companyid='$id' and sales_order.status !='returned' " ;

		

   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
public function accountinginventory($invoice)
{
	
	
	$this->db->select("*");
		$this->db->from('newstock');

  //$this->db->where('companyid',$companyid);
	
	
	
	 	$this->db->where("(invoice_number='$invoice'   OR invoice='$invoice'  )", NULL, FALSE);


	
	
	
	
	
	
	
	
	
	
	
		     $this->db->where('itemstatus <>',"returned");
			 //  $sql = "SELECT  * ,newstock.id  as id FROM newstock  WHERE newstock.invoice_number or newstock.invoice  = '" . $invoice . "' " ;

   //$sql = "SELECT  * ,newstock.id  as id FROM newstock  WHERE newstock.invoice_number  = '" . $invoice . "' " ;

   // $sql = "SELECT  * ,newstock.id  as id FROM newstock  WHERE newstock.invoice_number = '" . $invoice . "' and itemstatus!='returned' " ;
   		
$q = $this -> db -> get();
if($q->num_rows()>0){




   //$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
}		
	
	public function returnedinventory($invoice)
{
	
		    //  $this->db->where('itemstatus <>',"returned");

    $sql = "SELECT  * ,newstock.id  as id FROM newstock  WHERE newstock.invoice_number = '" . $invoice . "' and itemstatus='returned' " ;
   		
		   // $sql = "SELECT  * ,newstock.id  as id FROM newstock INNER JOIN products on  newstock.item_id= products.code  WHERE newstock.invoice_number = '" . $invoice . "' " ;


   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
		
	   public function searchalldispatch_count($searchterm)
	{

	
		//$sql = "SELECT COUNT(*) As cnt FROM bulkdispatch WHERE id = '$searchterm' or  description = '$searchterm' or remarks LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  	createdate	 LIKE '%" . $searchterm . "%'  ";
		 
		 $sql = "SELECT COUNT(*) As cnt FROM bulkdispatch WHERE id='$searchterm' or  description LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or  remarks LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  createdate LIKE '" . $this->db->escape_like_str($searchterm) . "%' ";

   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

public function searchalldispatch($searchterm,$limit)
{
	
	//
    //$sql = "SELECT * ,bulkdispatch.id  as id FROM bulkdispatch WHERE bulkdispatch.id = '$searchterm'   or bulkdispatch.description LIKE '%" . $searchterm . "%' or  remarks LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%'  order by ID DESC LIMIT " .$limit . ",20";
   	 $sql = "SELECT * FROM bulkdispatch WHERE id='$searchterm' or  description LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or  remarks LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  createdate LIKE '" . $this->db->escape_like_str($searchterm) . "%'    order by ID DESC LIMIT " .$limit . ",20";


   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	
	
	

	   public function searchallattendance_record_count($searchterm)
	{

	
	   $sql = "SELECT COUNT(*) As cnt  FROM attendance INNER JOIN people on  attendance.idno= people.idno  WHERE attendance.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or date LIKE '%" . $searchterm . "%'  or comment LIKE '%" . $searchterm . "%' or type LIKE '%" . $searchterm . "%'";

   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}



   
   function viewattandance($fromdate,$todate,$category, $type,$dublicate,$sms,$idno){





	if($type=="all"){
	$this->db->where("(  attendance.date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
//$this->db->where('grouping.memberid', 'people.id');


	}else{
		$this->db->where("(  attendance.date BETWEEN '$fromdate'  and '$todate' and type ='$type'   )", NULL, FALSE);



}

	if($category=="all"){
	$this->db->where("(  attendance.date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
		$this->db->join('people', 'attendance.idno = people.idno','INNER JOIN');

		$this->db->from('attendance');


	}else{
		
		$this->db->where("(  attendance.date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
		$this->db->join('people', 'attendance.idno = people.idno','INNER JOIN');

		$this->db->from('attendance');
		
		

}


if($sms !="all"){
		$this->db->where('attendance.timediffrence', $sms);

}
if($idno !="all"){
		$this->db->where('attendance.idno', $idno);

}


	 $this->db->order_by('attendance.date','ASC');

	
	 	if($dublicate=="no"){
			
			$this->db->select("  * ,timediffrence, SUM(timediffrence) AS timediffrence ");

									$this->db->group_by('attendance.idno');


				}else{

					
				}	 
	 
	 
	 
	 
			// $this->db->order_by('stock.closedate','DESC');


		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){



$bindings = $bioquery->result();

$records = $bioquery->num_rows();
   //return $bioquery->result();
   
   //return $bioquery->num_rows();
   
   return array(
    'records' => $bindings,
    'count' => $records,
);


}else{

  return false;
}
   }
	




   
function viewallattandance($fromdate,$todate,$companyid){





	$this->db->where("(  attendance.date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

		//$this->db->select("  *");

	
		$this->db->select("  * ,people.id  as peopleid,people.idnumber,people.idno,people.firstname,people.number,attendance.timediffrence,attendance.date,attendance.checkoutime,attendance.totaltime    ");

		$this->db->from('attendance');

		$this->db->join('people', 'attendance.idno = people.idno','INNER JOIN');








	 $this->db->order_by('attendance.date','ASC');

	 
	
	 
	 
	 
			// $this->db->order_by('stock.closedate','DESC');


		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){



   
   return $bioquery->result();
   


}else{

  return false;
}
   }
	




   
      
public function displayallinone($limit, $start,$database,$where,$value) {
   		$this->db->select("  * ,attendance.id  as id ");

		$this->db->where($where, $value);
				//$this->db->join('people', 'attendance.idno = people.idno','LEFT JOIN' );
//$this->db->where("timeout <>", "");
		 //$this->db->order_by('attendance.id','DESC');
		 		        $this->db->limit($limit, $start);
		 $this->db->order_by('attendance.date','DESC');

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
					$records = $query->num_rows();

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
				   return $data;
        }
   
        return false;
    
        
   }
   
   
   
   
   
   
   
   
	
	
function getepeople($id){

if($id!=null){
$this->db->select(" * from people where phone='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}









function getFeedbackmembers($COMPANYID){

if($COMPANYID!=null){



	$this->db->select(" *, people.id  as myid,smsconfig.id  as id,smsconfig.username  as username,smsconfig.password  as password  from  smsconfig INNER JOIN people on  smsconfig.paymenttypes= people.id   where sendfeedback='yes' and feedback !='' and paymenttypes !=''  and people.totalsms > 0 ");
	

		
	 $this->db->order_by('people.id','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}












	function getgroupmembers($category){

		$this->db->from('grouping');


	
		$this->db->select("  * ,people.id  as peopleid ");
		$this->db->where('grouping.categoryid', $category);
		$this->db->where('grouping.status',1);

		$this->db->where('people.phone <>',"");

		$this->db->join('people', 'grouping.memberid = people.id','INNER JOIN');
		
		
		//$this->db->join('attendance', 'people.idno = attendance.idno','INNER JOIN');

		

	 $this->db->order_by('people.firstname','ASC');

	 

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return $bioquery->result();
 
}else{

  return false;
}
   }
	



	
	
	
	

	function getallmobileusers(){

		$this->db->from('sms_in');


	
		$this->db->select("  * ,sms_in.sender_number  as phone ");
		$this->db->where('sms_in.used', 2);
		$this->db->where('sms_in.amount >',0);

		$this->db->where('sms_in.sender_number <>',"");

		

	 $this->db->group_by('sender_number');
	 $this->db->order_by('id','ASC');

	 

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return $bioquery->result();
 
}else{

  return false;
}
   }
	



	
	
	
	
	
function searchsms($fromdate,$todate,$companyid,$userid){

		$this->db->where("(  postedtime BETWEEN '$fromdate'  and '$todate' and userid ='$userid'   )", NULL, FALSE);







	 $this->db->order_by('id','DESC');
		$this->db->from('sms');

	 
	 
	 
			// $this->db->order_by('stock.closedate','DESC');


		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){



$bindings = $bioquery->result();

$records = $bioquery->num_rows();
   //return $bioquery->result();
   
   //return $bioquery->num_rows();
   
   return array(
    'records' => $bindings,
    'count' => $records,
);


}else{

  return false;
}
   }
	


	
	
	
	
	
	function count_allwheredatabydate($tablename,$where,$value,$where1,$mydate)
	{
		$this -> db -> where($where, $value);
		$this -> db -> like($where1, $mydate);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
function count_allwheredatabyorderdate($tablename,$where,$value,$fromdate,$todate,$where1,$value1)
	{
		$this -> db -> where($where, $value);
			//$this -> db -> where("ordersales", "new");
			$this->db->where("(ordersales ='new' or ordersales ='Served' or paymentstatus='No'  )", NULL, FALSE);
	$this->db->where($where1, $value1);
	$this->db->where('type <>',"Advance");
						$this->db->where('dateposted >=', $fromdate);
				$this->db->where('dateposted <=',$todate);
		//$this -> db -> like($where1, $mydate);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
	
function count_allwheredatabyorderdateHold($tablename,$where,$value,$fromdate,$todate,$type)
	{
		$this -> db -> where($where, $value);
			//$this -> db -> where("ordersales", "new");
			//$this->db->where("(ordersales ='new' or ordersales ='Served'  )", NULL, FALSE);
	
			
						//$this->db->where('dateposted >=', $fromdate);
				$this->db->where('dateposted <=',$todate);
		//$this -> db -> like($where1, $mydate);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	

function uploadata($data)
  
{
   $offday = $data['offday'];
      $groupid = $data['groupid'];

  if($offday!=null){
     $this->db->where('offday',$offday);
	$this->db->where('groupid',$groupid);
   $qq = $this->db->get('offdays');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('offday',$offday);
	  $this->db->where('groupid',$groupid);

      $this->db->update('offdays',$data);
   } else {
      
      $this->db->insert('offdays', $data);
      
   }


    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}

	
	
	function count_allsalesbydatecashier($tablename,$where,$value,$startdate,$enddate,$branchid,$maincategory) 
	{
		
		  if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate' and paymentstatus !='Cleared'   )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid' and paymentstatus !='Cleared'   )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value!="all")){
				$this->db->where("( ".$where."  ='$value'    and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'  and paymentstatus !='Cleared'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value=="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  and paymentstatus !='Cleared'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value!="all")){
				$this->db->where("( ".$where."  ='$value'    and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'  and paymentstatus !='Cleared'  )", NULL, FALSE);

		}else{
				$this->db->where("( ".$where."  ='$value'    and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and maincategory='$maincategory' and paymentstatus !='Cleared'  )", NULL, FALSE);

		}
		
	  
	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
	
	
	
	
	public function expensesperinvoice($invoiceno, $companyid)
	{ 

  $this -> db -> select('amount');

   $this -> db -> from('expenses');
     

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->where('idno',$invoiceno);
	  $this->db->where('companyid',$companyid);
$this->db->group_by('idno');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

	public function expensesperinvoiceOLD($invoiceno, $companyid)
{
	
		 $sql = "SELECT amount, SUM(amount) AS amount FROM  expenses WHERE 	companyid	 = '$companyid'  and idno = '$invoiceno' group by idno   ";
	///$this->db->group_by('idno');

   
	$q = $this->db->query($sql);
if($q->num_rows()>0){

   return $q->result_array();
   


}else{

  return false;
}
}
	
	
	
		
	
function getinvoice($table,$where, $value){
 
$this->db->select(" dateposted, userid,mytable,name,customer,description,credit,total");

 $this -> db -> limit(1);



   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();

 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

	
	
	public function balanceperinvoice($customer, $companyid)
{
	
		 $sql = "SELECT balance, SUM(balance) AS balance FROM  sales  WHERE 	companyid	 = '$companyid'  and customer = '$customer'   ";
	//$this->db->group_by('idno');

   
	$q = $this->db->query($sql);
if($q->num_rows()>0){

   return $q->result_array();
   


}else{

  return false;
}
}
	
	
	
	
	
	
	
	
public function displayallourexpenses($startdate,$closedate, $companyid)
{
//$this->db->select("  * ,amount, SUM(amount) AS amount ");



$this->db->where('companyid', $companyid);


$this->db->where('datespent >=', $startdate);
$this->db->where('datespent <=', $closedate);
	


//$this->db->where('status <>',"returned");

$this->db->order_by('datespent','DESC');

$this->db->from('expenses');





//$this->db->group_by('sales.invoice');
	



$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


	
	
	
	
	
	
	
	
	
	
	
public function displayallourexpenses222($startdate,$closedate, $companyid)
{
	echo $closedate;
	exit;
	//	 $sql = "SELECT * ,amount, SUM(amount) AS amount FROM  expenses WHERE 	companyid	 = '$companyid'  and datespent BETWEEN  '$startdate'  AND  '$closedate'  ";
	//$this->db->group_by('companyid');

$this->db->where('companyid', $companyid);


$this->db->where('datespent >=', $startdate);
$this->db->where('datespent <=', $closedate);
   
    $this->db->order_by('datespent','DESC');

	$q =$this->db->from('expenses');
	//$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


	
function count_allsalesbydate($tablename,$where,$value,$startdate,$enddate,$branchid,$maincategory,$companyid) 
	{
		
		  if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate' and companyid='$companyid'  )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid' and companyid='$companyid'  )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value!="all")){
				$this->db->where("( ".$where."  ='$value'    and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'  and companyid='$companyid'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value=="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  and companyid='$companyid'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value!="all")){
				$this->db->where("( ".$where."  ='$value'    and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and companyid='$companyid'   )", NULL, FALSE);

		}else{
				$this->db->where("( ".$where."  ='$value'    and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and maincategory='$maincategory' and companyid='$companyid' )", NULL, FALSE);

		}
		
	  
	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	function count_dailycollectionsbydate($tablename,$companyid,$branchid,$startdate,$enddate) 
	{
		
		 
		
		if($branchid=="all"){
		
	$this->db->where("( companyid='$companyid'  and  entrydate BETWEEN '$startdate' and '$enddate'   )", NULL, FALSE);

	}else{

	$this->db->where("( branchid='$branchid'  and  companyid='$companyid'  and  entrydate BETWEEN '$startdate' and '$enddate'   )", NULL, FALSE);

//$this->db->select(" * from  dailycollections where   entrydate BETWEEN '$startdate'  and '$closedate' and companyid='$companyid'  ");

}
	  
	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	

	function count_allsalesbydategroup11($tablename,$where,$value,$startdate,$enddate,$branchid,$maincategory,$paymentstatus) 
	{
		
		 
	  if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")&& ($paymentstatus=="all") ){
		  
					 $this->db->select("  * ,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance ");

				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		$this->db->group_by('userid');
	

		}else if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")&& ($paymentstatus!="all") ){
		  
					 $this->db->select("  * ,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance ");
	$this->db->where("( paymentstatus='$paymentstatus'   )", NULL, FALSE);
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		$this->db->group_by('userid');
	

		}else if(($maincategory=="all")&& ($value=="all") && ($paymentstatus=="all")){
		
		 $this->db->select("  * ,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance ");

		$this->db->group_by('userid');
	$this->db->group_by('branch');
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  )", NULL, FALSE);

		}
		
		else if(($maincategory=="all")&& ($value=="all") && ($paymentstatus!="all")){
			
		
		 $this->db->select("  * ,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance ");
	$this->db->where("( paymentstatus='$paymentstatus'   )", NULL, FALSE);

	
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value!="all") && ($paymentstatus=="all")){
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		}
		
		else if(($maincategory=="all")&& ($value!="all") && ($paymentstatus!="all")){
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);
	$this->db->where("( paymentstatus='$paymentstatus'   )", NULL, FALSE);

		}  else if(($maincategory !="all")&& ($value=="all") && ($paymentstatus=="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'   )", NULL, FALSE);

		}
		
		
		else if(($maincategory !="all")&& ($value=="all") && ($paymentstatus!="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'   )", NULL, FALSE);
$this->db->where("( paymentstatus='$paymentstatus'   )", NULL, FALSE);
		} else if(($maincategory !="all")&& ($value!="all") && ($paymentstatus=="all")){
				$this->db->where("( userid='$value'  and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value!="all") && ($paymentstatus!="all")){
				$this->db->where("( userid='$value'  and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);
$this->db->where("( paymentstatus='$paymentstatus'   )", NULL, FALSE);
		}
		
		else{
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and maincategory='$maincategory'   and  and paymentstatus='$paymentstatus' )", NULL, FALSE);

		}
		
	  //
	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
	function count_allmpesasalesbydate($tablename,$where,$value,$startdate,$enddate,$branchid) 
	{
		if(($branchid=="all")&& ($value=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		}else if(($branchid=="all")&& ($value!="all")){
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		} else if(($branchid !="all")&& ($value=="all")){
				$this->db->where("( branch='$branchid'  and dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		} else if(($branchid !="all")&& ($value!="all")){
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		}else{
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' )", NULL, FALSE);

		}
		

	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
		
	public function displayallmpesasalesbydate($limit, $start,$database,$where,$value,$startdate,$enddate,$branchid,$download) {
      			
	if($download=="yes"){
	  		  	$this->db->select('mpesasales.amount, mpesasales.code,mpesasales.dateposted,firstname', FALSE);
				$this->db->join('people', 'mpesasales.userid = people.id','INNER JOIN');
	}
	  
if(($branchid=="all")&& ($value=="all")){
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		}else if(($branchid=="all")&& ($value!="all")){
				$this->db->where("( mpesasales.userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		} else if(($branchid !="all")&& ($value=="all")){
				$this->db->where("( mpesasales.branch='$branchid'  and dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		} else if(($branchid !="all")&& ($value!="all")){
				$this->db->where("( mpesasales.userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		}else{
				$this->db->where("( mpesasales.userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' )", NULL, FALSE);

		}

	
		$this->db->order_by('mpesasales.id','DESC');

        $query = $this->db->get($database);

       
			
			
			
			
	if($download=="yes"){
			 if ($query->num_rows() > 0) {	
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Mpesa sales.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($query, $delimiter, $newline);

        force_download($filename, $data);
			 }
		
	}else{
			 if ($query->num_rows() > 0) {
		
		  foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
			}
        return false;
		
	}
	
			
          
      
   }
   
	
	   function populatedataloggs($id){


$this->db->select(" * from  loggs where userid= '$id' ");
$this -> db -> limit(1);
 $this->db->order_by('id','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}

	
	
	
	public function displayallmpesapendingsalesbydate($limit, $start,$database,$startdate,$enddate,$branchid) {
      
	  
	  
	  	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}	
		
	  
	$this->db->where("( exactime BETWEEN '$startdate'  and '$enddate'  and used='2' and amount >0 )", NULL, FALSE);
	$this->db->where("(sender_number !='to' or sender_number !='received'  )", NULL, FALSE);

				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
		function count_allpendingmpesasalesbydate($tablename,$startdate,$enddate,$branchid) 
	{

	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}	
				
				
				$this->db->where("( exactime BETWEEN '$startdate'  and '$enddate' and used='2'  and amount >0 )", NULL, FALSE);

				$this->db->where("(sender_number !='to' or sender_number !='received'  )", NULL, FALSE);


	
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
			
public function displayallsalesbydategroupNew($limit, $start,$database,$companyid,$userid,$startdate,$enddate,$branchid,$maincategory,$paymentstatus,$group,$type)
 {


//echo $type;
//exit;

	if($branchid !="all"){
$this->db->where('branch', $branchid);
	}
		if($userid !="all"){
$this->db->where('userid', $userid);

	}

$this->db->where('dateposted >=', $startdate);
$this->db->where('dateposted <=', $enddate);
 		 	
			if($group =="yes"){

		
			$this->db->select(' *,id, COUNT(id) AS counter', FALSE);

 	  //$this->db->where('status <>','returned');

	//$this->db->group_by('dateposted');
			$this->db->group_by('companyid');
	$this->db->group_by('userid');
	
}


$this->db->from('sales');









$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

	
	
	
		function count_allsalesbydategroup($tablename,$where,$value,$startdate,$enddate,$branchid,$maincategory,$paymentstatus,$group,$clearedby) 
	
 {




	if($branchid !="all"){
$this->db->where('branch', $branchid);
	}
		if($value !="all"){
$this->db->where('userid', $value);

	}
if($clearedby !="all"){
	
$this->db->where('updatedby',$clearedby);

	}

$this->db->where('dateposted >=', $startdate);
$this->db->where('dateposted <=', $enddate);

 	  //$this->db->where('status <>','returned');


	
	 	
			if($group =="yes"){

		
			$this->db->select(' *,id, COUNT(id) AS counter', FALSE);

 	  //$this->db->where('status <>','returned');

	//$this->db->group_by('dateposted');
			$this->db->group_by('companyid');
	$this->db->group_by('userid');
	
}



$this->db->from('sales');
		return $this->db->count_all_results();






}

	
	
	
	
		
public function displayallsalesbydategroup($limit, $start,$database,$companyid,$userid,$startdate,$enddate,$branchid,$maincategory,$paymentstatus,$group,$type,$clearedby)
 {

//
	
	if($type !="all"){
		
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}

	if($branchid !="all"){
$this->db->where('branch', $branchid);
	}
		if($maincategory !="all"){
$this->db->where('maincategory', $maincategory);
	}

if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	if($clearedby !="all"){
$this->db->where('updatedby', $clearedby);
	}
	

$this->db->where('companyid',$companyid);

$this->db->where('dateposted >=', $startdate);
$this->db->where('dateposted <=', $enddate);
 

 //echo$group;
 //exit;
if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 

	 
	 	$this->db->select('invoice, maindiscount,id,salestype,type,userid', FALSE);
	 	$this->db->select('total, SUM(total) AS total', FALSE);
 	$this->db->select('id, COUNT(id) AS counter', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);
 	$this->db->select('mytable,receiptno,updatedat,balance,description,cashier,customer,branch,houseno,paymentstatus,updatedby,othercurrencyamount,othercurrency,currency,name', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);
							 
							 
							 
							 

//echo $group;
//exit;
if(($group =="yes") || ($group =="Perfomance") ||( $group=="yesbydate")){
	
if($group=="yesbydate"){
		$this->db->group_by('mydateposted');

}
		$this->db->group_by('userid');

}else{
	
	$this->db->group_by('mydateposted');
	
}


			$this->db->group_by('companyid');
				if($branchid !="all"){
	$this->db->group_by('branch');
	}
			
			
			
	

	}else{
		  	$this->db->select(' *, dateposted AS mydateposted', FALSE);

	}
 $this->db->order_by("id","Desc");


$this->db->from('sales');









$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

	
	
	
	
	
	
	
	
	
	
	
public function displayallsalesbydatecbycategory($limit, $start,$database,$companyid,$userid,$startdate,$enddate,$branchid,$category,$paymentstatus,$group,$type)
 {

//

		
			if($category !="all"){
		
		$this->db->where("grouping.companyid", $companyid);

		$this->db->where("grouping.categoryid", $category);

$this->db->from('grouping');


		$this->db->join('sales', 'grouping.memberid = sales.customer','INNER JOIN');
		
		
		
		
			}else{
				
			$this->db->from('sales');
	
				
			}
		
			if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
			//exit;
									//	$this->db->where("(type !='Invoice'  OR type !='Credit'  )", NULL, FALSE);
$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
$this->db->where('type', $type);

		}
			}

	

	if($branchid !="all"){
$this->db->where('sales.branch', $branchid);
	}
	

if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	

	


$this->db->where('sales.companyid',$companyid);

$this->db->where('sales.dateposted >=', $startdate);
$this->db->where('sales.dateposted <=', $enddate);
 
 
 
 if($group !="no"){
		$this->db->group_by('customer');
//$this->db->select("  * ,amount , SUM(amount) AS myamount ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");
					 $this->db->select("  * ,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance,COUNT(invoice) As counter ");

		$this->db->group_by('branch');
 $this->db->order_by('counter','DESC');

}else{
	
 $this->db->order_by('dateposted','DESC');
	
}



//$this->db->from('sales');









$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
   
   
   
   public function displaydailytables($database,$companyid,$branchid,$startdate,$enddate,$mytable,$paymentstatus) {
      
	
	  
	
		
		if($branchid=="all"){
		
	$this->db->where("( companyid='$companyid'  and  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

	}else{

	$this->db->where("( branch='$branchid'  and  companyid='$companyid'  and  dateposted BETWEEN '$startdate' and '$enddate'   )", NULL, FALSE);

//$this->db->select(" * from  dailycollections where   entrydate BETWEEN '$startdate'  and '$closedate' and companyid='$companyid'  ");

}



if($mytable !="all"){
	$this->db->where('mytable',$mytable);
	
	}

	if($paymentstatus !="all"){
			$this->db->where('paymentstatus',$paymentstatus);

	
	}



	
	 $this->db->order_by('id','DESC');
	  
	  

				

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   
   
   
	public function displaydailycollectionbydate($limit, $start,$database,$companyid,$branchid,$startdate,$enddate) {
      
	
	  
		
		
		if($branchid=="all"){
		
	$this->db->where("( companyid='$companyid'  and  entrydate BETWEEN '$startdate' and '$enddate'   )", NULL, FALSE);

	}else{

	$this->db->where("( branchid='$branchid'  and  companyid='$companyid'  and  entrydate BETWEEN '$startdate' and '$enddate'   )", NULL, FALSE);

//$this->db->select(" * from  dailycollections where   entrydate BETWEEN '$startdate'  and '$closedate' and companyid='$companyid'  ");

}
	
	 $this->db->order_by('id','DESC');
	  
	  

				

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
	
	public function displayallsalesbydate2($limit, $start,$database,$where,$value,$startdate,$enddate,$branchid,$maincategory) {
      
	
	  if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")){
		  
		
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'   )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value=="all")){
		
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value!="all")){
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value=="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'   )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value!="all")){
				$this->db->where("( userid='$value'  and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'   )", NULL, FALSE);

		}else{
				$this->db->where("( userid='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and maincategory='$maincategory' )", NULL, FALSE);

		}
		
	  
	  

				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
	
   
   
   
   
   
   
   	public function displayallsalesbydatecashier($limit, $start,$database,$companyid,$value,$startdate,$enddate,$branchid,$maincategory) {
      
 $this->db->limit($limit, $start);
	 $this->db->where('dateposted >=', $startdate);
			$this->db->where('dateposted <=',$enddate);

						if($maincategory!="all") {
			$this->db->where('maincategory', $maincategory);
		
				}
	  
	  		
				if($branchid!="all") {
			$this->db->where('branch', $branchid);
		
				}
	  
	
	
				if($companyid!="all") {
			$this->db->where('companyid', $value);
		
				}
	
	
			$this->db->where('paymentstatus <>', "Cleared");
		
			
	
	
	  
	  
					$this->db->order_by('userid','DESC');
				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   
   
   
   
   
   
   
   	public function displayallsalesbytype($limit, $start,$database,$where,$value,$startdate,$enddate,$maincategory,$companyid) {
     //$this->db->where('dateposted <=', $startdate);
			//$this->db->where('dateposted <=',$enddate);
			$this->db->where('type',$maincategory);
			$this->db->where('companyid',$companyid);

				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'    )", NULL, FALSE);

		
		
	  
	  
				 $this->db->order_by('id','DESC');
				 $this->db->order_by('dateposted','DESC');
				 

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   //
   
   
   
   
      
   	public function displayallsalesbydateCashiersales($limit, $start,$database,$where,$value,$startdate,$enddate,$branchid,$maincategory,$companyid) {
      
	  
	          $this->db->limit($limit, $start);
	  	 $this->db->where('dateposted >=', $startdate);
			$this->db->where('dateposted <=',$enddate);

						if($maincategory!="all") {
			$this->db->where('maincategory', $maincategory);
		
				}
	  
	  		if($value!="all") {
			$this->db->where($where, $value);
		
				}

	  	if($branchid!="all") {
			$this->db->where('branch', $branchid);
				

				}
	  
	  
	  			$this->db->where('companyid', $companyid);
	  			//	$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate' and companyid='$companyid'   )", NULL, FALSE);


	  
				
				// $this->db->order_by('dateposted','desc');
				  $this->db->order_by('id','desc');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   	public function displayallsalesbydate($limit, $start,$database,$where,$value,$startdate,$enddate,$branchid,$maincategory,$companyid) {
      
	  
	          $this->db->limit($limit, $start);
	  	 $this->db->where('dateposted >=', $startdate);
			$this->db->where('dateposted <=',$enddate);

						if($maincategory!="all") {
			$this->db->where('maincategory', $maincategory);
		
				}
	  
	  		if($value!="all") {
			$this->db->where($where, $value);
		
				}

	  	if($branchid!="all") {
			$this->db->where('branch', $branchid);
				

				}
	  
	  
	  			$this->db->where('companyid', $companyid);
	  			//	$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate' and companyid='$companyid'   )", NULL, FALSE);


	  
				
				 $this->db->order_by('dateposted','asc');
				  $this->db->order_by('id','asc');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   
   
   	public function displayallsalesbyamountlimit($database,$where,$value,$startdate,$enddate,$branchid,$maincategory,$companyid) {
      
	
	  if(($maincategory=="all")&& ($value=="all") && ($branchid=="all")){
		  
		
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate' and companyid='$companyid'   )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value=="all")){
		
				$this->db->where("(  dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  and companyid='$companyid' )", NULL, FALSE);

		}else if(($maincategory=="all")&& ($value!="all")){
			
				$this->db->where("(   ".$where."  ='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid'  and companyid='$companyid'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value=="all")){
				$this->db->where("( maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate'  and branch='$branchid'  and companyid='$companyid'  )", NULL, FALSE);

		} else if(($maincategory !="all")&& ($value!="all")){
				$this->db->where("(  ".$where."  ='$value'    and maincategory='$maincategory'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and companyid='$companyid'   )", NULL, FALSE);

		}else{
				$this->db->where("( ".$where."  ='$value'  and dateposted BETWEEN '$startdate'  and '$enddate' and branch='$branchid' and maincategory='$maincategory' and companyid='$companyid'  )", NULL, FALSE);

		}
						 $this->db->select("  * ,total, SUM(total) AS mytotal ");

	  
	  
		$this->db->group_by('userid');
		$this->db->group_by('branch');
				 $this->db->order_by('id','DESC');
				 $this->db->order_by('dateposted','DESC');
				 
//$query = $this->db->get('questions')
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
             $row = $query->row(); 
    return $row->mytotal;
        }
        return false;
   }
   
   
   
   
   
public function displayallbydate($limit, $start,$database,$where,$value,$where1,$mydate) {
       // $this->db->limit($limit, $start);
		$this->db->where($where, $value);
						

						$this -> db -> like($where1, $mydate);

				//$this->db->where('date >=', $myadate);
				//$this->db->where('date <=',$myadate);

				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   


   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
public function displayallbydateorder($limit, $start,$database,$where,$value,$fromdate,$todate,$where1,$value1) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
				$this->db->where($where1, $value1);
//
				$this -> db -> where("ordersales", "new");
			//$this->db->where("(ordersales ='new' or ordersales ='Served' or paymentstatus='No'  )", NULL, FALSE);

						

					//	$this -> db -> like($where1, $mydate);

				$this->db->where('dateposted >=', $fromdate);
				$this->db->where('dateposted <=',$todate);

				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   


public function displayallbydateorderHold($limit, $start,$database,$where,$value,$fromdate,$todate,$type) {
        //$this->db->limit($limit, $start);
		$this->db->where($where, $value);
				//	$this -> db -> where("ordersales", "new");
			//$this->db->where("(ordersales ='new' or ordersales ='Served'  )", NULL, FALSE);

						

					//	$this -> db -> like($where1, $mydate);

				//$this->db->where('dateposted >=', $fromdate);
				$this->db->where('dateposted <=',$todate);

				 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   



public function pendingorder($database,$where,$value,$where1,$value1) {
      
		$this->db->where($where, $value);
				$this->db->where($where1, $value1);

		$this -> db -> where("ordersales", "new");

				 $this->db->order_by('id','ASC');
				 $this->db->group_by('mytable');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
              return $query->result();

        }
        return false;
   }
   




function populateimages($id){

if($id!=null){
$this->db->select("photo from people where id='$id'");

$bioquery1 = $this -> db -> get();
if($bioquery1->num_rows()>0){

   return $bioquery1->result();
  return true;
}else{

  return false;
}

}

}
function deletebyid($id,$tablename){

if($id!=null){

 $this->db->where('id',$id);
 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}





function deletebyname($column,$value,$tablename){

if($column!=null){

 $this->db->where($column,$value);
 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}










function cleardata($tablename){


 //$deleted=("delete * from  coursecodes where id >0");
	$this->db->where("(userid>0)", NULL, FALSE);

 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}




}





function viewlowproductbycode($code){

if($code!=null){
	

$this->db->select(" * from  products where   reorderevel>qty  and code='$code' or reorderevel=qty  and code='$code'");
	

	
	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}





function viewlowproductbycodestore($code){

if($code!=null){
	

$this->db->select(" * from  productlist where   code='$code' and reorderevel>qty   or  code='$code' and  reorderevel=qty  ");
	

	
	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}














function viewlowproductbysupplier($supplier,$orderamount,$companyid,$download,$category){

if($supplier!=null){
	
		
	if($download=="yes"){
$this->db->select("products.name,products.code, products.reorderquantity AS qty,products.buyingprice,productlist.buyingprice ,productlist.qty as QtyAvailable");

	}else{
	$this->db->select("  *  ");
	
		
	}

	
	 $this -> db -> from('products');
   
   
	if($orderamount !="all"){
  $this->db->where('orderamount',$orderamount);
	}
		if($supplier !="all"){
  $this->db->where('supplier',$supplier);
	}
	
		if($category !="all"){
  $this->db->where('category',$category);
	}



  $this->db->where('companyid',$companyid);
	
	
	
	 	$this->db->where("(reorderevel>qty  OR reorderevel=qty  )", NULL, FALSE);


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){




	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Low Store Items Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}
	






   


}else{

  return false;
}

}else{

  return false;
}


}




















function populatecollection($fromdate,$todate,$companyid,$branchname,$group){
			//	$data['allcollections'] = $this->usermodel->populatecollection($fromdate,$todate,$companyid,$branchname,"yes");

$this->db->select(" * ,collection.paiddate, collection.id AS id,collection.invoice,collection.amount,collection.remarks,collection.paymentmethodes,collection.transactioncode,collection.supplier,collection.createdate,collection.balance");


$this->db->where('collection.name', "payment");

	

	if($branchname !="all") {
$this->db->where('collection.branchid',$branchname);


	}
	
$this->db->where('collection.companyid',$companyid);
	



$this->db->where('paiddate >=', $fromdate);

$this->db->where('paiddate <=', $todate);
					$this->db->from('collection');


		$this->db->join('sales', 'collection.invoice = sales.invoice','INNER JOIN');
		//$this->db->join('productcategory', 'productlist.category = productcategory.id','INNER JOIN');
//$this->db->where('collection.invoice <>', "sales.invoice");
//$this->db->where("(collection.invoice != sales.invoice  )", NULL, FALSE);

$this->db->where('dateposted <= ', $fromdate);

//$this->db->where('dateposted >=', $todate);

	 $this->db->order_by('collection.id','desc');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	











function populatecollection2($fromdate,$todate,$companyid){

if($fromdate!=null){
$this->db->select(" * from  collection where   paiddate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and name='payment' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}







function populatecollectionsales($fromdate,$todate,$companyid,$type,$branch,$group){


	
	//$this->db->limit($limit, $start);
//$this->db->select("  * ,collection.id  AS id, ");
$this->db->select(" * ,collection.paiddate, collection.id AS id,collection.invoice,collection.amount,collection.remarks,collection.paymentmethodes,collection.transactioncode,collection.supplier,collection.createdate,collection.balance");



/*
 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('paiddate, WEEK(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('paiddate, MONTH(paiddate) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('paiddate, YEAR(paiddate) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 	$this->db->select('collection.amount, SUM(collection.amount) AS amount', FALSE);

	 	$this->db->select('collection.balance, SUM(collection.balance) AS balance', FALSE);



	//$this->db->group_by('mydateposted');
			//$this->db->group_by('collection.companyid');
			
				if($branch !="all"){
	//$this->db->group_by('collection.branchid');
	}
			
			
			
	

	}
	*/
	
$this->db->select('collection.amount, SUM(collection.amount) AS amount', FALSE);

	 	$this->db->select('collection.balance, SUM(collection.balance) AS balance', FALSE);

	$this->db->where('collection.name', $type);

 $this->db->group_by('collection.remarks');






	if($branch !="all"){
$this->db->where('collection.branchid', $branch);
	}
	
	
	
	
	
	
	
	
$this->db->where('collection.companyid', $companyid);

$this->db->where('paiddate >=', $fromdate);
$this->db->where('paiddate <=', $todate);
$this->db->from('collection');


		$this->db->join('sales', 'collection.invoice = sales.invoice','INNER JOIN');


$this->db->where('dateposted <= ', $fromdate);


	 $this->db->order_by('collection.id','desc');



	
	// $this->db->order_by('collection.paiddate','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

		
		
		
		
		return $bioquery->result();

		
	

   


}else{

  return false;
}




}


function populatecollectionbytype($fromdate,$todate,$companyid,$type,$branch,$group,$download){


	
	//$this->db->limit($limit, $start);
$this->db->select("  * ,collection.id  AS id, ");




 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('paiddate, WEEK(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('paiddate, MONTH(paiddate) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('paiddate, YEAR(paiddate) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 	$this->db->select('amount, SUM(amount) AS amount', FALSE);

	 	$this->db->select('balance, SUM(balance) AS balance', FALSE);



	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branchid');
	}
			
			
			
	

	}
	$this->db->where('name', $type);

 






	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
	
	
	
	
	
	
	
	
$this->db->where('collection.companyid', $companyid);

$this->db->where('paiddate >=', $fromdate);
$this->db->where('paiddate <=', $todate);
$this->db->from('collection');





	
	 $this->db->order_by('collection.paiddate','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Collection Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}
   
		
		
		

		
	

   


}else{

  return false;
}




}



function populateroomcollectionbytype($fromdate,$todate,$companyid,$type,$branch,$group){


	
	//$this->db->limit($limit, $start);
$this->db->select("  * ,roomcollection.id  AS id, ");




 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('paiddate, WEEK(paiddate) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('paiddate, MONTH(paiddate) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('paiddate, YEAR(paiddate) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('paiddate, DATE(paiddate) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 	$this->db->select('amount, SUM(amount) AS amount', FALSE);

	 	$this->db->select('balance, SUM(balance) AS balance', FALSE);



	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branchid');
	}
			
			
			
	

	}
	$this->db->where('name', $type);

 






	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
	
	
	
	
	
	
	
	
$this->db->where('roomcollection.companyid', $companyid);

$this->db->where('paiddate >=', $fromdate);
$this->db->where('paiddate <=', $todate);
$this->db->from('roomcollection');





	
	 $this->db->order_by('roomcollection.paiddate','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

		
		
		
		
		return $bioquery->result();

		
	

   


}else{

  return false;
}




}



function populatecollectionbytype_______2($fromdate,$todate,$companyid,$type){


if($fromdate!=null){
$this->db->select(" * from  collection where   paiddate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and name='$type'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}


function populateexpenseMAIN($fromdate,$todate,$branch,$group){

if($fromdate!=null){
	
	
	if($branch!="all"){
		$this->db->select(" * ,amount, SUM(amount) AS myamount from  expenses where   datespent BETWEEN '$fromdate'  and '$todate' and branchid='$branch'  and type='Expense'");

	}else{
		$this->db->select(" * ,amount, SUM(amount) AS myamount from  expenses where   datespent BETWEEN '$fromdate'  and '$todate'   and type='Expense'");

		
	}
	

//$this->db->select("    * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2   from  sales_order where   date BETWEEN '$fromdate'  and '$todate' and category='$category'  and status !='returned'  companyid='$companyid'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}






function populateexpense($fromdate,$todate,$companyid,$branch,$group){


	
	//$this->db->limit($limit, $start);
$this->db->select("  * ,expenses.id  AS id, ");




 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('datespent, DATE(datespent) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('datespent, WEEK(datespent) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('datespent, MONTH(datespent) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('datespent, YEAR(datespent) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('datespent, DATE(datespent) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 	$this->db->select('amount, SUM(amount) AS amount', FALSE);




	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branchid');
	}
			
			
			
	

	}
	$this->db->where('type', "Expense");

 






	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
	
	
	
	
	
	
	
	
$this->db->where('expenses.companyid', $companyid);

$this->db->where('datespent >=', $fromdate);
$this->db->where('datespent <=', $todate);
$this->db->from('expenses');





	
	 $this->db->order_by('expenses.datespent','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

		
		
		
		
		return $bioquery->result();

		
	

   


}else{

  return false;
}




}






function populateexpenseALL($fromdate,$todate,$companyid){

if($fromdate!=null){
$this->db->select(" * ,amount, SUM(amount) AS myamount from  expenses where   datespent BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and type='Expense'");

//$this->db->select("    * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2   from  sales_order where   date BETWEEN '$fromdate'  and '$todate' and category='$category'  and status !='returned'  companyid='$companyid'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}



function countbranches($companyid){

if($companyid!=null){

		$this->db->select(" * from  branch where  companyid='$companyid'  ");


	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->num_rows();
   


}else{

  return false;
}

}else{

  return false;
}


}








function viewlowproductbysupplierstore($supplier,$orderamount,$companyid,$branchid,$download,$category){

if($supplier!=null){
	
		if($download=="yes"){
$this->db->select("productlist.name,productlist.code, productlist.reorderquantity AS qty,productlist.buyingprice,productlist.qty as QtyAvailable");

	}else{
	$this->db->select("  *  ");
	
		
	}

	
	 $this -> db -> from('productlist');
   
   
	if($orderamount !="all"){
  $this->db->where('orderamount',$orderamount);
	}
		if($supplier !="all"){
  $this->db->where('supplier',$supplier);
	}
	if($branchid !="all"){
  $this->db->where('branch',$branchid);
	}
	if($category !="all"){
  $this->db->where('category',$category);
	}

  $this->db->where('companyid',$companyid);
	
	 $this->db->order_by('supplier','DESC');

	
	 	$this->db->where("(reorderevel>qty  OR reorderevel=qty  )", NULL, FALSE);


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){




	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Low Stock Items Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}
   


}else{

  return false;
}

}else{

  return false;
}


}

function viewppendimgbillssearchallexpenses($searchterm,$companyid,$branchid,$finalstartdate,$finalclosetdate, $download){

 

   $this -> db -> from('expenses');
   
   
	if($branchid !="all"){
  $this->db->where('branchid',$branchid);
	}
		if($searchterm !="all"){
  $this->db->where('supplier',$searchterm);
	}


$this->db->where('datespent >=', $finalstartdate);
$this->db->where('datespent <=', $finalclosetdate);

  $this->db->where("type","Bill");




  $this->db->where('companyid',$companyid);


  
	 $this->db->order_by('id','DESC');
   //$this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}







function viewproductbysubcategory($fromdate,$todate,$type,$companyid,$category,$branch){

if($fromdate!=null){
	
	
	 if($branch=="all"){
$this->db->select("    * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2   from  sales_order where   date BETWEEN '$fromdate'  and '$todate' and category='$category'  and status !='returned'  companyid='$companyid'");
	
	}else{
	
$this->db->select("    * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2   from  sales_order where  branch='$branch' and   date BETWEEN '$fromdate'  and '$todate' and category='$category'  and status !='returned' and companyid='$companyid' ");
	
	}
	
	$this->db->group_by('bprice');

		$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');

	 $this->db->order_by('id','desc');
 $this->db->order_by('date','DESC');	
$bioquery = $this -> db -> get();

			
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}














function viewproductsalesreport($fromdate,$todate,$type,$companyid,$code,$branch){

$this->db->select("  * ,amount , SUM(amount) AS myamount,qty, SUM(qty)  AS myqty ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");



	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
$this->db->where('type', $type);
	}

if($code !="all"){
$this->db->where('code', $code);
	}
	
	


$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
 $this->db->order_by('dateposted','DESC');

$this->db->from('sales_order');






		$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');
	



$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function viewproductsalesreport_1($fromdate,$todate,$type,$companyid,$code,$branch){

if($fromdate!=null){
	
	if(($type=="all")&& ($branch=="all")){
$this->db->select("   * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	
	}else if(($type=="all")&& ($branch!="all")){
$this->db->select("    * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where  branch='$branch' and   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	}else if(($type!="all")&& ($branch=="all")){
$this->db->select("     * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from   sales_order where  type ='$type'  and   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	}else{

$this->db->select("     * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from   sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and code='$code'  and status<>'returned'      ");
}
	

	
		//$this->db->group_by('bprice');

		$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');

	
			 $this->db->order_by('dateposted','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}









function searchserialreport($companyid,$code,$branch,$category){

	if($code!=null){


	$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,tax2 AS mytax2  ");




	if($branch !="all"){
$this->db->where('branch', $branch);
	}

	if($category =="serial"){
$this->db->where('serial', $code);
	}else if($category =="code"){
$this->db->where('code', $code);
	}else if($category =="otherserial"){
$this->db->where('address', $code);
	}else{
	$this->db->where("(serial='$code' OR  code='$code' OR  address='$code'   )", NULL, FALSE);


	}
	

$this->db->where('companyid', $companyid);



 $this->db->order_by('dateposted','DESC');
  $this->db->order_by('status','DESC');
 	




$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}



function viewserialproductsalesreport($fromdate,$todate,$type,$companyid,$code,$branch,$daterange,$category,$group,$transactiontype){

	if($fromdate!=null){

if($group =="yes"){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

	}else{
	$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,tax2 AS mytax2  ");
	
	}
	



	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
$this->db->where('type', $type);
	}
	if($daterange =="yes"){
		
$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
	}
		if($category =="serial"){
$this->db->where('serial', $code);
	}else if($category =="otherserial"){
$this->db->where('address', $code);
	}else{
		$this->db->where('code', $code);

	}
		if($transactiontype =="Sold"){
		
$this->db->where('status <>',"returned");

	}

$this->db->where('companyid', $companyid);



 $this->db->order_by('dateposted','DESC');
  $this->db->order_by('status','DESC');
 	
	if($group =="yes"){
				$this->db->group_by('status');

		$this->db->group_by('sprice');
$this->db->group_by('code');
	$this->db->group_by('branch');
		$this->db->group_by('serial');
	}
	



$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}



function viewserialproductsalesreport_old($fromdate,$todate,$type,$companyid,$code,$branch){

if($fromdate!=null){
	
	if(($type=="all")&& ($branch=="all")){
$this->db->select("   * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and serial='$code' and status<>'returned' ");
	
	
	}else if(($type=="all")&& ($branch!="all")){
$this->db->select("    * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where  branch='$branch' and   dateposted BETWEEN '$fromdate'  and '$todate' and serial='$code' and status<>'returned' ");
	
	}else if(($type!="all")&& ($branch=="all")){
$this->db->select("     * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from   sales_order where  type ='$type'  and   dateposted BETWEEN '$fromdate'  and '$todate' and serial='$code' and status<>'returned' ");
	
	}else{

$this->db->select("     * ,amount AS myamount,qty  AS myqty ,tax AS mytax,tax2, SUM(tax2) AS mytax2  from   sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and serial='$code'  and status<>'returned'      ");
}
	//

	
		$this->db->group_by('bprice');

		$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');

	
			 $this->db->order_by('dateposted','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}



function viewproductsalesreport11($fromdate,$todate,$type,$companyid,$code,$branch){

if($fromdate!=null){
	
	if(($type=="all")&& ($branch=="all")){
$this->db->select("   * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax  from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	}else if(($type=="all")&& ($branch!="all")){
$this->db->select("   * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax from  sales_order where  branch='$branch' and   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	}else if(($type!="all")&& ($branch=="all")){
$this->db->select("   * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax from  sales_order where  type ='$type'  and   dateposted BETWEEN '$fromdate'  and '$todate' and code='$code' and status<>'returned' ");
	
	}else{

$this->db->select("   * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax  from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and code='$code'  and status<>'returned'      ");
}
	
	
		$this->db->group_by('code');
	$this->db->group_by('branch');
	
			 $this->db->order_by('dateposted','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}

function viewproductsalesreportbycustomers($fromdate,$todate,$type,$companyid,$code){

if($fromdate!=null){
	
	if($type=="all"){
$this->db->select(" * from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and invoice='$code'");
	
	}else
if($type==""){
$this->db->select(" * from  sales_order where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and invoice='$code'");
}else {
$this->db->select(" * from  sales_order where   type ='$type'   and dateposted BETWEEN '$fromdate'  and '$todate'and companyid='$companyid' and invoice='$code'");
	}
	
	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}



function viewproductsalesreportbyuser2($fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch){

	if($fromdate!=null){
if(($type=="all") && ($maincategory=="all")&& ($branch=="all") && ($userid=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax   from sales_order where    companyid='$companyid'    and dateposted BETWEEN '$fromdate'  and '$todate' and status<>'returned' ");
	} else if(($type=="all") && ($maincategory=="all")&& ($branch!="all") && ($userid=="all") ){
$this->db->select(" * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,,tax, SUM(tax) AS mytax  from sales_order where    companyid='$companyid'    and dateposted BETWEEN '$fromdate'  and '$todate' and status<>'returned' ");
	}
	
	
	else if(($type=="all") && ($maincategory !="all")&& ($branch!="all") && ($userid=="all") ){
$this->db->select(" * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax   from sales_order where    companyid='$companyid' and maincategory='$maincategory' AND branch ='$branch'    and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'");
	}
	
	
	else if(($type=="all") && ($maincategory=="all")&& ($branch=="all")  && ($userid !="all")){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax  from sales_order where    companyid='$companyid' and userid='$userid'  and dateposted BETWEEN '$fromdate'  and '$todate' and status<>'returned' ");
	}else  if(($type=="all") && ($maincategory=="all")&& ($branch!="all") && ($userid !="all") ){
				$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax    from  sales_order where   companyid='$companyid' and branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate'  and userid='$userid' and status<>'returned'");

//$this->db->select(" , amount, SUM(amount) AS myamount  * from  sales_order  where    companyid='$companyid' and branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate'  and userid='$userid'   ");
	}else if(($type!="all") && ($branch!="all") && ($maincategory=="all") && ($userid !="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax   from sales_order where   branch ='$branch' and type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned'");
}else if(($type!="all") && ($maincategory!="all") && ($branch=="all") && ($userid !="all")){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax    from  sales_order where   type ='$type'and maincategory='$maincategory' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned'");
}else if(($type=="all") && ($branch=="all") && ($maincategory!="all") && ($userid !="all")){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax    from  sales_order where    maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned'");

}else if(($type=="all") && ($branch!="all") && ($maincategory!="all") && ($userid !="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax    from sales_order where  branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned'");
}else if(($type!="all") && ($branch!="all") && ($maincategory!="all") && ($userid !="all") ){
$this->db->select("   * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax    from sales_order where  type ='$type' and branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned'");
}

	
	// $this->db->select('amount, SUM(amount) AS amount', FALSE);
	// $this->db->select('qty, SUM(qty) AS myqty', FALSE);
//$this->db->where("( SUM(amount) AS myamount )", NULL, FALSE);
	
	
	
	
	
	
	
	///
	
	
	
	$this->db->group_by('code');
	$this->db->group_by('branch');
	
			 $this->db->order_by('dateposted','DESC');

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}
function Gettallviewproductsalesreportbyuser($fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch,$group,$vatype){

	if($fromdate!=null){

if($vatype !="all"){
$this->db->where('taxindicator', $vatype);
	}


	if($branch !="all"){
$this->db->where('branch', $branch);
	}
	
	
	
		if($type !="all"){
			
			
$this->db->where('salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	

$this->db->where('companyid', $companyid);

$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
 $this->db->order_by('dateposted','DESC');
 

		
		 $this->db->select(" id  ");

	
	
 
 

 
						 	 $this->db->select('qty, SUM(qty) AS myqty', FALSE);
						 	 $this->db->select('tax, SUM(tax) AS mytax', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS mytax2', FALSE);
 
							$this->db->select('bprice, SUM(bprice) AS bprice', FALSE);
						 	 $this->db->select('sprice, SUM(sprice) AS sprice', FALSE);
							 $this->db->select('amount, SUM(amount) AS amount', FALSE);
							 $this->db->select('discount, SUM(discount) AS discount', FALSE);
							 $this->db->select('SUM(bprice*qty) AS ttbprice', FALSE);
							 $this->db->select('SUM(sprice*qty) AS ttbsprice', FALSE);


 



$this->db->from('sales_order');

 	$this->db->group_by('companyid');
 	//$this->db->group_by('branch');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}



}else{

  return false;
}


}
function Countallviewproductsalesreportbyuser($fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch,$group,$vatype){

	if($fromdate!=null){


	if($vatype !="all"){
$this->db->where('taxindicator', $vatype);
	}


	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
$this->db->where('salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	

$this->db->where('companyid', $companyid);

$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
 $this->db->order_by('dateposted','DESC');
 

		
		 $this->db->select(" id  ");

	
	
 
 
 	//$this->db->group_by('companyid');

 
 
 


$this->db->from('sales_order');




 //  return $bioquery->result();
   		return $this->db->count_all_results();






}else{

  return false;
}


}






function viewproductsalesreportbyuserDetails($limit, $start,$fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch,$group,$groupuser,$paymentstatus){

	if($fromdate!=null){

 $this->db->limit($limit, $start);



	if($branch !="all"){
$this->db->where('sales_order.branch', $branch);
	}
		if($type !="all"){
$this->db->where('sales_order.salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('sales_order.maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('sales_order.userid', $userid);
	}
	
	

$this->db->where('sales_order.companyid', $companyid);

$this->db->where('sales_order.status <>',"returned");

$this->db->where('sales_order.dateposted >=', $fromdate);
$this->db->where('sales_order.dateposted <=', $todate);
 
 if($group =="yes"){
 $this->db->select("  * ,sales_order.amount, SUM(sales_order.amount) AS myamount,sales_order.qty, SUM(sales_order.qty) AS myqty ,sales_order.tax, SUM(sales_order.tax) AS mytax ,SUM(sales_order.tax2) AS mytax2  ");

 	//$this->db->group_by('sales_order.sprice');
 	//$this->db->group_by('sales_order.bprice');

	$this->db->group_by('sales_order.code');
	$this->db->group_by('sales_order.branch');
		//$this->db->group_by('sales_order.serial');
		
 if($groupuser =="yes"){
	 	$this->db->group_by('sales_order.userid');
 
 }

	}else{
		
		 $this->db->select("  * ,sales_order.amount as myamount,sales_order.qty as myqty ,sales_order.tax as mytax ,sales_order.tax2 AS mytax2  ");

	}
	
	if($paymentstatus !="all"){
	 	$this->db->join('sales', 'sales_order.invoice = sales.invoice','INNER JOIN');

$this->db->where('sales.paymentstatus', $paymentstatus);
	}
	

 
 
 
 
 $this->db->order_by('sales_order.category','ASC');
 //$this->db->order_by('sales_order.dateposted','DESC');

$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}




function viewproductsalesreportbyuserDetailsMergedRooms($limit, $start,$fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch,$group,$groupuser,$paymentstatus){

	if($fromdate!=null){

 $this->db->limit($limit, $start);



	if($branch !="all"){
$this->db->where('dailycharges.branch', $branch);
	}
		if($type !="all"){
$this->db->where('dailycharges.salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('dailycharges.maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('dailycharges.userid', $userid);
	}
	
	

$this->db->where('dailycharges.companyid', $companyid);

$this->db->where('dailycharges.status <>',"returned");

$this->db->where('dailycharges.dateposted >=', $fromdate);
$this->db->where('dailycharges.dateposted <=', $todate);
 
 if($group =="yes"){
 $this->db->select("  * ,dailycharges.amount, SUM(dailycharges.amount) AS myamount,dailycharges.qty, SUM(dailycharges.qty) AS myqty ,dailycharges.tax, SUM(dailycharges.tax) AS mytax ,SUM(dailycharges.tax2) AS mytax2  ");

 	//$this->db->group_by('dailycharges.sprice');
 	//$this->db->group_by('dailycharges.bprice');

	$this->db->group_by('dailycharges.code');
	$this->db->group_by('dailycharges.branch');
		//$this->db->group_by('dailycharges.serial');
		
 if($groupuser =="yes"){
	 	$this->db->group_by('dailycharges.userid');
 
 }

	}else{
		
		 $this->db->select("  * ,dailycharges.amount as myamount,dailycharges.qty as myqty ,dailycharges.tax as mytax ,dailycharges.tax2 AS mytax2  ");

	}
	
	if($paymentstatus !="all"){
	 	$this->db->join('sales', 'dailycharges.invoice = sales.invoice','INNER JOIN');

$this->db->where('sales.paymentstatus', $paymentstatus);
	}
	

 
 
 $this->db->where('dailycharges.chargeid',"");

 

 $this->db->order_by('dailycharges.dateposted','DESC');

$this->db->from('dailycharges');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}













function viewproductsalesreportbyuser($limit, $start,$fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch,$group,$vatype){

	if($fromdate!=null){

 $this->db->limit($limit, $start);



	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
$this->db->where('salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	if($vatype !="all"){
$this->db->where('taxindicator', $vatype);
	}

$this->db->where('companyid', $companyid);

$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
 
 if($group =="yes"){
 $this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

 	$this->db->group_by('sprice');
 	$this->db->group_by('bprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
		$this->db->group_by('serial');
		


	}else{
		
		 $this->db->select("  * ,amount as myamount,qty as myqty ,tax as mytax ,tax2 AS mytax2  ");

	}
	
 
 
 
 
 
 

 $this->db->order_by('dateposted','DESC');

$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}





function viewproductsalesbycategory($fromdate,$todate,$companyid,$category,$branch,$group){

	if($fromdate!=null){


//

	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		
	if($category !="all"){
$this->db->where('category', $category);
	}

	

$this->db->where('companyid', $companyid);

$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
 $this->db->order_by('dateposted','DESC');
 
 
 if($group =="yes"){
 $this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

 	//$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
		$this->db->group_by('serial');

	}else{
		
		 $this->db->select("  * ,amount as myamount,qty as myqty ,tax as mytax ,tax2 AS mytax2  ");

	}
	
 
 
 
 
 
 


$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}







function viewleaseditemsmovement($fromdate,$todate,$group,$companyid,$status,$branch,$movement){

	if($fromdate!=null){
		
		
		if($group=="Yes"){
$this->db->select("  * ,amount, SUM(amount) AS amount,qty, SUM(qty) AS qty  ");

	
}else{
	$this->db->select("  * ,amount AS amount,qty  AS qty  ");

	
}




	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
		
	if($status !="all"){
$this->db->where('status', $status);
	}

	
	

$this->db->where('companyid', $companyid);


$this->db->where('dateleased >=', $fromdate);
$this->db->where('dateleased <=', $todate);
// $this->db->order_by('dateleased','DESC');
 
 	
if($group=="Yes"){
	
	$this->db->group_by('product');
	$this->db->group_by('branchid');
	
	
}


	if($movement=="Slow"){
		
		 $this->db->order_by('qty','ASC');

	}else{
		 $this->db->order_by('qty','DESC');

		
	}
	
$this->db->limit('10');

$this->db->from('lease');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}







function viewdeliveryitemsbycustomer($fromdate,$todate,$group,$companyid,$userid,$status,$branch){

	if($fromdate!=null){
		if($group=="Yes"){
$this->db->select("  * ,amount, SUM(amount) AS amount,qty, SUM(qty) AS qty  ");

	
}else{
	$this->db->select("  * ,amount AS amount,qty  AS qty  ");

	
}




	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
		
	if($status !="all"){
$this->db->where('status', $status);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	

$this->db->where('companyid', $companyid);


$this->db->where('dateposted >= ', $fromdate);
$this->db->where('dateposted <= ', $todate);
 $this->db->order_by('dateposted','DESC');
 
 	
if($group=="Yes"){
		$this->db->group_by('status');
	$this->db->group_by('product');
	$this->db->group_by('branchid');
	$this->db->group_by('user');
	
}



$this->db->from('parceldelivery');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}




function viewleaseditemsbycustomer($fromdate,$todate,$group,$companyid,$userid,$status,$branch){

	if($fromdate!=null){
		if($group=="Yes"){
$this->db->select("  * ,amount, SUM(amount) AS amount,qty, SUM(qty) AS qty  ");

	
}else{
	$this->db->select("  * ,amount AS amount,qty  AS qty  ");

	
}




	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
		
	if($status !="all"){
$this->db->where('status', $status);
	}
if($userid !="all"){
$this->db->where('incharge', $userid);
	}
	
	

$this->db->where('companyid', $companyid);


$this->db->where('dateleased >=', $fromdate);
$this->db->where('dateleased <=', $todate);
 $this->db->order_by('dateleased','DESC');
 
 	
if($group=="Yes"){
		$this->db->group_by('status');
	$this->db->group_by('product');
	$this->db->group_by('branchid');
	$this->db->group_by('user');
	
}



$this->db->from('lease');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}






function viewleaseditemsbyuser($fromdate,$todate,$group,$companyid,$userid,$status,$branch,$product){

	if($fromdate!=null){
		if($group=="Yes"){
$this->db->select("  * ,amount, SUM(amount) AS amount,qty, SUM(qty) AS qty  ");

	
}else{
	$this->db->select("  * ,amount AS amount,qty  AS qty  ");

	
}

if($product !="all"){

 	$this->db->where("(product='$product' OR  serial='$product'    )", NULL, FALSE);

	}
		


	if($branch !="all"){
$this->db->where('branchid', $branch);
	}
		
	if($status !="all"){
$this->db->where('status', $status);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	

$this->db->where('companyid', $companyid);


$this->db->where('dateleased >=', $fromdate);
$this->db->where('dateleased <=', $todate);
 $this->db->order_by('dateleased','DESC');
 
 	
if($group=="Yes"){
		$this->db->group_by('status');
	$this->db->group_by('product');
	$this->db->group_by('branchid');
	$this->db->group_by('user');
	
}



$this->db->from('lease');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}


function viewproductsalesreportbymyuser($fromdate,$todate,$type,$companyid,$userid,$maincategory,$branch)
{
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax ,tax2 AS mytax2 ");



	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
			
$this->db->where('salestype', $type);
	}
	if($maincategory !="all"){
$this->db->where('maincategory', $maincategory);
	}
if($userid !="all"){
$this->db->where('userid', $userid);
	}
	
	


$this->db->where('status <>',"returned");

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
			 $this->db->order_by('dateposted','DESC');

$this->db->from('sales_order');





//$this->db->group_by('sales.invoice');
	



$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function loginallusers($userid)

{

   $this -> db -> select('*');

   $this -> db -> from('people');


   $this -> db -> where('status', 1);
 	$this->db->where("(category='user' OR  category='reseller'   )", NULL, FALSE);

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

   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result();

   }

   else

   {

     return false;

   }

}
















	function getcashiers($password)

{

   $this -> db -> select('*');

   $this -> db -> from('people');


   $this -> db -> where('status', 1);
 	$this->db->where("(category='user' OR  category='reseller'   )", NULL, FALSE);

   //$this -> db -> where('password', ($password));
			$this->db->where("(BINARY  password ='$password'  )", NULL, FALSE);

   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result();

   }

   else

   {

     return false;

   }

}


function Getcardnumberholder($userid,$companyid)

{

   $this -> db -> select('*');
   //$this -> db -> select('id,savedpoints,savedcoins,');

   $this -> db -> from('people');
   $this -> db -> where('id', ($userid));

   $this -> db -> where('companyid', ($companyid));

   //$this -> db -> where('status', 1);
 //	$this->db->where("(address_1='$cardnumber' OR  phone='$cardnumber'   OR  idno='$cardnumber'   )", NULL, FALSE);


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}




function Getmembedetails($cardnumber,$phone,$idno,$vehicleno,$branch)

{

   $this -> db -> select('*');

   $this -> db -> from('gatepass');

  // $this -> db -> where('address_1', ($cardnumber));

   //$this -> db -> where('status', 1);
    if(trim($cardnumber)=="") {
	  	$this->db->where("( phone='".$phone."' and branchid='$branch'   OR  idno='".$idno."'and branchid='$branch'   )", NULL, FALSE);

	   
   }else{ 
  	    	$this->db->where("(subject='".$cardnumber."' and branchid='$branch' OR  phone='".$phone."' and branchid='$branch'   OR  idno='".$idno."'and branchid='$branch'   )", NULL, FALSE);
  	    	//$this->db->where("(subject='".$cardnumber."' and branchid='$branch' OR  phone='".$phone."' and branchid='$branch'   OR  idno='".$idno."'and branchid='$branch'   OR  vehicleno='".$vehicleno."'and branchid='$branch' )", NULL, FALSE);

	   
   }


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}







function Confirmcheckin($branchid,$idno)

{
  $currentdate = date('Y-m-d');
   $this -> db -> select('*');

   $this -> db -> from('checkin');
$this -> db -> where('idno',$idno);
   $this -> db -> where('branchid',$branchid);
$this->db->where('createdate', $currentdate);
$this->db->where('checkoutdate',"");


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}















function Getmembernumber($cardnumber)

{

   $this -> db -> select('*');

   $this -> db -> from('people');

  // $this -> db -> where('address_1', ($cardnumber));

   //$this -> db -> where('status', 1);
 	$this->db->where("(address_1='".$cardnumber."' OR  phone='".$cardnumber."'   OR  idno='".$cardnumber."'  OR  account='".$cardnumber."' )", NULL, FALSE);


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}





function Getcardnumber($cardnumber,$companyid)

{

   $this -> db -> select('*');

   $this -> db -> from('people');

   $this -> db -> where('companyid', ($companyid));

   //$this -> db -> where('status', 1);
 	$this->db->where("(address_1='$cardnumber' OR  phone='$cardnumber'   OR  idno='$cardnumber'   )", NULL, FALSE);


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}









function viewtimerreportbyuser($finalfromdate,$finaltodate,$companyid,$user,$branchname)
{
if($finalfromdate!=null){
		$this->db->select("  * ");

	if(($user=="all") && ($branchname=="all") ){
$this->db->where('startdate >=', $finalfromdate);
$this->db->where('enddate <=', $finaltodate);
	
	}else if(($user=="all") && ($branchname!="all") ){
$this->db->where('startdate >=', $finalfromdate);
$this->db->where('enddate <=', $finaltodate);
	$this->db->where('branchid', $branchname);

	}else if(($user!="all") && ($branchname=="all") ){
		
$this->db->where('startdate >=', $finalfromdate);
$this->db->where('enddate <=', $finaltodate);
	$this->db->where('branchid', $branchname);	
	$this->db->where('userid', $user);	
	

}

$this->db->from('timer');
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}




function viewproductgroupsalesreportbycustomer($fromdate,$todate,$type,$companyid,$customer,$branch,$group,$searchtype){
	
//$this->db->select("  * ,tax, SUM(tax) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'  ");

	if($branch !="all"){
$this->db->where('sales.branch', $branch);
	}
		if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(sales.type ='Invoice'  OR sales.type ='Credit'  )", NULL, FALSE);
	
		}else{
			//exit;
									//	$this->db->where("(type !='Invoice'  OR type !='Credit'  )", NULL, FALSE);
$this->db->where('sales.type <>', "Invoice");
$this->db->where('sales.type <>', "Credit");
		}


	}
	

if($searchtype=="Personnel"){
	
		if($customer !="all"){
	 	$this->db->where("(sales.salesperson='$customer' and   sales.salesperson !='0'   )", NULL, FALSE);
	}else{
			 	$this->db->where("(  sales.salesperson !='0'   )", NULL, FALSE);

	}

}else if($searchtype=="Supplier"){
					 	//$this->db->where("(products.supplier='$customer'  )", NULL, FALSE);

		//$this->db->join('products', 'sales_order.code = products.code','INNER JOIN');



}else{
	
	
	
		if(trim($customer) !="all"){
			
			$this->db->where('sales.customer', $customer);

			 	//$this->db->where("(sales.customer='$customer' OR  sales.name='$customer'   )", NULL, FALSE);
	}else{
			 	$this->db->where("(  sales.customer !=''  OR sales.name !=''  )", NULL, FALSE);

	}

	
	
}





if($group=="no"){
	
	
//$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

			 	$this->db->where("(  sales_order.status !='returned'   )", NULL, FALSE);


	$this->db->join('sales_order', 'sales.invoice = sales_order.invoice','INNER JOIN');
//$this->db->select("  * ,sales.id  AS id,sales_order.tax2 AS mytax2,qty AS myqty,sales_order.amount AS myamount,tax AS mytax, sales_order.userid as user ");





if($searchtype=="Supplier"){
					 	$this->db->where("(products.supplier='$customer'  )", NULL, FALSE);

		$this->db->join('products', 'sales_order.code = products.code','INNER JOIN');



}







//$this->db->select("  * ,sales.id  AS id,sales_order.amount, SUM(sales_order.amount) AS myamount,sales_order.qty, SUM(sales_order.qty) AS qty,sales_order.qty, SUM(sales_order.qty) AS myqty,sales_order.tax, SUM(sales_order.tax) AS mytax,sales_order.tax2, SUM(sales_order.tax2) AS mytax2 ,sales_order.userid as user ");
	
	$this->db->select("  * ,sales.id  AS id,sales_order.amount  AS myamount,sales_order.qty AS qty,sales_order.qty AS myqty,sales_order.tax  AS mytax,sales_order.tax2 AS mytax2 ,sales_order.userid as user ");

	
	
	
	
	
	
	
	

	//$this->db->group_by('sales_order.sprice');

	//$this->db->group_by('sales_order.code');
	//$this->db->group_by('sales_order.branch');
	//$this->db->group_by('sales_order.serial');
	//$this->db->group_by('sales.salesperson ');
	//$this->db->group_by('sales.invoice ');







}else{
	
	//$this->db->select("  * ,sales.id  AS id,sales_order.amount, SUM(sales_order.amount) AS myamount,sales_order.qty, SUM(sales_order.qty) AS qty,sales_order.qty, SUM(sales_order.qty) AS myqty,sales_order.tax, SUM(sales_order.tax) AS mytax,sales_order.tax2, SUM(sales_order.tax2) AS mytax2 ,sales_order.userid as user ");

	$this->db->select("  * ,sales.id  AS id");
	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);




$this->db->group_by('sales.invoice');
	
}

	 $this->db->order_by('sales.dateposted','ASC');

$this->db->where('sales.companyid', $companyid);

$this->db->where('sales.dateposted >=', $fromdate);
$this->db->where('sales.dateposted <=', $todate);
$this->db->from('sales');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}










function viewproductgroupsalesreportbyagents($fromdate,$todate,$type,$companyid,$customer,$branch,$group,$searchtype){
	
//$this->db->select("  * ,tax, SUM(tax) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'  ");

	if($branch !="all"){
$this->db->where('sales.branch', $branch);
	}
		if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
			//exit;
									//	$this->db->where("(type !='Invoice'  OR type !='Credit'  )", NULL, FALSE);
$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}


	}
	

if($searchtype=="Personnel"){
	
		if($customer !="all"){
	 	$this->db->where("(sales.salesperson='$customer' and   sales.salesperson !='0'   )", NULL, FALSE);
	}else{
			 	$this->db->where("(  sales.salesperson !='0'   )", NULL, FALSE);

	}

}else{
		if($customer !="all"){
			 	$this->db->where("(sales.customer='$customer' OR  sales.name='$customer'   )", NULL, FALSE);
	}else{
			 	$this->db->where("(  sales.customer !=''  OR sales.name !=''  )", NULL, FALSE);

	}

	
}





if($group=="no"){
//$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

			 	$this->db->where("(  sales_order.status !='returned'   )", NULL, FALSE);


	$this->db->join('sales_order', 'sales.invoice = sales_order.invoice','INNER JOIN');
$this->db->select("  * ,sales.id  AS id,sales_order.tax2 AS mytax2,qty AS myqty,sales_order.amount AS myamount,tax AS mytax, sales_order.userid as user ");

}else{
						 $this->db->select("  * ,sales.id  AS id,total, SUM(total) AS total,cash, SUM(cash) AS cash,mpesa, SUM(mpesa) AS mpesa,credit, SUM(credit) AS credit,voucher, SUM(voucher) AS voucher, cheque, SUM(cheque) AS cheque,card, SUM(card) AS card,points, SUM(points) AS points,balance, SUM(balance) AS balance,COUNT(invoice) As counter ");

	//$this->db->select("  * ,sales.id  AS id");
	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);



$this->db->group_by('sales.salesperson');

	
	 $this->db->order_by('sales.dateposted','desc');
}


$this->db->where('sales.companyid', $companyid);

$this->db->where('sales.dateposted >=', $fromdate);
$this->db->where('sales.dateposted <=', $todate);
$this->db->from('sales');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}












function viewproductgroupsalesreportbycustomer22($fromdate,$todate,$type,$companyid,$customer,$branch){

if($fromdate!=null){
	
	
	if(($type="all") && ($branch=="all") ){
$this->db->select(" * from  sales where   dateposted BETWEEN '$fromdate'  and '$todate' and customer='$customer' or name='$customer' ");
	
	}else if(($type=="all") && ($branch!="all") ){
$this->db->select(" * from  sales where   branch ='$branch' and  dateposted BETWEEN '$fromdate'  and '$todate' and customer='$customer' or name='$customer' ");
	
	}else if(($type!="all") && ($branch=="all") ){
$this->db->select(" * from  sales where   type ='$type'  and  dateposted BETWEEN '$fromdate'  and '$todate' and customer='$customer' or name='$customer' ");
	}
	
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}


function viewproductgroupsalesreportbyuser($fromdate,$todate,$type,$companyid,$userid,$branch){

if($fromdate!=null){
	

if(($type=="all") && ($branch=="all") ){
$this->db->select(" * from  sales where    companyid='$companyid' and userid='$userid'  and dateposted BETWEEN '$fromdate'  and '$todate'   ");
	}else if(($type!="all") && ($branch!="all") ){
$this->db->select(" * from  sales where   branch ='$branch' and type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid'");
}else if(($type!="all") &&  ($branch=="all") ){
$this->db->select(" * from  sales where   type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid'");
}
else if(($type=="all") && ($branch!="all")  ){
$this->db->select(" * from  sales where  branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid'");
}else if(($type!="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select(" * from  sales where  userid='$userid' dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  ");
}

	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}


























function viewsalesrordereport($fromdate,$todate,$companyid,$supplier,$branchname,$mytable,$download){
			
			if($branchname!="all") {
			$this->db->where('branchid', $branchname);
			
				}
			
			if($supplier!="all") {
			$this->db->where('supplier', $supplier);
			
				}
			
				if($fromdate!="") {
		$this->db->where('fromdate >=', $fromdate);
				$this->db->where('fromdate <=', $todate);
			
				}
					
		if($mytable!="all") {
			$this->db->where('remarks', $mytable);
			
				}


				$this->db->where('status', "s.o");

		$this->db->where('companyid', $companyid);
						

		$this->db->from('purchaseorder');
	 $this->db->order_by('id','desc');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){

  // return $bioquery->result();
   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

		        $filename = 	$new_date."SalesOrder.csv";

		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}


}else{

  return false;
}



}









function viewsalesrordereport222($fromdate,$todate,$companyid,$supplier,$branchname){

if($fromdate!=null){
	if($supplier=="all") {
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='s.o'");
	
	
	}else  if(($supplier!="all")) {
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and supplier='$supplier' and status='s.o'");
	
	}else{

$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and supplier='$supplier' and status='s.o'"); 
}

	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}




	function viewsuggestionposted($fromdate,$todate,$subject,$type,$status,$companyid,$displaytype){


	if($fromdate!=null){

 //$this->db->limit($limit, $start);



	if($subject !="all"){
$this->db->where('subject', $subject);
	}
		if($type !="all"){
$this->db->where('type', $type);
	}
	if($status !="all"){
$this->db->where('status', $status);
	}

	
	

$this->db->where('companyid', $companyid);



$this->db->where('createdate >=', $fromdate);
$this->db->where('createdate <=', $todate);
 $this->db->order_by('createdate','DESC');
 
 
$this->db->from('complain');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){


   if($displaytype=="Download"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Suggestion Box Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   


   


}else{

  return false;
}


}else{

  return false;
}


}













function viewagingpurchasereport($fromdate,$todate,$companyid,$supplier,$reporttype,$download,$group){

 
 

if($group !="all"){
	

	$this->db->select(" people.company AS SupplierName,purchaseorder.invoice,purchaseorder.ordernumber,purchaseorder.description,purchaseorder.userid,purchaseorder.orderedby,purchaseorder.shippingto,purchaseorder.deliverymethod,purchaseorder.fob,purchaseorder.terms,purchaseorder.deliverydate,purchaseorder.status,purchaseorder.amount,SUM(purchaseorder.amount) AS amount, purchaseorder.companyid,purchaseorder.balance,,SUM(purchaseorder.balance) AS balance,purchaseorder.fromdate,purchaseorder.paiddate,purchaseorder.updateddate,purchaseorder.updatedid,purchaseorder.pin,purchaseorder.vattype,purchaseorder.vat,purchaseorder.taxable,purchaseorder.vatrate,purchaseorder.paid,,SUM(purchaseorder.paid) AS paid,   purchaseorder.transactionstatus,purchaseorder.remarks,purchaseorder.entrydate,purchaseorder.branchid,purchaseorder.controller,purchaseorder.approvalstatus,purchaseorder.branchto,purchaseorder.qty,purchaseorder.EXEMPTED,purchaseorder.ZERORATED,purchaseorder.agent,purchaseorder.paymentmethodes,purchaseorder.supplier as supplier,purchaseorder.id");	
//
	
	
	
	}else{
		
		
$this->db->select(" people.company AS SupplierName,purchaseorder.invoice,purchaseorder.ordernumber,purchaseorder.description,purchaseorder.userid,purchaseorder.orderedby,purchaseorder.shippingto,purchaseorder.deliverymethod,purchaseorder.fob,purchaseorder.terms,purchaseorder.deliverydate,purchaseorder.status,purchaseorder.amount,purchaseorder.companyid,purchaseorder.balance,purchaseorder.fromdate,purchaseorder.paiddate,purchaseorder.updateddate,purchaseorder.updatedid,purchaseorder.pin,purchaseorder.vattype,purchaseorder.vat,purchaseorder.taxable,purchaseorder.vatrate,purchaseorder.paid,purchaseorder.transactionstatus,purchaseorder.remarks,purchaseorder.entrydate,purchaseorder.branchid,purchaseorder.controller,purchaseorder.approvalstatus,purchaseorder.branchto,purchaseorder.qty,purchaseorder.EXEMPTED,purchaseorder.ZERORATED,purchaseorder.agent,purchaseorder.paymentmethodes,purchaseorder.supplier as supplier,purchaseorder.id");	
		
	}
 
 
 
 
 
 
 
 
//$this->db->select("  * ,purchaseorder.id  AS SupplierId, ");
	//$this->db->where('purchaseorder.status', 'purchases');
	$this->db->where("(purchaseorder.status='purchases' OR 	purchaseorder.status='p.o' and purchaseorder.transactionstatus='closed'   OR 	purchaseorder.status='Opening Balance' OR 	purchaseorder.status='creditnote'  )", NULL, FALSE);

	
		
		$this->db->where("(purchaseorder.paid < amount  )", NULL, FALSE);


		if($supplier !="all"){
	
	$this->db->where('purchaseorder.supplier', $supplier);
		}
		
		

	if($reporttype=="all"){
	
	$this->db->where('purchaseorder.fromdate >=', $fromdate);
$this->db->where('purchaseorder.fromdate <=', $todate);

		}else{
			
			
				$this->db->where('purchaseorder.paiddate >=', $fromdate);
$this->db->where('purchaseorder.paiddate <=', $todate);
			
			
		}

		$this->db->join('people', 'purchaseorder.supplier = people.id','INNER JOIN');

	 $this->db->order_by('purchaseorder.id','desc');

	 $this->db->order_by('purchaseorder.supplier','DESC');


if($group !="all"){
	
	 			$this->db->group_by('purchaseorder.supplier');

	
	
	
	
	}




$this->db->from('purchaseorder');

		






	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
 if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

		        $filename = 	$new_date."Invoice.csv";

		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   


}else{

  return false;
}




}








function viewpurchasereport($fromdate,$todate,$companyid,$supplier,$vattype,$daterange,$group,$payment,$groupall,$branchname,$paymentstatus){
	
	
	//$this->db->where('purchaseorder.status', 'purchases');
	$this->db->where("(purchaseorder.status='purchases' OR 	purchaseorder.status='p.o' )", NULL, FALSE);

		if($branchname!="all") {
			$this->db->where('branchid', $branchname);
			
				}
	
	
	if($payment=="yes"){
		$this->db->where("(paid < amount  )", NULL, FALSE);
	
	}
	if($paymentstatus !="all"){
		if($paymentstatus=="paid"){
			$this->db->where('balance >', 0);

		}else{
	$this->db->where('balance', 0);

		}
		}
	if($daterange=="yes"){
	
	$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);

		}
		if($supplier !="all"){
	
	
	     $this->db->where("(supplier='$supplier' OR 	description='$supplier' )", NULL, FALSE);

	//$this->db->where('supplier', $supplier);
		}
			if($vattype !="all"){
	
	$this->db->where('vattype', $vattype);
		}
		

	if($group!="all"){
		
		
		
		$this->db->where('grouping.categoryid', $group);
		$this->db->where('grouping.status',1);


		$this->db->join('grouping', 'purchaseorder.supplier = grouping.memberid','INNER JOIN');
		
		
		
		
		
	}

if($groupall=="yes"){
		
$this->db->select("  * ,purchaseorder.id  AS id,qty,SUM(qty) AS qty,paid,SUM(paid) AS paid,amount,SUM(amount) AS amount  ");


							$this->db->where('companyid', $companyid);
							$this->db->group_by('purchaseorder.supplier');

		
	}else{
		
		$this->db->select("  * ,purchaseorder.id  AS id, ");

	}
	 $this->db->order_by('purchaseorder.id','asc');


$this->db->from('purchaseorder');

		






	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}


function viewpurchasereport2($fromdate,$todate,$companyid,$supplier,$vattype,$daterange,$group){

if($fromdate!=null){
	if(($supplier=="all")&&($vattype=="all")) {
if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='purchases'");
		}else{
			$this->db->select(" * from  purchaseorder where   companyid='$companyid' and status='purchases'");

		}
		
		
	} else if(($supplier=="all")&&($vattype!="all")) {
		
		
		
		
		if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and vattype='$vattype' and status='purchases'");

		}else{
		$this->db->select(" * from  purchaseorder where  companyid='$companyid' and vattype='$vattype' and status='purchases'");

		}
		
	
	}else  if(($supplier!="all")&&($vattype=="all")) {
		
		if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and supplier='$supplier' and status='purchases'");

		}else{
		$this->db->select(" * from  purchaseorder where   companyid='$companyid'  and supplier='$supplier' and status='purchases'");

		}
		
	
	}else{

	if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and supplier='$supplier'  and vattype='$vattype' and status='purchases'"); 

		}else{
		$this->db->select(" * from  purchaseorder where    companyid='$companyid' and supplier='$supplier'  and vattype='$vattype' and status='purchases'"); 

		}
}

	if($group!="all"){
		
		
		
		$this->db->where('grouping.categoryid', $group);
		$this->db->where('grouping.status',1);


		$this->db->join('grouping', 'purchaseorder.supplier = grouping.memberid','INNER JOIN');
		
		
		
		
		
	}



	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}












function searchquotaion($fromdate,$todate,$companyid,$preparedby,$branchid, $type,$status){
			
			if($branchid!="all") {
			$this->db->where('branchid', $branchid);
			
				}
			
			if($preparedby!="all") {
			$this->db->where('preparedby', $preparedby);
			
				}
			
				if($fromdate!="") {
		$this->db->where('createdate >=', $fromdate);
				$this->db->where('createdate <=', $todate);
			
				}
					
		
			if($type!="all") {
			$this->db->where('deliverymethod', $type);
			
				}
if($status!="all") {
			$this->db->where('deliverydate', $status);
			
				}
				
							$this->db->where('companyid', $companyid);
						

							$this->db->from('quotation');
	 $this->db->order_by('id','desc');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function getbudetexpense($fromdate,$todate,$companyid,$code,$branchid){
			
			if($branchid!="all") {
			$this->db->where('branch', $branchid);
			
				}
				
		$this->db->where('crateddate >=', $fromdate);
			$this->db->where('crateddate <=', $todate);
			
			$this->db->where('account', $code);
		
		
 			$this->db->select("  * ,debit,SUM(debit) AS amount ");


							$this->db->where('companyid', $companyid);
							$this->db->group_by('account');

							$this->db->from('debitcredit');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}



}


function viewitemexpensereport($fromdate,$todate,$companyid,$supplier,$itemtype,$daterange,$code,$grouping,$branchid,$department,$user){
			
			if($branchid!="all") {
			$this->db->where('branch', $branchid);
			
				}
			
			if($supplier!="all") {
			//$this->db->where('supplier', $supplier);
			
				}
			
				if($daterange!="no") {
					
		$this->db->where('crateddate >=', $fromdate);
			$this->db->where('crateddate <=', $todate);
				

			
				}
				
					
						if($user!="all") {
			$this->db->where('expenses.userid', $user);
		
				}
						if($itemtype!="all") {
			$this->db->where('account', $code);
		
				}
				if($grouping=="yes"){
 			$this->db->group_by('account');

 			$this->db->select("  * ,statement.amount,SUM(statement.amount) AS amount ");
			//$this->db->select(" * ,buyingprice,SUM(buyingprice) AS buyingprice ");


		}
		
				if($department!="all") {
					
		$this->db->join('expenses', 'statement.invoice = expenses.invoice ','INNER JOIN');

				$this->db->where('expenses.department', $department);
			
			
				}

				
							$this->db->where('statement.companyid', $companyid);
							//$this->db->where('invoice_number <>', "");
							//$this->db->where('status', "newstock");

							$this->db->from('statement');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function viewitembasedpurchasereport($fromdate,$todate,$companyid,$supplier,$itemtype,$daterange,$code,$grouping,$category,$branchname,$download,$reporttype,$batchno){
			

		
			if($download=="yes"){
				
				
	///$this->db->select("newstock.id,  newstock.item_id ,newstock.total,newstock.buyingprice,newstock.description,newstock.userid,newstock.orderedby,newstock.shippingto,purchaseorder.deliverymethod,purchaseorder.fob,purchaseorder.terms,purchaseorder.deliverydate,purchaseorder.status,purchaseorder.amount,purchaseorder.companyid,purchaseorder.balance,purchaseorder.fromdate,purchaseorder.paiddate,purchaseorder.updateddate,purchaseorder.updatedid,purchaseorder.pin,purchaseorder.vattype,purchaseorder.vat,purchaseorder.taxable,purchaseorder.vatrate,purchaseorder.paid,purchaseorder.transactionstatus,purchaseorder.remarks,purchaseorder.entrydate,purchaseorder.branchid,purchaseorder.controller,purchaseorder.approvalstatus,purchaseorder.branchto,purchaseorder.qty,purchaseorder.EXEMPTED,purchaseorder.ZERORATED,purchaseorder.agent,purchaseorder.paymentmethodes,purchaseorder.supplier as supplier,purchaseorder.id");	

		//	$this->db->join('products', 'newstock.item_id = products.code ','INNER JOIN');


				}
		
		
			if($branchname!="all") {
			$this->db->where('newstock.type', $branchname);
			
				}
			if($supplier!="all") {
			$this->db->where('newstock.supplier', $supplier);
			
				}
				
					if($reporttype=="expiry") {
			
				if($daterange=="yes") {
				$this->db->where('enddate >=', $fromdate);
				$this->db->where('enddate <=', $todate);
			
				}else{
					
					$this->db->where('enddate <=', $todate);
					
				}
			
			
			
			
			
			
			
			
			
				}else{
				
			
				if($daterange=="yes") {
				$this->db->where('date >=', $fromdate);
				$this->db->where('date <=', $todate);
			
				}
				
				}
						if($itemtype!="all") {
			$this->db->where('newstock.item_id', $code);
		
				}
				
				
						if($batchno!="all") {
			$this->db->where('newstock.batchno', $batchno);
		
				}
				
				
				
				
				
				if($grouping=="yes"){
 			$this->db->group_by('newstock.item_id');

 			$this->db->select("  * ,newstock.total,SUM(newstock.total) AS mytotal ,newstock.buyingprice,SUM(newstock.buyingprice*newstock.total) AS finaltotal  ");
			//$this->db->select(" * ,buyingprice,SUM(buyingprice) AS buyingprice ");


		}else{
			
			$this->db->select("  * ,newstock.total AS mytotal");

		}
		
			if($category!="all"){
			$this->db->join('products', 'newstock.item_id = products.code ','INNER JOIN');

				$this->db->where('products.category', $category);

				}
		
		
		
		
		
				
							$this->db->where('newstock.companyid', $companyid);
							$this->db->where('invoice_number <>', "");
							$this->db->where('newstock.itemstatus <>', "sold");
     $this->db->where("(newstock.status='newstock' OR 	newstock.status='PO' and total !='0' )", NULL, FALSE);

							$this->db->from('newstock');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){
	
	
	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Purchase Items.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}
	

   


}else{

  return false;
}



}













function viewdeliveryreport($fromdate,$todate,$companyid,$supplier,$status,$daterange,$datetype){
			
			
			
			if($supplier!="all") {
			$this->db->where('supplier', $supplier);
			
				}
			
				if($daterange!="no") {
					
					
						if($datetype=="yes"){
$this->db->where('ordereddate >=', $fromdate);
				$this->db->where('ordereddate <=', $todate);
	}else {
		$this->db->where('updatedate >=', $fromdate);
				$this->db->where('updatedate <=', $todate);

	}
	
	
	
			
				}
						if($status!="all") {
			$this->db->where('status', $status);
		
				}
			
		
							$this->db->where('companyid', $companyid);

							$this->db->from('delivery');
	 $this->db->order_by('id','desc');

							
$bioquery = $this -> db -> get();	

if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

function viewpurchasereportpayment($fromdate,$todate,$companyid,$supplier,$daterange){

if($supplier!=null){
	if($supplier=="all") {
if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='purchases' ");
		}else{
			$this->db->select(" * from  purchaseorder where   companyid='$companyid' and status='purchases' ");

		}
		
		
	}else  if($supplier!="all") {
		
		if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and supplier='$supplier' and status='purchases'");

		}else{
		$this->db->select(" * from  purchaseorder where   companyid='$companyid'  and supplier='$supplier' and status='purchases' ");

		}
		
	
	}else{

	if($daterange=="yes"){
$this->db->select(" * from  purchaseorder where   fromdate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and supplier='$supplier' and status='purchases' ");

		}else{
		$this->db->select(" * from  purchaseorder where   companyid='$companyid'  and supplier='$supplier' and status='purchases'");

		}
		
}

	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}



function getmymemberid($phone,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('people');
$this -> db -> where('phone', $phone);
//$this -> db -> where('companyid', $companyid);
$this -> db -> where('userid', $userid);




 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}





function uploadallgroupmembers($data)
{
// Query to insert data in database
  $memberid = $data['memberid'];
  $companyid = $data['companyid'];
   $categoryid = $data['categoryid'];
 
 
	
$this->db->select(" * from grouping where 	memberid='$memberid' and companyid='$companyid' and categoryid='$categoryid' ");

   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {
	   $this->db->select(" * from category where  id='$categoryid' ");
 $categoryquery = $this -> db -> get();
  if($categoryquery -> num_rows() > 0)

   {
	   $this->db->insert('grouping', $data);  
	    if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
		     return false;
  
	  }
   }else{
	return false;   
   }
	 
	 

 
 

   }

  


}




function uploadASSETS($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

  if($code!=null){
     $this->db->where('code',$code);
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('assets');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('code',$code);
	  $this->db->where('companyid',$companyid);

      $this->db->update('assets',$data);
   } else {
      
      $this->db->insert('assets', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}







function uploadalldata($table,$data)
  
{
  
      $this->db->insert($table, $data);
      
 



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} 

function uploadCHILD($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

 
      
      $this->db->insert('uomproducts', $data);
     
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} 



function uploadupgrade($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

 
      
      $this->db->insert('upgrade', $data);
     
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} 




function uploadlabour($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

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



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} 










        function uploadASSETS3($mydata,$dublicate)
{
// Query to insert data in database

$dublicate = $dublicate;
  $userid = $mydata['userid'];
    $companyid = $mydata['companyid'];

   // $this -> db -> select('*');

  $this -> db -> from('people');


   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {

    $this->db->insert('assets', $mydata);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
		     return false;
  
	  }

   }

  


}




            function uploadmembers($mydata,$dublicate)
{
// Query to insert data in database

$dublicate = $dublicate;
  $userid = $mydata['userid'];
  $idno = $mydata['idno'];
    $companyid = $mydata['companyid'];
	$email = $mydata['email'];
   $phone = $mydata['phone'];
   // $this -> db -> select('*');

  //$this -> db -> from('people');

 // if($dublicate=="no"){
	   
  //$this->db->where("(idno!='' and idno='$idno'  and companyid='$companyid'  )", NULL, FALSE);

  //}
   //$this -> db -> select("* from people where idno='$idno' and companyid='$companyid' and phone='$phone'  ");



    $this->db->insert('people', $mydata);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
		     return false;
  
	  }

   

  


}
















function viewincomingreportcategory($fromdate,$todate,$companyid,$category,$branchname,$tobranch){

//$this->db->select("  * ,amount , SUM(amount) AS myamount,qty, SUM(qty)  AS myqty ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");

if($tobranch !="all"){
$this->db->where('stockto', $tobranch);
	}

	if($branchname !="all"){
$this->db->where('stockfrom', $branchname);
	}
	


	if($category!="all") {
			$this->db->join('products', 'newstock.item_id = products.code ','INNER JOIN');

				$this->db->where('products.category', $category);

				}
		











$this->db->where('newstock.companyid',$companyid);

$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);
$this->db->where('total <>', "");
$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

		 $this->db->order_by('newstock.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function searchdocuemntsbycategory($fromdate,$todate,$companyid,$category,$branchname,$daterange){



if($branchname !="all"){
$this->db->where('branchid', $branchname);
	}
	if($category !="all"){
$this->db->where('category', $category);
	}
	
$this->db->where('documents.companyid',$companyid);

	if($daterange !="all"){

	
	
$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
}
$this->db->from('documents');


 $this->db->order_by('documents.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function viewfarmreport($fromdate,$todate,$companyid,$branchname,$one){



	if($one !="all"){
$this->db->where('one', $one);
	}
	
	
$this->db->where('events.companyid',$companyid);

$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);
$this->db->from('events');

		 $this->db->order_by('events.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function viewpurchaseorderreport($fromdate,$todate,$companyid,$branchname,$supplier){




$this->db->where('status', "p.o");

	
	
$this->db->where('purchaseorder.companyid',$companyid);

$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);
$this->db->from('purchaseorder');
if($branchname!="all"){
		$this->db->where('purchaseorder.branchid', $branchname);

		
	}
	if($supplier!="all"){
		$this->db->where('purchaseorder.supplier', $supplier);

		
	}
		 $this->db->order_by('purchaseorder.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function bulkstoreupdate($category,$companyid,$branch,$equalto,$method){

if($category !="all"){
	if($category =="Equal"){
$this->db->where('sellingprice', $equalto);
	}else{
	$this->db->where('category', $category);
	
	}
	
	}
	$this->db->where('companyid',$companyid);



	
	
$this->db->from('products');

$this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}












function bulkbranchupdate($category,$companyid,$branch,$equalto,$method){

if($category !="all"){
$this->db->where('category', $category);
	}
	$this->db->where('companyid',$companyid);

if($branch !="all"){
$this->db->where('branch', $branch);
	}

if($method =="Equal"){
$this->db->where('sellingprice', $equalto);
	}
	
	
$this->db->from('productlist');

$this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}










function viewincomingreport($fromdate,$todate,$companyid,$supplier,$branchname,$tobranch,$user){

//$this->db->select("  * ,amount , SUM(amount) AS myamount,qty, SUM(qty)  AS myqty ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");
if($user !="all"){
$this->db->where('userid', $user);
	}
if($tobranch !="all"){
$this->db->where('stockto', $tobranch);
	}

	if($branchname !="all"){
$this->db->where('stockfrom', $branchname);
	}
		if($supplier !="all"){
$this->db->where('supplier', $supplier);
	}


$this->db->where('itemstatus <>', "sold");

$this->db->where('companyid',$companyid);

$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);
$this->db->where('total <>', "");
$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function viewallproductmovementsold($fromdate,$todate,$daterange,$branchname,$companyid,$code){


	if($branchname !="all"){
//$this->db->where('type', $branchname);
	$this->db->where("(type ='$branchname'  OR type ='Main'  )", NULL, FALSE);

	}
		if($code !="all"){
$this->db->where('item_id', $code);
	}
	if($daterange =="yes"){
$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);

	}


	 $this->db->order_by('id','DESC');
$this->db->where('companyid',$companyid);


$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

	

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function viewserialproductsalesreportold($fromdate,$todate,$type,$companyid,$code,$branch,$daterange,$category,$group,$transactiontype){

	if($fromdate!=null){

if($group =="yes"){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax ,SUM(tax2) AS mytax2  ");

	}else{
	$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,tax2 AS mytax2  ");
	
	}
	



	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		if($type !="all"){
$this->db->where('type', $type);
	}
	if($daterange =="yes"){
		
$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
	}
		if($category =="serial"){
$this->db->where('serial', $code);
	}else if($category =="otherserial"){
$this->db->where('address', $code);
	}else{
		$this->db->where('code', $code);

	}
		if($transactiontype =="Sold"){
		
$this->db->where('status <>',"returned");

	}

$this->db->where('companyid', $companyid);



 $this->db->order_by('dateposted','DESC');
  $this->db->order_by('status','DESC');
 	
	if($group =="yes"){
				$this->db->group_by('status');

		$this->db->group_by('sprice');
$this->db->group_by('code');
	$this->db->group_by('branch');
		$this->db->group_by('serial');
	}
	



$this->db->from('sales_order');


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}


}else{

  return false;
}


}



function viewproductvaluemain($category,$orderamount,$companyid,$supplier,$branchname,$type,$producttype,$maincategory,$vattype,$limit,$is_serialized){

$branchname=trim($branchname);

if(($branchname =="Main") || ($branchname =="Mainothers")|| ($branchname =="all"))  {
	
	}else{
		
		$this->db->where('branch', $branchname);

		
		
	}
	if(($branchname =="Main") || ($branchname =="Mainothers")) {
		
	if($maincategory !="all"){
		
$this->db->where('productcategory.maincategory', $maincategory);
	//$this->db->join('productlist', 'stock.code = productlist.code','INNER JOIN');
				$this->db->join('productcategory', 'products.category = productcategory.id','outer JOIN');



	}
	}else{
				
	if($maincategory !="all"){
		$this->db->where('productcategory.maincategory', $maincategory);
	$this->db->join('productcategory', 'productlist.category = productcategory.id','outer JOIN');
		
	}

		
	}
		if($producttype !="all"){
$this->db->where('producttype', $producttype);
	}
		if($supplier !="all"){
$this->db->where('supplier', $supplier);
	}
	if($vattype !="all"){
$this->db->where('vattype', $vattype);
	}
	if($category !="all"){
$this->db->where('category', $category);
	}
	if($orderamount !="all"){
$this->db->where('orderamount', $orderamount);
	}

	if($limit !="all"){
		
		
	if($limit=="Above"){
$this->db->where('qty > ', 0);
	}else{
	$this->db->where('qty <  ', 1);
	
		
	}
	}
	
	
	
	
		if($is_serialized !="all"){
$this->db->where('is_serialized', $is_serialized);
	}
	
	
	
	
	
//$this->db->where('companyid',$companyid);

if(($branchname =="Main") || ($branchname =="Mainothers")) {
		$this->db->select("taxtype,tax_included,hscode,is_serialized,vattype,producttype,expdate,orderamount,uom, products.id,products.store, products.code,products.name,products.description, products.qty,	products.buyingprice,	products.sellingprice, products.wholesaleprice	,products.reorderevel,products.category,products.supplier  ");
//$this->db->group_by('products.companyid');

	$this->db->from('products');

}else{
	if($branchname =="all"){
		
		$this->db->select("taxtype,tax_included,hscode,is_serialized,vattype, producttype,expdate,orderamount,uom,productlist.id,productlist.store, productlist.code,productlist.name,productlist.description, productlist.qty, SUM(productlist.qty)  AS qty,	productlist.buyingprice,	productlist.sellingprice, productlist.wholesaleprice	,productlist.reorderevel,productlist.category,productlist.supplier,productlist.branch ,productlist.hscode ");

	 $this->db->group_by('productlist.code');
//$this->db->group_by('productlist.companyid');
	}else{
		$this->db->select("taxtype,tax_included,hscode,is_serialized,vattype,producttype,expdate,orderamount,uom,productlist.id,productlist.store, code,productlist.name,productlist.description, qty,	buyingprice,	sellingprice, wholesaleprice	,reorderevel,category,supplier,branch,hscode  ");
	
	}
	
	
	
$this->db->from('productlist');
	
	
}





	

	$this->db->order_by('name','asc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){


 
   if($type=="download"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Stock Value Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   


}else{

  return false;
}



}








function viewproductsforetimsupdate($category,$orderamount,$companyid,$branchname,$producttype,$maincategory,$vattype){


if($branchname !="all"){
	if($branchname =="Main"){
		
	}else{
	//$this->db->where('branch', $branchname);
	
	}
	
	}

if($producttype !="all"){
$this->db->where('producttype', $producttype);
	}
	
	if($vattype !="all"){
$this->db->where('vattype', $vattype);
	}
	if($category !="all"){
$this->db->where('category', $category);
	}
	if($orderamount !="all"){
$this->db->where('orderamount', $orderamount);
	}
if($maincategory !="all"){
	$this->db->where('productcategory.maincategory', $maincategory);
	$this->db->join('productcategory', 'products.category = productcategory.id','outer JOIN');

	}

$this->db->where('products.companyid',$companyid);
$this->db->where("(itemclassification='' OR 	is_serialized='0' )", NULL, FALSE);

//$this->db->where('itemclassification', "");
$this->db->where('products.status', "active");

$this->db->from('products');


$this->db->order_by('products.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}








function viewlabourreport($fromdate,$todate,$companyid,$supplier,$branchname,$month,$code,$description){

//$this->db->select("  * ,amount , SUM(amount) AS myamount,qty, SUM(qty)  AS myqty ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");

if($branchname !="all"){
$this->db->where('branch', $branchname);
	}

	
		if($supplier !="all"){
$this->db->where('employeeid', $supplier);
	}

	if($month !="all"){
$this->db->where('month', $month);
	}
	if($code !="all"){
$this->db->where('code', $code);
	}

if($description !=""){
$this->db->where('description', $description);
	}
$this->db->where('companyid',$companyid);

$this->db->where('exactdate >=', $fromdate);
$this->db->where('exactdate <=', $todate);
//$this->db->where('total <>', "");
$this->db->from('labour');




	if($branchname =="all"){
		 $this->db->order_by('branch','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function displayexpiredproducts($fromdate,$todate,$companyid,$branchname){

$this->db->where('companyid',$companyid);

$this->db->where('expdate >=', $fromdate);
$this->db->where('expdate <=', $todate);


if($branchname =="Main"){
	
	$this->db->from('products');
	
	}else{
		
	$this->db->from('productlist');

	}
	
	

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


function searchforleaves($fromdate,$todate,$companyid,$branchname,$type){

$this->db->where('companyid',$companyid);
if($type=="posteddate"){
$this->db->where('startdate >=', $fromdate);
$this->db->where('startdate <=', $todate);
}else{
$this->db->where('postedtime >=', $fromdate);
$this->db->where('postedtime <=', $todate);	
}


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


		
	$this->db->from('leaves');


	
	

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function viewincomingreportperproject($fromdate,$todate,$companyid,$branchname,$tobranch,$category){

//$this->db->select("  * ,amount , SUM(amount) AS myamount,qty, SUM(qty)  AS myqty ,tax, SUM(tax)  AS mytax ,tax2, SUM(tax2)  AS mytax2 ");

if($tobranch !="all"){
$this->db->where('projectid', $tobranch);
	}



	if($category !="all"){
$this->db->where('category', $category);
	}




	if($branchname !="all"){
$this->db->where('stockfrom', $branchname);
	}
	


$this->db->where('companyid',$companyid);

$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);
$this->db->where('total <>', "");
$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}





function viewpoints($fromdate,$todate,$companyid){
$this->db->select("  * ,value , SUM(value) AS value,amount, SUM(amount)  AS amount  ");





$this->db->where('companyid',$companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);

	 $this->db->group_by('cardnumber');
$this->db->from('loyalitycards');


		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function updatecustomerbalances($fromdate,$todate,$companyid){
$this->db->select("  * ,credit , SUM(credit) AS credit,amount, SUM(amount)  AS amount  ");




//$this->db->where('credit >', 0);


$this->db->where('companyid',$companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);

	 $this->db->group_by('customer');
$this->db->from('sales');


		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function updaterentalbalances($fromdate,$todate,$companyid){
$this->db->select("  * ,paid , SUM(paid) AS paid,amount, SUM(amount)  AS amount  ");





//$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   OR 	status='Opening Balance'   and paid !=  amount )", NULL, FALSE);

//$this->db->where('companyid',$companyid);
$this->db->where('paymentstatus',"invoice");
$this->db->where('createdat >=', $fromdate);
$this->db->where('createdat <=', $todate);
 $this->db->group_by('plotno');
  $this->db->group_by('houseno');
	 $this->db->group_by('tenantid');
$this->db->from('invoice');


		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

function updatesupplierbalances($fromdate,$todate,$companyid){
$this->db->select("  * ,paid , SUM(paid) AS paid,amount, SUM(amount)  AS amount  ");





$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   OR 	status='Opening Balance'   and paid !=  amount )", NULL, FALSE);

$this->db->where('companyid',$companyid);

$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);

	 $this->db->group_by('supplier');
$this->db->from('purchaseorder');


		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}





function closefiscalyear($fromdate,$todate,$companyid,$branchid,$category){




$this->db->where("(status='1' OR 	balance !='0' OR  supplierbalance !='0'   OR 	lastbalancedate BETWEEN '$fromdate'  and '$todate' )", NULL, FALSE);

$this->db->where('companyid',$companyid);
$this->db->where('category',$category);
$this->db->where('status <>',0);
//$this->db->where('lastbalancedate >=', $fromdate);
//$this->db->where('lastbalancedate <=', $todate);

	
$this->db->from('people');


		// $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}


















function viewincomingreport__($fromdate,$todate,$companyid,$supplier,$branchname){

if($fromdate!=null){
	
	
	
	
	if(($supplier=="all")&& ($branchname!="all")){
		$this->db->select(" * from  newstock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and type='$branchname'");

	}else if($supplier=="all"){
$this->db->select(" * from  newstock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	
	}else{

$this->db->select(" * from  newstock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and supplier='$supplier'");
}
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}


function viewsalaryp9($user,$salaryyear,$companyid,$branchid){
	
$this->db->select("  * ,salary.salary  AS mysalary, ");

		$this->db->where('salaryyear', $salaryyear);


		$this->db->where('salary.memberid', $user);

		
$this->db->where('salary.companyid', $companyid);

if($branchid!="all"){
		$this->db->where('salary.branchid', $branchid);

		
	}



$this->db->from('salary');

		


$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');




	
	 $this->db->order_by('salary.id','ASC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}



function viewsalarystatement($user,$salaryyear,$companyid,$branchid){
	
$this->db->select("  * ,salary.salary  AS mysalary, ");

		$this->db->where('salaryyear', $salaryyear);


		$this->db->where('salary.memberid', $user);

		
$this->db->where('salary.companyid', $companyid);
if($branchid!="all"){
		$this->db->where('salary.branchid', $branchid);

		
	}





$this->db->from('salary');

		


$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');




	
	 $this->db->order_by('salary.id','ASC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}

function viewsalary($slarymonth,$salaryyear,$companyid,$branchid,$download,$category,$bankname){
	
	 
	
		
	if($download=="yes"){
$this->db->select(" people.number   as Customer Reference No, CONCAT(IFNULL(people.firstname,''),' ',IFNULL(people.surname,''))   AS BeneficiaryName,people.phone,people.bankname,people.bankcode as Beneficiary Bank Code / SWIFT Code,people.branchcode as BranchCode , people.bankaccount as Beneficiary Account No , salary.salary  AS BASIC PAY,salary.pay  AS ALLOWANCE PAYMENT,salary.gross  AS GROSS PAY,salary.nhif as NHIF,salary.nssf AS NSSF,salary.paye AS PAYE,salary.houselevy AS HLEVY,salary.advance as ADVANCE PAYMENT,salary.deductions as Deductions, salary.otherdeductions as OTHER DEDUCTIONS,salary.netsalry as NET AMOUNT, salary.transactiontypecode   as Transaction Type Code,salary.slarymonth as Purpose Of Payment,people.comments as Beneficiary Addr, salary.chargetype as Charge Type,salary.currency as Transaction Currency, salary.paymenttype as Payment Type ");

	}else{
	$this->db->select("  * ,salary.salary  AS mysalary, ");
	
		
	}

	
	if($slarymonth=="all"){
$this->db->where('salaryyear', $salaryyear);
 // $this->db->select(" *  from  salary where    salaryyear ='$salaryyear'  and companyid='$companyid'");
//$this->db->select("SELECT *,salary.salary  AS mysalary FROM salary INNER JOIN people on  salary.memberid= people.id  WHERE salaryyear ='$salaryyear'  and salary.companyid='$companyid'");
	
	}else{
		// $this->db->select(" *  from  salary where   slarymonth ='$slarymonth' and salaryyear ='$salaryyear'  and companyid='$companyid'");
$this->db->where('slarymonth', $slarymonth);
$this->db->where('salaryyear', $salaryyear);

}
		
		
		
	if($branchid!="all"){
		$this->db->where('salary.branchid', $branchid);

		
	}
	
		
	if($bankname!="all"){
		$this->db->where('salary.bankname', $bankname);

		
	}
	
	
$this->db->where('salary.companyid', $companyid);





$this->db->from('salary');

		


$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');


if($category !="all"){
	
$this->db->where('people.group', $category);
	
}

		 $this->db->order_by('people.number','ASC');

	 $this->db->order_by('salary.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	
	
	
	
	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Salary Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}
	
	
	
	
	
	
	
	

   


}else{

  return false;
}




}





function viewalldeductions($slarymonth,$salaryyear,$companyid,$code,$branchid,$download,$chargename){
	if($download =="yes"){
	
	if($chargename=="NHIF"){
			
$this->db->select(" salary.number  AS PAYROLL NUMBER, people.surname  AS SURNAME, people.firstname  AS LAST NAME, people.idnumber  AS ID NO,people.nhif  AS NHIF_NO,  employeepayments.amount   AS AMOUNT  ");

			
		}else if($chargename=="NSSF"){
			
$this->db->select(" salary.number  AS PAYROLL NUMBER,  people.surname  AS SURNAME,people.firstname  AS OTHER NAMES, people.idnumber  AS ID NO,people.kra  AS KRA PIN,people.nssf  AS NSSF NO,salary.salary  as GROSS PAY ,amount   AS AMOUNT ,'' AS VOLUNTARY ");

			
		} else if($chargename=="PAYE"){
			
$this->db->select("  * ,salary.salary  AS mysalary,salary.salaryid  AS mysalaryid,employeepayments.code  AS mycode,employeepayments.amount AS paidamount  ");

			
		}else{
			
$this->db->select("  * ,salary.salary  AS mysalary,salary.salaryid  AS mysalaryid,employeepayments.code  AS mycode,employeepayments.amount AS paidamount  ");

		}
	}else{
	$this->db->select("  * ,salary.salary  AS mysalary,salary.salaryid  AS mysalaryid,employeepayments.code  AS mycode,employeepayments.amount AS paidamount  ");
	
		
	}
	
	
	
	
	
	
	
	
	
	

	
	if($slarymonth=="all"){
$this->db->where('salaryyear', $salaryyear);

	}else{
		// $this->db->select(" *  from  salary where   slarymonth ='$slarymonth' and salaryyear ='$salaryyear'  and companyid='$companyid'");
$this->db->where('slarymonth', $slarymonth);
$this->db->where('salaryyear', $salaryyear);

}
		if($branchid!="all"){
		$this->db->where('salary.branchid', $branchid);

		
	}
		
$this->db->where('salary.companyid', $companyid);





$this->db->from('salary');

$this->db->join('employeepayments', 'salary.salaryid = employeepayments.salaryid','INNER JOIN');
if($code!="all"){
if($code=="Pay"){
	$this->db->where('employeepayments.category', $code);

}else if($code=="Deduction"){
	$this->db->where('employeepayments.category', $code);

	
}else{
$this->db->where('employeepayments.code', $code);
	
}
	
	
	

}

		


$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');


	 $this->db->group_by('employeepayments.code');
	 $this->db->group_by('salary.memberid');
	 //$this->db->group_by('employeepayments.salaryid');


	
	 $this->db->order_by('salary.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 //  return $bioquery->result();
   
   
   		if($download =="yes"){

		  $new_date=date("Y-m-d H:i");		

		$delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date.$chargename."Report.csv";
		


      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);


}else{
	
   return $bioquery->result();
	
	
}
	


}else{

  return false;
}




}

function viewprojectbudgetreport($code,$projectid,$download,$companyid){
	
$this->db->select("  * ,projectsbudget.id  AS id, ");

	
	if($projectid !="all"){
$this->db->where('projectid', $projectid);


}

	if($code !="all"){
$this->db->where('code', $code);


}
	
		
		
$this->db->where('projectsbudget.companyid', $companyid);





$this->db->from('projectsbudget');

		
	$this->db->order_by('projectsbudget.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	
		if($download =="yes"){

		  $new_date=date("Y-m-d H:i");		

		$delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date." Project Budget report.csv";
		


      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);


}else{
	
   return $bioquery->result();
	
	
}
	
	
	
	
	
	
	

   


}else{

  return false;
}




}



function viewbudgetreport($bmonth,$byear,$companyid){
	
$this->db->select("  * ,budget.id  AS id, ");

	
	if($bmonth=="all"){
$this->db->where('year', $byear);


	}else{
		// $this->db->select(" *  from  salary where   slarymonth ='$slarymonth' and salaryyear ='$salaryyear'  and companyid='$companyid'");
$this->db->where('month', $bmonth);
$this->db->where('year', $byear);

}
		
		
$this->db->where('budget.companyid', $companyid);





$this->db->from('budget');

		


//$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');




	
	 $this->db->order_by('budget.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}








function viewtaskassignedreport($user,$year,$period,$companyid){
	
$this->db->select("  * ,assigntask.id  AS id, ");

	
	if($period !="all"){
$this->db->where('period', $period);


	}
		
		
$this->db->where('assigntask.employeeid', $user);
$this->db->where('assigntask.companyid', $companyid);





$this->db->from('assigntask');

		


//$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');




	
	 $this->db->order_by('assigntask.period','ASC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}







function  viewtaskperformance($user,$fromdate,$todate,$period,$branchid,$companyid,$type,$tasktype){
	
	if($type=="performance"){
$this->db->select("  * ,dailytasks.id  AS id,achieved, SUM(achieved) AS achieved ");
	}
	
	if($user !="all"){
$this->db->where('userid', $user);


	}
			if($branchid !="all"){
$this->db->where('branchid', $branchid);


	}
			if($tasktype !="all"){
///$this->db->where('tasktype', $tasktype);


	}
	
$this->db->where('dailytasks.companyid', $companyid);
$this->db->where('exactdate >=', $fromdate);
$this->db->where('exactdate <=', $todate);




$this->db->from('dailytasks');

		


//$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');

	if($type=="performance"){
		 $this->db->group_by('taskid');
	}
	



	 $this->db->order_by('dailytasks.period','ASC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}







function viewinvoicereport($fromdate,$todate,$companyid,$supplier,$salestype,$pending, $searchtype,$download){


//$this->db->select("  * ,credit , SUM(credit) AS credit,amount, SUM(amount)  AS amount  ");
if($searchtype =="Salesperson"){
	if($supplier!="all"){
	
$this->db->where('salesperson', $supplier);
}


}else{
	
if($supplier!="all"){
	
$this->db->where('customer', $supplier);
}
	
	
}



if($salestype!="all"){
	
$this->db->where("(type='Credit' OR type='Invoice')", NULL, FALSE);
}

if($pending!="all"){
	
$this->db->where('credit >', 0);
}



$this->db->where('companyid',$companyid);
if($download !="bulk"){
	$this->db->where('dateposted >=', $fromdate);
}

$this->db->where('dateposted <=', $todate);
 $this->db->order_by('id','ASC');

	// $this->db->group_by('customer');
$this->db->from('sales');


		
	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}





function viewagingreport($fromdate,$todate,$companyid,$supplier,$reporttype,$duration,$branchname,$download,$group){
//
if($group !="all"){
$this->db->select("*,sales.id ,sales.invoice,sales.credit , SUM(sales.credit) AS credit,sales.amount, SUM(sales.amount)  AS amount , SUM(sales.total)  AS total, SUM(sales.balance)  AS balance ");
}else{
$this->db->join('people', 'sales.customer = people.id','INNER JOIN');

//$this->db->select("sales.id as InVoiceNo,people.company,people.firstname,sales.invoice,sales.credit,sales.amount,sales.balance, sales.dateposted,sales.due_date");
}

if($supplier!="all"){
	
$this->db->where('sales.customer', $supplier);
}
	
$this->db->where("(type = 'Credit' OR type = 'Invoice')", NULL, FALSE);

	if($branchname !="all"){
$this->db->where('sales.branch', $branchname);
	}

if($reporttype!="all"){
	
$this->db->where('credit >', 0);
if ($duration=="1801"){
	
$this->db->where('dateposted <=', $todate);	
}else{

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);	
}

}


$this->db->where('sales.companyid',$companyid);

if($group != "all"){
	$this->db->group_by('sales.customer');
}


$this->db->from('sales');


		 $this->db->order_by('sales.id','desc');
		 $this->db->order_by('sales.customer','ASC');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

		        $filename = 	$new_date."Invoice.csv";

		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   
   
   
   


}else{

  return false;
}



}



function viewproductmovement2($fromdate,$todate,$companyid,$code,$branchname ,$tobranch){

if($fromdate!=null){
	

if($branchname=="all"){
	
	$this->db->select(" * from  newstock where   item_id='$code' and   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");

}else if($branchname=="Store"){
	
	$this->db->select(" * from  newstock where   item_id='$code' and  type='Main' and date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");

}else{
	
	$this->db->select(" * from  newstock where   item_id='$code' and  type='$branchname' and date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");

	
}

	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}





function viewmergedsplittedbill($fromdate,$todate,$companyid,$code,$branch,$download){




if($branch !="all"){
$this->db->where('branchid', $branch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}


		if($download !="all"){
$this->db->where('type', $download);
	}



$this->db->where('companyid',$companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);

$this->db->from('splitbillmaster');




	if($code =="all"){
		// $this->db->order_by('branchid','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	

   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Damaged Items.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   
   


}else{

  return false;
}



}












function viewdamagedproducts($fromdate,$todate,$companyid,$code,$branch,$download,$supplier){



 if($download=="yes"){
	 $this->db->select("supplier  as name,	user,	qty,	details,	datedamaged,	incharge,	product,	entrydate	,original,	balance	,branchid,	invoice,	buyingprice	,ttbrice,
  ");
 }else{
	 
 }





if($branch !="all"){
$this->db->where('branchid', $branch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}


		if($code !="all"){
$this->db->where('product', $code);
	}

		if($supplier!="all"){
$this->db->where('supplier', $supplier);
	}



$this->db->where('companyid',$companyid);

$this->db->where('datedamaged >=', $fromdate);
$this->db->where('datedamaged <=', $todate);

$this->db->from('damages');




	if($code =="all"){
		 $this->db->order_by('branchid','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	

   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Damaged Items.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   
   


}else{

  return false;
}



}











function viewissuedproducts($fromdate,$todate,$companyid,$code,$branch){



if($branch !="all"){
$this->db->where('branchid', $branch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}


		if($code !="all"){
$this->db->where('item_id', $code);
	}



$this->db->where('companyid',$companyid);

$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);

$this->db->from('outgoingstock');




	if($branch =="all"){
		 $this->db->order_by('branchid','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function searchvisitorcheckins($fromdate,$todate,$companyid,$method,$branch,$type,$download){



if($branch !="all"){
$this->db->where('branchid', $branch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}


		if($method !="all"){
//$this->db->where('item_id', $code);
	}


$this->db->where('companyid',$companyid);

$this->db->where('checkindate >=', $fromdate);
$this->db->where('checkindate <=', $todate);

$this->db->from('checkin');





		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   
   
   
   
   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Checkin List.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   
   


}else{

  return false;
}



}









function searchvisitorfequency1($fromdate,$todate,$companyid,$branch,$download){

if($fromdate!=null){
	




	
	$this->db->select("SELECT *, COUNT(idno) AS Visited FROM checkin where   membership='Reciprocating Member'  and checkindate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and branchid='$branch' GROUP BY idno HAVING COUNT(idno) > 2 ");


	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}









	
function searchvisitorfequency($fromdate,$todate,$companyid,$branch,$download){
		
$query = $this->db->query("SELECT *, COUNT(idno) AS visited FROM checkin where companyid='$companyid'  and membership='Reciprocating Member' and checkindate BETWEEN '$fromdate'  and '$todate' and branchid='$branch'   GROUP BY idno HAVING COUNT(idno) > 2  ");


    if($query->num_rows() > 0)
    {
		return $query->result();
     
    }
    else
    {
        return false;
    }
   
   }
	






















function searchvisitorfequency111($fromdate,$todate,$companyid,$branch,$download){
 			$this->db->select("  * , COUNT(idno) AS Visited ");


if($branch !="all"){
$this->db->where('branchid', $branch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}
	$this->db->where("(membership = 'Reciprocating Member'     )", NULL, FALSE);

	//$this->db->select("SELECT *, COUNT(idno) AS Visited FROM checkin where   membership='Reciprocating Member'  and checkindate BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and branchid='$branch' GROUP BY idno HAVING COUNT(idno) > 2 ");

//O
$this->db->where('companyid',$companyid);

$this->db->where('checkindate >=', $fromdate);
$this->db->where('checkindate <=', $todate);
	$this->db->group_by('idno');

$this->db->from('checkin');





		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   
   
   
   
   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Checkin List.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   
   


}else{

  return false;
}



}













	function viewpeoplebymonth($fromdate,$todate,$companyid,$category,$branchid,$datetype,$status,$month){





if($category !="all"){
$this->db->where('category', $category);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}

	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}
		if($status !="all"){
$this->db->where('status', $status);
	}



$this->db->where('companyid',$companyid);
if($datetype=="new"){
		$this->db->where("( MONTH(createdate) ='$month'  )", NULL, FALSE);

	$this->db->where('createdate >=', $fromdate);
$this->db->where('createdate <=', $todate);
}else{
	
			$this->db->where("( MONTH(zip) ='$month'  )", NULL, FALSE);

	$this->db->where('zip >=', $fromdate);
$this->db->where('zip <=', $todate);

}


$this->db->from('people');




	

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}









	function viewpeople($fromdate,$todate,$companyid,$category,$branchid,$datetype,$status){





if($category !="all"){
$this->db->where('category', $category);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}

	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}
		if($status !="all"){
$this->db->where('status', $status);
	}



$this->db->where('companyid',$companyid);
if($datetype=="new"){
	$this->db->where('createdate >=', $fromdate);
$this->db->where('createdate <=', $todate);
}else{
	$this->db->where('zip >=', $fromdate);
$this->db->where('zip <=', $todate);
}


$this->db->from('people');




	

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}




function viewproductmovement($fromdate,$todate,$companyid,$code,$branchname ,$tobranch, $user){

if($user !="all"){
$this->db->where('userid', $user);
	}

if($tobranch !="all"){
$this->db->where('stockto', $tobranch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}

	if($branchname !="all"){
$this->db->where('stockfrom', $branchname);
	}
		if($code !="all"){
			//
$this->db->where('item_id', $code);
	}



$this->db->where('companyid',$companyid);

$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);

$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

		 $this->db->order_by('id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}







function viewadvancereport($fromdate,$todate,$companyid,$category ,$branchid,$user){

if($user !="all"){
$this->db->where('advanceid', $user);
	}

if($branchid!="all"){
$this->db->where('branchid', $branchid);

	}






	if($category !="all"){
$this->db->where('description', $category);
	}
	

$this->db->where('companyid', $companyid);

$this->db->where('createdat >=', $fromdate);
$this->db->where('createdat <=', $todate);

$this->db->from('advancepayment');

		 $this->db->order_by('createdat','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}

















function viewproductionmovement($fromdate,$todate,$companyid,$code,$branchname ,$tobranch, $user,$category){

if($user !="all"){
$this->db->where('userid', $user);
	}

if($tobranch !="all"){
$this->db->where('type', $tobranch);
	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}



if($category !="Main"){
	

	if($code !="all"){
$this->db->where('uomproducts.code', $code);
	}

		$this->db->join('uomproducts', 'newstock.item_id =  uomproducts.uom','outer JOIN');


$this->db->where('newstock.stockto',"Automatic Production adjustment");

	//	$this->db->where("(type = '$tobranch'     )", NULL, FALSE);

	}else{
		if($code !="all"){
$this->db->where('item_id', $code);
	}
	
	$this->db->where('newstock.stockto <>',"Automatic Production adjustment");
	
		
	}







	if($branchname !="all"){
$this->db->where('newstock.stockfrom', $branchname);
	}
	


$this->db->where('newstock.companyid',$companyid);

$this->db->where('newstock.date >=', $fromdate);
$this->db->where('newstock.date <=', $todate);

$this->db->from('newstock');




	if($branchname =="all"){
		 $this->db->order_by('newstock.type','ASC');
	}

		 $this->db->order_by('newstock.id','desc');

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}



function viewallproductmovements($fromdate,$todate,$daterange,$branchname,$companyid,$code){


	if($branchname !="all"){
//$this->db->where('type', $branchname);
	$this->db->where("(type ='$branchname'  OR type ='Main'  )", NULL, FALSE);

	}
		if($code !="all"){
$this->db->where('item_id', $code);
	}
	if($daterange =="yes"){
$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);

	}


	 $this->db->order_by('id','DESC');
$this->db->where('companyid',$companyid);


$this->db->from('newstock');


							//$this->db->where('newstock.itemstatus <>', "sold");


	if($branchname =="all"){
		 $this->db->order_by('type','ASC');
	}

	

	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}



}





function viewoclosereport($startdate,$companyid,$branchid,$department){	
//$this->db->select("  * ");
$this->db->select("  * ,stock.id  as stockid ");

$this->db->where('startdate', $startdate);



if($branchid!="all"){
			$this->db->where('stock.branchid', $branchid);

}
if($department!="all"){
			$this->db->where('stock.type', $department);

}
$this->db->where('stock.companyid', $companyid);







$this->db->from('stock');


		$this->db->join('productlist', 'stock.code = productlist.code','INNER JOIN');
				$this->db->join('productcategory', 'productlist.category = productcategory.id','INNER JOIN');


	 $this->db->order_by('productcategory.description','DESC');

	 $this->db->order_by('productlist.id','asc');
			 $this->db->order_by('stock.closedate','DESC');


		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	



function viewopenclosereport($startdate,$closedate,$companyid,$branchid,$department,$stockstatus,$shift){	
$this->db->select("  * ");



if($stockstatus=="Closed"){
$this->db->where('startdate', $startdate);

$this->db->where('closedate', $closedate);
}else{
	$this->db->where('startdate', $startdate);


	
}









if($branchid!="all"){
			$this->db->where('stock.branchid', $branchid);

}
if($department!="all"){
			$this->db->where('stock.type', $department);

}
$this->db->where('stock.companyid', $companyid);
$this->db->where('stock.shift', $shift);








$this->db->from('stock');


		$this->db->join('products', 'stock.code = products.code','INNER JOIN');
				$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');

			 $this->db->order_by('stock.closedate','DESC');

	 $this->db->order_by('productcategory.description','DESC');

	 $this->db->order_by('products.id','asc');
			 $this->db->order_by('stock.closedate','DESC');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	


function viewopenclosereport22($startdate,$closedate,$companyid,$branchid,$department){

if($startdate!=null){

	if(($branchid=="all")&& ($department=="all")){
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code  WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  ORDER BY products.category ASC ");

	} else if(($branchid=="all")&& ($department!="all")){
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  and stock.type ='$department' ORDER BY products.category ASC ");

	}else if(($branchid!="all")&& ($department=="all")){
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  and stock.branchid ='$branchid' ORDER BY products.category ASC ");

	}else{

	$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  and stock.companyid='$companyid' and stock.branchid ='$branchid'  and stock.type ='$department' ORDER BY products.category ASC");
}
		//$this->db->join('products', 'stock.code = products.code','INNER JOIN');

				//$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');

	 		// $this->db->order_by('productcategory.description','DESC');

	 //$this->db->order_by('products.id','asc');
		
	// $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}


function viewdailycollectionreport($startdate,$closedate,$companyid,$branchid){

if($startdate!=null){

	if($branchid=="all"){
		
$this->db->select(" * from  dailycollections where   entrydate BETWEEN '$startdate'  and '$closedate' and companyid='$companyid'");

	}else{
		
$this->db->select(" * from  dailycollections where   entrydate BETWEEN '$startdate'  and '$closedate' and companyid='$companyid' and branchid='$branchid' ");

}
	
	 $this->db->order_by('id','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}









function  viewstocktakingreport($startdate,$companyid,$branchid,$maincategory,$orderamount){
	//$this->db->select("  * ");
	$this->db->select("  * ,stocktaking.id  as id ,stocktaking.branchid  as branchid");

$this->db->where('startdate', $startdate);
$this->db->where('stocktaking.companyid',$companyid);	

$this->db->where("(stocktaking.updatedby !=''  OR stocktaking.updatedby > 0 )", NULL, FALSE);

//$this->db->where('stocktaking.updatedby <>', 0);


	
		
	if($branchid !="all"){
			if($branchid=="Main"){
	$this->db->where('stocktaking.type', "Main");
		
	}else{
		
		
		
$this->db->where('stocktaking.type', "branch");
		$this->db->where('stocktaking.branchid',$branchid);	
		
	}
	}
	
		if($maincategory!="all"){
	$this->db->where('stocktaking.maincategory',$maincategory);	

		
	}
	
	
		if($orderamount !="all"){
		//$this->db->where('orderamount',$orderamount);	

	}
	
			///$this->db->where('orderamount <>',"production");	

			//$this->db->where('orderamount <>',"Sub-Single-item");



	
	
					$this->db->from('stocktaking');


	//	$this->db->join('products', 'stocktaking.code = products.code','INNER JOIN');




		if($maincategory!="all"){
		//$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');

			// $this->db->order_by('productcategory.description','DESC');

	}
			 $this->db->order_by('stocktaking.startdate','DESC');


	 $this->db->order_by('stocktaking.id','asc');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	


function  viewstocktakingreportMain($startdate,$companyid,$branchid,$maincategory,$orderamount,$type){
	//$this->db->select("  * ");
	 if($type=="download"){
		 	$this->db->select(" stocktaking.code ,products.name,stocktaking.open as Available,products.buyingprice,products.sellingprice,stocktaking.added as Actual,stocktaking.balance as Variance,stocktaking.closedate,stocktaking.startdate");

	 }else{
		$this->db->select("  * ,stocktaking.id  as id ,stocktaking.branchid  as branchid");
	 
	 }

$this->db->where('startdate', $startdate);
$this->db->where('stocktaking.companyid',$companyid);	

$this->db->where("(stocktaking.updatedby !=''  OR stocktaking.updatedby > 0 )", NULL, FALSE);

//$this->db->where('stocktaking.updatedby <>', 0);


	
		
	if($branchid!="all"){
			if($branchid=="Main"){
	$this->db->where('stocktaking.type', "Main");
		
	}else{
		
		
		
$this->db->where('stocktaking.type', "branch");
		$this->db->where('stocktaking.branchid',$branchid);	
		
	}
	}
	
		if($maincategory!="all"){
	$this->db->where('stocktaking.maincategory',$maincategory);	

		
	}
	
	
		if($orderamount !="all"){
		$this->db->where('orderamount',$orderamount);	

	}
	
	


	
	
					$this->db->from('stocktaking');


		$this->db->join('products', 'stocktaking.code = products.code','INNER JOIN');




		if($maincategory!="all"){
		//$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');

			// $this->db->order_by('productcategory.description','DESC');

	}
			 $this->db->order_by('stocktaking.startdate','DESC');


	 $this->db->order_by('stocktaking.id','asc');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

    
   if($type=="download"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Stock Taking Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}


}else{

  return false;
}
   }
	








function  viewstocktakingreportserial($startdate,$companyid,$branchid){
	//$this->db->select("  * ");
	$this->db->select("  * ,serialstocktaking.id  as id ,serialstocktaking.branchid  as branchid");

$this->db->where('startdate', $startdate);

	

	if($branchid=="all") {
$this->db->where('serialstocktaking.companyid',$companyid);


	}else if($branchid !="all") {
$this->db->where('serialstocktaking.companyid',$companyid);	
		$this->db->where('serialstocktaking.branchid',$branchid);	

	

	}else if($branchid=="Main Store") {
			
		$this->db->where('serialstocktaking.type', "Main Store");
		$this->db->where('serialstocktaking.companyid',$companyid);	
	
		
		
		
	}

	
	
	
					$this->db->from('serialstocktaking');


		$this->db->join('products', 'serialstocktaking.code = products.code','INNER JOIN');
		//$this->db->join('productcategory', 'productlist.category = productcategory.id','INNER JOIN');

			 $this->db->order_by('serialstocktaking.startdate','DESC');


	 $this->db->order_by('serialstocktaking.id','desc');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	







function  viewstocountedserial($startdate,$companyid,$branchid,$code){
	$this->db->select("  * ,serialstocktaking.id  as id ,serialstocktaking.branchid  as branchid");

$this->db->where('startdate', $startdate);

	$this->db->where('serialstocktaking.code', $code);
$this->db->where('serialstocktaking.companyid',$companyid);
		$this->db->where('serialstocktaking.branchid',$branchid);	

					$this->db->from('serialstocktaking');


		$this->db->join('productlist', 'serialstocktaking.code = productlist.code','INNER JOIN');

			 $this->db->order_by('serialstocktaking.startdate','DESC');


	 $this->db->order_by('productlist.id','asc');

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}
   }
	











function  viewmissingserial($startdate,$companyid,$branchid,$code,$serial){
	$this->db->select("  * ,serialstocktaking.id  as id ,serialstocktaking.branchid  as branchid");

$this->db->where('serialstocktaking.startdate', $startdate);
	$this->db->where('serialstocktaking.serial', $serial);

	$this->db->where('serialstocktaking.code', $code);
$this->db->where('serialstocktaking.companyid',$companyid);
		$this->db->where('serialstocktaking.branchid',$branchid);	

					$this->db->from('serialstocktaking');


		//$this->db->join('serials', 'serialstocktaking.serial = serials.serial','INNER JOIN');

			 $this->db->order_by('serialstocktaking.serial','DESC');


		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  // return $bioquery->result();
   
  return true;


}else{

  return false;
}
   }
	


function viewstocktakingreport22($startdate,$companyid,$branchid,$maincategory){
//$ready.
if($startdate!=null){

	if(($branchid=="all")&& ($maincategory=="all")) {
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='branch'  and  stocktaking.companyid='$companyid'  ORDER BY products.category  ASC");

	}else if(($branchid=="all")&& ($maincategory!="all")) {
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='branch'  and  stocktaking.companyid='$companyid'  and  stocktaking.maincategory='$maincategory' ORDER BY products.category  ASC");

	}else if(($branchid!="all")&& ($maincategory=="all")) {
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='branch'  and  stocktaking.companyid='$companyid'  and  stocktaking.branchid='$branchid' ORDER BY products.category  ASC");

	}else if(($branchid!="all")&& ($branchid!="Main Store") && ($maincategory!="all")) {
		
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='branch'  and  stocktaking.companyid='$companyid'  and  stocktaking.branchid='$branchid' and  stocktaking.maincategory='$maincategory' ORDER BY products.category  ASC");

	}
	
		else if(($branchid=="Main Store")&& ($maincategory=="all")) {
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='Main Store'  and  stocktaking.companyid='$companyid'  ORDER BY products.category  ASC");

	}else if(($branchid=="Main Store")&& ($maincategory!="all")) {
//$this->db->select(" * from  stock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
	$this->db->select(" *,stocktaking.id FROM stocktaking INNER JOIN products on  stocktaking.code= products.code WHERE stocktaking.startdate ='$startdate'  and   stocktaking.type ='Main Store'  and  stocktaking.companyid='$companyid'  and  stocktaking.maincategory='$maincategory' ORDER BY products.category  ASC");

	}
	




	
	// $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}



















function viewincomingstorereport($fromdate,$todate,$companyid,$supplier){

if($fromdate!=null){
	
	if($supplier=="all"){
$this->db->select(" * from  newstock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and type='store' ");
	
	}else{

$this->db->select(" * from  newstock where   date BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and supplier='$supplier' and type='store' ");
}
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}else{

  return false;
}


}




function count_allsales($tablename,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$etimsstatus){
	
	if($type !="all"){	
if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}

	}
	
	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
	
		
	if($etimsstatus !="all"){
$this->db->where('etimsstatus', $etimsstatus);
	}
	
	if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
		
	}
	
	


function count_allsalesCategory($tablename,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user,$mytables,$customer){
	
	if($type !="all"){	
	
	
	
if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}

	}

	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
	
	
	if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	if($mytables !="all"){
			$this->db->where('sales.mytable', $mytables);
	}
		
	
			
	if($customer !="all"){
$this->db->where('sales.customer', $customer);
	}
		
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
		
	}
	


















function viewgroupsalesreportTOTALCATEGORY($limit, $start,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user,$mytable,$customer){
	
	//$this->db->limit($limit, $start);
//$this->db->select("  * ,sales.id  AS id, ");




 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 
	 	



	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branch');
	}
			
					
		
				
			
	

	}
	
 
$this->db->select('total, SUM(total) AS total', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);




	if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}
	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		
	if($user !="all"){
$this->db->where('userid', $user);
	}
	
	if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	
	
			
	if($customer !="all"){
$this->db->where('sales.customer', $customer);
	}
		
	
	
			if($mytable !="all"){
$this->db->where('sales.mytable', $mytable);
	}
		
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
$this->db->from('sales');


//$this->db->join('sales_order', 'sales.invoice = sales_order.invoice','INNER JOIN');



$this->db->group_by('sales.companyid');
	
	 $this->db->order_by('sales.dateposted','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	
		
		
		
		
		
		
		return $bioquery->result_array();

		
	

   


}else{

  return false;
}




}



 
function viewgroupsalesreportTOTAL($limit, $start,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user,$etimsstatus){
	
	//$this->db->limit($limit, $start);
//$this->db->select("  * ,sales.id  AS id, ");


	if($etimsstatus !="all"){
$this->db->where('etimsstatus', $etimsstatus);
	}
	

 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 
	 	



	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branch');
	}
	
			
				
			
	

	}
	
	
	
	
	
 
$this->db->select('total, SUM(total) AS total', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);
					 	 $this->db->select('advance, SUM(advance) AS advance', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);




	if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}
	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		
	if($user !="all"){
$this->db->where('userid', $user);
	}
	
	if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	
	
	
	
	
	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
$this->db->from('sales');


//$this->db->join('sales_order', 'sales.invoice = sales_order.invoice','INNER JOIN');



$this->db->group_by('sales.companyid');
	
	 $this->db->order_by('sales.dateposted','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	
		
		
		
		
		
		
		return $bioquery->result_array();

		
	

   


}else{

  return false;
}




}


 
function viewgroupsalesreportTOTAL1($limit, $start,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user){
	
	//$this->db->limit($limit, $start);
//$this->db->select("  * ,sales.id  AS id, ");

	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);


 if($group !="no"){
	 
	 
	
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 
	 	



	//$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branch');
	}
	
			
				
			
	

	
	
 
$this->db->select('total, SUM(total) AS total', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);

}


	if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}
	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		
	if($user !="all"){
$this->db->where('userid', $user);
	}
	
	if($paymentstatus !="all"){
$this->db->where('paymentstatus', $paymentstatus);
	}
	
	
	
	
	
	
	
	

$this->db->from('sales');


//$this->db->join('sales_order', 'sales.invoice = sales_order.invoice','INNER JOIN');



$this->db->group_by('sales.companyid');
	
	 $this->db->order_by('sales.dateposted','asc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	
		
		
		
		
		
		
		return $bioquery->result_array();

		
	

   


}else{

  return false;
}




}




function viewgroupsalesreport($limit, $start,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user,$etimsstatus){
	
	$this->db->limit($limit, $start);



	if($download=="yes"){
		
		
//$this->db->select("  * ,sales.id  AS id,sales.cashier,sales.type,sales.amount,sales.due_date,sales.name,sales.balance,sales.userid,sales.dateposted,people.firstname, people.company,sales.customer,sales.cash,sales.total,sales.companyid,sales.salestype,sales.maindiscount,sales.discountedtotal,sales.branch,sales.card,sales.voucher,sales.credit,sales.points,sales.mpesa,sales.cheque,sales.ordersales,sales.description,sales.mytable,sales.maincategory,sales.creditnote,sales.invoice,sales.paymentstatus,sales.updatedby,sales.updatedat,sales.plotno,sales.houseno,sales.baddebt,sales.tax1,sales.tax2,sales.cardnumber,sales.savedcoins,sales.savedpoints,sales.salesperson,sales.receiptno,sales.change,sales.currency,sales.othercurrency,sales.othercurrencyamount,sales.tocurrencyamount,sales.currcptno,sales.totrcptno,sales.intrldata,sales.rcptsign,sales.sdcdatetime,sales.taxblAmtA,sales.taxblAmtB,sales.taxblAmtC,sales.taxblAmtD,sales.taxblAmtE,sales.taxAmtA,sales.taxAmtB,sales.taxAmtC,sales.taxAmtD,sales.taxAmtE,sales.taxRtA,sales.taxRtB,sales.taxRtC,sales.taxRtD,sales.taxRtE,sales.sdcId,sales.mrcNo,sales.salesdump,sales.receiptdump,sales.etimsstatus,sales.itemsstockmaster,sales.id  ");
$this->db->select("  * ,sales.id  AS id,sales.cashier,sales.type,sales.amount,sales.due_date,sales.name,sales.balance,sales.userid,sales.dateposted,sales.customer,sales.cash,sales.total,sales.companyid,sales.salestype,sales.maindiscount,sales.discountedtotal,sales.branch,sales.card,sales.voucher,sales.credit,sales.points,sales.mpesa,sales.cheque,sales.ordersales,sales.description,sales.mytable,sales.maincategory,sales.creditnote,sales.invoice,sales.paymentstatus,sales.updatedby,sales.updatedat,sales.plotno,sales.houseno,sales.baddebt,sales.tax1,sales.tax2,sales.cardnumber,sales.savedcoins,sales.savedpoints,sales.salesperson,sales.receiptno,sales.change,sales.currency,sales.othercurrency,sales.othercurrencyamount,sales.tocurrencyamount,sales.currcptno,sales.totrcptno,sales.intrldata,sales.rcptsign,sales.sdcdatetime,sales.taxblAmtA,sales.taxblAmtB,sales.taxblAmtC,sales.taxblAmtD,sales.taxblAmtE,sales.taxAmtA,sales.taxAmtB,sales.taxAmtC,sales.taxAmtD,sales.taxAmtE,sales.taxRtA,sales.taxRtB,sales.taxRtC,sales.taxRtD,sales.taxRtE,sales.sdcId,sales.mrcNo,sales.salesdump,sales.receiptdump,sales.etimsstatus,sales.itemsstockmaster,sales.id  ");

//$this->db->join('people', 'sales.customer = people.id','outer JOIN');
	}else{
			$this->db->select("  * ,sales.id  AS id, ");	
		
	}



	if($etimsstatus !="all"){
$this->db->where('etimsstatus', $etimsstatus);
	}
	

 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	
	 
	 
	 
	 	$this->db->select('total, SUM(total) AS total', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);




	$this->db->group_by('mydateposted');
			$this->db->group_by('sales.companyid');
			
				if($branch !="all"){
	$this->db->group_by('branch');
	}
			
			
				
			/*		
				if($mytable !="all"){
$this->db->where('sales.mytable', $mytable);
	}
			
	*/

	}
	
 





	 //$this->db->select('mpesa, SUM(mpesa) AS ttmpesa', FALSE);
	 //$this->db->select('cash, SUM(cash) AS ttcash', FALSE);

		 	 //$this->db->select('credit, SUM(credit) AS ttcredit', FALSE);

			 	// $this->db->select('voucher, SUM(voucher) AS ttvoucher', FALSE);

				 	 //$this->db->select('cheque, SUM(cheque) AS ttcheque', FALSE);

					 //	 $this->db->select('card, SUM(card) AS ttcard', FALSE);

						 	 //$this->db->select('points, SUM(points) AS ttpoints', FALSE);
		
	if($type !="all"){
		
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}
	
	if($branch !="all"){
$this->db->where('branch', $branch);
	}
		
	if($user !="all"){
$this->db->where('userid', $user);
	}
	
	
	
	
	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
	 $this->db->order_by('sales.id','desc');

$this->db->from('sales');

	if($type=="Invoice"){
//$this->db->select('idnumber, AS PIN', FALSE);

//$this->db->join('people', 'sales.customer = people.id','INNER JOIN');
	
		}



//$this->db->group_by('sales.invoice');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Sales Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}

   


}else{

  return false;
}




}



function viewgroupsalesreportCategory($limit, $start,$fromdate,$todate,$type,$companyid,$branch,$paymentstatus,$saletypes,$download,$group,$user,$mytable,$customer,$display){
	
	$this->db->limit($limit, $start);
$this->db->select("  * ,sales.id  AS id, ");





 if($group !="no"){
	 if($group =="Daily"){
		
  	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Weekly"){
		
  	$this->db->select('dateposted, WEEK(dateposted) AS mydateposted', FALSE);
	
	 }else  if($group =="Monthly"){
		
  	$this->db->select('dateposted, MONTH(dateposted) AS mydateposted', FALSE);
	
	}else  if($group =="Yearly"){
		
  	$this->db->select('dateposted, YEAR(dateposted) AS mydateposted', FALSE);
	
	 }else{
		   	$this->db->select('dateposted, DATE(dateposted) AS mydateposted', FALSE);

		 
	 }
	 
	 
	 
	 
	 
	 
	 	$this->db->select('total, SUM(total) AS total', FALSE);

 	$this->db->select('amount, SUM(amount) AS amount', FALSE);
 	$this->db->select('cash, SUM(cash) AS cash', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);
							 $this->db->select('tax1, SUM(tax1) AS tax1', FALSE);
							 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);




	$this->db->group_by('mydateposted');
			$this->db->group_by('companyid');
			
				if($branch !="all"){
	$this->db->group_by('branch');
	}
			
			
				
		
		
	

	}
				if($mytable !="all"){
$this->db->where('sales.mytable', $mytable);
	}
		
 		
	if($customer !="all"){
$this->db->where('sales.customer', $customer);
	}
		




	 //$this->db->select('mpesa, SUM(mpesa) AS ttmpesa', FALSE);
	 //$this->db->select('cash, SUM(cash) AS ttcash', FALSE);

		 	 //$this->db->select('credit, SUM(credit) AS ttcredit', FALSE);

			 	// $this->db->select('voucher, SUM(voucher) AS ttvoucher', FALSE);

				 	 //$this->db->select('cheque, SUM(cheque) AS ttcheque', FALSE);

					 //	 $this->db->select('card, SUM(card) AS ttcard', FALSE);

						 	 //$this->db->select('points, SUM(points) AS ttpoints', FALSE);
		
	if($type !="all"){
		
		if($type=="Invoice"){
							$this->db->where("(type ='Invoice'  OR type ='Credit'  )", NULL, FALSE);
	
		}else{
		if($type=="Cash"){

$this->db->where('type <>', "Invoice");
$this->db->where('type <>', "Credit");
		}else{
		$this->db->where($type." <> ", 0);	
			
		}
		
		}


	}
	
	if($branch !="all"){
$this->db->where('sales.branch', $branch);
	}
		
	if($user !="all"){
$this->db->where('sales.userid', $user);
	}
	
	if($paymentstatus !="all"){
$this->db->where('sales.paymentstatus', $paymentstatus);
	}
	
	
	
	
	
	
	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('sales.dateposted >=', $fromdate);
$this->db->where('sales.dateposted <=', $todate);
	 $this->db->order_by('sales.id','desc');


	if($display=="detailed"){
$this->db->join('sales', 'sales_order.invoice = sales.invoice','INNER JOIN');

		
$this->db->from('sales_order');

		}else{
			
		$this->db->from('sales');
	
			
		}



//$this->db->group_by('sales.invoice');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	
	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Sales Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}

   


}else{

  return false;
}




}

 

function viewtotalsalescurrencyreport($finalfromdate,$finaltodate,$companyid,$branchname,$download,$currency){


	  $this->db->select('othercurrency');
	  $this->db->select('othercurrencyamount');

	  $this->db->select('branch');
	 
	 	$this->db->select('total, SUM(total) AS total', FALSE);
	 	$this->db->select('othercurrencyamount, SUM(othercurrencyamount) AS othercurrencyamount', FALSE);

	 	 $this->db->select('maindiscount, SUM(maindiscount) AS maindiscount', FALSE);
	 	
	 	 $this->db->select('dateposted', FALSE);






	if(($branchname !="all")  &&($branchname !="allcombined"))  {
		
		
$this->db->where('branch', $branchname);
	}

	if($currency !="all"){
	$this->db->where('othercurrency', $currency);
 }

	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $finalfromdate);
$this->db->where('dateposted <=', $finaltodate);
$this->db->from('sales');

				
if($branchname =="all"){
	$this->db->group_by('branch');
	$this->db->group_by('companyid');
}else  if($branchname =="allcombined"){
	$this->db->group_by('companyid');
}else{
	$this->db->group_by('companyid');	
	}
			
			
			
				

		
	
		$this->db->group_by('othercurrency');	


$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Sales Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}

   


}else{

  return false;
}




}



 







function viewtotalsalesreport($finalfromdate,$finaltodate,$companyid,$branchname,$download){



	  $this->db->select('branch', FALSE);
	 
	 	$this->db->select('total, SUM(total) AS total', FALSE);

	 	 $this->db->select('maindiscount, SUM(maindiscount) AS maindiscount', FALSE);
	 	
	 	 $this->db->select('dateposted', FALSE);







	if($branchname !="all"){
$this->db->where('branch', $branchname);
	}

	

	
	
	
$this->db->where('sales.companyid', $companyid);

$this->db->where('dateposted >=', $finalfromdate);
$this->db->where('dateposted <=', $finaltodate);
$this->db->from('sales');

				
if($branchname =="all"){
	$this->db->group_by('branch');
	//$this->db->group_by('companyid');
	
	
	}else{
	//$this->db->group_by('companyid');	
	}
			
			
			

	
	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
	

	if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Sales Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
			
		
		
		
		
		
		
		
		return $bioquery->result();

		
	}

   


}else{

  return false;
}




}



 











function getlastfeedback($mykey,$companyid){
	   $this -> db -> from('taskdata');

$this->db->select("mykey,date,exactdate");


	
$this->db->where('companyid', $companyid);
$this->db->where('mykey', $mykey);

 $this -> db -> limit(1);	
 	 $this->db->order_by('id','desc');

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}


function calculatetasktarget($mykey,$companyid){
	   $this -> db -> from('taskdata');

$this->db->select("  * ,achieved,SUM(achieved) AS achieved ");


	
$this->db->where('companyid', $companyid);
$this->db->where('mykey', $mykey);

$this->db->group_by('mykey');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}


function viewserialconfirmation($companyid,$itemid){
	   $this -> db -> from('serials');

$this->db->select("  * ,serials.id  AS id,qty,SUM(qty) AS qty ");


	
$this->db->where('companyid', $companyid);
$this->db->where('itemid', $itemid);

$this->db->group_by('itemid');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}




function viewprojectonfirmation($companyid,$invoice){
	   $this -> db -> from('projectsbudget');

$this->db->select("  expamount,SUM(expamount) AS expamount, qty,SUM(qty) AS qty ");


	
$this->db->where('companyid', $companyid);
$this->db->where('projectid', $invoice);

$this->db->group_by('projectid');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}



function viewsalesconfirmation($companyid,$invoice){
	   $this -> db -> from('sales_order');

$this->db->select(" sales_order.id  AS id,amount,SUM(amount) AS myamount ");


	
$this->db->where('companyid', $companyid);
$this->db->where('invoice', $invoice);

$this->db->group_by('invoice');
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}

function totalstock($companyid,$code){
	   $this -> db -> from('productlist');

$this->db->select("  productlist.id  AS id,qty,SUM(qty) AS qty,companyid ");


$this->db->where('companyid', $companyid);
$this->db->where('code', $code);
$this->db->where('status <> ', "inactive");

$this->db->group_by('code');
	$this->db->group_by('companyid');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}



function viewgroupsalesreport222($fromdate,$todate,$type,$companyid,$branch,$paymentstatus){

if($fromdate!=null){
	
	
	
	
		

  $this -> db -> select('*');

   $this -> db -> from('sales');
		if($branch!="all"){
		$this->db->where('branch',$branch);
			}
			if($paymentstatus!="all"){
		$this->db->where('paymentstatus',$paymentstatus);
			}
	$this->db->where('companyid',$companyid);
//$this->db->where('dateposted >=', $fromdate);
//$this->db->where('dateposted <=', $todate);
		  	$this->db->where("( dateposted BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
	$this->db->select('cash, SUM(cash) AS amount', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);
						 	 $this->db->select('creditnote, SUM(creditnote) AS creditnote', FALSE);

	
	
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}





function viewgroupsalesreport2($fromdate,$todate,$type,$companyid,$branch,$paymentstatus){

if($fromdate!=null){
	if(($type=="all") && ($branch=="all") && ($paymentstatus=="all") ){
$this->db->select(" * from  sales where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");
	}else 
	if(($type=="all") && ($branch=="all") && ($paymentstatus!="all")){
$this->db->select(" * from  sales where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and paymentstatus='$paymentstatus'  ");
	}else if(($type=="all") && ($branch!="all") && ($paymentstatus=="all")){
$this->db->select(" * from  sales where branch='$branch' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");
	}else if(($type=="all") && ($branch!="all") && ($paymentstatus!="all")){
$this->db->select(" * from  sales where branch='$branch' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and paymentstatus='$paymentstatus' ");
	}
		else if(($type!="all") && ($branch=="all") && ($paymentstatus=="all")){
$this->db->select(" * from  sales where type='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");
	}
	
	else if(($type!="all") && ($branch=="all") && ($paymentstatus!="all")  ){
$this->db->select(" * from  sales where type='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and paymentstatus='$paymentstatus'  ");
	}else {
$this->db->select(" * from  sales where branch='$branch'  and   type ='$type'   and dateposted BETWEEN '$fromdate'  and '$todate'and companyid='$companyid'  and paymentstatus='$paymentstatus' ");
	}
	
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}




function viewgroupsalesreportreading($fromdate,$todate,$type,$companyid){

if($fromdate!=null){
	
	if($type=="all"){
$this->db->select(" * from  salesreading where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' ");
	}else
if($type==""){
$this->db->select(" * from  salesreading where   dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'");
}else {
$this->db->select(" * from  salesreading where   type ='$type'   and dateposted BETWEEN '$fromdate'  and '$todate'and companyid='$companyid' ");
	}
	
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}





function viewsalesreportreading($fromdate,$todate,$type,$companyid,$maincategory){

if($fromdate!=null){

	if(($type=="all") && ($maincategory=="all") ){
$this->db->select(" * from  sales_reading where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and salestype='wholesale'   ");
	}else if(($type!="all") && ($maincategory=="all") ){
$this->db->select(" * from  sales_reading where   type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and salestype='wholesale'");
}else if(($type=="all") && ($maincategory!="all") ){
$this->db->select(" * from  sales_reading where    maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and salestype='wholesale' ");

}else if(($type!="all") && ($maincategory!="all") ){
$this->db->select(" * from  sales_reading where  type ='$type'  and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and salestype='wholesale' ");
}



else {
$this->db->select(" * from  sales_reading where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'    ");
	}
	

	
	
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}


function viewsalesmovementreport($fromdate,$todate,$type,$companyid,$maincategory,$branch,$category){

if($fromdate!=null){
	if(($type=="all") && ($maincategory=="all")&& ($branch=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'  ");
	}else
	if(($type=="all") && ($maincategory=="all")&& ($branch!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from sales_order where    companyid='$companyid' and branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate'   and status<>'returned'  ");
	}else if(($type!="all") && ($branch!="all") && ($maincategory=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from  sales_order where   branch ='$branch' and type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned' ");
}else if(($type!="all") && ($maincategory!="all") && ($branch=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from  sales_order where   type ='$type'and maincategory='$maincategory' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and status<>'returned' ");
}else if(($type=="all") && ($branch=="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from  sales_order where    maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned' ");

}else if(($type=="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from sales_order where  branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned' ");
}else if(($type!="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,SUM(tax2) AS mytax2  from  sales_order where  type ='$type' and branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned'  ");
}



else {
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,SUM(tax2) AS mytax2  from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'   ");
	}
	

	
	$this->db->group_by('code');
	$this->db->group_by('branch');

	 $this->db->order_by('myqty',$category);
$bioquery = $this -> db -> get();
 $this -> db -> limit(10);
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}
function viewsalesreport($fromdate,$todate,$type,$companyid,$maincategory,$branch){

if($fromdate!=null){
	if(($type=="all") && ($maincategory=="all")&& ($branch=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty ,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2    from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'  ");
	}else
	if(($type=="all") && ($maincategory=="all")&& ($branch!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2  from sales_order where    companyid='$companyid' and branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate'   and status<>'returned'  ");
	}else if(($type!="all") && ($branch!="all") && ($maincategory=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where   branch ='$branch' and type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status<>'returned' ");
}else if(($type!="all") && ($maincategory!="all") && ($branch=="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where   type ='$type'and maincategory='$maincategory' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and status<>'returned' ");
}else if(($type=="all") && ($branch=="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax ,tax2, SUM(tax2) AS mytax2 from  sales_order where    maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned' ");

}else if(($type=="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2  from sales_order where  branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned' ");
}else if(($type!="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2  from  sales_order where  type ='$type' and branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status<>'returned'  ");
}



else {
$this->db->select("  * ,amount, SUM(amount) AS myamount,qty, SUM(qty) AS myqty,tax, SUM(tax) AS mytax,tax2, SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status<>'returned'   ");
	}
	

		$this->db->group_by('sprice');

	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');

	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}




function viewsalesreportreturned($fromdate,$todate,$type,$companyid,$maincategory,$branch){




  $this -> db -> select('*');
//$this->db->select("  * ,tax, SUM(tax) AS tax,tax2,SUM(tax2) AS tax2,amount,SUM(amount) AS amount ");
	 $this->db->select('amount AS myamount', FALSE);
	 $this->db->select('qty AS myqty', FALSE);
	 $this->db->select('tax AS mytax', FALSE);
	 	 $this->db->select('tax2 AS mytax2', FALSE);

	// $this->db->select('sprice, SUM(sprice) AS sprice', FALSE);




   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
     if($maincategory !="all"){
	     //	$this->db->where('maincategory',$maincategory);

   }
	 $this->db->where('status',"returned");
	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( updatedat BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	





	 //$this->db->group_by('vattype');

	// $this->db->group_by('companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function viewsalesreportreturnedNow($fromdate,$todate,$type,$companyid,$maincategory,$branch,$user){




  $this -> db -> select('*');
//$this->db->select("  * ,tax, SUM(tax) AS tax,tax2,SUM(tax2) AS tax2,amount,SUM(amount) AS amount ");
	 $this->db->select('amount AS myamount', FALSE);
	 $this->db->select('qty AS myqty', FALSE);
	 $this->db->select('tax AS mytax', FALSE);
	 	 $this->db->select('tax2 AS mytax2', FALSE);

	// $this->db->select('sprice, SUM(sprice) AS sprice', FALSE);


   if (!isset($user)){

    
     $user="all";
    }
    else {

     $user=$user;

 }


	    if($user!="all"){
	     	$this->db->where('updatedby',$user);

		}
	 

   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
     if($maincategory !="all"){
	     //	$this->db->where('maincategory',$maincategory);

   }
	 $this->db->where('status',"returned");
	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( updatedat BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	





	 //$this->db->group_by('vattype');

	// $this->db->group_by('companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function viewsalesreportsuspended($fromdate,$todate,$type,$companyid,$maincategory,$branch){




  $this -> db -> select('*');
//$this->db->select("  * ,tax, SUM(tax) AS tax,tax2,SUM(tax2) AS tax2,amount,SUM(amount) AS amount ");
	 $this->db->select('amount AS myamount', FALSE);
	 $this->db->select('qty AS myqty', FALSE);
	 $this->db->select('tax AS mytax', FALSE);
	 	 $this->db->select('tax2 AS mytax2', FALSE);

	// $this->db->select('sprice, SUM(sprice) AS sprice', FALSE);

	 

   $this -> db -> from('sales_ordercancelled');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
     if($maincategory !="all"){
	     //	$this->db->where('maincategory',$maincategory);

   }
	 //$this->db->where('status',"returned");
	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	





	 //$this->db->group_by('vattype');

	// $this->db->group_by('companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}
function viewsalesreportreturned22($fromdate,$todate,$type,$companyid,$maincategory,$branch){

if($fromdate!=null){
	if(($type=="all") && ($maincategory=="all")&& ($branch=="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status='returned'  ");
	}else
	if(($type=="all") && ($maincategory=="all")&& ($branch!="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2   from sales_order where    companyid='$companyid' and branch='$branch'  and dateposted BETWEEN '$fromdate'  and '$todate'   and status='returned'  ");
	}else if(($type!="all") && ($branch!="all") && ($maincategory=="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2    from  sales_order where   branch ='$branch' and type ='$type' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and userid='$userid' and status='returned'  ");
}else if(($type!="all") && ($maincategory!="all") && ($branch=="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2   from  sales_order where   type ='$type'and maincategory='$maincategory' and  dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid'  and status='returned'  ");
}else if(($type=="all") && ($branch=="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2  from  sales_order where    maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='returned'  ");

}else if(($type=="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2   from sales_order where  branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='returned'  ");
}else if(($type!="all") && ($branch!="all") && ($maincategory!="all") ){
$this->db->select("  * * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2   from  sales_order where  type ='$type' and branch='$branch' and maincategory='$maincategory' and dateposted BETWEEN '$fromdate'  and '$todate' and companyid='$companyid' and status='returned'   ");
}



else {
$this->db->select("  * ,amount AS myamount,qty AS myqty ,tax AS mytax,SUM(tax2) AS mytax2    from sales_order where    companyid='$companyid' and dateposted BETWEEN '$fromdate'  and '$todate'  and status='returned'   ");
	}
	

	
	$this->db->group_by('code');
	$this->db->group_by('branch');
	$this->db->group_by('serial');
	 $this->db->order_by('id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}


public function countfollowup($finalid,$companyid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM followups WHERE finalid='$finalid' and companyid='$companyid'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
	 function newdealsfeedbacklookup($keyword,$companyid){ 
        $this->db->select('*')->from('dealsfeedback'); 
			   $this->db->where('companyid',$companyid);
        $this->db->like('name',$keyword,'both'); 

		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 
	
	
	
		 function newcategories($keyword,$companyid){ 
        $this->db->select('*')->from('category'); 
			   $this->db->where('companyid',$companyid);
        $this->db->like('name',$keyword,'both'); 

		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    }





function getitmclassificationcodes($keyword,$companyid,$database){ 


$this->db->select(" * from ".$database." where name LIKE '%" . $this->db->escape_like_str($keyword) . "%'  or itm_class_code LIKE '%" . $this->db->escape_like_str($keyword) . "%'     ORDER BY name ASC");
	// $this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}





	 function itemscodeslookup($keyword,$companyid,$database){ 
        $this->db->select('*')->from($database); 
			  // $this->db->where('companyid',$companyid);
        $this->db->like('name',$keyword,'both'); 
		 	 $this->db->or_like('description',$keyword,'both');

		 $this -> db -> limit(20);
        $query = $this->db->get();     
        return $query->result(); 
    }












 function newdealslookup($keyword,$companyid){ 
        $this->db->select('*')->from('deals'); 
		   // $this->db->where('category',"supplier");
			 //   $this->db->where('companyid',$companyid);
        $this->db->like('contact',$keyword,'both'); 
		 $this->db->or_like('owner',$keyword,'both');
		 	 $this->db->or_like('contactname',$keyword,'both');
			 	 $this->db->or_like('comments',$keyword,'both');
				 			
		$this->db->where("(companyid=".$companyid." and status='active' OR status='Intrested' OR status='Not Intrested' )", NULL, FALSE);

		
        $this->db->where('status<>',"Closed"); 
		 $this->db->where('status<>',"Dont Call");
		 	 $this->db->where('status<>',"Dont Call");
		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 


 function newfollowup($keyword,$companyid,$owner){ 
        $this->db->select('*')->from('deals'); 
		   // $this->db->where('category',"supplier");
			 //   $this->db->where('companyid',$companyid);
        $this->db->like('contact',$keyword,'both'); 
		 $this->db->or_like('owner',$keyword,'both');
		 	 $this->db->or_like('contactname',$keyword,'both');
			 	 $this->db->or_like('comments',$keyword,'both');
				 			
		$this->db->where("(companyid=".$companyid."   and owner=".$owner.")", NULL, FALSE);

		
        //$this->db->where('companyid',$companyid); 
		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 





function returnuserlookup($keyword,$companyid){ 
//
$this->db->select(" * from people where firstname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category='supplier'   OR  lastname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  companyid='$companyid' and category='supplier'   OR company LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category='supplier'  OR company LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category='supplier' ORDER BY firstname ASC");
 $this -> db -> limit(20);

$query = $this->db->get();   
		
return $query->result(); 

}








 function returnuserlookup2($keyword,$companyid){ 
        $this->db->select('*')->from('people'); 
		    $this->db->where('category',"supplier");
			 //   $this->db->where('companyid',$companyid);
        $this->db->like('firstname',$keyword,'both'); 
		 $this->db->or_like('lastname',$keyword,'both');
		 	 $this->db->or_like('phone',$keyword,'both');
			 	 $this->db->or_like('idno',$keyword,'both');
				 			 	 $this->db->or_like('company',$keyword,'both');
		$this->db->where("(companyid=".$companyid." and category='supplier' )", NULL, FALSE);

		
        //$this->db->where('companyid',$companyid); 
		 $this -> db -> limit(20);
        $query = $this->db->get();     
        return $query->result(); 
    } 

 function returnlookupcc($keyword,$companyid){ 
        $this->db->select('*')->from('sales'); 
        $this->db->like('invoice',$keyword,'both'); 
		    $this->db->where('companyid',$companyid);
        //$this->db->where('companyid',$companyid); 
		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 




 function glaccountlookup($keyword,$companyid){ 
        $this->db->select('*')->from('glaccounts'); 
        $this->db->like('accountname',$keyword,'both'); 
		 $this->db->or_like('number',$keyword,'both');

		    $this->db->where('companyid',$companyid);
      
		 $this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 











 function branchlookup($keyword,$branchid){ 
        $this->db->select('*')->from('productlist'); 
       // $this->db->like('name',$keyword,'both'); 
		        $this->db->like('name',$keyword); 

        $this->db->or_like('code',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'description'); 
		 $this->db->where('branch',$branchid);
	//$this->db->where("(branch=".$branchid." and status='active' OR 	branch!=".$branchid." and status='global' )", NULL, FALSE);

		$this -> db -> limit(50);
        $query = $this->db->get();   
		
        return $query->result(); 
    }

function branchlookpurchase($keyword,$branchid){ 

//$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and orderamount!='accompaniment'  OR  shortname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' OR  name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'   and orderamount!='accompaniment'   OR code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status='active'  and orderamount!='accompaniment'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and status='global'  and orderamount!='accompaniment'  ORDER BY name ASC");

$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid'  and status !='inactive' and orderamount !='production'    OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid' and  status !='inactive'  and orderamount !='production'    OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status !='inactive'   and orderamount !='production'   ORDER BY name ASC");
	//$this->db->where("(  )", NULL, FALSE);


$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}	




function lookupquotation($keyword,$branchid){ 


$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid'     OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'    OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid'     ORDER BY name ASC");
	//$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid'  and status !='inactive'    OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid' and  status !='inactive'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status !='active'    ORDER BY name ASC");

	
	
	//$this->db->where("(  )", NULL, FALSE);


$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}	


function branchlookupstocktaking($keyword,$branchid){ 

//$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and orderamount!='accompaniment'  OR  shortname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' OR  name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'   and orderamount!='accompaniment'   OR code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status='active'  and orderamount!='accompaniment'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and status='global'  and orderamount!='accompaniment'  ORDER BY name ASC");

$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and orderamount!='production' and status !='inactive'    OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'  and orderamount!='production' and status !='inactive'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status !='active'  and orderamount!='production'   ORDER BY name ASC");
	//$this->db->where("(  )", NULL, FALSE);


$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}


function branchlookupddd($keyword,$branchid){ 

//$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and orderamount!='accompaniment'  OR  shortname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' OR  name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'  and orderamount!='accompaniment'  OR  description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  status='global'   and orderamount!='accompaniment'   OR code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status='active'  and orderamount!='accompaniment'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and status='global'  and orderamount!='accompaniment'  ORDER BY name ASC");

$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and orderamount!='accompaniment' and status !='inactive'    OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'  and orderamount!='accompaniment' and status !='inactive'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status !='inactive'  and orderamount!='accompaniment'   ORDER BY name ASC");
	//$this->db->where("(  )", NULL, FALSE);


$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}

function branchlookupdddStocklist($keyword,$branchid){ 


$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid'  and status !='inactive'    OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  branch='$branchid'   and status !='inactive'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and branch='$branchid' and status !='inactive'   ORDER BY name ASC");
	//$this->db->where("(  )", NULL, FALSE);


$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}


function lookupdispatch222($keyword,$companyid,$frombranch){ 
//
if($frombranch=="Main"){
	$this->db->select(" * from products where name LIKE '%" . $this->db->escape_like_str($keyword) . "%'  ORDER BY name ASC");

}else{
	$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%'  ORDER BY name ASC");

	
}
	// $this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}


function lookupdispatch($keyword,$companyid,$frombranch){ 

if($frombranch=="Main"){
	$this->db->select(" * from products where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  companyid='companyid'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  ORDER BY name ASC");

}else{
	$this->db->select(" * from productlist where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  companyid='companyid'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  and branch='$frombranch' ORDER BY name ASC");

	
}
	// $this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}







function lookup($keyword,$companyid){ 
//
$this->db->select(" * from products where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  companyid='companyid'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  ORDER BY name ASC");
	// $this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}

function assetlookup($keyword,$companyid){ 


$this->db->select(" * from assets where name LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  OR  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and  companyid='companyid'   OR description LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  ORDER BY name ASC");
	// $this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}






function branchlookupmpesa($keyword,$companyid){ 


$this->db->select(" * from sms_in where sender_number LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2'   or  code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2'    or firstname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2'   or lastname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' or amount LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' ORDER BY sent_dt DESC");
	$this -> db -> limit(10);

$query = $this->db->get();   
		
return $query->result(); 

}





function branchlookupmpesaperbranch($keyword,$companyid,$branchid){ 

$this->db->select(" * from sms_in where sender_number LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid'  OR code LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid'  OR firstname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid' or lastname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid'  or amount LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid'   ORDER BY sent_dt DESC");

//$this->db->select(" * from sms_in where sender_number LIKE '%" . $this->db->escape_like_str($keyword) . "%' and 	used='2' and branchid = '$branchid' $ORDER BY sent_dt DESC");
	$this -> db -> limit(10);

$query = $this->db->get();   
		
return $query->result(); 

}










 function branchlookupp($keyword,$branchid){ 
        $this->db->select('*')->from('productlist'); 
		$this->db->where("(branch=".$branchid." and status='active' OR 	branch!=".$branchid." and status='global' )", NULL, FALSE);
		//$this->db->or_where('code',$keyword);
			//$this->db->or_where('status', 'global');
        //$this->db->like('name',$keyword,'both'); 
        $this->db->like('name',$keyword,'both'); 
   //$this->db->or_where("(code LIKE '%".$keyword."%' and tatus='active'   )", NULL, FALSE);
 //$this->db->or_where("(productlist.code='$keyword' AND productlist.status='active')", null ,FALSE);
        //$this->db->like('code',$keyword,'both'); 
        //$this->db->like('name',$keyword); 
           // $this->db->or_like('code', $keyword, 'both');
		//$this->db->like('status', 'active');
//$this->db->where('status', 'global');
//$this->db->or_where_in('code', $keyword);

		 //$this->db->or_like('description',$keyword,'description'); 
		   //$this->db->where('status','global');

		$this->db->or_like("(code='$keyword'  and branch='$branchid' and status='active' )", NULL, FALSE);

		 $this -> db -> limit(20);
        $query = $this->db->get();   
		
        return $query->result(); 
    } 
	






 function storelookup($keyword,$companyid){ 
        $this->db->select('*')->from('store'); 
        $this->db->like('name',$keyword,'both'); 
        $this->db->or_like('code',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'description'); 
		   $this->db->where('companyid',$companyid);
		// $this -> db -> limit(10);
        $query = $this->db->get();   
		
        return $query->result(); 
    } 




 function lookup2($keyword,$companyid){ 
        $this->db->select('*')->from('products'); 
        $this->db->like('name',$keyword); 
        $this->db->or_like('code',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'description'); 
		   $this->db->where('companyid',$companyid);
		 $this->db->order_by('name','ASC');

        $query = $this->db->get();   
		
        return $query->result(); 
    } 
	
	 function lookuplist($keyword,$companyid,$branchid){ 
        $this->db->select('*')->from('productlist');
				$this->db->where('branch',$branchid);

        $this->db->like('name',$keyword,'both'); 
        $this->db->or_like('code',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'description'); 
//
	  $this->db->where('companyid',$companyid);

		 $this -> db -> limit(50);
        $query = $this->db->get();   
		
        return $query->result(); 
    } 
	
	
	
	
	
	 function lookupstore($keyword,$companyid){ 
        $this->db->select('*')->from('store'); 
        $this->db->like('name',$keyword,'both'); 
        $this->db->or_like('code',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'description'); 
		   $this->db->where('companyid',$companyid);
		// $this -> db -> limit(10);
        $query = $this->db->get();   
		
        return $query->result(); 
    } 
	
	
	
	

function confirmaccountcode($code){

if($code!=null){
$this->db->select(" * from smsconfig where code='$code' ");

$n = $this -> db -> get();
if($n->num_rows()>0){

  return $n->result();
  // return true;
}else{

  return false;
}

}


}

function confirmemail($email){

if($email!=null){
$this->db->select(" * from people where email='$email' ");

$n = $this -> db -> get();
if($n->num_rows()>0){

  return $n->result();
  // return true;
}else{

  return false;
}

}


}

function confirmphone($phone){

if($phone!=null){
$this->db->select(" * from people where phone='$phone' ");

$n = $this -> db -> get();
if($n->num_rows()>0){

  return $n->result();
  // return true;
}else{

  return false;
}

}


}



function uploadallproduct($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

  if($code!=null){
     $this->db->where('code',$code);
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('products');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('code',$code);
	  $this->db->where('companyid',$companyid);

      $this->db->update('products',$data);
   } else {
      
      $this->db->insert('products', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}

function uploadallproductlist($data)
  
{
   $code = $data['code'];
      $branch = $data['branch'];

      $companyid = $data['companyid'];

  if($code!=null){
     $this->db->where('code',$code);
	      $this->db->where('branch',$branch);

 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('productlist');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('code',$code);
	  $this->db->where('companyid',$companyid);

      $this->db->update('productlist',$data);
   } else {
      
      $this->db->insert('productlist', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}








function uploadallproductlistchild($data,$type)
  
{
   $code = $data['code'];
      $branch = $data['branch'];

      $companyid = $data['companyid'];

  if($code!=null){
     $this->db->where('code',$code);
	      $this->db->where('branch',$branch);
    $this->db->where('status',"child");
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('uomproducts');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('code',$code);
	  $this->db->where('companyid',$companyid);

      $this->db->update('uomproducts',$data);
   } else {
      
      $this->db->insert('uomproducts', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}










function uploadfollowups($data,$nextstep)
  
{

      $companyid = $data['companyid'];

  if($nextstep!=null){
    
	      $this->db->where('nextstep',$nextstep);
  
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('followups');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
        $this->db->where('nextstep',$nextstep);
  
		$this->db->where('companyid',$companyid);
      $this->db->update('followups',$data);
   } else {
      
      $this->db->insert('followups', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}






function uploadalldeals($data,$nextstep)
  
{

      $companyid = $data['companyid'];

  if($nextstep!=null){
    
	      $this->db->where('nextstep',$nextstep);
  
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('deals');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
        $this->db->where('nextstep',$nextstep);
  
		$this->db->where('companyid',$companyid);
      $this->db->update('deals',$data);
   } else {
      
      $this->db->insert('deals', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}





function uploadallcategoryproduct($data)
  
{
   $id = $data['id'];
      $companyid = $data['companyid'];

  if($id!=null){
     $this->db->where('id',$id);
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('productcategory');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('id',$id);
	  $this->db->where('companyid',$companyid);

      $this->db->update('productcategory',$data);
   } else {
      
      $this->db->insert('productcategory', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}


function uploadallstoreproduct($data)
  
{
   $code = $data['code'];
      $companyid = $data['companyid'];

  if($code!=null){
     $this->db->where('code',$code);
 $this->db->where('companyid',$companyid);
   $qq = $this->db->get('store');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('code',$code);
	  $this->db->where('companyid',$companyid);

      $this->db->update('store',$data);
   } else {
      
      $this->db->insert('store', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}








function uploadallimage($data)
  
{
   $img_id = $data['img_id'];
  if($img_id!=null){
     $this->db->where('img_id',$img_id);
 
   $qq = $this->db->get('alldocuments');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('img_id',$img_id);
      $this->db->update('alldocuments',$data);
   } else {
      
      $this->db->insert('alldocuments', $data);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}



public function searchallasalary($searchterm,$limit)
{
	//$this->db->select("  * ,salary.salary  AS mysalary, ");

    $sql = "SELECT *,salary.salary  AS mysalary,salary.id  AS id FROM salary INNER JOIN people on  salary.memberid= people.id  WHERE people.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or lastname LIKE '%" . $searchterm . "%'  or email LIKE '%" . $searchterm . "%'  or salary.bankaccount LIKE '%" . $searchterm . "%' or salary.number LIKE '%" . $searchterm . "%'  or  salary.slarymonth LIKE '%" . $searchterm . "%'  order by salary.id desc LIMIT " .$limit . ",20 ";
   		//$this->db->select("  * ,attendance.id  as myid ");
	//$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code  WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  ORDER BY products.category ASC ");


	//$this->db->join('people', 'attendance.idno = people.idno','INNER JOIN' );
		//$this->db->join('attendance', 'people.idno = attendance.idno','INNER JOIN');


   $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	
	
	   public function searchallsalary_record_count($searchterm)
	{

	    $sql = "SELECT COUNT(*) As cnt FROM salary INNER JOIN people on  salary.memberid= people.id  WHERE people.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or lastname LIKE '%" . $searchterm . "%' or email LIKE '%" . $searchterm . "%'  or salary.bankaccount LIKE '%" . $searchterm . "%' or salary.number LIKE '%" . $searchterm . "%' or  salary.slarymonth LIKE '%" . $searchterm . "%'";
		//$sql = "SELECT COUNT(*) As cnt  FROM attendance INNER JOIN people on  attendance.idno= people.idno  WHERE attendance.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or date LIKE '%" . $searchterm . "%'  or comment LIKE '%" . $searchterm . "%' or type LIKE '%" . $searchterm . "%'";

   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}


	
	

function populateviewcustomers(){
 

  $this -> db -> select('*');

   $this -> db -> from('people');

 //$this -> db -> where('category', 'customer');
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function add_studentdata($data,$id) 
{
   $this->db->where('id',$id);
   $q = $this->db->get('students');

   if ( $q->num_rows() > 0 ) 
   {
      $this->db->where('id',$id);
      $this->db->update('students',$data);
   } else {
     
      $this->db->insert('students',$data);
   }
   $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE)
	   {// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
}





function populateallstatus($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
  $this->db->where($where1,$value1);
  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function getstatementtenats($plotno,$fromdate,$todate,$display){


	  if($display=="payment"){

		$this->db->select(" *,paymentcode,coparent, SUM(coparent) AS coparent,paid, SUM(paid) AS paid ,amount, SUM(amount) AS amount,vat, SUM(vat) AS vat,commission, SUM(commission) AS commission from  invoice where type= 'RENT'   AND plotno= '$plotno'      and createdat BETWEEN '$fromdate'  and '$todate'  and  paid  >0 group by houseno  ORDER BY priority ");

	}else{
			$this->db->select(" * ,paymentcode,coparent, SUM(coparent) AS coparent,paid, SUM(paid) AS paid ,amount, SUM(amount) AS amount,vat, SUM(vat) AS vat,commission, SUM(commission) AS commission from  invoice where type= 'RENT'   AND  plotno= '$plotno'      and createdat BETWEEN '$fromdate'  and '$todate' group by houseno ORDER BY priority ASC ");
			
			
			
//$this->db->select(" * ,amount, SUM(amount) AS amount,vat, SUM(vat) AS vat,commission, SUM(commission) AS commission from  invoice where  type= 'RENT'   AND plotno= '$plotno'      and createdat BETWEEN '$fromdate'  and '$todate' group by houseno ORDER BY priority ASC ");

	}

//$this->db->select(" * from  invoice where plotno= '$plotno'      and createdat BETWEEN '$fromdate'  and '$todate'  group by houseno");

	 
	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}


function populaterentaldailypeyment($id){

if($id!=null){
$postedtime = date('Y-m-d');
$this->db->select(" * from receipt where companyid='$id' and  createdat='$postedtime'  ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}


function pendingbookings($id,$branchid){
 

  $this -> db -> select('*');

   $this -> db -> from('dailycharges');
     $this->db->where('companyid',$id);

$this->db->where('branch',$branchid);

$this->db->where('status',"reserved");
$this->db->where('type',"room");
//$this->db->where('bedid <>', "");
//$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatedailypeymentpendingbybeds($id,$branchid){
 

  $this -> db -> select('*');

   $this -> db -> from('dailycharges');
     $this->db->where('companyid',$id);

$this->db->where('branch',$branchid);

$this->db->where('status',"new");
$this->db->where('type',"room");
$this->db->where('bedid <>', "");
//$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatedailypeymentpending($id,$branchid){
 

  $this -> db -> select('*');

   $this -> db -> from('bookroom');
     $this->db->where('companyid',$id);

$this->db->where('branchid',$branchid);


$this->db->where('credit >', 0);
//$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function activerooms($id,$branchid){
 

  $this -> db -> select('*');

   $this -> db -> from('bookroom');
     $this->db->where('companyid',$id);

$this->db->where('status',"active");
$this->db->where('branchid',$branchid);


//$this->db->where('credit >', 0);
//$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatedailypeyment($id,$branchid,$startdate,$closedate){
 

  $this -> db -> select('*');

   $this -> db -> from('bookroom');
     $this->db->where('companyid',$id);

$this->db->where('branchid',$branchid);


$this->db->where('createdat >=', $startdate);
$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function populatedailypeymentOccupied($id,$branchid,$startdate,$closedate){
 

  $this -> db -> select('*');

   $this -> db -> from('bookroom');
     $this->db->where('companyid',$id);

$this->db->where('branchid',$branchid);
//$this->db->where('status',"vacant");
	$this->db->where("( status='vacant' OR  createdat BETWEEN  '$startdate'  AND  '$closedate'  )", NULL, FALSE);

//
//$this->db->where('createdat >=', $startdate);
//$this->db->where('createdat <=', $closedate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function populatedailypeyment22($id,$branchid,$startdate,$closedate){

if($id!=null){
$postedtime = date('Y-m-d');
$this->db->select(" * from bookroom where  branchid	 = '$branchid'  and createdat BETWEEN  '$startdate'  AND  '$closedate' and companyid='$id' ");

///$this->db->select(" * from bookroom where companyid='$id' and  createdat like '$postedtime%'  ");
 $this->db->order_by('id','desc');

$bioquery = $this -> db -> get();

if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}


function populateallstatusSSSSS($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
  $this->db->where($where1,$value1);
  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function populateallstatusSSSSSSS($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
  $this->db->where($where1,$value1);
  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}













	function getusers($email, $password)

{
   $this -> db -> select('*');

   $this -> db -> from('people');

  // $this -> db -> where('email', $email);
  $this -> db -> where('status', 1);
 	$this->db->where("(category='user' OR  category='reseller'   )", NULL, FALSE);

			$this->db->where("(BINARY  password ='$password'  and  BINARY email ='$email'  )", NULL, FALSE);


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result();

   }

   else

   {

     return false;

   }

}






function populatestudent($username, $password){

if($username!=null){
$this->db->select(" * from people where username='$username' or password='$password'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   //return $bioquery->result();
   return true;
}else{

  return false;
}

}


}




function updatemember($data,$id) 
{

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

   if ( $this->db->update('people',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}



function populatemember($id){

if($id!=null){
$this->db->select(" * from people where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}else{
 return false;	
}


}
function populatmyemember($id){

if($id!=null){
$this->db->select(" * from people where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result();
 
}else{

  return false;
}

}else{
 return false;	
}


}
function populategrouping($id){

if($id!=null){
$this->db->select(" * from grouping where 	memberid='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}else{
 return false;	
}


}




function populatesalescategoryraw($id){

if($id!=null){
$this->db->select(" * from sales_orderraw where 	invoice='$id' ");

 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}else{
 return false;	
}


}



public function allterms_handler($searchterm,$name)
	{
		
		
		if($searchterm)
		{
		
			$this->session->set_userdata($name, $searchterm);
			return $searchterm;
		}
		elseif($this->session->userdata($name))
		{
			$searchterm = $this->session->userdata($name);
			return $searchterm;
		}
		else
		{
			$searchterm ="";
			return $searchterm;
		}
	}
	





function populatesalescategory($id){

if($id!=null){
$this->db->select(" * from sales_order where 	invoice='$id' ");

 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}else{
 return false;	
}


}











function confirmgcustomedrouping($memberid, $categoryid,$companyid){

if($memberid!=null){
$this->db->select(" * from productsgrouping where 	memberid='$memberid' and companyid='$companyid' and categoryid='$categoryid' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return true;
 
}else{

  return false;
}

}else{
 return false;	
}


}



function confirmgrouping($memberid, $categoryid,$companyid){

if($memberid!=null){
$this->db->select(" * from grouping where 	memberid='$memberid' and companyid='$companyid' and categoryid='$categoryid' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return true;
 
}else{

  return false;
}

}else{
 return false;	
}


}

function confirmmodule($code, $url){
 

  $this -> db -> select('*');

   $this -> db -> from('modules');
$this -> db -> where('id', $code);
 $this -> db -> where('url', $url);


 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return true;

   }

   else

   {

     return false;

   }

}


function confirmactionmodule($actionid, $moduleid){
 

  $this -> db -> select('*');

   $this -> db -> from('modulesactions');
$this -> db -> where('actionid', $actionid);
 $this -> db -> where('moduleid', $moduleid);


 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return true;

   }

   else

   {

     return false;

   }

}


function confirmgpermissionaction($userid, $moduleid,$actionid){

if($userid!=null){
$this->db->select(" * from permissionsactions where 	userid='$userid' and 	actionid='$actionid'  ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return true;
 
}else{

  return false;
}

}else{
 return false;	
}


}








function populateproductsrouping($memberid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('productsgrouping');
$this -> db -> where('memberid', $memberid);
 $this -> db -> where('companyid', $userid);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}












function populatemembergrouping($memberid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('grouping');
$this -> db -> where('memberid', $memberid);
 $this -> db -> where('companyid', $userid);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatememberbygrouping($groupid,$companyid,$status){
 

  $this -> db -> select('*');

   $this -> db -> from('grouping');
$this -> db -> where('categoryid', $groupid);
$this -> db -> where('status', $status);
 //$this -> db -> where('userid', $userid);
 $this -> db -> where('companyid', $companyid);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}










	
	public function viewreportbycustomedcategory($limit, $start,$database,$companyid,$categoryid) {
	//
		$this->db->where("productsgrouping.companyid", $companyid);
		$this->db->where("productsgrouping.status", 1);
		$this->db->where("productsgrouping.categoryid", $categoryid);

$this->db->from('productsgrouping');


		$this->db->join('products', 'productsgrouping.memberid = products.code','INNER JOIN');


				$this->db->order_by('products.name','ASC');

			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	




	
	public function viewreportbycustomedcategoryuser($limit, $start,$database,$companyid,$categoryid) {
	//
		$this->db->where("productsgrouping.companyid", $companyid);
		$this->db->where("productsgrouping.status", 1);
		$this->db->where("productsgrouping.categoryid", $categoryid);

$this->db->from('productsgrouping');


		$this->db->join('products', 'productsgrouping.memberid = products.code','INNER JOIN');


				$this->db->order_by('products.name','ASC');

			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	









function updatecustomedcategorytatus($id,$status) 
{
 $postedtime = date('Y-m-d');
		$currenttime = date('h:i:sa');
 		$dateposted =  ($postedtime)." ".$currenttime ;
   $this->db->where('id',$id);
 $this->db->set('status',$status);
$this->db->set('updatedat',$dateposted);
   if ( $this->db->update('productsgrouping') ) 
   {
      return true;
   } else {
     
          return false;
   }
}

function updatefollowup($id,$status) 
{
 $postedtime = date('Y-m-d');
		$currenttime = date('h:i:sa');
 		$dateposted =  ($postedtime)." ".$currenttime ;
   $this->db->where('id',$id);
 $this->db->set('status',$status);
//$this->db->set('updatedat',$dateposted);
   if ( $this->db->update('followups') ) 
   {
      return true;
   } else {
     
          return false;
   }
}









function updatefollowuptransfer($dealowner,$newowner,$fromdate,$todate,$companyid) 
{
 
	
$this->db->where('userid',$dealowner);
$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
   
   
   
   
$this->db->set('userid',$newowner);
 $this->db->set('dealowner',$dealowner);

   if ( $this->db->update('followups ') ) 
   {
      return true;
   } else {
     
          return false;
   }
}







function updatedealtransfer($dealowner,$newowner,$fromdate,$todate,$companyid) 
{
 
	
$this->db->where('userid',$dealowner);
$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);
   
   
   
   
$this->db->set('userid',$newowner);
 $this->db->set('dealowner',$dealowner);

   if ( $this->db->update('deals') ) 
   {
      return true;
   } else {
     
          return false;
   }
}









function updatememberstatus($id,$status) 
{
 $postedtime = date('Y-m-d');
		$currenttime = date('h:i:sa');
 		$dateposted =  ($postedtime)." ".$currenttime ;
   $this->db->where('id',$id);
 $this->db->set('status',$status);
$this->db->set('updatedat',$dateposted);
   if ( $this->db->update('grouping') ) 
   {
      return true;
   } else {
     
          return false;
   }
}

function updateusersms($id,$totalsms) 
{

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

 $this->db->set('totalsms',$totalsms);
   if ( $this->db->update('people') ) 
   {
      return true;
   } else {
     
          return false;
   }
}









            function uploadmembers3($data)
{
// Query to insert data in database
  $userid = $data['userid'];
  $idno = $data['idno'];
   $phone = $data['phone'];
   $this -> db -> select("* from people where idno='$idno' and userid='$userid' and phone='$phone'");

   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {

    $this->db->insert('people', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
		     return false;
  
	  }

   }

  


}









function getnewstudent($username){

if($username!=null){
$this->db->select(" * from registration where username='$username'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   //return $bioquery->result();
   return true;
}else{

  return false;
}

}


}






function deletedata($id,$where,$tablename){

if($id!=null){

 $this->db->where($where,$id);
 
 //$this->db->truncate('table_name');
 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}


function deletemydata($id,$where,$tablename){

if($id!=null){

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

 $deleted=$this->db->truncate($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}






function truncatemydata($tablename){

if($tablename!=null){



 $deleted=$this->db->truncate($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}










function deletedatafour($tablename,$where,$id,$where1,$id1,$where2,$id2){

if($id!=null){

 $this->db->where($where,$id);
  $this->db->where($where1,$id1);
    $this->db->where($where2,$id2);

 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}





function deletedatatwo($tablename,$where,$id,$where1,$id1){

if($id!=null){

 $this->db->where($where,$id);
  $this->db->where($where1,$id1);
 $deleted=$this->db->delete($tablename);

if($deleted){

   return true;
   


}else{

  return false;
}

}


}







function getsales()

{

   $this -> db -> select('id');

   $this -> db -> from('sales');

 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() >0)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}


function getsetting()

{

   $this -> db -> select('*');

   $this -> db -> from('setting');

 $this->db->order_by('name','DESC');


 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}








function updatemyuser($data,$id) 
{

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

   if ( $this->db->update('people',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}



function updateproductlist($data,$code,$branch) 
{
   $this->db->where('branch',$branch);

   $this->db->where('code',$code);

   if ( $this->db->update('productlist',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}











function updateproduct($data,$code) 
{

   $this->db->where('code',$code);

   if ( $this->db->update('products',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}
function updateproductbyid($data,$id) 
{

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

   if ( $this->db->update('products',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}

function updatesalesreading($data,$id) 
{

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

   if ( $this->db->update('sales_reading',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}








function updatesalesorderraw($data,$id) 
{

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

   if ( $this->db->update('sales_orderraw',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}

function updateReceivings($data,$id) 
{

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

   if ( $this->db->update('newstock',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}

function geteproductlistbyid($code,$branchid){

if($code!=null){
$this->db->select(" * from productlist where code='$code' and branch='$branchid'  and status !='inactive'  or  shortname='$code' and branch='$branchid'  and status !='inactive'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}



function geteserials($serial,$branchid){

if($serial!=null){
	//$this->db->select(" * from serials where serial='$serial' or otherserial='$serial'   and branch='$branchid' or branch='Main'   and status='new'  ");

$this->db->select(" * from serials where serial='$serial' and branch='$branchid'   ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}


function geteproductlistbyserial($serial,$branchid){

if($serial!=null){
	
	$serial = htmlspecialchars($serial);
	//$this->db->select(" * from serials where serial='$serial' or otherserial='$serial'   and branch='$branchid' or branch='Main'   and status='new'  ");

$this->db->select(" * from serials where serial='$serial' and branch='$branchid'    and status='new'   or  otherserial='". $this->db->escape_like_str($serial)."' and branch='$branchid'    and status='new'  ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}




function getproductlistbyid($id){

if($id!=null){
$this->db->select(" * from productlist where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}



function geteproductbyid($id){

if($id!=null){
$this->db->select(" * from products where code='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}

function getestorebyid($id){

if($id!=null){
$this->db->select(" * from store where code='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}

function getallproductbyid($id){

if($id!=null){
$this->db->select(" * from products where id='$id' or shortname ='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}


function getesupplierbyid($id){

if($id!=null){
$this->db->select(" * from purchaseorder where invoice='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}





function getemployee($id){

if($id!=null){
$this->db->select(" * from people where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}







function getesales_orderraw($id,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_orderraw');
     $this->db->where('invoice',$id);
$this->db->where('status','new');

	  //$this->db->where('userid',$userid);

	
	 //$this->db->select('amount, SUM(amount) AS amount', FALSE);
	  //$this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

$this->db->order_by("id", "DESC");
   $this -> db -> limit(500);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function getesales_ordermain($id,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
$this->db->where('status','new');

	  //$this->db->where('userid',$userid);

	
	 //$this->db->select('amount, SUM(amount) AS amount', FALSE);
	  //$this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

$this->db->order_by("id", "DESC");
   $this -> db -> limit(500);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function countdata($table,$column,$value,$column2,$value2){

if($column!=null){
	

$this->db->select(" * from  ".$table." where   ".$column." ='$value' and  ".$column2." ='$value2' ");
	

	
	
	
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   
return $bioquery->num_rows();

}else{

  return 0;
}


}else{

  return false;
}


}



function updateproductbydeductcode($data,$code) 
{

   $this->db->where('orderamount',$code);

   if ( $this->db->update('products',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}









function getesales_reading($id,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_reading');
     $this->db->where('invoice',$id);
	  $this->db->where('userid',$userid);
	 // $this->db->where('salestype','wholesale');

	 //$this->db->select('discount, SUM(discount) AS discount', FALSE);
	// $this->db->select('amount, SUM(amount) AS amount', FALSE);
	 // $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function getesales_order($id,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('userid',$userid);
	 // $this->db->where('salestype','wholesale');

	 //$this->db->select('discount, SUM(discount) AS discount', FALSE);
	// $this->db->select('amount, SUM(amount) AS amount', FALSE);
	 // $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

$this->db->order_by("id", "DESC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



 function Projectlookup($keyword,$companyid){ 
        $this->db->select('*')->from('projects'); 
        $this->db->like('name',$keyword,'both'); 
		 $this->db->or_like('description',$keyword,'both');
				 $this->db->or_like('invoice',$keyword,'both');


		    $this->db->where('companyid',$companyid);
        //$this->db->where('companyid',$companyid); 
		 //$this -> db -> limit(10);
        $query = $this->db->get();     
        return $query->result(); 
    } 





 function allsupplierlookup22($keyword,$companyid){ 
        $this->db->select('*')->from('people'); 
        $this->db->like('firstname',$keyword,'both'); 
		 $this->db->or_like('lastname',$keyword,'both');
		 	 $this->db->or_like('phone',$keyword,'both');
			 	 $this->db->or_like('idno',$keyword,'both');
				  $this->db->or_like('company',$keyword,'both');
				 			 	// $this->db->or_like('company',$keyword,'both');

		    $this->db->where('companyid',$companyid);
        $this->db->where('category',"supplier"); 
		$this -> db -> limit(50);
        $query = $this->db->get();     
        return $query->result(); 
    } 

 
function allsupplierlookup($keyword,$companyid){ 


$this->db->select(" * from people where firstname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  and category ='supplier'  OR  company LIKE '%" . $this->db->escape_like_str($keyword) . "%'  and companyid='$companyid' and category ='supplier'    OR lastname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and  category ='supplier'   OR email LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category ='supplier'  OR idno LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category ='supplier'  OR phone LIKE '%" . $this->db->escape_like_str($keyword) . "%'and category ='supplier'  and companyid='$companyid' ORDER BY id ASC");
	
//$this->db->where("( and category ='supplier' )", NULL, FALSE);

// $this->db->where('category',"supplier"); 
$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}





function allcustomerlookupCustomeronly($keyword,$companyid){ 


$this->db->select(" * from people where firstname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid'  and category ='member'  OR  company LIKE '%" . $this->db->escape_like_str($keyword) . "%'  and companyid='$companyid' and category ='member'    OR lastname LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and  category ='member'   OR email LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category ='member'  OR idno LIKE '%" . $this->db->escape_like_str($keyword) . "%' and companyid='$companyid' and category ='member'  OR phone LIKE '%" . $this->db->escape_like_str($keyword) . "%'and category ='member'  and companyid='$companyid' ORDER BY id ASC");
	
//$this->db->where("( and category !='supplier' )", NULL, FALSE);

// $this->db->where('category',"supplier"); 
$this -> db -> limit(50);

$query = $this->db->get();   
		
return $query->result(); 

}











 function allcustomerlookup($keyword,$companyid){ 
        $this->db->select('*')->from('people'); 
        $this->db->like('firstname',$keyword,'both'); 
		 $this->db->or_like('lastname',$keyword,'both');
		 	 $this->db->or_like('phone',$keyword,'both');
			 	 $this->db->or_like('idno',$keyword,'both');
				 			 	 $this->db->or_like('company',$keyword,'both');

		    $this->db->where('companyid',$companyid);
        //$this->db->where('companyid',$companyid); 
		$this -> db -> limit(50);
        $query = $this->db->get();     
        return $query->result(); 
    } 





function getVat($id,$companyid){
 

  $this -> db -> select('tax,tax2');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status<>','returned');

	 $this->db->select('tax, SUM(tax) AS tax', FALSE);
	  $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

$this->db->group_by("invoice");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

function getProductDashboard($branchname,$companyid){
 

  $this -> db -> select('*');
if($branchname=="Main"){
	   $this -> db -> from('products');

}else{
	   $this -> db -> from('productlist');
		$this->db->where('branch',$branchname);				
	
}
    
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status',"active");
	//$this->db->where("( SUM(qty) AS qty )", NULL, FALSE);
		$this->db->select('SUM(buyingprice*qty) AS buyingprice', FALSE);

  $this->db->select('qty, SUM(qty) AS qty', FALSE);

$this->db->group_by('category');
//$this->db->group_by('companyid');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function getReturnesales_orderReceipt($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status<>','returned');

	 //$this->db->select('amount, SUM(amount) AS amount', FALSE);
	  //$this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function getReturnesales_orderReceiptHold($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_orderholding');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status<>','returned');

	 //$this->db->select('amount, SUM(amount) AS amount', FALSE);
	  //$this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function getesales_ordermergedbill($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('user',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status <>','returned');
	  	 // $this->db->where('ordersales ','new');

				//$this->db->where("(status !='returned'  OR ordersales !='printed'  )", NULL, FALSE);

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
	  	  $this->db->select('tax, SUM(tax) AS tax', FALSE);
	  	  $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);

//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function getesales_ordermergedbillold($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status <>','returned');
	  	 // $this->db->where('ordersales ','new');

				//$this->db->where("(status !='returned'  OR ordersales !='printed'  )", NULL, FALSE);

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
	  	  $this->db->select('tax, SUM(tax) AS tax', FALSE);
	  	  $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);

//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function getesales_Creditnote($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status','returned');
	  	 // $this->db->where('ordersales ','new');

				//$this->db->where("(status !='returned'  OR ordersales !='printed'  )", NULL, FALSE);

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
	  	  $this->db->select('tax, SUM(tax) AS tax', FALSE);

//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('sprice');
$this->db->group_by('description');

//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function getesales_orderReceipt($id,$companyid){
 

 // $this -> db -> select('*');
  $this -> db -> select('id,uom,vattype,code,taxtype,sprice,tax,address,type,discount,tax2,branch,sales_order.serial,description,taxable');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','ASC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('status <>','returned');
	  	 // $this->db->where('ordersales ','new');

				//$this->db->where("(status !='returned'  OR ordersales !='printed'  )", NULL, FALSE);

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
	  	  $this->db->select('tax, SUM(tax) AS tax', FALSE);
	  	  $this->db->select('taxable, SUM(taxable) AS taxable', FALSE);

//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('sprice');
$this->db->group_by('description');
$this->db->group_by('serial');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function getesalesbydepartment($fromdate,$todate,$companyid,$branch){



  $this -> db -> select('*');

   $this -> db -> from('sales');
  	$this->db->where('branch',$branch);

	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( dateposted BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	

	 $this->db->select('cash, SUM(cash) AS amount', FALSE);
	 	 $this->db->select('mpesa, SUM(mpesa) AS mpesa', FALSE);

		 	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

			 	 $this->db->select('voucher, SUM(voucher) AS voucher', FALSE);

				 	 $this->db->select('cheque, SUM(cheque) AS cheque', FALSE);

					 	 $this->db->select('card, SUM(card) AS card', FALSE);

						 	 $this->db->select('points, SUM(points) AS points', FALSE);

$this->db->group_by('maincategory');


//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function getesalesbyproduct($fromdate,$todate,$companyid,$branch){



  $this -> db -> select('*');

   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
	  $this->db->where('status<>','returned');

	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

		 $this->db->select('tax, SUM(tax) AS tax', FALSE);
		 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);

	 $this->db->select('amount, SUM(amount) AS myamount', FALSE);
$this->db->group_by('category');

 if($branch !="all"){
$this->db->group_by('branch');

   }else{
	 $this->db->group_by('companyid');
  
   }



//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function getvatsales($fromdate,$todate,$companyid,$branch,$type,$vatrate){

///
$vatrate=trim($vatrate);
  $this -> db -> select('*');
//$this->db->select("  * ,tax, SUM(tax) AS tax,tax2,SUM(tax2) AS tax2,amount,SUM(amount) AS amount ");
	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	 $this->db->select('tax, SUM(tax) AS tax', FALSE);
	 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);
	 $this->db->select('sprice, SUM(sprice) AS sprice', FALSE);
$this->db->where('status <>',"returned");

   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
      if($vatrate != "all"){
	 $this->db->where('mode',$vatrate);

   }
   
   
	 $this->db->where('vattype',$type);

	 $this->db->where('companyid',$companyid);
	 
	 	
	$this->db->where('dateposted >=', $fromdate);
$this->db->where('dateposted <=', $todate);
	  
	  //	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	


 if($branch !="all"){
$this->db->group_by('branch');

   }else{
	 $this->db->group_by('companyid');
  
   }
 
      if($vatrate != "all"){
	$this->db->group_by('mode');

   }
//$this->db->where('vatrate',"16");
	//$this->db->group_by('vatrate');
	// $this->db->group_by('vatrate');

	 $this->db->group_by('vattype');

	 $this->db->group_by('companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}




function getvatpurchase($fromdate,$todate,$companyid,$branch,$type){



  $this -> db -> select('*');
	 //$this->db->select('newstock.amount, SUM(newstock.amount) AS amount', FALSE);
	// $this->db->select('newstock.vat, SUM(newstock.vat) AS vat', FALSE);

	 //$this->db->select('buyingprice, SUM(buyingprice*total) AS buyingprice', FALSE);
	 
 			//$this->db->select("  * ,total,SUM(total) AS mytotal ,buyingprice,SUM(buyingprice*total) AS buyingprice  ");
			
			
		//$this->db->join('purchaseorder', 'newstock.invoice_number = purchaseorder.invoice','INNER JOIN');


   $this -> db -> from('newstock');
   if($branch !="all"){
	     	$this->db->where('type',$branch);

   }
	 $this->db->where('newstock.tax',$type);

	 $this->db->where('newstock.companyid',$companyid);
	 	 $this->db->where("invoice_number<>","");
	 	 $this->db->where("newstock.supplier<>","");

	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'  and newstock.vat!='' )", NULL, FALSE);

	


	
	 
//$this->db->group_by('newstock.tax');

   
 if($branch !="all"){
$this->db->group_by('type');

   }

  
   



	 $this->db->group_by('newstock.companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}






function getvatsalesBytype($companyid,$invoice,$branchid){

	 $this->db->select('tax, taxable,invoice, SUM(tax) AS mytax,SUM(taxable) AS mytaxable');


   $this -> db -> from('sales_order');

	     	$this->db->where('invoice',$invoice);
    	     //	$this->db->where('branchid',$branchid);


	 $this->db->where('sales_order.companyid',$companyid);
	 	 $this->db->where("invoice <>","");
	  $this->db->where('status <>','returned');

	  
	  

	


	
	 
$this->db->group_by('invoice');

$this->db->order_by('id');

   



	// $this->db->group_by('newstock.companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}









function getbuyingpricebyinvoice($companyid,$invoice){

	 $this->db->select('bprice, SUM(bprice*qty) AS ttbprice');




   $this -> db -> from('sales_order');

	     	$this->db->where('invoice',$invoice);

	 $this->db->where('sales_order.companyid',$companyid);
	 
 	  $this->db->where('status <>','returned');
	  
	  

	


	
	 
$this->db->group_by('invoice');


   



 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

function getvatpurchaseBytype($companyid,$invoice,$type){

	 $this->db->select('buyingprice, total,invoice_number, SUM(buyingprice*total) AS totalvat');


  //$this -> db -> select('*');

   $this -> db -> from('newstock');

	     	$this->db->where('invoice_number',$invoice);
    	$this->db->where('tax',$type);

	 $this->db->where('newstock.companyid',$companyid);
	 	 $this->db->where("invoice_number<>","");
	 	 $this->db->where("newstock.supplier<>","");
$this->db->where('itemstatus <>',"returned");

	  
	  

	


	
	 
$this->db->group_by('invoice_number');
$this->db->group_by('tax');
$this->db->order_by('id');

   



	// $this->db->group_by('newstock.companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}


function getallvatpurchases($fromdate,$todate,$companyid,$branch,$vatrate){

	
$this->db->select("  * ,purchaseorder.id  AS id, ");
	//$this->db->where('purchaseorder.status', 'purchases');
$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   )", NULL, FALSE);

	
	
	$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);

		
	
			if($branch !="all"){
	
	$this->db->where('branchid', $branch);
		}
		






$this->db->from('purchaseorder');

		






	
	 $this->db->order_by('purchaseorder.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}











function getallvatexpenses($fromdate,$todate,$companyid,$branch,$vatrate){

	
$this->db->select("  * ,expenses.id  AS id, ");
	//$this->db->where('expenses.type', 'Expense');

	
	
	$this->db->where('datespent >=', $fromdate);
$this->db->where('datespent <=', $todate);

		
	
			if($branch !="all"){
	
	$this->db->where('branchid', $branch);
		}
		






$this->db->from('expenses');

		






	
	 $this->db->order_by('expenses.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}



function getallAdvances($fromdate,$todate,$companyid,$branch,$vatrate){

	
$this->db->select("  *  ");
	//$this->db->where('description', "payment received");

	
	
	$this->db->where('createdat >=', $fromdate);
$this->db->where('createdat <=', $todate);

		
	
			if($branch !="all"){
	
	$this->db->where('branchid', $branch);
		}
		






$this->db->from('advance');

		






	
	 $this->db->order_by('advance.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}

function getallAdvancesgroup($fromdate,$todate,$companyid,$branch,$vatrate){

	
$this->db->select("  *  ");
	//$this->db->where('description', "payment received");

	
	
	$this->db->where('createdat >=', $fromdate);
$this->db->where('createdat <=', $todate);

		
	
			if($branch !="all"){
	
	$this->db->where('branchid', $branch);
		}
		






$this->db->from('advancepayment');

		
$this->db->select('amount, SUM(amount) AS amount', FALSE);


$this->db->group_by('branchid');




	
	 $this->db->order_by('advancepayment.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}

function getallAdvancesgroup_2($fromdate,$todate,$companyid,$branch,$vatrate){

	
$this->db->select("  *  ");
	//$this->db->where('description', "payment received");

	
	
	$this->db->where('createdat >=', $fromdate);
$this->db->where('createdat <=', $todate);

		
	
			if($branch !="all"){
	
	$this->db->where('branchid', $branch);
		}
		






$this->db->from('advance');

		
$this->db->select('amount, SUM(amount) AS amount', FALSE);


$this->db->group_by('branchid');




	
	 $this->db->order_by('advance.id','desc');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}




function getallvatpurchases2($fromdate,$todate,$companyid,$branch,$type){



  $this -> db -> select('*');
	 $this->db->select('newstock.amount, SUM(newstock.amount) AS amount', FALSE);
	 $this->db->select('newstock.vat, SUM(newstock.vat) AS vat', FALSE);

	 $this->db->select('buyingprice, SUM(buyingprice*total) AS buyingprice', FALSE);
	 
 			//$this->db->select("  * ,total,SUM(total) AS mytotal ,buyingprice,SUM(buyingprice*total) AS buyingprice  ");
			
			
		$this->db->join('purchaseorder', 'newstock.invoice_number = purchaseorder.invoice','INNER JOIN');


   $this -> db -> from('newstock');
   if($branch !="all"){
	     	$this->db->where('type',$branch);

   }
	 $this->db->where('newstock.tax',$type);

	 $this->db->where('newstock.companyid',$companyid);
	 	 $this->db->where("invoice_number<>","");
	 	 $this->db->where("supplier<>","");

	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'  and newstock.vat!='' )", NULL, FALSE);

	


	
	 
//$this->db->group_by('newstock.tax');

   
 if($branch !="all"){
$this->db->group_by('type');

   }

  
   



	 $this->db->group_by('newstock.companyid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}



function getesalesbyproductcategory($fromdate,$todate,$companyid,$branch,$category){



  $this -> db -> select('*');

   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
 if($category !="all"){
	     	$this->db->where('category',$category);

   }
	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	

	$this->db->select('amount, SUM(amount) AS myamount', FALSE);
	 	$this->db->select('sprice, SUM(sprice * qty) AS myamount', FALSE);
	 	$this->db->select('bprice, SUM(bprice * qty) AS mybprice', FALSE);

	 
$this->db->group_by('category');

 if($branch !="all"){
$this->db->group_by('branch');

   }
	 $this->db->group_by('companyid');
	  $this->db->where('status <>','returned');



//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function getesalesbyproductbydepartment($fromdate,$todate,$companyid,$branch){



  $this -> db -> select('*');

   $this -> db -> from('sales_order');
   if($branch !="all"){
	     	$this->db->where('branch',$branch);

   }
	  $this->db->where('status<>','returned');

	 $this->db->where('companyid',$companyid);
	  
	  	$this->db->where("( date BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);

	//
	
	 $this->db->select('tax, SUM(tax) AS tax', FALSE);
		 $this->db->select('tax2, SUM(tax2) AS tax2', FALSE);
	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
$this->db->group_by('companyid');
$this->db->group_by('maincategory');
   if($branch !="all"){
	   $this->db->group_by('branch');


   }


//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}
function getmpesalesbycode($code){
 

 $this -> db -> select('*');

   $this -> db -> from('sms_in');
    
	  $this->db->where('code',$code);
	  
	  $this -> db -> limit(1);



$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}




function getmpesales($companyid){
 

 $this -> db -> select('*');

   $this -> db -> from('sms_in');
     $this->db->where('printed',0);
	  $this->db->where('used',2);
	  	  $this->db->where('amount <>',0);
	  $this -> db -> limit(1);



$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}



function verifyattendance($fromdate,$todate,$idno,$companyid,$branchid)
	{
		
	$sql = "SELECT COUNT(*) As cnt FROM checkin WHERE   checkindate BETWEEN '$fromdate'  and '$todate'   and idno= '$idno' and branchid= '$branchid'  and companyid= '$companyid' ";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}







function getereservations($fromdate,$todate,$status,$plotno,$houseno){



  $this -> db -> select('*');

   $this -> db -> from('advance');

$this->db->where('startdate >=', $fromdate);
$this->db->where('startdate <=', $todate);


if($status !="all"){
$this->db->where('status', $status);
}

	 
	  if($plotno !="all"){
$this->db->where('plotno', $plotno);
}

if($houseno !="all"){
$this->db->where('houseno', $houseno);
}






$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}







function getbulkreservations($fromdate,$todate,$status,$plotno,$houseno){



  $this -> db -> select('*');

   $this -> db -> from('bulkreservation');

//$this->db->where('checkin >=', $fromdate);
//$this->db->where('checkin <=', $todate);
$this->db->where("( createdat BETWEEN '$fromdate'  and '$todate'    or checkoutdate BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);


if($status !="all"){
$this->db->where('paymentstatus', $status);
}

	 
	  if($plotno !="all"){
$this->db->where('plotno', $plotno);
}

if($houseno !="all"){
$this->db->where('houseno', $houseno);
}






$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function geteroomsreservations($fromdate,$todate,$status,$plotno,$houseno){



  $this -> db -> select('*');

   $this -> db -> from('dailycharges');

//$this->db->where('checkin >=', $fromdate);
//$this->db->where('checkin <=', $todate);
$this->db->where("( checkin BETWEEN '$fromdate'  and '$todate'    or checkout BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);


if($status !="all"){
$this->db->where('status', $status);
}

	 
	  if($plotno !="all"){
$this->db->where('plotno', $plotno);
}

if($houseno !="all"){
$this->db->where('houseno', $houseno);
}






$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function searchmeterreadings($fromdate,$todate,$status,$plotno,$houseno){



  $this -> db -> select('*');

   $this -> db -> from('meterreading');

//$this->db->where('checkin >=', $fromdate);
//$this->db->where('checkin <=', $todate);
$this->db->where("( createdat BETWEEN '$fromdate'  and '$todate'     )", NULL, FALSE);


if($status !="all"){
$this->db->where('status', $status);
}

	 
	  if($plotno !="all"){
$this->db->where('plotno', $plotno);
}

if($houseno !="all"){
$this->db->where('houseno', $houseno);
}






$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}











function getesalesbympesa($fromdate,$todate,$status,$branchname,$download){

if($download=="yes"){
	  		  	$this->db->select('sender_number,code,amount,firstname,exactime,account_number,used', FALSE);
		
	}else{
		
	
  $this -> db -> select('*');	
	}
	 
   $this -> db -> from('sms_in');
	if($branchname !="all"){
$this->db->where('branchid', $branchname);
	}


	if($status=="All"){
	  	$this->db->where("( exactime BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
				//$this->db->where("(sender_number !='to' or sender_number !='received'  )", NULL, FALSE);
	  $this->db->where('sender_number <>','to');
	  	  $this->db->where('sender_number <>','your');
	  	  $this->db->where('code <>','*missing');
	  	  $this->db->where('firstname <>','reversed');

	  	  //$this->db->where('sender_number <>','received');
	  	  $this->db->where('amount >',0);


		}else{
				$this->db->where("( used='$status'  and exactime BETWEEN '$fromdate'  and '$todate'   )", NULL, FALSE);
				$this->db->where("(sender_number !='to' or sender_number !='received'  )", NULL, FALSE);

		}




$this->db->order_by("used", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	   
	   
	   			
	if($download=="yes"){

		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Raw Mpesa Data.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($query1, $delimiter, $newline);

        force_download($filename, $data);
		
		
	}else{
		  return $query1->result();
		
	}
	
	   


   }

   else

   {

     return false;

   }

}






function getesalesmy_orderNoKitchen($id,$companyid,$branchid){
 

 //$this -> db -> select('*');
  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');
  $this->db->where('start <>',"yes");
	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function getesalesmy_orderkitchen($id,$companyid,$mybranch,$maincategory){
 

 //$this -> db -> select('*');
  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_orderkitchen');
     $this->db->where('invoice',$id);
	   if($mybranch !="all"){
	   $this->db->where('branch',$mybranch);
}

	   $this->db->where('maincategory',$maincategory);

	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function getesalesmy_orderEnabled($id,$companyid){
 

 //$this -> db -> select('*');
  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');
 $this->db->where('start','no');
	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function getesalesmy_order($id,$companyid){
 

 //$this -> db -> select('*');
  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function getesalesmy_orderonlyNoKitchen($id,$companyid,$maincategory){
 

  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');
	  $this->db->where('maincategory <>',$maincategory);

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
//$this->db->group_by('code');

$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function getesalesmy_orderonly($id,$companyid){
 

  $this -> db -> select('code,sprice,tax,address,discount,type');

   $this -> db -> from('sales_order');
    $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  $this->db->where('ordersales','new');
	  $this->db->where('status <>','returned');

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
$this->db->group_by('address');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}








function getesales_orderReceiptWholesale($id,$companyid){
 

 /// $this -> db -> select('*');
  $this -> db -> select('id,uom,code,sprice,tax,address,type,discount,tax2,branch,sales_order.serial,description,taxable');

   $this -> db -> from('sales_order');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	 // $this->db->where('salestype','wholesale');

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




















function getreceivings($invoice_number){
 

  $this -> db -> select('*');

   $this -> db -> from('newstock');
     $this->db->where('invoice_number',$invoice_number);
	      $this->db->where('itemstatus <>',"returned");
		 $this->db->order_by('id','desc');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}
function geteallsales_orderoriginalReturned($finalsalesid){

if($finalsalesid!=null){
$this->db->select(" * from sales_order where invoice='$finalsalesid' and status='returned' and ordersales != 'returned'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}

function geteallsales_orderoriginal($finalsalesid){

if($finalsalesid!=null){
$this->db->select(" * from sales_order where invoice='$finalsalesid' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}





function geteallsales_orderoriginalNoreturn($finalsalesid){

if($finalsalesid!=null){
$this->db->select(" * from sales_order where invoice='$finalsalesid' and status !='returned' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}








function geteallsales_orderoriginalHold($finalsalesid){

if($finalsalesid!=null){
$this->db->select(" * from sales_orderholding where invoice='$finalsalesid' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}


function geteallQUOTES_orderCounter($finalsalesid,$deliverymethod){
 

  $this -> db -> select('amount');

   $this -> db -> from('purchaseorderlist');
     $this->db->where('invoice',$finalsalesid);
		 
  $this->db->where('status <>',"returned");
	if($deliverymethod=="requisition"){
		$this->db->select('SUM(bprice*qty*unit) AS amount', FALSE);
	
	}else{
	$this->db->select('SUM(	sprice*qty*unit) AS amount', FALSE);
	
		
	}
	// $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  //

$this->db->group_by('invoice');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}


function geteallsales_orderCounter($finalsalesid){
 

  $this -> db -> select('amount');

   $this -> db -> from('sales_orderraw');
     $this->db->where('invoice',$finalsalesid);
		 
  $this->db->where('status <>',"returned");

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  

$this->db->group_by('invoice');
  $this -> db -> limit(1);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}




function geteallsales_Bookings($finalsalesid)
	{
		 
		$sql = "SELECT COUNT(transactioncode) As cnt FROM dailycharges WHERE transactioncode='$finalsalesid' and ordersales='active' ";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}



function geteallsales_order($finalsalesid){

if($finalsalesid!=null){
$this->db->select(" * from sales_orderraw where invoice='$finalsalesid' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}

function getesales_order1($finalsalesid,$userid){

if($id!=null){
$this->db->select(" * from sales_order where invoice='$id' and userid='$userid'  ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}



function getesoldreadingproduct($id){

if($id!=null){
$this->db->select(" * from sales_reading where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}









function getesmade($id){

if($id!=null){
$this->db->select(" * from sales where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}



function getesoldproduct($id){

if($id!=null){
$this->db->select(" * from sales_order where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}

function getesoldproductrawNoarray($id){

if($id!=null){
$this->db->select(" * from sales_orderraw where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
 
}else{

  return false;
}

}


}


function getesoldproductraw($id){

if($id!=null){
$this->db->select(" * from sales_orderraw where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}




function geteeceivedproduct($id){

if($id!=null){
$this->db->select(" * from newstock where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}










function getpeople($id){

if($id!=null){
$this->db->select(" * from people where id= '".($id)."' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}

}
function geteuser($id){

if($id!=null){
$this->db->select(" * from people where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}
function getsalesbyinvoice($invoice){

if($invoice!=null){
$this->db->select(" invoice, customer,name,salesperson,description  from sales where invoice='$invoice' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}










function getstockbyinvoice($invoice){

if($invoice!=null){
$this->db->select(" invoice,ordernumber, supplier  from purchaseorder where invoice='$invoice' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
 
}else{

  return false;
}

}


}


function populatedocumets($id){

if($id!=null){
$this->db->select(" * from documents where studentid= '$id'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();

}else{

  return false;
}

}}

function populatemessage($id){

if($id!=null){
$this->db->select(" * from coursemessage where courseid= '$id'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();

}else{

  return false;
}

}

}

function confirmaccount($id)
{// Query to insert data in database
  //$paymentno = $data['paymentno'];
   $this -> db -> select("* from people where id='$id'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() > 0)

   {
    return $query->result_array();

   
  //return true;


   }

   else

   {

     return false;

   }
}




function confirmasmsccount($id)
{// Query to insert data in database
  //$paymentno = $data['paymentno'];
   $this -> db -> select("* from smsconfig where id='$id'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() > 0)

   {
    return $query->result_array();

   
  //return true;


   }

   else

   {

     return false;

   }
}




function confirmgroupsaccount($id)
{// Query to insert data in database
  //$paymentno = $data['paymentno'];
   $this -> db -> select("* from grouping where categoryid='$id' AND status='1'");

   $query = $this -> db -> get();
  if($query -> num_rows() > 0)

   {
    return $query->result();

   
  //return true;


   }

   else

   {

     return false;

   }
}




function confirmstep($paymentno)
{// Query to insert data in database
  //$paymentno = $data['paymentno'];
   $this -> db -> select("* from studentapplication where paymentno='$paymentno'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() > 0)

   {
    return $query->result();

   
  //return true;


   }

   else

   {

     return false;

   }
}


function confirmstep1($paymentno)
  
{
 
  if($paymentno!=null){
     $this->db->where('paymentno',$paymentno);
 
   $confirmquey = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $confirmquey->num_rows() > 0 ) 
   {
        return $confirmquey->result();

          //return true;

   } else {
      
      return false;
      
   }


  
     
    
} else {

     return false;

        }

}



public function countSales1($branch, $userid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM sales_orderraw WHERE userid='$userid'  and branch='$branch'  group by invoice  ORDER BY id  asc  ";
   	$this->db->group_by('dob');

		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}




public function countSales($branch, $userid){
 

  $this -> db -> select('invoice');

   $this -> db -> from('sales_orderraw');
     $this->db->where('userid',$userid);
	
	  $this->db->where('branch',$branch);


$this->db->group_by('invoice');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}




public function countMembers($userid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM people WHERE userid='$userid'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	public function countamount($userid)
	{
		 $sql = "SELECT totalsms FROM people WHERE id='$userid'  ";
   
		$q = $this->db->query($sql);
		
		 return $q->result_array();
	}
	
	
	
	
	
		public function countserial($itemid)
	{
		
			$this->db->select("serial");
   $this -> db -> where("itemid", $itemid);
		
		$this->db->from("serials");
		
	return $this->db->count_all_results();
	}
	
	
	
	
	
	
	
	
	
	
	
		public function countBirthdays($companyid,$mydob)
	{
			 //$sql = "SELECT *,dob, MONTH(dob) AS mydob, COUNT(*) As cnt  FROM  people WHERE companyid = '$companyid'  HAVING  mydob = '$mydob' ";
			 $sql = "SELECT COUNT(*) As cnt,dob, MONTH(dob) AS mydob  FROM  people WHERE companyid = '$companyid'   and  dob LIKE '%" . $this->db->escape_like_str($mydob) . "%'   ";

   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
	
	
	
	
	
	
	
	
	
	public function countSMS($userid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM sms WHERE userid='$userid'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	function count_where($tablename,$where,$value,$where1,$value1)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
function count_wherepurchases($tablename,$where,$value,$where1,$value1)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
					//	$this->db->where("(status='purchases' OR 	status='p.o'  OR 	status='Opening Balance' and transactionstatus='closed')", NULL, FALSE);
				$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   OR 	status='Opening Balance' OR 	status='creditnote'  )", NULL, FALSE);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	public function displayalldatacustomedorder($limit, $start,$database,$where,$value,$where1,$value1,$order_by,$ORDER) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
	 $this->db->order_by($order_by,$ORDER);

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	

	
	
	
	
	public function displayduebirthdates($limit, $start,$database,$where,$value,$where1,$value1) {

	
	 $sql = "SELECT * ,dob, MONTH(dob) AS mydob  FROM  people WHERE companyid = '$value'  HAVING  mydob = '$value1' order by  dob asc ";
    	
	 
   
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

	
	
	
		
		
	public function displayduebirthdates_($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);
						$this->db->select("  * ,dob, MONTH(dob) AS mydob  HAVING  mydob = 11 ");
							 	//$this->db->select("  * ,dob, MONTH(dob) AS mydob HAVING  mydob = 11   ");
				//$this->db->where("(HAVING  mydob = 11  )", NULL, FALSE);

//$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit, ");
$this->db->where($where, $value);
		//$this->db->where($where1, $value1);
$this->db->from('people');



	//$this->db->group_by('dob');
	//$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	
	
	
	public function displayalldata($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);
								 $this->db->order_by('id','DESC');

		$this->db->where($where, $value);
		$this->db->where($where1, $value1);

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
		public function displayalldataWITHORDER($limit, $start,$database,$where,$value,$where1,$value1,$order_by,$ORDER) {
        $this->db->limit($limit, $start);
 $this->db->order_by($order_by,$ORDER);

		$this->db->where($where, $value);
		$this->db->where($where1, $value1);

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	public function Confirmdetails($idno,$phone,$email) {
       
		 $this->db->order_by('id','DESC');
		$this->db->where("companyid", 1);
      
		 $this->db->limit(1);
		
				$this->db->where("(idno='$idno' OR 	phone='$phone'    OR 	email='$email'  )", NULL, FALSE);

        $query = $this->db->get("people");


       if($query -> num_rows() >0)

   {
  return $query->result_array();

   }

   else

   {

     return false;

   }
   }
	
	
	
	
	public function displayallpurchases($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);

		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
				$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   OR 	status='Opening Balance' OR 	status='creditnote' )", NULL, FALSE);
			$this->db->order_by('id','DESC');

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
			$records = $query->num_rows();
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
           		
		            return $data;

        }
        return false;
   }
	
	
		public function displayalldatadelivery($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);

		$this->db->where($where, $value);
		//$this->db->where($where1, $value1);
				$this->db->where("(branch='$value1' OR 	branch='Main')", NULL, FALSE);

			 $this->db->order_by('delivery.ordereddate','DESC');

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   		public function lowstock($limit, $start,$database,$where,$value,$where1,$value1) {

        $this->db->limit($limit, $start);

		$this->db->where($where, $value);
		//$this->db->where($where1, $value1);
				$this->db->where("(qty<reorderevel  )", NULL, FALSE);

			 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
   public function displayalldataacountingVariance($limit, $start,$database,$where,$value,$where1,$value1) {

	
	 $sql = "SELECT * ,debit, SUM(debit) AS mydebit,credit, SUM(credit) AS mycredit  FROM  debitcredit WHERE companyid = '$value' group by code   HAVING  mydebit <> mycredit  order by id desc ";
    	
	 
   
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

	
   	public function CountdisplayalldataacountingVariance($database,$where,$value,$where1,$value1) {

	
			 //$sql = "SELECT *,dob, MONTH(dob) AS mydob, COUNT(*) As cnt  FROM  people WHERE companyid = '$companyid'  HAVING  mydob = '$mydob' ";
			// $sql = "SELECT COUNT(*) As cnt,dob, MONTH(dob) AS mydob  FROM  people WHERE companyid = '$companyid'   and  dob LIKE '%" . $this->db->escape_like_str($mydob) . "%'   ";

   	// $sql = "SELECT COUNT(*) As cnt ,debit, SUM(debit) AS mydebit,credit, SUM(credit) AS mycredit  FROM  debitcredit WHERE companyid = '$value' group by code   HAVING  mydebit <> mycredit  ";
	 $sql = "SELECT * ,debit, SUM(debit) AS mydebit,credit, SUM(credit) AS mycredit  FROM  debitcredit WHERE companyid = '$value' group by code   HAVING  mydebit <> mycredit   ";

		$q = $this->db->query($sql);
		return$row = $q->row(); 
		//return $row->cnt;
	}
	

   
	
	public function displayalldataacounting($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		 $this->db->order_by('crateddate','DESC');
		 $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	public function last_recordproject($companyid){
		
		 	 $this->db->select('id');


        $this->db->limit(1);
		$this->db->where("companyid", $companyid);
		
		
 $this->db->order_by('id','DESC');
				
				 

        $query = $this->db->get("projects");

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	
	
	public function last_recordsalesreceipt($companyid){
		
		 	 $this->db->select('receiptno');


        $this->db->limit(1);
		$this->db->where("companyid", $companyid);
			$this->db->where("receiptno <>", "");
		
 $this->db->order_by('id','DESC');
				
				 

        $query = $this->db->get("sales");

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	
	
	
		public function last_record($table,$companyid,$userid){
		
		 


        $this->db->limit(1);
		$this->db->where("companyid", $companyid);
		$this->db->where("userid", $userid);
		
		
		$this->db->order_by('id','DESC');
				
	 $this->db->select('id');
        $query = $this->db->get($table);

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public function last_recordsalesraw($branch,$userid){
		
		 


        $this->db->limit(1);
		$this->db->where("branch", $branch);
		$this->db->where("userid", $userid);
			//$this->db->where("status", "new");
$this->db->where("(status='new'   OR status='returned'  )", NULL, FALSE);


		

				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
				 
	 $this->db->select('invoice');
	  $this->db->order_by('id','DESC');
        $query = $this->db->get("sales_orderraw");

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	
	
	
	
	
	
	
	
		public function last_recordsalesraw2($companyid,$userid){
		
$query = $this->db->query("SELECT invoice FROM sales_orderraw where userid='$userid'  and status='new' and companyid='$companyid'   ORDER BY id DESC LIMIT 1");
$result = $query->result_array();
return $result;
//
   
   }
	
	
	
			public function last_purchasecode($companyid){
		
$query = $this->db->query("SELECT id FROM 	purchaseorder where companyid='$companyid'    ORDER BY id DESC LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
		public function last_salesbycustomer($customer,$companyid){
		
$query = $this->db->query("SELECT * FROM 	sales where companyid='$companyid' and customer='$customer'    ORDER BY id DESC LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	
		
	
		public function last_payrolltcode($companyid){
		
$query = $this->db->query("SELECT code FROM salarycharges where companyid='$companyid'    ORDER BY id DESC LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	public function last_productbyid($companyid){
		
$query = $this->db->query("SELECT id FROM products where companyid='$companyid'    ORDER BY id DESC LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	
		public function last_productcode($companyid){
		
$query = $this->db->query("SELECT code FROM products where companyid='$companyid'    ORDER BY id DESC LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	
	
		public function last_membernumber1($companyid){
		
		 


        $this->db->limit(1);
		$this->db->where("companyid", $companyid);
		$this->db->where("address_1<>", "");
		$this->db->where("category", "member");
		
	
				
				 	$this->db->order_by('id','DESC');
					 $this->db->order_by('address_1','DESC');
	 $this->db->select('address_1');
        $query = $this->db->get("people");

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	
	
		public function last_membernumber($companyid){
		
$query = $this->db->query("SELECT address_1 FROM people where companyid='$companyid'  and  address_1 !='' and category='member'    ORDER BY CAST(address_1 AS DECIMAL(10,0)) DESC  LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	
	
		
	
		public function last_glaccount($companyid){
		
$query = $this->db->query("SELECT number FROM glaccounts where companyid='$companyid'  and  number !=''     ORDER BY CAST(number AS DECIMAL(10,0)) DESC  LIMIT 1");


    if($query->num_rows() > 0)
    {
		return $query->result_array();
     
    }
    else
    {
        return false;
    }
   
   }
	
	
	
	
	
	public function last_recordsalee($companyid,$userid){
		
$query = $this->db->query("SELECT invoice FROM sales_order where userid='$userid'  and status='new' and companyid='$companyid'   ORDER BY id DESC LIMIT 1");
$result = $query->result_array();
return $result;

   
   }
	
	
	
	public function last_recordsale($companyid,$userid){
		
		 


        $this->db->limit(1);
		$this->db->where("companyid", $companyid);
		$this->db->where("userid", $userid);
			$this->db->where("status", "new");
		
 $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');
				 
	 $this->db->select('invoice');
        $query = $this->db->get("sales_order");

        if($query->num_rows() > 0)
    {
        
		return $query->result_array();
    }
    else
    {
        return false;
    }
   }
	
	
	

	
	public function displayallsalesdata($limit, $start,$database,$where,$value,$where1,$value1) {
		$this->db->cache_on(); 
	 $this->db->select('serial,code,branch,address,uom,taxindicator,amount,tax,taxable,tax2,ordersales,bprice, sprice,id,qty,discount,uomqty,taxtype');

			$this->db->limit($limit, $start);
				 $this->db->order_by('id','DESC');
				 $this->db->order_by('date','DESC');
				 $this->db->order_by('status','ASC');

$this->db->where($where, $value);
		$this->db->where($where1, $value1);


        $query = $this->db->get($database);
//$this->db->cache_off(); 
        if ($query->num_rows() > 0) {
			$records = $query->num_rows();

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            //return $data;
			
			
			   return array(
    'data' => $data,
    'records' => $records,
);
        }else{
			  return false;
		}
      
   }
	

function Getuseddays($id,$type){
 

  $this -> db -> select('*');

   $this -> db -> from('leaves');
     $this->db->where('userid',$id);
		  $this->db->where('status',"3");
	  $this->db->where('type',$type);

	 $this->db->select('duration, SUM(duration) AS duration', FALSE);
	  

$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}





function Gettatalbeds($roomid)
	{
		
	$sql = "SELECT COUNT(*) As cnt FROM beds WHERE    	roomid	 = '$roomid'";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

function Gettatalroomsandbeds($transactioncode)
	{
		
	$sql = "SELECT COUNT(*) As cnt FROM dailycharges WHERE    	transactioncode	 = '$transactioncode'";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

function GettatalbedsOccupied($roomid)
	{
		
	$sql = "SELECT COUNT(*) As cnt FROM beds WHERE    	roomid	 = '$roomid' and status='occupied'";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}





function GettatalbedsVacant($roomid)
	{
		
	$sql = "SELECT COUNT(*) As cnt FROM beds WHERE    	roomid	 = '$roomid' and status='active'";


		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}




function Getalluseddaysbytype($id ,$type){
 

  $this -> db -> select('*');

   $this -> db -> from('leaves');
     $this->db->where('userid',$id);
	     $this->db->where('type',$type);
		  $this->db->where('status',"3");


	 $this->db->select('duration, SUM(duration) AS duration', FALSE);
	  

$this->db->group_by('userid');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}


function Getalluseddays($id){
 

  $this -> db -> select('*');

   $this -> db -> from('leaves');
     $this->db->where('userid',$id);
	

	 $this->db->select('duration, SUM(duration) AS duration', FALSE);
	  		  $this->db->where('status',"3");


$this->db->group_by('userid');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

	function GethalfOffDays($group,$startdate,$enddate){
 

  $this -> db -> select('*');

   $this -> db -> from('offdays');
     $this->db->where('groupid',$group);
	
	  $this->db->where('type',2);
	  $this->db->where('offday >=', $startdate);
	$this->db->where('offday <=',$enddate);
	  	 $this->db->select('offday, COUNT(offday) AS offday', FALSE);

	  


 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
 return $query1->result_array();
//return $this->db->count_all_results();
   }

   else

   {

     return false;

   }

}

	function GetOffDays($group,$startdate,$enddate){
 

  $this -> db -> select('*');

   $this -> db -> from('offdays');
     $this->db->where('groupid',$group);
	
	  //$this->db->where('type',$type);
	  $this->db->where('offday >=', $startdate);
	$this->db->where('offday <=',$enddate);
	  	 $this->db->select('offday, COUNT(offday) AS offday', FALSE);

	  


 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
 return $query1->result_array();
//return $this->db->count_all_results();
   }

   else

   {

     return false;

   }

}

function GetRemainingDaysByuser($id,$type){
 

  $this -> db -> select('*');

   $this -> db -> from('groupleavedays');
     $this->db->where('employeeno',$id);
	
	  $this->db->where('type',$type);
	 $this->db->select('days, SUM(days) AS days', FALSE);


$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

function Getqty($companyid,$code){
 $this->db->cache_on();

//  $this -> db -> select('total');
	 $this->db->select('total, SUM(total) AS myqty', FALSE);
   $this -> db -> from('newstock');

     $this->db->where('invoice_number',$code);
	
	$this->db->where('companyid',$companyid);
	$this->db->order_by('invoice_number','DESC');

$this->db->group_by('invoice_number');
$this->db->cache_off();
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}



function GetRemainingDaysByGroup($group,$type){
 

  $this -> db -> select('*');

   $this -> db -> from('groupleavedays');
     
	  $this->db->where('group',$group);
	  $this->db->where('type',$type);

	 $this->db->select('days, SUM(days) AS days', FALSE);
	  

$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}


function GetBalanceDaysByGroup($userid,$group){
 

  $this -> db -> select('*');

   $this -> db -> from('groupleavedays');
     
	  //$this->db->where('group',$group);

   $this->db->where("(employeeno='$userid' OR group='$group')", NULL, NULL);

	 $this->db->select('days, SUM(days) AS days', FALSE);
	

$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function GetBalanceDaysusergroup($userid,$group){
 

  $this -> db -> select('*');

   $this -> db -> from('groupleavedays');
     
	  //$this->db->where('group',$group);

   $this->db->where("(employeeno='$userid' OR group='$group')", NULL, NULL);

	 $this->db->select('days, SUM(days) AS days', FALSE);
	

//$this->db->group_by('type');
$this->db->group_by('employeeno');
$this->db->group_by('group');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

function GetUsedDaysByuser($userid){
 

  $this -> db -> select('*');

   $this -> db -> from('leaves');
     
	  $this->db->where('status','2');
	  $this->db->where('userid',$userid);

	 $this->db->select('duration, SUM(duration) AS duration', FALSE);
	  

$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}





function GettheUsedDaysByuser($userid,$type){
 

  $this -> db -> select('*');

   $this -> db -> from('leaves');
     
	  $this->db->where('status','3');
	  $this->db->where('userid',$userid);
	  $this->db->where('type',$type);

	 $this->db->select('duration, SUM(duration) AS duration', FALSE);
	  

$this->db->group_by('type');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}



	public function displayalldatatwo($limit, $start,$database,$where,$value,$where1,$value1) {
		
		
		
				 $this->db->order_by('id','DESC');
				

			$this->db->where($where, $value);
		$this->db->where($where1, $value1);
	 $this->db->limit($limit, $start);

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
			$records = $query->num_rows();

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            //return $data;
			
			
			   return array(
    'data' => $data,
    'records' => $records,
);
        }
        return false;
   }
	
	
	
	public function displayalldatastocktaking($limit, $start,$database,$where,$value,$where1,$value1) {
			$this->db->select("  * ,stocktaking.id  AS id ");

        $this->db->limit($limit, $start);
			$this->db->where("(branchid = '$value'  OR branchid=0  )", NULL, FALSE);

	//	$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		/// as id

		//$this->db->join('products', 'stocktaking.code = products.code','INNER JOIN');
				//$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');

			 $this->db->order_by('stocktaking.id','DESC');

	 		 //$this->db->order_by('productcategory.description','DESC');

	// $this->db->order_by('products.code','asc');
	 

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
		
	public function displayalldatatrialbalance($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
        $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit, ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);

$this->db->where('debitcredit.'.$where,$value);
$this->db->where('debitcredit.'.$where1,$value1);

$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
//$this->db->join('table3', 'table1.id = table3.id');

	$this->db->group_by('account');
	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	public function viewaccounttransfer($database,$where,$value,$where1,$value1,$fromdate,$todate,$accountname,$download) {
	//
	
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
  if($accountname !="all"){

	$this->db->where('account', $accountname);
  
  }
    if($value !="all"){
	   $this->db->where('debitcredit.'.$where,$value);
 
  }
$this->db->where('type','Transfer');  
$this->db->where('debitcredit.'.$where1,$value1);
  
$this->db->from('debitcredit');
 
$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
		
		
		$this->db->select("  * ,glaccounts.description  AS mydescription, debitcredit.description AS description,debitcredit.id AS id  ");
  
//$this->db->join('table3', 'table1.id = table3.id');

	//$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','DESC');
	
      //  $query = $this->db->get($database);
	  
	  
	  $bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){


 
   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Gl Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
		return $bioquery->result();


	}
   


}else{

  return false;
}

  
	  
			
   }
	
		
		
		
		
		public function displayspecificaccountCounter($database,$where,$value,$where1,$value1,$fromdate,$todate,$accountname,$download,$limit, $start) {
	//
	        //$this->db->limit($limit, $start);

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
  if($accountname=="all"){
	  
  }else{
	$this->db->where('account', $accountname);
  
  }
  $this->db->where($where,$value);
$this->db->where($where1,$value1);
  
$this->db->from('debitcredit');
 
$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
		
		
		$this->db->select("  * ,glaccounts.description, debitcredit.description AS mydescription,debitcredit.id AS id  ");
  
//$this->db->join('table3', 'table1.id = table3.id');

	//$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','DESC');
	
      //  $query = $this->db->get($database);
	  
	  
	  //$this->db->from($tablename);
		
		return $this->db->count_all_results();
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
			
   }
		
		
		
	
		public function displayspecificaccount($database,$where,$value,$where1,$value1,$fromdate,$todate,$accountname,$download,$limit, $start) {
	//
	        $this->db->limit($limit, $start);

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
  if($accountname=="all"){
	  
  }else{
	$this->db->where('account', $accountname);
  
  }
  $this->db->where($where,$value);
$this->db->where($where1,$value1);
  
$this->db->from('debitcredit');
 
$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
		
		
		$this->db->select("  * ,glaccounts.description, debitcredit.description AS mydescription,debitcredit.id AS id  ");
  
//$this->db->join('table3', 'table1.id = table3.id');

	//$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','DESC');
	
      //  $query = $this->db->get($database);
	  
	  
	  $bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){
		$records = $bioquery->num_rows();


 
   if($download=="yes"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Gl Report.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
		
		 foreach($bioquery->result() as $row)
        {
            $data[] = $row;
        }
		//
	   	   return array(
    'data' => $data,
    'records' => $records,
);
	


	}
   


}else{

  return false;
}

	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
			
   }
			
	public function displayalljournalentry($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
        $this->db->limit($limit, $start);
	
	
			//$this->db->select(" debitcredit.id AS id ");

	
	
	
	
	
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
		$this->db->select("  * ,glaccounts.description, debitcredit.description AS mydescription,debitcredit.id AS id ");

//$this->db->join('table3', 'table1.id = table3.id');

	//$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','DESC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	public function displayasset($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
       // $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname,glaccounts.accounttype as myaccounttype ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=1 )", NULL, FALSE);


	$this->db->group_by('glaccounts.accounttype');
	$this->db->order_by('accounttype','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
		
		
				
		
	public function displayLiability($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
        $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname  ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=2 )", NULL, FALSE);


	$this->db->group_by('glaccounts.accounttype');
	$this->db->order_by('accounttype','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
			
	public function displayEquity($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
        $this->db->limit($limit, $start);
	
//$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit, ");
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname  ");

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=3 )", NULL, FALSE);



	$this->db->group_by('glaccounts.accounttype');
	$this->db->order_by('accounttype','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
		
		
		
	public function displaysuaccount($database,$where,$value,$where1,$value1,$fromdate,$todate) {
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glgroups.id AS mygroupcode,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname   ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('debitcredit.'.$where,$value);
$this->db->where($where1,$value1);
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
//$this->db->where("(glaccounts.description=4  )", NULL, FALSE);
//$this->db->select("  * ,glgroups.accountname,  AS groupname ");

$this->db->join('glgroups', 'glaccounts.accounttype = glgroups.id','INNER JOIN');

	$this->db->group_by('account');
		//$this->db->group_by('bankname');

	
			$this->db->order_by('glgroups.groupname ','DESC');

	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
		
		
		

		
	public function displayincome2($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
       // $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glgroups.id AS mygroupcode,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid, glaccounts.bankname as mybankname ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('debitcredit.'.$where,$value);
$this->db->where('debitcredit.'.$where1,$value1);

$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=4 and glaccounts.bankname='' )", NULL, FALSE);
//$this->db->where("(glaccounts.number='1520' )", NULL, FALSE);

//$this->db->select("  * ,glgroups.accountname,  AS groupname ");
//$this->db->join('glaccounts', 'glaccounts.mybankname = glaccounts.number','outer JOIN');

$this->db->join('glgroups', 'glaccounts.accounttype = glgroups.id','INNER JOIN');
//$this->db->where("(glaccounts.number = '1520'  )", NULL, FALSE);

	$this->db->group_by('account');
			$this->db->order_by('glgroups.groupname ','DESC');

	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
		
	public function displayincome($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate,$branch) {
      //  $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glgroups.id AS mygroupcode,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('debitcredit.'.$where,$value);
$this->db->where('debitcredit.'.$where1,$value1);
if($branch !="all"){
$this->db->where('branch', $branch);
}
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=4 )", NULL, FALSE);
//$this->db->where("(glaccounts.description=4 and glaccounts.bankname='' )", NULL, FALSE);

$this->db->join('glgroups', 'glaccounts.accounttype = glgroups.id','INNER JOIN');

	$this->db->group_by('account');
			$this->db->order_by('glgroups.groupname ','DESC');

	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	public function displayexpense($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate,$branch) {
        $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glgroups.id AS mygroupcode,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname  ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('debitcredit.'.$where,$value);
$this->db->where('debitcredit.'.$where1,$value1);
if($branch !="all"){
$this->db->where('branch', $branch);
}

$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=5   )", NULL, FALSE);
$this->db->join('glgroups', 'glaccounts.accounttype = glgroups.id','INNER JOIN');


	$this->db->group_by('account');
		$this->db->order_by('glgroups.groupname ','DESC');

	$this->db->order_by('mygroupcode','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
		







public function displaynetprofit($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate) {
        $this->db->limit($limit, $start);
	$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname,glaccounts.accounttype as myaccounttype,debitcredit.account as account ");

//$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit, ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where($where,$value);
	if($value1 != "all"){
	$this->db->where($where1,$value1);

	}


$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=5  OR  glaccounts.description=4 )", NULL, FALSE);
//$this->db->where("(glaccounts.description=5 )", NULL, FALSE);


	$this->db->group_by('glaccounts.description');
	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	









public function displaybalancesheettype($limit, $start,$database,$where,$value,$where1,$value1,$fromdate,$todate,$type) {
      //  $this->db->limit($limit, $start);
	
$this->db->select("  * ,credit, SUM(credit) AS credit,debit,SUM(debit) AS debit,glgroups.id AS mygroupcode,glaccounts.accountname as myaccountname,glaccounts.id as subaccountid,glaccounts.bankname as mybankname,glaccounts.accounttype as myaccounttype,debitcredit.account as account ");
$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where($where,$value);
	if($value1 != "all"){
	$this->db->where($where1,$value1);

	}
$this->db->from('debitcredit');

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description='$type' )", NULL, FALSE);
//$this->db->where("(glaccounts.description=4 and glaccounts.bankname='' )", NULL, FALSE);

$this->db->join('glgroups', 'glaccounts.accounttype = glgroups.id','INNER JOIN');

	$this->db->group_by('account');
			$this->db->order_by('glgroups.accounttype ','DESC');

	$this->db->order_by('account','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }




		
			
	public function displayalldacashflow2($database,$where,$value,$where1,$value1,$fromdate,$todate,$bankname) {
        //$this->db->limit($limit, $start);
			if($fromdate!=""){

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('account', $bankname);

	if($bankname != "all"){
//$this->db->where('account', $bankname);

	}

	if($value1 != "all"){
	$this->db->where('debitcredit.'.$where1,$value1);

	}

$this->db->where('debitcredit.'.$where,$value);

$this->db->from('debitcredit');


$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=1  )", NULL, FALSE);
$this->db->select("  * ,debitcredit.description, debitcredit.description AS mydescription ,debitcredit.id AS id ");
 $this->db->select('debit, SUM(debit) AS debit', FALSE);
	 $this->db->select('credit, SUM(credit) AS credit', FALSE);

	$this->db->group_by('account');
		$this->db->group_by('transactioncode');

	
	
	$this->db->order_by('debitcredit.crateddate','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
			}else{
				 return false;
			}
   }
	
	
	
	
	
	
		
			
	public function displayalldacashflow($database,$where,$value,$where1,$value1,$fromdate,$todate,$bankname) {
        //$this->db->limit($limit, $start);
			if($fromdate!=""){

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('account', $bankname);
	if($value1 != "all"){
	$this->db->where('debitcredit.'.$where1,$value1);

	}

$this->db->where('debitcredit.'.$where,$value);

$this->db->from('debitcredit');


$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=1  )", NULL, FALSE);
$this->db->select("  * ,debitcredit.description, debitcredit.description AS mydescription ,debitcredit.id AS id ");


	//$this->db->group_by('account');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
			}else{
				 return false;
			}
   }
	
		
				
	
	
		public function displayalldacashflowSpecific($database,$where,$value,$where1,$value1,$fromdate,$todate,$bankname) {
        //$this->db->limit($limit, $start);
			if($fromdate!=""){

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('account', $bankname);
	if($value1 != "all"){
	$this->db->where('debitcredit.'.$where1,$value1);

	}

$this->db->where('debitcredit.'.$where,$value);

$this->db->from('debitcredit');


$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
//$this->db->where("(glaccounts.description=1  )", NULL, FALSE);
$this->db->select("  * ,debitcredit.description, debitcredit.description AS mydescription ,debitcredit.id AS id ");


	//$this->db->group_by('account');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
			}else{
				 return false;
			}
   }
	
		
				
	
	
	
	
	
	
	
		
				
	public function displayallbankreconcilition($database,$where,$value,$where1,$value1,$fromdate,$todate,$bankname,$confirm) {
        //$this->db->limit($limit, $start);
			if($fromdate!=""){

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('account', $bankname);
$this->db->from('debitcredit');

if($confirm !="All"){
	$this->db->where('confirm', $confirm);

	
}
$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');
$this->db->where("(glaccounts.description=1  )", NULL, FALSE);
$this->db->select("  * ,debitcredit.description, debitcredit.description AS mydescription ,debitcredit.id AS id ");


	//$this->db->group_by('account');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
      //  $query = $this->db->get($database);
			$query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
			}else{
				 return false;
			}
   }
	
			
	public function displayalldebitcreditsummary($companyid,$fromdate,$todate,$bankname,$branchid) {

		

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('account', $bankname);
$this->db->where('companyid', $companyid);


	if($branchid != "all"){
	
$this->db->where('debitcredit.branch', $branchid);
	}
$this->db->from('debitcredit');
	 $this->db->select('debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('credit, SUM(credit) AS finalcredit', FALSE);




	$this->db->group_by('account');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
  
   }
	
		
		
		
		
		
		
		
		
			public function displaycustomerstatementcUSTOMERS($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);
$this->db->where('account <>',5500);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('sales.customer');
$this->db->group_by('debitcredit.crateddate');
	
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
		
		
		
		
		
		
			public function displaycustomerstatementcUSTOMERSNOopening2($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//
$this->db->where('crateddate <=', $fromdate);

//$this->db->where('crateddate >=', $fromdate);
//$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('sales.customer');

	
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
		
		
		
		
			public function displaycustomerstatementcUSTOMERSNOopening($limit, $start,$branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){

				      $this->db->limit($limit, $start);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//
$this->db->where('crateddate <=', $fromdate);

//$this->db->where('crateddate >=', $fromdate);
//$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);








//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);

//$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 // $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);
		 
	$this->db->group_by('sales.customer');
//$this->db->group_by('debitcredit.code');
	
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
				public function displayallstatementBillNOopening($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 
		 
		  	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('expenses.datespent AS fromdate', FALSE);
	 	 $this->db->select('expenses.idno AS ordernumber', FALSE);
	 	 $this->db->select('expenses.updatedat  AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);

		 
		 
		 







//$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $fromdate);



//$this->db->where('fromdate >=', $fromdate);
//$this->db->where('fromdate <=', $todate);



$this->db->where('expenses.supplier', $supplier);
$this->db->where('expenses.type', "Bill");

$this->db->where('expenses.companyid', $companyid);

$this->db->from('expenses');







$this->db->join('debitcredit', 'expenses.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');




	$this->db->group_by('expenses.supplier');

	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }

		
			
			
			
			
			
			
			
			
		public function displayallstatementBill($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('expenses.datespent AS fromdate', FALSE);
	 	 $this->db->select('expenses.idno AS ordernumber', FALSE);
	 	 $this->db->select('expenses.updatedat  AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);









$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);



//$this->db->where('fromdate >=', $fromdate);
//$this->db->where('fromdate <=', $todate);
$this->db->where('expenses.supplier', $supplier);
$this->db->where('expenses.type', "Bill");

$this->db->where('expenses.companyid', $companyid);

$this->db->from('expenses');

	
$this->db->join('debitcredit', 'expenses.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('expenses.supplier');

	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }

		
			
		
		
		public function displayallstatementSupplier($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('purchaseorder.fromdate AS fromdate', FALSE);
	 	 $this->db->select('purchaseorder.ordernumber AS ordernumber', FALSE);
	 	 $this->db->select('purchaseorder.paiddate AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);









$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);



//$this->db->where('fromdate >=', $fromdate);
//$this->db->where('fromdate <=', $todate);
$this->db->where('purchaseorder.supplier', $supplier);
$this->db->where('purchaseorder.companyid', $companyid);

$this->db->from('purchaseorder');

	///
//$this->db->join('debitcredit', 'purchaseorder.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

//$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('purchaseorder.supplier');

	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }

		
			
			
			
			
			
		public function displayallstatementSupplierNOopening($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('purchaseorder.fromdate AS fromdate', FALSE);
	 	 $this->db->select('purchaseorder.ordernumber AS ordernumber', FALSE);
	 	 $this->db->select('purchaseorder.paiddate AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);









//$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $fromdate);



//$this->db->where('fromdate >=', $fromdate);
//$this->db->where('fromdate <=', $todate);
$this->db->where('purchaseorder.supplier', $supplier);
$this->db->where('purchaseorder.companyid', $companyid);

$this->db->from('purchaseorder');

	
$this->db->join('debitcredit', 'purchaseorder.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('purchaseorder.supplier');

	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }

		
			
			
			
			
			
			
			
			
			
			
				public function displayallstatement($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('purchaseorder.fromdate AS fromdate', FALSE);
	 	 $this->db->select('purchaseorder.ordernumber AS ordernumber', FALSE);
	 	 $this->db->select('purchaseorder.paiddate AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	 	  $this->db->select('debitcredit.code AS code', FALSE);


$this->db->where('fromdate >=', $fromdate);
$this->db->where('fromdate <=', $todate);
$this->db->where('purchaseorder.supplier', $supplier);
$this->db->where('purchaseorder.companyid', $companyid);

$this->db->from('purchaseorder');

	
$this->db->join('debitcredit', 'purchaseorder.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('account');
	$this->db->group_by('type');
		$this->db->group_by('crateddate');
	$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
	
		
		
			
	public function displayallstatementAllBills($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 	 $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 $this->db->select('debitcredit.account AS account', FALSE);
	 


		  	 $this->db->select('debitcredit.description AS mydescription', FALSE);
	 	 $this->db->select('expenses.datespent AS fromdate', FALSE);
	 	 $this->db->select('expenses.idno AS ordernumber', FALSE);
	 	 $this->db->select('expenses.updatedat  AS paiddate', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);

		 
		 

$this->db->where('datespent >=', $fromdate);
$this->db->where('datespent <=', $todate);
$this->db->where('expenses.supplier', $supplier);
$this->db->where('expenses.companyid', $companyid);

$this->db->from('expenses');

	
$this->db->join('debitcredit', 'expenses.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',2130);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');


	$this->db->group_by('account');
	$this->db->group_by('debitcredit.type');
		$this->db->group_by('crateddate');
	$this->db->group_by('code');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
	
		
		
		
		
			
	public function displaycustomerstatementLATEST($branchid,$companyid,$supplier) {

			if($supplier!=""){
				      $this->db->limit(1);

 
	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
$this->db->where('sales.customer', $supplier);

$this->db->where('sales.companyid', $companyid);

$this->db->order_by('sales.dateposted','DESC');




	
	
           $query = $this->db->get("sales");

  if($query->num_rows()>0){

  return $query->result_array();
 
}else{

  return false;
}

   }
	}
	
	
	
	public function displaycustomerstatementBoth($limit, $start,$branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
				      $this->db->limit($limit, $start);

 
//

$this->db->where('debitcredit.crateddate >=', $fromdate);
$this->db->where('debitcredit.crateddate <=', $todate);
$this->db->where('debitcredit.supplier', $supplier);
$this->db->where('debitcredit.companyid', $companyid);

//$this->db->from('sales');

//$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');

//$this->db->join('debitcredit', 'sales.customer = debitcredit.supplier','INNER JOIN');
$this->db->where('account<>',1100);
$this->db->where('account<>',1110);
$this->db->where('account<>',5500);
$this->db->where('account<>',2130);

$this->db->where('account<>',2060);


















$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
 	 $this->db->select('debitcredit.description AS mytable', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS fromdate', FALSE);
	 	 $this->db->select('debitcredit.id AS ordernumber', FALSE);
	 	 //$this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	//$this->db->order_by('debitcredit.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		//$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');
	$this->db->group_by('transactioncode');
	$this->db->group_by('mode');

	$this->db->group_by('debitcredit.code');
	//
	
           $query = $this->db->get("debitcredit");

     if ($query->num_rows() > 0) {

           return $query->result();

        }else{
		
			   return false;
			
						$data["correctdate"]= $this->displaycustomerstatementLATEST($branchid,$companyid,$supplier);
						 $correctstartdate=	$data["correctdate"][0]["fromdate"];
						
    $correctstartdate = date("Y-m-d 00:00:00", strtotime($data["correctdate"][0]["fromdate"]));
   
 
			
			
					      $this->db->limit($limit, $start);

 
//

$this->db->where('crateddate >=', $correctstartdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

//$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);

$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	$this->db->order_by('sales.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');

	$this->db->group_by('debitcredit.code');
	
	
           $query = $this->db->get("sales");

  if($query -> num_rows() >0)

   {
  return $query->result();

   }

   else

   {

     return false;

   }

	
		}






			}

   }
		
		
		
	
	
	
	
	
	public function displaycustomerstatement($limit, $start,$branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
				      $this->db->limit($limit, $start);

 
//

$this->db->where('debitcredit.crateddate >=', $fromdate);
$this->db->where('debitcredit.crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

//$this->db->from('sales');

$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');

//$this->db->join('debitcredit', 'sales.customer = debitcredit.supplier','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);

$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
 	 $this->db->select('sales.mytable AS mytable', FALSE);
	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	$this->db->order_by('sales.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		//$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');
	$this->db->group_by('transactioncode');
	$this->db->group_by('mode');

	$this->db->group_by('debitcredit.code');
	//
	
           $query = $this->db->get("sales");

     if ($query->num_rows() > 0) {

           return $query->result();

        }else{
		
			   return false;
			
						$data["correctdate"]= $this->displaycustomerstatementLATEST($branchid,$companyid,$supplier);
						 $correctstartdate=	$data["correctdate"][0]["fromdate"];
						
    $correctstartdate = date("Y-m-d 00:00:00", strtotime($data["correctdate"][0]["fromdate"]));
   
 
			
			
					      $this->db->limit($limit, $start);

 
//

$this->db->where('crateddate >=', $correctstartdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

//$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);

$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	$this->db->order_by('sales.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');

	$this->db->group_by('debitcredit.code');
	
	
           $query = $this->db->get("sales");

  if($query -> num_rows() >0)

   {
  return $query->result();

   }

   else

   {

     return false;

   }

	
		}






			}

   }
		
		
		
			
			
	public function displaycustomerstatement333($limit, $start,$branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
				      $this->db->limit($limit, $start);

 
//

$this->db->where('debitcredit.crateddate >=', $fromdate);
$this->db->where('debitcredit.crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

//$this->db->from('sales');

	$this->db->join('debitcredit', 'sales.customer = debitcredit.Supplier','INNER JOIN');

//$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);

$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);
 	 $this->db->select('sales.mytable AS mytable', FALSE);
	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	$this->db->order_by('sales.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		//$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');
	$this->db->group_by('transactioncode');
	$this->db->group_by('mode');

	$this->db->group_by('debitcredit.code');
	//
	
           $query = $this->db->get("sales");

     if ($query->num_rows() > 0) {

           return $query->result();

        }else{
		
			   return false;
			
						$data["correctdate"]= $this->displaycustomerstatementLATEST($branchid,$companyid,$supplier);
						 $correctstartdate=	$data["correctdate"][0]["fromdate"];
						
    $correctstartdate = date("Y-m-d 00:00:00", strtotime($data["correctdate"][0]["fromdate"]));
   
 
			
			
					      $this->db->limit($limit, $start);

 
//

$this->db->where('crateddate >=', $correctstartdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

//$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);

$this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);
					  	$this->db->order_by('debitcredit.crateddate','ASC');
						  	$this->db->order_by('sales.dateposted','DESC');

///$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');

	$this->db->group_by('debitcredit.code');
	
	
           $query = $this->db->get("sales");

  if($query -> num_rows() >0)

   {
  return $query->result();

   }

   else

   {

     return false;

   }

	
		}






			}

   }
		
		
		
			
	public function displaycustomerstatement2($branchid,$companyid,$fromdate,$todate,$supplier,$type) {

			if($fromdate!=""){
 $this->db->select('debitcredit.debit, SUM(debit) AS finaldebit', FALSE);
	 $this->db->select('debitcredit.credit, SUM(debitcredit.credit) AS finalcredit', FALSE);
	 $this->db->select('debitcredit.id AS id', FALSE);
	 $this->db->select('debitcredit.account AS account', FALSE);
	 	 $this->db->select('debitcredit.crateddate AS crateddate', FALSE);
	 	 $this->db->select('debitcredit.transactioncode AS transactioncode', FALSE);

	 	 $this->db->select('debitcredit.description AS mydescription', FALSE);
		 	 	 $this->db->select('debitcredit.type AS mytype', FALSE);
	  $this->db->select('debitcredit.code AS code', FALSE);

	 	 $this->db->select('sales.dateposted AS fromdate', FALSE);
	 	 $this->db->select('sales.id AS ordernumber', FALSE);
	 	 $this->db->select('sales.due_date AS paiddate', FALSE);
//

$this->db->where('crateddate >=', $fromdate);
$this->db->where('crateddate <=', $todate);
$this->db->where('sales.customer', $supplier);
$this->db->where('sales.companyid', $companyid);

$this->db->from('sales');

	
$this->db->join('debitcredit', 'sales.invoice = debitcredit.code','INNER JOIN');
$this->db->where('account <>',1100);
$this->db->where('account <>',1110);
$this->db->where('account <>',5500);
//$this->db->where("(account='4000'   OR account='1000'  )", NULL, FALSE);

$this->db->join('glaccounts', 'debitcredit.account = glaccounts.number','INNER JOIN');

	$this->db->group_by('debitcredit.type');
		$this->db->group_by('debitcredit.crateddate');
	$this->db->group_by('account');

	$this->db->group_by('debitcredit.code');
	$this->db->order_by('debitcredit.crateddate','ASC');
	
		 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
   
   }else{
				 return false;
			}
   }
		
	public function displayalldatathreesales($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		  	$this->db->select('todate, TODAY(todate) AS todate', FALSE);

		$this->db->where($where2, $value2);
		
		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
			$invoice="";
            foreach ($query->result() as $row) {
                $data[] = $row;
				$invoice=$row->invoice;
            }
            return $invoice;
			
        }
        return false;
   }






	public function displayalldatathreeDeals($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
	
	$this->db->select('*,todate, DATE(todate) AS todate', FALSE);
	$this->db->where($where, $value);
		//$this->db->where($where1, $value1);
				$this->db->where("(status='Interested'   OR status='Green'  OR status='Call Later'  OR status='Not Picking'  )", NULL, FALSE);

		$this->db->where('date(todate)', $value2);
		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
















		
	public function displayalldatathree($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	public function displayalldatathreeDueDeals($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
		$this->db->where("(status='Interested'   OR status='Green'  OR status='Call Later'  OR status='Not Picking'  )", NULL, FALSE);

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);


        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	
	
	
		function count_allwheredatafour($tablename,$where,$value,$where1,$value1,$where2,$value2,$where3,$value3)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
				$this -> db -> where($where2, $value2);
							$this->db->where($where3, $value3);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
		public function displayalldatafour($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2,$where3,$value3) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
			$this->db->where($where3, $value3);
		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
		
		function count_allwheredatafive($tablename,$where,$value,$where1,$value1,$where2,$value2,$where3,$value3,$where4,$value4)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
				$this -> db -> where($where2, $value2);
							$this->db->where($where3, $value3);
							$this->db->where($where4, $value4);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
		public function displayalldatafive($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2,$where3,$value3,$where4,$value4) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
			$this->db->where($where3, $value3);
										$this->db->where($where4, $value4);

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		function count_allcreditinvoice($tablename,$where,$value)
	{
		$this -> db -> where($where, $value);
				$this->db->where("(type='Invoice'   OR type='Credit'  )", NULL, FALSE);
		$this->db->where("credit <>", 0);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
		function count_allcreditinvoicebysupplier($tablename,$where,$value,$where1,$value1)
	{
		$this -> db -> where($where, $value);
				$this -> db -> where($where1, $value1);

				$this->db->where("(type='Invoice'   OR type='Credit'  )", NULL, FALSE);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
	
	public function displayallcreditinvoicebycustomer($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		//$this->db->where($where2, $value2);
			$this->db->where("(type='Invoice'   OR type='Credit'  )", NULL, FALSE);

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	
	
	
	
function getmyemployee($phone,$companyid,$firstname){
 

  $this -> db -> select('*');

   $this -> db -> from('people');
$this -> db -> where('phone', $phone);
$this -> db -> where('companyid', $companyid);
$this -> db -> where('firstname', $firstname);


 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

	
	
	
	
	public function displayallcreditinvoice($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		//$this->db->where($where1, $value1);
		//$this->db->where($where2, $value2);
		
			$this->db->where("(type='Invoice'   OR type='Credit'  OR type='Opening Balance' )", NULL, FALSE);
		$this->db->where("credit <>", 0);

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
      
   public function displayalldatathreeinvoices($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
				  	//$this->db->select("  * ,total, SUM(total) AS total,amount, SUM(amount) AS amount, credit, SUM(credit) AS credit, balance,SUM(balance) AS balance, ");
				//  $this->db->group_by('customer');

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
   
   public function displayalldatathreeinvoicesAll($limit, $start,$database,$where,$value,$where1,$value1,$where2,$value2) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		$this->db->where($where2, $value2);
				  	$this->db->select("  * ,total, SUM(total) AS total,amount, SUM(amount) AS amount, credit, SUM(credit) AS credit, balance,SUM(balance) AS balance, ");
				  $this->db->group_by('customer');

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	
	public function displayalldatathreebalance($limit, $start,$database,$where,$value,$where1,$value1) {
        $this->db->limit($limit, $start);
		
		  
		  	$this->db->select("  * ,amount, SUM(amount) AS amount, paid, SUM(paid) AS paid, ");

		$this->db->where($where, $value);
		$this->db->where($where1, $value1);
		

				$this->db->where("(status='Bill' OR 	status='Opening Balance'    OR status='purchases' OR 	status='p.o'  )", NULL, FALSE);
					//	$this->db->where("(status='purchases' OR 	status='p.o' and transactionstatus='closed'   OR 	status='Opening Balance' OR 	status='creditnote'  )", NULL, FALSE);

		
		//$this->db->where("(amount < paid   )", NULL, FALSE);

		$this->db->where("(amount < paid  OR amount!=paid  )", NULL, FALSE);
				  $this->db->group_by('supplier');

		 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	function count_allwheredatathreebalance($tablename,$where,$value,$where1,$value1)
	{
		$this -> db -> where($where, $value);
		//$this -> db -> where($where1, $value1);
						$this->db->where("(status='Bill'   OR status='purchases'  )", NULL, FALSE);

		$this->db->where("(amount>paid  OR amount!=paid  )", NULL, FALSE);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
				
				
				
				
				function count_allwheredatathreeDeals($tablename,$where,$value,$where1,$value1,$where2,$value2)
	{
						$this->db->select('*,todate, DATE(todate) AS todate', FALSE);
	$this->db->where($where, $value);
			$this->db->where("(status='Interested'   OR status='Green'  OR status='Call Later'  OR status='Not Picking'  )", NULL, FALSE);

		//$this->db->where($where1, $value1);
		$this->db->where('date(todate)', $value2);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
				
				
				
				
				
				
				
				
				
				
				
				
	
	function count_allwheredatathreeDealsDue($tablename,$where,$value,$where1,$value1,$where2,$value2)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
				$this -> db -> where($where2, $value2);
						$this->db->where("(status='Interested'   OR status='Green'  OR status='Call Later'  OR status='Not Picking'  )", NULL, FALSE);

		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
				
				

	function count_allwheredatathree($tablename,$where,$value,$where1,$value1,$where2,$value2)
	{
		$this -> db -> where($where, $value);
		$this -> db -> where($where1, $value1);
				$this -> db -> where($where2, $value2);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
		function count_allwheredataStockTaking($tablename,$where,$value,$where1,$value1)
	{
		//$this -> db -> where($where, $value);
	$this->db->where("(branchid = '$value'  OR branchid=0  )", NULL, FALSE);

	
	$this->db->where($where1,$value1);



		
		
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	function count_allwheredata($tablename,$where,$value,$where1,$value1)
	{
		$this -> db -> where($where, $value);
		
	
	$this->db->where($where1,$value1);



		
		
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	
	function count_allwherelow($tablename,$where,$value)
	{
		$this -> db -> where($where, $value);
		
			
		$this->db->where("(qty<reorderevel  OR qty=reorderevel  )", NULL, FALSE);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	function count_allwhere($tablename,$where,$value)
	{
		$this -> db -> where($where, $value);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
		public function displayallsmsperuser($limit, $start,$userid) {
        $this->db->limit($limit, $start);
		$this->db->where('userid', $userid);
        $query = $this->db->get("sms");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }	
 


public function displayallmembersperuser($limit, $start,$userid) {
        $this->db->limit($limit, $start);
		$this->db->where('userid', $userid);
        $query = $this->db->get("people");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }	

public function displayallproducts($limit, $start,$userid) {
        $this->db->limit($limit, $start);
		$this->db->where('userid', $userid);
        $query = $this->db->get("products");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }


   
public function displayallowproducts($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
				$this->db->where("(qty<reorderevel  OR qty=reorderevel  )", NULL, FALSE);

				// $this->db->order_by('id','DESC');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   function count_allwhereallstock($tablename,$where,$value)
	{
		
		$this->db->join('products', 'stock.code = products.code','INNER  JOIN');
				$this->db->join('productcategory', 'products.category = productcategory.id','INNER  JOIN');
		$this -> db -> where($where, $value);


		 // $this->db->group_by('stock.code');



		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
   
   public function displayallstock($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
		
		
		$this->db->select("  * ,stock.id  AS id,stock.userid  AS userid ");

		
		
		$this->db->join('products', 'stock.code = products.code','INNER JOIN');
				$this->db->join('productcategory', 'products.category = productcategory.id','INNER JOIN');


	 		 $this->db->order_by('productcategory.description','DESC');

			 		 $this->db->order_by('stock.branchid','asc');
	 $this->db->order_by('products.id','DESC');
			 		 $this->db->order_by('stock.startdate','desc');

		
 //$this->db->group_by('stock.branchid');
  //$this->db->group_by('stock.type');
  //$this->db->group_by('stock.code');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   public function displayallsalary($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		//$this->db->where($where, $value);
		
$this->db->select("  * ,salary.bankname,salary.bankaccount,salary.salary  AS mysalary,salary.id  AS id ");


		$this->db->join('people', 'salary.memberid = people.id','INNER JOIN');


	 $this->db->order_by('salary.id','DESC');


		
		        $query = $this->db->get($database);

		
		
		
		
		

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
      
public function displayallevents($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
public function displayall($limit, $start,$database,$where,$value) {
        $this->db->limit($limit, $start);
		$this->db->where($where, $value);
 $this->db->order_by('id','DESC');
        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   	function count_allwherewithor($tablename,$userid) 
	{
		$this->db->where("(userid='$userid' OR 	taskowner='$userid' OR 	controller='$userid')", NULL, FALSE);

		//$this -> db -> where($where, $value);
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
   
   public function displayallwithor($limit, $start,$database,$userid) {
        $this->db->limit($limit, $start);
	
			$this->db->where("(userid='$userid' OR 	taskowner='$userid' OR 	controller='$userid')", NULL, FALSE);
 $this->db->order_by('id','desc');

        $query = $this->db->get($database);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   
   
   
   
function populatecoursedocumets($id){

if($id!=null){
$this->db->select(" * from coursedocuments where courseid= '$id'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();

}else{

  return false;
}

}

}
function adddb($data,$table)
{
	$this->smsdb = $this->load->database('smsdb', true);
	
    $this->smsdb->insert($table, $data);
  if ($this->smsdb->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }
 }
function add($data,$table)
{
	$this->db->insert($table, $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }
 }




function uploadbackup($data)
{// Query to insert data in database
    //$regnumber = $data['regnumber'];
  
    $this->db->insert('backup', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }


}










function uploadcoursedocument($data)
{// Query to insert data in database
    //$regnumber = $data['regnumber'];
  
    $this->db->insert('coursedocuments', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }


}

function startapplication($data)
{// Query to insert data in database
    //$regnumber = $data['regnumber'];
  
    $this->db->insert('studentapplication', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }


}
function signup($data)
{// Query to insert data in database
  $email = $data['email'];
   $this -> db -> select("* from people where email='$email'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {

    $this->db->insert('people', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }

   }

   else

   {

     return false;

   }
}

function populatebiodata($id){

if($id!=null){
$this->db->select(" * from students where id= '$id'");
$this -> db -> limit(1);
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();

}else{

  return false;
}

}


}


function updatephoto($data,$paymentno)
  
{
 
  if($paymentno!=null){
     $this->db->where('paymentno',$paymentno);
 
   $qq = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('paymentno',$paymentno);
      $this->db->update('studentapplication',$data);

      $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }

   } else {
      
      return false;
      
   }


  
     
    
} else

   {

     return false;

   }

}


function updatepayment($data,$paymentno)
  
{
 
  if($paymentno!=null){
     $this->db->where('paymentno',$paymentno);
 
   $qq = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('paymentno',$paymentno);
      $this->db->update('studentapplication',$data);

      $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }

   } else {
      
      return false;
      
   }


  
     
    
} else

   {

     return false;

   }

}

function updateacademic($data,$paymentno)
  
{
 
  if($paymentno!=null){
     $this->db->where('paymentno',$paymentno);
 
   $qq = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('paymentno',$paymentno);
      $this->db->update('studentapplication',$data);

      $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }

   } else {
      
      return false;
      
   }


  
     
    
} else

   {

     return false;

   }

}











function updatecouse($data,$paymentno)
  
{
 
  if($paymentno!=null){
     $this->db->where('paymentno',$paymentno);
 
   $qq = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('paymentno',$paymentno);
      $this->db->update('studentapplication',$data);

      $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }

   } else {
      
      return false;
      
   }


  
     
    
} else

   {

     return false;

   }

}















function updateapplication($data,$id)
  
{
 
  if($id!=null){
     $this->db->where('id',$id);
 
   $qq = $this->db->get('studentapplication');
    $this->db->trans_start();
   if ( $qq->num_rows() > 0 ) 
   {
      $this->db->where('id',$id);
      $this->db->update('studentapplication',$data);
   } else {
      
      $this->db->insert('studentapplication', $datas);
      
   }



  
    $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE){// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
    
} else

   {

     return false;

   }

}

 function updateapplication1($data,$id) 
{
   $this->db->where('id',$id);
   $q = $this->db->get('studentapplication');

   if ( $q->num_rows() > 0 ) 
   {
      $this->db->where('id',$id);
      $this->db->update('studentapplication',$data);
   } 
}


 function populateapplyself($studentid){
 

  $this -> db -> select('*');

   $this -> db -> from('studentapplication');

 $this -> db -> where('studentid', $studentid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}
function populateproducts(){
 

  $this -> db -> select('*');

   $this -> db -> from('products');

 //$this -> db -> where('indexno', $indexno);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatexpenses(){
 

  $this -> db -> select('*');

   $this -> db -> from('expenses');

 //$this -> db -> where('indexno', $indexno);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatemyexpenses($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('expenses');

 $this -> db -> where('userid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}



function populateproductcategory(){
 

  $this -> db -> select('*');

   $this -> db -> from('productcategory');

 //$this -> db -> where('indexno', $indexno);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function populatemergedinvoice($invoice){

if($invoice!=null){
$this->db->select(" * from splitbillmaster where invoice='$invoice' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}


}








function confirmuser($table,$where, $value){
 

 if ($this->db->field_exists($where, $table))
{
        // some code...

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 //$this->db->order_by('id','DESC');
     $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
     return true;

   }

   else

   {

     return false;

   }
}else{
	     return false;

}
}








function getpendingorder($mybranch,$companyid,$maincategory){
 
  $this -> db -> select('invoice');

   $this -> db -> from('sales_orderkitchen');
   
   if($mybranch !="all"){
  $this->db->where('branch',$mybranch);
	}

  $this->db->where('maincategory',$maincategory);

  $this->db->where('companyid',$companyid);

	 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}




function populateprintinvoice($branch,$invoice){
//
if($invoice!=null){
	
 if($branch !="all"){
	 
$this->db->select("id,branch, receiptno,invoice ,currency,customer,name, discountedtotal,due_date,description ,type ,dateposted ,mytable ,maindiscount ,userid,balance,creditnote,cashier,savedpoints,savedcoins,	cash,   mpesa,cheque,card,voucher,credit,points,total,salesperson from sales where invoice='$invoice'  and branch='$branch'");
}else{
	
	$this->db->select("id, branch,receiptno,invoice ,currency,customer,name, discountedtotal,due_date,description ,type ,dateposted ,mytable ,maindiscount ,userid,balance,creditnote,cashier,savedpoints,savedcoins,	cash,   mpesa,cheque,card,voucher,credit,points,total,salesperson from sales where invoice='$invoice'");

}
	

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}


}




function populateinvoice($invoice){

if($invoice!=null){
$this->db->select("id,updatedby,paymentstatus, receiptno,salestype,invoice ,currency,customer,name, discountedtotal,due_date,description ,type ,dateposted ,mytable ,maindiscount ,userid,balance,creditnote,cashier,savedpoints,savedcoins,	cash,   mpesa,cheque,card,voucher,credit,points,total,salesperson,othercurrency,othercurrencyamount,tocurrencyamount, cardnumber,baddebt,currcptno,totrcptno,intrldata,rcptsign from sales where invoice='$invoice'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}


}

function populateinvoiceAll($invoice){

if($invoice!=null){
$this->db->select("*  from sales where invoice='$invoice'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}


}



function getesales_readingReceipt($id,$companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('sales_reading');
     $this->db->where('invoice',$id);
	  $this->db->where('companyid',$companyid);
	  //$this->db->where('salestype','retail');

	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	  $this->db->select('qty, SUM(qty) AS qty', FALSE);
//$this->db->where("( SUM(amount) AS amount )", NULL, FALSE);
$this->db->group_by('code');
//$this->db->where('createdat >=', $fromdate);
//$this->db->where('createdat <=', $todate);

//$this->db->order_by("id", "desc");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function populateinvoicereading($invoice){

if($invoice!=null){
$this->db->select(" * from salesreading where invoice='$invoice' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}


}






function populatemyinvoicebycode($invoice,$companyid,$userid,$status){
 if(($invoice)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sales');
	
	$this->db->where("(invoice='$invoice' and companyid='$companyid'  OR 	id='$invoice' and companyid='$companyid')", NULL, FALSE);

 if($status !="all"){
	 $this -> db -> where('userid', $userid); 
 }

 //$this -> db -> where('invoice', $invoice);
  //$this -> db -> where('companyid', $companyid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}






function populateincomeglgroups($companyid){
 if(($companyid)!=""){
	 


  //$this -> db -> select('*');
$this->db->select("  * ,accounttype AS mainaccounttype  ");

   $this -> db -> from('glgroups');
	
	$this->db->where("(	accounttype='4' and companyid='$companyid')", NULL, FALSE);

 
 $this->db->order_by('groupname','DESC');
 	$this->db->group_by('id');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}






function populateexpenseglgroups($companyid){
 if(($companyid)!=""){
	 


  //$this -> db -> select('*');
$this->db->select("  * ,accounttype AS mainaccounttype  ");

   $this -> db -> from('glgroups');
	
	$this->db->where("(accounttype='5'    and companyid='$companyid')", NULL, FALSE);

 
 $this->db->order_by('groupname','DESC');
 	$this->db->group_by('id');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}








function populateinvoicebycode($invoice,$companyid){
 if(($invoice)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sales');
	
	$this->db->where("(invoice='$invoice' and companyid='$companyid'  OR 	id='$invoice' and companyid='$companyid')", NULL, FALSE);

 //$this -> db -> where('invoice', $invoice);
  //$this -> db -> where('companyid', $companyid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}


function populateinvoicebycodeHold($invoice,$companyid){
 if(($invoice)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sales_holding');
	
	$this->db->where("(invoice='$invoice' and companyid='$companyid'  OR 	id='$invoice' and companyid='$companyid')", NULL, FALSE);

 //$this -> db -> where('invoice', $invoice);
  //$this -> db -> where('companyid', $companyid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}

function populatependingpayment($phone){
 if(($phone)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sms_in');
	$this->db->where("(sender_number='$phone' and used='2'  OR 	code='$phone' and used='2' )", NULL, FALSE);

 $this -> db -> where('sender_number <>', "settlement");
 $this -> db -> where('sender_number <>', "to");
// $this -> db -> where('sender_number <>', "received");
 $this -> db -> where('sender_number <>', "your"); 
  $this -> db -> where('sender_number <>', "office"); 
 //$this -> db -> where('sender_numbe', $phone);
 //$this -> db -> where('transactioncode', $phone);
 
 
 $this -> db -> where('used', 2);
$this->db->order_by('id','desc');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}



function populatependingpaymentEcitizen($phone,$salesid){
 if(($phone)!=""){
	
$phone=trim($phone);

  $this -> db -> select('*');

   $this -> db -> from('sms_in');
	$this->db->where("(sender_number='$phone'  OR 	code='$phone' OR 	msisdn='$salesid'  OR 	account_number='$phone' )", NULL, FALSE);

 //$this -> db -> where('sender_numbe', $phone);
 //$this -> db -> where('transactioncode', $phone);
 
 
 $this -> db -> where('used', 2);
$this->db->order_by('id','desc');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}










function populatependingpaymentFromEcitizenDirect($salesid){
 if(($salesid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sms_in');
	$this->db->where("(msisdn='$salesid' and used='2')", NULL, FALSE);

 //$this -> db -> where('sender_numbe', $phone);
 //$this -> db -> where('transactioncode', $phone);
 
 
 $this -> db -> where('used', 2);
$this->db->order_by('id','desc');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}












function populatependingpaymentbybranch($phone,$branchid){
 if(($phone)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sms_in');
	$this->db->where("(sender_number='$phone' and used='2' and branchid='$branchid'  OR 	code='$phone' and used='2'  and branchid='$branchid' )", NULL, FALSE);
	//$this->db->where("(sender_number !='to' or sender_number !='received'   or sender_number !='your' or sender_number !='office' or sender_number !='settlement' )", NULL, FALSE);

 $this -> db -> where('sender_number <>', "settlement");
 $this -> db -> where('sender_number <>', "to");
 //$this -> db -> where('sender_number <>', "received");
 $this -> db -> where('sender_number <>', "your"); 
  $this -> db -> where('sender_number <>', "office"); 

 $this -> db -> where('used', 2);
$this->db->order_by('id','desc');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}




function populatependingpaymentbybranchEcitizen($phone,$branchid,$salesid){
 if(($phone)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sms_in');
	$this->db->where("(sender_number='$phone' and used='2' and branchid='$branchid'  OR 	code='$phone' and used='2'  and branchid='$branchid'  OR msisdn='$salesid' and used='2'  and branchid='$branchid' OR account_number='$phone' and used='2'  and branchid='$branchid')", NULL, FALSE);

 //$this -> db -> where('sender_numbe', $phone);
 //$this -> db -> where('transactioncode', $phone);
 
 
 $this -> db -> where('used', 2);
$this->db->order_by('id','desc');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}




function populatependingmypayment($code){
 if(($code)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('sms_in');
		//$this->db->where("(phone='$phone' and status='0'  OR 	transactioncode	!='$phone' and status='0' )", NULL, FALSE);

 $this -> db -> where('code', $code);
 //$this -> db -> where('transactioncode', $phone);
 $this -> db -> where('used', 2);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}




function populatecategorybyowner($id){
 if(($id)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('productcategory');

 $this -> db -> where('id', $id);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}

function populatecategorybytasktype($id){
 if(($id)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('tasktype');

 $this -> db -> where('id', $id);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}


function  tasktype($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('tasktype');

 $this -> db -> where('companyid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}













function populatetaslcategory($companyid){
 if(($companyid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('tasktype');

 $this -> db -> where('companyid', $companyid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}


function populatecategory($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('category');

 $this -> db -> where('companyid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}
   
       function updatedatabytwo($data,$tablename,$where,$value,$where1,$value1)

{
  
	$this->db->where($where, $value);
 	$this->db->where($where1, $value1);

   if ( $this->db->update($tablename, $data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}
       function updatedatabythree($data,$tablename,$where,$value,$where1,$value1,$where2,$value2)

{
  
	$this->db->where($where, $value);
 	$this->db->where($where1, $value1);
 	$this->db->where($where2, $value2);

   if ( $this->db->update($tablename, $data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}



    function updatedatadb($data,$tablename,$where,$value)
    {

	$this->smsdb = $this->load->database('smsdb', true);
  
	
	$this->smsdb->where($where, $value);
 
  $this->smsdb->update($tablename, $data);
   if ($this->smsdb->affected_rows() > 0) 
      {
  return true;

      }else{
        return false; 
      }
  }
  
  
  
  
  
  
  
  
    function updatedata($data,$tablename,$where,$value)
    {

	$this->db->where($where, $value);
 
  $this->db->update($tablename, $data);
   if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
        return false; 
      }
  }
   
function populateproductcategorybyid($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('productcategory');

 $this -> db -> where('companyid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}


	
	public function searchmethod_handler($method)
	{
		if($method)
		{
			$this->session->set_userdata('method', $method);
			return $method;
		}
		elseif($this->session->userdata('method'))
		{
			$method = $this->session->userdata('method');
			return $method;
		}
		else
		{
			$method ="";
			return $method;
		}
	}
	public function searchterm_handler($searchterm)
	{
		if($searchterm)
		{
			$this->session->set_userdata('searchterm', $searchterm);
			return $searchterm;
		}
		elseif($this->session->userdata('searchterm'))
		{
			$searchterm = $this->session->userdata('searchterm');
			return $searchterm;
		}
		else
		{
			$searchterm ="";
			return $searchterm;
		}
	}
	
	public function searchallstore($searchterm,$limit)
{
    $sql = "SELECT * FROM  productlist
            WHERE name LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or category LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or supplier LIKE '%" . $searchterm . "%'  or code = '" . $searchterm . "'   or 	description	 LIKE '%" . $searchterm . "%'    LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	public function searchallproducts($searchterm,$limit)
{
	
	    $sql = "SELECT * FROM  products  WHERE  code = '$searchterm'  or  hscode = '$searchterm'  LIMIT " .$limit . ",20";

	
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
		
        $sql = "SELECT * FROM  products  WHERE  code = '$searchterm'   or name LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or 	description	 LIKE '%" . $this->db->escape_like_str($searchterm ). "%'    LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
    }
}
public function searchallassets($searchterm,$limit)
{
    $sql = "SELECT * FROM  assets
            WHERE  code = '$searchterm'   or name LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or 	description	 LIKE '%" . $searchterm . "%'    LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}






public function searchforloyalitypoints($searchterm,$type,$limit)
{
	if($type=="all"){
    $sql = "SELECT * FROM   loyalitycards  WHERE  cardnumber = '$searchterm'   or dateposted LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or 	description	 LIKE '%" . $searchterm . "%'    LIMIT " .$limit . ",200";
    }else{
		
	    $sql = "SELECT * FROM   loyalitycards  WHERE   type= '" . $type . "'  and  cardnumber = '$searchterm'   or dateposted LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or 	description	 LIKE '%" . $searchterm . "%'     LIMIT " .$limit . ",200";
	
	}
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}













	public function searchalldeals_count($searchterm,$branchid,$startdate,$closedate, $limit,$dealtype)
	{
		if($searchterm=="all"){


	if($dealtype=="Normal"){
					 $sql = "SELECT COUNT(*) As cnt FROM deals WHERE    	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate' ";
    
	}else{
					 $sql = "SELECT COUNT(*) As cnt FROM deals WHERE    	branchid	 = '$branchid'  and todate BETWEEN  '$startdate'  AND  '$closedate' ";
    	
	}

			
		}else{
			
			if($dealtype=="Normal"){
				 $sql = "SELECT COUNT(*) As cnt FROM deals WHERE   userid = '$searchterm'    and 	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate' ";
    
	}else{
				 $sql = "SELECT COUNT(*) As cnt FROM deals WHERE   userid = '$searchterm'    and 	branchid	 = '$branchid'  and todate BETWEEN  '$startdate'  AND  '$closedate' ";
    	
	}
			
	
			
		}
            
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}


public function searchalldeals222($searchterm,$branchid,$startdate,$closedate, $limit,$dealtype)
{
	if($searchterm=="all"){
		


	if($dealtype=="Normal"){
		$sql = "SELECT * FROM  deals  WHERE 	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    
	}else{
		$sql = "SELECT * FROM  deals  WHERE 	branchid	 = '$branchid'  and todate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    	
	}




    
	}else{
    	
	
	
	

	if($dealtype=="Normal"){
	 $sql = "SELECT * FROM  deals WHERE  userid = '$searchterm'    and 	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    
	}else{
	 $sql = "SELECT * FROM  deals WHERE  userid = '$searchterm'    and 	branchid	 = '$branchid'  and todate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    	
	}



	
	
	
	}
	
	
   
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}



public function searchalldeals($searchterm,$branchid,$startdate,$closedate, $limit,$dealtype,$action)
{

	
	
		       // $this->db->limit($limit, $start);

		if($dealtype =="Normal"){
	$this->db->where('fromdate >=', $startdate);
$this->db->where('fromdate <=', $closedate);
	}else{
$this->db->where("(   todate  BETWEEN '$startdate'  and '$closedate'  )", NULL, FALSE);
	
		
	
	//$this->db->where('todate >=', $startdate );
	//$this->db->where('todate <=', $closedate);	
	}
	
	
	
	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}

	if($searchterm !="all"){
$this->db->where('userid', $searchterm);
	}
	
	if($action !="all"){
$this->db->where('status', $action);
	}
		
		

//$this->db->where('companyid',$companyid);

		 $this->db->order_by('id','desc');


$this->db->from('deals');


	
	
	
	
	 $q = $this -> db -> get();
	
   
	//$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}




















public function searchallexpense_count($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		
		
		if($searchterm=="all"){
					 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE    	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";

			
		}else{
			
				 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE   supplier = '$searchterm'    and 	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";
	
			
		}
            
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}






public function downloaddetailedsles($searchdata)
 
 {
	 
	
	
	 							$downloadtype = $searchdata['downloadtype'];

							$group = $searchdata['group'];
							$branchid = $searchdata['branchid'];
							   $todate = $searchdata['todate'];
                             	$maincategory = $searchdata['maincategory'];

                               $fromdate = $searchdata['fromdate'];
                             $new_date=date("Y-m-d h:s");
        $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Detailed Sales.csv";
		
		

    if($group =="yes"){
		
		if($downloadtype=="Summary"){
		$this -> db -> select('products.name as ItemName,SUM(sales_order.qty) AS qty, SUM(sales_order.amount) AS amount,sales_order.salestype');		
	}else{$this -> db -> select('products.name as ItemName,sales_order.invoice,sales_order.code, SUM(sales_order.qty) AS qty, SUM(sales_order.amount) AS amount,sales_order.salestype,sales_order.discount,sales_order.dateposted,sales_order.address,sales_order.description,sales_order.user,sales_order.bprice,sales_order.type, SUM(sales_order.tax) AS tax,sales_order.sprice,sales_order.balance,sales_order.customer,sales_order.userid,sales_order.mode,sales_order.companyid,sales_order.date,sales_order.maincategory,sales_order.category,sales_order.status,sales_order.branch,sales_order.ordersales,sales_order.uom,sales_order.sales,sales_order.updatedat,sales_order.updatedby,sales_order.taxindicator,sales_order.serial,  SUM(sales_order.tax2) AS tax2,sales_order.discountype,sales_order.discountvalue,sales_order.vattype, SUM(sales_order.taxable) AS taxable ,sales_order.uomqty,sales_order.stockbefore,sales_order.stockafter,sales_order.taxtype,sales_order.myinvoice');		
		}
	

 
  }else{
	  
	 	if($downloadtype=="Summary"){
	$this -> db -> select('products.name as ItemName,sales_order.qty,sales_order.amount,sales_order.salestype');	  
	  
		}else{
			
			$this -> db -> select('products.name as ItemName,sales_order.invoice,sales_order.code,sales_order.qty,sales_order.amount,sales_order.salestype,sales_order.discount,sales_order.dateposted,sales_order.address,sales_order.description,sales_order.user,sales_order.bprice,sales_order.type,sales_order.tax,sales_order.sprice,sales_order.balance,sales_order.customer,sales_order.userid,sales_order.mode,sales_order.companyid,sales_order.date,sales_order.maincategory,sales_order.category,sales_order.status,sales_order.branch,sales_order.ordersales,sales_order.uom,sales_order.sales,sales_order.updatedat,sales_order.updatedby,sales_order.taxindicator,sales_order.serial,sales_order.tax2,sales_order.discountype,sales_order.discountvalue,sales_order.vattype,sales_order.taxable,sales_order.uomqty,sales_order.stockbefore,sales_order.stockafter,sales_order.taxtype,sales_order.myinvoice');	  
	  	
		} 
	  
	  
	  

  }
	

	


   $this -> db -> from('sales_order');
   
   	$this->db->join('products', 'sales_order.code = products.code','INNER JOIN');
   
    $this->db->where('sales_order.status <>',"returned");

   //

    if($branchid !="all"){
	//$this->db->where("branch",$branchid);
 	$this->db->where("sales_order.branch",$branchid);

  }
	    if($maincategory !="all"){
	$this->db->where("sales_order.maincategory",$maincategory);
 
  }


    if($group =="yes"){
	
	
	$this->db->group_by('sales_order.sprice');

	$this->db->group_by('sales_order.code');
	$this->db->group_by('sales_order.branch');
	$this->db->group_by('sales_order.serial');
	
 
  }

//
$this->db->where('sales_order.dateposted >=', $fromdate);
	$this->db->where('sales_order.dateposted <=',$todate);
 $this->db->order_by('sales_order.id','desc');
 $query = $this -> db -> get();
 
 if($query -> num_rows() >0)

   {
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);

			force_download($filename, $data); 

   }

   else

   {

     return false;

   }
  

}













public function downloadsummarysles($searchdata)
 
 {
	
	 
							$branchid = $searchdata['branchid'];
							   $todate = $searchdata['todate'];
                             
                               $fromdate = $searchdata['fromdate'];
                             $new_date=date("Y-m-d h:s");
        $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Summary Sales.csv";
		 //$this -> db -> select(' people.firstname AS CustomerName, sales.cashier,sales.type,sales.amount,sales.due_date,sales.name,sales.balance,sales.userid,sales.dateposted,sales.customer,sales.cash,sales.total,sales.companyid,sales.salestype,sales.maindiscount,sales.discountedtotal,sales.branch,sales.card,sales.voucher,sales.credit,sales.points,sales.mpesa,sales.cheque,sales.ordersales,sales.description,sales.mytable,sales.maincategory,sales.creditnote,sales.invoice,sales.paymentstatus,sales.updatedby,sales.updatedat,sales.plotno,sales.houseno,sales.baddebt,sales.tax1,sales.tax2,sales.cardnumber,sales.savedcoins,sales.savedpoints,sales.salesperson');

 $this -> db -> select(' sales.id as Rct_No,people.firstname AS CustomerName, sales.type,sales.amount,sales.due_date,sales.name,sales.balance,sales.userid,sales.dateposted,sales.customer,sales.cash,sales.total,sales.companyid,sales.salestype,sales.maindiscount as OriginalAmount,sales.discountedtotal,sales.branch,sales.card,sales.voucher,sales.credit,sales.points,sales.mpesa,sales.cheque,sales.ordersales,sales.description,sales.mytable,sales.maincategory,sales.creditnote,sales.invoice,sales.paymentstatus,sales.updatedby,sales.updatedat,sales.plotno,sales.houseno,sales.baddebt,sales.tax1,sales.tax2,sales.cardnumber,sales.savedcoins,sales.savedpoints,sales.salesperson,sales.cashier,receiptno,people.idnumber as PIN');
   //	$this->db->join('people', 'sales.customer = people.id','LEFT JOIN');
$this->db->join('people', 'people.id = sales.customer', 'left');
   $this -> db -> from('sales');

    if($branchid !="all"){
	$this->db->where("sales.branch",$branchid);
 
  }
	

$this->db->where('sales.dateposted >=', $fromdate);
		$this->db->where('sales.dateposted <=',$todate);
 $this->db->order_by('sales.id','desc');
 $query = $this -> db -> get();
 
 if($query -> num_rows() >0)

   {
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);

			force_download($filename, $data); 

   }

   else

   {

     return false;

   }
  

}






	
	
public function searchallexpensesrequest($searchterm,$branchid,$startdate,$closedate, $type)
 
 {
	 
	
  $this -> db -> select('*');

   $this -> db -> from('expenses');
   if($searchterm !="all"){
	$this->db->where("userid",$searchterm);
 
  }
    if($branchid !="all"){
	$this->db->where("branchid",$branchid);
 
  }
	
  if($type !="all"){
	  //echo $type;
	  //exit;
	    if($type=="ApprovedActive"){
		   	$this->db->where("(status='Active' OR  status='Approved' OR  status='Closed'    )", NULL, FALSE);
	  }else{
		 $this->db->where("status",$type);
 		   	//$this->db->where("(type='$type'   )", NULL, FALSE);

	  }
  }
$this->db->where('datespent >=', $startdate);
		$this->db->where('datespent <=',$closedate);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
  

}

	

public function searchallexpensesrequest2($searchterm,$branchid,$sdatespenttartdate,$closedate, $limit)
{
	if($searchterm=="all"){
		
		 $sql = "SELECT * FROM  expenses  WHERE 	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate'  and type !='Rejected'";
    
	}else{
		
	 $sql = "SELECT * FROM  expenses  WHERE  userid = '$searchterm'    and 	branchid	 = '$branchid'  and  BETWEEN  '$startdate'  AND  '$closedate' and type !='Rejected'  ";
    	
		
	}
	
   
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}














public function searchallexpenserequest_count($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		if($searchterm=="all"){
					 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE    	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";

			
		}else{
			
				 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE   userid = '$searchterm'    and 	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";
	
			
		}
            
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}







public function searchallexpense_count22($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		if($searchterm=="all"){
					 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE    	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";

			
		}else{
			
				 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE   supplier = '$searchterm'    and 	branchid	 = '$branchid'  and datespent BETWEEN  '$startdate'  AND  '$closedate' ";
	
			
		}
            
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}


public function searchallexpenses($searchterm,$companyid,$branchid,$startdate,$closedate,$download,$department,$bankname)
{
		
	 $new_date=date("Y-m-d H:i");		

  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Expense Download.csv";
		
		
		
		if($download=="yes"){
$this->db->select("expenses.id,expenses.type,expenses.userid,expenses.amount,expenses.description,expenses.dateposted,expenses.user,expenses.status,expenses.datespent,expenses.receiptno,expenses.idno,expenses.contact,expenses.controller,expenses.imgid,expenses.img_id,expenses.companyid,expenses.accounttype,expenses.bankname,expenses.supplier,expenses.invoice,expenses.paymentmethodes,expenses.duedate,expenses.terms,expenses.balance,expenses.paid,expenses.updatedat,expenses.updatedby,expenses.branchid,expenses.allocation,expenses.invoicestatus,expenses.allocationtext,expenses.loanpaid,expenses.loanbalance,expenses.duration,expenses.monthlyrate,branch.name  as BranchName");

	}else{
	$this->db->select("  *  ");
	
		
	}

	
	 $this -> db -> from('expenses');
   
   
	if($branchid !="all"){
  $this->db->where('expenses.branchid',$branchid);
	}
		if($searchterm !="all"){
  $this->db->where('expenses.supplier',$searchterm);
	}
		if($bankname !="all"){
  $this->db->where('expenses.bankname',$bankname);
	}
		if($department !="all"){
  $this->db->where('expenses.department',$department);
	}
  $this->db->where('expenses.type',"Expense");
  $this->db->where('expenses.companyid',$companyid);

		
$this->db->where('expenses.datespent >=', $startdate);
$this->db->where('expenses.datespent <=', $closedate);
				 $this->db->order_by('expenses.id','DESC');

		
		
		
		
		
		
		
		
		
		
		if($download=="yes"){
							   //	$this->db->select("  * ,glaccounts.number  as bank ");

		//$this->db->join('glaccounts', 'expenses.bankname = glaccounts.number','INNER JOIN' );
		$this->db->join('branch', 'expenses.branchid = branch.id','INNER JOIN' );

	
			
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Low Store Items Report.csv";
		
				$bioquery = $this -> db -> get();


if($bioquery->num_rows()>0){


        $data = $this->dbutil->csv_from_result($bioquery, $delimiter, $newline);

        force_download($filename, $data);
		
		
}
			
		
		}else{
		
	

	
	
	
   				$q = $this -> db -> get();

    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
		}
}







public function searchalltasks($companyid,$searchterm,$branchid,$startdate,$closedate, $status,$category,$limit,$datetype)
{
 

   $this -> db -> from('tasks');
   
   
	if($branchid !="all"){
  $this->db->where('branchid',$branchid);
	}
		if($searchterm !="all"){
  $this->db->where('taskowner',$searchterm);
	}
	if($status !="all"){
  $this->db->where('status',$status);
	}
	if($category !="all"){
  $this->db->where('category',$category);
	}
	if($datetype=="Created"){
$this->db->where('dateposted >=', $startdate);
$this->db->where('dateposted <=', $closedate);	
}else{
	$this->db->where('fromdate >=', $startdate);
$this->db->where('fromdate <=', $closedate);	
	}







  $this->db->where('companyid',$companyid);

		if($searchterm !="all"){
	 $this->db->order_by('id','DESC');
	}else{
		 $this->db->order_by('taskowner','ASC');
		 $this->db->order_by('id','DESC');

	}
  
	 
   //$this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
   if($query1->num_rows() > 0)
    {
		$records = $query1->num_rows();
        foreach($query1->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}







public function searchalltasks2__________($searchterm,$branchid,$startdate,$closedate, $status,$limit)
{
	if($searchterm=="all"){
		 $sql = "SELECT * FROM   tasks WHERE 	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    
	}else{
	 $sql = "SELECT * FROM   tasks WHERE  taskowner = '$searchterm'    and 	branchid	 = '$branchid'  and fromdate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    	
	}
	
   
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


























public function searchallfollowups_count($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		if($searchterm=="all"){
					 $sql = "SELECT COUNT(*) As cnt FROM followups WHERE    	branchid	 = '$branchid'  and date BETWEEN  '$startdate'  AND  '$closedate' ";

			
		}else{
			
				 $sql = "SELECT COUNT(*) As cnt FROM followups WHERE   userid = '$searchterm'    and 	branchid	 = '$branchid'  and date BETWEEN  '$startdate'  AND  '$closedate' ";
	
			
		}
            
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}


public function searchallfollowps($searchterm,$branchid,$startdate,$closedate, $limit,$dealtype,$action)
{

	
	
		       // $this->db->limit($limit, $start);

	
		if($dealtype =="Normal"){
	$this->db->where('date >=',  $startdate);
$this->db->where('date <=',$closedate );
	}else{
		
	$this->db->where('nextdate >=',$startdate);
	$this->db->where('nextdate <=', $closedate );	
	}
	
	
	
	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}

	if($searchterm !="all"){
$this->db->where('userid', $searchterm);
	}
	
	if($action !="all"){
$this->db->where('status', $action);
	}
		
		

//$this->db->where('companyid',$companyid);

		 $this->db->order_by('id','desc');


$this->db->from('followups');


	
	
	
	
	 $q = $this -> db -> get();
	
   
	//$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


















	public function searchallopenclosestock_count($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		
            
		 $sql = "SELECT COUNT(*) As cnt FROM stock WHERE   code = '$searchterm'    and 	branchid	 = '$branchid'  and startdate BETWEEN  '$startdate'  AND  '$closedate' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}


	public function searchallstocktaking_count($searchterm,$branchid,$startdate,$closedate, $limit)
	{
		
            
		 $sql = "SELECT COUNT(*) As cnt FROM stocktaking WHERE   code = '$searchterm'    and 	branchid	 = '$branchid'  and startdate BETWEEN  '$startdate'  AND  '$closedate' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}



	
	
	
	public function searchallclosingstock_count($searchterm,$branchid,$startdate, $limit)
	{
		
            
		 $sql = "SELECT COUNT(*) As cnt FROM stock WHERE   code = '$searchterm'    and 	branchid	 = '$branchid'  and startdate =  '$startdate' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

	
	public function Splitmpesa()
{
	   // $sql = "select   * from  sms_in where used='0' and  amount < 0 ORDER BY id DESC limit 50";

    $sql = "select   * from  sms_in where used='0'  ORDER BY id DESC limit 50";
    			//$user_query=mysql_query("select   * from  sms_in where used='0' or  amount <1 ORDER BY id DESC limit 20 ")or die(mysql_error());

	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

	
	
function populatealldataarraythree($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


	
public function searchallclosingtock($searchterm,$branchid,$startdate, $limit)
{
    $sql = "SELECT * FROM  stock WHERE  code = '$searchterm'    and 	branchid	 = '$branchid'  and startdate =  '$startdate'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

public function searchallopenclosestock($searchterm,$branchid,$startdate,$closedate,$daterange,$companyid, $limit)
{
			       // $this->db->limit($limit, $start);

	if($daterange!="all"){
		$this->db->where('startdate >=', $startdate);
$this->db->where('startdate <=', $closedate);
	}
	
	
	
	
	
	
	
	if($branchid !="all"){
$this->db->where('branchid', $branchid);
	}

	if($searchterm !="all"){
$this->db->where('code', $searchterm);
	}
		
		

$this->db->where('companyid',$companyid);

		 $this->db->order_by('id','desc');


$this->db->from('stock');


	
	
	
	
	 $q = $this -> db -> get();

    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


public function populatealltables($database)

{
	
    $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema ='robipos'  ";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
          return $q->result();

    }
    else
    {
        return false;
    }
}
public function searchallstocktaking($searchterm,$branchid,$startdate,$closedate, $limit)
{
    $sql = "SELECT * FROM  stocktaking
            WHERE  code = '$searchterm'    and 	branchid	 = '$branchid'  and startdate BETWEEN  '$startdate'  AND  '$closedate'  LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

	public function searchallproductlist_2($searchterm,$limit,$viewproductsfromallbranches,$branch)
{
	if($viewproductsfromallbranches==true){
		    $sql = "SELECT * FROM  productlist WHERE  code = '$searchterm'   or name LIKE '" . $this->db->escape_like_str($searchterm) . "%'  or 	description	 LIKE '%" . $searchterm . "%'    LIMIT " .$limit . ",20";

	}else{
		    $sql = "SELECT * FROM  productlist WHERE  code = '$searchterm'  and branch='$branch'  or name LIKE '" . $this->db->escape_like_str($searchterm) . "%' and branch='$branch' or 	description	 LIKE '%" . $searchterm . "%'  and branch='$branch'  LIMIT " .$limit . ",20";

	}
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


public function searchallproductlist($searchterm,$limit,$viewproductsfromallbranches,$branch)
{
	if($viewproductsfromallbranches==true){
		    $sql = "SELECT * FROM  productlist WHERE  code = '$searchterm'  or  hscode = '$searchterm'  LIMIT " .$limit . ",100";

	}else{
		    $sql = "SELECT * FROM  productlist WHERE  code = '$searchterm'  and branch='$branch' or  hscode = '$searchterm' and branch='$branch'   LIMIT " .$limit . ",100";

	}
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}




	public function searchallstore_count($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM productlist WHERE name LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or category LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or supplier LIKE '%" . $searchterm . "%' or code = '" . $searchterm . "' or 	description	 LIKE '%" . $searchterm . "%'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	public function searchallproducts_count($searchterm)
	{
	
		
	
		$sql = "SELECT COUNT(*) As cnt FROM products WHERE code = '$searchterm'  or  hscode = '$searchterm'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		if($row->cnt >0 ){
				
		return $row->cnt;

		}else{
	
			
		$sql = "SELECT COUNT(*) As cnt FROM products WHERE code = '$searchterm' or name LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  	description	 LIKE '%" . $searchterm . "%'  ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
		}
	}


	public function searchalldispatchstockitems_count($searchterm)
	{
	
		
	
			$sql = "SELECT COUNT(*) As cnt FROM newstock WHERE description = '$searchterm' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		if($row->cnt >0 ){
		return $row->cnt;

		}else{
			
		$sql = "SELECT COUNT(*) As cnt FROM newstock WHERE item_id = '$searchterm' or description LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  	invoice	 LIKE '%" . $searchterm . "%'  or  	invoice_number	 LIKE '%" . $searchterm . "%'  or  	serial	 LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
		}
	}




public function searchallalldispatchstockitems($searchterm,$limit)
{
	
	$sql = "SELECT * FROM  newstock  WHERE  description = '$searchterm'     LIMIT " .$limit . ",20";

	
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
	 $sql = "SELECT * FROM newstock WHERE item_id = '$searchterm' or description LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  	invoice	 LIKE '%" . $searchterm . "%'  or  	invoice_number	 LIKE '%" . $searchterm . "%'  or  	serial	 LIKE '%" . $searchterm . "%'  LIMIT " .$limit . ",20";

	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
    }
}












public function searchallproductlist_count($searchterm,$viewproductsfromallbranches,$branchid)
	{
		if($viewproductsfromallbranches==true){
					 $sql = "SELECT COUNT(*) As cnt FROM productlist WHERE code = '$searchterm'  and branch = '$branchid' or name LIKE '" . $this->db->escape_like_str($searchterm) . "%'  and branch = '$branchid' or  	description	 LIKE '%" . $searchterm . "%'   and branch = '$branchid' ";

		}else{
				 $sql = "SELECT COUNT(*) As cnt FROM productlist WHERE code = '$searchterm' or name LIKE '" . $this->db->escape_like_str($searchterm) . "%' or  	description	 LIKE '%" . $searchterm . "%'  ";
	
		}
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}








	public function search_task_count($searchterm,$comapanyid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM tasks WHERE taskowner LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and companyid='$comapanyid' or subject LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  and companyid='$comapanyid' or fromdate LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or todate LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or 	status	 LIKE '%" . $searchterm . "%' and companyid='$comapanyid'  or 	controller LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or comments LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or dateposted LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or category LIKE '%" . $searchterm . "%' and companyid='$comapanyid'  or userid LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or companyid LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
public function searchalltask($searchterm,$limit,$comapanyid)
{
    $sql = "SELECT * FROM tasks WHERE taskowner LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  and companyid='$comapanyid' or subject LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and companyid='$comapanyid' or fromdate LIKE '%" . $searchterm . "%'  and companyid='$comapanyid' or todate LIKE '%" . $searchterm . "%'   and companyid='$comapanyid' or 	status	 LIKE '%" . $searchterm . "%'   and companyid='$comapanyid' or 	controller LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or comments LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or dateposted LIKE '%" . $searchterm . "%' and companyid='$comapanyid' or category LIKE '%" . $searchterm . "%'and companyid='$comapanyid'  or userid LIKE '%" . $searchterm . "%'and companyid='$comapanyid' or companyid LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}




	public function search_record_payment($searchterm,$method)
	{
   	if($method=="whole"){
				 $sql = "SELECT COUNT(*) As cnt FROM  sms_in WHERE code = '" . $this->db->escape_like_str($searchterm) . "' or sender_number = '" . $this->db->escape_like_str($searchterm) . "'";

	}else{
				 $sql = "SELECT COUNT(*) As cnt FROM  sms_in WHERE SUBSTRING(code,-4 )  =  '" . $this->db->escape_like_str($searchterm) . "' or  SUBSTRING(sender_number,-4 )=  '%" . $this->db->escape_like_str($searchterm) . "'";

	}
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}

	public function searchallreceivedmoney($searchterm,$method,$limit)
{
	
	if($method=="whole"){
		  $sql = "SELECT * FROM sms_in 
            WHERE code = '" . $this->db->escape_like_str($searchterm) . "'  or sender_number = '" . $this->db->escape_like_str($searchterm) . "' LIMIT " .$limit . ",20";
    
	}else{
	  $sql = "SELECT * FROM sms_in
            WHERE SUBSTRING(code,-4 )  = '" . $this->db->escape_like_str($searchterm) . "' or SUBSTRING(sender_number,-4 ) = '" . $this->db->escape_like_str($searchterm) . "' LIMIT " .$limit . ",20";
    	
	}
  
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}








	public function search_record_count($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM people WHERE zip LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or phone LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or gender LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%' or 	lastname	 LIKE '%" . $searchterm . "%' or country LIKE '%" . $searchterm . "%' or 	email LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or company LIKE '%" . $searchterm . "%' or website LIKE '%" . $searchterm . "%' or userid LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%' or town  LIKE '%" . $searchterm . "%'  or city  LIKE '%" . $searchterm . "%' or address_1  LIKE '%" . $searchterm . "%'  or computerno  LIKE '%" . $searchterm . "%' or account  LIKE '%" . $searchterm . "%'  or status  LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
		public function searchallamembers($searchterm,$limit)
{
    $sql = "SELECT * FROM people
            WHERE zip LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or phone LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or gender LIKE '%" . $searchterm . "%'  or firstname LIKE '%" . $searchterm . "%'   or 	lastname	 LIKE '%" . $searchterm . "%'   or country LIKE '%" . $searchterm . "%' or 	email LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or company LIKE '%" . $searchterm . "%' or website LIKE '%" . $searchterm . "%' or userid LIKE '%" . $searchterm . "%'or createdate LIKE '%" . $searchterm . "%'  or town  LIKE '%" . $searchterm . "%'   or city  LIKE '%" . $searchterm . "%' or address_1  LIKE '%" . $searchterm . "%' or computerno  LIKE '%" . $searchterm . "%'  or account  LIKE '%" . $searchterm . "%' or status  LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
		public function searchallusers($searchterm,$limit)
{
   
   $sql = "SELECT * FROM people WHERE status LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and category='user' or phone LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and category='user' or gender LIKE '%" . $searchterm . "%' and category='user'  or firstname LIKE '%" . $searchterm . "%' and category='user'  or 	surname	 LIKE '%" . $searchterm . "%' and category='user'  or country LIKE '%" . $searchterm . "%' and category='user' or 	email LIKE '%" . $searchterm . "%' and category='user' or idno LIKE '%" . $searchterm . "%' and category='user' or usertype LIKE '%" . $searchterm . "%' and category='user' or code LIKE '%" . $searchterm . "%' and category='user' or id LIKE '%" . $searchterm . "%' and category='user' or createddate LIKE '%" . $searchterm . "%' and category='user'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}




	public function searchallplots($searchterm,$companyid,$limit)
{
    $sql = "SELECT * FROM plot WHERE name LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and  companyid='$companyid'  or id LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and  companyid='$companyid'  or  location LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and  companyid='$companyid' or description  LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and  companyid='$companyid'  LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}


public function searchvisitorHistory($searchterm,$limit)
{
    $sql = "SELECT * FROM checkin WHERE idno LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or name LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or business LIKE '%" . $searchterm . "%'  or phone LIKE '%" . $searchterm . "%'   or 	category	 LIKE '%" . $searchterm . "%'   or vehicleno LIKE '%" . $searchterm . "%' or 	location LIKE '%" . $searchterm . "%' or phone LIKE '%" . $searchterm . "%' or email LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%' or subject LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",300";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}




public function searchvisitor($searchterm,$limit)
{
    $sql = "SELECT * FROM gatepass WHERE idno LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or name LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or business LIKE '%" . $searchterm . "%'  or phone LIKE '%" . $searchterm . "%'   or 	category	 LIKE '%" . $searchterm . "%'   or vehicleno LIKE '%" . $searchterm . "%' or 	location LIKE '%" . $searchterm . "%' or phone LIKE '%" . $searchterm . "%' or email LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%' or subject LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",300";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}





function confirmvisitor($cardnumber)

{

   $this -> db -> select('*');

   $this -> db -> from('gatepass');

 	$this->db->where("(idno='".$cardnumber."' OR  phone='".$cardnumber."'   OR  vehicleno='".$cardnumber."'  OR  subject='".$cardnumber."' )", NULL, FALSE);


   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result_array();

   }

   else

   {

     return false;

   }

}









public function confirmvisitor22($searchterm,$limit)
{
    $sql = "SELECT * FROM gatepass WHERE idno LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or name LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or business LIKE '%" . $searchterm . "%'  or phone LIKE '%" . $searchterm . "%'   or 	category	 LIKE '%" . $searchterm . "%'   or vehicleno LIKE '%" . $searchterm . "%' or 	location LIKE '%" . $searchterm . "%' or phone LIKE '%" . $searchterm . "%' or email LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%' or subject LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",300";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

   if($q -> num_rows() >0)

   {
  return $q->result_array();

   }

   else

   {

     return false;

   }
}





public function searchallcalls($searchterm,$limit)
{
    $sql = "SELECT * FROM callcenter WHERE subject LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or createdate LIKE '%" . $searchterm . "%'  or status LIKE '%" . $searchterm . "%'   or 	category	 LIKE '%" . $searchterm . "%'   or callername LIKE '%" . $searchterm . "%' or 	receiver LIKE '%" . $searchterm . "%' or phone LIKE '%" . $searchterm . "%' or media LIKE '%" . $searchterm . "%' or calldate LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",300";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}






public function searchallactivities($searchterm,$limit)
{
    $sql = "SELECT * FROM activityloggs WHERE ip LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or actiondone LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or timedone LIKE '%" . $searchterm . "%'  or userid = '" . $searchterm . "'  LIMIT " .$limit . ",20";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}



public function searchuserloggs($searchterm,$limit)
{
    $sql = "SELECT * FROM loggs WHERE ip LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or timein LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or timeout LIKE '%" . $searchterm . "%'  or userid = '" . $searchterm . "'  LIMIT " .$limit . ",20";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}






public function searchdocuemnts($searchterm,$limit)
{
    $sql = "SELECT * FROM documents WHERE documentname LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or idno LIKE '%" . $searchterm . "%'  or dateposted LIKE '%" . $searchterm . "%'   or 	category	 LIKE '%" . $searchterm . "%'   or status LIKE '%" . $searchterm . "%' or 	entrydate LIKE '%" . $searchterm . "%'  LIMIT " .$limit . ",20";
    //
		 $this->db->order_by('id','desc');

	$q = $this->db->query($sql);

    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}




	public function searchallserials($keyword,$branch,$status,$limit,$companyid,$supplier,$type)
{
	
				    $this->db->select('*')->from('serials'); 
 
	if($keyword != "all"){

   		
	
	//	$this->db->where("(code ='$keyword' or name ='$keyword'  or serial ='$keyword'  )", NULL, FALSE);
			
		$this->db->like('serial',$keyword,'both'); 
		 $this->db->or_like('code',$keyword,'both');
		  $this->db->or_like('invoice',$keyword,'both');
		 	 $this->db->or_like('name',$keyword,'both');	
			 //$this->db->or_like('status',$keyword,'both');	

			}

				if($supplier !="all"){
					

	$this->db->where('description',$supplier);
  
			}

		if($status !="all"){

	$this->db->where('status',$status);
  
			}

		
			if($branch !="all"){
   $this->db->where('branch',$branch);
		
}










$query1 = $this -> db -> get();
 
    if($query1->num_rows() > 0)
    {
		
		
		
 
   if($type=="download"){
				
		  $new_date=date("Y-m-d H:i");		

		  $delimiter = ",";

        $newline = "\r\n";

        $filename = 	$new_date."Serials.csv";
		
		
		
      // $result = $this->db->query($query);

        $data = $this->dbutil->csv_from_result($query1, $delimiter, $newline);

        force_download($filename, $data);

		
	}else{
	
	
	
		
		
		
		$records = $query1->num_rows();
        foreach($query1->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);

}
   
    }
    else
    {
        return false;
    }
}



public function searchallquotation($searchterm,$limit,$companyid)
{
    $sql = "SELECT * FROM quotation WHERE id = '" . $this->db->escape_like_str($searchterm) . "'or supplier LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or shippingto LIKE '%" . $searchterm . "%'  or orderdate LIKE '%" . $searchterm . "%'   or 	terms	 LIKE '%" . $searchterm . "%'   or status LIKE '%" . $searchterm . "%' or 	amount LIKE '%" . $searchterm . "%' or ordernumber LIKE '%" . $searchterm . "%' or createdate LIKE '%" . $searchterm . "%' 	LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}















public function searchallevents($searchterm,$limit,$companyid)
{
    $sql = "SELECT * FROM events WHERE status LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or location LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or title LIKE '%" . $searchterm . "%'  or fromdate LIKE '%" . $searchterm . "%'   or 	todate	 LIKE '%" . $searchterm . "%'   or host LIKE '%" . $searchterm . "%' or 	participants LIKE '%" . $searchterm . "%' or comments LIKE '%" . $searchterm . "%' or repeated LIKE '%" . $searchterm . "%'  or userid LIKE '%" . $searchterm . "%'or dateposted LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}


public function searchtimer($searchterm,$limit,$companyid)
{
    $sql = "SELECT * FROM timer WHERE 	id LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or enddate LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or amount LIKE '%" . $searchterm . "%'  or startdate LIKE '%" . $searchterm . "%'   or 	station	 LIKE '%" . $searchterm . "%'   or description LIKE '%" . $searchterm . "%' or 	product LIKE '%" . $searchterm . "%' or customer LIKE '%" . $searchterm . "%' or invoice LIKE '%" . $searchterm . "%'  or duration LIKE '%" . $searchterm . "%'or status LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
		$records = $q->num_rows();
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
       	   return array(
    'data' => $data,
    'records' => $records,
);
    }
    else
    {
        return false;
    }
}

	public function search_dealscount($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM deals WHERE owner LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or title LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or contact LIKE '%" . $searchterm . "%' or contactname LIKE '%" . $searchterm . "%' or 	allsteps	 LIKE '%" . $searchterm . "%' or comments LIKE '%" . $searchterm . "%' or 	dealowner LIKE '%" . $searchterm . "%' or probability LIKE '%" . $searchterm . "%' or fromdate LIKE '%" . $searchterm . "%' or stage LIKE '%" . $searchterm . "%' or userid LIKE '%" . $searchterm . "%' or status LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
		public function searchallmydeals($searchterm,$limit)
{
    $sql = "SELECT * FROM deals
            WHERE owner LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or title LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or contact LIKE '%" . $searchterm . "%'  or contactname LIKE '%" . $searchterm . "%'   or 	allsteps	 LIKE '%" . $searchterm . "%'   or comments LIKE '%" . $searchterm . "%' or 	dealowner LIKE '%" . $searchterm . "%' or probability LIKE '%" . $searchterm . "%' or fromdate LIKE '%" . $searchterm . "%' or stage LIKE '%" . $searchterm . "%' or userid LIKE '%" . $searchterm . "%'or status LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}





public function search_expensecount($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE  invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or user LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or contact LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or 	amount	 LIKE '%" . $searchterm . "%' or datespent LIKE '%" . $searchterm . "%'  or status LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
	
	
		public function searchallmyexpenses($searchterm,$limit)
{
	

    $sql = "SELECT * FROM expenses  WHERE id LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or user LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or contact LIKE '%" . $searchterm . "%'  or idno LIKE '%" . $searchterm . "%'   or 	amount	 LIKE '%" . $searchterm . "%'   or datespent LIKE '%" . $searchterm . "%' or status LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}









	
		public function searchallmyitemexpenses($searchterm,$limit)
{
	

    $sql = "SELECT * FROM expenses  WHERE id LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or user LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or contact LIKE '%" . $searchterm . "%'  or idno LIKE '%" . $searchterm . "%'   or 	amount	 LIKE '%" . $searchterm . "%'   or datespent LIKE '%" . $searchterm . "%' or status LIKE '%" . $searchterm . "%'   LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}












		public function searchallmysalesorder($searchterm,$limit)
{
    $sql = "SELECT * ,purchaseorder.id  AS id,purchaseorder.status  AS status,purchaseorder.fromdate  AS fromdate,purchaseorder.paiddate  AS paiddate, purchaseorder.paid  AS paid FROM purchaseorder INNER JOIN people on purchaseorder.supplier=people.id WHERE purchaseorder.id LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or purchaseorder.transactionstatus  LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or  invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or ordernumber LIKE '%" . $searchterm . "%'  or 	orderedby LIKE '%" . $searchterm . "%'   or 	amount	 LIKE '%" . $searchterm . "%'   or 	purchaseorder.status LIKE '%" . $searchterm . "%'or 	pin LIKE '%" . $searchterm . "%'  or orderdate LIKE '%" . $searchterm . "%'  or terms LIKE '%" . $searchterm . "%'  or paiddate LIKE '%" . $searchterm . "%' 	or people.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or lastname LIKE '%" . $searchterm . "%'  or email LIKE '%" . $searchterm . "%'  or people.company LIKE '%" . $searchterm . "%' order by purchaseorder.id  DESC LIMIT " .$limit . ",50";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}






public function searchpurchase($searchterm,$limit)
{
  

$this->db->cache_on(); 
	 $this->db->select(' *');
	 

			$this->db->limit($limit, "50");
				

$this->db->where("description", $searchterm);
		
				 $this->db->order_by('id','DESC');


        $q = $this->db->get("purchaseorder");









   if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}














		public function searchallmyinvoices($searchterm,$limit)
{
	//$this->db->select("  * ,sales.id  AS id ");

    $sql = "SELECT * ,sales.id  AS id,sales.userid  AS userid,people.userid  AS myuserid,   sales.balance  AS balance, people.balance  AS mybalance  FROM sales INNER JOIN people on sales.customer=people.id WHERE sales.id LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or mytable LIKE '%" . $searchterm . "%'  or 	due_date LIKE '%" . $searchterm . "%'   or 	amount	 LIKE '%" . $searchterm . "%'   or 	type LIKE '%" . $searchterm . "%' or dateposted LIKE '%" . $searchterm . "%'  or cashier LIKE '%" . $searchterm . "%'  or name LIKE '%" . $searchterm . "%' 	or people.idno LIKE '%" . $searchterm . "%' or  people.phone LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or lastname LIKE '%" . $searchterm . "%'  or email LIKE '%" . $searchterm . "%'  or people.company LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",1000";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
		
		
		
		$sql = "SELECT * FROM sales  WHERE id LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or invoice LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or mytable LIKE '%" . $searchterm . "%'  or name LIKE '%" . $searchterm . "%'or cashier LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
		
		
    }
}







	

		public function searchserials($searchterm,$limit,$companyid,$searchtype)
{
	if($searchtype=="all"){
		 $sql = "SELECT * FROM delivery  WHERE orderno LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or serial LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or status LIKE '%" . $this->db->escape_like_str($searchterm ). "%'  or name LIKE '%" . $this->db->escape_like_str($searchterm ). "%' LIMIT " .$limit . ",20";

		
	}else{
 $sql = "SELECT * FROM delivery  WHERE orderno LIKE '" . $this->db->escape_like_str($searchterm) . "' or serial LIKE '" . $this->db->escape_like_str($searchterm) . "'  or status LIKE '" . $this->db->escape_like_str($searchterm ). "'  or name LIKE '" .$this->db->escape_like_str( $searchterm) . "' or code LIKE '" .$this->db->escape_like_str( $searchterm) . "' LIMIT " .$limit . ",20";

		
	}
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}




		

		public function searchserialsdamageditems($searchterm,$limit,$companyid,$searchtype)
{
	if($searchtype=="all"){
		 $sql = "SELECT * FROM damages  WHERE product LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or user LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or datedamaged LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or incharge LIKE '%" . $searchterm . "%'  or details LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",20";

		
	}else{
 $sql = "SELECT * FROM damages  WHERE product LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or user LIKE '" . $this->db->escape_like_str($searchterm) . "' or datedamaged LIKE '" . $this->db->escape_like_str($searchterm) . "'  or incharge LIKE '" . $searchterm . "'  or details LIKE '" . $searchterm . "' or code LIKE '" . $searchterm . "' LIMIT " .$limit . ",20";

		
	}
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}

	public function searchforleaseditems($searchterm,$limit,$companyid,$searchtype)
{

 $sql = "SELECT * FROM lease  WHERE product LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or serial LIKE '" . $this->db->escape_like_str($searchterm) . "' or dateleased LIKE '" . $this->db->escape_like_str($searchterm) . "'  or incharge LIKE '" . $searchterm . "'  or details LIKE '" . $searchterm . "' or product LIKE '" . $searchterm . "' or amount LIKE '" . $searchterm . "' or duedate LIKE '" . $searchterm . "' or status LIKE '" . $searchterm . "'  or mode LIKE '" . $searchterm . "'   LIMIT " .$limit . ",20";

		
	
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}












		public function searchGL($searchterm,$limit )
{
	

    $sql = "SELECT * FROM journal WHERE id ='$searchterm'  or invoice='$searchterm' or description LIKE '%" . $searchterm . "%'  or entrydate LIKE '%" . $searchterm . "%'  LIMIT " .$limit . ",500"; 
	
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}












		public function searchJE($searchterm,$limit )
{
	//$this->db->select("  * ,sales.id  AS id ");

    $sql = "SELECT * ,debitcredit.id  AS id,debitcredit.description  AS description FROM debitcredit INNER JOIN glaccounts on debitcredit.account=glaccounts.number WHERE debitcredit.code LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or debitcredit.account LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or debitcredit.description LIKE '%" . $searchterm . "%'  or 	debitcredit.crateddate LIKE '%" . $searchterm . "%'   or 	debitcredit.refno	 LIKE '%" . $searchterm . "%'    LIMIT " .$limit . ",500"; 
	
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}






		public function searchbanks($searchterm,$limit)
{
	//$this->db->select("  * ,sales.id  AS id ");

    $sql = "SELECT * ,banks.id  AS id,banks.description  AS description FROM banks INNER JOIN glaccounts on banks.bankname=glaccounts.number WHERE banks.id LIKE '%" . $this->db->escape_like_str($searchterm) . "%'  or banks.amount LIKE '%" . $this->db->escape_like_str($searchterm) . "%' or glaccounts.description LIKE '%" . $searchterm . "%'  or 	incharge LIKE '%" . $searchterm . "%'   or 	paiddate	 LIKE '%" . $searchterm . "%'  or 	invoice LIKE '%" . $searchterm . "%'or glaccounts.accountname LIKE '%" . $searchterm . "%'  LIMIT " .$limit . ",20"; 
	
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}



public function viewbaks($limit, $bankname,$startdate,$enddate,$companyid)
{
	//$this->db->select("  * ,sales.id  AS id ");
if($bankname=="all"){
	    $sql = "SELECT * ,banks.id  AS id,banks.amount  AS description,banks.amount  AS amount FROM banks INNER JOIN glaccounts on banks.bankname=glaccounts.number WHERE banks.paiddate  BETWEEN '$startdate'  and '$enddate' and banks.companyid='$companyid'  LIMIT " .$limit . ",20"; 

}else{
		    $sql = "SELECT * ,banks.id  AS id,banks.description  AS description,banks.amount  AS amount FROM banks INNER JOIN glaccounts on banks.bankname=glaccounts.number WHERE banks.paiddate  BETWEEN '$startdate'  and '$enddate' and   banks.bankname='$bankname'  and   banks.companyid='$companyid'  LIMIT " .$limit . ",20"; 

	
	
}

	
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
$records = $q->num_rows();

        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
		
		   return array(
    'data' => $data,
    'records' => $records,
);
       // return $data;
    }
    else
    {
        return false;
    }
}


public function search_expenserequestcount($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM expenses WHERE user LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and type !='Expense' or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and type !='Expense' or contact LIKE '%" . $searchterm . "%' and type !='Expense' or idno LIKE '%" . $searchterm . "%' and type !='Expense' or 	amount	 LIKE '%" . $searchterm . "%' and type !='Expense' or datespent LIKE '%" . $searchterm . "%' and type !='Expense'  or status LIKE '%" . $searchterm . "%' and type !='Expense'";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
		public function searchallmyexpensesrequest($searchterm,$limit)
{
    $sql = "SELECT * FROM expenses
            WHERE user LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and type !='Expense' or description LIKE '%" . $this->db->escape_like_str($searchterm) . "%' and type !='Expense' or contact LIKE '%" . $searchterm . "%' and type !='Expense'  or idno LIKE '%" . $searchterm . "%' and type !='Expense'  or 	amount	 LIKE '%" . $searchterm . "%'  and type !='Expense' or datespent LIKE '%" . $searchterm . "%' and type !='Expense' or status LIKE '%" . $searchterm . "%'  and type !='Expense'  LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}


















function populatesuppliersbyid($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('productcategory');

 $this -> db -> where('companyid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}





function populatemyproductcategory($userid){
 if(($userid)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('productcategory');

 $this -> db -> where('companyid', $userid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}








function getcategory($id){
 if(($id)!=""){
	 


  $this -> db -> select('*');

   $this -> db -> from('category');

 $this -> db -> where('id', $id);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
 }else{
	    return false; 
 }
}


function populate($table){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

 
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populateuser(){
 

  $this -> db -> select('*');

   $this -> db -> from('people');
// 
 $this -> db -> where('status', 1);
 	$this->db->where("(category='user' OR  category='reseller'   )", NULL, FALSE);
 $this -> db -> where('dropdown <>', "no");
 $this->db->order_by('email','ASC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}




function populatealldata($table,$order_by,$ORDER){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function getloanbalance($companyid,$memberid){
 

  $this -> db -> select('type,amount,paid,loanpaid,loanbalance');

   $this -> db -> from('expenses');
   $this->db->where('type',"Advance");
  $this->db->where('supplier',$memberid);
	  $this->db->where('companyid',$companyid);
	 $this->db->select('paid, SUM(paid) AS paid', FALSE);
	 $this->db->select('amount, SUM(amount) AS amount', FALSE);
	 $this->db->select('loanpaid, SUM(loanpaid) AS loanpaid', FALSE);

		$this->db->where("(paid > loanpaid   )", NULL, FALSE);

$this->db->group_by('supplier');
$this->db->group_by('companyid');


 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}


















function gettotalbranchstock($companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('productlist');

	  $this->db->where('companyid',$companyid);
	 $this->db->select('qty, SUM(qty) AS qty', FALSE);

$this->db->group_by('category');
$this->db->group_by('code');


$this->db->order_by("category", "ASC");
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}







function viewproductvalue($tablename,$branchname,$orderamount){



if(($orderamount=="all")){
	$this->db->select(" productlist.id, code,productlist.name,productlist.description, qty,	buyingprice,	sellingprice, wholesaleprice	,reorderevel,category,supplier,branch FROM productlist INNER JOIN productcategory on  productlist.category=productcategory.id WHERE branch='$branchname' ");
}else{
	
		$this->db->select(" productlist.id, code,productlist.name,productlist.description, qty,	buyingprice,	sellingprice, wholesaleprice	,reorderevel,category,supplier,branch FROM productlist INNER JOIN productcategory on  productlist.category=productcategory.id WHERE branch='$branchname' and productlist.orderamount= '$orderamount'");

}
	//$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  ORDER BY products.category ASC ");


	 		 $this->db->order_by('productcategory.description','DESC');

	 $this->db->order_by('productlist.id','asc');

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}





function viewproductvalueall($tablename,$companyid,$orderamount){



if(($orderamount=="all")){
	$this->db->select(" productlist.id, code,productlist.name,productlist.description, qty,	buyingprice,	sellingprice, wholesaleprice	,reorderevel,category,supplier,branch FROM productlist INNER JOIN productcategory on  productlist.category=productcategory.id WHERE productlist.companyid='$companyid' ");
}else{
	
 $this->db->select(" productlist.id, code,productlist.name,productlist.description, qty,	buyingprice,	sellingprice, wholesaleprice	,reorderevel,category,supplier,branch FROM productlist INNER JOIN productcategory on  productlist.category=productcategory.id WHERE productlist.companyid='$companyid' and productlist.orderamount= '$orderamount' ");

}
	//$this->db->select(" * FROM stock INNER JOIN products on  stock.code= products.code WHERE stock.startdate ='$startdate' and stock.closedate ='$closedate'  ORDER BY products.category ASC ");


	 		 $this->db->order_by('productcategory.description','DESC');

	 $this->db->order_by('productlist.id','asc');

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}





function populatealldatawhere($table,$order_by,$ORDER,$where, $value){
 

  $this -> db -> select('*');
 $this->db->where($where,$value);

   $this -> db -> from($table);

 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function populatealldatawhereMaincategory($table,$order_by,$ORDER,$where, $value){
 

  $this -> db -> select('products.id,products.description,products.name,products.sellingprice,products.status');
 $this->db->where("products.status","active");

$this->db->where('productcategory.maincategory', $value);
	$this->db->join('productcategory', 'products.category = productcategory.id','outer JOIN');





   $this -> db -> from($table);

 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}









  function createRandomPassword() {

		  	$session_data = $this->session->userdata('logged_in');

			$data['id'] = $session_data['id'];
         $userid = $data['id'];
		   $data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];


	$chars =  $userid.time()."9563232303232023232023456789453242131231425325241231235325325632521321354612142545355324535523424353454645";
					  //$chars = '2342355ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-+';

	srand((double)microtime()*100000000);
	$i = 0;
	$pass = '' ;
	while ($i <= 4) {

		$num = rand() % 33;

		$tmp = substr($chars, $num, 1);

		$pass = $pass . $tmp;

		$i++;

	}
	return $pass;
}
 
function populatealldatawherestock($table,$order_by,$ORDER,$where, $value,$orderamount){
 

  $this -> db -> select('*');
 if($orderamount=="all"){
 $this->db->where($where,$value);

 }else{
	 	  $this->db->where("orderamount",$orderamount);

 $this->db->where($where,$value); 
 }

   $this -> db -> from($table);

 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatealldb2($table,$where, $value){
 
 
 $this->smsdb = $this->load->database('smsdb', true);
  $this -> smsdb-> select('*');

   $this -> smsdb -> from($table);

  $this->smsdb->where($where,$value);
 $this->smsdb->order_by('id','desc');
 $query1 = $this -> smsdb -> get();
 
 
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populateallrole($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populateall($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','DESC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populateallsales($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
 $this->db->where('status <>',"returned");

  $this->db->where($where,$value);
 $this->db->order_by('id','DESC');
 $query1 = $this -> db -> get();
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function populateallroomstobecleaned($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*,house.id as id');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
//$this->db->where("house.status","freezed");
	$this->db->where("( house.status='Blocked' OR house.status='Vacant Dirty' OR house.status='Occupied Dirty' OR house.status='freezed' OR house.status='Pending'   )", NULL, FALSE);

  	$this->db->join('house', 'asignrooms.roomid=house.id','INNER JOIN' );

 $this->db->order_by('house.id','DESC');
    //$this -> db -> limit(100);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}









function populatealldataarraysales($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','DESC');
    $this -> db -> limit(100);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


function some_model_function() {	
  $this->db->from('table');
  return $num_rows = $this->db->count_all_results();
}

function populatelatestidcounter($table,$where, $value)
{
	

	//$query = $this->db->query('SELECT * FROM my_table');
//echo $query->num_rows(); 
  $query = $this->db->query("SELECT * FROM ".$table." where '".$where."' = '$value' ");
  return $query->num_rows();
}



function populatelatestidcounter1($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);

 $this->db->order_by('id','DESC');
    //$this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {

 	return $this->query1->num_rows();

   }

   else

   {

     return false;

   }
}


function populatelatestid($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);

 $this->db->order_by('id','DESC');
    $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}

function populatealldataarray($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}

function populatealldataarrayboolean($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
     return true;

   }

   else

   {

     return false;

   }
}


function populatealldatanoarraySales($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
	//	$this->db->where("(branch=".$branchid." and status='active' OR 	branch!=".$branchid." and status='global' )", NULL, FALSE);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','ASC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



function populatealldatanoarray($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
	//	$this->db->where("(branch=".$branchid." and status='active' OR 	branch!=".$branchid." and status='global' )", NULL, FALSE);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}







function populateallBRANCHSTOC($table,$where, $value,$where1, $branchid){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
		$this->db->where("(type = ".$branchid."  OR 	stockto = ".$branchid."     )", NULL, FALSE);

  $this->db->where($where,$value);
  //$this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}





function populatealldatanoarraywithaccounting($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

function populatealldatanoarrayorderbydata($table,$where, $value,$where1, $value1,$order_by,$ORDER){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by($order_by,$ORDER);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatanoarrayorderby($table,$order_by,$ORDER,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}





function populatealldaTENANTS($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
			$this->db->where("(category='accommodation' OR 	category ='accomodation' )", NULL, FALSE);

  //$this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

function populatealldatacategories($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
if($value!="all"){
	  $this->db->where($where,$value);

	
}
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatanoarraythreetask($table,$where, $value,$where1, $value1,$where2, $value2,$period){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
			if($period !="all") {
		$this->db->where('period', $period);
			
				}
	

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatanoarraySEVENReadings($table,$where, $value,$where1, $value1,$where2, $value2,$readingtype,$roomno,$where3, $value3,$where4, $value4){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	    $this->db->where($where3,$value3);
		 $this->db->where($where4,$value4);
	if($readingtype=="single"){
		$this->db->where('houseno', $roomno);

}
	 $this->db->order_by('id','DESC');

	
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatanoarrayFIVEReadings($table,$where, $value,$where1, $value1,$where2, $value2,$readingtype,$roomno){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
	if($readingtype =="single"){
		$this->db->where('houseno', $roomno);

}
		$this->db->where('status', "occupied");
	 $this->db->order_by('id','DESC');

	
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}


function populatealldatanoarraythree($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatanoarraythreeDISPATCH($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
  $this->db->where("(qty <>total  )", NULL, false);

    //$this->db->where($where2,$value2);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



function populatealldatanoarraythreechild($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
  	if($value2 !="all"){
    $this->db->where($where2,$value2);
	}
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}
function populatealldatanoarraythreeAsc($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  	  	//$this->db->where("(end ='$value2'  OR invoice_number ='$value2'  )", NULL, FALSE);
	//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);

  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
 $this->db->order_by('id','asc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}
function populatealldatanoarraythreeForPurchases($table,$where, $value,$where1, $value1,$where2, $value2){
 
// 
  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    //$this->db->where($where2,$value2);
	  	$this->db->where("(invoice ='$value2'  OR invoice_number ='$value2'  )", NULL, FALSE);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}


function populatealldatanoarraythreesales($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
 $this->db->order_by('id','desc');
    $this -> db -> limit(200);
 $query1 = $this -> db -> get();
 
 
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}


function populatealldatanoarrayfourconfirmProducts($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3,$where4,$where5){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	if($value3!="all"){
	    $this->db->where($where3,$value3);
	}
if($value3!="all"){
	    $this->db->where($where3,$value3);
	}
	
	
	if($where4!="all"){
	    $this->db->where("producttype",$where4);
	}
	if($where5!="all"){
	    $this->db->where("category",$where5);
	}
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



































function populatealldatanoarrayfourconfirm($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	if($value3!="all"){
	    $this->db->where($where3,$value3);
	}

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}


function populatethreenoarray($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}

function populatealldatarraythreebydate($table,$where, $value, $where1, $value1, $column, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,total, SUM(total) AS total ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
     $this->db->where('total <>',0);
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('date >=', $startdate);
		$this->db->where('date <=',$enddate);
  $this->db->where('itemstatus <>',"returned");
    $this->db->where('itemstatus<>',"sold");
//
$this->db->group_by('item_id');
	$this->db->group_by('type');
	 $this->db->order_by('id','DESC');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


function populatealldatarraythreebydateCompanyall($table,$where, $value, $where1, $value1, $column, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,total, SUM(total) AS total ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
     $this->db->where('total <>',0);
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('date >=', $startdate);
		$this->db->where('date <=',$enddate);
  $this->db->where('itemstatus <>',"returned");
//
$this->db->group_by('item_id');
	$this->db->group_by('type');
	 $this->db->order_by('id','DESC');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


function populatealldatarraythreebydateCompany($table,$where, $value, $where1, $value1, $column, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,total, SUM(total) AS total ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  

  $this->db->where($where1,$value1);
     $this->db->where('total <>',0);
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('date >=', $startdate);
		$this->db->where('date <=',$enddate);
  $this->db->where('itemstatus <>',"returned");
//
$this->db->group_by('item_id');
	$this->db->group_by('companyid');
	// $this->db->order_by('id','DESC');

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}

function populatealldatarrayfourbydate($table,$where, $value, $where1, $value1, $column, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,total, SUM(total) AS total ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
     $this->db->where('total >',0);
	    $this->db->where('stockto <>',"Automatic Production adjustment");
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('date >=', $startdate);
		$this->db->where('date <=',$enddate);
$this->db->group_by('item_id');
	$this->db->group_by('type');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}



function populatealllabourbydate($table,$where, $companyid, $where1, $branchid, $month, $startdate, $enddate){
 


	$this->db->select("  * ,qty, SUM(qty) AS myqty,people.id as myid,labour.id as id,labour.code as mycode ");

   $this -> db -> from($table);

  $this->db->where($where,$companyid);
  if($branchid!="all"){
	    $this->db->where($where1,$branchid);

  }
  $this->db->where("month",$month);
    // $this->db->where('total >',0);
	$this->db->where('exactdate >=', $startdate);
		$this->db->where('exactdate <=',$enddate);
  
  

		$this->db->join('people', 'labour.employeeid = people.idno','INNER JOIN');

    
$this->db->group_by('labour.code');
	$this->db->group_by('labour.employeeid');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}






function populatealltrialbalance($table,$where, $value, $where1, $value1, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,debit, SUM(debit) AS debit, credit, SUM(credit) AS credit, ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
  
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('crateddate >=', $startdate);
	//$this->db->group_by('branch');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}








function populateallexpensebyno($table,$sum, $where,$value){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,".$sum.", SUM(".$sum.") AS ".$sum." ");

   $this -> db -> from($table);

  $this->db->where($where,$value);

  
	
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}




function populateonefiscalyear($companyid, $startdate){
 

   $this -> db -> from('fiscalyear');

  $this->db->where('companyid',$companyid);

  
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('startdate <= ', $startdate);
		$this->db->where('enddate >=',$startdate);

	 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}






function populateoneGLfiscalyear($companyid, $startdate,$number){
 

   $this -> db -> from('glfiscalyear');

  $this->db->where('companyid',$companyid);
  $this->db->where('number',$number);

  
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('startdate <= ', $startdate);
		$this->db->where('enddate >=',$startdate);

	 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}






function populatecustomersupplierGLfiscalyear($companyid, $startdate,$number){
 

   $this -> db -> from('customerfiscalyear');

  $this->db->where('companyid',$companyid);
  $this->db->where('number',$number);

  
		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('startdate <= ', $startdate);
		$this->db->where('enddate >=',$startdate);

	 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}













function populatedeductions($database,$companyid, $amount){
 

   $this -> db -> from($database);

  $this->db->where('companyid',$companyid);

  
	//$this->db->where('start >=', $amount);
		//$this->db->where('end =< ', $amount);
$this->db->where("(   '$amount'  BETWEEN start  and end )", NULL, FALSE);

	 $this->db->order_by('id','DESC');
   $this -> db -> limit(1);

 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}




function populatealldatarraythreebydatesolditems($table,$where, $value, $where1, $value1, $column, $startdate, $enddate){
 

  //$this -> db -> select('*');
	$this->db->select("  * ,qty, SUM(qty) AS qty ");

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
  $this->db->where('status <>',"returned");

		//$this->db->where("(   date  BETWEEN '$startdate'  and '$enddate'  )", NULL, FALSE);
	$this->db->where('dateposted >=', $startdate);
		$this->db->where('dateposted <=',$enddate);
$this->db->group_by('code');
	$this->db->group_by('branch');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}



function populatealldatanorraythree($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


function populatealldatawhererooms($table,$order_by,$ORDER,$where, $value){
 

  $this -> db -> select('*');
 $this->db->where($where,$value);

   $this -> db -> from($table);
$this -> db -> limit(200);

 $this->db->order_by($order_by,$ORDER);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function populatealldatarraythreeWater($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);

  $this->db->order_by('opening','asc');
  	
 $this->db->order_by('meternumber','asc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

function populatealldatarraythree($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}


function populatealldatarrayfour($table,$where,$value,$where1,$value1,$where2,$value2,$where3,$value3){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
$this->db->where($where3,$value3);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatarraythreestocktaking($table,$where, $value,$where1, $value1,$where2, $value2){
 
    $this -> db -> limit(2);

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function populatealldatarraythreeclosingbalance($table,$where, $value,$where1, $value1,$where2, $value2){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
 $this->db->order_by('id','ASC');
 $query1 = $this -> db -> get();
 
    $this -> db -> limit(2);
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}











function populatealldatanoarrayfourMissing($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  //$this->db->where($where1,$value1);
  	$this->db->where("(status ='new'  OR status ='Missing'  )", NULL, FALSE);

    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

function populatealldatanoarrayfiveEtims($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3 ,$where4, $value4){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
	   //
	   	 //  $this->db->where($where4,$value4);
		$this->db->where("(etimsstatus='2'  OR 	etimsstatus='3' )", NULL, FALSE);
  $this->db->order_by('id','desc');

 $query1 = $this -> db -> get();

    $this -> db -> limit(200);

 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



	public function search_sms_count($searchterm,$userid)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM sms WHERE message LIKE '%" . $this->db->escape_like_str($searchterm) . "%' AND userid='$userid'   or postedtime LIKE '%" . $this->db->escape_like_str($searchterm) . "%' AND userid='$userid' or contact LIKE '%" . $searchterm . "%' AND userid='$userid' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
		
		public function searchallasms($searchterm,$limit,$userid)
{
    $sql = "SELECT * FROM sms
            WHERE message LIKE '%" . $this->db->escape_like_str($searchterm) . "%' AND userid='$userid' or postedtime LIKE '%" . $this->db->escape_like_str($searchterm) . "%' AND userid='$userid' or contact LIKE '%" . $searchterm . "%'  AND userid='$userid'  LIMIT " .$limit . ",20";
    
	$q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}

	
	
	
	

function populatealldatanoarrayfiveLabour($code,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3 ,$where4, $value4){

		$this->db->from('employeecharges');


		$this->db->select("  * ,employeecharges.id  as peopleid ");
		$this->db->where('employeecharges.code', $code);
	

		$this->db->where('people.phone <>',"");

		$this->db->join('people', 'employeecharges.memberid = people.id','INNER JOIN');
		
		
	
	 

		$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

 return $bioquery->result();
 
}else{

  return false;
}
   }
	



	








function populatealldatanoarrayfiveLabour22($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3 ,$where4, $value4){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
	   	   $this->db->where($where4,$value4);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



function populatealldatanoarrayfive($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3 ,$where4, $value4){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
	   	   $this->db->where($where4,$value4);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}



function populatealldatanoarraysix($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3 ,$where4, $value4,$where5, $value5){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
	   	   $this->db->where($where4,$value4);
	   	   $this->db->where($where5,$value5);

 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

function populatealldatanoarrayfour($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);
//
  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}

















function CloseStockOpening($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
	if($value3 !="all"){
		$this->db->join('products', 'stock.code = products.code','outer JOIN');


		
	$this->db->where('productcategory.maincategory', $value3);
	$this->db->join('productcategory', 'products.category = productcategory.id','outer JOIN');

	}

	
 $this->db->order_by('stock.id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}




function MainstoreStockOpening($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
	if($value3 !="all"){
	$this->db->where('productcategory.maincategory', $value3);
	$this->db->join('productcategory', 'products.category = productcategory.id','outer JOIN');

	}

	
 $this->db->order_by('products.id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}






function BranchStockOpening($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	
	if($value3 !="all"){
	$this->db->where('productcategory.maincategory', $value3);
	$this->db->join('productcategory', 'productlist.category = productcategory.id','outer JOIN');

	}

	
 $this->db->order_by('productlist.id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}












function populatealldataarrayfour($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}











function populatealldataarrayOpenstock($table,$where, $value,$where1, $value1,$where2, $value2,$where3, $value3){
 

 
 

  $this -> db -> select('id,open');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  $this->db->where($where1,$value1);
    $this->db->where($where2,$value2);
	   $this->db->where($where3,$value3);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }
}


function populateallproductlist($table,$where, $value,$branchid){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
  //$this->db->where($where1,$value1);
		$this->db->where("(branch=".$branchid." and status='active' OR 	branch!=".$branchid." and status='global' )", NULL, FALSE);

 $this->db->order_by('name','ASC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }
}
function populateallarraytwo($table,$where, $value,$where1, $value1){
 

  $this -> db -> select('*');

   $this -> db -> from($table);
 $this->db->where($where,$value);
  $this->db->where($where1,$value1);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}
function populateallarray($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}

function populateallarraysales($table,$where, $value){
 

  $this -> db -> select('id,entrydate,checkoutdate');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
     //$this -> db -> limit(20);

 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function populateallarrayproducts($table,$where, $value){
 

  $this -> db -> select('id,assetaccount,salesaccount,expenseaccount,producttype');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}



function populateallarraylease($table,$where, $value){
 

  $this -> db -> select('*');

   $this -> db -> from($table);

  $this->db->where($where,$value);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
    $this -> db -> limit(1);
 if($query1 -> num_rows() >0)

   {
  return $query1->result_array();

   }

   else

   {

     return false;

   }

}




function populatesupplier(){
 

  $this -> db -> select('*');

   $this -> db -> from('people');

 //$this -> db -> where('category', 'supplier');
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatuserpermissions($moduleid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissions');
 $this -> db -> where('moduleid', $moduleid);

 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;
  //return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatuserpermissionsroles($moduleid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('rolepermissions');
 $this -> db -> where('moduleid', $moduleid);

 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;
  //return $query1->result();

   }

   else

   {

     return false;

   }

}
function populatuserpermissionsbyaction($moduleid,$actionid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissionsactions');
 $this -> db -> where('moduleid', $moduleid);
 $this -> db -> where('actionid', $actionid);
 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;
  //return $query1->result();

   }

   else

   {

     return false;

   }

}




function populatumypermissionsdata($userid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissions');

 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 $this->db->order_by('sort','ASC');
 if($query1 -> num_rows() >0)

   {
	     //return true;
  return $query1->result();

   }

   else

   {

     return false;

   }

}






function populatuserpermissionsdata($moduleid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissionsactions');
 $this -> db -> where('moduleid', $moduleid);

 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     //return true;
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function populatuserpermissionsdataarray($moduleid,$actionid,$userid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissionsactions');
 $this -> db -> where('moduleid', $moduleid);
$this -> db -> where('actionid', $actionid);
 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;


   }

   else

   {

     return false;

   }

}


function populatuserpermissionsactionsroles($moduleid,$userid,$actionid){
 

  $this -> db -> select('*');

   $this -> db -> from('rolepermissionsactions');
 $this -> db -> where('moduleid', $moduleid);
$this -> db -> where('actionid', $actionid);
 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;
  //return $query1->result();

   }

   else

   {

     return false;

   }

}





function populatuserpermissionsactions($moduleid,$userid,$actionid){
 

  $this -> db -> select('*');

   $this -> db -> from('permissionsactions');
 $this -> db -> where('moduleid', $moduleid);
$this -> db -> where('actionid', $actionid);
 $this -> db -> where('userid', $userid);
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
	     return true;
  //return $query1->result();

   }

   else

   {

     return false;

   }

}











function populatemysupplier($companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('people');
 $this -> db -> where('category', 'supplier');

 $this -> db -> where('companyid', $companyid);
 $this->db->order_by('company','ASC');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatemypeople($companyid){
 

  $this -> db -> select('*');

   $this -> db -> from('people');

 $this -> db -> where('companyid', $companyid);
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}
function populateviewmember($userid){
 

  $this -> db -> select('*');

   $this -> db -> from('people');
 $this -> db -> where('userid', $userid);
 $this -> db -> where('category', 'member');
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}





function populateviewemployees(){
 

  $this -> db -> select('*');

   $this -> db -> from('people');

 $this -> db -> where('category', 'employee');
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}



function populateviewsetting(){
 

  $this -> db -> select('*');

   $this -> db -> from('setting');

 //$this -> db -> where('category', 'employee');
// $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populateviewloyality(){
 

  $this -> db -> select('*');

   $this -> db -> from('loyalitycards');

 //$this -> db -> where('category', 'employee');
 $this->db->order_by('id','desc');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


function populatemymessage($courseid){
 

  $this -> db -> select('*');

   $this -> db -> from('coursemessage');

 $this -> db -> where('courseid', $courseid);
   $this->db->order_by('id');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}


}
?>