<?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="Balance Sheet Summary";



?>


<!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">
			<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:#EAEAEA; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col"> Code</th>
                    <th width="30%" scope="col"> Account </th>
                    <th width="15%" scope="col"><div style="text-align:right">Total</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 ledger_id,  sum(dr_amt-cr_amt) as opening_amt from journal where jv_date <'".$_REQUEST['fdate']."' group by ledger_id";
//		 $query = mysqli_query($conn,$sql);
//		 while($info=mysqli_fetch_object($query)){
//  		 $opening_amt[$info->ledger_id]=$info->opening_amt;
//		 }
		 
		 $sql = "select a.ledger_group_id,  sum(j.dr_amt-j.cr_amt) as assets_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.acc_class in (1) group by a.ledger_group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $assets_amt[$info->ledger_group_id]=$info->assets_amt;
		 
		 }
		 
		  $sql = "select a.ledger_group_id,  sum(j.cr_amt-j.dr_amt) as liabilities_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.acc_class in (2) group by a.ledger_group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $liabilities_amt[$info->ledger_group_id]=$info->liabilities_amt;
		 
		 }
		 
		// $sql = "select ledger_id,  sum(dr_amt-cr_amt) as closing_amt from journal where jv_date <='".$_REQUEST['tdate']."' group by ledger_id";
