<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['hrm_id'])):
header('Location:../index.php');
endif;
$cval=0;
$edate=date("Y-m-d"); 
//$grn_no=0;

//////////////// Get Sequence Number ----------------------- 
 $querydoc=mysqli_query($con,"select * from sequence where seq_id=15 ")or die(mysqli_error());

    while($row=mysqli_fetch_array($querydoc)){ 
    $dn_no=$row['cno'];  
    }

//////////////////End of gettinh sequence Number ------------------
?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Sales Invox | Debit Note Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>
<style>
      div.scroll {
         display: block; 
            border: 0px solid red; 
            padding: 5px; 
            margin-top: 5px; 
            width: 100%; 
            height: 300px; 
            overflow-y: scroll; 
      }
    </style>

    <style>
#mydiv {
  width: 100%;
  padding: 5px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 10px;
}
</style>


</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">

          <div class="page-header">
            <h3 class="page-title">
                Debit Note
            </h3>
          
          </div>
          <div class="col-22 grid-margin">
              <div class="card">
                <div class="card-body">
              
                          
    <form class="needs-validation" novalidate method="post" action="debitnote_new_add.php" enctype="multipart/form-data" onsubmit="return validateForm()">                   
                    <div class="row">                     
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">DN No: </label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" id="rno" name="rno" value=<?php echo $dn_no ; ?> readonly>
    </div>
    </div>




                      </div>
                      <div class="col-md-4">                  
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Employee </label>
                          <div class="col-sm-9">
                         <select class="form-control select2" style="width: 100%;" name="emp_id" id="emp_id" required >
           <option value="0">~~~Select ~~~~</option>  
          <?php            
          $queryemp=mysqli_query($con,"SELECT *,
           CONCAT(`employee_master`.`emp_name`,', ',`employee_master`.`rep`) AS `emp`
          FROM employee_master  ")or die(mysqli_error());
          
          while($rowemp=mysqli_fetch_array($queryemp)){
          ?>
          <option value="<?php echo $rowemp['emp_id'];?>"><?php echo $rowemp['emp'];?></option>        
          <?php }?>
          </select> 
                          </div> 

                          </div>  

                      </div>                            
                  <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label"> Date: </label>
                          <div class="col-sm-8">
                            <input type="date" class="form-control" id="gdate" name="gdate" >
 
                          </div>
                        </div>

                    </div> <!-------End of first form raw------->

            

</div>
<div class="row">                     
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Amount: </label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" id="amount" name="amount" >
                          </div>
                        </div>




                      </div>
                      <div class="col-md-4">                  
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Customer </label>
                          <div class="col-sm-9">
                         <select class="form-control select2" style="width: 100%;" name="cus_id" id="cus_id" required >
           <option value="0">~~~Select ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select *,CONCAT(`customer_master`.`cus_name`,', ',`customer_master`.`cus_code`) AS `cus` from customer_master  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['cus_id'];?>"><?php echo $rowd['cus'];?></option>        
          <?php }?>
          </select> 
                          </div> 

                          </div>  

                      </div>                            
                  <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Ref: </label>
                          <div class="col-sm-8">
                            <input type="text" class="form-control" id="ref" name="ref" >

                          </div>
                        </div>

                    </div> <!-------End of first form raw------->


<!-------Third raw------->

</div>
<div class="row">                     
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Note </label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" id="note" name="note" >
                          </div>
                        </div>
                      </div>
                                         
                          <div class="col-md-4">                  
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Tot Apply </label>
                          <div class="col-sm-9">
                          <input id="totalInput"  class="form-control" type="number" name="totalInput" value="0";readonly>
                          </div> 

                          </div>  


                      </div> 
                    </div>
<!---------end of third raw----------->
<!------------------------     New Chq Raw -------------->


 <div class="scroll">             
                    <table id="item-disp"  class="table"> 
                        
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Document</th>
                          <th>Date</th>
                          <th>Type</th>
                          <th>Amount</th>
                          <th>Balance</th>
                          <th>Settle Amount</th>
                          <th>Apply</th>
                                                
</tr>
</thead>
</table>
</div>

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


<button type="submit" class="btn btn-primary">Save</button>
<button class="btn btn-light">Cancel</button>
</form>
</div>


  <div class="content-wrapper">

  </div>  <!----- <div class="content-wrapper">------>
  </div>
          
          
  </div>

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
</div>
</div>
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });

function validateForm() { 

    //````````route````````````````````````````````````
    if($('#bname').val().trim().length == 0) {
           //alert("Please enter the route you visited !!!");               
            return false;
    } ////``````````````````````````````````````````

    



 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

/////////////////////////////////////////
 function updateArray(appamt,rr) {
var ftot=0;
var docid=rr;
var tot_appamtValue=0;
var app=0;
 // var valx = parseFloat(val1.value);
var appamt_n=parseFloat(appamt);
var ptot=document.getElementById("totalInput").value;
var ptot_n=parseFloat(ptot);
var ftot=0;
ftot=appamt_n + ptot_n;
var receipt_amt1=document.getElementById("amount").value;
var receipt_amt=parseFloat(receipt_amt1);
//alert(receipt_amt);


var appamtElements = document.getElementsByName('appamt[]');

var tt=appamtElements.length;

  for (var i = 0; i < appamtElements.length; i++) {
     if (appamtElements[i].value>0){
      app=parseFloat(appamtElements[i].value);
      tot_appamtValue = tot_appamtValue  + app;
    }
  }


if (tot_appamtValue>receipt_amt){
alert("error");

}
else {
 document.getElementById("totalInput").value = tot_appamtValue ;
}




//var appamtValue = appamtElements[i].value;
//alert(tot_appamtValue);
//alert(appamt_n);
//alert(ptot_n);
//alert(ftot);
//alert(val);
//var bb=document.getElementById('ddc').value;
//alert(bb);


 }




  



  ///////////////////////////////
  $('#cus_id').on('change', function(){
  var cus_id = $('#cus_id').val();




  //alert(cus_id);  
 $("#item-disp tr").remove();
      $.ajax({
                 type: 'POST',
                 dataType: "json",
                 url: 'cus_excess_disp.php',
                        data: {
                        'cus_id' : cus_id,  
                         },
                cache: false,
                success: function(result1) {
                  // alert("Please enter the route you visited !!!");
                  

 var row = $('<tr class="bg-success text-black"><td width=10%>' + "Docid" + '</td><td width=10%>' + "Date" + '</td><td width=10%>'+ "Amount" + '</td><td width=10%>' + "Balance" +   '</td><td width=10%>' + "Apply Amount" +'</td>');
                 $('#item-disp').append(row);

                   for (var i=0; i<result1.length; i++) {
//var docid=result1[i].docid;
//var docid=document.getElementById('appamt[]').value;
//alert(docid);
var row = $('<tr class="table-success text-black"><td width=10%><input type="text" class="form-control" id="ddc" name="dd[]"  value="' + result1[i].docid+ '" readonly>' + '</td><td width=10%>' + result1[i].docdate +   '</td><td width=10%>' + result1[i].amount +   '</td><td width=10%>' + result1[i].bal +  
                        '</td><td width=10%><input type="hidden" class="form-control" id="doc[]" name="doc[]"  value="' + result1[i].docid+ '" readonly> <input type="text" class="form-control" id="appamt[]" name="appamt[]" onchange="updateArray(this.value, this.previousElementSibling.value)" >'  +
                       '</td>'  );

                        $('#item-disp').append(row);


                   } ///  for (var i=0; i<result1.length; i++) {

                   }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({

     })  //// end of $('#rtype').on('change', function(){

  ////////////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

</script>



