<?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'];
}else{
  header('Location: login');
  die();
}
$p_id = $_GET["id"];
$ref_id = $p_id;

    ?>
<!DOCTYPE html>
<html>
<head>
 
  <?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">
  <style>
    .select2-container{ width: 100% !important; }
  </style>
  <title>Edit Purchase Return</title>
</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><!-- /.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"]))
      {
        
           include('include/script.php');
        $s_id = $_GET["id"];
        $statement = $connect->prepare("
          SELECT m.dated,m.inv_no, m.v_id,a.acc_name name,m.discount,m.discount_dsc,m.freight,m.freight_dsc,m.due_day,m.gst_pr,m.fst_pr, m.ast_pr,m.gst_pr,m.description from  accounts a, pr m where m.v_id=a.acc_no and m.pr_id = :p_id
            LIMIT 1
        ");
        $statement->execute(
          array(
            ':p_id'       =>  $p_id
            )
          );
        $result = $statement->fetchAll();
       
  
 
        foreach($result as $row)
        {
          $gst_pr = $row["gst_pr"];
          $fst_pr = $row["fst_pr"];
          $ast_pr = $row["ast_pr"];
        ?>
        
        <script>
        $(document).ready(function(){
          
           $('#dated').val("<?php echo $row["dated"]; ?>");
          $('#inv_no').val("<?php echo $row["inv_no"]; ?>");          
          $('#p_id').val("<?php echo $p_id; ?>"); 
          $('#v_id').val("<?php echo $row["v_id"]; ?>");
          $('#gst_pr').val("<?php echo $row["gst_pr"]; ?>");
          $('#fst_pr').val("<?php echo $row["fst_pr"]; ?>");
          $('#ast_pr').val("<?php echo $row["ast_pr"]; ?>");
          $('#discount').val("<?php echo $row["discount"]; ?>");
          $('#discount_dsc').val("<?php echo $row["discount_dsc"]; ?>");
          $('#freight').val("<?php echo $row["freight"]; ?>");
          $('#freight_dsc').val("<?php echo $row["freight_dsc"]; ?>");
         
        
             
          $('#description').val("<?php echo $row["description"]; ?>");
          $('.select2').select2();
          $('.loaded').val(0);
        });
        </script>
        
        <form method="post" id="data_form" AutoComplete="off" />
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td colspan="2" align="center"><h2 style="margin-top:10.5px">Edit Purchase Return (<?php echo $_GET['id'];?>)</h2></td>
            </tr>
      
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-6">
                   
                      <br />
                        <select name="v_id" id="v_id" class="form-control select2" required />
                           <?php echo fill_party($connect);?>
                        </select>
                        <br />
                        <br />
                        
                        <input type="text" name="description" id="description" class="form-control description" placeholder="Enter Description"  />
                        <br />
                  
                  
                       
                    
                    </div>
                    <div class="col-md-6">
                     
                      <br />
                      <table>
                      <tr>
                         <td colspan="2">
                         <b>Date :</b>
                      <input type="text" name="dated" id="dated" class="form-control input-sm" placeholder="Select Date" readonly AutoComplete="off" required />
                      </td>
                      <td>
                      <b>Inv No. :</b><input type="text" name="inv_no" id="inv_no" class="form-control inv_no" value="">
                       <span id="inv" style="color:red"></span></td>
                       
                       
                      </tr>
                       <tr>
                         <td>
                         <b>GST :</b>
                      <input type="number" name="gst_pr" id="gst_pr" class="form-control gst_pr input-sm" placeholder="GST" value="<?php echo $gst_pr; ?>"   required />
                      </td>
                      <td>
                      <b>Further Tax. :</b><input type="number" name="fst_pr" id="fst_pr" class="form-control fst_pr" value="<?php echo $fst_pr;?>" required>
                       </td>
                        <td>
                      <b>Advance Tax. :</b><input type="number" name="ast_pr" id="ast_pr" class="form-control ast_pr" value="<?php echo $ast_pr;?>" required>
                       </td>
                       
                       
                      </tr>
                
                
                
                      </table>
                    
                      
                    </div>
                  </div>
                  <br />
                          <table id="items-table" class="table table-responsive table-bordered">
                     <thead>
                        <tr>
                        <td width="3%">#</td><td width="35%">Item</td><td width="10%">Qty</td><td width="15%">ListRate</td><td width="8%">Dis%</td><td width="14%">Rate</td><td width="15%">Amount</td>
                       </tr>
                     </thead>
                      
                <?php
                    $statement = $connect->prepare("
                      SELECT t.type_name,i.item_name,i.type_id,d.item_no,d.ctn,d.qty,d.rate,d.dis_pr from pr_detail d, items i,item_type t where t.type_id=i.type_id and i.item_id=d.item_no and d.pr_id = :p_id
                    ");
                    $statement->execute(
                      array(
                        ':p_id'       =>  $p_id
                      )
                    );
                    $item_result = $statement->fetchAll();
                    $m = 0;
                    $gross_amount = 0;
                    $amount =0;
                    foreach($item_result as $sub_row)
                    {
                      $dis_pr = $sub_row['dis_pr'];
                      $rate = $sub_row['rate'];
                      $qty = $sub_row['qty'];
                      $dis_rate =  $rate * $dis_pr / 100;
                      $dis_pr_rate = $rate - $dis_rate;
                       $item = $sub_row['type_name'] .' - '.$sub_row['item_name'];
                      $amount = $dis_pr_rate * $qty;
                      $item_amount = $amount;
                      $m = $m + 1;
                      $gross_amount = $gross_amount + $item_amount;
                    ?>
                     <tr id="row_id_<?php echo $m; ?>">
                      <td><span id="sr_no"><?php echo $m; ?></span></td>
                      <td><input type="hidden" name="item_no[]" class="form-control " value="<?php echo $sub_row["item_no"]; ?>" /><input type="text" name="item_name[]" class="form-control " step="any" value="<?php echo $item;?>" readOnly></td>
                    
                        
                      <td><input type="number" name="qty[]" id="qty<?php echo $m; ?>" class="form-control input-sm number_only qty tot" value="<?php echo $sub_row["qty"]; ?>" /></td>
                      <td><input type="number" name="rate[]" id="rate<?php echo $m; ?>" class="form-control input-sm number_only rate" value="<?php echo $sub_row["rate"]; ?>"/></td>
                      <td><input type="number" name="dis_pr[]" id="dis_pr<?php echo $m; ?>" class="form-control input-sm number_only dis_pr" value="<?php echo $sub_row["dis_pr"]; ?>"/></td>
                       <td><input type="number" name="dis_rate[]" id="rate<?php echo $m; ?>" class="form-control input-sm number_only dis_rate" value="<?php echo $dis_pr_rate; ?>" readOnly/></td>
                      <td><input type="number" name="amount[]" id="amount<?php echo $m; ?>"  class="form-control input-sm amount" value="" readonly /></td>                      
                      
                      <td><button type="button" name="remove_row[]" id="<?php echo $m; ?>" class="btn btn-danger btn-xs remove_row form-control">X</button> </td>
                      
                      
                     </td>
                    
                    </tr>
                    <?php
                   
                    }
                    ?>
                  
                  </table>
                   <table width="100%" id="invoice-item-table" class="table table-bordered">
                     <tr>        
       
           
          <td width="22%"><input type="hidden" name="item_id" id="item_id" class="form-control input-sm item_id" ><select name="type_id" id="type_id"  class="form-control input-sm type_id select2"><?php echo fill_itemtype($connect);?></select></td>
         <td width="23%"><input type="text" name="item_name" id="item_name" class="form-control input-sm number_only item_name" list="datalist1" placeholder="Description"></td>         
          <td width="10%"><input type="number" name="item_qty" id="item_qty"  class="form-control input-sm number_only item_qty" placeholder="Qty"></td>
          <td width="15%"><input type="number" name="item_rate" id="item_rate"  class="form-control input-sm number_only item_rate" step="any" placeholder="ListRate"></td>
          <td width="10%"><input type="number" name="item_dis_pr" id="item_dis_pr"  class="form-control input-sm number_only item_dis_pr" step="any" placeholder="Dis(%)"></td>
          <td width="15%"><input type="number" name="item_dis_rate" id="item_dis_rate"  class="form-control input-sm number_only item_dis_rate" step="any" placeholder="Rate" readonly><input type="hidden" name="item_cost" id="item_cost"  class="form-control input-sm number_only item_cost" step="any" placeholder="Cost" readonly></td>
             <td width="5%"><button type="button" name="add_row" id="add_row" class="form-control btn btn-success btn-xs"><i class="fa fa-download"></i></button></td></tr>
                  </table>
                        <div align="left">
                    <span id="item_info"></span>
                     <span id="item_detail"></span>
                     
                  </div>        
           
                  <div align="left">
                    <span id="item_info"></span>
                    <span id="item_detail"></span>
                  </div>
                     
                 
                </td>
              </tr>
               <tr>
                <td align="right"><b>Gross Amount</td>
                <td align="right"><b><span id="total_amount"></span></b></td>
              </tr>
             <tr>
                <td width="80%" align="right"><span>GST (%): <span id="gst_pr_text"><?php echo $gst_pr;?>%</span></td>
                <td width="20%" align="right"><input type="number" name="gst_amount" id="gst_amount" class="form-control gst_amount"  style="text-align:right" readonly></td>
              </tr>


           <tr>
                <td width="80%" align="right"><span>Further Tax (%) :  <span id="fst_pr_text"><?php echo $fst_pr;?>%</span></td>
                <td width="20%" align="right"><input type="number" name="fst_amount" id="fst_amount" class="form-control fst_amount" style="text-align:right" readonly></td>
              </tr>
        <tr>
                <td width="80%" align="right">Advance Tax (%): <span id="ast_pr_text"><?php echo $ast_pr;?>%</span></td>
                <td width="20%" align="right"><input type="number" name="ast_amount" id="ast_amount" class="form-control ast_amount" style="text-align:right" readonly></td>
              </tr>        
              
                <tr>
                <td width="80%"><input type="text" name="discount_dsc" id="discount_dsc" class="form-control discount_dsc" placeholder="Less Detail :"></td>
                <td width="20%" align="right"><input type="number" name="discount" id="discount" class="form-control discount" placeholder="Less Amount" style="text-align:right" value="0"></td>
              </tr>
                <tr>
                <td width="80%"><input type="text" name="freight_dsc" id="freight_dsc" class="form-control freight_dsc" placeholder="Add Detail :"></td>
                <td width="20%" align="right"><input type="number" name="freight" id="freight" class="form-control freight" placeholder="Add Amount" style="text-align:right" value="0"></td>
              </tr>
             
              <tr>
                <td width="80%"align="right"><b>Net Amount :</td>
                <td width="20%" align="right"><b><span id="net_total"></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="<?php echo $m;?>" />
                 <input type="hidden" name="loaded" id="loaded" class="loaded"  />
                <input type="hidden" name="p_id" id="p_id" class="p_id"  />
                   
                    
                    <input type="hidden" name="btn_action" id="btn_action" class="btn_action" /> 
                    <input type="hidden" name="first_row" id="first_row" class="first_row" value="0" /> 
                    <input type="hidden" name="cust_name" id="cust_name" /> 
                   
                  
                   <table>
                    <tr>   
                  <table>
                    <tr>   
                  <td><input type="submit" name="save" id="btn_save" class="form-control btn btn-primary" value="Update" /></td>
                 
                </tr>
              </table>
                </tr>
              </table>
                </td>
                <tr>
                
              </tr>
              </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>

  
<datalist id="datalist1">
  
</datalist>
  
</body>
</html>

 <script>
 
  $(document).ready(function(){
    $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
    
    $('#dated').datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
    $('.select2').select2();
  });
 
   
  </script>
     
    
      <script>
      $(document).ready(function(){
        
      
       var total_amount = $('#total_amount').text();
       var count = "<?php echo $m;?>";
         var loaded = $('#loaded').val();
       if(loaded==0){$('#v_id').focus();loaded=1;$('#loaded').val(1);cal_final_total(count);}
         

       $(document).on('click', '#add_row', function(){

          var item_no = $('#item_id').val();
           if(item_no==''){$('#type_id').focus();}
           var item_name = $('#item_name').val();
           var type_id = $('#type_id').select2('data')[0]['text'];
           var item = type_id+ ' - ' +item_name;
           var qty = $('#item_qty').val();
           var rate = $('#item_rate').val();
           var dis_pr = $('#item_dis_pr').val();
           var dis_rate = parseFloat(rate) + parseFloat(rate) * parseFloat(dis_pr) / 100;
           var amount = parseFloat(qty) * parseFloat(dis_rate);

           if(type_id ==''){$('#type_id').focus();return false;}
           if(item_name ==''){$('#item_name').focus();return false;}
           if(qty ==''){$('#item_qty').focus();return false;}
           if(rate ==''){$('#item_rate').focus();return false;}
          if(item_no > 0)
          {      
          

           count++;
           $('#total_item').val(count);
          var html_code = '';
          html_code += '<tr id="row_id_'+count+'">';
          html_code += '<td>'+count+'</td>';
          html_code += '<td><input type="hidden" name="item_no[]" id="item_no'+count+'" data-srno="'+count+'" class="form-control item_no input-sm" step="any" value="'+item_no+'" readOnly><input type="text" name="item_name[]" data-srno="'+count+'" class="form-control" value="'+item+'" readOnly></td>';                 
          html_code += '<td><input type="number" name="qty[]" id="qty'+count+'" data-srno="'+count+'" class="form-control input-sm qty" step="any" value="'+qty+'"></td>';
          html_code += '<td><input type="number" name="rate[]" id="rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only rate" step="any" value="'+rate+'" ></td>';
          html_code += '<td><input type="number" name="dis_pr[]" id="dis_pr'+count+'" data-srno="'+count+'" class="form-control input-sm number_only dis_pr" step="any" value="'+dis_pr+'" ></td>';
          html_code += '<td><input type="number" name="dis_rate[]" id="dis_rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only dis_rate" step="any" value="'+dis_rate+'" readOnly></td>';
          html_code += '<td><input type="number" name="amount[]" id="amount'+count+'" data-srno="'+count+'" class="form-control input-sm number_only amount" step="any" value="'+amount+'" readOnly></td>';        
       
          html_code += '<td><button type="button" name="remove_row" id="'+count+'" class="form-control btn-xs btn-danger btn-xs remove_row">X</button></td>';
          html_code += '</tr>';
          $('#items-table').append(html_code);

           $('#item_detail').text('');
           $('#item_qty').val('');
           $('#item_rate').val('');
           $('#item_name').val('');
           $('#item_dis_pr').val('');
           $('#item_dis_rate').val('');
           $('#item_cost').val('');
           $('#type_id').val('refresh');
           cal_final_total(count);          
          $('#type_id').focus();
        }else{return false;}
        
        });
        
      $(document).on('click', '.remove_row', function(){
            var row_id = $(this).attr("id");
           
            $('#row_id_'+row_id).remove();
             cal_final_total(count);
            count--;
            $('#total_item').val(count);
         });     
      $(document).on('select2:select','.select2', function(e){
            var x = $(this).val();
            $.ajax({
              url:"find_item.php",
              method:"POST",
              data:{type_id:x},
              dataType:"text",
              success:function(data)
              {
                
                $('#item_name').val('');
                $('#datalist1').html(data);
                
              
              }
           
         })
      });
       
       
      function cal_final_total(count)
        {
         var final_item_amount = 0;
         var  final_dis_amount = 0;
         var gst_pr = $('#gst_pr').val();
         var fst_pr = $('#fst_pr').val();
         var ast_pr = $('#ast_pr').val();
         var gst_amount= 0;
         var ast_amount = 0;
         var fst_amount = 0;
          for(j=1; j<=count; j++)
          {
            var qty = 0;
            var rate = 0;
            var amount = 0;
           var dis_pr = 0;
           var dis_rate = 0;
           
            var total_amount = 0;
           var dis_pr_rate = 0;
           
            qty = $('#qty'+j).val();
            dis_pr = $('#dis_pr'+j).val();
            

            if(qty > 0)
            {
              rate = $('#rate'+j).val();
              
             if(rate > 0)
              {
                amount = parseFloat(qty) * parseFloat(rate);
                $('#amount'+j).val(amount);             
                 if(dis_pr > 0)
                {
                  dis_rate = parseFloat(rate) * parseFloat(dis_pr)/100;
                  
                  dis_pr_rate = parseFloat(rate) - parseFloat(dis_rate);
                 amount = qty * dis_pr_rate;
                 $('#amount'+j).val(amount);   
                  $('#dis_rate'+j).val(dis_pr_rate); 
                //  $('#amount'+j).val(amount); 
                }else{$('#dis_rate'+j).val(dis_pr_rate);}
                 
                 item_amount = parseFloat(amount);
                final_item_amount = parseFloat(final_item_amount) + parseFloat(item_amount);
                    
               
               
            
              }
            }
          }
          var new_final_item_amount = Number(final_item_amount).toFixed(2);
          $('#total_amount').text(new_final_item_amount);
        

      
          net_total = parseFloat(final_item_amount) ;
          if(gst_pr > 0)
          {
            $('#gst_pr_text').text(gst_pr + '%');
            gst_amount = parseFloat(final_item_amount) * parseFloat(gst_pr)/100;
            $('#gst_amount').val(gst_amount);
          }
           if(fst_pr > 0)
          {
            $('#fst_pr_text').text(fst_pr+'%');
            fst_amount = parseFloat(final_item_amount) * parseFloat(fst_pr)/100;
            $('#fst_amount').val(fst_amount);
          }
           if(ast_pr > 0)
          {
            $('#ast_pr_text').text(ast_pr+'%');
            ast_amount = parseFloat(final_item_amount) * parseFloat(ast_pr)/100;
            $('#ast_amount').val(ast_amount);
          }
         
          //$('#gst_amount').val(gst_amount);
          net_total = parseFloat(final_item_amount) + parseFloat(gst_amount) + parseFloat(fst_amount) + parseFloat(ast_amount);
          
         // $('#total_item_discount').text(final_dis_amount);
          // $('#total_item_discount').text(final_dis_amount);
       var discount = $('#discount').val();
         var freight = $('#freight').val();
           if(discount==''){discount=0;}
         if(freight==''){freight=0;}
         net_total = parseFloat(net_total) -  parseFloat(discount) + parseFloat(freight);
     //   net_total = new Number(net_total).toLocaleString("en-US");
        // gst_amount = new Number(gst_amount).toLocaleString("en-US");
       var new_net_total =  Number(net_total).toFixed(2);
       var new_gst_amount = Number(gst_amount).toFixed(2);
          $('#net_total').text(new_net_total);  
   
        }      
        
 $(document).on('blur', '.item_qty', function(){
  
    var item_no = $('#item_id').val();
    var type_id = $('#type_id').val();
    var v_id = $('#v_id').val();
    var doc_type = 'purchase';
    $('#item_detail').text('wait....');
    
    $.ajax({
      url:"item_history.php",
      method:"POST",
      data:{item_no:item_no, v_id:v_id,type_id:type_id, doc_type:doc_type},
      dataType:"json",
      success:function(data)
      {   
         if(item_no > 0)
        {
       // $('#rate'+count).val(data.rate);
        
        $('#item_detail').html(data.item_detail);
        }
        
        
      }
    })
  });
   
        
    $(document).on('blur', '.item_name', function(){
      
          var item_id = 0;
          var btn_action = 'check';
          var type_id = $('#type_id').val();
          var item_name = $('#item_name').val();
          $.ajax({
                  url:"check_item.php",
                  method:"POST",
                  data:{btn_action:btn_action,type_id:type_id,item_name:item_name},
                  dataType:"json",
                  success:function(data)
                  {
                  item_id = data.item_id;
                  if(item_id > 0)
                    {
                      $('#item_id').val(data.item_id); 
                      $('#item_cost').val(data.item_cost);
                      $('#item_rate').val(data.list_rate);      
                    }else{$('#item_no').val(0);$('#item_info').text('New Item Will Be add');}
                  
                  }
                
                })
          
        }); 
       $(document).on('blur', '.item_rate', function(){
         var item_id = $('#item_id').val(); 
         if(item_id ==0)
                    {
                      if(confirm("Are you sure you want to add this item?"))
                      {
                       item_cost = $('#item_rate').val();
                        var btn_action = 'add';
                        var type_id = $('#type_id').val();
                        var item_name = $('#item_name').val();
                        $.ajax({
                          url:"create_item.php",
                          method:"POST",
                          data:{type_id:type_id, item_name:item_name, item_cost:item_cost, btn_action:btn_action},
                          dataType:"json",
                          success:function(data)
                          {
                            $('#item_id').val(data.item_id);
                            $('#item_cost').val(data.item_cost);
                            alert('Item Added');
                           
                           
                          }
                        })
                      }
                      else
                      {
                        return false;
                      }
                    }
                  
          
        }); 
      
        $(document).on('blur', '.item_dis_pr', function(){
          var item_dis_pr = $('#item_dis_pr').val();
          var item_rate = $('#item_rate').val();
          if(item_dis_pr > 0)
          {
            var item_dis_rate = parseFloat(item_rate) * parseFloat(item_dis_pr) / 100;
            $('#item_dis_rate').val(item_rate-item_dis_rate);
          }else{$('#item_dis_rate').val(item_rate);}
        });
         $(document).on('blur', '.description', function(){
        
        });
        
          $(document).on('submit', '#data_form',function(event){
            event.preventDefault();
              cal_final_total(count);
              var net_total = $('#net_total').text();
              if(net_total < 1)
                {alert('Invoice Not Acceptable');
                return false;
                }
            $('#btn_save').val('Wait...');
            $('#btn_save').attr('disabled','disabled');
            $('#btn_action').val('update');
            var _data = $(this).serialize();
            $.ajax({
              url:"purchase_return_action.php",
              method:"POST",
              data:_data,
              success:function(data)
              {
               $('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
             //  window.open("sale_invoice_a4?print=1&id=0","_blank");
              window.location = "purchase_return";
              }
            })
          });
           
           $(document).on('blur', '.rate', function(){
          cal_final_total(count);
        }); 
           $(document).on('blur', '.fst_pr', function(){
            if($(this).val()==''){$(this).val(0);}
          cal_final_total(count);
        }); 
        $(document).on('blur', '.gst_pr', function(){
          if($(this).val()==''){$(this).val(0);}
          cal_final_total(count);
        }); 
        $(document).on('blur', '.ast_pr', function(){
          if($(this).val()==''){$(this).val(0);}
          cal_final_total(count);
        }); 
               $(document).on('blur', '.dis_pr', function(){
          cal_final_total(count);
        }); 
          $(document).on('blur', '.dis_rate', function(){
          cal_final_total(count);
        }); 
        $(document).on('blur', '.discount', function(){
          cal_final_total(count);
        });
        $(document).on('blur', '.freight', function(){
           cal_final_total(count);
        });   
       
        
      });
      </script>
    
      <?php
      }
     } 
      ?>