<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
<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">Customer Ledger</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

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

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Customer 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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<script>
  $(function() {
  // Initialize the datepicker for the "From Date" input
  $("#from-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });

  // Initialize the datepicker for the "To Date" input
  $("#to-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });
});
</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['customer'])
{

$customer=$_GET['customer'];


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('printcustomerledger.php?customer=<?php echo $customer; ?>&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));

$cp=mysqli_query($conn,"select * from customer where ID='$customer'");
$cp1=mysqli_fetch_assoc($cp);

echo "
<table style='width:100%;'><tr><td style='width:50%;'>Customer : <b>".$cp1['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=mysqli_query($conn,"select sum(amount) as amount from receipt where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and customer='$customer'");
$py1=mysqli_fetch_assoc($py);

$sl=mysqli_query($conn,"select sum(total) as amount from invoice where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and customer='$customer'");
$sl1=mysqli_fetch_assoc($sl);

$cgst=0;
$sgst=0;
$igst=0;
$adjs=0;
$d=mysqli_query($conn,"select * from invoice where date<'$frm' and customer='$customer' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
while($d1=mysqli_fetch_assoc($d))
{
$cgst=$cgst+$d1['cgst_amt'];
$sgst=$sgst+$d1['sgst_amt'];
$igst=$igst+$d1['igst_amt'];
}

$opl=mysqli_query($conn,"select * from debitor_ledger where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and customer='$customer'");
$opl1=mysqli_fetch_assoc($opl);

$opcr=0;
$opdb=0;
if($opl1['paymenttype']=='debit')
{
$opdb=$opl1['amount'];
}
elseif($opl1['paymenttype']=='credit')
{
$opcr=$opl1['amount'];
}

$tex=$sl1['amount']+$opcr;
$trc=$py1['amount']+$opdb;

$tex=number_format($tex,0,'.','').'.00';
$trc=number_format($trc,0,'.','').'.00';

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=mysqli_query($conn,"

select invoiceno as id, date as date, time as time, concat('Invoice #: ') as details, sum(total) as credit, NULL as debit, cgst_amt as cgs, sgst_amt as sgs, igst_amt as igs, 'invoice' as type from invoice where customer='$customer' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno
UNION
select ID as id, date as date, time as time, concat('Receipt:', description , ' #: ', ID) as details, NULL as credit, amount as debit, null as cgs, null as sgs, null as igs, 'receipt' as type from receipt where customer='$customer' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'
UNION
select ID as id, date as date, time as time, concat('Opening Balance #: ', ID) as details, amount as credit, NULL as debit, null as cgs, null as sgs, null as igs, 'opening' as type from debitor_ledger where customer='$customer' and paymenttype='credit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'
UNION
select ID as id, date as date, time as time, concat('Opening Balance #: ', ID) as details, NULL as credit, amount as debit, null as cgs, null as sgs, null as igs, 'opening' as type from debitor_ledger where customer='$customer' and paymenttype='debit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

order by date asc, time asc");

/*$cr=0;
$db=0;*/
while($result=mysqli_fetch_assoc($sql))
{

if($result['type']=='invoice')
{
$cred=$result['credit']+$result['adjs'];
}
else
{
$cred=$result['credit'];
}

$debit=$result['debit'];

if($debit)$debit=''.number_format($debit,0,'.','').'.00';

if($cred!=0)
{
$credit=number_format($cred,0,'.','').'.00';
}
else
{
$credit='';
}

$cr=$cr+$credit;
$db=$db+$debit;

$bl=$db-$cr;

if($result['type']=='invoice')
{
	$sl=mysqli_query($conn,"select * from invoice where invoiceno='$result[id]'");
	$sl1=mysqli_fetch_assoc($sl);
	
	if($sl1['billtype']=='manual')
	{
	$iino=$sl1['invno'];
	}
	else
	{
	$iino=$sl1['invno'];
	}
}

echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>".$result['details']."";

if($result['type']=='invoice')
{
	echo"$iino";
}

echo"</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>";
}
elseif($cr==$db)
{
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
{

	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>';

		}

	}

?>

<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('printcustomerledger.php?customer=<?php echo $customer; ?>');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>
</td>

<!-- <td style="text-align:center; padding-left:5px;">
<form method="get" action="pdf.php" target="_blank">
                        
                <input type="hidden" name="date" value="" />
                <input type="hidden" name="file" value="printdaybook.php" />
                <input type="hidden" name="title" value="Daybook PDF" />                
                <button type="submit" name="submit" style="margin-top:15px;" value="GeneratePDF">Generate PDF</button>
                
</form>
</td>

<td style="text-align:center; padding-left:7px;">

<a href='daybook-excel.php?date='><button type="submit" name="submit" value="Excel" style="margin-top:-4px;">Excel</button></a> -->

<!-- </td> -->

</tr>
</table>

</center><br />

<?php

echo"

<center>	

<table style='width:95%;'>

<tr><td style='width:20%;'></td><td style='width:60%;'>

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

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

	<table>
	
	<tr><td>From Date  <input class='form-control'; type='text' name='fromdate' class='fromdate' id='from-datepicker' title='*' /required></td>
	
	<td style='padding-left:15px;'>To Date : <input type='text' name='todate' class='todate' id='to-datepicker' title='*' /required></td></tr>
	
	</table>
	
	<center><button type='submit' name='submit' class='btn btn-sm btn-primary' value='submit' style='margin-top:7px;'>Submit</button></center><br>
	
</form></td>

<td style='width:20%;' valign='top'>

</td></tr>

</table>

</center>";

$cp=mysqli_query($conn,"select * from customer where ID='$customer'");
$cp1=mysqli_fetch_assoc($cp);

echo "
<p>Customer : <b>".$cp1['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=mysqli_query($conn,"

select invoiceno as id, date as date, time as time, concat('Invoice #: ') as details, sum(total) as credit, NULL as debit, cgst_amt as cgs, sgst_amt as sgs, igst_amt as igs, 'invoice' as type from invoice where customer='$customer' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno
UNION
select ID as id, date as date, time as time, concat('Receipt: ', description, ' #: ', ID) as details, NULL as credit, amount as debit, null as cgs, null as sgs, null as igs, 'receipt' as type from receipt where customer='$customer' and date between '$aca1[fromdate]' and '$aca1[todate]'
UNION
select ID as id, date as date, time as time, concat('Opening Balance #: ', ID) as details, amount as credit, NULL as debit, null as cgs, null as sgs, null as igs, 'opening' as type from debitor_ledger where customer='$customer' and paymenttype='credit' and date between '$aca1[fromdate]' and '$aca1[todate]'
UNION
select ID as id, date as date, time as time, concat('Opening Balance #: ', ID) as details, NULL as credit, amount as debit, null as cgs, null as sgs, null as igs, 'opening' as type from debitor_ledger where customer='$customer' and paymenttype='debit' and date between '$aca1[fromdate]' and '$aca1[todate]'

order by date asc, time asc") or die(mysqli_error($conn));

$cr=0;
$db=0;
while($result=mysqli_fetch_assoc($sql))
{

if($result['type']=='invoice')
{
$cred=$result['credit']+$result['adjs'];
}
else
{
$cred=$result['credit'];
}

$debit=$result['debit'];

if($debit)$debit=''.number_format($debit,0,'.','').'.00';

//echo"$cred";

if($cred!=0)
{
$credit=number_format($cred,0,'.','').'.00';
}
else
{
$credit='';
}

$cr=$cr+$credit;
$db=$db+$debit;

$bl=$db-$cr;

if($result['type']=='invoice')
{
	$sl=mysqli_query($conn,"select * from invoice where invoiceno='$result[id]'");
	$sl1=mysqli_fetch_assoc($sl);
	
	$iino=$sl1['invno'];
	
}

echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>".$result['details']."";

if($result['type']=='invoice')
{
	echo"$iino";
}

echo"</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>";
}
elseif($cr==$db)
{
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='debitor_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-danger">
				<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'];
	
	echo"<center><b>Period : ".$fromdate." - ".$todate."</b></center>";
	
	$frm=date('Y-m-d',strtotime($fromdate));
	$tod=date('Y-m-d',strtotime($todate));
	
	
echo "<table style='width:100%; margin:7px auto;'>

<tr><td><a href='debitor_obl.php'>Opening Balance</a></td>

<td style='text-align:right;'>";

?>

<a href="JavaScript:newPopup('printcustomerledger.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>

</table>

<table id='dynamic-table' class='table table-striped table-bordered table-hover'>
<thead><th width='40%'>Ledger Name</th><th width='14%'>Total Received</th><th width='14%'>Total Invoiced</th><th width='14%'>Closing Balance</th><th></th></thead>";

		$get="select * from customer order by name ASC";
		$get1=mysqli_query($conn,$get) or die("Could Not Get Supplier Details");
		while($getsite5=mysqli_fetch_assoc($get1))
		{

		$sl=mysqli_query($conn,"select sum(total) as amount from invoice where customer='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$sl1=mysqli_fetch_assoc($sl);
		
		$re=mysqli_query($conn,"select sum(amount) as amount from receipt where customer='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$re1=mysqli_fetch_assoc($re);
		
		$cgst=0;
		$sgst=0;
		$igst=0;
		$adjs=0;
		$d=mysqli_query($conn,"select * from invoice where customer='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
		while($d1=mysqli_fetch_assoc($d))
		{
		$cgst=$cgst+$d1['cgst_amt'];
		$sgst=$sgst+$d1['sgst_amt'];
		$igst=$igst+$d1['igst_amt'];
		$adjs=$adjs+$d1['adjustment'];
		}
		
		$obl=mysqli_query($conn,"select * from debitor_ledger where customer='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$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'];
		}
		
		$cre=$sl1['amount']+$credit+$adjs;
		$deb=$re1['amount']+$debit;
		
		if($deb!='0' && $deb!='')
		{
		$deb=number_format($deb,0,'.','').'.00';
		}
		else
		{
		$deb='';
		}
		
		if($cre!='0' && $cre!='')
		{
		$cre=number_format($cre,0,'.','').'.00';
		}
		else
		{
		$cre='';
		}		
		
		$cbl=$deb-$cre;
		
		if($cbl!=0)
		{

		echo"<tr><td><a href='debitor_ledger.php?customer=".$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,0,'.','').".00</td><td>";
		
		if($num>0)
		{
		echo"<a href='editdebitor_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="debitor_ledger.php">Back</a></center>';
	
}
else
{


echo"<center>

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

	<table>	

	<tr><td>From Date  <input class='form-control' type='text' name='fromdate' class='fromdate' id='from-datepicker' title='*' /required></td>	

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

	</table>
	
	<br>

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

</center><br>";


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

<tr><td><a href='debitor_obl.php'>Opening Balance</a></td>

<td style='text-align:right;'>";

?>

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

<?php

echo"</td></tr>

</table>

<table id='dynamic-table' class='table table-striped table-bordered table-hover'>
<thead><th width='40%'>Ledger Name</th><th width='14%'>Total Received</th><th width='14%'>Total Invoiced</th><th width='14%'>Closing Balance</th><th></th></thead>";

		$get="select * from customer order by name ASC";
		$get1=mysqli_query($conn,$get) or die("Could Not Get Supplier Details");
		while($getsite5=mysqli_fetch_assoc($get1))
		{

		$sl=mysqli_query($conn,"select sum(total) as amount from invoice where customer='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$sl1=mysqli_fetch_assoc($sl);
		
		$re=mysqli_query($conn,"select sum(amount) as amount from receipt where customer='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$re1=mysqli_fetch_assoc($re);
		
		$cgst=0;
		$sgst=0;
		$igst=0;
		$adjs=0;
		$d=mysqli_query($conn,"select * from invoice where customer='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno");
		while($d1=mysqli_fetch_assoc($d))
		{
		$cgst=$cgst+$d1['cgst_amt'];
		$sgst=$sgst+$d1['sgst_amt'];
		$igst=$igst+$d1['igst_amt'];
		$adjs=$adjs+$d1['adjustment'];
		}
		
		$obl=mysqli_query($conn,"select * from debitor_ledger where customer='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$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'];
		}
		
		$cre=$sl1['amount']+$credit+$adjs;
		$deb=$re1['amount']+$debit;
		
		if($deb!='0' && $deb!='')
		{
		$deb=number_format($deb,0,'.','').'.00';
		}
		else
		{
		$deb='';
		}
		
		if($cre!='0' && $cre!='')
		{
		$cre=number_format($cre,0,'.','').'.00';
		}
		else
		{
		$cre='';
		}		
		
		$cbl = intval($deb) - intval($cre);

		
		if($cbl!=0)
		{

		echo"<tr><td><a href='debitor_ledger.php?customer=".$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,0,'.','').".00</td><td>";
		
		if($num>0)
		{
		echo"<a href='editdebitor_obl.php?id=".$getsite5['ID']."'><i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";
		}
		
		echo"</td></tr>";
		
		}

		}
		
echo'</table>';

}

}

?>

<br>
<br>


								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					

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


<?php

include 'bottom.php';

}
else
{

 header("location:login.php");

}

ob_flush();




?>