<?php
	$customerno = $_GET["i"];
			
			//CUSTOMER DETAILS
			$q = "select * from register where customerno='$customerno'";
			$r = mysql_query($q, $con);
			$crows = mysql_fetch_array($r);
			
			$fullname = $crows["surname"]." ".$crows["othernames"];
			$fullname = strtoupper($fullname);
	
	
	$sqll="select * from payments where customer='$customerno'";
	$result = mysql_query($sqll, $con);
	
	$counter = mysql_num_rows($result);
	
	
	if($counter !=0){
		echo "<div name='myarea' id='myarea'>";
		echo "<table width='100%' border='0' cellpadding='5px'>";
		echo "<tr><td width='20%'> <th><center> <img src='images/dusakheader.png' width='100%'><h4> $fullname INVENTORY RECORDS </h4></center> <th width='20%'>&nbsp;</tr>";
		echo "</table>";
		
		
			echo "<table width='100%' border='1' cellpadding='10px'>";
				echo "<tr><th align='center'> S/N  <th> <center>DATE</center> <th align='left'> CASH ADVANCE <th align='left'> BAGS   <th align='left'> WEIGHT (KG)<th align='left'> PRICE (KG) <th align='left'> TOTAL PRICE (<del>N</del>)<th align='left'> BALANCE (<del>N</del>)  <th align='left'> DEBIT (<del>N</del>)<th align='left'> STATUS </tr>";
	
	$sn=1;
	$ttotalprice = 0;
	while($rows = mysql_fetch_array($result))
	  {
		extract($rows);
		$ttotalprice += $totalprice;
		echo "<tr><td align='center'> $sn <td align='center'> $date <td> ".number_format($credit, 2)." <td> $bags <td> $weight <td> ".number_format($price, 2)." <td> ".number_format($totalprice, 2)."<td>".number_format($balance, 2)." <td> ".number_format($debit, 2)." <td> "; 
				
				//get status
				if($debit > 0)
				{
					//check debit table
					$q_status = "select * from debt where date='$date' and customerno='$customer' and amount='$debit'";
					$r_status = mysql_query($q_status, $con);
					$countstatus = mysql_num_rows($r_status);
					if($countstatus > 0)
					{
					while($rowstatus = mysql_fetch_array($r_status)){ $status = $rowstatus['status']; }
							
					}else{ $status = "clear"; }
				}
				
				if($balance > 0)
				{
					//check balance table
					$q_status = "select * from balance where date='$date' and customerno='$customer' and amount='$balance'";
					$r_status = mysql_query($q_status, $con);
					$countstatus = mysql_num_rows($r_status);
					if($countstatus > 0)
					{
					while($rowstatus = mysql_fetch_array($r_status)){ $status = $rowstatus['status']; }
							
					}else{ $status = "clear"; }
				}
				
				
				echo "$status </tr>";
		$sn++;
	  }
	  
		echo "</table>";
	  
	  echo "<table width='100%'>";
	  echo "<tr><td width='70%'>";
	  
		  echo "<br><br><table>";
		   echo "<tr><td> Company's Approval: _____________________ &nbsp; &nbsp; <td> Date: _____________ </tr>";
		   echo "<tr><td><br> Customer's Signature: _____________________ &nbsp; &nbsp; <td><br> Date: _____________ </tr>";
		  echo "</table>";
		
	  echo "<td>";
			
		//get advance history
		$sqll="select * from advancepayhistory where customer='$customerno'";
		$result = mysql_query($sqll, $con);
		
		$counter = mysql_num_rows($result);
		$totaladvance = 0;
		
		if($counter != 0){
			while($crows = mysql_fetch_array($result))
			{
				extract($crows);
				$totaladvance += $amount;
			}
		}
			
			echo "<table width='100%' border='2'>";
			echo "<tr><td><b>TOTAL AMOUNT 	<td>	<del>N</del>".number_format($ttotalprice, 2)."</tr>";
			echo "<tr><td><b>TOTAL ADVANCE 	<td>  	<del>N</del>".number_format($totaladvance, 2)." </tr>";
			
			//get clear balance 
			$totalclear = 0;
			$q = "select * from balance where customerno='$customerno' and status='clear'";
			$r = mysql_query($q, $con);
			$countclear = mysql_num_rows($r);
			if($countclear>0)
			{
				while($rows = mysql_fetch_array($r))
				{
					$totalclear += $amount; 
				}
			}
			////////////
			
			
			$dif =  $ttotalprice - $totaladvance - $totalclear;
			$dif =  abs($dif);
			
			echo "<tr><td><b>DIFFERENCE <td>  <del>N</del>".number_format($dif, 2)." </tr>";
			echo "</table>";
			
			
	 echo "</tr>";
	 echo "</table>";
	
	
	
	 echo "</div>";
	 echo "<button name='btn' id='btn' onclick='printform()'>Print</button>";
		
	}else{
		echo "<font color='red'><h2 align='center'>NO PAYMENT FOR THIS CUSTOMER </h2></font>";
	}
	
	?>
	
	
<script>
function printform(){    
   var originalContents = document.body.innerHTML;
   document.getElementById('btn').style.display="none";
     var printContents = document.getElementById('myarea').innerHTML;      
   document.body.innerHTML = printContents;   
   window.print();     
   document.body.innerHTML = originalContents;
}
</script>