<?php 

session_start();

if(!$_SESSION['userId']) {
	header('location: ../../index.php');	
} 
?>
<?php



	function generateRow(){
		$contents = '';
		include_once('../connection.php');
		$id = $_GET['id'];
		$idd = $_GET['idd'];
		$sql = "SELECT  vendor_name,invoice_number,description,'' as quantity, '' as debit,paid_amount as paid, paid_date as date,id FROM padmanaba_vendor_accounts where  invoice_number = '$id' and vendor_name ='$idd' and paid_amount >'0' union SELECT  vendor_name,invoice_number,description,quantity,debit_amount,'' as paid,debit_date as date,id FROM padmanaba_vendor_debit where  invoice_number = '$id' and vendor_name ='$idd' and debit_amount >'0' union select '' as vendor_name, '' as invoice_number, '' as description,'' as quantity,'' as debit,sum(paid_amount) as paid,'TOTAL PAID' as date,'' as id from padmanaba_vendor_accounts where  invoice_number = '$id' and vendor_name ='$idd' and paid_amount >'0' union select '' as vendor_name, '' as invoice_number, '' as description,'' as quantity,sum(debit_amount) as debit,'' as paid,'TOTAL DEBIT' as date,'' as id from padmanaba_vendor_debit where  invoice_number = '$id' and vendor_name ='$idd' and debit_amount >'0' union select '' as vendor_name, '' as invoice_number, '' as description,'' as quantity,'' as debit,(cost-debit_amount) as paid,'COST OF BILL' as date,'' as id from padmanaba_invoices where  invoice_number = '$id' and vendor_name ='$idd' union select vendor_name as vendor_name, invoice_number as invoice_number, '' as description,'' as quantity,'' as debit,((cost-debit_amount)-paid_amount) as paid,'COST TO PAY' as date,'' as id from padmanaba_invoices where  invoice_number = '$id' and vendor_name ='$idd'";

		//use for MySQLi OOP
		/*$query = $conn->query($sql);
		while($row = $query->fetch_assoc()){
			$contents .= "
			<tr>
				<td>".$row['id']."</td>
				<td>".$row['firstname']."</td>
				<td>".$row['lastname']."</td>
				<td>".$row['address']."</td>
			</tr>
			";
		}*/
		////////////////

		// use for MySQLi Procedural
		 $query = mysqli_query($conn, $sql);
		 while($row = mysqli_fetch_assoc($query)){
		 	$contents .= "
		 	<tr>
		 	
		 		<td>".$row['vendor_name']."</td>
		 		<td>".$row['invoice_number']."</td>
		 		<td>".$row['description']."</td>
				<td>".$row['quantity']."</td>
				<td>".$row['date']."</td>
				<td>".$row['debit']."</td>
				<td>".$row['paid']."</td>
			</tr>
		 	";
		 }
		////////////////
		
		return $contents;
	}
	

	require_once('../tcpdf/tcpdf.php');  
    $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
    $pdf->SetCreator(PDF_CREATOR);  
    $pdf->SetTitle("Available stocks report");  
    $pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
    $pdf->SetDefaultMonospacedFont('helvetica');  
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
    $pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
    $pdf->setPrintHeader(false);  
    $pdf->setPrintFooter(false);  
    $pdf->SetAutoPageBreak(TRUE, 10);  
    $pdf->SetFont('helvetica', '', 11);  
    $pdf->AddPage();  
    $content = '';  
    $content .= '
      	<p style="text-align:center;"><img src="../../desk.padmanaba.in/tcpdf/padmanaba.jpg" alt="Logo" width="200" height="80"></p>
		<b> DATE: <?php echo $date; ?> </b>
      	<h4 align="center">Vendor Payment Details</h4>
      	<h4>Vendor payment Details</h4>
      	<table border="1" cellspacing="0" cellpadding="3"> 
           <tr>  
                
				<th width="10%">Vendor Name</th>
				<th width="10%">Invoice Number</th>
				<th width="22%">Description</th> 
				<th width="10%">Quantity</th>
				<th width="22%">Debited Date</th>
				<th width="12%">DEBIT</th>
				<th width="12%">PAID</th>

           </tr>  
      ';  
    
    $content .= generateRow();  
    $content .= '</table>'; 
    $pdf->writeHTML($content);  
    $pdf->Output('payments_from_padmanaba.pdf', 'I');
	

?>
