<?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="Trial Balance Periodical";

$grp_sql="SELECT * FROM user_group  WHERE id='".$_SESSION['user']['group']."'";
$grp_data = find_all_field_sql($conn,$grp_sql);

?>


<!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 align="right">GL Group:&nbsp;&nbsp;</td>
        <td colspan="3">
		 <?php
			 auto_complete_from_db_sql($conn,'SELECT concat(a.group_id," :: ",a.group_name), concat(a.group_id," :: ",a.group_name) FROM ledger_group a
			WHERE 1 order by a.group_id','gl_group');
		?>
                  <input type="text" class="form-control" id="gl_group" name="gl_group" value="<?php echo $_REQUEST['gl_group']?>" >		</td>
        <td 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>
      
      <tr height="50">
        <td align="right">Date Interval:&nbsp;&nbsp;</td>
        <td width="156"><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="61" align="center" ><strong> -to- </strong></td>
        <td width="208" ><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>
      </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  class="table table-sm" style="font-size:12px;" id="data_table" width="100%">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col">GL Code</th>
                    <th width="30%" scope="col">GL Account </th>
                    <th width="15%" scope="col"><div style="text-align:right">OPENING</div></th>
                    <th width="15%" scope="col"><div style="text-align:right">DEBITS</div> </th>
                    <th width="15%" scope="col"><div style="text-align:right">CREDITS</div></th>
                    <th width="15%" scope="col"><div style="text-align:right">BALANCE</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($_REQUEST['fdate']!=''&&$_REQUEST['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 ledger_id,  sum(dr_amt) as dr_amt, sum(cr_amt) as cr_amt from journal where jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' group by ledger_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $dr_amt[$info->ledger_id]=$info->dr_amt;
		 $cr_amt[$info->ledger_id]=$info->cr_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;
		 }

				?>
	
				
	<?php
	$sql_grp = 'select g.group_id, g.group_name from ledger_group g, accounts_ledger a where  g.group_id=a.ledger_group_id and g.acc_class in (1,2) '.$group_con.' group by g.group_id order by g.group_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->group_name; ?> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                  </tr>
               
		
				
				
					<?php

						$sql = 'select a.* from accounts_ledger a where a.ledger_group_id="'.$data_grp->group_id.'"  order by a.ledger_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data->ledger_id;?></a></td>
                   <td>
				   
				   <a href="61_account_01.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&gl_account=<?=$data->ledger_id.' :: '.$data->ledger_name;?>" target="_blank" style="text-decoration:none; "><?php echo $data->ledger_name;?></a></td>
                   <td align="right">
				   <?php
				    $opening_balance=$opening_amt[$data->ledger_id]; $tot_opening_amt +=$opening_amt[$data->ledger_id];
				    if($opening_balance>0) echo number_format($opening_balance,2).'(Dr)'; elseif($opening_balance<0) echo number_format(((-1)*$opening_balance),2).'(Cr)';else echo "0.00"; 			   
				   ?>
				   </td>
 				   <td align="right"><?php echo number_format($dr_amt[$data->ledger_id],2); $tot_dr_amt +=$dr_amt[$data->ledger_id];?></td>
                   <td align="right"><?php echo number_format($cr_amt[$data->ledger_id],2); $tot_cr_amt +=$cr_amt[$data->ledger_id];?></td>
                   <td align="right">
				   <?php 
				    $closing_balance=$closing_amt[$data->ledger_id]; $tot_closing_amt +=$closing_amt[$data->ledger_id];
				    if($closing_balance>0) echo number_format($closing_balance,2).'(Dr)'; elseif($closing_balance<0) echo number_format(((-1)*$closing_balance),2).'(Cr)';else echo "0.00";
				   ?></td>
                 </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>SUB TOTAL</strong></td>
                   <td align="right"><strong>
				   <?php 
				   $tot_opening_balance=$tot_opening_amt; 
			 if($tot_opening_balance>0) echo number_format($tot_opening_balance,2).'(Dr)'; elseif($tot_opening_balance<0) echo number_format(((-1)*$tot_opening_balance),2).'(Cr)';else echo "0.00";?>
				   </strong></td>
                   <td align="right"><strong><?php echo number_format($tot_dr_amt,2); $sub_tot_dr_amt+=$tot_dr_amt;?></strong></td>
                   <td align="right"><strong><?php echo number_format($tot_cr_amt,2); $sub_tot_cr_amt+=$tot_cr_amt;?></strong></td>
                   <td align="right"><strong>
				   
				   <?php
				    $tot_closing_balance=$tot_closing_amt; 
 if($tot_closing_balance>0) echo number_format($tot_closing_balance,2).'(Dr)'; elseif($tot_closing_balance<0) echo number_format(((-1)*$tot_closing_balance),2).'(Cr)';else echo "0.00";?>
				   </strong></td>
                 </tr>
				 <?php 
				 $tot_opening_amt = 0;
				 $tot_dr_amt = 0;
				 $tot_cr_amt = 0;
				 $tot_closing_amt = 0;
				 }?>
				 
				 
				 
				 
				 			
	<?php
$sql_grp_ex = 'select g.group_id, g.group_name from ledger_group g, accounts_ledger a where  g.group_id=a.ledger_group_id and g.acc_class in (3,4) '.$group_con.' group by g.group_id order by g.group_id';
$query_grp_ex = mysqli_query($conn,$sql_grp_ex);
while ($data_grp_ex = mysqli_fetch_object($query_grp_ex)) {
	?>		
                  <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_ex->group_name; ?> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="15%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                  </tr>
               
		
				
				
					<?php

						$sql_ex = 'select a.* from accounts_ledger a where a.ledger_group_id="'.$data_grp_ex->group_id.'"  order by a.ledger_id';
						$query_ex = mysqli_query($conn,$sql_ex);
						while ($data_ex = mysqli_fetch_object($query_ex)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data_ex->ledger_id;?></a></td>
                   <td>
				   
				   <a href="61_account_01.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&gl_account=<?=$data_ex->ledger_id.' :: '.$data_ex->ledger_name;?>" target="_blank" style="text-decoration:none; "><?php echo $data_ex->ledger_name;?></a></td>
                   <td align="right">
				    0.00 
				   </td>
 				   <td align="right"><?php echo number_format($dr_amt[$data_ex->ledger_id],2); $tot_dr_amt_ex +=$dr_amt[$data_ex->ledger_id];?></td>
                   <td align="right"><?php echo number_format($cr_amt[$data_ex->ledger_id],2); $tot_cr_amt_ex +=$cr_amt[$data_ex->ledger_id];?></td>
                   <td align="right">
				    0.00  
				  </td>
                 </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>SUB TOTAL</strong></td>
                   <td align="right"><strong>
				    0.00 
				   </strong></td>
                   <td align="right"><strong><?php echo number_format($tot_dr_amt_ex,2); $sub_tot_dr_amt_ex+=$tot_dr_amt_ex;?></strong></td>
                   <td align="right"><strong><?php echo number_format($tot_cr_amt_ex,2); $sub_tot_cr_amt_ex+=$tot_cr_amt_ex;?></strong></td>
                   <td align="right"><strong>
				    0.00 
				   </strong></td>
                 </tr>
				 <?php 
				 $tot_dr_amt_ex = 0;
				 $tot_cr_amt_ex = 0;			
				 }?>
				 
				 
				 
				 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>NET TOTAL</strong></td>
                   <td align="right"><strong>
				    0.00 
				   </strong></td>
                   <td align="right"><strong><?php echo number_format($net_tot_dr=$sub_tot_dr_amt+$sub_tot_dr_amt_ex,2); ?></strong></td>
                   <td align="right"><strong><?php echo number_format($net_tot_cr=$sub_tot_cr_amt+$sub_tot_cr_amt_ex,2); ?></strong></td>
                   <td align="right"><strong>
				    0.00 
				   </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 ======= -->

<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',
                },
                {
                    extend: 'pdf',
					
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									
messageTop: 'For the period from <?php echo "<strong>" ?><?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?><?php echo "</strong>" ?>',
                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>

<!--<script type="text/javascript">
$(document).ready(function() {
    // Check if DataTable is already initialized
    if ($.fn.DataTable.isDataTable('#example')) {
        // If DataTable is already initialized, destroy it
        $('#example').DataTable().destroy();
    }
    
    // Reinitialize DataTable with new options
    $('#example').dataTable({
        'iDisplayLength': 50
    });
});
</script>-->

</body>

</html>