<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');
$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];
$page_title="Statement of Changes in Equity";



?>


<!DOCTYPE html>
<html lang="en">


<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<?php
  do_calander('#fdate');
  do_calander('#tdate');

  ?>
  

<body>

  <!-- ======= Header ======= -->
 
  
  <?php 
  require_once('inc_files/inc_header.php');
  ?>
  
  <!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
		  <li class="breadcrumb-item"> Reports</li>    
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
	  
	
		<div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
            <p class="card-title" style="font-size:14px; margin-bottom:0; padding-bottom:0;"><a href="61_reports_all.php?active_id=901&menu_id=9">< Back To Report List </a></p>

              <!-- No Labels Form -->
           <form class="row g-3" action="" method="post" name="codz" id="codz">
   		       
			
	<table width="80%" border="0" align="center">
      
      
      <tr height="50">
        <td width="30%" align="right">Date Interval:&nbsp;&nbsp;</td>
        <td width="20%"  align="center"><strong>
          <input type="text" name="fdate" id="fdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['fdate'])?$_REQUEST['fdate']:date('Y-01-01');?>" />
        </strong></td>
        <td width="5%" align="center" ><strong> -to- </strong></td>
        <td width="20%"  align="center" ><strong>
          <input type="text" name="tdate" id="tdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['tdate'])?$_REQUEST['tdate']:date('Y-m-d');?>" />
        </strong></td>
        <td width="25%" rowspan="3" align="center" ><strong>
          &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
        </strong></td>
      </tr>
      
      
    </table>
               
  </form><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body" style="height:400px;">
			<br>
			
			
             
			
			 
             

              <!-- Table with stripped rows -->
              <table width="36%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff; border-top: 1px solid #CCCCCC;">
                    <th width="40%" scope="col">&nbsp;Particulars </th>
                    <th width="20%" scope="col"><div style="text-align:right">Share Capital</div></th>
                    <th width="20%" scope="col"><div style="text-align:right">Retained Earnings</div></th>
                    <th width="20%" scope="col"><div style="text-align:right">Total&nbsp;</div> </th>
                  </tr>
                </thead>
                <tbody>
<?php
				
 // $gl_group = $_REQUEST['gl_group'];
//  $group=explode(' :: ',$gl_group);
//  $group_id=$group[0];
//  
//if($group_id!=''){
//$group_con=" and g.group_id='".$group_id."'";
//}


if($_REQUEST['fdate']==''){
$_REQUEST['fdate']=date('Y-01-01');
}

if($_REQUEST['tdate']==''){
$_REQUEST['tdate']=date('Y-m-d');
}

//if($_POST['fdate']!=''&&$_POST['tdate']!='')
$date_con .= ' and m.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'"';
		 
		 $sql = "select a.ledger_group_id,  sum(j.cr_amt-j.dr_amt) as opening_equity_amt from ledger_group g, accounts_ledger a, journal j 
		 where  g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and  j.jv_date<'".$_REQUEST['fdate']."' and g.group_id=21001 group by a.ledger_group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $opening_equity_amt=$info->opening_equity_amt;
		 }
		  
		 $sql = "select sum(j.cr_amt-j.dr_amt) as ope_retained_sales_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (3) and j.jv_date <'".$_REQUEST['fdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $ope_retained_sales_amt=$info->ope_retained_sales_amt;
		 }
		 
		 $sql = "select sum(j.dr_amt-j.cr_amt) as ope_retained_expense_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (4) and j.jv_date <'".$_REQUEST['fdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $ope_retained_expense_amt=$info->ope_retained_expense_amt;
		 }
		 
		 $opening_retained_earnings=$ope_retained_sales_amt-$ope_retained_expense_amt;
		 $total_opening_equity_retained_earnings=$opening_equity_amt+$opening_retained_earnings;		 
		 
		 
		 $sql = "select a.ledger_group_id,  sum(j.cr_amt-j.dr_amt) as current_equity_amt from ledger_group g, accounts_ledger a, journal j 
		 where  g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' and g.group_id=21001 group by a.ledger_group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $current_equity_amt=$info->current_equity_amt; 
		 }
		  
		 $sql = "select sum(j.cr_amt-j.dr_amt) as current_retained_sales_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (3) and j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $current_retained_sales_amt=$info->current_retained_sales_amt;
		 }
		 
		 $sql = "select sum(j.dr_amt-j.cr_amt) as current_retained_expense_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (4) and j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $current_retained_expense_amt=$info->current_retained_expense_amt;
		 }
		 
		 $current_retained_earnings=$current_retained_sales_amt-$current_retained_expense_amt;
		 $total_current_equity_retained_earnings=$current_equity_amt+$current_retained_earnings;
		 
		 
		$sql = "select a.ledger_group_id,  sum(j.cr_amt-j.dr_amt) as equity_amt from ledger_group g, accounts_ledger a, journal j 
		 where  g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and  j.jv_date<='".$_REQUEST['tdate']."' and g.group_id=21001 group by a.ledger_group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $equity_amt=$info->equity_amt;
		 }
		  
		 $sql = "select sum(j.cr_amt-j.dr_amt) as retained_sales_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (3) and j.jv_date <='".$_REQUEST['tdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $retained_sales_amt=$info->retained_sales_amt;
		 }
		 
		 $sql = "select sum(j.dr_amt-j.cr_amt) as retained_expense_amt from ledger_group g, accounts_ledger a, journal j 
		 where g.group_id=a.ledger_group_id and a.ledger_id=j.ledger_id and a.acc_class in (4) and j.jv_date <='".$_REQUEST['tdate']."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $retained_expense_amt=$info->retained_expense_amt;
		 }
		 
		 $retained_earnings=$retained_sales_amt-$retained_expense_amt;
		 $total_equity_retained_earnings=$equity_amt+$retained_earnings;
		 
		 			
		?>
				
			
                 
                 <tr>
                   <td>&nbsp;Balance as on <strong><?php echo date("d M, Y",strtotime($_REQUEST['fdate']))?></strong></td>
                   <td align="right"><?php echo number_format($opening_equity_amt,2);?></td>                   
                   <td align="right"><?php echo number_format($opening_retained_earnings,2);?></td>                  
                   <td align="right"><?php echo number_format($total_opening_equity_retained_earnings,2);?>&nbsp;</td>
				   </tr>
				  
	
                 <tr>
                   <td>&nbsp;Comprehensive Income/Loss for the year</td>
                   <td align="right"><?php echo number_format($current_equity_amt,2);?></td>
                   <td align="right"><?php echo number_format($current_retained_earnings,2);?></td>
                   <td align="right"><?php echo number_format($total_current_equity_retained_earnings,2);?>&nbsp;</td>
                  </tr>
                 <tr>
                   <td>&nbsp;Balance as on <strong><?=date("d M, Y",strtotime($_REQUEST['tdate']))?></strong></td>
                   <td align="right"><strong><?php echo number_format($equity_amt,2);?></strong></td>
                   <td align="right"><strong><?php echo number_format($retained_earnings,2);?></strong></td>
                   <td align="right"><strong><?php echo number_format($total_equity_retained_earnings,2);?></strong>&nbsp;</td>
                 </tr>
                </tbody>
              </table>
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

  
 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= End Footer ======= -->

</body>

</html>