<?php
		require_once("../include/config.php");
		require_once("../include/startup.php");
	
	
		$obj_proforma								= new Invoice_frproforma();
if (!headers_sent($filename, $linenum)) {
			header('Content-type: application/pdf');					
		} else {
			echo "Headers already sent in $filename on line $linenum\n" .
 			"Cannot redirect, for now please click this <a ";
 				exit;
		}


		$memberid_proforma 						= intval($_GET['memberid']);
		$memberinvoicesid_proforma 			= intval($_GET['memberinvoicesid']);
		$companyid_proforma						= intval($_GET['companyid']);
		$billing_membercompanyid_proforma 	= intval($_GET['billing_membercompanyid']);
		$memberpaymentheaderid					= intval($_GET['memberpaymentheaderid']);

/*$query="select * from mstmember x inner join trninvoicetomember a on x.memberid = a.memberid 
					inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid 
					
					inner join user_company_payment_header c on a.memberinvoicesid = c.memberinvoicesid 
					inner join user_company_payment_detail d on c.memberpaymentheaderid = d.memberpaymentheaderid 
					where  c.memberpaymentheaderid=$memberpaymentheaderid GROUP by c.memberpaymentheaderid ";*/
					
$query="select c.memberpaymentheaderid,c.billed_membercompanyid,c.head_receipt_no,c.head_receipt_date,
				c.memberinvoicesid,
			f.upload_logo,f.upload_header,f.companyname,f.gstno,f.panno,
			f.Addline1,f.Addline2,f.head_CityName,f.head_StateidName,f.Country,f.head_Pin,f.head_TelNo,f.head_Email,
			d.mode_of_payment,d.chequeno,d.chequedate,d.chequeamount,d.bankname,d.branch,
			a.memberinvoicesid,a.billing_membercompanyid,a.billed_membername,a.inv_GST_no,a.inv_Add1,a.inv_Add2,
			a.inv_CityName,a.inv_Pin,a.inv_State,a.inv_StateName,a.inv_Country,a.invoiceno,a.invoicedate
			from mstmember x 
			inner join trninvoicetomember a on x.memberid = a.memberid 
			
			inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid 
			 INNER join user_company f on x.memberid = f.memberid
			inner join user_company_payment_header c on a.memberinvoicesid = c.memberinvoicesid 
			inner join user_company_payment_detail d on c.memberpaymentheaderid = d.memberpaymentheaderid 
			where  c.memberpaymentheaderid=$memberpaymentheaderid GROUP by c.memberpaymentheaderid";
           	
		$querylist = $obj_proforma ->getInvoice('',$query);
		 $rowtoproforma 		=count($querylist);
			$counter = 0;
			$igst_amount = 0;
	//die();
	//echo '<pre>';
//print_r($querylist);
	

		
		for($i=0;$i<$rowtoproforma;$i++)
		{
			
				 $counter++;
			
			$memberinvoicesid								= 	$memberinvoicesid_proforma;	
			$memberid										= 	$memberid_proforma;
			$billed_membercompanyid						=	$querylist[$i]['billed_membercompanyid'];
			$billing_membercompanyid					= $billing_membercompanyid_proforma;
			$billed_membername 							= $querylist[$i]['billed_membername'];
			$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'].''.$querylist[$i]['inv_Add2'].'</br>'.$querylist[$i]['inv_CityName'].','.$querylist[$i]['inv_StateName'].'-'.$querylist[$i]['inv_Pin'];
			$panno											= $querylist[$i]['panno'];
			$gstno											= $querylist[$i]['gstno'];
			
			$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'];
			$head_receipt_no								= $querylist[$i]['head_receipt_no'];
			$head_receipt_date							= checkIfNullDate(date("d-m-Y",strtotime($querylist[$i]['head_receipt_date'])));
			$chequeamount									= $querylist[$i]['chequeamount'];
			$chequeamount1									= number_to_words($chequeamount);
			$chequeno										= $querylist[$i]['chequeno'];
			$bankname										= $querylist[$i]['bankname'];
			$chequedate										= checkIfNullDate(date("d-m-Y",strtotime($querylist[$i]['chequedate'])));
			$mode_of_payment								= $querylist[$i]['mode_of_payment'];
			$Addline1										= $querylist[$i]['Addline1'];
			$Addline2										= $querylist[$i]['Addline2'];
			$head_CityName									= $querylist[$i]['head_CityName'];
			$Country											= $querylist[$i]['Country'];
			$head_Pin										= $querylist[$i]['head_Pin'];
			$head_TelNo										= $querylist[$i]['head_TelNo'];
			$head_Email										= $querylist[$i]['head_Email'];
			$upload_logo_display 						= $querylist[$i]['upload_logo'];
			$upload_header_display  					= $querylist[$i]['upload_header'];
			$companyname 									= $querylist[$i]['companyname'];
			
			
			$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>';
					}
			}
			
						
									
		}
					
									
								
	
			$replace = array(
			
					"[chequeamount]"						=> $chequeamount,
					"[chequeamount1]"						=>$chequeamount1,
					"[chequeno]"							=> $chequeno,
					"[bankname]"							=> $bankname,
					"[chequedate]"							=> $chequedate,
					"[mode_of_payment]"					=> $mode_of_payment,	
			
					"[productname]"								=> $productname,
					"[billed_membername]"						=> $billed_membername,
					"[ProductBaseAmount]"						=> $ProductBaseAmount,
					"[InvoiceTotalAmount]"						=> $InvoiceTotalAmount,
					"[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,
					"[company_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,
			       "[head_receipt_no]"							=>$head_receipt_no,
			       "[head_receipt_date]"						=>$head_receipt_date,
			        "[header_display]"							=> $header_display,
			       "[companyname]"								=> $companyname
				
					);
				
						
		$dir = GLOBAL_INCLUDE_PATH."htmltopdf";
			
		$ManTemplateFile = "invoice_template_receipt.php";	
		ob_start();						
		require_once($ManTemplateFile);
		$pdf_html_man = ob_get_contents();
		ob_end_clean();
		require_once($dir.'/dompdf/dompdf_config.inc.php');
		$pdf_html =  strtr($pdf_html_man,$replace);					
		$dompdf = new DOMPDF(); // Create new instance of dompdf
		$dompdf->load_html($pdf_html); // Load the html
		$dompdf->render(); // Parse the html, convert to PDF
					
		$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
				
		$dompdf -> __destruct();
		unset($dompdf);
		
		echo $pdf_content;
		?>