<?php
include('config.php');
$search=$_POST['search']['value'];
$length=$_POST['length'];
$from_date=$_POST['from_date'];
$to_date=$_POST['to_date'];
$status="completed";
if($search==""){
 if($from_date==""){
       $sql = "SELECT count(*)as count from vendor_invoice_details where status='".$status."'   order by invoice_date desc";
 
    }else{
        $sql = "SELECT count(*)as count from vendor_invoice_details where status='".$status."' and invoice_date>= '".$from_date."' and invoice_date<= '".$to_date."'  order by invoice_date desc";

    }$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
$sql = "SELECT * from vendor_invoice_details where status='".$status."'  order by invoice_date desc limit $start,$length";
 }else{
     $sql = "SELECT * from vendor_invoice_details where status='".$status."' and invoice_date>= '".$from_date."' and invoice_date<= '".$to_date."'  order by invoice_date desc limit $start,$length";

 }//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count,
            "sql"=>$sql
        )
        
        );
}else{
    
  if($from_date==""){  
$sql = "SELECT count(*) as count from vendor_invoice_details where  status='".$status."' and (customer_name like '%".$search."%' or invoice_no like '%".$search."%' or msisdn like '%".$search."%') order by invoice_date desc";
  }else{
      $sql = "SELECT count(*) as count from vendor_invoice_details where  status='".$status."' and invoice_date>= '".$from_date."' and invoice_date<= '".$to_date."' and (customer_name like '%".$search."%' or invoice_no like '%".$search."%' or msisdn like '%".$search."%') order by invoice_date desc";

  }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
  if($from_date==""){  
$sql = "SELECT * from vendor_invoice_details  where status='".$status."'  and (customer_name like '%".$search."%' or invoice_no like '%".$search."%' or msisdn like '%".$search."%') order by invoice_date desc limit $start, $length";

  }else{
      $sql = "SELECT * from vendor_invoice_details  where status='".$status."' and  invoice_date>= '".$from_date."' and invoice_date<= '".$to_date."' and (customer_name like '%".$search."%' or invoice_no like '%".$search."%' or msisdn like '%".$search."%') order by invoice_date desc limit $start, $length";

  }


//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}
?>