<?php 
include("../include/config.php");
include("../include/functions.php"); 
if($_REQUEST['exportExcel']=='yes'){
$where='';
$where='';
if($_REQUEST['search_coupon_code']!=''){
$name=$_REQUEST['search_coupon_code'];
$where.=" and coupon_code='".$name."'";	
}
if($_REQUEST['search_from_date']!='' && $_REQUEST['search_to_date']!=''){
$where.=" and generate_date>='".$_REQUEST['search_from_date']."' and generate_date<='".$_REQUEST['search_to_date']."' ";
}
$order_by=' id ';
$order_by2=' desc ';  
$sql=$obj->Query("select * from $tbl_coupon where 1=1 $where order by $order_by $order_by2 ",$debug=-1);

$csvData='';
$csvData.="Coupon Code,Valid For, Particular User's Email ,Discount,Minimum Purchase,Valid Till,Number of Uses  ";
$csvData.="\n\r";
$reccnt=$obj->numRows($sql);

if($reccnt>0){
    $count=1;
    while($data=$obj->fetchNextObject($sql))
    {   
  	$csvData.=stripslashes($data->coupon_code).",";
	$csvData.=stripslashes($line->valid_for)." User(s),";
	$csvData.=stripslashes($data->to_email).",";
	$discount='';
	if($data->discount_type=='Direct') { $discount.= 'Rs.'; } $discount.=stripslashes($data->discount); ?> <?php if($data->discount_type=='Percent') { $discount.= '%'; }
	$csvData.=stripslashes($discount).",";
	$csvData.=$website_currency_symbol." ".$data->minimum_purchase.",";
	$expire_date='';
	if($data->expire_date!='0000-00-00'){$expire_date=date('d M Y',strtotime($data->expire_date));}
	$csvData.=stripslashes($expire_date).",";
	$ordArr=$obj->query("select count(*) as cs from $tbl_order  where coupon_code='".$data->coupon_code."'");
	$rsCoup=$obj->fetchNextObject($ordArr);
	$csvData.= $rsCoup->cs.",";
	$csvData.="\n\r";        
    $count++;    
    } }
				
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=Coupon-Report-".date("dMY_H:i").".csv");
echo $csvData;
exit;
header("location:".$_SERVER['HTTP_REFERER']);
exit();
}
?>