<?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">Imprest 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>
								Imprest 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 -->

						<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>
<?php

	if(isset($_GET['account']))
	{
		
		$account=$_GET['account'];
		$fromdate=$_GET['fromdate'];
		$todate=$_GET['todate'];
		
		$frm=date('Y-m-d',strtotime($fromdate));
		$tod=date('Y-m-d',strtotime($todate));
		
		$fc=mysqli_query($conn,"select * from factory where ID='$account'");
		$fc1=mysqli_fetch_assoc($fc);
		
		echo"<table style='width:95%; margin:auto;'>
		
		<tr><td>
		
		<table style='width:100%;'>
		
		<tr><td style='width:50%;'>Account Head: <b>".$fc1['factory']."</b></td><td style='width:50%; text-align:right;'></td></tr>
		
		</table>
		
		</td></tr>
		
		<tr><td>

		<table class='table table-striped table-bordered table-condensed' id='dynamic-table'>

		<thead><th width='10%'>Date</th><th width='20%'>Description</th><th width='12%'>Debit</th><th width='12%'>Credit</th></thead>";

		if($fromdate!='')
		{
		$getsite3="select date as date,description as description,amount as debit,null as credit from imprest where factory='$account' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'
		UNION
		select date as date,description as description,null as debit,amount as credit from expense where factory='$account' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'
		order by date asc";
		}
		else
		{
		$getsite3="select date as date,description as description,amount as debit,null as credit from imprest where factory='$account' and date between '$aca1[fromdate]' and '$aca1[todate]'
		UNION
		select date as date,description as description,null as debit,amount as credit from expense where factory='$account' and date between '$aca1[fromdate]' and '$aca1[todate]'
		order by date asc";
		}
		
		$getsite4=mysqli_query($conn,$getsite3) or die("Could not get");
		
		$sno=0;
		$db=0;
		$cr=0;
		
		while($getsite5=mysqli_fetch_assoc($getsite4))
		{

		 $sno=$sno+1;

		 echo"<tr><td>".date('d-m-Y',strtotime($getsite5['date']))."</td>
		 <td>$getsite5[description]</td>
		 <td style='text-align:right;'>$getsite5[debit]</td>
		 <td style='text-align:right;'>$getsite5[credit]</td></tr>";
		 
		 $db=$db+$getsite5['debit'];
		 $cr=$cr+$getsite5['credit'];

		}

		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></tr></table>
		
		</td></tr>
		
		</table><br />
		
		<center><a href='imprestledger.php'>Back</a></center>";
		
	}
	else
	{

	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>

	<table width='95%'>
	
	<tr><td>

	<table class='table table-striped table-bordered table-condensed' id='dynamic-table'>

	<thead><th>Account Head</th><th width='25%'>Amount</th></thead>";

	$getsite3="select * from factory order by ID asc";
	$getsite4=mysqli_query($conn,$getsite3) or die("Could not get");
	
	$sno=0;

	while($getsite5=mysqli_fetch_assoc($getsite4))
	{
		 
		 $sno=$sno+1;
		 
		 $imp=mysqli_query($conn,"select sum(amount) as amount from imprest where factory='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]' and date>='2021-04-01'");
		 $imp1=mysqli_fetch_assoc($imp);
		 
		 $sume=mysqli_query($conn,"select sum(amount) as amount from expense where factory='$getsite5[ID]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]' and date>='2021-04-01'");
		 $sume1=mysqli_fetch_assoc($sume);
		 
		 $bal=$imp1['amount']-$sume1['amount'];
		 
		 echo"<tr><td><a href='imprestledger.php?account=".$getsite5['ID']."&fromdate=".$fromdate."&todate=".$todate."'>$getsite5[factory]</a></td><td style='text-align:right;'>".number_format($bal,2,'.','')."</td></tr>";

	}

	echo"</table></td></tr></table>

	<br />
	
	<a href='imprestledger.php'>Back</a>

	</center>";
	

	}
	
	else
	{
	
	echo"<center>

	<form action='imprestledger.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='*' /></td></tr>		

		</table>

		<br>

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

	</form>

	</center><br>";	

	echo"<center>

	<table width='95%'>
	
	<tr><td>

	<table class='table table-striped table-bordered table-condensed' id='dynamic-table'>

	<thead><th>Account Head</th><th width='25%'>Amount</th></thead>";

	$getsite3="select * from factory order by ID asc";
	$getsite4=mysqli_query($conn,$getsite3) or die("Could not get");
	
	$sno=0;

	while($getsite5=mysqli_fetch_assoc($getsite4))
	{
		 
		 $sno=$sno+1;
		 
		 $imp=mysqli_query($conn,"select sum(amount) as amount from imprest where factory='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]' and date>='2021-04-01'");
		 $imp1=mysqli_fetch_assoc($imp);
		 
		 $sume=mysqli_query($conn,"select sum(amount) as amount from expense where factory='$getsite5[ID]' and date between '$aca1[fromdate]' and '$aca1[todate]' and date>='2021-04-01'");
		 $sume1=mysqli_fetch_assoc($sume);
		 
		 $bal=$imp1['amount']-$sume1['amount'];
		 
		 echo"<tr><td><a href='imprestledger.php?account=".$getsite5['ID']."'>$getsite5[factory]</a></td><td style='text-align:right;'>".number_format($bal,2,'.','')."</td></tr>";

	}

	echo"</table></td></tr></table>

	</center>";
	
	}
	
	}

?>




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

?>