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

$csv = (isset($_POST['csv_output'])?'1':'0');
		
$csv1 = (isset($_POST['csv_outputs'])?'1':'0');
$csv2 = (isset($_POST['csv_output2'])?'1':'0');
$csv3 = (isset($_POST['csv_output3'])?'1':'0');
	if ($csv=='1')
		{
			header("Content-type: application/octet-stream");
			header("Content-Disposition: attachment; filename=\"GSTR1.csv\"");
			
			$paymentlist = str_replace('\\"','"',$_POST['csv_output']);
			echo $paymentlist;
			exit();
		}
	else if ($csv1=='1')
		{
			header("Content-type: application/octet-stream");
			header("Content-Disposition: attachment; filename=\"GST-3B.csv\"");
			
			$paymentlist1 = str_replace('\\"','"',$_POST['csv_outputs']);
			echo $paymentlist1;
			exit();
		}
		else if ($csv2=='1')
		{
			//header("Content-type: application/octet-stream");
			//header("Content-Disposition: attachment; filename=\"GST-3B.csv\"");
			header("Content-type: application/octet-stream");
			header("Content-Disposition: attachment; filename=\"Receiptformatreport_fortally.csv\"");
			$paymentlist2 = str_replace('\\"','"',$_POST['csv_output2']);
			echo $paymentlist2;
			exit();
		}
		else if ($csv3=='1')
		{
			//header("Content-type: application/octet-stream");
			//header("Content-Disposition: attachment; filename=\"GST-3B.csv\"");
			header("Content-type: application/octet-stream");
			header("Content-Disposition: attachment; filename=\"Invoiceformatreport_fortally.csv\"");
			$searchresultCSV = str_replace('\\"','"',$_POST['csv_output3']);
			echo $searchresultCSV;
			exit();
		}
		/*else
		{
			require_once("header1.html");
		}*/

