<?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,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,d.Bankname,d.BankAC,d.Branch,d.IFSC_Code,a.cgst_percentage,a.sgst_percentage,a.igst_percentage,b.* 
					from mstmember x
				  	inner join trninvoicetomember_frproforma a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails_frproforma 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 = 1 and a.memberinvoicesid = $memberinvoicesid_proforma";
         		
		$querylist = $obj_proforma ->getInvoice('',$query);
		$rowtoproforma 		=count($querylist);
			$counter = 0;

		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									= 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']);
			$InvoiceTotalAmount1							= $querylist[$i]['InvoiceTotalAmount'];
			$InvoiceSubTotalAmount						= format_number($querylist[$i]['InvoiceSubTotalAmount']);
			$quantity										= $querylist[$i]['quantity'];
			//$discount_amount								= $querylist[$i]['discount_amount'];
			//$price											= $querylist[$i]['InvoiceTotalAmount'];
			$ProductBaseAmount							= format_number($querylist[$i]['ProductBaseAmount']);
			$ProductTotalAmount							= format_number($querylist[$i]['ProductTotalAmount']);
			$productname									= $querylist[$i]['productname'];
			$product_description								= str_replace("\r\n", "<br>", $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							= ucwords(number_to_words($InvoiceTotalAmount1));
			if($rowtoproforma >= 2)
			{
				
				$data_design = 'style="height:auto;"';
			}
			else
			{
				$data_design = 'style="height:auto"';
				
			}
					
				$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'];
		$BankName = $querylist[0]['Bankname'];
		$Bank_ACno      = $querylist[0]['BankAC'];
	    $Branch  		= $querylist[0]['Branch'];
		$IFSC_Code    = $querylist[0]['IFSC_Code'];
		
					if($igst_amount == '')
		{

				$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>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>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>';
					}
			}	
		if($inv_GST_no == 'GST Not applicable')
		{
					
			
				$contain_gstno = ' ';					
								
		}
		else
		{
				$contain_gstno = ' GST No:'.$inv_GST_no.'';
		}				
			
			$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]"                         =>	$contain_gstno,
					"[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
			       
					/*"[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');
		//require_once($dir.'/dompdf/autoload.inc.php');
 		$pdf_html =  strtr($pdf_html_man,$replace);	
		
		//use Dompdf\Dompdf;	
		
		$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;
		?>