<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('30','views','R');     
$_SESSION['cuPages']='fin_proandlos.php';   
$cuPage='fin_proandlos.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='financ';
$menuh='Financials';
$phead='finprls';
$page='Profit &amp; Loss';
$dtnow = date("Y-m-d h:i:s", time());


$today = strftime("%Y-%m-%d", time());

if(isset($_POST['tfdate'])){	
$fdate=$_POST['tfdate'];	
}else{
$fdate=date('Y-m-d', strtotime('-29 days', time()));	
}
if(isset($_POST['ttdate'])){
$tdate=$_POST['ttdate'];	
}else{
$tdate=$today;	
}
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-9">    
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Profit &amp; Loss Statement</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>    
<div class="col-md-12 table-responsive">
<div class="row">    
<center>
<h3 class="page-title">INCOME STATEMENT</h3>
</center>
</div>
<div class="row" >     
<div class="col-md-12">
<div class="row">
<div class="col-md-6 text-left">
<h4>FROM: <?php echo date("d-M-Y", strtotime($fdate));?></h4>    
</div>
<div class="col-md-6 text-right">
<h4>To: <?php echo date("d-M-Y", strtotime($tdate));?></h4>    
</div>
</div>
</div>
<div class="clearfix" ></div>
<!-- REVENUE --> 
<table class="table table-bordered   table-hover">
<tbody>
<tr>
<td width="50%" class="bg-gray"><b>REVENUE</b></td>
<td width="16.16%" class="bg-gray"><b>Debit</b></td>
<td width="16.16%" class="bg-gray"><b>Credit</b></td>
<td width="16.16%" class="bg-gray"><b>Balance</b></td>    
</tr>
<?php
$trcredit = 0;
$trdebit = 0;
$trbalance = 0;
$sql="SELECT * FROM tbl_ledgerbalance WHERE clsid='5'ORDER BY id ASC";
$resultl=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowl=mysqli_fetch_array($resultl)){
$ldebit = $rowl['debit'];
$lcredit=$rowl['credit'];
$lnet=($lcredit-$ldebit); 
?>
<?php if(ABS($lnet)>0){
            $trcredit += $lcredit;
            $trdebit += $ldebit;
            $trbalance += $lnet;   
?>    
<tr>
<td style="padding-left:30px;"> <?php echo $rowl['ledger'];?></td>
<td class="text-right"><?php echo number_format($ldebit,2);?></td>
<td class="text-right"><?php echo number_format($lcredit,2);?></td>
<td class="text-right"><?php echo number_format($lnet,2);?></td>    
</tr>
<?php } ?>   
<?php } ?>  
<tr style="border-bottom:none;border-top:none;">
    <td colspan="4"></td>
</tr>     
<tr>    
<td ><b>&nbsp;Total Revenue</b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($trdebit,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($trcredit,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($trbalance,2);?></b></td>    
</tr>
</tbody>    
</table>
<!--/REVENUE-->
<!--EXPENSES-->
<table class="table table-bordered   table-hover">
<tbody>
<tr>
<td width="50%" class="bg-gray"><b>EXPENSES</b></td>
<td width="16.16%" class="bg-gray"><b>Debit</b></td>
<td width="16.16%" class="bg-gray"><b>Credit</b></td>
<td width="16.16%" class="bg-gray"><b>Balance</b></td>   
</tr>

<?php
$tecredit = 0;
$tedebit = 0;
$tebalance = 0;
$sql="SELECT * FROM tbl_ledgerbalance WHERE clsid='4'ORDER BY id ASC";
$resultl=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowl=mysqli_fetch_array($resultl)){
$ldebit = $rowl['debit'];
$lcredit=$rowl['credit'];
$lnet=($ldebit-$lcredit); 
?>
<?php if(ABS($lnet)>0){
            $tecredit += $lcredit;
            $tedebit += $ldebit;
            $tebalance += $lnet;   
?>    
<tr>
<td style="padding-left:30px;"> <?php echo $rowl['ledger'];?></td>
<td class="text-right"><?php echo number_format($ldebit,2);?></td>
<td class="text-right"><?php echo number_format($lcredit,2);?></td>
<td class="text-right"><?php echo number_format($lnet,2);?></td>    
</tr>
<?php } ?>   
<?php } ?>  
<tr style="border-bottom:none;border-top:none;">
    <td colspan="4"></td>
</tr>     
<tr>    
<td ><b>&nbsp;Total Expenses</b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($tedebit,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($tecredit,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($tebalance,2);?></b></td>    
</tr>
</tbody>    
</table>
<table class="table table-bordered   table-hover">
<tbody>
<tr class="bg-gray">
<td width="50%" style="padding-left:15px;"><b>NET INCOME</b></td>
<td width="16.16%" style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format(($trdebit-$tedebit),2);?></b>
</td>
<td width="16.16%" style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format(($trcredit-$tecredit),2);?></b>
</td>
<td width="16.16%" style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format(($trbalance-$tebalance),2);?></b>
</td>    
</tr>
</tbody>
</table>    
</div>    
</div>
</div>
</div>    
</div>


<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">

</script>    
<!-- /page script -->
</html>    