if($G_memberid > 0)
{
	
	$companyid = intval($_REQUEST['txtcompanyid']);
	$memberinvoicesid = intval($_REQUEST['txtmemberinvoicesid']);
	$memberid = $G_memberid;
		
	$memberuserscompanyid = $G_memberuserscompanyid;
	//$membercompanyid= $G_membercompanyid;

	$membercompanyid						= intval($_GET['membercompanyid']);
	$obj_client  = new Client();
	$obj  = new Company();
	$obj_proforma  = new Invoice_frproforma();
	$obj_Invoice  = new Invoice();
	$obj_Memberpaymentheader  = new Memberpaymentheader();	
		
	if(isset($_POST['SUBMIT']))
	{
		
		$Product['Email1'] 	      			= sanitize($_POST['Email1'],SQL);
		$Product['coverletter'] 	      			= sanitize($_POST['coverletter'],SQL);
	 	//$Product['clientcompanyid']     	= sanitize($_POST['txtcompanyid'],SQL);
	
		$whereclient = "clientcompanyid = ".$companyid;
		$editData	 = $obj_client -> EditClient($Product,$whereclient);
	if($memberuserscompanyid > 0)
	{ 
  	 	$query="select c.head_Email ,c.Email1,c.coverletter,c.panno,a.duedate,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceSubTotalAmount,a.invoicedate
					,a.duedate,a.billed_membername,a.discount_amount,a.inv_Add1,a.inv_Pin,a.inv_CityName,a.inv_Add2,a.inv_StateName,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceBaseAmount,d.gstno,a.inv_GST_no,
					a.InvoiceBaseAmountBeforeDiscount,a.cgst_amount,a.sgst_amount,a.igst_amount,d.upload_logo,d.upload_header,d.Addline1,d.Addline2,d.head_CityName,d.head_StateidName,d.companyname,d.head_Pin,d.head_TelNo,d.panno,d.Bankname,d.BankAC,d.Branch,d.IFSC_Code,c.PersonName1,d.head_Email as companyEmail,b.* from mstmember x
				  	inner join trninvoicetomember a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
              	inner join mstclients c on b.billed_membercompanyid = c.clientcompanyid
              	inner join user_company d on a.billing_membercompanyid=d.membercompanyid
              	inner join trnuser_company e on x.memberid = e.memberid
             	where x.memberid =$memberid  and a.billing_membercompanyid=$membercompanyid and  e.memberuserscompanyid = $memberuserscompanyid and  a.isdeleted= 0 and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid";
    }
    else
    {
    	  	 	$query="select c.head_Email ,c.Email1,c.coverletter,c.panno,a.duedate,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceSubTotalAmount,a.invoicedate
					,a.duedate,a.billed_membername,a.discount_amount,a.inv_Add1,a.inv_Pin,a.inv_CityName,a.inv_Add2,a.inv_StateName,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceBaseAmount,d.gstno,a.inv_GST_no,
					a.InvoiceBaseAmountBeforeDiscount,a.cgst_amount,a.sgst_amount,a.igst_amount,d.upload_logo,d.upload_header,d.Addline1,d.Addline2,d.head_CityName,d.head_StateidName,d.companyname,d.head_Pin,d.head_TelNo,d.panno,d.Bankname,d.BankAC,d.Branch,d.IFSC_Code,c.PersonName1,d.head_Email as companyEmail,b.* from mstmember x
				  	inner join trninvoicetomember a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
              	inner join mstclients c on b.billed_membercompanyid = c.clientcompanyid
              	inner join user_company d on a.billing_membercompanyid=d.membercompanyid
             	where x.memberid =$memberid and  a.isdeleted= 0 and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid";
    }
          
/*

             	$query="select c.head_Email,a.billed_membername,a.duedate,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceSubTotalAmount,a.invoicedate
					,a.duedate,a.billed_membername,a.discount_amount,a.inv_Add1,a.inv_Pin,a.inv_CityName,a.inv_Add2,a.inv_StateName,a.invoiceno,a.InvoiceBaseAmount,d.gstno,a.inv_GST_no,
					a.InvoiceBaseAmountBeforeDiscount,a.cgst_amount,a.sgst_amount,a.igst_amount,
					b.ProductTotalAmount,
					d.upload_logo,d.upload_header,d.companyname,d.gstno,d.panno,
					d.Addline1,d.Addline2,d.head_CityName,d.head_StateidName,d.Country,d.head_Pin,d.head_TelNo,d.head_Email,a.cgst_percentage,a.sgst_percentage,a.igst_percentage,b.* 
					from mstmember x
				  	inner join trninvoicetomember a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
              	inner join mstclients c on b.billed_membercompanyid = c.clientcompanyid
              	inner join user_company d on a.billing_membercompanyid=d.membercompanyid
             	where x.memberid =$memberid and  a.isdeleted= 0 and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid";
             */	//echo $query;
				//die();
		
		$querylist = $obj_proforma ->getInvoice('',$query);
		$rowtoproforma 		=count($querylist);



			//echo $csvcolumnsname;
			
		


		$counter = 0;
		$igst_amount = 0;
		for($i=0;$i<$rowtoproforma;$i++)
		{		
			$counter++;
			$memberid										= $memberid;
			$memberinvoicesid								= $memberinvoicesid;	
			$billed_membercompanyid						= $querylist[$i]['billed_membercompanyid'];
			$billing_membercompanyid					= $billing_membercompanyid_proforma;
			$billed_membername 							= $querylist[$i]['billed_membername'];
			$billed_PersonName 							= $querylist[$i]['PersonName1'];

			$InvoiceBaseAmountBeforeDiscount       = $querylist[$i]['InvoiceBaseAmountBeforeDiscount'];
			$inv_Add1										= $querylist[$i]['inv_Add1'];
			$inv_Add2										= $querylist[$i]['inv_Add2'];
			$inv_Add3										= $querylist[$i]['inv_Add3'];
			$inv_Add4										= $querylist[$i]['inv_Add4'];
			$address                        			= $querylist[$i]['inv_Add1'].'</br>'.$querylist[$i]['inv_Add2'].'</br>'.$querylist[$i]['inv_CityName'].' '.$querylist[$i]['inv_Pin'];
			$panno											= $querylist[$i]['panno'];
			$gstno											= $querylist[$i]['gstno'];
			$quotes_proforma_invoice					=	0;
			$inv_CityName 									= $querylist[$i]['inv_CityName'];
			$inv_Pin 										= $querylist[$i]['inv_Pin'];
			$inv_StateName 								= $querylist[$i]['inv_StateName'];
			$inv_head_Stateid								= $querylist[$i]['inv_head_Stateid'];
			$invoiceno 										= $querylist[$i]['invoiceno'];
			$invoicedate									= date('d-m-Y',strtotime($querylist[$i]['invoicedate']));
			$duedate											= $querylist[$i]['duedate'];
			$status 											= $querylist[$i]['status'];
			$igst_percentage								= $querylist[$i]['igst_percentage'];
			$igst_amount									= format_number($querylist[$i]['igst_amount']);
			$cgst_percentage								= $querylist[$i]['cgst_percentage'];
			$cgst_amount									= format_number($querylist[$i]['cgst_amount']);
			$sgst_percentage								= $querylist[$i]['sgst_percentage'];
			$sgst_amount									= format_number($querylist[$i]['sgst_amount']);
			$ugst_percentage								= $querylist[$i]['ugst_percentage'];
			$ugst_amount									= format_number($querylist[$i]['ugst_amount']);
			$discount_percentage							= $querylist[$i]['discount_percentage'];
			$discount_amount								= format_number($querylist[$i]['discount_amount']);
			$InvoiceBaseAmountBeforeDiscount			= $querylist[$i]['InvoiceBaseAmountBeforeDiscount'];
			$InvoiceBaseAmount							= format_number($querylist[$i]['InvoiceBaseAmount']);
			$InvoiceTotalAmount							= format_number($querylist[$i]['InvoiceTotalAmount']);
				$InvoiceSubTotalAmount						= format_number($querylist[$i]['InvoiceSubTotalAmount']);
			$InvoiceTotalAmount1							= $querylist[$i]['InvoiceTotalAmount'];
			$quantity										= $querylist[$i]['quantity'];
			//$discount_amount							= $querylist[$i]['discount_amount'];
			//$price											= $querylist[$i]['InvoiceTotalAmount'];
			$ProductBaseAmount							= number_format($querylist[$i]['ProductBaseAmount']);
			$ProductTotalAmount							= number_format($querylist[$i]['ProductTotalAmount']);
			$productname									= $querylist[$i]['productname'];
			//$product_description						= str_replace(' ', "\n", $querylist[$i]['product_description']);
			$product_description							= $querylist[$i]['product_description'];
			$product_unit									= $querylist[$i]['product_unit'];
			$inv_GST_no										= $querylist[$i]['inv_GST_no'];
			
			
		
			//$InvoiceTotalAmountinword					= number_to_words($InvoiceTotalAmount1);
			$InvoiceTotalAmountinword					= ucwords(number_to_words($InvoiceTotalAmount1));
						
				
				
				if($rowtoproforma >=3)
			{
				
				$data_design = 'style="height:auto;"';
				//$data_design = 'style="height:150px;"';
			}
			else
			{
				$data_design = 'style="height:180px;"';
				
			}

				$productarray .='<tr>';
				$productarray	.='<td style="padding-top:20px;border-top:none;border-bottom:none;">'.$counter.'</td>';
				$productarray	.='<td style="border-top:none;border-bottom:none;"><div '.$data_design.'><b>'.$productname.'</b>';
				$productarray	.='<br><br><b>'.$product_description.'</b></div><br>';
				$productarray	.='</td>';
				$productarray	.='<td  style="border-top:none;border-bottom:none;" valign="top" align="right"><br><b>'.$ProductBaseAmount.'</b></td>';
				$productarray	.=	'<td  style="border-top:none;border-bottom:none;" valign="top" align="right"><br><b>'.$quantity.'</b></td>';
				$productarray	.='<td  style="border-top:none;border-bottom:none;" valign="top" align="right"><br><b>'.$ProductTotalAmount.'</b></td>';
			}
			
$companyname 		= $querylist[0]['companyname'];
		$gstno 				= $querylist[0]['gstno'];
		$panno 				= $querylist[0]['panno'];
		$Addline1 			= $querylist[0]['Addline1'];
		$Addline2 			= $querylist[0]['Addline2'];
		$head_CityName 	= $querylist[0]['head_CityName'];
		$head_StateidName = $querylist[0]['head_StateidName'];
		$Country 			= $querylist[0]['Country'];
		$head_Pin 			= $querylist[0]['head_Pin'];
		$head_TelNo 		= $querylist[0]['head_TelNo'];
		$head_Email 		= $querylist[0]['head_Email'];
		$upload_logo_display 	= $querylist[0]['upload_logo'];
		$upload_header_display  = $querylist[0]['upload_header'];
		$formEmails										= $querylist[0]['companyEmail'];
		$toEmails									= $querylist[0]['head_Email'].','.$querylist[0]['Email1'];
		$coverletters										= $querylist[0]['coverletter'];
		$BankName = $querylist[0]['Bankname'];
		$Bank_ACno      = $querylist[0]['BankAC'];
	    $Branch  		= $querylist[0]['Branch'];
		$IFSC_Code    = $querylist[0]['IFSC_Code'];
			if($igst_amount == '' || $igst_amount == 0)
		{

				$row_gst .='<tr>';
				$row_gst .='<td valign="top" colspan="2" rowspan="4">
							<b>In words
							Rs. '.$InvoiceTotalAmountinword.' Only
							<br>GST ID : '.$gstno.'
							<br>PAN No : '.$panno.'
							<br><br>Bank Name : '.$BankName.'
							<br>Bank A/C : '.$Bank_ACno.'
							<br>Branch : '.$Branch.'
							<br>IFSC Code : '.$IFSC_Code.'</b></td>';
				//$row_gst .='<td colspan="2">';
				//$row_gst .='<b>SubTotal</b>';
				//$row_gst .='</td>';
				//$row_gst .='<td align="right"><b>'.$InvoiceSubTotalAmount.'</b>';
				//$row_gst .='</td>';
				//$row_gst .='</tr>';
				//$row_gst .='<tr>';
			 	$row_gst .='<td colspan="2">';
				$row_gst .='<b>CGST '.$cgst_percentage.'%</b>';
				$row_gst .='</td>';
				$row_gst .='<td align="right">';
				$row_gst .='<b>'.$cgst_amount.'</b>';
				$row_gst .='</td>';
				$row_gst .='</tr>';
				$row_gst .='<tr>';
				$row_gst .='<td colspan="2">';
				$row_gst .='<b>SGST '.$sgst_percentage.'%</b>';
				$row_gst .='</td>';
				$row_gst .='<td align="right"><b>'.$sgst_amount.'</b></td>';
				$row_gst .='</tr>';
				$row_gst .='<tr>';
				$row_gst .='<td colspan="2">';
				$row_gst .='<b>Grand Total</b>';
				$row_gst .='</td>';
				$row_gst .='<td align="right"><b>'.$InvoiceTotalAmount.'</b></td>';
				$row_gst .='</tr>';
				$row_gst .='<tr>';
				$row_gst .='<td colspan="3">
							<b>For '.$companyname.'</b><br><br>
							<b>Director </b><br><br>(Electronic Invoice do not need signature)';
				$row_gst .='</td>';
				$row_gst .='</tr>';
		}
		else 
		{
				$row_gst .='<tr>';
				$row_gst .='<td valign="top" colspan="2" rowspan="3">
							<b>In words
							Rs. '.$InvoiceTotalAmountinword.' Only
							<br>GST ID : '.$gstno.'
							<br>PAN No : '.$panno.'
							<br><br>Bank Name : '.$BankName.'
							<br>Bank A/C : '.$Bank_ACno.'
							<br>Branch : '.$Branch.'
							<br>IFSC Code : '.$IFSC_Code.'</b></td>';
				//$row_gst .='<td colspan="2">';
				//$row_gst .='<b>SubTotal</b>';
				//$row_gst .='</td>';
				//$row_gst .='<td align="right"><b>'.$InvoiceSubTotalAmount.'</b>';
				//$row_gst .='</td>';
				//$row_gst .='</tr>';
				//$row_gst .='<tr>';
				$row_gst .='<td colspan="2">';
				$row_gst .='<b>IGST '.$igst_percentage.'%</b>';
				$row_gst .='</td>';
				$row_gst .='<td align="right">';
				$row_gst .='<b>'.$igst_amount.'</b>';
				$row_gst .='</td>';
				$row_gst .='</tr>';

				$row_gst .='<tr>';
				$row_gst .='<td colspan="2">';
				$row_gst .='<b>Grand Total</b>';
				$row_gst .='</td>';
				$row_gst .='<td align="right"><b>'.$InvoiceTotalAmount.'</b></td>';
				$row_gst .='</tr>';
				$row_gst .='<tr>';
				$row_gst .='<td colspan="3">';
				$row_gst .='<b>For '.$companyname.'<br><br>
									<b>Director </b><br><br>
									(Electronic Invoice do not need signature)';
				$row_gst .='</td>';
				$row_gst .='</tr>';
				
		}
		
									
			if($discount_amount != '') {
			$row_discount .='<tr>';
			$row_discount .='<td colspan="2"><b>Less Rs.'.$discount_amount.'  discount</b></td>';
			$row_discount .='<td colspan="2"><b>Discount </b></td>';
			$row_discount .='<td align="right"><b>'.$discount_amount.'</b></td>';
			$row_discount .='</tr>';
		}
		else {
			$row_discount .='';
			}
									
		$upload_header = GALLERY_PHOTO_PATH.$querylist[0]['upload_header'];
		$upload_logo = GALLERY_PHOTO_PATH.$querylist[0]['upload_logo'];
		if($upload_header_display != '')
		{
			$header_display = '<td valign="top" style="border-bottom:none;">
												<img src="'.$upload_header.'" width="280px" height="150px">
									 </td>';
		}
		else 
		{
					if($upload_logo_display != '')
					{
						$header_display = '<td valign="top" width="30%"  style="border-bottom:none;">
												<img src="'.$upload_logo.'" width="150px">
												</td>
												<td  width="70%">
												<h2 style="margin:0;">'.$companyname.'</h2>
													
													<p>Admn Off: '.$Addline1.'<br>
													'.$Addline2.' '.$head_CityName.' - '.$head_Pin.' '.$head_StateidName.' <br>
													Phone : '.$head_TelNo.'<br>
													Email : '.$head_Email.'
													</p>
												</td>';
					}
					else 
					{
						$header_display = '<td colspan="5"  style="border-bottom:none;">
													<h2 style="margin:0;">'.$companyname.'</h2>
													
													<p>Admn Off: '.$Addline1.'<br>
													'.$Addline2.' '.$head_CityName.' - '.$head_Pin.' '.$head_StateidName.' <br>
													Phone : '.$head_TelNo.'<br>
													Email : '.$head_Email.'
													</p>
												</td>';
					}

					
				}
		
			$swiftdir = GLOBAL_INCLUDE_PATH."htmltopdf";
			//echo $dir; 
			//$MerTemplateFile = $dir."/final_invoice-manu_new.php";
			$MerTemplateFile = "invoice_template_new.php";
	
			ob_start();
			require_once($MerTemplateFile);
			$pdf_html_mer = ob_get_contents();
			ob_end_clean();
	
			require_once($swiftdir.'/dompdf/dompdf_config.inc.php');
			require_once($swiftdir.'/swift/swift_required.php');
				
			$replace = array(
			      "[productname]"								=> $productname,
					"[billed_membername]"						=> $billed_membername,
					"[ProductBaseAmount]"						=> $ProductBaseAmount,
					"[InvoiceTotalAmount]"						=> $InvoiceTotalAmount,
					"[InvoiceSubTotalAmount]"						=> $InvoiceSubTotalAmount,
					"[InvoiceBaseAmount]"						=> $InvoiceBaseAmount,
					"[InvoiceTotalAmountinword]"				=> $InvoiceTotalAmountinword,
					"[address]"										=>	$address,
					"[InvoiceBaseAmountBeforeDiscount]"		=> $InvoiceBaseAmountBeforeDiscount,
					"[productname]"								=> $productname,
					"[product_description]"						=> $product_description,
					"[ProductTotalAmount]"						=> $ProductTotalAmount,
					"[ProductTotalAmount1]"						=> $ProductTotalAmount1,
					"[inv_GST_no]"                         =>	$inv_GST_no,
					"[inv_Add1]"                         	=>	$inv_Add1,
					"[panno]" 										=>	$panno,
					"[gstno]" 										=>	$gstno,
					"[quantity]"									=> $quantity,
					"[product_unit]" 								=> $product_unit,
					"[cgst_amount_product]"						=> $cgst_amount_product,
					"[sgst_amount_product]"						=> $sgst_amount_product,
					"[igst_amount_product]"						=> $igst_amount_product,
					"[invoiceno]"			     				   => $invoiceno,
					"[invoicedate]"								=> $invoicedate,
					"[discount_amount]"							=> $discount_amount,
					"[productarray]"								=>	$productarray,
					"[cgst_amount]"								=> $cgst_amount,
					"[sgst_amount]"								=> $sgst_amount,
					"[igst_amount]"								=> $igst_amount,
					"[row_gst]"										=>	$row_gst,
					"[row_discount]"								=>	$row_discount,
 		         "[rowspan]"             					=> $rowspan,
 		         "[header_display]"							=> $header_display	
					);
		 	
		  			//require_once("proforma_template_new.php");
		  			
		  				

	
	          $resultstr	  = strtr($TEMPLATE["PAGES"]["PAGE"],$replace);
	          $result_template = strtr($pdf_html_mer,$replace);
		
				 $dompdf = new DOMPDF(); // Create new instance of dompdf
				 $dompdf->load_html($result_template); // Load the html
				 $dompdf->render(); // Parse the html, convert to PDF
				 $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
								
				// $html_message = nl2br("Dear ".$billed_membername.",");
								$html_message = nl2br("<tr><b>Dear $billed_PersonName,</b></tr><tr><br></tr><tr>$coverletters</tr><tr><br><br></tr><tr>Regards,</tr><tr><b>$companyname</b></tr><tr><b>$head_TelNo</b></tr>");
					
					//$html_message3 = nl2br("Regards,");
					//$html_message4 = nl2br("");
					//$html_message5 = nl2br($billed_membername);		formEmails
			 	// $fromEmail = array('nagma.m@evergrowsolutions.com' =>'Invoice');
				$fromEmail = array($formEmails =>'Invoice');
										
					// Load the SwiftMailer files
					//print_r($toEmails);
				$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer	
				$fileName = str_replace(' ','-',$billed_membername).'.pdf'; 
					//$message = Swift_Message::newInstance('/usr/sbin/exim -bs')
				$message = Swift_Message::newInstance()
								       ->setSubject('Invoice') // Message subject
									   ->setTo($toEmails) // Array of people to send to
									   ->setBcc($BCCEmails) // Array of people to send to
									   ->setFrom($fromEmail) // From:
									   ->setBody($html_message,'text/html')
									   // ->setBody($html_message2,'text/html')
									    // ->addPart('Please find attached invoice', 'text/html')
									    // Attach that HTML message from earlier
				

								


									   ->attach(Swift_Attachment::newInstance($pdf_content,$fileName, 'application/pdf')); // Attach the generated PDF from earlier
						
				// Send the email, and show user message
				if ($mailer->send($message))
				{
					//$success = true;
					$mail_array['is_mail_send']=1;
					$mail_array['mail_send_by']=$G_memberid;
					$mail_array['mail_send_date']=date('Y-m-d');
					$wheremail="memberinvoicesid = $memberinvoicesid";
				
					$editemail =$obj_Invoice->EditInvoice($mail_array,$wheremail);
				echo "<script type='text/javascript'>alert('Email Send Successfully.');
													window.location.href='".DOC_ROOT."admin/viewInvoice.php';
													window.location.reload();
													</script>";	
			
					 
				}	
				else
				{
					$error = true;
					$strmessageHeader = "There was Error while Sending Emails. Please try again";
					echo $strmessageHeader;		
				}
				$dompdf -> __destruct();
				unset($dompdf);
				unset($pdf_content);
				//unset($mailer);
				unset($message);			
    		
	}
	else 
	{

		//echo"<pre>";
		//print_r($_POST);

		$montharray = sanitize($_POST['cmbmonth'],SQL);
				$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";
				}		
 $sql_fy = "select YEAR(CURDATE())-1 as last_fy_year,YEAR(CURDATE()) as This_fy_year,YEAR(CURDATE())+1 as next_fy_year
			 			    from mstmember x
					  		
					  		 where x.memberid = $memberid";
			  		 
					//echo $sql;
					//die();
					$sqllist_fy = $obj->getCompany('','',$sql_fy);
					$last_fy_year						= $sqllist_fy[0]['last_fy_year'];
					$This_fy_year						= $sqllist_fy[0]['This_fy_year'];
					$next_fy_year						= $sqllist_fy[0]['next_fy_year'];
	$display1 = sanitize($_POST['txtfromdate'],SQL);
	$display2 = sanitize($_POST['txttodate'],SQL);
	
	$fromdate	= date("Y-m-d", strtotime($_POST['txtfromdate']));
	$todate		= date("Y-m-d", strtotime($_POST['txttodate']));
		if ($fromdate=='1970-01-01')
	{
		//$where_date ="YEAR (invoicedate) = YEAR (CURDATE()) and ";
		$where_date ="date(invoicedate) BETWEEN '$This_fy_year-04-01' and '$next_fy_year-03-31' and ";
		
	}
	else
	{
		$where_date ="date(invoicedate) BETWEEN '$fromdate' and '$todate'and";
	}





 
$day = sanitize($_POST['opt'],SQL);
$todayedate = sanitize($_POST['todayedate'],SQL);
$yesterdaydate = sanitize($_POST['yesterdaydate'],SQL);
$this_month = sanitize($_POST['this_month'],SQL);
$last_month = sanitize($_POST['last_month'],SQL);

$this_year = sanitize($_POST['this_year'],SQL);
	$this_years =  explode('-',$this_year);
	$this_year1 .= $this_years[0];
	$Next_year2 .= $this_years[1];

$last_year = sanitize($_POST['last_years'],SQL);
	$last_years =  explode('-',$last_year);
	$last_year1 .= $last_years[0];
	$this_year3 .= $last_years[1];

$this_quarter = sanitize($_POST['this_quarter'],SQL);
	$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 = sanitize($_POST['last_quarter'],SQL);
	$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'  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "yesterday":
        $where_day .= "and invoicedate = '$yesterday'  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "Last_7_days":
     	$where_day .= "and invoicedate >= CURRENT_DATE()-7  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "last_30_days":
   		//$where_day .= "and invoicedate >= CURRENT_DATE()-30";
   		$where_day .= "and MONTH(invoicedate) in ('$this_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "This_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        $where_day .= "and MONTH(invoicedate) in ('$this_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        
        break;
    case "Last_month":
       // $where_day .= "and invoicedate >= CURRENT_DATE()-60";
         $where_day .= "and MONTH(invoicedate) in ('$last_month')  and YEAR (invoicedate) = YEAR (CURDATE())";
        break;
    case "This_quarter":
     // $where_day .= "and invoicedate >= QUARTER('$today')";
		//$where_day = "and MONTH(invoicedate) in ('$this_quarter_month1,$this_quarter_month2,$this_quarter_month3')  and YEAR (invoicedate) = YEAR (CURDATE())";
    	if($this_quarter=='1')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='2')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='3')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='4')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($this_quarter=='5')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
    	if($this_quarter=='6')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='7')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' AND and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='8')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='9')
    	{

		$where_day .= "and QUARTER(invoicedate)='3'and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='10')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='11')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($this_quarter=='12')
    	{

		$where_day .= "and QUARTER(invoicedate)='4'  and YEAR (invoicedate) = YEAR (CURDATE())";
		}
        break;
    	case "Last_quarter":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and MONTH(invoicedate) in ('$last_quarter_month1,$last_quarter_month2,$last_quarter_month3')  and YEAR (invoicedate) = YEAR (CURDATE())";
    	if($last_quarter=='1')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='2')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='3')
    	{

		$where_day .= "and QUARTER(invoicedate)='1' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='4')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
		if($last_quarter=='5')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}	
    	if($last_quarter=='6')
    	{

		$where_day .= "and QUARTER(invoicedate)='2' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='7')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' AND and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='8')
    	{

		$where_day .= "and QUARTER(invoicedate)='3' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='9')
    	{

		$where_day .= "and QUARTER(invoicedate)='3'and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='10')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='11')
    	{

		$where_day .= "and QUARTER(invoicedate)='4' and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		if($last_quarter=='12')
    	{

		$where_day .= "and QUARTER(invoicedate)='4'  and YEAR (invoicedate) = YEAR (CURDATE())";
		}
		
		//$where_day .= "";
        break;
         case "This_year":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and YEAR(invoicedate) in('$this_year')";

       //  $sql="SELECT *FROM mytable where date BETWEEN CAST('$pst-04-01' AS DATE) AND CAST('$pt-03-31' AS DATE)";

		$where_day ="and date(invoicedate) BETWEEN '$this_year1-04-01' and '$Next_year2-03-31'";
		//$where_date .= "";
        break;
         case "Last_year":
    // $where_day .= "and invoicedate >= QUARTER(CURDATE())-1";
		//$where_day = "and YEAR(invoicedate) in('$last_year')";
		$where_day ="and date(invoicedate) BETWEEN '$last_year1-04-01' and '$this_year3-03-31'";

		
		//$where_day .= "";
        break;
    case "All_time":
    	$where_day .= "";
       // $where_day .= "and invoicedate >= CURRENT_DATE()-30";
        break;
    default:
        $where_day .= "";
}

$radiopaymentmode = sanitize($_POST['radiopaymentmode1'],SQL);
switch ($radiopaymentmode) {
    case "Partially_Paid":
    
        $where_paymentmode .= "and InvoiceTotalAmount-TotalAmount !='0'";
        // $where_paymentmode1 .= "and InvoiceTotalAmount !=paid_total_amount";
        $where_paymentmode1 .= "and InvoiceTotalAmount-paid_total_amount >'0'";
        break;
    case "Not_Paid":
        $where_paymentmode .= "and paid_total_amount = ' '";
        $where_paymentmode1 .= "and paid_total_amount = ' '";
        break;
    case "Full_Paid":
     	 $where_paymentmode1 .= "and InvoiceTotalAmount =paid_total_amount";
     	$where_paymentmode .= "and InvoiceTotalAmount-TotalAmount ='0'";
        break;
    case "All":
   		//$where_day .= "and invoicedate >= CURRENT_DATE()-30";
   		$where_paymentmode .= "";
   		$where_paymentmode1 .= "";
        break;
    default:
        $where_paymentmode .= "";
        $where_paymentmode1 .= "";
}

//echo $this_quarterg;

			// All members 
			if($memberuserscompanyid > 0)
			{	
					if($day){
			 	  $sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 inner join trnuser_company g on x.memberid = g.memberid
					  		 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and  g.memberuserscompanyid = $memberuserscompanyid  $where_paymentmode $where_day and  a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned) ";
					  		}
					  		else{
					  			 $sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 inner join trnuser_company g on x.memberid = g.memberid
					  		 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and  g.memberuserscompanyid = $memberuserscompanyid $where_paymentmode  and $where_date    a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned) ";

					  		}
			}
			else
			{
						if($day){
			 	  $sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid $where_paymentmode $where_day and  a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned) ";
					  		}
					  		else{
					  			 $sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid $where_paymentmode  and $where_date    a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned) ";

					  		}
			}			  		
					//echo $sql;
					//die();
					$sqllist = $obj->getCompany('','',$sql);
					if(is_array($sqllist))
					{
						$rowcount = count($sqllist);
					}
				if($memberuserscompanyid > 0)
				{	
					if($day){
					 $sql2 = "select *,e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,MONTH(CURRENT_DATE()) as today_date,CURRENT_DATE() -1 as Yesterday_date 
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		  inner join trnuser_company g on x.memberid = g.memberid
					  		 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and g.memberuserscompanyid = $memberuserscompanyid $where_paymentmode  $where_day and  a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid  order by a.invoicedate ASC";
					  		}
					  		else{
					  		$sql2 = "select *,e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,MONTH(CURRENT_DATE()) as today_date,CURRENT_DATE() -1 as Yesterday_date 
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		  inner join trnuser_company g on x.memberid = g.memberid
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and  g.memberuserscompanyid = $memberuserscompanyid  $where_paymentmode   and $where_date a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid  order by a.invoicedate ASC";

					  		}
				}
				else
				{
					if($day){
					 $sql2 = "select *,e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,MONTH(CURRENT_DATE()) as today_date,CURRENT_DATE() -1 as Yesterday_date 
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid $where_paymentmode  $where_day and  a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid  order by a.invoicedate ASC";
					  		}
					  		else{
					  		$sql2 = "select *,e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,MONTH(CURRENT_DATE()) as today_date,CURRENT_DATE() -1 as Yesterday_date 
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid $where_paymentmode   and $where_date a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid  order by a.invoicedate ASC";

					  		}
				}	  		
					//echo $sql2;
					//die();
					$heading = "Invoice GSTR1";
					$sqllist2 = $obj->getCompany('','',$sql2);
					$rowcount = count($sqllist2);
					if ($sqllist2)
					{
						$rowcount_adv = $rowcount;
					}
					else
					{
						$rowcount_adv=0;
					}
				if($memberuserscompanyid > 0)
				{	
					if($day){
					$sql_invoice2 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts ,SUM(paid_total_amount) as paid_total_amount1,SUM(igst_amount) as igst_amounts,SUM(cgst_amount) as cgst_amounts,SUM(sgst_amount) as sgst_amounts,SUM(ugst_amount) as ugst_amounts,SUM(InvoiceBaseAmount) as InvoiceBaseAmounts from trninvoicetomember a  where memberid = $memberid  and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
					}
					else{
						$sql_invoice2 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts ,SUM(paid_total_amount) as paid_total_amount1,SUM(igst_amount) as igst_amounts,SUM(cgst_amount) as cgst_amounts,SUM(sgst_amount) as sgst_amounts,SUM(ugst_amount) as ugst_amounts,SUM(InvoiceBaseAmount) as InvoiceBaseAmounts from trninvoicetomember a d where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
					}
				}
				else
				{
					if($day){
					$sql_invoice2 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts ,SUM(paid_total_amount) as paid_total_amount1,SUM(igst_amount) as igst_amounts,SUM(cgst_amount) as cgst_amounts,SUM(sgst_amount) as sgst_amounts,SUM(ugst_amount) as ugst_amounts,SUM(InvoiceBaseAmount) as InvoiceBaseAmounts from trninvoicetomember a  where memberid = $memberid and a.billing_membercompanyid=$membercompanyid  and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
					}
					else{
						$sql_invoice2 = "select SUM(InvoiceTotalAmount) as InvoiceTotalAmounts ,SUM(paid_total_amount) as paid_total_amount1,SUM(igst_amount) as igst_amounts,SUM(cgst_amount) as cgst_amounts,SUM(sgst_amount) as sgst_amounts,SUM(ugst_amount) as ugst_amounts,SUM(InvoiceBaseAmount) as InvoiceBaseAmounts from trninvoicetomember a  where memberid = $memberid and a.billing_membercompanyid=$membercompanyid  and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
					}
				}	

	//echo $sql_invoice2;
						$sql_invoice_list2 = $obj_Invoice->getInvoice('',$sql_invoice2);
	
		
			if($memberuserscompanyid > 0)
				{	
					if($day){
					$sql_payment2 = "select SUM(TotalAmount) as TotalAmounts3,SUM(tds) as Totaltds from user_company_payment_header a inner join trninvoicetomember b on a.memberinvoicesid = b.memberinvoicesid
					where memberid = $memberid  and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
					}
					else{
						$sql_payment2 = "select SUM(TotalAmount) as TotalAmounts3,SUM(tds) as Totaltds from user_company_payment_header a inner join trninvoicetomember b on a.memberinvoicesid = b.memberinvoicesid  where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
					}
				}
				else
				{
					if($day){
					$sql_payment2 = "select SUM(TotalAmount) as TotalAmounts3,SUM(tds) as Totaltds from user_company_payment_header a inner join trninvoicetomember b on a.memberinvoicesid = b.memberinvoicesid  where memberid = $memberid and a.billing_membercompanyid=$membercompanyid  and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
					}
					else{
						$sql_payment2 = "select SUM(TotalAmount) as TotalAmounts3,SUM(tds) as Totaltds from user_company_payment_header a inner join trninvoicetomember b on a.memberinvoicesid = b.memberinvoicesid where memberid = $memberid and a.billing_membercompanyid=$membercompanyid  and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
					}
				}	
