<?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="Account Transactions";


$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 Account:&nbsp;&nbsp;</td>
          <td colspan="3">
		 			<?php
			
					auto_complete_from_db_sql($conn,'SELECT concat(a.ledger_id," :: ",a.ledger_name," >>> [GL Group: ",g.group_name,"]"), concat(a.ledger_id," :: ",a.ledger_name," >>> [GL Group: ",g.group_name,"]") FROM accounts_ledger a, ledger_group g 
					 WHERE a.ledger_group_id=g.group_id','gl_account');
				
					?>
					
					
					<input type="text" class="form-control" name="gl_account" id="gl_account" autocomplete="off" value="<?php echo $_REQUEST['gl_account']?>" required >
		  </td>
          <td rowspan="4" 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">Name:&nbsp;&nbsp;</td>
        <td colspan="3">
		 <?php 
				 auto_complete_from_db_sql($conn,'SELECT concat(a.ref_no," :: ",a.display_name," >>> [", t.reference_type,"]"), concat(a.ref_no," :: ",a.display_name," >>> [", t.reference_type,"]") FROM accounts_reference a, reference_type t
					 WHERE a.ref_type=t.id','acc_reference'); 
				 ?>
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="<?php echo $_REQUEST['acc_reference']?>" >		</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-m-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 width="100%" class="table table-sm" style="font-size:12px;" id="data_table">
                <thead>		
								
<?php
					
  $gl_account = $_REQUEST['gl_account'];
  $account=explode(' :: ',$gl_account);
  $ledger_id=$account[0];
  
if($ledger_id!=''){
$ledger_con=" and j.ledger_id='".$ledger_id."' ";
}


$ex_ledger_found = find_a_field($conn,'accounts_ledger','ledger_id','ledger_id="'.$ledger_id.'" and acc_class in (3,4)');

  $acc_reference = $_REQUEST['acc_reference'];
  $reference=explode(' :: ',$acc_reference);
  $ref_no=$reference[0];
  
if($ref_no!=''){
$ref_con=" and j.ref_no='".$ref_no."' ";
}


if($_REQUEST['fdate']==''){
$_REQUEST['fdate']=date('Y-m-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 sum(j.dr_amt-j.cr_amt) as opening_amt
		  from journal j, accounts_ledger a
		  where j.ledger_id=a.ledger_id and j.jv_date<'".$_REQUEST['fdate']."' ".$ref_con.$ledger_con." ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 
		 $op_balance=$info->opening_amt;
  		
		}
					
	?>
				
				
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970;  background:#f6f9ff; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col" style="white-space:nowrap;">Date</th>
                    <th width="10%" scope="col">JV No </th>
                    <th width="18%" scope="col">Account </th>
                    <th width="15%" scope="col">Name </th>
                    <th width="17%" scope="col">DESCRIPTION</th>
                    <th width="3%" scope="col">Type</th>
                    <th width="8%" scope="col"><div style="text-align:right">DEBITS</div> </th>
                    <th width="8%" scope="col"><div style="text-align:right">CREDITS</div></th>
                    <?php if($ex_ledger_found==0){?><th width="11%" scope="col"><div style="text-align:right">BALANCE</div></th><?php }?>
                  </tr>
                </thead>
                <tbody>
				

				<?php if($ex_ledger_found==0){?>
				<tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td><strong>Opening Balance<?php echo $ex_ledger_found;?></strong></td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="right">&nbsp;</td>
                   <td align="right" bgcolor="#f7f7f7">
				   <strong><?php if($op_balance>0) echo number_format($op_balance,2).'(Dr)'; elseif($op_balance<0) echo number_format(((-1)*$op_balance),2).'(Cr)';else echo "0.00"; ?></strong></td>
                 </tr>
				
				<?php }?>
		<?php
		
		$sql = "select ref_no, display_name from accounts_reference where 1 group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $display_name[$info->ref_no]=$info->display_name;
	
		}
		
	
						
					
					
					
						 $sql = 'select a.ledger_name, m.jv_no, m.view_jv_no, m.jv_date, j.ref_no, j.dr_amt, j.cr_amt, j.ledger_id, j.narration, j.tr_from
						from journal_master m, journal j, accounts_ledger a
						where m.jv_no=j.jv_no and j.ledger_id=a.ledger_id '.$ref_con.$date_con.$ledger_con.' order by m.jv_date, m.jv_no';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
					
					

				
                 
                 <tr>
                   <td style="white-space:nowrap;"><?php echo date('d-M-y',strtotime($data->jv_date));?></td>
                   <td><a href="view_file/journal_print_view.php?jv_no=<?php echo $data->jv_no;;?>" target="_blank"><?php echo $data->view_jv_no;?></a></td>
                   <td><?php echo $data->ledger_name;?></td>
                   <td><?php echo $display_name[$data->ref_no];?></td>
                   <td><?php echo $data->narration;?></td>
     			   <td><?php echo $data->tr_from;?></td>
 				   <td align="right"><?php echo number_format($data->dr_amt,2); $total_dr_amt+=$data->dr_amt;?></td>
                   <td align="right"><?php echo number_format($data->cr_amt,2); $total_cr_amt+=$data->cr_amt;?></td>
                   <?php if($ex_ledger_found==0){?>
				   <td align="right" bgcolor="#f7f7f7"><strong>
<?php $op_balance = $op_balance+($data->dr_amt-$data->cr_amt); if($op_balance>0) echo number_format($op_balance,2).'(Dr)'; elseif($op_balance<0) echo number_format(((-1)*$op_balance),2).'(Cr)';else echo "0.00"; ?></strong></td> <?php }?>
                 </tr>
				  <?php }?>
                 <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL</strong></td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="right"><strong><?php echo number_format($total_dr_amt,2);?></strong></td>
                   <td align="right"><strong><?php echo number_format($total_cr_amt,2);?></strong></td>
                    <?php if($ex_ledger_found==0){?><td align="right">&nbsp;</td><?php }?>
                 </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>