//		 $query = mysqli_query($conn,$sql);
//		 while($info=mysqli_fetch_object($query)){
//  		 $closing_amt[$info->ledger_id]=$info->closing_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;
		 			
		?>
				
				<?php
						$sql_grp = 'select g.acc_sub_class, c.sub_class_name from acc_sub_class c, ledger_group g, accounts_ledger a where c.id=g.acc_sub_class and g.group_id=a.ledger_group_id  
						and c.acc_class in (1) '.$group_con.'
						group by g.acc_sub_class order by c.id';
						$query_grp = mysqli_query($conn,$sql_grp);
						while ($data_grp = mysqli_fetch_object($query_grp)) {
				
				?>
				
				
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff;; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="30%" scope="col" ><?php echo $data_grp->sub_class_name; ?> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                  </tr>
               
				
		
				
				
				
				<?php			
					
						$sql = 'select g.* from ledger_group g, accounts_ledger a where g.group_id=a.ledger_group_id and g.acc_sub_class="'.$data_grp->acc_sub_class.'"
						 group by g.group_id order by g.group_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data->group_id;?></td>
                   <td>
				   
<a href="61_account_03.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&gl_group=<?=$data->group_id.' :: '.$data->group_name;?>" target="_blank" style="text-decoration:none; "><?php echo $data->group_name;?></a></td>
                   <td align="right">
				   <?php  echo number_format($assets_amt[$data->group_id],2); $sub_tot_assets_amt +=$assets_amt[$data->group_id]; ?></td>
                  </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>SUB TOTAL</strong></td>
                   <td align="right"><strong><?php echo number_format($sub_tot_assets_amt,2); $grand_tot_assets_amt +=$sub_tot_assets_amt;?></strong></td>
                  </tr>
				 <?php 
				 $sub_tot_assets_amt = 0;
				 }?>
				 
				 
				 <tr style="background:#f6f9ff; text-transform:uppercase;">
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL Assets</strong></td>
                   <td align="right"><strong><?php echo number_format($grand_tot_assets_amt,2);?></strong></td>
                  </tr>
				  
				  
				  
				  <?php
						$sql_grp = 'select g.acc_sub_class, c.sub_class_name from acc_sub_class c, ledger_group g, accounts_ledger a where c.id=g.acc_sub_class and g.group_id=a.ledger_group_id  
						and c.acc_class in (2) and c.id not in (21) '.$group_con.'
						group by g.acc_sub_class order by c.id';
						$query_grp = mysqli_query($conn,$sql_grp);
						while ($data_grp = mysqli_fetch_object($query_grp)) {
				
				?>
				
				
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff;; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="30%" scope="col" ><?php echo $data_grp->sub_class_name; ?> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                  </tr>
               
				
		
				
				
				
				<?php			
					
						$sql = 'select g.* from ledger_group g, accounts_ledger a where g.group_id=a.ledger_group_id and g.acc_sub_class="'.$data_grp->acc_sub_class.'"
						 group by g.group_id order by g.group_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data->group_id;?></td>
                   <td>
				   
<a href="61_account_03.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&gl_group=<?=$data->group_id.' :: '.$data->group_name;?>" target="_blank" style="text-decoration:none; "><?php echo $data->group_name;?></a></td>
                   <td align="right">
				   <?php  echo number_format($liabilities_amt[$data->group_id],2); $sub_tot_liabilities_amt +=$liabilities_amt[$data->group_id]; ?></td>
                  </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>SUB TOTAL</strong></td>
                   <td align="right"><strong><?php echo number_format($sub_tot_liabilities_amt,2); $grand_tot_liabilities_amt +=$sub_tot_liabilities_amt;?></strong></td>
                  </tr>
				 <?php 
				 $sub_tot_liabilities_amt = 0;
				 }?>
				 
				 
				 <tr style="background:#f6f9ff; text-transform:uppercase;">
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL Liabilities</strong></td>
                   <td align="right"><strong><?php echo number_format($grand_tot_liabilities_amt,2);?></strong></td>
                  </tr>
				  
				  
				  
				  <?php
						$sql_grp = 'select g.acc_sub_class, c.sub_class_name from acc_sub_class c, ledger_group g, accounts_ledger a where c.id=g.acc_sub_class and g.group_id=a.ledger_group_id  
						and c.acc_class in (2) and c.id in (21) '.$group_con.'
						group by g.acc_sub_class order by c.id';
						$query_grp = mysqli_query($conn,$sql_grp);
						while ($data_grp = mysqli_fetch_object($query_grp)) {
				
				?>
				
				
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff;; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="30%" scope="col" ><?php echo $data_grp->sub_class_name; ?> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                  </tr>
               
				
		
				
				
				
				<?php			
					
						$sql = 'select g.* from ledger_group g, accounts_ledger a where g.group_id=a.ledger_group_id and g.acc_sub_class="'.$data_grp->acc_sub_class.'"
						 group by g.group_id order by g.group_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data->group_id;?></td>
                   <td>
				   
<a href="61_account_03.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&gl_group=<?=$data->group_id.' :: '.$data->group_name;?>" target="_blank" style="text-decoration:none; "><?php echo $data->group_name;?></a></td>
                   <td align="right">
				   <?php  echo number_format($liabilities_amt[$data->group_id],2); $sub_tot_equity_amt +=$liabilities_amt[$data->group_id]; ?></td>
                  </tr>
				  <?php }?>
				  
				  <?php  number_format($sub_tot_equity_amt,2); $grand_tot_equity_amt +=$sub_tot_equity_amt;?>
				  
				  <tr>
                   <td>&nbsp;</td>
                   <td>
				   <a href="61_business_05.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>" target="_blank" style="text-decoration:none; ">Retained Earnings</a></td>
                   <td align="right">
				   <?php  echo number_format($retained_earnings,2);  ?></td>
                  </tr>
	
                 
				 <?php 
				 $sub_tot_equity_amt = 0;
				 }?>
				 
				
				 
				 
				 <tr style="background:#f6f9ff; text-transform:uppercase;">
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL Shareholders' equity</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_shareholders_equity_amt=$grand_tot_equity_amt+$retained_earnings,2);?></strong></td>
                  </tr>
				  
				  <tr style="background:#f6f9ff; text-transform:uppercase;">
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL Liabilities &amp; Equities</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_liabilities_equity_amt=$grand_tot_liabilities_amt+$tot_shareholders_equity_amt,2);?></strong></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>