<?php
ob_start();
error_reporting(0);
include("connect.php");
session_start();
$ver = $_SESSION['distributor_user_id'];
$user_role= $_SESSION['user_role'];  
if(!isset($_SESSION["distributor_user_id"])){
   header('location:https://tarkashcorp.appexperts.net/index.php');
}
/* vars for export */
// database record to be exported
$cdate1 = $_GET['cdate1'];
$cdate2 = $_GET['cdate2'];
$catid = $_GET['did'];
// filename for export
$csv_filename = 'RejectedOrderReport_'.date('Y-m-d').'.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$csv_filename);

$fp = fopen('php://output', 'w');
fputcsv($fp, array('Bill No.', 'Customer Name', 'Total Quantity','Total Amount','Transaction Date','Reject Date'));
if($catid!="")
{
	$res = mysqli_query($con,"select * from tbl_master_customer,tbl_transaction where tbl_transaction.customer_id=tbl_master_customer.customer_id and tbl_master_customer.distributor_id='$catid' && tbl_transaction.transaction_status='Reject' && tbl_transaction.cancel_date>='$cdate1' && tbl_transaction.cancel_date<='$cdate2'");
}
else
{
	$res = mysqli_query($con,"select * from tbl_master_customer,tbl_transaction where tbl_transaction.customer_id=tbl_master_customer.customer_id and tbl_master_customer.distributor_id='$catid' && tbl_transaction.transaction_status='Reject' && tbl_transaction.cancel_date>='$cdate1' && tbl_transaction.cancel_date<='$cdate2'");
}
if(mysqli_num_rows($res)>0)
{     
	while($fetch_product2=mysqli_fetch_assoc($res))
	{
		extract($fetch_product2);
		fputcsv($fp, array($customer_transaction_id,$customer_name,$total_quantity,$total_amount,$transaction_date,$cancel_date));
	}
}
exit;
?>