<?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'];


//do_calander('#jv_date');
// Common Information

$page_title="IOU Settlement";
$page="29_voucher_iou_settlement.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='journal_master';	
$unique='jv_no';

$request_no=$_REQUEST['request_no'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where jv_no="'.$request_no.'"'));


$sjv_sql="SELECT sum(j.dr_amt) as sjv_amt FROM secondary_journal j, accounts_ledger a  WHERE j.ledger_id=a.ledger_id and j.jv_no='".$request_no."'";
$sjv_data = find_all_field_sql($conn,$sjv_sql);

$iou_sql="SELECT sum(j.dr_amt) as iou_amt FROM journal_iou_cash_receive j, accounts_ledger a  WHERE j.ledger_id=a.ledger_id and j.jv_no='".$request_no."'";
$iou_data = find_all_field_sql($conn,$iou_sql);
 

// End Common Information

if(isset($_POST['ins_master_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];
$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');
$group_for = $_SESSION['user']['group'];
$jv_date=$_POST['invoice_date'];
$ref_no_ms=$_POST['ref_no_ms'];
$special_note='IOU Settlement';
$jv_type=3;
$jv_special=1;
$jv_title=find_a_field($conn,'journal_type','jv_title','id="'.$jv_type.'"');

		$YR = date('Y',strtotime($jv_date));
 		$year = date('y',strtotime($jv_date));
  		$month = date('m',strtotime($jv_date));
 		$inv_cy_id = find_a_field($conn,'journal_master','max(jv_id)','year="'.$YR.'" and jv_type="'.$jv_type.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$invoice_no=''.$year.''.$month.''.$jv_type.''.$cy_id;
		$view_invoice_no=$jv_title.''.$year.''.$month.''.$jv_type.''.$cy_id;

  $master_ledger=$_POST['master_ledger'];
  $ms_ledger=explode(' ::',$master_ledger);
  $master_ledger_id=$ms_ledger[0];

$remarks = $_POST['remarks'];

$tr_from='Journal';
//$grp_sql="SELECT * FROM ledger_group  WHERE group_id='".$ledger_group_id."'";
//$grp_data = find_all_field_sql($conn,$grp_sql);

  $ins_sql = 'INSERT INTO  journal_master (jv_no, year, jv_id, view_jv_no, jv_type, jv_date, group_for, master_ledger_id, tr_from, remarks, status, entry_at, entry_by,  ref_no, jv_special, special_note, emp_code) VALUES

("'.$invoice_no.'", "'.$YR.'", "'.$cy_id.'", "'.$view_invoice_no.'", "'.$jv_type.'", "'.$jv_date.'", "'.$group_for.'", "'.$master_ledger_id.'", "'.$tr_from.'", "'.$remarks.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'",  "'.$ref_no_ms.'", "'.$jv_special.'", "'.$special_note.'", "'.$ref_no_ms.'")';
	
mysqli_query($conn,$ins_sql);


header("location:".$page.'&request_no='.$invoice_no."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>&request_no=<?=$invoice_no?>";
</script>-->

<?php

}




if(isset($_POST['ins_primary_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];

$ms_sql="SELECT * FROM journal_master  WHERE jv_no='".$request_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$jv_no=$ms_data->jv_no;
$jv_date=$ms_data->jv_date;
$relevant_ledger=$ms_data->master_ledger_id;
$tr_from=$ms_data->tr_from;
$tr_no=0;
$cc_code=0;
$tr_id=0;
$ref_no=$_POST['ref_no'];
$group_for=$ms_data->group_for;

$gl_account=$_POST['gl_account'];

  $ledger=explode(' ::',$gl_account);
  $ledger_id=$ledger[0];

$narration=$_POST['narration'];
$dr_amt=$_POST['dr_amt'];
$cr_amt=0;

if($dr_amt>0){
add_to_sec_journal($conn, $jv_no,  $jv_date,  $ledger_id, $ref_no, $narration,  $dr_amt,  $cr_amt,  $tr_from,  $tr_no,  $relevant_ledger,  $cc_code,  $tr_id,  $group_for,  $status,  $entry_by,  $entry_at);
}

header("location:".$page.'&request_no='.$jv_no."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>&request_no=<?=$jv_no?>";
</script>-->

<?php

}



if(isset($_POST['ins_return_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];

$ms_sql="SELECT * FROM journal_master  WHERE jv_no='".$request_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$jv_no=$ms_data->jv_no;
$jv_date=$ms_data->jv_date;
$iou_ledger=$ms_data->master_ledger_id;
$ref_no=$ms_data->ref_no;
$return_ledger_id=$_POST['return_ledger_id'];
$narration=$_POST['narration'];
$tr_from=$ms_data->tr_from;

$group_for=$ms_data->group_for;


$narration=$_POST['narration'];
$dr_amt=$_POST['dr_amt'];
$cr_amt=0;

if($dr_amt>0){
$ins_sql = 'INSERT INTO  journal_iou_cash_receive (jv_no, jv_date, ledger_id, ref_no, dr_amt, cr_amt, tr_from, tr_no, tr_date, tr_id, group_for, status, entry_by, entry_at, narration) VALUES

("'.$jv_no.'", "'.$jv_date.'", "'.$return_ledger_id.'", "'.$ref_no.'", "'.$dr_amt.'", "'.$cr_amt.'", "'.$tr_from.'", "'.$tr_no.'", "'.$tr_date.'", "'.$tr_id.'", "'.$group_for.'", "'.$status.'", "'.$entry_by.'", "'.$entry_at.'", "'.$narration.'")';
	
mysqli_query($conn,$ins_sql);
}

header("location:".$page.'&request_no='.$jv_no."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>&request_no=<?=$jv_no?>";
</script>-->

<?php

}




if(isset($_POST['confirm_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];

$ms_sql="SELECT * FROM journal_master  WHERE jv_no='".$request_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$jv_no=$ms_data->jv_no;
$jv_date=$ms_data->jv_date;
$ledger_id=$ms_data->master_ledger_id;
$relevant_ledger=0;
$tr_from=$ms_data->tr_from;
$tr_no=0;
$cc_code=0;
$tr_id=0;
$ref_no=$ms_data->ref_no;
$group_for=$ms_data->group_for;

$narration=$ms_data->remarks;
$cr_amt=$_POST['total_dr_amt'];
$dr_amt=0;
$total_iou_amt=$_POST['total_iou_amt'];


if($cr_amt>0){
add_to_sec_journal($conn, $jv_no,  $jv_date,  $ledger_id, $ref_no,  $narration,  $dr_amt,  $cr_amt,  $tr_from,  $tr_no,  $relevant_ledger,  $cc_code,  $tr_id,  $group_for,  $status,  $entry_by,  $entry_at);
}


$jv_approval = find_a_field($conn,'journal_type','jv_approval','journal_type="'.$tr_from.'"');

if($jv_approval=="Yes"){

sec_journal_journal($conn,$jv_no,$jv_no,$tr_from);

$time_now = date('Y-m-d H:i:s');

$up_sj='update secondary_journal set status="CHECKED", checked_at="'.$time_now.'", checked_by="'.$_SESSION['user']['id'].'" 
where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_sj);

$up_jv='update journal set status="CHECKED", checked_by="'.$_SESSION['user']['id'].'", checked_at="'.$time_now.'"
 where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jv);

$up_jm='update journal_master set status="CHECKED", checked_by="'.$_SESSION['user']['id'].'", checked_at="'.$time_now.'"
 where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jm);

}elseif($jv_approval=="No") {
$up_jm='update journal_master set status="UNCHECKED" where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jm);

$up_sj='update secondary_journal set status="UNCHECKED" where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_sj);
}


if($total_iou_amt>0){
auto_insert_iou_return_secoundary_journal($conn,$jv_no);
}



header("location:".$page."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>-->

<?php

}





if(isset($_POST['del_primary_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];

$ms_sql="SELECT * FROM journal_master  WHERE jv_no='".$request_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$status='MANUAL';
$entry_by = $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$jv_no=$ms_data->jv_no;
$tr_from=$ms_data->tr_from;

$del_id=$_POST['del_id'];

 $sql_delp = 'delete from secondary_journal where  id="'.$del_id.'"';
mysqli_query($conn,$sql_delp);

header("location:".$page.'&request_no='.$jv_no."");


?>

<?php /*?><script language="javascript">
window.location.href = "21_voucher.php?request_no=<?=$jv_no?>";
</script><?php */?>

<?php

}





if(isset($_POST['del_iou_data']))

{

$ms_sql="SELECT * FROM journal_master  WHERE jv_no='".$request_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$status='MANUAL';
$entry_by = $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$jv_no=$ms_data->jv_no;
$tr_from=$ms_data->tr_from;

$del_iou_id=$_POST['del_iou_id'];

$del_iou = 'delete from journal_iou_cash_receive where  id="'.$del_iou_id.'"';
mysqli_query($conn,$del_iou);

header("location:".$page.'&request_no='.$jv_no."");


?>

<?php /*?><script language="javascript">
window.location.href = "21_voucher.php?request_no=<?=$jv_no?>";
</script><?php */?>

<?php

}





?>



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

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

  

  
  <?php
  do_calander('#invoice_date');
  //create_combobox('master_ledger');
  ?>
 
 
 
 
<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">General Voucher</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-6">

          <div class="card">
            <div class="card-body">
              <p>&nbsp;</p>

              <!-- General Form Elements -->
              <form action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label"><span style="color:#FF0000;">Date</span></label>
                  <div class="col-sm-9">
			   <?php if($request_no>0){?> 
			    <input type="text" class="form-control" id="jv_date" name="jv_date" value="<?php echo $ms_data->jv_date; ?>" required readonly="">
			   <?php } else {?>
			   <input type="text" class="form-control" id="invoice_date" name="invoice_date" value="" required>
			   <!--<input type="time" class="form-control" id="time" name="time" value="" required>-->
			   <?php }?>
			   

                  </div>
				  
				  
				  
                </div>
                <div class="row mb-3">
                  <label for="inputEmail" class="col-sm-3 col-form-label">Voucher No</label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="jv_no" name="jv_no" value="<?php if($request_no>0){ echo $ms_data->jv_no; } ?>" readonly="" >
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputEmail" class="col-sm-3 col-form-label"><span style="color:#FF0000;">Employee</span></label>
                  <div class="col-sm-9">
			<?php if($request_no>0){?>
            <select id="emp_ref_no" name="emp_ref_no" required class="form-select" >

	<?php foreign_relation($conn,'accounts_reference','ref_no','concat(display_name," (EMP CODE: ",ref_no,")")',$_POST['emp_ref_no'],'ref_no="'.$ms_data->ref_no.'"');?>
		
        	</select>
			<?php } else {
			
			?>
			
					<select id="ref_no_ms" name="ref_no_ms"  class="form-select"  required >
                      <option></option>
 <?php foreign_relation($conn,'accounts_reference','ref_no','concat(display_name," (EMP CODE: ",ref_no,")")',$_POST['ref_no_ms'], 'ref_type=1 group by ref_no ');?>
                    </select>
			<?php }?>
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputEmail" class="col-sm-3 col-form-label"><span style="color:#FF0000;">GL Account</span></label>
                  <div class="col-sm-9">
			<?php if($request_no>0){?>
            <select id="master_ledger_id" name="master_ledger_id" required class="form-select" >

	<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['master_ledger_id'],'ledger_id="'.$ms_data->master_ledger_id.'"');?>
		
        	</select>
			<?php } else {
			
			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="General"','master_ledger');
			
			?>
			
			
			<input type="text" class="form-control" id="master_ledger" name="master_ledger" autocomplete="off"  value="" required>
			<?php }?>
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputEmail" class="col-sm-3 col-form-label"><span style="color:#FF0000;">Balance</span></label>
                  <div class="col-sm-3" >
				  <?php
if($ms_data->ref_no!=''){
$ms_ref_con=" and j.ref_no='".$ms_data->ref_no."' ";
}
  
if($ms_data->master_ledger_id!=''){
$ms_ledger_con=" and j.ledger_id='".$ms_data->master_ledger_id."' ";
}
  
  		$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  ".$ms_ref_con.$ms_ledger_con." ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $op_balance=$info->opening_amt;
		}
				  ?>
				  
			<?php if($request_no>0){?>
            <input type="text" class="form-control" id="gl_balance" name="gl_balance" value="<?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"; ?>"  readonly="" style="font-size:12px"> 
			<?php } ?>
                  </div>
				  <div class="col-sm-3">
				   <label for="inputEmail" class="col-sm-12 col-form-label"><span style="color:#FF0000;">Outstanding</span></label>
				  </div>
				  
				  <div class="col-sm-3">
				  

				  <?php if($request_no>0){?>
            <input type="text" class="form-control" id="outstanding_balance" name="outstanding_balance" 
			value="<?php echo number_format($outstanding_balance=$op_balance-($sjv_data->sjv_amt+$iou_data->iou_amt),2); ?>"  readonly="" style="font-size:12px">
			<?php } ?>
				  </div>
				  
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputPassword" class="col-sm-3 col-form-label">Note</label>
                  <div class="col-sm-9">
                    <textarea class="form-control" style="height: 80px" id="remarks" name="remarks" ><?php if($request_no>0){ echo $ms_data->remarks; } ?></textarea>
                  </div>
                </div>
                        
                <?php /*?><div class="row mb-3">
                  <label for="inputNumber" class="col-sm-3 col-form-label">Attach File(s)</label>
                  <div class="col-sm-9">
                    <input class="form-control" type="file" id="attached_file" name="attached_file" >
                  </div>
                </div><?php */?>
                   

                <div class="text-center">
				<?php if($request_no>0) {?>
                  <!--<button type="submit" class="btn btn-success btn-sm" name="up_master_data" id="up_master_data" >Update Data</button>-->
				 <?php }else {?>
				  <button type="submit" class="btn btn-primary btn-sm" name="ins_master_data" id="ins_master_data" >Initiate Entry</button>
				 <?php }?>
                 <!-- <button type="reset" class="btn btn-secondary" onClick="parent.location='<?php echo $page?>'" >Reset</button>-->
                </div>

              </form><!-- End General Form Elements -->

            </div>
          </div>
</div>


<div class="col-lg-6">

          <div class="card">
            <div class="card-body">
		     <h5 class="card-title"><strong>Cash on Cash Return</strong></h5>
			 
			 <form  action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
             <table class="table table-bordered ">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="35%" scope="col">GL ACCOUNT </th>
                    <th width="35%" scope="col">DESCRIPTION</th>
                    <th width="20%" scope="col">DEBITS</th>
                    <th width="10%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
					
	<select id="return_ledger_id" name="return_ledger_id" required class="form-select" >
	<option></option>

	<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['return_ledger_id'],'ledger_type!="General"');?>
		
   </select></th>
                    <td><input type="text" class="form-control" name="narration" id="narration"></td>
                    <td><input type="text" class="form-control" name="dr_amt" id="dr_amt" required></td>
                    <td> 
		<button type="submit" class="btn btn-light btn-sm" name="ins_return_data" id="ins_return_data" ><img src="view_file/add.png" width="30" height="30"/></button>					</td>
                  </tr>
                </tbody>
              </table>
                

              </form>
			  
			
			  
			  <table class="table table-bordered " style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="7%" scope="col">SL</th>
                    <th width="35%" scope="col">GL ACCOUNT </th>
                    <th width="29%" scope="col">DESCRIPTION</th>
                    <th width="19%" scope="col"><center>
                      DEBITS
                    </center></th>
                    <th width="10%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					$sl=1;
					
					 $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 j.*, a.ledger_name from journal_iou_cash_receive j, accounts_ledger a
						where j.ledger_id=a.ledger_id and j.jv_no="'.$request_no.'"  and j.dr_amt>0 order by id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                    
                  <tr>
                    <th scope="row"><?php echo $sl++;?></th>
                    <td><?php echo $data->ledger_name;?></td>
                    <td><?php echo $data->narration;?></td>
                    <td align="right"><?php echo number_format($data->dr_amt,2); $total_return_dr_amt +=$data->dr_amt;?>&nbsp;&nbsp;</td>
                    <td align="right">
					<form  action="" method="post" name="form1" id="form1" onSubmit="return check()">
					<input type="hidden" class="form-control" id="del_iou_id" name="del_iou_id" value="<?php echo $data->id; ?>" required readonly="">
					<button type="submit" class="btn btn-light btn-sm" name="del_iou_data" id="del_iou_data" >
					<img src="view_file/delete.png" width="20" height="20"/></button></form>
					</td>
                  </tr>
				  
				  <?php }?>
				  
				  <tr>
                      <th scope="row">&nbsp;</th>
                      <td>&nbsp;</td>
                      <td><strong>TOTAL</strong></td>
                      <td align="right"><strong><?php echo number_format($total_return_dr_amt,2);?></strong></td>
                      <td align="right">&nbsp;</td>
				  </tr>
                </tbody>
              </table>
			
			</div>
		</div>
</div>




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

          <div class="card">
            <div class="card-body">
               <br/>
 <form  action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
             <table class="table table-bordered ">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="30%" scope="col">GL ACCOUNT </th>
                    <th width="30%" scope="col">Sub Ledger</th>
                    <th width="20%" scope="col">DESCRIPTION</th>
                    <th width="12%" scope="col">DEBITS</th>
                    <th width="8%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
					
					<?php
					if($request_no>0) {
					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_id!="'.$ms_data->master_ledger_id.'" and  a.ledger_type="General"','gl_account');
					}
					?>
					
					
					<input type="text" class="form-control" name="gl_account" id="gl_account" autocomplete="off" required></th>
                    <th scope="row">
					<select id="ref_no" name="ref_no"  class="form-select"  style="width: 95%;">
                      <option></option>
 <?php foreign_relation($conn,'accounts_reference a, reference_type b','a.ref_no','concat(a.display_name," (Type: ",b.reference_type,")")',$ms_data->ref_no,
					  'a.ref_type=b.id group by a.ref_no ');?>
                    </select></th>
                    <td><input type="text" class="form-control" name="narration" id="narration"></td>
                    <td><input type="text" class="form-control" name="dr_amt" id="dr_amt" required></td>
                    <td> 
		<button type="submit" class="btn btn-primary btn-sm" name="ins_primary_data" id="ins_primary_data" ><i class="bi bi-plus"></i>ADD&nbsp;&nbsp;</button>
					</td>
                  </tr>
                </tbody>
              </table>
                

              </form><!-- End General Form Elements -->
			  
			  
			  

            </div>
          </div>
</div>



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

          <div class="card">
            <div class="card-body">
              
             <br/>
              <!-- Bordered Table -->
			
              <table class="table table-bordered " style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="2%" scope="col">SL</th>
                    <th width="10%" scope="col">GL CODE </th>
                    <th width="25%" scope="col">GL ACCOUNT </th>
                    <th width="25%" scope="col">Sub Ledger </th>
                    <th width="20%" scope="col">DESCRIPTION</th>
                    <th width="10%" scope="col"><center>
                      DEBITS
                    </center></th>
                    <th width="8%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					$sl=1;
					
					 $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 j.*, a.ledger_name from secondary_journal j, accounts_ledger a
						where j.ledger_id=a.ledger_id and j.jv_no="'.$request_no.'"  and j.dr_amt>0 order by id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                  <tr>
                    <th scope="row"><?php echo $sl++;?></th>
                    <th scope="row"><?php echo $data->ledger_id;?></th>
                    <td><?php echo $data->ledger_name;?></td>
                    <td><?php echo $display_name[$data->ref_no];?></td>
                    <td><?php echo $data->narration;?></td>
                    <td align="right"><?php echo number_format($data->dr_amt,2); $total_dr_amt +=$data->dr_amt;?>&nbsp;&nbsp;</td>
                    <td align="center">  <form  action="" method="post" name="form1" id="form1" onSubmit="return check()">
					<input type="hidden" class="form-control" id="del_id" name="del_id" value="<?php echo $data->id; ?>" required readonly="">
					<button type="submit" class="btn btn-danger btn-sm"  name="del_primary_data" id="del_primary_data">
					<i class="bi bi-exclamation-octagon"></i></button></form></td>
                  </tr>
				  
				  <?php }?>
                </tbody>
              </table>
			  
              <!-- End Bordered Table -->
			  <br>
			  <br>
			  <br>
			  <table class="table">
                
                <tbody>
				
				
                  <tr>
                    <th width="2%" scope="row">&nbsp;</th>
                    <th width="11%" scope="row">&nbsp;</th>
                    <td width="30%">&nbsp;</td>
                    <td width="26%" align="right"><strong>Total</strong></td>
                    <td width="24%" align="right"><strong><?php echo number_format($total_dr_amt,2);?>
					
					</strong>&nbsp;&nbsp;</td>
                    <td width="7%" align="right">&nbsp;</td>
                  </tr>
              </table>
			  <br>
			  <br>
			  
			  <hr>

           <form action="" method="post" name="form1" id="form1" onSubmit="return check()">   
<div class="text-left">

<input type="hidden" class="form-control" id="total_dr_amt" name="total_dr_amt" value="<?php echo $total_dr_amt; ?>" required readonly="">

<input type="hidden" class="form-control" id="total_iou_amt" name="total_iou_amt" value="<?php echo $iou_data->iou_amt; ?>" required readonly="">
				
                  <button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" >Save &amp; Publish</button> &nbsp;&nbsp;
				 
				  <button type="reset" class="btn btn-primary btn-sm" onClick="parent.location='<?php echo $page?>'" >Save as Draft</button> &nbsp;&nbsp;
				 
                  <!--<button type="submit" class="btn btn-danger btn-sm" name="cancel_data" id="cancel_data" >Cancel</button>-->
              </div>
        </form>

            </div>
          </div>

          

        </div>
      </div>
    </section>

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

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

<!--<script>
    $("#time").val("08:00");
  </script>-->

 <?php 
 selected_two("#ref_no");
 selected_two("#ref_no_ms");
 ?>
</body>

</html>