<?php
require_once("../include/config.php");
require_once("../include/startup.php");

$csv = (isset($_POST['csv_outputs'])?'1':'0');
	if ($csv=='1')
		{
			header("Content-type: application/octet-stream");
			header("Content-Disposition: attachment; filename=\"Invoice_reportqqq.csv\"");
			
			$paymentlist = str_replace('\\"','"',$_POST['csv_outputs']);
			echo $paymentlist;
			exit();
		}
		else
		{
			require_once("header1.html");
		}
if($G_memberid > 0)
{
	
	$companyid = intval($_REQUEST['txtcompanyid']);
	$memberinvoicesid = intval($_REQUEST['txtmemberinvoicesid']);
	$memberid = $G_memberid;
	$obj_client  = new Client();
	$obj  = new Company();
	$obj_proforma  = new Invoice_frproforma();
	$obj_Invoice  = new Invoice();
		


$montharray = $_POST['cmbmonth'];
				$montharray_count =  count($_POST['cmbmonth']);
				if($montharray_count > 0) 
				{
					$month_id = '(';
					for($i=0;$i<$montharray_count;$i++) 
					{
							$month =  explode('-',$montharray[$i]);
						if($i == 0) 
						{
								$month_id .= $month[0];
							$monthvar .= $month[1];
						}
						else 
						{
							$month_id .=",".$month[0];
							$monthvar .= ",".$month[1];
						}
								 
					}
					$month_id .= ')';
					
					$where_cond_month = "MONTH(a.invoicedate) in $month_id and";
				}		

	$display1 = $_POST['txtfromdate'];
	$display2 = $_POST['txttodate'];
	
	$fromdate	= date("Y-m-d", strtotime($_POST['txtfromdate']));
	$todate		= date("Y-m-d", strtotime($_POST['txttodate']));
		if ($fromdate=='1970-01-01')
	{
		$where_date ="";
	}
	else
	{
		$where_date ="date(a.invoicedate) BETWEEN '$fromdate' and '$todate'and";
	}
$day = $_POST['opt'];
$todayedate = $_POST['todayedate'];
$yesterdaydate = $_POST['yesterdaydate'];
$this_month = $_POST['this_month'];
$last_month = $_POST['last_month'];

$this_quarter = $_POST['this_quarter'];
	$this_quarter_months =  explode('-',$this_quarter);
		$this_quarter_month1 .= $this_quarter_months[0];
		$this_quarter_month2 .= $this_quarter_months[1];
		$this_quarter_month3 .= $this_quarter_months[2];
$last_quarter = $_POST['last_quarter'];
	$last_quarter_months =  explode('-',$last_quarter);
		$last_quarter_month1 .= $last_quarter_months[0];
		$last_quarter_month2 .= $last_quarter_months[1];
		$last_quarter_month3 .= $last_quarter_months[2];		
$today=date('Y-m-d', strtotime($todayedate));
$yesterday=date('Y-m-d', strtotime($yesterdaydate));


switch ($day) {
    case "today":
    
        $where_day .= "and invoicedate = '$today'";
        break;
    case "yesterday":
        $where_day .= "and invoicedate = '$yesterday'";
        break;
    case "Last_7_days":
     	$where_day .= "and invoicedate >= CURRENT_DATE()-7";
        break;
    case "last_30_days":
   		//$where_day .= "and invoicedate >= CURRENT_DATE()-30";
   		$where_day .= "and MONTH(invoicedate) in ('$this_month')";
        break;
    case "This_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        $where_day .= "and MONTH(invoicedate) in ('$this_month')";
        
        break;
    case "Last_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-60";
         $where_day .= "and MONTH(invoicedate) in ('$last_month')";
        break;
    case "This_quarter":
		$where_day = "and MONTH(invoicedate) in ('$this_quarter_month1,$this_quarter_month2,$this_quarter_month3')";
		//$where_day .= "";
        break;
    case "Last_quarter":
		$where_day = "and MONTH(invoicedate) in ('$last_quarter_month1,$last_quarter_month2,$last_quarter_month3')";
		//$where_day .= "";
        break;
    case "All_time":
    	$where_day .= "";
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        break;
    default:
        $where_day .= "";
}



					//die();
					  		    	
			
	$sql_invoice1 = "select SUM(cgst_amount) as cgst_amount,SUM(sgst_amount) as sgst_amount,SUM(igst_amount) as igst_amount,SUM(InvoiceBaseAmount) as InvoiceBaseAmount,SUM(InvoiceTotalAmount) as InvoiceTotalAmounts  
					from trninvoicetomember

					 where memberid = $memberid  and isdeleted=0 $where_day and $where_date  quotes_proforma_invoice = 2";


					 echo $sql_invoice1;
					 die();
					 $heading = "GSTR 3 B";
	//echo $sql_invoice1;
$sql_invoice_list1 = $obj_Invoice->getInvoice('',$sql_invoice1);
	
				//$sqllist2 = $obj->getCompany('','',$sql2);
					$rowcount = count($sql_invoice_list1);
					if ($sql_invoice_list1)
	{
		$rowcount_adv = $rowcount;
	}
	else
	{
		$rowcount_adv=0;
	}
			//
			//$InvoiceTotalAmount1  				= $sql_invoice_list1[0]['InvoiceTotalAmounts'];

			$maxrows = $rowcount_adv;
			$csvcolumnsname ='"","Amount","IGST","CGST","SGST","Total"'."\n";
echo $csvcolumnsname;
			//$InvoiceTotalAmount1  				= $sqllist2[0]['InvoiceTotalAllAmounts'];
	

	//echo $InvoiceTotalAmount1;
			 for($i=0;$i<$maxrows;$i++) 
         	{ 
         		$memberinvoicesid					= $sql_invoice_list1[$i]['memberinvoicesid'];
         		$companyid							= $sql_invoice_list1[$i]['billed_membercompanyid'];
         		$billing_membercompanyid		= $sql_invoice_list1[$i]['billing_membercompanyid'];
         		
         		$invoiceno							= $sql_invoice_list1[$i]['invoiceno'];
         		//$InvoiceTotalAmount				= $sqllist[$i]['InvoiceTotalAmount'];
         		$invoicedate						= date("j M Y",strtotime($sql_invoice_list1[$i]['invoicedate']));
         		$invoicedatemonth						= date("M Y",strtotime($sql_invoice_list1[$i]['invoicedate']));
         		//date("M jS, Y", strtotime("2011-01-05"));
         		$duedate								= date("j M Y",strtotime($sql_invoice_list1[$i]['duedate']));
         		
         			$InvoiceBaseAmount			= $sql_invoice_list1[$i]['InvoiceBaseAmount'];
					$InvoiceTotalAmount			= $sql_invoice_list1[$i]['InvoiceTotalAmount'];
					
         	
				
         			$cgst_amount					= $sql_invoice_list1[$i]['cgst_amount'];
			
					$sgst_amount					= $sql_invoice_list1[$i]['sgst_amount'];
					$igst_amount					= $sql_invoice_list1[$i]['igst_amount'];
					$sgst_percentage 				= $sql_invoice_list1[$i]['sgst_percentage'];
					$cgst_percentage 				= $sql_invoice_list1[$i]['cgst_percentage'];
					$igst_percentage 				= $sql_invoice_list1[$i]['igst_percentage'];
					$InvoiceTotalAmount1  				= $sql_invoice_list1[$i]['InvoiceTotalAmounts'];

         		$reportname = '"'.$heading.'"'."\n";
						$csvtotalincome = "\n".'"","","","","","","","","Total Amount","'.$InvoiceTotalAmount1.'",""'."\n";
						$csvdata .= '"'.$invoicedatemonth.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						$paymentresultCSV1 = $reportname.$csvcolumnsname.$csvdata.$csvtotalincome;

        }


}
else if (!headers_sent($filename, $linenum)) 
{
				$redirect_url = DOC_ROOT."login.php";
				header("Location: $redirect_url");
    				exit;
					// You would most likely trigger an error here.
}
else 
{
				echo "Headers already sent in $filename on line $linenum\n" .
          			"Cannot redirect, for now please click this <a ";
    				exit;
}   




		?>