<?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="Pay Bills";
$page="54_supplier_pay_bill.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='journal_master';	
$unique='jv_no';

  $acc_reference = $_POST['acc_reference'];

  $reference=explode(' :: ',$acc_reference);
   $ref_no=$reference[0];
  
 
 
// End Common Information

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

{
//$_POST['entry_by'] = $_SESSION['user']['id'];
$status='CHECKED';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');
$group_for = $_SESSION['user']['group'];
$ref_no=$_POST['ref_no'];
$jv_date=$_POST['jv_date'];

		$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.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$jv_no=''.$year.''.$month.''.$cy_id;
		$view_jv_no='PY-'.$year.''.$month.''.$cy_id;
		
$master_ledger_id=$_POST['master_ledger_id'];
$remarks = $_POST['remarks'];
$jv_type=2;
$tr_from='Payment';
  
//$ref_sql="SELECT * FROM accounts_reference  WHERE ref_no='".$ref_no."'";
//$ref_data = find_all_field_sql($conn,$ref_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) VALUES

("'.$jv_no.'", "'.$YR.'", "'.$cy_id.'", "'.$view_jv_no.'", "'.$jv_type.'", "'.$jv_date.'", "'.$group_for.'", "'.$master_ledger_id.'", "'.$tr_from.'", "'.$remarks.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$ref_no.'")';
	
mysqli_query($conn,$ins_sql);


$sql = 'select m.jv_no, m.view_jv_no as jv_narration, m.jv_date, m.ref_no, sum(j.cr_amt) as inv_amt, j.ledger_id  from journal_master m, journal j
where m.jv_no=j.jv_no and m.ref_no="'.$ref_no.'" and m.tr_from="Purchase" and j.cr_amt>0 group by j.jv_no order by m.jv_date, m.jv_no';
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

if($_POST['pay_amt_'.$data->jv_no]>0)
{
$pay_amt= $_POST['pay_amt_'.$data->jv_no];

$ins_sqljv = 'INSERT INTO  journal_pay_bill (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) VALUES

("'.$jv_no.'", "'.$jv_date.'", "'.$data->ledger_id.'", "'.$ref_no.'", "'.$pay_amt.'", "'.$cr_amt.'", "'.$tr_from.'", "'.$data->jv_no.'", "'.$data->jv_date.'", "'.$tr_id.'", "'.$group_for.'", "'.$status.'", "'.$entry_by.'", "'.$entry_at.'")';
	
mysqli_query($conn,$ins_sqljv);

$ins_id=mysqli_insert_id($conn);

add_to_sec_journal($conn, $jv_no, $jv_date, $data->ledger_id, $ref_no, $data->jv_narration, $pay_amt, $cr_amt, $tr_from, $data->jv_no, $master_ledger_id, $cc_code, $ins_id, $group_for, $status, $entry_by, $entry_at);

}	
}

$narration='Pay Bills';

$total_amt=$_POST['total_amt'];

if($total_amt>0){
add_to_sec_journal($conn, $jv_no, $jv_date, $master_ledger_id, $ref_no, $narration,  $cr_amt,  $total_amt,  $tr_from,  $jv_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);

}






?>

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

<?php } ?>





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

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


  <?php
  do_calander('#jv_date');
  create_combobox('master_ledger');
  ?>
  
  
  
<script language="javascript">

function SUMcalculation(jv_no){
var total_amt = 0;

<?php

$sql = "select m.jv_no, m.view_jv_no, m.jv_date, sum(j.cr_amt) as inv_amt from journal_master m, journal j
where m.jv_no=j.jv_no and m.tr_from='Purchase' and m.ref_no='".$ref_no."' and j.cr_amt>0 group by j.jv_no order by m.jv_date, m.jv_no";
$query = mysqli_query($conn,$sql);
while($data=mysqli_fetch_object($query)){
?>
total_amt = total_amt + document.getElementById('pay_amt_<?php echo $data->jv_no?>').value*1;;
<?php
}

?>
document.getElementById('total_amt').value = total_amt;



}


</script>


 
 
 <script language="javascript">

