<?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="Bank Book";



?>


<!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 and a.ledger_type="Bank"','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="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-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>
		
		<?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."' ";
}

if($_REQUEST['fdate']==''){
$_REQUEST['fdate']=date('Y-m-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-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 ref_no, full_name from accounts_reference where 1 group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $full_name[$info->ref_no]=$info->full_name;
		 }
		 
		 $sql = "select jv_no, view_jv_no from journal_master where 1 group by jv_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $view_jv_no[$info->jv_no]=$info->view_jv_no;
		 }

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

          <div class="card">
            <div class="card-body">
			<br>
			<?php if($ledger_id>0){?>
              <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 colspan="6" scope="col">&nbsp;To Receipt </th>
                    <th width="12%" scope="col"><div style="text-align:right"></div></th>
                  </tr>
                </thead>
                <tbody>

				
		
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="7%" scope="col" >&nbsp;Date</th>
                    <th width="9%" scope="col" >JV No </th>
                    <th width="26%" scope="col" >Account</th>
                    <th width="19%" scope="col" >Name</th>
                    <th width="20%" scope="col" >DESCRIPTION</th>
                    <th width="7%" scope="col" >Type</th>
                    <th width="12%" scope="col" ><div style="text-align:right">Amount&nbsp;</div></th>
                  </tr>
               
		
				
				
					<?php
					
	$tr_from_receive =" and j.tr_from in ('Receipt','Contra')";

	$rsql		= "select j.jv_no from journal j where j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."'  ".$tr_from_receive.$ledger_con." and j.dr_amt>0  order by j.jv_date,j.jv_no";

	$rquery		= mysqli_query($conn,$rsql);
	
	$rcount     = mysqli_num_rows($rquery);

	if($rcount>0)

	{

	while($infor=mysqli_fetch_object($rquery)){

	++$cr;

	if($cr==1){$jvsr .= $infor->jv_no;}

	else{$jvsr .= ','.$infor->jv_no;}

	}

	}
					
					
					
					

						 $sql = "select j.jv_no, j.jv_date, j.cr_amt as receive_amt, j.tr_from, j.narration, j.ref_no, a.ledger_name
						 from accounts_ledger a, ledger_group l, journal j 
where a.ledger_group_id=l.group_id and a.ledger_id=j.ledger_id  and j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' and j.cr_amt>0  and j.jv_no in (".$jvsr.")  
order by j.jv_date, j.tr_from, j.jv_no";
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td>&nbsp;<?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 $view_jv_no[$data->jv_no]?></a></td>
                   <td><?php echo $data->ledger_name;?></td>
                   <td><?php echo $full_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->receive_amt,2); $tot_receive_amt +=$data->receive_amt;?></td>
                  </tr>
				  <?php }?>
	
                  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;TOTAL RECEIVED</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_receive_amt,2); ?></strong></td>
                  </tr>
				  
				  
				   <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#EAEAEA; border-top: 1px solid #CCCCCC;">
                    <th colspan="6" scope="col">&nbsp;By Payment </th>
                    <th width="12%" scope="col"><div style="text-align:right"></div></th>
                  </tr>
				  
				  
				  
				  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="7%" scope="col" >&nbsp;Date</th>
                    <th width="9%" scope="col" >JV No </th>
                    <th width="26%" scope="col" >Account</th>
                    <th width="19%" scope="col" >Name</th>
                    <th width="20%" scope="col" >DESCRIPTION</th>
                    <th width="7%" scope="col" >Type</th>
                    <th width="12%" scope="col" ><div style="text-align:right">Amount&nbsp;</div></th>
                  </tr>
               
		
				
				
					<?php
					
	$tr_from_payment =" and j.tr_from in ('Payment','Contra')";

	 $psql		= "select j.jv_no from journal j where j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."'  ".$tr_from_payment.$ledger_con." and j.cr_amt>0  order by j.jv_date,j.jv_no";

	$pquery		= mysqli_query($conn,$psql);
	
	$pcount     = mysqli_num_rows($pquery);

	if($pcount>0)

	{

	while($infop=mysqli_fetch_object($pquery)){

	++$cp;

	if($cp==1){$jvsp .= $infop->jv_no;}

	else{$jvsp .= ','.$infop->jv_no;}

	}

	}
					
					
					
					

						  $sql = "select j.jv_no, j.jv_date, j.dr_amt as payment_amt, j.tr_from, j.narration, j.ref_no, a.ledger_name
						 from accounts_ledger a, ledger_group l, journal j 
where a.ledger_group_id=l.group_id and a.ledger_id=j.ledger_id  and j.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' and j.dr_amt>0  and j.jv_no in (".$jvsp.")  
order by j.jv_date, j.tr_from, j.jv_no";
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td>&nbsp;<?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 $view_jv_no[$data->jv_no]?></a></td>
                   <td><?php echo $data->ledger_name;?></td>
                   <td><?php echo $full_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->payment_amt,2); $tot_payment_amt +=$data->payment_amt;?></td>
                  </tr>
				  <?php }?>
	
                  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;TOTAL PAYMENT</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_payment_amt,2); ?></strong></td>
                  </tr>
				  
				  <tr>
                   <td colspan="6" align="left">&nbsp;</td>
                   <td align="right">&nbsp;</td>
                  </tr>
				  
				  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;Opening balance as on <?php echo date('d M, Y',strtotime($_REQUEST['fdate']));?></strong></td>
                   <td align="right"><strong><?php echo number_format($opening_amt[$ledger_id],2); ?></strong></td>
                  </tr>
				  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;Received in this period</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_receive_amt,2); ?></strong></td>
                  </tr>
				  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;Total after received</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_after_receive_amt=($opening_amt[$ledger_id]+$tot_receive_amt),2); ?></strong></td>
                  </tr>
				  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;Payment in this period</strong></td>
                   <td align="right"><strong><?php echo number_format($tot_payment_amt,2); ?></strong></td>
                  </tr>
				  <tr>
                   <td colspan="6" align="left"><strong>&nbsp;Closing balance as on <?php echo date('d M, Y',strtotime($_REQUEST['tdate']));?></strong></td>
                   <td align="right"><strong><?php echo number_format($closing_amt[$ledger_id],2); ?></strong></td>
                  </tr>
                </tbody>
              </table>
			  <?php }?>
			
             

            </div>
          </div>

        </div>
		
		
		
		



        
      </div>
    </section>

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

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

</body>

</html>