<?php
  //invoice.php  
  include('db_con.php');
  include('function.php');
  
  if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
    $user_acc_no = $_SESSION['user_acc_no'];
}else{
  header('Location: login');
  die();
}
$dated = date('Y-m-d');
            
 
$pay_id = $_GET["id"];
$ref_id = $pay_id;
  
  
  
  
  ?>
<!DOCTYPE html>
<html>
<head>
  <title><?php echo basename(__FILE__, '.php'); ?></title>
  <?php include('include/head.php'); ?>
  <link rel="stylesheet" href="css/datepicker.css">
  <link rel="stylesheet" href="css/jquery.datetimepicker.css">
  <link rel="stylesheet" href="css/jquery.datetimepicker.min.css">
  <link rel="stylesheet" href="css/jquery.jquery-ui.min.css">
  
  
</head>
<body>
  <?php include('include/header.php'); ?>
  <?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Payables</a></li>
              <li class="breadcrumb-item"><a href="ar_cash_payment">Cash Payment</a></li>
              <li class="breadcrumb-item active">New Cash Payment Voucher </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
            <?php
    
if(isset($_GET["update"]) && isset($_GET["id"]))
      {
        $pay_id = $_GET["id"];
        $statement = $connect->prepare("
          SELECT r.dated, r.description, r.v_id ,a.acc_name name,r.amount FROM pay_voucher r,accounts a
            WHERE r.v_id=a.acc_no and pay_id = :pay_id
            LIMIT 1
        ");
        $statement->execute(
          array(
            ':pay_id'       =>  $_GET["id"]
            )
          );
        $result = $statement->fetchAll();
        foreach($result as $row)
        {
          
        ?>
        <script src="vendors/scripts/script.js"></script>
        <script>
        $(document).ready(function(){
          
          $('#dated').val("<?php echo $row["dated"]; ?>");
          $('#description').val("<?php echo $row["description"]; ?>");
          $('#v_id').val("<?php echo $row["v_id"]; ?>");
          $('#amount').val("<?php echo $row["amount"]; ?>");
         $('.select2').select2();
          
        });
        </script>
          
       <form method="post" id="data_form">
      <br />
      
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td colspan="2" align="center"><h2 style="margin-top:10.5px">Edit Cash Payment Detail</h2></td>
            </tr>
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-4">  
                      <input type="text" name="dated" id="dated" class="form-control input-sm" readonly AutoComplete="off" required />
                      <br />
                 
    
                 
                  
                  
                    </div>
                    <div class="col-md-8">
             
                   <br />
                   
                    
                     
                    
                    </div>
                  </div>
                   
               
                  
                  <table width="100%"id="item-table" class="table table-bordered">
                   <tr>
                      <th width="5%">Sr#.</th>
                      <th width="40%">To | Vendor</th>
                      <th width="40%">Description</th>
                     
                      <th width="15%">Amount</th>
                      <th></th>
                      
                      
                    </tr>
                      <tr>
                      <td><span id="sr_no">1</span></td>
                      
                      <td><select name="v_id" id="v_id" data-srno="1" class="form-control input-sm v_id  select2" required />
                        <option value="<?php echo $row["v_id"]; ?>"><?php echo $row["name"]; ?></option>
                        <?php echo fill_accounts($connect);?>
                        
                        </select></td>
                         <td><input type="text" name="description" id="description" data-srno="1" class="form-control input-sm description" value="<?php echo $row["description"]; ?>" /></td>
                      
                      <td><input type="text" name="amount" id="amount" data-srno="1" class="form-control input-sm number_only amount" value="<?php echo $row["amount"]; ?>" /></td>
                   </tr>
                   <?php
                 }
                 ?>
                    
                  </table>
                 
                  <div align="left">
                    <span>Balance : </span><span id="acc_balance"></span>
                  </div>
                  </td>
                  </tr>
                
             
              <tr>
                <td width="70%" align="right"><b>Total : <b></td><td align="right"><span id="total_amount"></span></b></td>
               
                </tr>
              <tr>
                <td colspan="2"></td>
              </tr>
             
              <tr>
                <td colspan="2" align="center">
                  <input type="hidden" name="total_item" id="total_item" value="0" />
                  <input type="hidden" name="btn_action" id="btn_action" />
                  <input type="hidden" name="pay_id" id="pay_id" value="<?php echo $pay_id;?>" />
                  <input type="submit" name="update" id="btn_save" class="form-control btn btn-primary btn_save" value="Update" />
                </td>
              </tr>
          </table>
        </div>
      </form>
    
      </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 
    </div>
    </div>
  
  
  <script src="js/jquery-ui.js"></script>
  <script src="js/jquery-ui.min.js"></script>
  
  
  
</body>
</html>
<script>
$(document).ready(function () {
    $('.btn_save').click(function(event) {
  if(!event.detail || event.detail == 1){ return true; }
  else { return false; }
});
});
</script>
  <script>
  var isSubmitting = false
$(document).ready(function () {
    $('#data_form').submit(function(){
        isSubmitting = true
    })
    $('#data_form').data('initial-state', $('#data_form').serialize());
    $(window).on('beforeunload', function() {
        if (!isSubmitting && $('#data_form').serialize() != $('#data_form').data('initial-state')){
            return 'You have unsaved changes which will not be saved.'
        }
    });
})
</script>
 <script>
 
  $(document).ready(function(){
    $(window).keydown(function(event){
    if(event.keyCode == 13) {
    
      event.preventDefault();
    }
  });
    $('#dated').datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
  })
  
  </script>
 <script type="text/javascript">
      $(document).ready(function(){
        var final_total_amt = $('#total_amount').text();
        var count = 0;
        
       
        $(document).on('blur', '.v_id', function(){
        var acc_no = $('#v_id'+count).val();
    
   
    
    $.ajax({
      url:"acc_balance.php",
      method:"POST",
      data:{acc_no:acc_no},
      dataType:"json",
      success:function(data)
      {       
        
        $('#acc_balance').html(data.acc_balance);
        
        
      }
    })
  });
        
        function cal_final_total(count)
        {
        
          var total_amount = 0;
         
          
          for(j=1; j<=count; j++)
          {
            
            
       
            var amount = $('#amount'+j).val();
           
            
            
            
           
            
            
        
            if(amount > 0)
            {
              total_amount = parseFloat(total_amount) + parseFloat(amount);
              
             
               
             
            }
            
          
            
          }
          
        
          $('#total_amount').text(total_amount);
        }
        
        $(document).on('submit', '#data_form',function(event){
            event.preventDefault();
              cal_final_total(count);
              
            $('#btn_save').val('Wait...');
            $('#btn_save').attr('disabled','disabled');
            $('#btn_action').val('update');
            var _data = $(this).serialize();
            $.ajax({
              url:"ap_cash_payment_action.php",
              method:"POST",
              data:_data,
              success:function(data)
              {
               $('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
               
                window.location = "ap_cash_payment";
              }
            })
          });
       
      
        
      
          
          
        
        $(document).on('blur', '.amount', function(){
          cal_final_total(count);
         
        });
   });     
</script>
  <?php
}
?>
