<?php 
include("../include/config.php");
include("../include/functions.php"); 
if($_REQUEST['exportExcel']=='yes'){
$where='';
if($_REQUEST['search_order_id']!=''){
$name=$_REQUEST['search_order_id'];
$where.=" and a.order_id='".$name."'";	
}

if($_REQUEST['search_user_id']!=''){
$name=$_REQUEST['search_user_id'];
$where.=" and a.user_id='".$name."'";	
}

if($_REQUEST['search_email']!=''){
$name=$_REQUEST['search_email']	;
$where.=" and c.email like '%$name%' ";	
}
if($_REQUEST['search_from_date']!='' && $_REQUEST['search_to_date']!=''){
$where.=" and a.subscription_date>='".$_REQUEST['search_from_date']."' and a.subscription_date<='".$_REQUEST['search_to_date']."' ";
}
if($_REQUEST['subscription_status'] || $_REQUEST['subscription_status']=='0'){
$name=$_REQUEST['subscription_status'];
$where.=" and a.subscription_status='".$name."'";		
}
$where.=" and f.order_status!=9 ";

$order_by='a.sub_id';
$order_by2=' desc ';  
$sql=$obj->Query("SELECT a.user_id,a.subscription_date,a.order_id,sub_id,a.subscription_status,a.reason_for_cancel,a.cancel_status,a.next_order_date,b.*,c.* FROM `tbl_quicklly_pass_subscription` as a left join tbl_quicklly_pass_plan as b on b.id=a.plan_id left join tbl_user as c on a.user_id=c.id left join tbl_quicklly_pass_subscription_order as f on f.id=a.order_id where 1=1 $where order by $order_by $order_by2",$debug=-1);

$csvData='';
$csvData.="Order Date,Order ID, Subscription Type, User ID ,Email, Subscription Status, Reson for Cancel";
$csvData.="\n\r";
$reccnt=$obj->numRows($sql);

if($reccnt>0){
    $count=1;
    while($data=$obj->fetchNextObject($sql))
    {   
 if($data->subscription_status==1 && $data->cancel_status==0){
	 $subscription_status="Active";
 } else {
	 $subscription_status="Cancel";
 }	 
	 
  	$csvData.=date('d/m/Y',strtotime($data->subscription_date)).",";
	$csvData.=$data->order_id.",";
	$csvData.=stripslashes($data->plan_name).",";
	$csvData.=stripslashes($data->user_id).",";
	$csvData.=stripslashes($data->email).",";
	$csvData.=stripslashes($subscription_status).",";
	$csvData.=stripslashes($data->reason_for_cancel).",";
	$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=Quicklly-Pass-Order-Report-".date("dMY_H:i").".csv");
echo $csvData;
exit;
header("location:".$_SERVER['HTTP_REFERER']);
exit();
}
?>