//echo $sql_payment2;

				$sql_paymentlist2 = $obj_Memberpaymentheader->getMemberpaymentheader('',$sql_payment2);
						
	//echo $sql_invoice1;
						$sql_invoice_list2 = $obj_Invoice->getInvoice('',$sql_invoice2);
	
		
			
						$InvoiceTotalAmount1  				= $sql_invoice_list2[0]['InvoiceTotalAmounts'];
						$igst_amounts  				= $sql_invoice_list2[0]['igst_amounts'];
						$cgst_amounts  				= $sql_invoice_list2[0]['cgst_amounts'];
						$sgst_amounts  				= $sql_invoice_list2[0]['sgst_amounts'];
						$ugst_amounts  				= $sql_invoice_list2[0]['ugst_amounts'];
						$InvoiceBaseAmounts  				= $sql_invoice_list2[0]['InvoiceBaseAmounts'];
							$TotalAmounts3  				= $sql_paymentlist2[0]['TotalAmounts3'];
						$paid_total_amount1  				= $sql_invoice_list2[0]['paid_total_amount1'];
						$Totaltds  				= $sql_paymentlist2[0]['Totaltds'];
						//$balance1sds= $InvoiceTotalAmount1 - $paid_total_amount1-$Totaltds;

						if($paid_total_amount1 == '0')
         		{
         			$balance1sds 							= $InvoiceTotalAmount1 - $TotalAmounts3-$Totaltds;
         			//$balance1sds 							= $InvoiceTotalAmount1 - $TotalAmounts3;
         		}
         		else
         		{
					//$balance1 =  $InvoiceTotalAmount - $paid_total_amount;
					//$balance1sds= $InvoiceTotalAmount1 - $paid_total_amount1;
					$balance1sds= $InvoiceTotalAmount1 - $paid_total_amount1-$Totaltds;
					//$balance1sds= $InvoiceTotalAmount1 - $paid_total_amount1;
         		}
					if($memberuserscompanyid > 0)
					{		
						if($day){
								$sqlduequery5 = "select SUM(a.TotalAmount) as paid_total_amount,SUM(InvoiceTotalAmount) as InvoiceTotalAmounts1 from user_company_payment_header a
									INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

									where memberid = $memberid  and c.billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
								}
								else{
									$sqlduequery5 = "select SUM(a.TotalAmount) as paid_total_amount,SUM(InvoiceTotalAmount) as InvoiceTotalAmounts1 from user_company_payment_header a
									INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

									where memberid = $memberid  and c.billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
								}
					}
					else
					{
						if($day){
								$sqlduequery5 = "select SUM(a.TotalAmount) as paid_total_amount,SUM(InvoiceTotalAmount) as InvoiceTotalAmounts1 from user_company_payment_header a
									INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

									where memberid = $memberid and a.billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and   quotes_proforma_invoice = 2";
								}
								else{
									$sqlduequery5 = "select SUM(a.TotalAmount) as paid_total_amount,SUM(InvoiceTotalAmount) as InvoiceTotalAmounts1 from user_company_payment_header a
									INNER join trninvoicetomember c on a.memberinvoicesid = c.memberinvoicesid

									where memberid = $memberid and a.billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode1 $where_day and $where_date  quotes_proforma_invoice = 2";
								}
					}			
							//echo $sqlduequery5;

						//die();
								$sqlduequery_list5 = $obj_Invoice->getInvoice('',$sqlduequery5);
	
		
								$InvoiceTotalAmounts  				= $sql_invoice_list5[0]['InvoiceTotalAmounts1'];
								$paid_total_amountsd 				= $sqlduequery_list5[0]['paid_total_amount'];

        					 $balance1sd= $InvoiceTotalAmount1 - $paid_total_amountsd;
        			if($memberuserscompanyid > 0)
					{		 
        					 if($day)
        					 {
									$sql_invoice3 = "select  invoicedate as invoicedatemonth  from trninvoicetomember where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode $where_day and   quotes_proforma_invoice = 2 limit 0,1";
							}
							else{
								$sql_invoice3 = "select  invoicedate as invoicedatemonth  from trninvoicetomember where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode  and $where_date  quotes_proforma_invoice = 2 limit 0,1";

							}
					}
					else
					{
							 if($day)
        					 {
									$sql_invoice3 = "select  invoicedate as invoicedatemonth  from trninvoicetomember where memberid = $memberid and a.billing_membercompanyid=$membercompanyid  and isdeleted=0 $where_paymentmode $where_day and   quotes_proforma_invoice = 2 limit 0,1";
							}
							else{
								$sql_invoice3 = "select  invoicedate as invoicedatemonth  from trninvoicetomember where memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and isdeleted=0 $where_paymentmode  and $where_date  quotes_proforma_invoice = 2 limit 0,1";

							}
					}		

								$sql_invoice_list3 = $obj_Invoice->getInvoice('',$sql_invoice3);
	


					$maxrows = $rowcount_adv;
					$csvcolumnsname ='"","<b>Company Name</b>","Invoice No","Invoice Date","GST NO","Amount","IGST","CGST","SGST","Total"'."\n";
				
			 for($i=0;$i<$maxrows;$i++) 
         	{ 
         		$memberinvoicesid					= $sqllist2[$i]['memberinvoicesid'];
         		$companyid							= $sqllist2[$i]['billed_membercompanyid'];
         		$billing_membercompanyid		= $sqllist2[$i]['billing_membercompanyid'];
         		$statusdata							= $sqllist2[$i]['status'];
         		$invoiceno							= $sqllist2[$i]['invoiceno'];
         		//$InvoiceTotalAmount				= $sqllist[$i]['InvoiceTotalAmount'];
         		$invoicedate						= date("j M Y",strtotime($sqllist2[$i]['invoicedate']));
         		$invoicedatemonth						= date("M Y",strtotime($sqllist2[$i]['invoicedate']));

         		//date("M jS, Y", strtotime("2011-01-05"));
         		$duedate								= date("j M Y",strtotime($sqllist2[$i]['duedate']));
         		$billed_membername				= $sqllist2[$i]['billed_membername'];   
         		$TotalAmount 						= $sqllist2[$i]['TotalAmount']; //total payment by that invoice id
         		$approved							= $sqllist2[$i]['approved'];  //Invoice approve status
         			$InvoiceBaseAmount			= $sqllist2[$i]['InvoiceBaseAmount'];
					$InvoiceTotalAmount			= $sqllist2[$i]['InvoiceTotalAmount'];
					$inv_GST_no						=	$sqllist2[$i]['inv_GST_no'];
         	
					$is_mail_send						= $sqllist2[$i]['is_mail_send'];
     			  $balance1 							= $InvoiceTotalAmount - $TotalAmount;
         			$paid_total_amount				= $sqllist2[$i]['paid_total_amount'];
         			if($paid_total_amount == '0')
         			{
         		
         			 $balance1 							= $InvoiceTotalAmount - $TotalAmount;
         			}
         			else
         			{
         	
						$balance1 =  $InvoiceTotalAmount - $paid_total_amount;
         			}

         			$cgst_amount					= $sqllist2[$i]['cgst_amount'];
			
					$sgst_amount					= $sqllist2[$i]['sgst_amount'];
					$igst_amount					= $sqllist2[$i]['igst_amount'];
					$sgst_percentage 				= $sqllist2[$i]['sgst_percentage'];
					$cgst_percentage 				= $sqllist2[$i]['cgst_percentage'];
					$igst_percentage 				= $sqllist2[$i]['igst_percentage'];


         				$reportname = '"'.$heading.'"'."\n";
						//$csvtotalincome = "\n".'"","","","","","","","","Total Amount","'.$InvoiceTotalAmount1.'",""'."\n";
						$csvtotalincome = "\n".'"","","","","Total","'.$InvoiceBaseAmounts.'","'.$igst_amounts.'","'.$cgst_amounts.'","'.$sgst_amounts.'","'.$InvoiceTotalAmount1.'",""'."\n";
						
						$csvdata .= '"'.$invoicedatemonth.'","'.$billed_membername.'","'.$invoiceno.'","'.$invoicedate.'","'.$inv_GST_no.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						$paymentresultCSV = $reportname.$csvcolumnsname.$csvdata.$csvtotalincome;

       			 }

       		if($memberuserscompanyid > 0)
			{	 
       			if($day)
       			{
        			$sql_invoice1 = "select *,CONCAT(MONTHNAME(invoicedate),'-',YEAR(invoicedate)) as invoicedate_ym ,COUNT(*),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 InvoiceTotalAmount 
					from trninvoicetomember

					 where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_day and   quotes_proforma_invoice = 2 GROUP by MONTH(invoicedate)";
					 $heading1 = "GSTR 3 B";
				}
				else{
					$sql_invoice1 = "select *,CONCAT(MONTHNAME(invoicedate),'-',YEAR(invoicedate)) as invoicedate_ym ,COUNT(*),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 InvoiceTotalAmount 
					from trninvoicetomember

					 where memberid = $memberid   and billing_membercompanyid=$membercompanyid and isdeleted=0  and $where_date  quotes_proforma_invoice = 2 GROUP by MONTH(invoicedate)";
					 $heading1 = "GSTR 3 B";
				}
			}
			else
			{
					if($day)
       			{
        			$sql_invoice1 = "select *,CONCAT(MONTHNAME(invoicedate),'-',YEAR(invoicedate)) as invoicedate_ym ,COUNT(*),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 InvoiceTotalAmount 
					from trninvoicetomember

					 where memberid = $memberid and billing_membercompanyid=$membercompanyid and isdeleted=0 $where_day and   quotes_proforma_invoice = 2 GROUP by MONTH(invoicedate)";
					 $heading1 = "GSTR 3 B";
				}
				else{
					$sql_invoice1 = "select *,CONCAT(MONTHNAME(invoicedate),'-',YEAR(invoicedate)) as invoicedate_ym ,COUNT(*),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 InvoiceTotalAmount 
					from trninvoicetomember

					 where memberid = $memberid and billing_membercompanyid=$membercompanyid and isdeleted=0  and $where_date  quotes_proforma_invoice = 2 GROUP by MONTH(invoicedate)";
					 $heading1 = "GSTR 3 B";
				}
			}	
				//echo $sql_invoice1;
				$sql_invoice_list1 = $obj_Invoice->getInvoice('',$sql_invoice1);
	
				//$sqllist2 = $obj->getCompany('','',$sql2);
					$rowcount1 = count($sql_invoice_list1);
					if ($sql_invoice_list1)
					{
						$rowcount_adv1 = $rowcount1;
					}
					else
					{
						$rowcount_adv1=0;
					}
			
			$maxrows1 = $rowcount_adv1;
			$csvcolumnsname1 ='"","Amount","IGST","CGST","SGST","Total"'."\n";

			 for($i=0;$i<$maxrows1;$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'];
         		
         		$invoicedatemonth1						= date("M Y",strtotime($sql_invoice_list1[$i]['invoicedate_ym']));
         		//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'];

         				$reportname1 = '"'.$heading1.'"'."\n";
						//$csvtotalincome1 = "\n".'"","","","","Total Amount","'.$InvoiceTotalAmount1.'",""'."\n";
						$csvtotalincome1 = "\n".'"Total","'.$InvoiceBaseAmounts.'","'.$igst_amounts.'","'.$cgst_amounts.'","'.$sgst_amounts.'","'.$InvoiceTotalAmount1.'",""'."\n";
						
						$csvdata1 .= '"'.$invoicedatemonth1.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						$paymentresultCSV1 = $reportname1.$csvcolumnsname1.$csvdata1.$csvtotalincome1;

      			  }


      			  if($memberuserscompanyid > 0)
			{	 
       			if($day)
       			{
        			$sql_invoice_tally = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.MemberID,a.invoiceno,d.head_receipt_no,d.head_receipt_date,d.TotalAmount,e.bankname,d.tds,e.branch,e.transition_no,e.chequeno,e.mode_of_payment,e.transition_amount,e.chequeamount,e.transition_date,e.chequedate from mstmember x 
									inner join trninvoicetomember a on x.memberid = a.memberid
									inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
		
									inner join mststate c on x.head_Stateid = c.StateId
									inner join user_company_payment_header  d on a.memberinvoicesid = d.memberinvoicesid
									inner join user_company_payment_detail  e on d.memberpaymentheaderid = e.memberpaymentheaderid
					where x.memberid = $memberid $where_paymentmode   $where_day and   a.billing_membercompanyid=$membercompanyid and   a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		group by d.head_receipt_no
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned),d.head_receipt_no

					 ";
					// $heading1 = "GSTR 3 B";
					 $heading_tally = "Receipt Format Report For Tally";
				}
				else{
					$sql_invoice_tally = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.MemberID,a.invoiceno,d.head_receipt_no,d.head_receipt_date,d.TotalAmount,e.bankname,d.tds,e.branch,e.transition_no,e.chequeno,e.mode_of_payment,e.transition_amount,e.chequeamount,e.transition_date,e.chequedate  from mstmember x 
									inner join trninvoicetomember a on x.memberid = a.memberid
									inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
		
									inner join mststate c on x.head_Stateid = c.StateId
									inner join user_company_payment_header  d on a.memberinvoicesid = d.memberinvoicesid
									inner join user_company_payment_detail  e on d.memberpaymentheaderid = e.memberpaymentheaderid

					where x.memberid = $memberid $where_paymentmode  and $where_date  a.billing_membercompanyid=$membercompanyid and a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		group by d.head_receipt_no
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned),d.head_receipt_no";
					 //$heading1 = "GSTR 3 B";
					 $heading_tally = "Receipt Format Report For Tally";
				}
			}
			else
			{
					if($day)
       			{
        			$sql_invoice_tally = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.MemberID,a.invoiceno,d.head_receipt_no,d.head_receipt_date,d.TotalAmount,e.bankname,d.tds,e.branch,e.transition_no,e.chequeno,e.mode_of_payment,e.transition_amount,e.chequeamount,e.transition_date,e.chequedate from mstmember x 
									inner join trninvoicetomember a on x.memberid = a.memberid
									inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
		
									inner join mststate c on x.head_Stateid = c.StateId
									inner join user_company_payment_header  d on a.memberinvoicesid = d.memberinvoicesid
									inner join user_company_payment_detail  e on d.memberpaymentheaderid = e.memberpaymentheaderid

					 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid $where_paymentmode   $where_day and    a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		group by d.head_receipt_no
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned)";
					 //$heading1 = "GSTR 3 B";
					   $heading_tally = "Receipt Format Report For Tally";
				}
				else{
					$sql_invoice_tally = "select a.memberinvoicesid,a.billed_membercompanyid,a.billing_membercompanyid,a.MemberID,a.invoiceno,d.head_receipt_no,d.head_receipt_date,d.TotalAmount,e.bankname,d.tds,e.branch,e.transition_no,e.chequeno,e.mode_of_payment,e.transition_amount,e.chequeamount,e.transition_date,e.chequedate from mstmember x 
									inner join trninvoicetomember a on x.memberid = a.memberid
									inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
		
									inner join mststate c on x.head_Stateid = c.StateId
									inner join user_company_payment_header  d on a.memberinvoicesid = d.memberinvoicesid
									inner join user_company_payment_detail  e on d.memberpaymentheaderid = e.memberpaymentheaderid

					where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and  $where_paymentmode   $where_date a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  	group by d.head_receipt_no
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned),CAST(d.head_receipt_no AS unsigned)";
					// $heading1 = "GSTR 3 B";
					  $heading_tally = "Receipt Format Report For Tally";

				}
			}	
			//echo $sql_invoice_tally;
			//die();
					$invoicelist_tally = $obj_Invoice -> getInvoice('',$sql_invoice_tally);
						//print_r($companylist);	
					$rowcount_tally = count($invoicelist_tally);
					if ($invoicelist_tally)
					{
						$rowcount_adv_tally = $rowcount_tally;
					}
					else
					{
						$rowcount_adv_tally=0;
					}

					$maxrows_tally = $rowcount_adv_tally;
			$csvcolumnsname_tally ='"Sr.No","Receipt No","Receipt Date","Client Name","Invoice No","Receipt Amount","TDS","Transition No","Cheque No","Bank Name","Branch Name"'."\n";

			 for($i=0;$i<$maxrows_tally;$i++) 
         	{ 
         		$memberinvoicesid					= $invoicelist_tally[$i]['memberinvoicesid'];
         		$companyid							= $invoicelist_tally[$i]['billed_membercompanyid'];
         		$billing_membercompanyid		= $invoicelist_tally[$i]['billing_membercompanyid'];
         		
         		$MemberID								= 	$invoicelist_tally[$i]['MemberID'];
										$MemberName 							= 	str_replace("'","",$invoicelist_tally[$i]['billed_membername']);
										$invoiceno							= $invoicelist_tally[$i]['invoiceno'];
									 	//$invoicesrno							= 	$invoicelist_tally[$i]['invoicesrno'];
									 		$head_receipt_no					= $invoicelist_tally[$i]['head_receipt_no'];
									 	$head_receipt_date_for_check		=  $invoicelist_tally[$i]['head_receipt_date'];	
									 	$head_receipt_date 					= 	date("d-m-Y", strtotime($invoicelist_tally[$i]['head_receipt_date']));	
									 	$TotalAmount 							= 	$invoicelist_tally[$i]['TotalAmount'];
									 	$chequeno 								=	str_replace("'","",$invoicelist_tally[$i]['chequeno']);
										//$bankname 								= 	$invoicelist_tally[$i]['bankname'];
										//$branch 									= 	$invoicelist_tally[$i]['branch'];
										$bankname						= $invoicelist_tally[$i]['bankname'];
											$Totaltdsamount  				= $invoicelist_tally[$i]['tds'];
										$branch						= $invoicelist_tally[$i]['branch'];
										$counter									=	$i+1;
										$transition_no						= $invoicelist_tally[$i]['transition_no'];
										$paymentno						= $invoicelist_tally[$i]['chequeno'];
										$mode_of_payment  				= $invoicelist_tally[$i]['mode_of_payment']; 
										if($mode_of_payment == 'Online_Pay'){
         									
         									$chequeamount						= $invoicelist_tally[$i]['transition_amount'];
         									$payment_date						= date("d-m-Y",strtotime($invoicelist_tally[$i]['transition_date']));  
         									//$bankname						= '';
											//$branch						= '';

											}	
											else{
												
												$chequeamount						= $invoicelist_tally[$i]['chequeamount'];
												
												$payment_date						= date("d-m-Y",strtotime($invoicelist_tally[$i]['chequedate'])); 
											}


         				$reportname_tally = '"'.$heading_tally.'"'."\n";
						//$csvtotalincome1 = "\n".'"","","","","Total Amount","'.$InvoiceTotalAmount1.'",""'."\n";
					//	$csvtotalincome1 = "\n".'"Total","'.$InvoiceBaseAmounts.'","'.$igst_amounts.'","'.$cgst_amounts.'","'.$sgst_amounts.'","'.$InvoiceTotalAmount1.'",""'."\n";
						
					$csvdata_tally .= '"'.$counter.'","'.$head_receipt_no.'","'.$head_receipt_date.'","'.$MemberName.'","'.$invoiceno.'","'.$TotalAmount.'","'.$Totaltdsamount.'","'.$transition_no.'","'.$paymentno.'","'.$bankname.'","'.$branch.'"'."\n";
						//$paymentresultCSV = $reportname1.$csvcolumnsname1.$csvdata1.$csvtotalincome1;
						$paymentresultCSV2 = $reportname_tally.$csvcolumnsname_tally.$csvdata_tally;

      			  }
      			  if($memberuserscompanyid > 0)
					{	 
       					if($day)
       					{
       						$sql_invoice_tally1 = "SELECT c.memberid,invoicesrno,invoicedate,invoiceno,billed_membername,itemno,productname,product_description,ProductTotalAmount,igst_amount,cgst_amount,sgst_amount,inv_head_stateid,inv_StateName,inv_GST_no, InvoiceBaseAmount, InvoiceTotalAmount, companytype,(a.memberinvoicesid) as memberinvoicesid, (b.memberinvoicesid) as detailsmemberinvoicesid
							FROM trninvoicetomember a 
							inner join trninvoicetomemberdetails b on a.memberinvoicesid=b.memberinvoicesid
							inner join mstmember c on a.memberid=c.memberid
							where c.memberid = $memberid $where_paymentmode   $where_day and   a.billing_membercompanyid=$membercompanyid and   a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
					  		 $reportheader = 'Invoice List';
       					}
       					else
       					{
       						$sql_invoice_tally1 = "SELECT c.memberid,invoicesrno,invoicedate,invoiceno,billed_membername,itemno,productname,product_description,ProductTotalAmount,igst_amount,cgst_amount,sgst_amount,inv_head_stateid,inv_StateName,inv_GST_no, InvoiceBaseAmount, InvoiceTotalAmount, companytype,(a.memberinvoicesid) as memberinvoicesid, (b.memberinvoicesid) as detailsmemberinvoicesid
							FROM trninvoicetomember a 
							inner join trninvoicetomemberdetails b on a.memberinvoicesid=b.memberinvoicesid
							inner join mstmember c on a.memberid=c.memberid
							where c.memberid = $memberid $where_paymentmode  and  $where_date    a.billing_membercompanyid=$membercompanyid and   a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
					  		 $reportheader = 'Invoice List';

       					}
       				}
       				else
       				{
       					if($day)
       					{
       						$sql_invoice_tally1 = "SELECT cc.memberid,invoicesrno,invoicedate,invoiceno,billed_membername,itemno,productname,product_description,ProductTotalAmount,igst_amount,cgst_amount,sgst_amount,inv_head_stateid,inv_StateName,inv_GST_no, InvoiceBaseAmount, InvoiceTotalAmount, companytype,(a.memberinvoicesid) as memberinvoicesid, (b.memberinvoicesid) as detailsmemberinvoicesid
							FROM trninvoicetomember a 
							inner join trninvoicetomemberdetails b on a.memberinvoicesid=b.memberinvoicesid
							inner join mstmember c on a.memberid=c.memberid
							where c.memberid = $memberid and a.billing_membercompanyid=$membercompanyid $where_paymentmode   $where_day and   a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  		
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
					  		 $reportheader = 'Invoice List';
       					}
       					else
       					{

       						$sql_invoice_tally1 = "SELECT c.memberid,invoicesrno,invoicedate,invoiceno,billed_membername,itemno,productname,product_description,ProductTotalAmount,igst_amount,cgst_amount,sgst_amount,inv_head_stateid,inv_StateName,inv_GST_no, InvoiceBaseAmount, InvoiceTotalAmount, companytype,(a.memberinvoicesid) as memberinvoicesid, (b.memberinvoicesid) as detailsmemberinvoicesid
							FROM trninvoicetomember a 
							inner join trninvoicetomemberdetails b on a.memberinvoicesid=b.memberinvoicesid
							inner join mstmember c on a.memberid=c.memberid
							where c.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid $where_paymentmode  and  $where_date a.isdeleted= 0 and a.quotes_proforma_invoice = 2 
					  	
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
					  		 $reportheader = 'Invoice List';
       					}
       				}