function count(){



var total_unit=(document.getElementById('total_unit').value)*1; 

var unit_price=(document.getElementById('unit_price').value)*1;

document.getElementById('total_amt').value=(total_unit*unit_price);

}


function final_count(){



//var unit_price=(document.getElementById('unit_price').value)*1; 
//
//var bag_size=(document.getElementById('bag_size').value)*1;
//
//var init_bag_unit=(document.getElementById('init_bag_unit').value)*1;  
//
//var init_dist_unit=(document.getElementById('init_dist_unit').value)=(bag_size*init_bag_unit);
//
//document.getElementById('init_total_unit').value=(init_dist_unit*1);
//
//document.getElementById('init_total_amt').value=(unit_price*init_dist_unit);

var dist_unit=(document.getElementById('dist_unit').value)*1; 

document.getElementById('total_unit').value=(dist_unit*1);

var total_amt=(document.getElementById('total_amt').value)*1; 

document.getElementById('unit_price').value=(total_amt/dist_unit);



}

//window.onload = function() {
//  document.getElementById("itemin").focus();
//};


</script>


<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">Purchase Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

   

    <section class="section">
      <div class="row">
	  
	  <?php if ($ref_no=="") { ?>
	  <div class="col-lg-12">

          <div class="card">
            <div class="card-body">
              
			  <h5 class="card-title"><strong>&nbsp;</strong></h5>

              <!-- General Form Elements -->
                <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
                <div class="row mb-3" >
                  <label for="email" class="col-sm-2 col-form-label"><strong><center>Supplier</center></strong></label>
                  <div class="col-sm-6">
				  
				  <?php 
				 auto_complete_from_db_sql($conn,'SELECT concat(a.ref_no," :: ",a.full_name), concat(a.ref_no," :: ",a.full_name) FROM accounts_reference a
					 WHERE ref_type=3','acc_reference'); 
				 ?>
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="" required>
                  </div>
				  
				     <div class="col-sm-4">
					 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary btn-sm" name="submit" id="submit" >&nbsp;<i class="bi bi-arrow-down-circle me-1"></i> View Invoice&nbsp;&nbsp;&nbsp;</button>
					 </div>
				  
                </div>
				
		
  
              </form><!-- End General Form Elements -->

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


	  <?php }?>
	  
        <?php if(isset($_POST['submit'])){ ?>
<form action="" method="post" name="form1" id="form1" onSubmit="return check()"  style="font-size:14px;">
        <div class="col-lg-12">

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

              <!-- General Form Elements -->
              
			  
			  <div class="row mb-3">
                  <label for="inputText" class="col-sm-2 col-form-label"><span style="color:#FF0000;">Supplier</span></label>
                  <div class="col-sm-6">
			  
		  <select id="ref_no" name="ref_no"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'accounts_reference','ref_no','concat(ref_no," :: ",full_name)',$_POST['ref_no'],'ref_no="'.$ref_no.'"');?>
		
         </select>

                  </div>
			  
                </div>
			  
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-2 col-form-label"><span style="color:#FF0000;">Payment Date</span></label>
                  <div class="col-sm-6">
			  
			    <input type="text" class="form-control" id="jv_date" name="jv_date" value="<?php echo $ms_data->jv_date; ?>" required readonly="">

                  </div>
			  
                </div>
				
				
                
				<div class="row mb-3">
                  <label for="inputEmail" class="col-sm-2 col-form-label"><span style="color:#FF0000;">Payment Account</span></label>
                  <div class="col-sm-6">
			
      <select id="master_ledger_id" name="master_ledger_id" required class="form-select" >
	  
	  <option></option>

	<?php foreign_relation($conn,'accounts_ledger','ledger_id','concat(ledger_id," :: ",ledger_name)',$_POST['master_ledger_id'],'ledger_type!="General"');?>
		
      </select>
			
			
			
                  </div>
                </div>
				<div class="row mb-3">
                  <label for="inputPassword" class="col-sm-2 col-form-label">Note</label>
                  <div class="col-sm-6">
                    <textarea class="form-control" style="height: 80px" id="remarks" name="remarks" ></textarea>
                  </div>
                </div>
                        
          
              <!-- End General Form Elements -->

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

<?php }?>

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

          <div class="card">
            <div class="card-body">
              
              <h5 class="card-title"><strong>Outstanding Transactions</strong></h5>
              <!-- Bordered Table -->
			
              <table width="35%" class="table table-bordered " style="font-size:12px;">
                <thead>
                  <tr>
                    <th width="6%" scope="col">SL</th>
                    <th width="24%" scope="col">Bill DATE</th>
                    <th width="19%" scope="col">Bill NO </th>
                    <th width="14%" scope="col">DUE DAYS </th>
                    <th width="12%" scope="col" align="right">Bill AMT</th>
                    <th width="11%" scope="col" align="right">DUE AMT</th>
                    <th width="14%" scope="col" align="right"><center>
                      PAYMENT
                    </center></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					$sl=1;
					
					
					$sql = "select sum(dr_amt) as paid_amt, tr_no  from journal_pay_bill where tr_from='Payment' group by tr_no ";
					$query = mysqli_query($conn,$sql);
					while($data=mysqli_fetch_object($query)){
					$paid_amt[$data->tr_no]=$data->paid_amt;
					
					}
					
					
						$sql = 'select m.jv_no, m.view_jv_no, m.jv_date, m.ref_no, sum(j.cr_amt) as inv_amt, j.ledger_id  from journal_master m, journal j
						where m.jv_no=j.jv_no and m.ref_no="'.$ref_no.'" and m.tr_from="Purchase" and j.cr_amt>0 group by j.jv_no order by m.jv_date, m.jv_no';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
					
