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

<?php

	if(isset($_GET['category']))
	{
		
		$category=$_GET['category'];
		$fromdate=$_GET['fromdate'];
		$todate=$_GET['todate'];
		
		$frm=date('Y-m-d',strtotime($fromdate));
		$tod=date('Y-m-d',strtotime($todate));
		
		if($fromdate!='')
		{
		$gg=mysql_query("select sum(amount) as amount from expense where category='$category' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$gg1=mysql_fetch_array($gg);
		}
		else
		{
		$gg=mysql_query("select sum(amount) as amount from expense where category='$category' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		$gg1=mysql_fetch_array($gg);
		}
		
		echo"<table style='width:95%; margin:auto;'>
		
		<tr><td>
		
		<table style='width:100%;'>
		
		<tr><td style='width:50%;'>Category: <b>".$category."</b></td><td style='width:50%; text-align:right;'>Total: <b>".$gg1['amount']."</b></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%'>Name</th><th>Category</th><th>Description</th>
		<th width='12%'>Amount</th></thead>";

		if($fromdate!='')
		{
		$getsite3="select * from expense where category='$category' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]' order by ID asc";
		}
		else
		{
		$getsite3="select * from expense where category='$category' and date between '$aca1[fromdate]' and '$aca1[todate]' order by ID asc";
		}
		
		$getsite4=mysql_query($getsite3) or die("Could not get");
		
		$sno=0;
		
		while($getsite5=mysql_fetch_array($getsite4))
		{

		 $sno=$sno+1;

		 echo"<tr><td><span style='display:none;'>".$getsite5['date']."</span>".date('d-m-Y',strtotime($getsite5['date']))."</td>
		 <td>$getsite5[name]</td>
		 <td>$getsite5[category]</td>
		 <td>$getsite5[description]</td><td style='text-align:right;'>$getsite5[amount]</td></tr>";

		}

		echo"</table>
		
		</td></tr>
		</table><br />
		
		<center><a href='expenseledger.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>Category</th><th width='25%'>Amount</th></thead>";

	$getsite3="select * from exp_category order by ID asc";
	$getsite4=mysql_query($getsite3) or die("Could not get");
	
	$sno=0;

	while($getsite5=mysql_fetch_array($getsite4))
	{
		 
		 $sno=$sno+1;
		 
		 $sume=mysql_query("select sum(amount) as amount from expense where category='$getsite5[category]' and date between '$frm' and '$tod' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		 $sume1=mysql_fetch_array($sume);
		 
		 echo"<tr><td><a href='expenseledger.php?category=".str_replace("&","%26",$getsite5['category'])."&fromdate=".$fromdate."&todate=".$todate."'>$getsite5[category]</a></td><td style='text-align:right;'>$sume1[amount]</td></tr>";

	}

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

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

	</center>";
	
		
	}
	
	else
	{
	
	echo"<center>

	<form action='expenseledger.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"<center>

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

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

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

	$getsite3="select * from exp_category order by ID asc";
	$getsite4=mysql_query($getsite3) or die("Could not get");
	
	$sno=0;

	while($getsite5=mysql_fetch_array($getsite4))
	{
		 
		 $sno=$sno+1;
		 
		 $sume=mysql_query("select sum(amount) as amount from expense where category='$getsite5[category]' and date between '$aca1[fromdate]' and '$aca1[todate]'");
		 $sume1=mysql_fetch_array($sume);
		 
		 echo"<tr><td><a href='expenseledger.php?category=".str_replace("&","%26",$getsite5['category'])."'>$getsite5[category]</a></td><td style='text-align:right;'>$sume1[amount]</td></tr>";

	}

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

	</center>";
	
	}
	
	}

?>




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

?>