<?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']);

$query="select c.head_Email,c.panno,a.billed_membername,a.duedate,a.billing_membercompanyid,a.status,a.invoiceno,a.InvoiceTotalAmount,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,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_proforma and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid_proforma";
             	
		$querylist = $obj_proforma ->getInvoice('',$query);
		$rowtoproforma 		=count($querylist);
			$counter = 0;
	
	//echo '<pre>';
//print_r($querylist);
	

		for($i=0;$i<$rowtoproforma;$i++){
			
				 $counter++;
			$memberid										= 	$memberid_proforma;
			$memberinvoicesid								= 	$memberinvoicesid_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'].'</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									= $querylist[$i]['igst_amount'];
			$cgst_percentage								= $querylist[$i]['cgst_percentage'];
			$cgst_amount									= $querylist[$i]['cgst_amount'];
			$sgst_percentage								= $querylist[$i]['sgst_percentage'];
			$sgst_amount									= $querylist[$i]['sgst_amount'];
			$ugst_percentage								= $querylist[$i]['ugst_percentage'];
			$ugst_amount									= $querylist[$i]['ugst_amount'];
			$discount_percentage							= $querylist[$i]['discount_percentage'];
			$discount_amount								= number_format($querylist[$i]['discount_amount']);
			$InvoiceBaseAmountBeforeDiscount			= $querylist[$i]['InvoiceBaseAmountBeforeDiscount'];
			$InvoiceBaseAmount							= number_format($querylist[$i]['InvoiceBaseAmount']);
			$InvoiceTotalAmount							= number_format($querylist[$i]['InvoiceTotalAmount']);
			$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);
					
			
								$productarray .='<tr>';
								$productarray	.='<td style="padding-top:20px;">'.$counter.'</td>';
								$productarray	.='<td><div style="height:200px"><b>'.$productname.'</b>';
								$productarray	.='<br><br><b>'.$product_description.'</b></div><br>';
								$productarray	.='</td>';
								$productarray	.='<td valign="top"><br>'.$ProductBaseAmount.'</td>';
								$productarray	.=	'<td valign="top"><br>'.$quantity.'</td>';
								$productarray	.='<td valign="top"><br>'.$ProductBaseAmount.'</td>';
								$productarray	.=	'</tr>	';	
							}
					if($igst_amount == '')
									{
									$row_gst .='<tr>';
									$row_gst .='<td colspan="2" style="padding-top:15px;">CGST 9%</td>';
									$row_gst .='<td style="padding-top:15px;">'.$cgst_amount.'</td>';
									$row_gst .='</tr>';
									$row_gst .='<tr>';
									$row_gst .='<td colspan="2" style="padding-top:15px;">SGST 9%</td>';
									$row_gst .='<td style="padding-top:15px;">'.$sgst_amount.'</td>';
									$row_gst .='</tr>';
									}
									else {
									
										$row_gst .='<tr>';
										$row_gst .='<td colspan="2" style="padding-top:15px;">IGST 18%</td>';
										$row_gst .='<td style="padding-top:15px;">'.$igst_amount.'</td>';
										$row_gst .='</tr>';
										
									}
									
									if($discount_amount != '') {
									$row_discount .='<tr>';
									$row_discount .='<td colspan="2"><b>Less 36000/- discount (1 yr. Membership Free)</b></td>';
									$row_discount .='<td colspan="2"><b>DISCOUNT </b></td>';
									$row_discount .='<td><b>'.$discount_amount.'</b></td>';
									$row_discount .='</tr>';
								}
								else {
									$row_discount .='';
									}
		//die();		
			$replace = array(
					"[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
					/*"[header_image]"								=> $header_image,
					"[regionalofficeaddress]"					=>	$regionalofficeaddress,
					"[issue_date]"									=>	$issue_date*/
					);
				
						
		$dir = GLOBAL_INCLUDE_PATH."htmltopdf";
			
		$ManTemplateFile = "proforma_template_new.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;
		?>