<?php
$t_date=date('Y-m-d');
$f_dt = $data->jv_date;		   
$t_dt =  date('Y-m-d', strtotime('+1 day', strtotime($t_date)));

$from_date = strtotime($f_dt);
$to_date = strtotime($t_dt);
$datediff = $to_date - $from_date;
 $count_days = round($datediff / (60 * 60 * 24));

		   ?>
				
				<?php if ($due_amt=$data->inv_amt-$paid_amt[$data->jv_no]>0) {?>
                  <tr>
                    <td scope="row"><?php echo $sl++;?></td>
                    <td><?php echo date('d-M-Y',strtotime($data->jv_date));?></td>
                    <td><a href="view_file/purchase_bill_print_view.php?jv_no=<?php echo $data->jv_no;?>" target="_blank"><?php echo $data->view_jv_no;?></a></td>
                    <td><?php echo $count_days;?></td>
                    <td align="right"><?php echo number_format($data->inv_amt,2);?></td>
                    <td align="right"><?php echo number_format($due_amt=$data->inv_amt-$paid_amt[$data->jv_no],2);?></td>
                    <td align="right"> <input class="form-control" name="pay_amt_<?php echo $data->jv_no?>" id="pay_amt_<?php echo $data->jv_no?>" type="text" onKeyUp="SUMcalculation(<?php echo $data->jv_no?>)" value=""  style="height:33px" />	</td>
                  </tr>
				  <?php } }?>
                  
				  
				  
                </tbody>
              </table>
			  
              <!-- End Bordered Table -->
			  
			  <br>
			  <br>
			  
			  <table class="table">
                
                <tbody>
					
				  
				  <th width="6%" scope="col">&nbsp;</th>
                    <th width="24%" scope="col">&nbsp;</th>
                    <th width="19%" scope="col">&nbsp;</th>
                    <th width="14%" scope="col">&nbsp;</th>
                    <th width="12%" scope="col" align="right">&nbsp;</th>
                    <th width="11%" scope="col" align="right">TOTAL</th>
                    <th width="14%" scope="col" align="right"><input name="total_amt" id="total_amt" type="text" class="form-control" readonly=""  value="<?php echo $total_amt?>"  /></th>
				</tbody>
                  
              </table>
			  <br>
			 
			  
			  
          
<div class="text-left" align="right">
 
				 
				  <button type="reset" class="btn btn-primary btn-sm" onClick="parent.location='<?php echo $page?>'" >&nbsp;&nbsp;Clear&nbsp;&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp
				  <button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" >&nbsp;&nbsp;Save &amp; Close&nbsp;&nbsp;</button>
                  
              </div>
       
		
		 <br>

            </div>
          </div>

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

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

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

</body>

</html>