<?PHP

ob_start();

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysql_query("select * from admin where username='$user'");

$gtp1=mysql_fetch_array($gtp);



include 'top.php';

?>







<div class="main-content">

				<div class="main-content-inner">

					<div class="breadcrumbs ace-save-state" id="breadcrumbs">

						<ul class="breadcrumb">

							<li>

								<i class="ace-icon fa fa-home home-icon"></i>

								<a href="#">Home</a>

							</li>

							<li class="active">Outstandings</li>

						</ul><!-- /.breadcrumb -->

					</div>



					<div class="page-content">			



						<div class="page-header">



<?php



$fi=mysql_query("select * from financial where user='$user' order by ID desc");

$fi1=mysql_fetch_array($fi);



$financial=$fi1['financial'];



?>



					<table style="width:100%;">

                        

                        <tr><td style="width:50%;">

                        

							<h1>

								Outstandings

							</h1>

                            

                            </td>

                            

                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">

                            

                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>

                            

                            </td></tr></table>

                            

						</div><!-- /.page-header -->

<br />



						<div class="row">

							<div class="col-xs-12">

                            

								<!-- PAGE CONTENT BEGINS -->

                                

<script src="assets/js/jquery.js"></script>





<?php



if(isset($_GET['type']))

{



$type=$_GET['type'];



if($type=='payables')

{



echo"<table class='table table-bordered table-condensed' style='width:75%; margin:auto;'>



<thead><tr><th style='width:70%;'>Supplier</th><th style='width:30%;'>Amount</th></tr></thead>";



$tpy=0;

$sup=mysql_query("select * from supplier order by name asc");

while($sup1=mysql_fetch_array($sup))

{



//payables



$pur=mysql_query("select sum(total) as debit from purchase where supplier='$sup1[ID]' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$pur1=mysql_fetch_array($pur);



$pym=mysql_query("select sum(amount) as credit from payment where supplier='$sup1[ID]' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$pym1=mysql_fetch_array($pym);



$sopcr=mysql_query("select sum(amount) as credit from supplier_ledger where supplier='$sup1[ID]' and paymenttype='credit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$sopcr1=mysql_fetch_array($sopcr);



$sopdb=mysql_query("select sum(amount) as debit from supplier_ledger where supplier='$sup1[ID]' and paymenttype='debit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$sopdb1=mysql_fetch_array($sopdb);



$payable=$pur1['debit']-$pym1['credit']-$sopcr1['credit']+$sopdb1['debit'];



//



if($payable!=0)

{



echo"<tr><td>".$sup1['name']."</td><td style='text-align:right;'>".number_format($payable,2,'.','')."</td></tr>";



$tpy=$tpy+$payable;

}



}



echo"<tr><td style='text-align:right;'></td><td style='text-align:right;'>".number_format($tpy,2,'.','')."</td></tr></table>";



}

elseif($type=='receivables')

{



echo"<table class='table table-bordered table-condensed' style='width:75%; margin:auto;'>



<thead><tr><th style='width:70%;'>Customer</th><th style='width:30%;'>Amount</th></tr></thead>";



$trc=0;

$sup=mysql_query("select * from customer order by name asc");

while($sup1=mysql_fetch_array($sup))

{



//receivables



$stl=0;

$sal=mysql_query("select sum(total) as credit from invoice where customer='$sup1[ID]' and date between '$fi1[fromdate]' and '$fi1[todate]' group by invoiceno");

while($sal1=mysql_fetch_array($sal))

{

	$stl=$stl+$sal1['credit'];

}



$rec=mysql_query("select sum(amount) as debit from receipt where customer='$sup1[ID]' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$rec1=mysql_fetch_array($rec);



$copcr=mysql_query("select sum(amount) as credit from debitor_ledger where customer='$sup1[ID]' and paymenttype='credit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$copcr1=mysql_fetch_array($copcr);



$copdb=mysql_query("select sum(amount) as debit from debitor_ledger where customer='$sup1[ID]' and paymenttype='debit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$copdb1=mysql_fetch_array($copdb);



$receivable=$stl-$rec1['debit']+$copcr1['credit']-$copdb1['debit'];



//



if($receivable!=0)

{

echo"<tr><td>".$sup1['name']."</td><td style='text-align:right;'>".number_format($receivable,2,'.','')."</td></tr>";



$trc=$trc+$receivable;

}



}



echo"<tr><td style='text-align:right;'></td><td style='text-align:right;'>".number_format($trc,2,'.','')."</td></tr></table>";



}



echo"<br /><br /><center><a href='outstandings.php'>Back</a></center><br />";



}

else

{



//payables



$pur=mysql_query("select sum(total) as debit from purchase where date between '$fi1[fromdate]' and '$fi1[todate]'");

$pur1=mysql_fetch_array($pur);



$pym=mysql_query("select sum(amount) as credit from payment where date between '$fi1[fromdate]' and '$fi1[todate]'");

$pym1=mysql_fetch_array($pym);



$sopcr=mysql_query("select sum(amount) as credit from creditor_ledger where paymenttype='credit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$sopcr1=mysql_fetch_array($sopcr);



$sopdb=mysql_query("select sum(amount) as debit from creditor_ledger where paymenttype='debit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$sopdb1=mysql_fetch_array($sopdb);



$payable=$pur1['debit']-$pym1['credit']-$sopcr1['credit']+$sopdb1['debit'];



//



//receivables



$stl=0;

$sal=mysql_query("select sum(total) as credit from invoice where date between '$fi1[fromdate]' and '$fi1[todate]' group by invoiceno");

while($sal1=mysql_fetch_array($sal))

{

	$stl=$stl+$sal1['credit'];

}



$rec=mysql_query("select sum(amount) as debit from receipt where date between '$fi1[fromdate]' and '$fi1[todate]'");

$rec1=mysql_fetch_array($rec);



$copcr=mysql_query("select sum(amount) as credit from debitor_ledger where paymenttype='credit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$copcr1=mysql_fetch_array($copcr);



$copdb=mysql_query("select sum(amount) as debit from debitor_ledger where paymenttype='debit' and date between '$fi1[fromdate]' and '$fi1[todate]'");

$copdb1=mysql_fetch_array($copdb);



$receivable=$stl-$rec1['debit']+$copcr1['credit']-$copdb1['debit'];



//



echo"<br /><table class='table table-bordered table-condensed' style='width:75%; margin:auto;'>



<thead><tr><th>Description</th><th>Amount</th></tr></thead>



<tr><td><a href='outstandings.php?type=payables'>Payables</a></td><td style='text-align:right;'>".number_format($payable,2,'.','')."</td></tr>



<tr><td><a href='outstandings.php?type=receivables'>Receivables</a></td><td style='text-align:right;'>".number_format($receivable,2,'.','')."</td></tr>



</table><br /><br />";



}



?>





							<br>

<br>

	



								<!-- PAGE CONTENT ENDS -->

							</div><!-- /.col -->

						</div><!-- /.row -->

					



					</div><!-- /.page-content -->

				</div>

			</div><!-- /.main-content -->





<?php



include 'bottom.php';



}

else

{



 header("location:login.php");

 

}



ob_flush();



?>