<?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" id="itemdata">     
<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>Opening</b></td>
<td width="16.16%" class="bg-gray"><b>During Time</b></td>
<td width="16.16%" class="bg-gray"><b>Closing</b></td>    
</tr>
<?php     
$sql="SELECT DISTINCT grid,groups FROM tbl_ledger WHERE clsid='5' ORDER BY grid ASC";
$result=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowg=mysqli_fetch_array($result)){
$gopval=(get_goupvaltest($rowg['grid'],'C','O','G',$fdate)-get_goupvaltest($rowg['grid'],'D','O','G',$fdate));
$gdebit=get_goupvaltest($rowg['grid'],'D','D','G',$fdate,$tdate);
$gcredit=get_goupvaltest($rowg['grid'],'C','D','G',$fdate,$tdate);
$gnet=($gcredit-$gdebit);
$gcval=($gopval+$gnet);    
?>
<?php if(ABS($gcval)>0){?>     
<tr>
<td ><b><i class="fa fa-caret-right" aria-hidden="true"></i>&nbsp;<?php echo $rowg['groups'];?></b></td>
<td></td>
<td></td>
<td></td>    
</tr>
<?php } ?>    
<?php     
$sql="SELECT DISTINCT sgrid,sgroup FROM tbl_ledger WHERE clsid='5' AND grid='".$rowg['grid']."' ORDER BY sgrid ASC";
$results=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowsg=mysqli_fetch_array($results)){
$sopval=(get_goupvaltest($rowsg['sgrid'],'C','O','S',$fdate)-get_goupvaltest($rowsg['sgrid'],'D','O','S',$fdate));
$sdebit=get_goupvaltest($rowsg['sgrid'],'D','D','S',$fdate,$tdate);
$scredit=get_goupvaltest($rowsg['sgrid'],'C','D','S',$fdate,$tdate);
$snet=($scredit-$sdebit);
$scval=($sopval+$snet);     
?>
<?php if(ABS($scval)>0){?>     
<tr>
<td style="padding-left: 15px;"><i class="fa fa-caret-right" aria-hidden="true"></i>&nbsp;<?php echo $rowsg['sgroup'];?></td>
<td></td>
<td></td>
<td></td>    
</tr>
<?php } ?>   
<?php     
$sql="SELECT id,code,ledger FROM tbl_ledger WHERE clsid='5' AND sgrid='".$rowsg['sgrid']."' ORDER BY id ASC";
$resultl=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowl=mysqli_fetch_array($resultl)){
$lopval=(get_ledgervaltest($rowl['id'],'C','O',$fdate)-get_ledgervaltest($rowl['id'],'D','O',$fdate));
$ldebit=get_ledgervaltest($rowl['id'],'D','D',$fdate,$tdate);
$lcredit=get_ledgervaltest($rowl['id'],'C','D',$fdate,$tdate);
$lnet=($lcredit-$ldebit);
$lcval=($lopval+$lnet);    
?>
<?php if(ABS($lcval)>0){?>    
<tr>
<td style="padding-left:30px;"><?php echo $rowl['code'];?>: <?php echo $rowl['ledger'];?></td>
<td class="text-right"><?php echo number_format($lopval,2);?></td>
<td class="text-right"><?php echo number_format($lnet,2);?></td>
<td class="text-right"><?php echo number_format($lcval,2);?></td>    
</tr>
<?php } ?>   
<?php }} ?>
<?php if(ABS($gcval)>0){?>     
<tr>
<td style="text-align: right;padding-right:20px;"><strong>Total <?php echo $rowg['groups'];?></strong></td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gopval),2);?></b>
</td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gnet),2);?></b>
</td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gcval),2);?></b>
</td>    
</tr>
<?php } ?>    
<?php } ?>
<?php 
$rcopval=(get_goupvaltest('5','C','O','C',$fdate)-get_goupvaltest('5','D','O','C',$fdate));
$rcdebit=get_goupvaltest('5','D','D','C',$fdate,$tdate);
$rccredit=get_goupvaltest('5','C','D','C',$fdate,$tdate);
$rcnet=($rccredit-$rcdebit);
$rccval=($rcopval+$rcnet);    
?>
<?php if(ABS($rccval)>0){?>    
<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($rcopval,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($rcnet,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($rccval,2);?></b></td>    
</tr>
<?php } ?>
</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>Opening</b></td>
<td width="16.16%" class="bg-gray"><b>During Time</b></td>
<td width="16.16%" class="bg-gray"><b>Closing</b></td>    
</tr>
<?php     
$sql="SELECT DISTINCT grid,groups FROM tbl_ledger WHERE clsid='4' ORDER BY grid ASC";
$result=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowg=mysqli_fetch_array($result)){
$gopval=(get_goupvaltest($rowg['grid'],'D','O','G',$fdate)-get_goupvaltest($rowg['grid'],'C','O','G',$fdate));
$gdebit=get_goupvaltest($rowg['grid'],'D','D','G',$fdate,$tdate);
$gcredit=get_goupvaltest($rowg['grid'],'C','D','G',$fdate,$tdate);
$gnet=($gdebit-$gcredit);
$gcval=($gopval+$gnet);    
?>
<?php if(ABS($gcval)>0){?>    
<tr>
<td ><b><i class="fa fa-caret-right" aria-hidden="true"></i>&nbsp;<?php echo $rowg['groups'];?></b></td>
<td></td>
<td></td>
<td></td>    
</tr>
<?php } ?>   
<?php     
$sql="SELECT DISTINCT sgrid,sgroup FROM tbl_ledger WHERE clsid='4' AND grid='".$rowg['grid']."' ORDER BY sgrid ASC";
$results=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowsg=mysqli_fetch_array($results)){
$sopval=(get_goupvaltest($rowsg['sgrid'],'D','O','S',$fdate)-get_goupvaltest($rowsg['sgrid'],'C','O','S',$fdate));
$sdebit=get_goupvaltest($rowsg['sgrid'],'D','D','S',$fdate,$tdate);
$scredit=get_goupvaltest($rowsg['sgrid'],'C','D','S',$fdate,$tdate);
$snet=($sdebit-$scredit);
$scval=($sopval+$snet);    
?>
<?php if(ABS($scval)>0){?>     
<tr>
<td style="padding-left: 15px;"><i class="fa fa-caret-right" aria-hidden="true"></i>&nbsp;<?php echo $rowsg['sgroup'];?></td>
<td></td>
<td></td>
<td></td>    
</tr>
<?php } ?>   
<?php     
$sql="SELECT id,code,ledger FROM tbl_ledger WHERE clsid='4' AND sgrid='".$rowsg['sgrid']."' ORDER BY id ASC";
$resultl=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowl=mysqli_fetch_array($resultl)){
$lopval=(get_ledgervaltest($rowl['id'],'D','O',$fdate)-get_ledgervaltest($rowl['id'],'C','O',$fdate));
$ldebit=get_ledgervaltest($rowl['id'],'D','D',$fdate,$tdate);
$lcredit=get_ledgervaltest($rowl['id'],'C','D',$fdate,$tdate);
$lnet=($ldebit-$lcredit);
$lcval=($lopval+$lnet);    
?>
<?php if(ABS($lcval)>0){?>    
<tr>
<td style="padding-left:30px;"><?php echo $rowl['code'];?>: <?php echo $rowl['ledger'];?></td>
<td class="text-right"><?php echo number_format($lopval,2);?></td>
<td class="text-right"><?php echo number_format($lnet,2);?></td>
<td class="text-right"><?php echo number_format($lcval,2);?></td>    
</tr> 
<?php } ?>   
<?php }} ?>
<?php if(ABS($gcval)>0){?>    
<tr>
<td style="text-align: right;padding-right:20px;"><strong>Total <?php echo $rowg['groups'];?></strong></td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gopval),2);?></b>
</td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gnet),2);?></b>
</td>
<td style="text-align: right; border-top: 1px double #000000;"><b><?php echo number_format(ABS($gcval),2);?></b>
</td>    
</tr>
<?php } ?>    
<?php } ?>
<?php 
$ecopval=(get_goupvaltest('4','D','O','C',$fdate)-get_goupvaltest('4','C','O','C',$fdate));
$ecdebit=get_goupvaltest('4','D','D','C',$fdate,$tdate);
$eccredit=get_goupvaltest('4','C','D','C',$fdate,$tdate);
$ecnet=($ecdebit-$eccredit);
$eccval=($ecopval+$ecnet);    
?>    
<?php if(ABS($eccval)>0){?>     
<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($ecopval,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($ecnet,2);?></b></td>
<td style="text-align: right; border-top: 1px double #000000; border-bottom: 4px double #000000;"><b><?php echo number_format($eccval,2);?></b></td>    
</tr>
<?php } ?>
</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(($rcopval-$ecopval),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(($rcnet-$ecnet),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(($rccval-$eccval),2);?></b>
</td>    
</tr>
</tbody>
</table>    
</div>    
</div>
</div>
</div>    
</div>
<div class="col-md-3">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">Filter</h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<div class="col-md-12 popup_details_div">
<div class="col-md-1"></div>
<form action="fin_proandlos.php"  method="post" >
<div class="col-md-10">    
<div class="form-group" >
<label>From:</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
<input type="text" maxlength="18" class="form-control datetimepicker" name="tfdate" placeholder="From Date" autocomplete="off" readonly>
</div>
</div>
<div class="form-group" >
<label>To:</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
<input type="text" maxlength="18" class="form-control datetimepicker" name="ttdate" placeholder="End Date" autocomplete="off" readonly>
</div>
</div>
</div>    
<div class="col-md-1"></div>    
</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-6"></div>
<div class="col-md-6 text-right" >
<input type="button" id="reset" class="btn btn-flat bg-red btn-sm " value="Reset"/>    
<input type="submit" id="plview" class="btn btn-flat bg-purple btn-sm" value="Submit"/>
</div> 
</div>    
</div>
</div>
</div>
</form>
</div>    
</div>
</div>    

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

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
// ReadData();
// function ReadData(){
// $.ajax({
// url: "fin_plview.php",
// method: "POST",
// data:{ 
// body:1
// },
// success: function(data){
// $('#itemdata').html(data);   
// }
// })
// };
    
// $(document).on('click', '#plview', function(e) {
// var fdate = $('#stdate').val();
// var tdate = $('#endate').val();
// toastr.options = {'positionClass': 'toast-top-center'};
    
// if(fdate.length<=0){
// toastr.info('Please Enter From Date!');
// return;    
// }

// if(tdate.length<=0){
// toastr.info('Please Enter To Date!');
// return;    
// }

// if(Date.parse(tdate) <= Date.parse(fdate)){
// toastr.info('To Date Must Greater or Equel than From Date!');
// return;    
// }    

// // $.ajax({
// // url: "fin_plview.php",
// // method: "POST",
// // data:{ 
// // tfdate:fdate, ttdate:tdate
// // },
// // success: function(data){    
// // $('#itemdata').html(data);   
// // }
// // })    
    
// });    
</script>    
<!-- /page script -->
</html>    