<?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();
}
$s_id = $_GET["id"];
$ref_id = $s_id;
$sql = $connect->query("select inv_no from sale where s_id = '".$s_id."'");
$inv_no = $sql->fetchColumn();

    ?>
<!DOCTYPE html>
<html>
<head>
  <title>Edit Invoice</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">
 <style>
    .select2-container{ width: 100% !important; }
  </style> 
  
</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["edit"]) && isset($_GET["id"]))
      {
        
           include('include/script.php');
        $s_id = $_GET["id"];
        $statement = $connect->prepare("
          SELECT m.dated,m.inv_no,m.po_no,m.chal_no,m.gd_no,m.bilty_no,m.cust_id,c.name,m.discount,m.discount_dsc,m.freight,m.freight_dsc,m.due_day,m.emp_id, m.title_id,m.gst,m.gst_pr,m.sm_id,m.description,c.sale_target,c.cr_limit,m.pre_balance,m.over_dues from  customers c, sale m where m.cust_id=c.cust_id and m.s_id = :s_id
            LIMIT 1
        ");
        $statement->execute(
          array(
            ':s_id'       =>  $s_id
            )
          );
        $result = $statement->fetchAll();
       
  
 
        foreach($result as $row)
        {
        ?>
        
        <script>
        $(document).ready(function(){
          
           $('#dated').val("<?php echo $row["dated"]; ?>");
          $('#inv_no').val("<?php echo $row["inv_no"]; ?>");
            $('#gd_no').val("<?php echo $row["gd_no"]; ?>");
           $('#po_no').val("<?php echo $row["po_no"]; ?>");
          $('#chal_no').val("<?php echo $row["chal_no"]; ?>");
           
          $('#cust_id').val("<?php echo $row["cust_id"]; ?>");
          $('#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"]; ?>");
          $('#due_day').val("<?php echo $row["due_day"]; ?>");
          $('#emp_id').val("<?php echo $row["emp_id"]; ?>");
         
           $('#gst').val("<?php echo $row["gst"]; ?>");
          $('#gst_pr').text("<?php echo $row["gst_pr"]; ?>");
           $('.gst_pr').val("<?php echo $row["gst_pr"]; ?>");
               $('#sm_id').val("<?php echo $row["sm_id"]; ?>");
                $('#description').val("<?php echo $row["description"]; ?>");
                $('#span_sale_target').text("<?php echo $row["sale_target"]; ?>");
                $('#span_cr_limit').text("<?php echo $row["cr_limit"]; ?>");
                $('#cr_limit').val("<?php echo $row["cr_limit"]; ?>");
                $('#span_cur_balance').text("<?php echo $row["pre_balance"]; ?>");
                $('#cur_balance').val("<?php echo $row["pre_balance"]; ?>");
                $('#span_over_dues').text("<?php echo $row["over_dues"]; ?>");
          $('.select2').select2();
          $('.loaded').val(1);
        });
        </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 GST Invoice (<?php echo $inv_no;?>)</h2></td>
            </tr>
            <br />
            <br />
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-6">
                      <br />
                        <select name="cust_id" id="cust_id" class="form-control input-sm cust_id select2" data-show-subtext="true" data-live-search="true" required />
                        <option value="<?php echo $row["cust_id"]; ?>"><?php echo $row["name"]; ?>
                           <?php echo fill_c_name($connect);?>
                        </select>
                        <br />
                        <br />
                        
                        
                        <input type="text" name="description" id="description" class="form-control" placeholder="Enter Description" />
                        <br />
                         <table>
                        <tr>
                        <td>Sales Target</td><td>Credit Limit</td><td>Cur Balance</td><td>Over Dues</td>
                        </tr>
                        <tr>
                        <td><span id="span_sale_target"></span></td>
                        <td><span id="span_cr_limit"></span></td>
                        <td><span id="span_cur_balance"></span></td>
                        <td><span id="span_over_dues"></span></td>
                        </tr>
                        </table>
                         <span id="cust_limit" style="color:red;font-size:15px"></span>
                       
                    </div>
                    <div class="col-md-6">
                     
                   <table>
                      <tr>
                         <td>
                         <b>Date :</b>
                      <input type="text" name="dated" id="dated" class="form-control input-sm" placeholder="Select Date" value="<?php echo $dated; ?>" readonly AutoComplete="off" required />
                      </td>
                      <td>
                      <b>Inv No. :</b><input type="text" name="inv_no" id="inv_no" class="form-control inv_no">
                       <span id="inv" style="color:red"></span><input type="hidden" name="due_day" id="due_day" class="form-control due_day" value="0"></td><td>
                      <b>PO No. :</b><input type="text" name="po_no" id="po_no" class="form-control po_no">
                      </td>                       
                    
                    
                     
                      </tr>                   
                
                  
                      <tr>
                      
                      
                      
                      
                      <td colspan="2">
                      <b>Delivered By :</b>
                      <select name="emp_id" id="emp_id" class="form-control input-sm emp_id" required />
                           <?php echo fill_emp($connect);?>
                        </select>
                        </td>
                     
                         <td>
                      <b>GD No. :</b><input type="text" name="gd_no" id="gd_no" class="form-control gd_no">
                      </td>
                      </tr>
                      </table>
                    
                      
                    </div>
                  </div>
                  <br />
                 <table width="100%" id="invoice-item-table" class="table table-bordered">
                   <tr>        
       
           
          <td width="22%"><input type="hidden" name="item_no" id="item_no" class="form-control input-sm item_no" ><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>
                  </table>
                        <div align="left">
                    <span id="item_info"></span>
                     <span id="item_detail"></span>
                     
                  </div>        
                   <table id="items-table" class="table table-responsive table-bordered">
                     <thead>
                       <tr>
                        <td width="3%">#</td><td width="27%">Item</td><td width="10%">Qty</td><td width="15%">ListRate</td><td width="10%">Dis%</td><td width="15%">Rate</td><td width="20%">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,
                      
                      d.cost
                      from sale_detail d, items i,item_type t where t.type_id=i.type_id and i.item_id=d.item_no and d.s_id = :s_id
                    ");
                    $statement->execute(
                      array(
                        ':s_id'       =>  $s_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="text" 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="text" name="rate[]" id="rate<?php echo $m; ?>" class="form-control input-sm number_only rate" step="any" value="<?php echo $sub_row["rate"]; ?>"/></td>
                      <td><input type="text" 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="text" name="dis_rate[]" id="rate<?php echo $m; ?>" class="form-control input-sm number_only dis_rate" step="any" value="<?php echo $dis_pr_rate; ?>" readOnly/></td>
                      <td><input type="text" 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>
                  <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"><b>GST :<span id="gst_pr"></span> (%) :</td>
                <td width="80%" align="right"><b><input type="text" name="gst_amount" id="gst_amount" class="form-control gst_amount"  value="<?php echo $gross_amount;?>" style="text-align:right" readonly></b></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="80%" align="right"><input type="text" name="discount" id="discount" class="form-control discount" placeholder="Less Amount" style="text-align:right"></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="80%" align="right"><input type="text" name="freight" id="freight" class="form-control freight" placeholder="Add Amount" style="text-align:right"></td>
              </tr>
             
              <tr>
                <td width="80%"align="right"><b>Net Amount :</td>
                <td width="80%" 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="sale_target" id="sale_target"class="form-control"  />
                        <input type="hidden" name="cr_limit" id="cr_limit" class="form-control"  />
                        <input type="hidden" name="cur_balance" id="cur_balance" class="form-control"   />
                        <input type="hidden" name="over_dues" id="over_dues" class="form-control" />
                        <input type="hidden" name="gst_pr" class="gst_pr" />
                        <input type="hidden" name="loaded" id="loaded" class="loaded" value="1" />
                        <input type="hidden" name="cust_name" id="cust_name" /> 
                        <input type="hidden" name="btn_action" id="btn_action" />
                        <input type="hidden" name="s_id" id="s_id" value="<?php echo $s_id;?>" /> 
                  <table>
                    <tr>   
                  
                  <td><input type="submit" name="update" id="btn_save" class="form-control btn btn-primary" value="Update" /></td>
                 
               
                </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>
  

   $('.select2').select2();
  </script>
 <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
    });
  });
 
   
  </script>
     
    <script>
      $(document).ready(function(){
        
      
       var total_amount = $('#total_amount').text();
       var count = "<?php echo $m;?>";   
       $('#total_item').val(count);    
      var loaded = $('#loaded').val();
      if(loaded==1){cal_final_total(count);$('#loaded').val(2);}
        

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

          var item_no = $('#item_no').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+'" readOnly></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+'" readOnly></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;
         
          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_amount = parseFloat(dis_rate) * parseFloat(qty);
                  dis_pr_rate = parseFloat(rate) - parseFloat(dis_rate);
                  amount = amount - dis_amount;
                  $('#dis_rate'+j).val(dis_pr_rate); 
                  $('#amount'+j).val(amount); 
                }else{$('#dis_rate'+j).val(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);
          
          
          var net_total = 0;
          var gst_amount= 0;
         gst_pr = $('.gst_pr').val();
          net_total = parseFloat(final_item_amount) ;
          if(gst_pr > 0)
          {
            gst_amount = parseFloat(final_item_amount) * parseFloat(gst_pr)/100;
          }
         
          $('#gst_amount').val(gst_amount);
          net_total = parseFloat(final_item_amount) + parseFloat(gst_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);  
        
            $('#gst_amount').val(new_gst_amount);            
          var cr_limit = $('#cr_limit').val();
            var cur_balance = $('#cur_balance').val();
            cur_balance = parseFloat(cur_balance) + parseFloat(net_total);
            var over_dues = 0;
            over_dues = parseFloat(cur_balance) - parseFloat(cr_limit);
            $('#over_dues').val(over_dues);
          //  $('#span_over_dues').text(over_dues);
            var cr_status = 'Credit Limit Exceeded';
            var cr_status_clear = '';
            if(cur_balance > cr_limit)
            {
              //$('#cust_limit').text(cr_status);
              document.getElementById("cust_limit").innerHTML = "<img style='width:20px;height:20px;padding:1px' src='upload/warning.gif' class='mark'>"+ ' Credit Limit Exceeded';
            }else{$('#cust_limit').text(cr_status_clear);}
        }        
        
 $(document).on('blur', '.item_qty', function(){
  
    var item_no = $('#item_no').val();
    var type_id = $('#type_id').val();
    var cust_id = $('#cust_id').val();
   
    
    $.ajax({
      url:"item_history.php",
      method:"POST",
      data:{item_no:item_no, cust_id:cust_id,type_id:type_id},
      dataType:"json",
      success:function(data)
      {   
         if(item_no > 0)
        {
       // $('#rate'+count).val(data.rate);
        
        $('#item_detail').html(data.item_detail);
        }
        
        
      }
    })
  });
   
  $(document).on('blur', '.description', function(){
          var cust_id = $('#cust_id').val();            
          $.ajax({
                url:"cust_data.php",
                method:"POST",
                data:{cust_id:cust_id},
                dataType:"json",
                success:function(data){
                  $('#sm_id').val(data.sm_id);
                     $('#cr_limit').val(data.cr_limit);
                      $('#sale_target').val(data.sale_target);
                      $('#cur_balance').val(data.balance);
                      $('#pre_balance').val(data.balance);
                       $('#span_cr_limit').text(data.cr_limit);
                      $('#span_sale_target').text(data.sale_target);
                      $('#span_cur_balance').text(data.balance);
                      $('#span_over_dues').text(data.over_amount);
                     // $('#gst').val(data.gst); 
                      $('#due_day').val(data.due_day);              
                     
                }
           });
          
        });

 
        
    $(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_no').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_no').val(); 
         if(item_id ==0)
                    {
                      if(confirm("Are you sure you want to add this item?"))
                      {
                        var cost = prompt("Please enter Item Cost", "");
                        if(cost !=null)
                        {
                          var item_cost = cost;
                        }
                        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_no').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('blur', '.contact', function(){
          $('#cust_name').focus();
        });
          $(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:"gsales_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 = "gsales";
              }
            })
          });
           $(document).on('blur', '.item_rate',function(){
        var cost  = $('#item_cost').val();
        var rate  = $('#item_rate').val();
      
          
        
           

                    if(parseFloat(rate) < parseFloat(cost) ){
                      $('#item_rate').val(null);
                      $('#item_rate').focus();
                      
                    }else{return true;}

          
          cal_final_total(count);
          
        });  
           $(document).on('blur', '.rate', function(){
          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
      }
      }
      ?>