<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php   

$from;
$to;

$branchID=$_SESSION['branch'];



if($_REQUEST['from']!=" " && $_REQUEST['from']!=NULL ) {
$from=date("Y-m-d",strtotime($_REQUEST['from']));  

}else {
$from=date("Y-m-01");  
}

if($_REQUEST['to']!=" " && $_REQUEST['to']!=NULL   ) {
$to=date("Y-m-d",strtotime($_REQUEST['to']));
}else {
$to=date("Y-m-t");  
} 

// getting branch details

$m->where("branch_id", $_SESSION['branch']);
$branch=$m->get("edu_branch_config");

$sql = "SELECT * FROM eduportal_chart_of_accounts where all_data='All' and parent_id='76'";

 // print_r($sql);
  $incomeAccounts = $m->query($sql);

$sql = "SELECT * FROM eduportal_chart_of_accounts where all_data='All' and parent_id='45'";

// print_r($sql);
$expenseAccounts = $m->query($sql);





?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Print Income Statement</title>
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<script type="text/javascript">
// window.print();
</script>

<style type="text/css">

tr th {border-top:1px solid gray;border-left:1px solid black;border-bottom:1px solid black;text-align:center;font-size:10px; padding:3px;}
tr td {border-bottom:1px solid gray;border-left:1px solid black;font-size:12px; padding:3px;}

.border{border:1px solid black;}
.center{text-align:center;}
.alignment{padding-left:1%;text-align:left;}
.borderTop{
border-top: none;
}
</style>

</head>

<body style="font-size:12px;">

<br>

<div class="page-header-space">
<div class="page-headers" style="text-align: center">
<div class="row text-center">
<div class="col-sm-12">
<span class="font-weight-bold" style="font-size: 2rem"><?php echo $brid[0]['Branch_Name'];?></span><br>
<span style="font-size:18px;">Income Statement</span> <br><br>
<span style="font-size:14px;">For the Period <?php echo date("01-M-Y");?> to <?php echo date("t-M-Y");?></span>
</div>
</div>    
</div>
</div>
<br>
<table id="listofstudents3" data-page-length='100' class="table table-striped table-bordered table-bordered" style="width:60%; margin-left: auto;margin-right: auto;border-right: 1px solid gray">

<thead>

<tr>
<th width="60%" style="background-color:#042954;color:#fff;font-size:16px;">Income Statement</th>
<th width="8%" style="background-color:#042954;color:#fff;font-size:16px;">RS</th>

</tr>          

<tbody class="">

<tr>

<td style="border-top: none;color: black;font-size: 16px;" colspan="2">
<center><strong>Income</strong></center>

</td>

</tr>
<?php foreach ($incomeAccounts as $incomeAccount) { 

$accountID=$incomeAccount['coa_id'];
$sql = "SELECT sum(debit_amount) as debit FROM eduportal_accounts_voucher_detailed_entries";
$sql .= " where voucher_date BETWEEN '" .$from. "' AND '" .$to. "' "; 
$sql .= " and branch_id='$branchID' and debit_account_id = '$accountID'";

$IncomeDebit = $m->query($sql);



$sql = "SELECT sum(credit_amount) as credit FROM eduportal_accounts_voucher_detailed_entries  ";
$sql .= "where voucher_date BETWEEN '" .$from. "' AND '" .$to. "' "; 
$sql .= "and branch_id='$branchID' and credit_account_id = '$accountID'";

$IncomeCredit = $m->query($sql);


$totalIncome+=$IncomeDebit[0]['debit']+$IncomeCredit[0]['credit'];
	?>

<tr>

<td style="font-size:14px;padding: 5px;"><?php echo $incomeAccount['account_title'];?></td>
<td style="font-size:14px;text-align: center;"><?php echo $IncomeDebit[0]['debit']+$IncomeCredit[0]['credit'];?></td>
</tr>
<?php } ?>


<tr style="background-color:#042954;">
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;">Total Income</td>
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;text-align: center;"><?php echo $totalIncome;?></td>

</tr>

<tr>

<td style="border-top: none;color: black;font-size: 16px;" colspan="2">
<center><strong>Expenses</strong></center>

</td>
</tr>

<?php foreach ($expenseAccounts as $expenseAccount) { 

$accountID=$expenseAccount['coa_id'];
$sql = "SELECT sum(debit_amount) as debit FROM eduportal_accounts_voucher_detailed_entries  ";
$sql .= "where voucher_date BETWEEN '" .$from. "' AND '" .$to. "' "; 
$sql .= "and branch_id='$branchID' and credit_account_id = '$accountID'";
//echo $sql;
$ExpenceDebite = $m->query($sql);


$sql = "SELECT sum(credit_amount) as credit FROM eduportal_accounts_voucher_detailed_entries  ";
$sql .= "where voucher_date BETWEEN '" .$from. "' AND '" .$to. "' "; 
$sql .= "and branch_id='$branchID' and debit_account_id = '$accountID' ";
//echo $sql;
$ExpenceCredit = $m->query($sql);


$totalExpense+=$ExpenceDebite[0]['debit']+$ExpenceCredit[0]['credit'];
	?>

<tr>
<td style="font-size:14px;padding: 5px;"><?php echo $expenseAccount['account_title'];?></td>
<td style="font-size:14px;text-align: center;"><?php echo $ExpenceDebite[0]['debit']+$ExpenceCredit[0]['credit'];?></td>


</tr>

<?php } ?>

<tr style="background-color:#042954;">
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;">Total Expenses</td>
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;text-align: center;"><?php echo $totalExpense;?></td>

</tr>

<tr style="background-color:#042954;">
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;text-align: center;">Net Profit</td>
<td style="font-weight: bold; color: #fff; font-size: 16px;padding: 5px;text-align: center;"><?php echo $totalIncome-$totalExpense;?></td>

</tr>

</tbody>



</table><br>


</body>
</html>