//echo $sql_invoice_tally1;
//die();


       				$invoicelist_tally1 = $obj_Invoice -> getInvoice('',$sql_invoice_tally1);
						//print_r($companylist);	
					$rowcount_tally1 = count($invoicelist_tally1);
					if ($invoicelist_tally1)
					{
						$rowcount_adv_tally1 = $rowcount_tally1;
					}
					else
					{
						$rowcount_adv_tally1=0;
					}
										$maxrows_tally1 = $rowcount_adv_tally1;

					
					$maxrows_tally1 = $rowcount_adv_tally1;
			//$csvcolumnsname_tally ='"Sr.No","Receipt No","Receipt Date","Client Name","Invoice No","Receipt Amount","TDS","Transition No","Cheque No","Bank Name","Branch Name"'."\n";
	$csvcolumnsname4 ='"","<b>Company Name</b>","Invoice No","Invoice Date","GST NO","itemno","productname","product_description","ProductTotalAmount","Amount","IGST","CGST","SGST","Total"'."\n";
			 for($i=0;$i<$maxrows_tally1;$i++) 
         	{ 
         		$memberinvoicesid					= $invoicelist_tally1[$i]['memberinvoicesid'];
         		$detailsmemberinvoicesid					= $invoicelist_tally1[$i]['detailsmemberinvoicesid'];
         		
         		$companyid							= $invoicelist_tally1[$i]['billed_membercompanyid'];
         		$billing_membercompanyid		= $invoicelist_tally1[$i]['billing_membercompanyid'];
         		$statusdata							= $invoicelist_tally1[$i]['status'];
         		//$invoiceno							= $invoicelist_tally1[$i]['invoiceno'];
         		//$InvoiceTotalAmount				= $sqllist[$i]['InvoiceTotalAmount'];
         		$invoicesrno								= 	$invoicelist_tally1[$i]['invoiceno'];
				$InvoiceBaseAmount			= $invoicelist_tally1[$i]['InvoiceBaseAmount'];
         		$invoicedate						= date("j M Y",strtotime($invoicelist_tally1[$i]['invoicedate']));
         		$monthdetails						= date("M Y",strtotime($invoicelist_tally1[$i]['invoicedate']));

         		//date("M jS, Y", strtotime("2011-01-05"));
         		$duedate								= date("j M Y",strtotime($invoicelist_tally1[$i]['duedate']));
         		$billed_membername				= $invoicelist_tally1[$i]['billed_membername'];   
         		$TotalAmount 						= $invoicelist_tally1[$i]['TotalAmount']; //total payment by that invoice id
         		$approved							= $invoicelist_tally1[$i]['approved'];  //Invoice approve status
         		
					$InvoiceTotalAmount			= $invoicelist_tally1[$i]['InvoiceTotalAmount'];
					$inv_GST_no						=	$invoicelist_tally1[$i]['inv_GST_no'];
         			
					$itemno						=	$invoicelist_tally1[$i]['itemno'];
					$productname						=	$invoicelist_tally1[$i]['productname'];
					$product_description						=	$invoicelist_tally1[$i]['product_description'];
					$ProductTotalAmount						=	$invoicelist_tally1[$i]['ProductTotalAmount'];


					$is_mail_send						= $invoicelist_tally1[$i]['is_mail_send'];
     			  $balance1 							= $InvoiceTotalAmount - $TotalAmount;
         			$paid_total_amount				= $invoicelist_tally1[$i]['paid_total_amount'];
         			if($paid_total_amount == '0')
         			{
         		
         			 $balance1 							= $InvoiceTotalAmount - $TotalAmount;
         			}
         			else
         			{
         	
						$balance1 =  $InvoiceTotalAmount - $paid_total_amount;
         			}

         			$cgst_amount					= $invoicelist_tally1[$i]['cgst_amount'];
			
					$sgst_amount					= $invoicelist_tally1[$i]['sgst_amount'];
					$igst_amount					= $invoicelist_tally1[$i]['igst_amount'];
					$sgst_percentage 				= $invoicelist_tally1[$i]['sgst_percentage'];
					$cgst_percentage 				= $invoicelist_tally1[$i]['cgst_percentage'];
					$igst_percentage 				= $invoicelist_tally1[$i]['igst_percentage'];
					$invoicesrno_for_display 			= 	$invoicesrno;
					$invoicedatemonthdetails= 	$monthdetails;

         				$reportname4 = '"'.$heading.'"'."\n";
						//$csvtotalincome = "\n".'"","","","","","","","","Total Amount","'.$InvoiceTotalAmount1.'",""'."\n";
						
         		
						$csvtotalincome4 = "\n".'"","","","","Total","'.$InvoiceBaseAmounts.'","'.$igst_amounts.'","'.$cgst_amounts.'","'.$sgst_amounts.'","'.$InvoiceTotalAmount1.'",""'."\n";
						
					if($invoicesrno_for_display == $invoiceno)
					{
						//$csvdata4 .= '"","","","","","'.$itemno.'","'.$productname.'","'.$product_description.'","'.$ProductTotalAmount.'","","","","",""'."\n".'"","","","","","","","Total","'.$InvoiceBaseAmount.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						$csvdata4 .= '"","","","","","'.$itemno.'","'.$productname.'","'.$product_description.'","'.$ProductTotalAmount.'","","","","",""'."\n";
					}
					else
					{
						if($invoicedatemonthdetails == $invoicedatemonth)
						{

							$csvdata4 .= '"","'.$billed_membername.'","'.$invoicesrno_for_display.'","'.$invoicedate.'","'.$inv_GST_no.'","'.$itemno.'","'.$productname.'","'.$product_description.'","'.$ProductTotalAmount.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						}
						else
						{

							$csvdata4 .= '"'.$invoicedatemonthdetails.'","'.$billed_membername.'","'.$invoicesrno_for_display.'","'.$invoicedate.'","'.$inv_GST_no.'","'.$itemno.'","'.$productname.'","'.$product_description.'","'.$ProductTotalAmount.'","'.$InvoiceBaseAmount.'","'.$igst_amount.'","'.$cgst_amount.'","'.$sgst_amount.'","'.$InvoiceTotalAmount.'"'."\n";
						}
					}
					$invoiceno = $invoicesrno_for_display;
					$invoicedatemonth = $invoicedatemonthdetails;
					
						$searchresultCSV = $reportheader.$csvcolumnsname4.$csvdata4;
					

       			 }

				
      			if($memberuserscompanyid > 0)
				{
					  $sql1 = "select MONTHNAME(CURRENT_DATE()) as This_monthname,MONTHNAME(CURRENT_DATE())-1 as Last_monthname,MONTH(CURRENT_DATE()) as This_month,MONTH(CURRENT_DATE())-1 as Last_month,CURRENT_DATE() as today_date,CURRENT_DATE() -1 as Yesterday_date,MONTH(CURRENT_DATE())+1 as This_month_quter2,MONTH(CURRENT_DATE())+2 as This_month_quter3,MONTH(CURRENT_DATE())-2 as Last_month_quter2,MONTH(CURRENT_DATE())-3 as Last_month_quter3,MONTH(CURRENT_DATE())-4 as Last_month_quter4,YEAR(CURDATE())-1 as last_year,YEAR(CURDATE()) as This_year,YEAR(CURDATE())+1 as next_year
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		  inner join trnuser_company c on x.memberid = c.memberid
					  		 where x.memberid = $memberid  and a.billing_membercompanyid=$membercompanyid and c.memberuserscompanyid = $memberuserscompanyid and a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid";

				}
				else
				{
					  $sql1 = "select MONTHNAME(CURRENT_DATE()) as This_monthname,MONTHNAME(CURRENT_DATE())-1 as Last_monthname,MONTH(CURRENT_DATE()) as This_month,MONTH(CURRENT_DATE())-1 as Last_month,CURRENT_DATE() as today_date,CURRENT_DATE() -1 as Yesterday_date,MONTH(CURRENT_DATE())+1 as This_month_quter2,MONTH(CURRENT_DATE())+2 as This_month_quter3,MONTH(CURRENT_DATE())-2 as Last_month_quter2,MONTH(CURRENT_DATE())-3 as Last_month_quter3,MONTH(CURRENT_DATE())-4 as Last_month_quter4,YEAR(CURDATE())-1 as last_year,YEAR(CURDATE()) as This_year,YEAR(CURDATE())+1 as next_year
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 where x.memberid = $memberid and a.billing_membercompanyid=$membercompanyid and a.isdeleted= 0 and quotes_proforma_invoice = 2 
					  		 group by a.memberinvoicesid";
				}	  		 
					//echo $sql;
					//die();
					$sqllist1 = $obj->getCompany('','',$sql1);


					$today_date						= date("d-M",strtotime($sqllist1[0]['today_date']));
					$Yesterday_date						= date("d-M",strtotime($sqllist1[0]['Yesterday_date']));
					$This_month						= $sqllist1[0]['This_month'];
					 $This_monthname = date("M", mktime(0, 0, 0, $This_month, 10));
					$Last_month						= $sqllist1[0]['Last_month'];
					$Last_month						= $sqllist1[0]['Last_month'];
					 $Last_monthname = date("M", mktime(0, 0, 0, $Last_month, 10));
					//$This_monthname						= $sqllist1[0]['This_monthname'];
					 $last_year						= $sqllist1[0]['last_year'];
					$This_year						= $sqllist1[0]['This_year'];
					$next_year						= $sqllist1[0]['next_year'];
					$This_month_quter2						= $sqllist1[0]['This_month_quter2'];
					$This_quter_monthname2 = date("M", mktime(0, 0, 0, $This_month_quter2, 10));
					$This_month_quter3						= $sqllist1[0]['This_month_quter3'];
					$This_quter_monthname3 = date("M", mktime(0, 0, 0, $This_month_quter3, 10));
					$Last_month_quter2						= $sqllist1[0]['Last_month_quter2'];
					$Last_quter_monthname2 = date("M", mktime(0, 0, 0, $Last_month_quter2, 10));
					$Last_month_quter3						= $sqllist1[0]['Last_month_quter3'];
					$Last_quter_monthname3 = date("M", mktime(0, 0, 0, $Last_month_quter3, 10));
					$Last_month_quter1						= $sqllist1[0]['Last_month_quter4'];
					$Last_quter_monthname1 = date("M", mktime(0, 0, 0, $Last_month_quter1, 10));

					if($This_month == '1')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '2')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '3')
					{
						$contain="Jan-Feb-Mar";
					}
					if($This_month == '4')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '5')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '6')
					{
						$contain="Apr-May-Jun";
					}
					if($This_month == '7')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '8')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '9')
					{
						$contain="Jul-Aug-Sept";
					}
					if($This_month == '10')
					{
						$contain="Oct-Nov-Dec";
					}
					if($This_month == '11')
					{
						$contain="Oct-Nov-Dec";
					}
					if($This_month == '12')
					{
						$contain="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '1')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '2')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '3')
					{
						$contain1="Jan-Feb-Mar";
					}
					if($Last_month_quter3 == '4')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '5')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '6')
					{
						$contain1="Apr-May-Jun";
					}
					if($Last_month_quter3 == '7')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '8')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '9')
					{
						$contain1="Jul-Aug-Sept";
					}
					if($Last_month_quter3 == '10')
					{
						$contain1="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '11')
					{
						$contain1="Oct-Nov-Dec";
					}
					if($Last_month_quter3 == '12')
					{
						$contain1="Oct-Nov-Dec";
					}

					//echo $This_month;
					//echo $Last_month;
					//echo $This_monthname;
					//echo $Last_monthname;
					// Active members 
				if($memberuserscompanyid > 0)
				{	
					$sql1 = "select * from user_company where memberid = $memberid and membercompanyid=$membercompanyid and memberapprovalstatus = 1";
				}
				else
				{
					$sql1 = "select * from user_company where memberid = $memberid and memberapprovalstatus = 1";
				}	
					$Activesqllist = $obj->getCompany('','',$sql1);
					if(is_array($Activesqllist))
					{
						$Activerowcount = count($Activesqllist);
					}
				
					// Inactive members 
				if($memberuserscompanyid > 0)
				{		
					$sql2 = "select * from user_company where memberid = $memberid and membercompanyid=$membercompanyid and memberapprovalstatus = 0";
				}
				else
				{
					$sql2 = "select * from user_company where memberid = $memberid and memberapprovalstatus = 0";
				}	
					$Inactivesqllist = $obj->getCompany('','',$sql2);
					if(is_array($Inactivesqllist))
					{
						$Inactiverowcount = count($Inactivesqllist);
					}
					
					
				if($memberuserscompanyid > 0)
				{	
				  $sql_payment = "SELECT memberinvoicesid,SUM(TotalAmount) as paid_total_amount
									FROM user_company_payment_header GROUP BY  memberinvoicesid";

				}
				else
				{
					$sql_payment = "SELECT memberinvoicesid,SUM(TotalAmount) as paid_total_amount
									FROM user_company_payment_header GROUP BY  memberinvoicesid";
				}				
									
               $sql_paymentlist = $obj->getCompany('','',$sql_payment);
					if(is_array($sql_paymentlist))
					{
						$payment_rowcount = count($sql_paymentlist);
					}
					//$TotalAmount1 = 0;
					 for($i=0;$i<$payment_rowcount;$i++) 
         		 { 
         	 		 $memberinvoicesid				= $sql_paymentlist[$i]['memberinvoicesid'];
         		 	 $invoicearry['paid_total_amount']				=             $sql_paymentlist[$i]['paid_total_amount'];
         			 $where ="memberinvoicesid =$memberinvoicesid";
						 $update_invoice = $obj_Invoice ->EditInvoice($invoicearry,$where);
      			}
		}
 	
		require_once("header1.html");
		require_once("leftnav.html");
		require_once("viewInvoice1.html");
		require_once("footer1.html");
}
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;
}   



	
	
?>
