<?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">Supplier Ledger</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

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

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Supplier Ledger
							</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>
<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>
								
<?php



if($_GET['supplier'])

{



$supplier=$_GET['supplier'];





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

{

$fromdate=$_GET['fromdate'];

$todate=$_GET['todate'];



?>



<script type="text/javascript">

// Popup window code

function newPopup(url) {

	popupWindow = window.open(

		url,'popUpWindow','height=700,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,onscreendisplay=yes,menubar=no,location=no,directories=no,status=yes')

}

</script>



<center>



<table>

<tr>



<td style="text-align:center; padding-right:5px;">

<a href="JavaScript:newPopup('printcreditorledger.php?supplier=<?php echo $supplier; ?>&fromdate=<?php echo $fromdate; ?>&todate=<?php echo $todate; ?>');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

</td>



</tr>

</table>



</center><br />



<?php



$frm=date('Y-m-d',strtotime($fromdate));

$to=date('Y-m-d',strtotime($todate));



$sp=mysql_query("select * from supplier where ID='$supplier'");

$sp1=mysql_fetch_array($sp);



echo "

<table style='width:100%;'><tr><td style='width:50%;'>Supplier : <b>".$sp1['name']."</b></td><td style='width:50%; text-align:right;'>Period : <b>".$fromdate." - ".$todate."</b></td></tr></table>

<center><table class='table table-striped table-bordered table-condensed'>

<thead><th width='10%'>Date</th><th width='40%'>Description</th><th width='15%'>Debit</th><th width='15%'>Credit</th><th width='15%'>Closing Balance</th></thead>";





$py=mysql_query("select sum(amount) as amount from payment where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and supplier='$supplier'");

$py1=mysql_fetch_array($py);



$sono=0;

$po=mysql_query("select sum(total) as amount from purchase where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and supplier='$supplier' group by invoiceno");

while($po1=mysql_fetch_array($po))

{

$sono=$sono+$po1['amount'];

}


		$cgst=0;
		$sgst=0;
		$igst=0;
		$loi=mysql_query("select * from purchase where supplier='$supplier' and date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
		while($loi1=mysql_fetch_array($loi))
		{
			$cgst=$cgst+$loi1['cgst_amt'];
			$sgst=$sgst+$loi1['sgst_amt'];
			$igst=$igst+$loi1['igst_amt'];
		}



$opl=mysql_query("select * from creditor_ledger where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and supplier='$supplier'");

$opl1=mysql_fetch_array($opl);


$opcr=0;

$opdb=0;

if($opl1['paymenttype']=='debit')

{

$opdb=$opl1['amount'];

}

elseif($opl1['paymenttype']=='credit')

{

$opcr=$opl1['amount'];

}

$tex=$py1['amount']+$opcr;

$trc=$sono+$opdb;



if($trc>$tex)

{

$bl=$trc-$tex;



echo"<tr><td colspan='2'>B/F</td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td><td></td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



$db=$bl;

$cr=0;



}

elseif($tex>$trc)

{

$bl=$tex-$trc;



echo"<tr><td colspan='2'>B/F</td><td></td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td><td style='text-align:right;'>-".number_format($bl,2,'.','')."</td></tr>";



$db=0;

$cr=$bl;



}

else

{

$bl=0;

$cr=0;

$db=0;

}





$sql=mysql_query("



select time as time, invoiceno as id, date as date, concat('Purchase #: ', invoiceno) as details, NULL as credit, sum(total) as debit, cgst_amt as cgt, sgst_amt as sgt, 'purchase' as type from purchase where supplier='$supplier' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno

UNION

select time as time, ID as id, date as date, concat('Payment: ', description, ' #: ', ID) as details, amount as credit, NULL as debit, null as cgt, null as sgt, 'payment' as type from payment where supplier='$supplier' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, date as date, concat('Opening Balance #: ', ID) as details, amount as credit, NULL as debit, null as cgt, null as sgt, 'opening' as type from creditor_ledger where supplier='$supplier' and paymenttype='credit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, date as date, concat('Opening Balance #: ', ID) as details, NULL as credit, amount as debit, null as cgt, null as sgt, 'opening' as type from creditor_ledger where supplier='$supplier' and paymenttype='debit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'



order by time asc");



/*$cr=0;

$db=0;*/

while($result=mysql_fetch_array($sql))

{



$credit=$result['credit'];


if($result['type']=='purchase')
{
$debit=$result['debit']+$result['adjs'];
}
else
{
$debit=$result['debit'];
}



if($debit)$debit=''.number_format($debit,2,'.','');

if($credit)$credit=''.number_format($credit,2,'.','');



$cr=$cr+$result['credit'];

$db=$db+$debit;



$bl=$db-$cr;



echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>".$result['details']."</td><td style='text-align:right;'>".$debit."</td><td style='text-align:right;'>".$credit."</td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



}





echo"<tr><td colspan='2' style='text-align:right;'>Total</td><td style='text-align:right;'>".number_format($db,2,'.','')."</td><td style='text-align:right;'>".number_format($cr,2,'.','')."</td><td></td></tr>";



if($db>$cr)

{

$cb=$db-$cr;



echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td><td style='text-align:right;'></td><td></td></tr>";

}

elseif($cr>$db)

{

$cb=$cr-$db;



echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'></td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td><td></td></tr>";

}

else

{

echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>NIL</td><td style='text-align:right;'>NIL</td><td></td></tr>";

}



echo"</table><br /><br />



<center><a href='javascript:history.back();'>Back</a></center>";



}



else

{



?>



<script type="text/javascript">

// Popup window code

function newPopup(url) {

	popupWindow = window.open(

		url,'popUpWindow','height=700,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,onscreendisplay=yes,menubar=no,location=no,directories=no,status=yes')

}

</script>



<center>



<table>

<tr>

<td style="text-align:center; padding-right:5px;">

<a href="JavaScript:newPopup('printsupplierledger.php?supplier=<?php echo $supplier; ?>');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

</td>

</tr>

</table>



</center><br />



<?php





echo"



<center>	



<form action='creditor_ledger.php' method='get' id='wrk'>



<input type='hidden' name='supplier' value='".$supplier."' />



	<table>

	

	<tr><td>From Date : <input type='text' name='fromdate' class='fromdate' id='required' title='*' /></td>

	

	<td style='padding-left:15px;'>To Date : <input type='text' name='todate' class='todate' id='required' title='*' /></td></tr>

	

	</table>

	<br>

	<button type='submit' name='submit' class='btn btn-sm btn-primary' value='submit'>Submit</button>

	

</form>



	</center><br>";



$sp=mysql_query("select * from supplier where ID='$supplier'");

$sp1=mysql_fetch_array($sp);



echo "

<p>Supplier : <b>".$sp1['name']."</b></p>

<center><table class='table table-striped table-bordered table-condensed'>

<thead><th width='10%'>Date</th><th width='40%'>Description</th><th width='15%'>Debit</th><th width='15%'>Credit</th><th width='15%'>Closing Balance</th></thead>";



$sql=mysql_query("



select time as time, invoiceno as id, date as date, concat('Purchase #: ', invoiceno) as details, NULL as credit, sum(total) as debit, cgst_amt as cgt, sgst_amt as sgt, 'purchase' as type from purchase where supplier='$supplier' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno

UNION

select time as time, ID as id, date as date, concat('Payment: ', description, ' #: ', ID) as details, amount as credit, NULL as debit, null as cgt, null as sgt, 'payment' as type from payment where supplier='$supplier' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, date as date, concat('Opening Balance #: ', ID) as details, amount as credit, NULL as debit, null as cgt, null as sgt, 'opening' as type from creditor_ledger where supplier='$supplier' and paymenttype='credit' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, date as date, concat('Opening Balance #: ', ID) as details, NULL as credit, amount as debit, null as cgt, null as sgt, 'opening' as type from creditor_ledger where supplier='$supplier' and paymenttype='debit' and date between '$aca1[fromdate]' and '$aca1[todate]'



order by time asc") or die(mysql_error());



$cr=0;

$db=0;

while($result=mysql_fetch_array($sql))

{



$credit=$result['credit'];


if($result['type']=='purchase')
{
$debit=$result['debit']+$result['adjs'];
}
else
{
$debit=$result['debit'];
}



if($debit)$debit=''.number_format($debit,2,'.','');

if($credit)$credit=''.number_format($credit,2,'.','');



$cr=$cr+$result['credit'];

$db=$db+$debit;



$bl=$db-$cr;



echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>".$result['details']."</td><td style='text-align:right;'>".$debit."</td><td style='text-align:right;'>".$credit."</td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



}





echo"<tr><td colspan='2' style='text-align:right;'>Total</td><td style='text-align:right;'>".number_format($db,2,'.','')."</td><td style='text-align:right;'>".number_format($cr,2,'.','')."</td><td></td></tr>";



if($db>$cr)

{

$cb=$db-$cr;



echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td><td style='text-align:right;'></td><td></td></tr>";

}

elseif($cr>$db)

{

$cb=$cr-$db;



echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'></td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td><td></td></tr>";

}

else

{

echo"<tr><td colspan='2' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>NIL</td><td style='text-align:right;'>NIL</td><td></td></tr>";

}



echo"</table><br /><br />



<center><a href='creditor_ledger.php'>Back</a></center>";



}



}



else

{



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

	{

	 $status=$_GET['s'];

		 if($status=='1')

		 {

		 echo '<div class="alert alert-success">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values inserted successfully.</strong>

			  </div>';

		}

		elseif($status=='2')

		 {

		 echo '<div class="alert alert-error">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values Not inserted. Please try again.</strong>

			  </div>';

		}

		elseif($status=='3')

		 {

		 echo '<div class="alert alert-success">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values edited successfully.</strong>

			  </div>';

		}

		elseif($status=='4')

		 {

		 echo '<div class="alert alert-error">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values Not Edited. Please try again.</strong>

			  </div>';

		}

	}

	
?>



<script type="text/javascript">

// Popup window code

function newPopup(url) {

	popupWindow = window.open(

		url,'popUpWindow','height=700,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,onscreendisplay=yes,menubar=no,location=no,directories=no,status=yes')

}

</script>


<?php

if(isset($_GET['submit']))
{
	
	$fromdate=$_GET['fromdate'];
	$todate=$_GET['todate'];
	
	$frm=date('Y-m-d',strtotime($fromdate));
	$tod=date('Y-m-d',strtotime($todate));
	
	echo"<center><b>Period : ".$fromdate." - ".$todate."</b></center>";
	
echo"

<table style='width:100%; margin:7px auto;'>

<tr><td><a href='creditor_obl.php'>Opening Balance</a></td><td style='width:50%; text-align:right;'>";
?>

<a href="JavaScript:newPopup('printsupplierledger.php?fromdate=<?php echo $fromdate; ?>&todate=<?php echo $todate; ?>');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

<?php

echo"</td></tr>

<center><table id='dynamic-table' class='table table-striped table-bordered table-hover'>

<thead><th width='40%'>Ledger Name</th><th width='18%'>Total Purchased</th><th width='18%'>Total Paid</th><th width='18%'>Closing Balance</th><th></th></thead>";


		$get="select * from supplier order by name ASC";
		$get1=mysql_query($get) or die("Could Not Get Supplier Details");
		while($getsite5=mysql_fetch_array($get1))
		{

		$sl=mysql_query("select sum(total) as amount from purchase where supplier='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$sl1=mysql_fetch_array($sl);
		
		$re=mysql_query("select sum(amount) as amount from payment where supplier='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$re1=mysql_fetch_array($re);
		
		$obl=mysql_query("select * from creditor_ledger where supplier='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$num=mysql_num_rows($obl);
		$obl1=mysql_fetch_array($obl);

		
		$adjs=0;
		$cgst=0;
		$sgst=0;
		$loi=mysql_query("select * from purchase where supplier='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
		while($loi1=mysql_fetch_array($loi))
		{
			$adjs=$adjs+$loi1['adjustment'];
			$cgst=$cgst+$loi1['cgst_amt'];
			$sgst=$sgst+$loi1['sgst_amt'];
		}
		

		$credit=0;
		$debit=0;
		if($obl1['paymenttype']=='credit')
		{

		$credit=$obl1['amount'];
		$debit=0;

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

		$credit=0;
		$debit=$obl1['amount'];

		}

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

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

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

		}
		else
		{

		$deb='';

		}

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

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

		}

		else
		{

		$cre='';

		}
		
		if($cbl!=0)
		{

		echo"<tr><td><a href='creditor_ledger.php?supplier=".$getsite5['ID']."'>$getsite5[name]</a></td><td style='text-align:right;'>".$deb."</td><td style='text-align:right;'>".$cre."</td><td style='text-align:right;'>".number_format($cbl,2,'.','')."</td><td>";

		if($num>0)
		{

		echo"<a href='editcreditor_obl.php?id=".$getsite5['ID']."'><i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";

		}

		echo"</td></tr>";		
		
		}

		}

echo'</table><br />

<center>

<a href="creditor_ledger.php">Back</a>

</center>';


}
else
{


echo"<center>

	<form action='creditor_ledger.php' method='get' id='wrk'>

	<table>	

	<tr><td>From Date : <input type='text' name='fromdate' class='fromdate' id='required' title='*' /></td>	

	<td style='padding-left:15px;'>To Date : <input type='text' name='todate' class='todate' id='required' title='*' /></td></tr>	

	</table>
	
	<br>

	<button type='submit' name='submit' value='submit' class='btn btn-sm btn-primary'>Submit</button><br>
	
	</form>

</center><br>";


echo"

<table style='width:100%; margin:7px auto;'>

<tr><td><a href='creditor_obl.php'>Opening Balance</a></td><td style='width:50%; text-align:right;'>";
?>

<a href="JavaScript:newPopup('printsupplierledger.php');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

<?php

echo"</td></tr>

<center><table id='dynamic-table' class='table table-striped table-bordered table-hover'>

<thead><th width='40%'>Ledger Name</th><th width='18%'>Total Purchased</th><th width='18%'>Total Paid</th><th width='18%'>Closing Balance</th><th></th></thead>";


		$get="select * from supplier order by name ASC";
		$get1=mysql_query($get) or die("Could Not Get Supplier Details");
		while($getsite5=mysql_fetch_array($get1))
		{

		$sl=mysql_query("select sum(total) as amount from purchase where supplier='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$sl1=mysql_fetch_array($sl);
		
		$re=mysql_query("select sum(amount) as amount from payment where supplier='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$re1=mysql_fetch_array($re);
		
		$obl=mysql_query("select * from creditor_ledger where supplier='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$num=mysql_num_rows($obl);
		$obl1=mysql_fetch_array($obl);

		
		$adjs=0;
		$cgst=0;
		$sgst=0;
		$loi=mysql_query("select * from purchase where supplier='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
		while($loi1=mysql_fetch_array($loi))
		{
			$adjs=$adjs+$loi1['adjustment'];
			$cgst=$cgst+$loi1['cgst_amt'];
			$sgst=$sgst+$loi1['sgst_amt'];
		}
		

		$credit=0;
		$debit=0;
		if($obl1['paymenttype']=='credit')
		{

		$credit=$obl1['amount'];
		$debit=0;

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

		$credit=0;
		$debit=$obl1['amount'];

		}

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

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

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

		}
		else
		{

		$deb='';

		}

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

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

		}

		else
		{

		$cre='';

		}
		
		if($cbl!=0)
		{

		echo"<tr><td><a href='creditor_ledger.php?supplier=".$getsite5['ID']."'>$getsite5[name]</a></td><td style='text-align:right;'>".$deb."</td><td style='text-align:right;'>".$cre."</td><td style='text-align:right;'>".number_format($cbl,2,'.','')."</td><td>";

		if($num>0)
		{

		echo"<a href='editcreditor_obl.php?id=".$getsite5['ID']."'><i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";

		}

		echo"</td></tr>";		
		
		}

		}

echo'</table></center>';

}

}

?>

							<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();

?>