<?php







include 'connect.php';







$supplier=$_GET['supplier'];







		$sl=mysqli_query($conn,"select sum(total) as amount from purchase where supplier='$supplier'");



		$sl1=mysqli_fetch_assoc($sl);



		



		$re=mysqli_query($conn,"select sum(amount) as amount from payment where supplier='$supplier'");



		$re1=mysqli_fetch_assoc($re);



		



		$obl=mysqli_query($conn,"select * from creditor_ledger where supplier='$supplier'");



		$num=mysqli_num_rows($obl);



		$obl1=mysqli_fetch_assoc($obl);



		



		$credit=0;



		$debit=0;



		if($obl1['paymenttype']=='credit')



		{



		$credit=$obl1['amount'];



		$debit=0;



		}



		elseif($obl1['paymenttype']=='debit')



		{



		$credit=0;



		$debit=$obl1['amount'];



		}



		$adjs=0;

		$loi=mysqli_query($conn,"select * from purchase where supplier='$supplier'");

		while($loi1=mysqli_fetch_assoc($loi))

		{

			$adjs=$adjs+$loi1['adjustment'];

		}





		//echo"$credit & $debit";

		

		

		$dnt=mysqli_query($conn,"select sum(amount) as amount from debit_note where supplier='$supplier'");



		$dnt1=mysqli_fetch_assoc($dnt);



		



		$deb=$sl1['amount']+$debit+$adjs;



		$cre=$re1['amount']+$credit+$dnt1['amount'];



		



		$cbl=$sl1['amount']+$adjs-$re1['amount']-$dnt1['amount']-$credit+$debit;



		



		/*if($cbl!=0)



		{*/



		



		if($deb!='0' && $deb!='')



		{



		$deb=number_format($deb,2,'.','');



		}



		else



		{



		$deb='';



		}



		



		if($cre!='0' && $cre!='')



		{



		$cre=number_format($cre,2,'.','');



		}



		else



		{



		$cre='';



		}







echo"<div id='bal'>".number_format($cbl,2,'.','')."</div>";











?>