<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="report.csv"');
$data=array();
$data[]=array("customer name","invoice no","invoice date","Payment Date","Payment","Payment Mode");
$from_date=$_REQUEST['from_date'];
$to_date=$_REQUEST['to_date'];
$id=$_REQUEST['id'];
require_once "config.php";
if($from_date==""){
   $sql="select id.*,idh.balance as balance1,idh.payment_mode,idh.created_on  from invoice_details id join invoice_details_history idh where id.id=idh.customer_id and id.id='".$id."'";
 
}else{
$sql="select id.*,idh.balance as balance1,idh.payment_mode,idh.created_on from invoice_details id join invoice_details_history idh where id.invoice_date>='".$from_date."' and  id.invoice_date<='".$to_date."' and id.id=idh.customer_id and id.id='".$id."'";
//echo $sql;die();
}
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}
while($result=mysqli_fetch_array($query)){
   $data[]=array($result['customer_name'],$result['invoice_no'],$result['invoice_date'],$result['created_on'],$result['balance1'],$result['payment_mode']);
    
}
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
   // $val = explode(",", $line);
    fputcsv($fp, $line);
}
fclose($fp);
?>