<?php
session_start();

if($_SESSION['name']==''){
    header("Location:index.php");die();
}

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="bower_components/Ionicons/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="bower_components/morris.js/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="bower_components/jvectormap/jquery-jvectormap.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="bower_components/bootstrap-daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="bower_components/datatables/css/dataTables.bootstrap.min.css">
  <link rel="stylesheet" href="bower_components/datatables/css/dataTables.bootstrap.css">
  
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="bower_components/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->

<!-- Bootstrap 3.3.7 -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Morris.js charts -->

<!-- Sparkline -->
 <script src="vendor/datatables/jquery.dataTables.js"></script>
    <script src="vendor/datatables/dataTables.bootstrap4.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<!--<script src="dist/js/pages/dashboard.js"></script>-->
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php include("header.php");?>
  <!-- Left side column. contains the logo and sidebar -->
  <aside class="main-sidebar">
    <!-- sidebar: style can be found in sidebar.less -->
    <?php include("sidebar.php");?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
  

    <!-- Main content -->
    <section class="content">
      
        <div class="row">
            <div class="col-md-12">
          <!-- Custom Tabs -->
          <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
<!--                 <li class=""><a href="#tab_11" data-toggle="tab">Invoice</a></li>-->
<!--              <li><a href="#tab_21" data-toggle="tab">View Hotel</a></li>-->
              <li class="active rm"><a href="#tab_11" data-toggle="tab">Purchase Order</a></li>
            
             <li><a href="#tab_21" data-toggle="tab">Purchase Order History</a></li>
             
             
            </ul>
            <div class="tab-content">
                <div class="tab-pane active " id="tab_11">
                    <form id='form11' class='align'>
                    Customer Name:<span class='required'></span><br>    
                 <input type='text' id='name' style="width:350px"><br>
                  Address1:<span class='required'></span><br>
                 <input type='text' id='adr1'  style="width:350px"><br>
                  Address2:<span class='required'></span><br>
                  <input type='text' id='adr2'  style="width:350px"><br>
                   Address3:<span class='required'></span><br>
                  <input type='text' id='adr3'  style="width:350px"><br>
                   Mobile No:<br>
                  <input type='text' id='msisdn'><br>
                    GST No:<br>
                 <input type='text' id='gst_no'><br>
                  Email ID:<br>
                 <input type='text' id='email_id'><br>
                  </form>
               <form id='form11' class='align' style='float:right;margin-right: 100px;'>
                    Ref No: 
                 <span id='quotation_no'></span><br>
                
                  Date:<br>
                  <input type='text' class='datepicker' id='from_date'><br>
                   Enquiry Ref No:<span class='required'></span><br>
                  <input type='text' class='' id='enquiry_ref_no'><br>
                  <div style='display:none'>   
                  Kind Atn:<span class='required'></span><br>
                  <input type='text' class='' id='kind_atn'><br>
                  </div>
                  </form>
                  
                    <br><br>
                       <table class='table' id='table1'><thead>
        <tr><th>Sl no</th>
<th>Product</th>
<!--<th>Size</th>
<th>HSN</th>-->
<th>Qty</th>
<th>UOM</th>
<th>Unit Price</th>
<th>GST</th>
<th>Grand Total</th>
<th style='display:none'>FDA</th>
<th>Action</th>
</tr>
</thead><tbody></tbody></table>  
                    
                    
                     
                    <form id='add_remove_form'>  
                  <table>
                        <tr>
                              <td>
                 Product:
                 <br>
                 <input type="text" id='product_name' style='width:280px'>
                </td>
<!--                <td>
                 Size
                 <br>
                 <input type="text" style='width:120px'  id='specification'>
                            </td>
                            <td>
                 HSN/SAC:
                 <br>
                 <input type="text" style='width:80px'  id='hsn'>
                            </td>-->
                  <td>
                 Qty:
                 <br>
                 <input type="text" style='width:50px' id='qty'>
                </td>
                 
                  <td>
                 UOM:
                 <br>
                 <input type="text" style='width:100px' id='unit'>
               </td>
                <td>
                 Rate/unit(Rs):
                 <br>
                 <input type="text" style='width:120px' id='base_price'>
                 </td>
                 <td>
                 GST:
                 <br>
                 <select style='width:50px' id='gst'><option value='0'>0%</option>
                 <option value='5'>5%</option>
                 <option value='12' >12%</option>
                 <option value='18' selected>18%</option>
                 <option value='28'>28%</option></select>
                 </div>
                   <td>
                 Grand Total:
                 <br>
                 <input type="text" style='width:120px' id='grand_total' disabled="">
                 </td>
                 <td style='display:none'>
                 FDA:
                 <br> <input type="checkbox" id='fda'>
                
                 </td>
                <td>
                
                 <br>
                 <button id='add_remove'>Add</button>
                </td>
                        </tr>
                    </table>
                       <br>
                       <div style='display:none'>
                      Delivery :<span class='required'></span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<input value="7" style="width:100px" type="text" id="delivery"> Days
                      <br> <br>
                      Payment Terms:<span class='required'></span>&nbsp;&nbsp;&nbsp;<input  style="width:400px"  type="text" id="payment_terms">
                        <br>
                        <input type="checkbox" id="warranty"> Warranty
                         <br>
                        <input type="checkbox" id="test_report"> Test Report
                       </div>
                        
                       <input type='checkbox' id='to_pay_basis'>Shipment Charges: to pay basis
                       <br> <br>
                       Shipment Mode:   <br><input type='text' id='shipment_mode'>
                    </form>
                <br>
                 <br> 
                 <button class='btn btn-primary' id='add_invoice'>Submit</button>
                 <br>
                   <table class='' id=''><thead>
                             <tr>
                                  <th colspan='7'></th>
                                 <th>Total Price &nbsp;</th><th>: &nbsp;</th><th class='t1_total'></th></tr>
        <tr>
                                  <th colspan='7'></th>
                                  <th>Discount(%)</th><th><input style='width:50px' type='text' value='0' id='discount'>&nbsp;</th><th id='discount_val'></th></tr>
                               <tr>
                                  <th colspan='7'></th>
                                 <th>Net Price</th><th>: &nbsp;</th><th class='net_total'></th></tr>
                                <tr>
                             <tr> <th colspan='7' style='width:7in'></th>
            <th>GST &nbsp;</th><th>: &nbsp;</th><th class='t1_gst'></th></tr>
        <tr> <th colspan='7'></th> 
<th>Grand Total &nbsp;</th><th>: &nbsp;</th><th class='t1_grand_total'></th>

</tr>
</thead><tbody></tbody></table> 
              </div>
               <div class="tab-pane" id="tab_21">
                 
                   From:<input type='text' class='datepicker' id='from_date1'>
                    to:<input type='text' class='datepicker' id='to_date1'>
<!--                    <select id='status'><option value='pending'>Pending</option>
                    <option value='completed'>Completed</option>
                    </select>-->
                  <button class='btn btn-primary' id="get_invoice">Refresh</button>
                  <br><br>
                   <table class='table' id='table11' style='width:100%'>
                      <thead>
                          <tr class="q" style='background:green;color:white'><th>Sl</th>
                              <th class="q">Created On</th>
                              <th class="q">Customer Name</th>
<!--                          <th>Address 1</th>
                          <th>Address 2</th>
                          <th>GST No</th>-->
                          <th class='w1'>Purchase No</th>
<!--                            <th>View Products</th>-->
                           <th>Email Status</th>
                          
                           
                          <th class='w' style="width:300px">Action</th></tr>
                      </thead>
                      
                  </table>
              </div>
                
            
            
              <!-- /.tab-pane -->
              <div class="tab-pane" id="tab_2">
                 
              </div>
              <!-- /.tab-pane -->
              <div class="tab-pane" id="tab_3">
               
              </div>
              <!-- /.tab-pane -->
            </div>
            <!-- /.tab-content -->
          </div>
          <!-- nav-tabs-custom -->
        </div>
            
            
        </div>
        
        
        
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
 
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- jQuery 3 -->

</body>
</html>
<script>
$("body").on("click",".delete_quotation",function(){
    var id=$(this).attr("value");
    if(confirm("Are You Sure?")){
     $.ajax({
   url:"quotation_delete_all.php" ,
   data:{id:id},
   type:"POST",
   dataType:"JSON",
  
   success:function(data1){
  
   notify(data1+ " Row Deleted Successfully");
  
   from_date=$("#from_date1").val();     
    to_date=$("#to_date1").val();     
   status="";
get_cusomers(from_date,to_date,status);
    },
   error:function(xhr){
       notify1(xhr.responseText);
   }
});
    }
   })
 $("body").on("input","#edit_discount",function(){
       var discount=$(this).val();
       if(isNaN(discount)){
           $(this).val("0");
       }
        var discount=$(this).val();
       var t1_total=$(".edit_t1_total").html();
       if(t1_total==""){
           return;
       }
       
       
       var value=parseFloat(t1_total)-parseFloat(t1_total*discount/100);
       $(".edit_net_total").html(value);
       var net_total=value;
         $("#edit_discount_val").html(t1_total*discount/100);
         var discount_val=t1_total*discount/100;
       // var t1_gst=$(".t1_gst").html();
         var t1_gst=parseFloat(edit_grand_t1_gst);
        //console.log(value+"--"+shipment_charges+"--"+t1_gst);
         var t=parseFloat(value)+parseFloat(t1_gst);
        $(".edit_t1_gst").html(t1_gst);
        $("#edit_shipment_charges").val(0);
         $("#edit_shipment_charges_val").html(0);
         edit_calculate_discount(discount,discount_val,0);
   });
     $("body").on("input","#discount",function(){
       var discount=$(this).val();
       if(isNaN(discount)){
           $(this).val("0");
       }
        var discount=$(this).val();
       var t1_total=$(".t1_total").html();
       if(t1_total==""){
           return;
       }
       
        
       var value=parseFloat(t1_total)-parseFloat(t1_total*discount/100);
       $(".net_total").html(value.toFixed(2));
       var net_total=value;
         $("#discount_val").html(t1_total*discount/100);
         var discount_val=t1_total*discount/100;
       // var t1_gst=$(".t1_gst").html();
         var t1_gst=(grand_t1_gst);
       //alert(grand_t1_gst);
         var t=parseFloat(value)+parseFloat(t1_gst);
        $(".t1_gst").html(t1_gst.toFixed(2));
        $("#shipment_charges").val(0);
         $("#shipment_charges_val").html(0);
         calculate_discount(discount,discount_val,0);
   });
</script>
<style>
    .required:before{
   color: red;
   content: "*";
   position: absolute;
font-weight:bold !important;font-size: 18px;
}
    
    .w{width:250px !important}
     .w1{width:200px !important}
</style>
<script src="jquery-ui.js"></script>
<link href='jquery-ui.css' rel='stylesheet'>
<script src='notify.min.js'></script>
<script src='notify.js'></script>
<link href='chosen.css' rel='stylesheet'>
<script>
    $("#from_date1").datepicker({ dateFormat: "yy-mm-dd"});
    $("#to_date1").datepicker({ dateFormat: "yy-mm-dd"});
    
    function notify(data){
$.notify(data, "success");
    }
     function notify1(data){
$.notify(data, "error");
    }
    
    $("#add_invoice_dialog").click(function(){
        $("#tab_11").dialog({"title":"Add Invoice"});
        name_auto();
    });
    
    
    
    var $input = $("#name").autocomplete({
    /* select: function (event, ui) {
     var value = ui.item.name
     //ac_name=$("#acc_name_val").val();   
     member_id=ui.item.value;
     }, */
    select: function (event, ui) {
    $("#name").val(ui.item.label); //ui.item is your object from the array
 $("#adr1").val(ui.item.adr1);
 $("#adr2").val(ui.item.adr2);
    $("#gst_no").val(ui.item.gst);
     $("#email_id").val(ui.item.email_id);
      $("#msisdn").val(ui.item.msisdn);
       $("#adr3").val(ui.item.adr3);
     
  
    return false;
    }, selectFirst: true,
            change: function (event, ui) {
            if (ui.item.label == "No results found") {
            //here is null if entered value is not match in suggestion list
            $(this).val((ui.item ? ui.item.id : ""));
            }
            },
            response: function (event, ui) {
            if (!ui.content.length) {
            var noResult = {value: "", label: "No results found"};
            ui.content.push(noResult);
            }
            },
            // source: "car_model.php",
//            source: [
//                       
//                               
//                               
//                                {
//                                  label: "c",
//                            value: "c",
//                                },
//                         
//                            ],
            source: function( request, response ) {
        $.ajax({
          url: "get_name_quotation.php",
          type:"POST",
          dataType: "json",
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
            minLength: 0,
    });
    $("<button class='align' type='button'>&nbsp;</button>")
            .attr("tabIndex", - 1)
            .attr("title", "Show All Items")
            .insertAfter($input)
            .button({
            icons: {
            primary: "ui-icon-triangle-1-s"
            },
                    text: false
            })
            .removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function () {
            // close if already visible                        
            if ($input.autocomplete("widget").is(":visible")) {
            $input.autocomplete("close");
            return;
            }
            $(this).blur();
            $input.autocomplete("search", "");
            $input.focus();
            });
     $("span.ui-dialog-title").text('My New Title');

</script>
<script>

    var $input_city = $("#product_name").autocomplete({
    /* select: function (event, ui) {
     var value = ui.item.name
     //ac_name=$("#acc_name_val").val();   
     member_id=ui.item.value;
     }, */
    select: function (event, ui) {
    $("#product_name").val(ui.item.label); //ui.item is your object from the array
  
   
  
    return false;
    }, selectFirst: true,
            change: function (event, ui) {
            if (ui.item.label == "No results found") {
            //here is null if entered value is not match in suggestion list
            $(this).val((ui.item ? ui.item.id : ""));
            }
            },
            response: function (event, ui) {
            if (!ui.content.length) {
            var noResult = {value: "", label: "No results found"};
            ui.content.push(noResult);
            }
            },
            // source: "car_model.php",
//            source: [
//                       
//                               
//                               
//                                {
//                                  label: "c",
//                            value: "c",
//                                },
//                         
//                            ],
            source: function( request, response ) {
        $.ajax({
          url: "get_product_name.php",
          type:"POST",
          dataType: "json",
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
            minLength: 0,
    });
    $("<button class='align' type='button'>&nbsp;</button>")
            .attr("tabIndex", - 1)
            .attr("title", "Show All Items")
            .insertAfter($input_city)
            .button({
            icons: {
            primary: "ui-icon-triangle-1-s"
            },
                    text: false
            })
            .removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function () {
            // close if already visible                        
            if ($input_city.autocomplete("widget").is(":visible")) {
            $input_city.autocomplete("close");
            return;
            }
            $(this).blur();
            $input_city.autocomplete("search", "");
            $input_city.focus();
            });
     $("span.ui-dialog-title").text('My New Title');

</script>



<div id='balance_div' style='display: none'></div>
<style>
    .ui-dialog > .ui-widget-header {background: #cc6f6f;}
    #table11_div{background:#da9f9f}
    .align{display:inline-block}
    #table1 thead tr{background: green;

color: white;}
    .highlight{
                         color:red;
                     }
</style>


<script>
     $("#from_date").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", "0");
//    $("#to_date").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", "0");
    $.ajax({
        url:"get_quotation_no.php",
    data:{ type:"purchase"},
      type:"POST",
     
      success:function(data1){
        quotation_no=data1;
              $("#quotation_no").html(quotation_no);  
      } 
    })
    var k1=1;
$("#add_remove").click(function(e){
    e.preventDefault();
    var product=$("#product_name").val();
    var hsn=$("#hsn").val();
    var qty=$("#qty").val();
    var unit=$("#unit").val();
    var base_price=$("#base_price").val();
    var gst=$("#gst").val();
    var total=$("#grand_total").val();
    var specification=$("#specification").val();
     var fda=$("#fda").val();
     flag=0;
     if(product==""||hsn==""||qty==""||unit==""||base_price==""||gst==""||specification==""){
         notify1("All fields are mandatory");
        
            return false;
     }
     var i1=0;
     $("#table1 tr").each(function(){
     if(i1>=1){
        var product1= $(this).find("td:nth-child(2)").find("input").val();
        if(product==product1){
             //flag=1;
           
        }
    }
    i1++;
    });
    if(flag==1){
        notify1("product Exists");return false; 
    }
     
           
        
     if($("#fda").is(":checked")){
          fd= "<td><input type='checkbox' class='fda1' checked>FDA</td>"  
     }else{
         fd=  "<td><input type='checkbox' class='fda1'>FDA</td>"  
     }
     
     fd="";
    var html="";
    html=html+"<tr>"
       +"<td>"+ k1++ +"</td>"
    +"<td><input class='product1' style='width:250px' type='text' value='"+product+"'></td>"
    // +"<td><input class='specification1' style='width:100px' type='text' value='"+specification+"'></td>"
     //+"<td><input class='hsn1' type='text' style='width:80px'  value='"+hsn+"'></td>"
      +"<td><input class='qty1' type='text' style='width:50px' value='"+qty+"'></td>"
       +"<td><input class='unit1' type='text' style='width:60px'  value='"+unit+"'></td>"
        +"<td><input class='base_price1' type='text'  style='width:100px' value='"+base_price+"'></td>"
 +"<td><select   style='width:50px' class='gst1'>";
 if(gst==0){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }
 if(gst==5){
    html=html+"<option value='0' >0%</option>"
  +"<option value='5' selected>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }
 if(gst==12){
    html=html+"<option value='0' >0%</option>"
  +"<option value='5' >5%</option>"
   +"<option value='12' selected>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==18){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18' selected>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==28){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28' selected>28%</option>";
 }



 html=html+"</select></td>"
  +"<td><input type='text' disabled  class='total1' style='width:100px'  value='"+total+"'></td>"
 +fd
  +"<td><button class='btn delete'>Delete</button></td>"  
            +"</tr>";
    
    $("#table1").append(html);
    
    var k=1;
   $("#table1 tr").each(function(i){
       var c=1
       $("td",this).each(function(){
           if(c==1){
               $(this).html(k++);
           }
           c++;
       });
     
   });
  

    calculate();
    $("#add_remove_form").trigger("reset");
       });
</script>

<script>
$("body").on("click",".delete",function(){
    var id=$(this).val();
  var this_var=  $(this);
  this_var.parent().parent().remove();
  var k=1;
   $("#table1 tr").each(function(i){
       var c=1
       $("td",this).each(function(){
           if(c==1){
               $(this).html(k++);
           }
           c++;
       });
       
   });
   
   
        calculate();
  
  
//    $.ajax({
//      url:"delete_product_quotation.php",
//      data:{id:id},
//      type:"POST",
//      dataType:"JSON",
//      success:function(data1){
//          notify(data1+ " Row Deleted Successfully" );
//          this_var.parent().parent().remove();
//      },error:function(xhr){
//          notify1(xhr.responseText);
//  }
//});
});

$("body").on("input","#qty",function(){
    var value=$(this).val();
   var unit_price= $(this).parent().parent().find("td:nth-child(4)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(6)").find("input");
    if(!isNaN(value) && !isNaN(unit_price) ){
      this_var.val(value*unit_price);
    }
});


$("body").on("input","#base_price",function(){
    var unit_price=$(this).val();
 var qty= $(this).parent().parent().find("td:nth-child(2)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(6)").find("input");
    if(!isNaN(qty) && !isNaN(unit_price) ){
      this_var.val(qty*unit_price);
    }
});


$("body").on("input",".qty1",function(){
    var value=$(this).val();
   var unit_price= $(this).parent().parent().find("td:nth-child(5)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(7)").find("input");
    if(!isNaN(value) && !isNaN(unit_price) ){
      this_var.val(value*unit_price);
    }
    
     calculate();
    
    
    
});


$("body").on("input",".base_price1",function(){
    var unit_price=$(this).val();
 var qty= $(this).parent().parent().find("td:nth-child(3)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(7)").find("input");
    if(!isNaN(qty) && !isNaN(unit_price) ){
      this_var.val(qty*unit_price);
    }
    
       calculate();
    
});



$("body").on("change",".gst1",function(){
  
    
     
   calculate();
    
})


function calculate(){
    var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
             var i=0;
     $("#table1 tr").each(function(){
     if(i>=1){
        var product= $(this).find("td:nth-child(2)").find("input").val();
         var hsn= $(this).find("td:nth-child(4)").find("input").val();
          var qty= $(this).find("td:nth-child(3)").find("input").val();
           var unit= $(this).find("td:nth-child(4)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
       t1_gst=parseFloat(t1_gst)+parseFloat(gst/100)*(grand_total-(grand_total*discount/100));
         t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
       
    
        }
    i++;
});

var t2=parseFloat(t1_gst)+parseFloat(t1_grand_total);
$(".t1_total").html(t1_base_price);
      $(".t1_gst").html(t1_gst);
       $(".t1_grand_total").html(t2);
       grand_t1_gst=t1_gst;
       
       $("#discount").val(0);
       $("#shipment_charges").val(0);
        $("#discount_val").html(0);
         $(".net_total").html(0);
          $("#discount_val").html(0);
           $("#shipment_charges_val").html(0);
       
       
       
}
</script>

<script>
$("#add_invoice").click(function(){
    var customer_name=$("#name").val();
    var adr1=$("#adr1").val();
    var adr2=$("#adr2").val();
     var gst_no=$("#gst_no").val();
     var from_date=$("#from_date").val();
      var kind_atn=$("#kind_atn").val();
       var enquiry_ref_no=$("#enquiry_ref_no").val();
       var email_id=$("#email_id").val();
       var msisdn=$("#msisdn").val();
        var adr3=$("#adr3").val();
         var delivery=$("#delivery").val();
          var discount= $("#discount").val();
    var discount_val= $("#discount_val").html();
    var shipment_mode=$("#shipment_mode").val();
      var net_total= $(".net_total").html();
      net_total=parseFloat(net_total).toFixed(2);
      discount=parseFloat(discount).toFixed(2);
      discount_val=parseFloat(discount_val).toFixed(2);
          var payment_terms=$("#payment_terms").val();
          if($("#warranty").is(":checked")){
              warranty="yes";
          }else{
              warranty="no";
          }
          if($("#test_report").is(":checked")){
              test_report="yes";
          }else{
              test_report="no";
          }
            if($("#to_pay_basis").is(":checked")){
              to_pay_basis="yes";
          }else{
              to_pay_basis="no";
          }
     if(customer_name==""||adr1==""||adr2==""||from_date==""||enquiry_ref_no==""){
         notify1("All * fields are mandatory");return false;
     }
     var discount1= $("#discount").val();
     if(discount1==""){
         alert("Discount cannot be empty");return false;
     }
     
      if(isNaN(delivery)){
             notify1("Delivery days should be numeric");return false;
         }
          
    var quotation=[];
    var i=0;
     var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
    $("#table1 tr").each(function(){
     if(i>=1){
        var product= $(this).find("td:nth-child(2)").find("input").val();
           var specification= $(this).find("td:nth-child(3)").find("input").val();
         var hsn= $(this).find("td:nth-child(4)").find("input").val();
          var qty= $(this).find("td:nth-child(3)").find("input").val();
           var unit= $(this).find("td:nth-child(4)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        var fda= $(this).find("td:nth-child(8)").find("input").val();
         if($(this).find("td:nth-child(8)").find("input").is(":checked")){
              fda="yes";
          }else{
              fda="no";
          }
         t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
       // t1_gst=parseFloat(t1_gst)+parseFloat(gst/100)*grand_total;
         t1_gst=parseFloat(t1_gst)+parseFloat(gst/100*(grand_total-(grand_total*discount/100)));
       
         t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
         
         
         
         
         
         quotation.push({"product":product,"hsn":hsn,"qty":qty,"unit":unit,"base_price":base_price,"gst":gst,"grand_total":grand_total,"specification":specification,"fda":fda});
     }
     
     
     
     
     i++;
    });
    
    var t2_grand=parseFloat(t1_gst)+parseFloat(t1_grand_total);

  if(quotation.length==0){
        notify1("All fields are mandatory");return false;
  }
    
    
    
    
     quotation=JSON.stringify(quotation);
    var shipment_charges=0;
    var shipment_charges_val=0;
        $("#add_invoice").attr("disabled", true);
      $.ajax({
      url:"add_quotation.php",
      data:{discount:discount,shipment_charges:shipment_charges,net_total:net_total,discount_val:discount_val,
        shipment_charges_val:shipment_charges_val,shipment_mode:shipment_mode,
          to_pay_basis:to_pay_basis,type:"purchase",test_report:test_report,warranty:warranty,delivery:delivery,payment_terms:payment_terms,msisdn:msisdn,adr3:adr3,email_id:email_id,enquiry_ref_no:enquiry_ref_no,kind_atn:kind_atn,quotation:quotation,customer_name:customer_name,adr1:adr1,adr2:adr2,gst_no:gst_no,from_date:from_date},
      type:"POST",
      dataType:"JSON",
      success:function(data1){
          notify(data1.data+ " Row Inserted Successfully");
        $("#table1 tbody").remove();
        $("#tab_11 input").each(function(){
            $(this).val("");
               $(this).html("");
               
        })
          get_pdf(data1.quotation_no,t1_base_price,t1_gst,t2_grand);
        //get_pdf(97);
         $("#add_invoice").attr("disabled", false);
      },
          error:function(xhr){
              notify1(xhr.responseText);
               $("#add_invoice").attr("disabled", false);
          }
   });
})
function  get_pdf(quotation,t1_base_price,t1_gst,t2_grand){
   
    $.ajax({
      url:"get_data_for_pdf.php",
      data:{quotation_no:quotation},
      type:"POST",
      dataType:"JSON",
      success:function(data1){
         var customer_data=data1.data;
         var product_data=data1.production;
        var pdf_data=pdf_data1;
        var date=customer_data[0].created_on;
        var y=date.split("-")[0];
        var m=date.split("-")[1];
        var d=date.split("-")[2];
        var dt=d+"-"+m+"-"+y;
         pdf_data=pdf_data.replace("#customer_name",customer_data[0].customer_name);
         pdf_data=pdf_data.replace("#adr1",customer_data[0].adr1);
         pdf_data=pdf_data.replace("#adr2",customer_data[0].adr2);
          pdf_data=pdf_data.replace("#your",customer_data[0].gst_no);
            pdf_data=pdf_data.replace("#shipment_mode",customer_data[0].shipment_mode);
         pdf_data=pdf_data.replace("#gst_no","29AASFA7701N1ZT");
         pdf_data=pdf_data.replace("#quotation_no",customer_data[0].quotation_no);
          pdf_data=pdf_data.replace("#date",dt);
           pdf_data=pdf_data.replace("#enquiry_ref_no",customer_data[0].enquiry_ref_no);
            pdf_data=pdf_data.replace("#kind_attn",customer_data[0].kind_atn);
             pdf_data=pdf_data.replace("#grand_total",addZeroes(t1_base_price));
              pdf_data=pdf_data.replace("#grand_gst",addZeroes(t1_gst));
               var t=parseFloat(t2_grand)-parseFloat(customer_data[0].discount_val);
              pdf_data=pdf_data.replace("#discount",customer_data[0].discount);
                    // pdf_data=pdf_data.replace("#shipment_charges",customer_data[0].shipment_charges);
                      pdf_data=pdf_data.replace("#net_total",customer_data[0].net_total);
                       pdf_data=pdf_data.replace("#discount_val",customer_data[0].discount_val);
                  if(customer_data[0].to_pay_basis=="yes"){
                       pdf_data=pdf_data.replace("#to_pay_basis","Shipment Charges:to pay basis");
                  }else{
                       pdf_data=pdf_data.replace("#to_pay_basis","");
                  }
               pdf_data=pdf_data.replace("#grand_total1",addZeroes(t));
                pdf_data=pdf_data.replace("#adr3",customer_data[0].adr3);
                 pdf_data=pdf_data.replace("#email",customer_data[0].email_id);
                  pdf_data=pdf_data.replace("#msisdn",customer_data[0].msisdn);
                  pdf_data=pdf_data.replace("#delivery",customer_data[0].delivery);
                  pdf_data=pdf_data.replace("#terms",customer_data[0].payment_terms);
                  
                  if(customer_data[0].test_report!="yes"){
                       pdf_data=pdf_data.replace("*TEST REPORTS: All testing will be carried out in-house and Test Certificates will be sent along with invoice.","");
                  }
                  if(customer_data[0].warranty!="yes"){
                       pdf_data=pdf_data.replace("*WARRANTY: All products of Armol Polymers are warranted against failure due to defective materials or poor workmanship. If the buyer wants to claim warranty against these, the buyer needs to inform Armol Polymers within 30 days of product delivery. After the stipulated time of 30 days, no warranty claim is accepted. Warranty claim will not be accepted for any reasons except reasons mentioned above.","");
                  }
                pdf_data=pdf_data.replace("#amount_in_words",number2words(Math.round(t))+' Only');
               
            
            var html="";var k=1;
        for(var i=0;i<product_data.length;i++){
            
            if(product_data[i].fda=="yes"){
                pd="<td style='text-align:left'>"+product_data[i].product_name+"<br></br><span>Conforming to FDA 21. CFR. 177. 2600</span></td>"
            }else{
                  pd="<td style='text-align:left'>"+product_data[i].product_name+"</td>"
            }
            html=html+"<tr class='pagebreak1' style='text-align:center;padding:3px'><td>"+ k++ +"</td>"
            +pd
    // +"<td ><div>"+product_data[i].specification+"</div></td>"
    //+"<td>"+product_data[i].hsn+"</td>"
    +"<td>"+product_data[i].qty+"</td>"
    +"<td>"+product_data[i].unit+"</td>"
      +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].gst))+"%</td>"
    +"<td  style='text-align:right'>"+addZeroes(parseFloat(product_data[i].base_price))+"</td>"
   
    +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].grand_total))+"</td>"
   
    
            +"</tr>"
        }
          pdf_data=pdf_data.replace("<tr><td>#sl_no</td><td>#description</td><td>#qty</td><td>#unit</td><td>#gst</td><td>#base_price</td><td>#total</td></tr>",html);
   // pdf_data=pdf_data+"<style type='text/css'> .pagebreak1{page-break-after: always !important;page-break-before: always !important;page-break-inside:     avoid !important;}</style>";   
 //$("#pdf_div").html(pdf_data);
    $.ajax({
      url:"get_pdf.php",
      data:{pdf_data:pdf_data},
      type:"POST",
     
      success:function(data1){
         
          $("#pdf_link").attr("href","downloads/"+data1);
           $("#pdf_link")[0].click();
         
          
      }
  });   
         
      },
          error:function(xhr){
              notify1(xhr.responseText);
          }
   });
    
    
}

 $.ajax({
      url:"po_pdf.php",
      success:function(data1){
          pdf_data1=data1;
      }
 });

function addZeroes(num) {
   return num.toLocaleString("en", {useGrouping: false, minimumFractionDigits: 2})
}


</script>
<div id='pdf_div' style='display:none'></div>
<div id='pdf_link_div'></div>
<a  target='_new' id='pdf_link'>Click</a>
<script>
tab= $("#table11").DataTable();
     from_date=$("#from_date1").val();     
    to_date=$("#to_date1").val();   
    var search_value="";
    $('#table11').on('search.dt', function() {
     search_value = $('.dataTables_filter input').val();
    
});

   status="";
get_cusomers("","","");
    $("#get_invoice").click(function(){
    from_date=$("#from_date1").val();     
    to_date=$("#to_date1").val();     
   status="";
get_cusomers(from_date,to_date,status);
  });
 
   function get_cusomers(from_date,to_date,status) {
      tab.destroy();
   
           tab= $("#table11").DataTable({
                  "processing": true,
        "serverSide": true,
        "bPaginate":true, // Pagination True
      "sPaginationType":"full_numbers", // And its type.
       "iDisplayLength": 10,
       //Initial no order.
         "search": {
    "search": search_value
  },
        "ajax": {
           //  "url": "get_completed_tests_for_print_backup.php",
           url:"get_quotation_details.php", 
   data:{from_date:from_date,to_date:to_date,status:status,type:"purchase"},
   type:"POST",
   dataType:"JSON",
   
           
            "dataSrc": function (json1) {
          var return_data=new Array(); 
          var data=json1.data;
          var k=1;
          for(var i=0;i<data.length;i++){
           
        return_data.push({
              
          'id':k++,
          "created_on":data[i].created_on,
         "customer_name":data[i].customer_name,
         //"adr1":data[i].adr1,
          //"adr2":data[i].adr2,
         //"gst_no":data[i].gst_no,
         "quotation_no":data[i].quotation_no,
        // "products":"<button class='btn btn-primary view' value='"+data[i].quotation_no+"' >View</button>",
         "email_status":data[i].email_status,
         "action":"&nbsp;"
+"<button class='align edit btn btn-primary' title='Edit' value='"+data[i].quotation_no+"'><span class='glyphicon glyphicon-pencil'></span></button>&nbsp;&nbsp;"
+"<button title='Download'   value='"+data[i].quotation_no+"'   class='get_report1 btn btn-primary'><img src='pdf.png' width='18' ></button>"
+"&nbsp;<button title='Email'   value='"+data[i].quotation_no+"' name='"+data[i].customer_name+"' email_id='"+data[i].email_id+"' class='email_pdf btn btn-primary'>Email</button>"
+"&nbsp;<button title='Delete'   value='"+data[i].quotation_no+"' name='"+data[i].customer_name+"' email_id='"+data[i].email_id+"' class='delete_quotation btn btn-primary'>Delete</button>",

           "recordsTotal":11,
          
           "recordsFiltered":11,
          
        });
       }
    $("#table11_filter").find("input").css({"width":"700px","margin-left":"-80%"});
       $("#table11_filter").find("input").attr("placeholder","Search Customer Name or Purchase Number");
      return return_data;
       }
    
        } ,
        "createdRow": function ( row, data, index ) {
         
                $('td',row).find(".credit").parent().parent().addClass('highlight');
            
        },
         "columnDefs": [
        { 
            "targets": [ 0,2,3,5], //first column / numbering column
            "orderable": false, //set not orderable
        },
        ],
        "columns": [
           { "data": "id" },
           { "data": "created_on" },
            { "data": "customer_name" },
              // { "data": "adr1" },
                // { "data": "adr2" },
                  // { "data": "gst_no" },
                       { "data": "quotation_no" },
                         
                        //   { "data": "products" },
                            { "data": "email_status" },
                                   { "data": "action" }
               
        ]
   
             });
              
                                    
       
}
</script>
<script>
    $("body").on("click",".get_report1",function(){
       var qtn=$(this).val();
       $.ajax({
          url:"get_sum_quotation_proforma.php" ,
          data:{qtn:qtn},
          type:"POST",
          dataType:"JSON",
          success:function(data1){
               get_pdf_edit(qtn,data1.total,data1.gst,data1.grand_total);
          },error:function(xhr){
              notify1(xhr.reponseText);
          }
       });
       
    });
$("body").on("click",".view",function(){
    var quotation_no=$(this).val();
     $.ajax({
      url:"get_quotation_details_all.php",
      data:{quotation_no:quotation_no},
      type:"POST",
     dataType:"JSON",
      success:function(data1){
         var data=data1.data;
         var customer_name=data1.customer_name;
          var html="<table class='table' id='cust_table'><thead><tr><th>Sl No</th>"
          
            +"<th>Product</th>"
    
    +"<th>HSN</th>"
     +"<th>Qty</th>"
    +"<th>Unit</th>"
    +"<th>Base Price</th>"
    +"<th>GST</th>"
    +"<th>Total</th>"
            +"</tr></thead><tbody>";
          var k=1;
          for(var i=0;i<data.length;i++){
              html=html+"<tr>"
               +"<td>"+ k++ +"</td>"  
            +"<td>"+data[i].product_name+"</td>"  
     +"<td>"+data[i].hsn+"</td>"  
      +"<td>"+data[i].qty+"</td>"  
       +"<td>"+data[i].unit+"</td>"  
        +"<td>"+data[i].base_price+"</td>"  
 +"<td>"+data[i].gst+"</td>"  
  +"<td>"+data[i].grand_total+"</td>"  
            +"</tr>";
          }
          html=html+"</tbody></table>";
          
          $("#pdf_div").html(html);
          $("#pdf_div").dialog({"title":customer_name,"width":"50%"});
          $("#cust_table").DataTable();
      },
      error:function(xhr){
          notify1(xhr.responseText);
      }
  });  
  
});

</script>

<div id='edit_dialog' style='display:none'>
    <form id='form11' class='align'>
                    Customer Name:<span class='required'></span><br>    
                 <input type='text' id='edit_name' style="width:350px"><br>
                  Address1:<span class='required'></span><br>
                  <input type='text' id='edit_adr1' style="width:350px"><br>
                  Address2:<span class='required'></span><br>
                  <input type='text' id='edit_adr2' style="width:350px"><br>
                  Address3:<span class='required'></span><br>
                  <input type='text' id='edit_adr3' style="width:350px"><br>
                  Mobile No:<br>
                  <input type='text' id='edit_msisdn'><br>
                    GST No:<br>
                 <input type='text' id='edit_gst_no'><br>
                  Email Id:<br>
                 <input type='text' id='edit_email_id'><br>
                  </form>
               <form id='form11' class='align' style='float:right'>
                    Ref No:    
                 <span id='edit_quotation_no'></span><br>
                
                  Date:<br>
                  <input type='text' class='datepicker' id='edit_from_date'><br>
                     Enquiry Ref No:<span class='required'></span><br>
                  <input type='text' class='' id='edit_enquiry_ref_no'><br>
                  <div style='display:none'> 
                  Kind Atn:<span class='required'></span><br>
                  <input type='text' class='' id='edit_kind_atn'><br>
                  </div>
                  </form>
                  
                    <br><br>
                       <table class='table' id='edit_table1'><thead>
        <tr style='background: green;
color: white;'><th>Sl no</th>
<th>Product</th>
<!--<th>Size</th>
<th>HSN/SAC</th>-->
<th>Qty</th>
<th>UOM</th>
<th>Unit Price</th>
<th>GST</th>
<th>Grand Total</th>
<th style='display:none'>FDA</th>
<th>Action</th>
</tr>
</thead><tbody></tbody></table>  
                    
                    
                    <form id='f1'>  
                    
                  <table>
                        <tr>
                              <td>
                 Product:
                 <br>
                 <input type="text" id='edit_product_name' style='width:230px' >
                </td>
<!--                <td>
                 Size:
                 <br>
                 <input type="text" id='edit_specification' style='width:100px' >
                </td>        
                <td>
                 HSN/SAC:
                 <br>
                 <input type="text"  id='edit_hsn' style='width:80px'>
                            </td>-->
                  <td>
                 Qty:
                 <br>
                 <input type="text" style='width:50px' id='edit_qty'>
                </td>
                 
                  <td>
                 UOM:
                 <br>
                 <input type="text" style='width:100px' id='edit_unit'>
               </td>
                <td>
                 Rate/unit(Rs):
                 <br>
                 <input type="text" style='width:120px' id='edit_base_price'>
                 </td>
                 <td>
                 GST:
                 <br>
                 <select style='width:50px' id='edit_gst'><option value='0'>0%</option>
                 <option value='5'>5%</option>
                 <option value='12'>12%</option>
                 <option value='18' selected>18%</option>
                 <option value='28'>28%</option></select>
                 </div>
                   <td>
                 Grand Total:
                 <br>
                 <input type="text" style='width:120px' id='edit_grand_total' disabled="">
                 </td>
                 <td style='display:none'>
                 FDA:
                 <br> <input type="checkbox" id='fda1'>
                
                 </td>
                <td>
                
                 <br>
                 <button id='edit_add_remove'>Add</button>
                </td>
                        </tr>
                    </table>
                         <input type='checkbox' id='edit_to_pay_basis'>Shipment Charges: to pay basis
                         <br> <br>
                       Shipment Mode:   <br><input type='text' id='edit_shipment_mode'>
                    </form>
                    <br>
                    <div style='display:none'>
                      Delivery:<span class='required'></span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input style="width:100px" type="text" id="edit_delivery"> Days 
                      <br> <br>
                      Payment Terms:<span class='required'></span>&nbsp;&nbsp;&nbsp;<input style="width:400px"  type="text" id="edit_payment_terms">  
                        <br>
                        <input type="checkbox" id="edit_warranty">Warranty
                         <br>
                        <input type="checkbox" id="edit_test_report">Test Report
                    </div>
                        
                <br>
                 <br> 
                 <button class='btn btn-primary' id='edit_add_invoice'>Update</button>
                 <br>
                   <table class='' id=''><thead>
                             <tr>
                                  <th colspan='7'></th>
                                 <th>Total Price &nbsp;</th><th>: &nbsp;</th><th class='edit_t1_total'></th></tr>
                             
                             <tr>
                                  <th colspan='7'></th>
                                  <th>Discount(%)</th><th><input style='width:50px' type='text' value='0' id='edit_discount'>&nbsp;</th><th id='edit_discount_val'></th></tr>
                               <tr>
                                  <th colspan='7'></th>
                                 <th>Net Price</th><th>: &nbsp;</th><th class='edit_net_total'></th></tr>
                                <tr>
                             
        <tr> <th colspan='7' style='width:7in'></th>
            <th>GST &nbsp;</th><th>: &nbsp;</th><th class='edit_t1_gst'></th></tr>
        <tr> <th colspan='7'></th> 
<th>Grand Total &nbsp;</th><th>: &nbsp;</th><th class='edit_t1_grand_total'></th>

</tr>
</thead><tbody></tbody></table> 
    
    
</div>


<script>

    var $input1 = $("#edit_name").autocomplete({
    /* select: function (event, ui) {
     var value = ui.item.name
     //ac_name=$("#acc_name_val").val();   
     member_id=ui.item.value;
     }, */
    select: function (event, ui) {
    $("#edit_name").val(ui.item.label); //ui.item is your object from the array
   
   $("#edit_adr1").val(ui.item.adr1);
 $("#edit_adr2").val(ui.item.adr2);
    $("#edit_gst_no").val(ui.item.gst);
     $("#edit_email_id").val(ui.item.email_id);
     $("#edit_msisdn").val(ui.item.msisdn);
     $("#edit_adr3").val(ui.item.adr3);
  
    return false;
    }, selectFirst: true,
            change: function (event, ui) {
            if (ui.item.label == "No results found") {
            //here is null if entered value is not match in suggestion list
            $(this).val((ui.item ? ui.item.id : ""));
            }
            },
            response: function (event, ui) {
            if (!ui.content.length) {
            var noResult = {value: "", label: "No results found"};
            ui.content.push(noResult);
            }
            },
            // source: "car_model.php",
//            source: [
//                       
//                               
//                               
//                                {
//                                  label: "c",
//                            value: "c",
//                                },
//                         
//                            ],
            source: function( request, response ) {
        $.ajax({
          url: "get_name_quotation.php",
          type:"POST",
          dataType: "json",
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
            minLength: 0,
    });
    $("<button class='align' type='button'>&nbsp;</button>")
            .attr("tabIndex", - 1)
            .attr("title", "Show All Items")
            .insertAfter($input1)
            .button({
            icons: {
            primary: "ui-icon-triangle-1-s"
            },
                    text: false
            })
            .removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function () {
            // close if already visible                        
            if ($input1.autocomplete("widget").is(":visible")) {
            $input1.autocomplete("close");
            return;
            }
            $(this).blur();
            $input1.autocomplete("search", "");
            $input1.focus();
            });
     $("span.ui-dialog-title").text('My New Title');

</script>
<script>
function name_auto(){ 
    var $input1 = $("#edit_product_name").autocomplete({
    /* select: function (event, ui) {
     var value = ui.item.name
     //ac_name=$("#acc_name_val").val();   
     member_id=ui.item.value;
     }, */
    select: function (event, ui) {
    $("#edit_product_name").val(ui.item.label); //ui.item is your object from the array
   
   
  
    return false;
    }, selectFirst: true,
 change: function (event, ui) {
            if (ui.item.label == "No results found") {
            //here is null if entered value is not match in suggestion list
            $(this).val((ui.item ? ui.item.id : ""));
            }
            },
            response: function (event, ui) {
            if (!ui.content.length) {
            var noResult = {value: "", label: "No results found"};
            ui.content.push(noResult);
            }
            },
            // source: "car_model.php",
//            source: [
//                       
//                               
//                               
//                                {
//                                  label: "c",
//                            value: "c",
//                                },
//                         
//                            ],
            source: function( request, response ) {
        $.ajax({
          url: "get_product_name.php",
          type:"POST",
          dataType: "json",
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
            minLength: 0,
    });
    $("<button class='align' type='button'>&nbsp;</button>")
            .attr("tabIndex", - 1)
            .attr("title", "Show All Items")
            .insertAfter($input1)
            .button({
            icons: {
            primary: "ui-icon-triangle-1-s"
            },
                    text: false
            })
            .removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function () {
            // close if already visible                        
            if ($input1.autocomplete("widget").is(":visible")) {
            $input1.autocomplete("close");
            return;
            }
            $(this).blur();
            $input1.autocomplete("search", "");
            $input1.focus();
            });
     $("span.ui-dialog-title").text('My New Title');
}
name_auto();
$("#edit_from_date").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", "0");



var k1=1;
$("#edit_add_remove").click(function(e){
    e.preventDefault();
    var product=$("#edit_product_name").val();
    var hsn=$("#edit_hsn").val();
    var qty=$("#edit_qty").val();
    var unit=$("#edit_unit").val();
    var base_price=$("#edit_base_price").val();
    var gst=$("#edit_gst").val();
      var specification=$("#edit_specification").val();
    var total=$("#edit_grand_total").val();
     var fda=$("#fda1").val();
     flag=0;
     if(product==""||hsn==""||qty==""||unit==""||base_price==""||gst==""||specification==""){
         notify1("All fields are mandatory");
        
            return false;
     }
     var i1=0;
     $("#edit_table1 tr").each(function(){
     if(i1>=1){
        var product1= $(this).find("td:nth-child(2)").find("input").val();
        if(product==product1){
            // flag=1;
           
        }
    }
    i1++;
    });
    if(flag==1){
        notify1("product Exists");return false; 
    }
    
        
     
     
     
    var html="";
    html=html+"<tr>"
       +"<td>"+ k1++ +"</td>"
    +"<td><input class='edit_product1' style='width:280px' type='text' value='"+product+"'></td>"
   //  +"<td><input class='edit_specification1' style='width:100px' type='text' value='"+specification+"'></td>"
    // +"<td><input class='edit_hsn1' type='text' style='width:120px'  value='"+hsn+"'></td>"
      +"<td><input class='edit_qty1' type='text' style='width:80px' value='"+qty+"'></td>"
       +"<td><input class='edit_unit1' type='text' style='width:100px'  value='"+unit+"'></td>"
        +"<td><input class='edit_base_price1' type='text'  style='width:100px' value='"+base_price+"'></td>"
 +"<td><select   style='width:50px' class='edit_gst1'>";
 if(gst==0){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }
 if(gst==5){
    html=html+"<option value='0' >0%</option>"
  +"<option value='5' selected>5%</option>"
  +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==18){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='18' selected>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==28){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
  +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28' selected>28%</option>";
 }
if(gst==12){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
  +"<option value='12' selected>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28' >28%</option>";
 }
 
  if($("#fda1").is(":checked")){
          fd= "<td><input type='checkbox' class='edit_fda1' checked>FDA</td>"  
     }else{
         fd=  "<td><input type='checkbox' class='edit_fda1'>FDA</td>"  
     }
     
 fd="";
     

 html=html+"</select></td>"
  +"<td><input type='text' disabled  class='edit_total1' style='width:100px'  value='"+total+"'></td>"
  +fd
  +"<td><button class='btn edit_delete'>Delete</button></td>"  
            +"</tr>";
    
    $("#edit_table1").append(html);
    $("#f1").trigger("reset");
    var k=1;
   $("#edit_table1 tr").each(function(i){
       var c=1
       $("td",this).each(function(){
           if(c==1){
               $(this).html(k++);
           }
           c++;
       });
       
   });
  $("#edit_discount").val(0);
    $("#edit_shipment_charges").val(0);
    $(".edit_net_total").html(0);
    $("#edit_discount_val").html(0);
    $("#edit_shipment_charges_val").html(0);

    edit_calculate();
       });
</script>


<script>
$("body").on("click",".edit_delete",function(){
    var id=$(this).val();
  var this_var=  $(this);
  this_var.parent().parent().remove();
  var k=1;
   $("#edit_table1 tr").each(function(i){
       var c=1
       $("td",this).each(function(){
           if(c==1){
               $(this).html(k++);
           }
           c++;
       });
       
   });
   $("#edit_discount").val(0);
    $("#edit_shipment_charges").val(0);
    $(".edit_net_total").html(0);
    $("#edit_discount_val").html(0);
    $("#edit_shipment_charges_val").html(0);
   
        edit_calculate();
  
  
//    $.ajax({
//      url:"delete_product_quotation.php",
//      data:{id:id},
//      type:"POST",
//      dataType:"JSON",
//      success:function(data1){
//          notify(data1+ " Row Deleted Successfully" );
//          this_var.parent().parent().remove();
//      },error:function(xhr){
//          notify1(xhr.responseText);
//  }
//});
});

$("body").on("input","#edit_qty",function(){
    var value=$(this).val();
   var unit_price= $(this).parent().parent().find("td:nth-child(4)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(6)").find("input");
    if(!isNaN(value) && !isNaN(unit_price) ){
      this_var.val(value*unit_price);
    }
});


$("body").on("input","#edit_base_price",function(){
    var unit_price=$(this).val();
 var qty= $(this).parent().parent().find("td:nth-child(2)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(6)").find("input");
    if(!isNaN(qty) && !isNaN(unit_price) ){
      this_var.val(qty*unit_price);
    }
});


$("body").on("input",".edit_qty1",function(){
    var value=$(this).val();
   var unit_price= $(this).parent().parent().find("td:nth-child(5)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(7)").find("input");
    if(!isNaN(value) && !isNaN(unit_price) ){
      this_var.val(value*unit_price);
    }
     $("#edit_discount").val(0);
       $("#edit_shipment_charges").val(0);
        $("#edit_discount_val").html(0);
         $(".edit_net_total").html(0);
          $("#edit_discount_val").html(0);
           $("#edit_shipment_charges_val").html(0);
     edit_calculate();
    
    
    
});


$("body").on("input",".edit_base_price1",function(){
    var unit_price=$(this).val();
 var qty= $(this).parent().parent().find("td:nth-child(3)").find("input").val();
  
     // var gst= $(this).parent().parent().find("td:nth-child(6)").find("select").val();
     
        var this_var= $(this).parent().parent().find("td:nth-child(7)").find("input");
    if(!isNaN(qty) && !isNaN(unit_price) ){
      this_var.val(qty*unit_price);
    }
     $("#edit_discount").val(0);
       $("#edit_shipment_charges").val(0);
        $("#edit_discount_val").html(0);
         $(".edit_net_total").html(0);
          $("#edit_discount_val").html(0);
           $("#edit_shipment_charges_val").html(0);
       edit_calculate();
    
});



$("body").on("change",".edit_gst1",function(){
  
     $("#edit_discount").val(0);
       $("#edit_shipment_charges").val(0);
        $("#edit_discount_val").html(0);
         $(".edit_net_total").html(0);
          $("#edit_discount_val").html(0);
           $("#edit_shipment_charges_val").html(0);
     
   edit_calculate();
    
})


function edit_calculate(){
    var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
             var i=0;
              var discount=$("#edit_discount").val();
     $("#edit_table1 tbody tr").each(function(){
     
        var product= $(this).find("td:nth-child(2)").find("input").val();
           var qty= $(this).find("td:nth-child(3)").find("input").val();
           var unit= $(this).find("td:nth-child(4)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
       // t1_gst=parseFloat(t1_gst)+parseFloat(gst/100)*grand_total;
         t1_gst=parseFloat(t1_gst)+parseFloat(gst/100*(grand_total-(grand_total*discount/100))); 
        t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
    
        console.log(t1_base_price);
});
var t_val=t1_base_price-t1_base_price*discount/100;
t1_gst_ship=t1_gst;
t1_gst=parseFloat(t1_gst)
//alert(edit_discount_val+"-"+edit_shipment_charges_val);
var t2=parseFloat(t1_gst)+parseFloat(t_val);
//var t2=parseFloat(t1_gst)+parseFloat(t1_grand_total);

edit_grand_t1_gst=t1_gst;
$(".edit_t1_total").html(t1_base_price);
      $(".edit_t1_gst").html(t1_gst);
       $(".edit_t1_grand_total").html(t2);
       //alert(t2);
}
</script>
<style>
  
  .ui-autocomplete {
     z-index: 999999 !important;
}  
</style>

<script>
$("body").on("click",".edit",function(){
    $("#edit_table1 tbody").html("");
    var qtn=$(this).val()
    $.ajax({
       url:"get_edit_quotation.php",
       data:{qtn:qtn},
       type:"POST",
       dataType:"JSON",
       success:function(data1){
           var cust_data=data1.cust_data;
           var product_data=data1.product_data;
            var html="";
            var k1=1;
            for(var i=0;i<product_data.length;i++){
    html=html+"<tr>"
       +"<td>"+ k1++ +"</td>"
    +"<td><input class='edit_product1' style='width:280px' type='text' value='"+product_data[i].product_name+"'></td>"
     // +"<td><input class='edit_specification1' type='text' style='width:100px'  value='"+product_data[i].specification+"'></td>"
    // +"<td><input class='edit_hsn1' type='text' style='width:100px'  value='"+product_data[i].hsn+"'></td>"
      +"<td><input class='edit_qty1' type='text' style='width:80px' value='"+product_data[i].qty+"'></td>"
       +"<td><input class='edit_unit1' type='text' style='width:100px'  value='"+product_data[i].unit+"'></td>"
        +"<td><input class='edit_base_price1' type='text'  style='width:100px' value='"+product_data[i].base_price+"'></td>"
 +"<td><select   style='width:50px' class='edit_gst1'>";
 var gst=product_data[i].gst;
 if(gst==0){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
  +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }
 if(gst==5){
    html=html+"<option value='0' >0%</option>"
  +"<option value='5' selected>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==18){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18' selected>18%</option>"
    +"<option value='28'>28%</option>";
 }

if(gst==28){
    html=html+"<option value='0' selected>0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12'>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28' selected>28%</option>";
 }

if(gst==12){
    html=html+"<option value='0' >0%</option>"
  +"<option value='5'>5%</option>"
   +"<option value='12' selected>12%</option>"
   +"<option value='18'>18%</option>"
    +"<option value='28' >28%</option>";
 }
if(product_data[i].fda=="yes"){
    fd="<td><input type='checkbox' class='edit_fda1' checked>FDA</td>"
}else{
    fd="<td><input type='checkbox' class='edit_fda1' >FDA</td>";
}
fd="";


 html=html+"</select></td>"
  +"<td><input type='text' disabled  class='edit_total1' style='width:100px'  value='"+product_data[i].grand_total+"'></td>"
+fd
            +"<td><button class='btn edit_delete'>Delete</button></td>"  
            +"</tr>";
    
    
    }
    $("#edit_table1").append(html);
    var k=1;
   $("#edit_table1 tr").each(function(i){
       var c=1
       $("td",this).each(function(){
           if(c==1){
               $(this).html(k++);
           }
           c++;
       });
       
   });
  $("#edit_discount").val(cust_data[0].discount);
                   $("#edit_discount_val").html(cust_data[0].discount_val);
                    $(".edit_net_total").html(cust_data[0].net_total);
                     $("#edit_shipment_charges").val(cust_data[0].shipment_charges);
                      $("#edit_shipment_charges_val").html(cust_data[0].shipment_charges_val);

    edit_calculate();
           
           
         if(cust_data[0].to_pay_basis=="yes"){
    $("#edit_to_pay_basis").attr("checked",true);
}
if(cust_data[0].test_report=="yes"){
   $("#edit_test_report").attr("checked",true);
}
  
           
           $("#edit_name").val(cust_data[0].customer_name);
            $("#edit_adr1").val(cust_data[0].adr1);
             $("#edit_adr2").val(cust_data[0].adr2);
              $("#edit_adr3").val(cust_data[0].adr3);
              $("#edit_msisdn").val(cust_data[0].msisdn);
              $("#edit_gst_no").val(cust_data[0].gst_no);
                $("#edit_enquiry_ref_no").val(cust_data[0].enquiry_ref_no);
                  $("#edit_kind_atn").val(cust_data[0].kind_atn);
                   $("#edit_email_id").val(cust_data[0].email_id);
                $("#edit_quotation_no").html(qtn);
                  $("#edit_delivery").val(cust_data[0].delivery);
                    $("#edit_payment_terms").val(cust_data[0].payment_terms);
                 $("#edit_from_date").val(cust_data[0].created_on);
              $("#edit_dialog").dialog({"width":"75%"});
       }
       
    });
});




$("#edit_add_invoice").click(function(){
var qtn= $("#edit_quotation_no").html();
    var customer_name=$("#edit_name").val();
    var adr1=$("#edit_adr1").val();
    var adr2=$("#edit_adr2").val();
     var gst_no=$("#edit_gst_no").val();
     var from_date=$("#edit_from_date").val();
     var enquiry_ref_no=$("#edit_enquiry_ref_no").val();
     var kind_atn=$("#edit_kind_atn").val();
      var email_id=$("#edit_email_id").val();
      var msisdn=$("#edit_msisdn").val();
      var adr3=$("#edit_adr3").val();
       var delivery=$("#edit_delivery").val();
         var discount= $("#edit_discount").val();
          var shipment_mode= $("#edit_shipment_mode").val();
   var shipment_charges= $("#edit_shipment_charges").val();
   var net_total= $(".edit_net_total").html();
   var discount_val= $("#edit_discount_val").html();
   var shipment_charges_val= $("#edit_shipment_charges_val").html();
   
   net_total=parseFloat(net_total).toFixed(2);
      discount=parseFloat(discount).toFixed(2);
      discount_val=parseFloat(discount_val).toFixed(2);
          var payment_terms=$("#edit_payment_terms").val();
          if($("#edit_warranty").is(":checked")){
              warranty="yes";
          }else{
              warranty="no";
          }
          if($("#edit_test_report").is(":checked")){
              test_report="yes";
          }else{
              test_report="no";
          }
           if($("#edit_to_pay_basis").is(":checked")){
              to_pay_basis="yes";
          }else{
              to_pay_basis="no";
          }
     if(customer_name==""||adr1==""||adr2==""||from_date==""||enquiry_ref_no==""){
         notify1("All * fields are mandatory");return false;
     }
      if(isNaN(delivery)){
             notify1("Delivery days should be numeric");return false;
         }
           var discount1= $("#edit_discount").val();
          if(discount1==""){
         alert("Discount cannot be empty");return false;
     }
         
    var quotation=[];
    var i=0;
     var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
    $("#edit_table1 tr").each(function(){
     if(i>=1){
        var product= $(this).find("td:nth-child(2)").find("input").val();
           var qty= $(this).find("td:nth-child(3)").find("input").val();
           var unit= $(this).find("td:nth-child(4)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        var fda= $(this).find("td:nth-child(8)").find("input").val();
         if($(this).find("td:nth-child(8)").find("input").is(":checked")){
              fda="yes";
          }else{
              fda="no";
          }
         t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
        t1_gst=parseFloat(t1_gst)+parseFloat(gst/100);
         t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
         
         
         
         
         
         quotation.push({"product":product,"qty":qty,"unit":unit,"base_price":base_price,"gst":gst,"grand_total":grand_total,"fda":fda});
     }
     
     
     
     
     i++;
    });
    
    var t2_grand=parseFloat((t1_gst*t1_grand_total))+parseFloat(t1_grand_total);

  
    
    
    
    
     quotation=JSON.stringify(quotation);
      $("#edit_add_invoice").attr("disabled", true);
      $.ajax({
      url:"update_quotation.php",
      data:{to_pay_basis:to_pay_basis,discount:discount,shipment_charges:shipment_charges,net_total:net_total,discount_val:discount_val,
        shipment_charges_val:shipment_charges_val,shipment_mode:shipment_mode,  
          test_report:test_report,warranty:warranty,delivery:delivery,payment_terms:payment_terms,adr3:adr3,msisdn:msisdn,email_id:email_id,enquiry_ref_no:enquiry_ref_no,kind_atn:kind_atn,qtn:qtn,quotation:quotation,customer_name:customer_name,adr1:adr1,adr2:adr2,gst_no:gst_no,from_date:from_date},
      type:"POST",
      dataType:"JSON",
      success:function(data1){
          notify(data1.data+ " Row Inserted Successfully");
           $("#edit_dialog").dialog("close");
           var from_date=$("#from_date1").val();     
   var to_date=$("#to_date1").val();     
  var status="";
get_cusomers(from_date,to_date,status);
//        $("#table1 tbody").remove();
//        $("#tab_11 input").each(function(){
//            $(this).val("");
//               $(this).html("");
//               
//        })
         // get_pdf(data1.quotation_no,t1_base_price,t1_gst,t2_grand);
        //get_pdf(97);
        $("#edit_add_invoice").attr("disabled",false);
      },
          error:function(xhr){
              notify1(xhr.responseText);
              $("#edit_add_invoice").attr("disabled",false);
          }
   });
})

var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");

//function number2words(n){
//    
//    if (n < 20) return num[n];
//    var digit = n%10;
//    if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
//    if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " " + number2words(n%100));
//    return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
//}

function number2words(amount) {
    var words = new Array();
    words[0] = '';
    words[1] = 'One';
    words[2] = 'Two';
    words[3] = 'Three';
    words[4] = 'Four';
    words[5] = 'Five';
    words[6] = 'Six';
    words[7] = 'Seven';
    words[8] = 'Eight';
    words[9] = 'Nine';
    words[10] = 'Ten';
    words[11] = 'Eleven';
    words[12] = 'Twelve';
    words[13] = 'Thirteen';
    words[14] = 'Fourteen';
    words[15] = 'Fifteen';
    words[16] = 'Sixteen';
    words[17] = 'Seventeen';
    words[18] = 'Eighteen';
    words[19] = 'Nineteen';
    words[20] = 'Twenty';
    words[30] = 'Thirty';
    words[40] = 'Forty';
    words[50] = 'Fifty';
    words[60] = 'Sixty';
    words[70] = 'Seventy';
    words[80] = 'Eighty';
    words[90] = 'Ninety';
    amount = amount.toString();
    var atemp = amount.split(".");
    var number = atemp[0].split(",").join("");
    var n_length = number.length;
    var words_string = "";
    if (n_length <= 9) {
        var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var received_n_array = new Array();
        for (var i = 0; i < n_length; i++) {
            received_n_array[i] = number.substr(i, 1);
        }
        for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
            n_array[i] = received_n_array[j];
        }
        for (var i = 0, j = 1; i < 9; i++, j++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                if (n_array[i] == 1) {
                    n_array[j] = 10 + parseInt(n_array[j]);
                    n_array[i] = 0;
                }
            }
        }
        value = "";
        for (var i = 0; i < 9; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                value = n_array[i] * 10;
            } else {
                value = n_array[i];
            }
            if (value != 0) {
                words_string += words[value] + " ";
            }
            if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Crores ";
            }
            if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Lakhs ";
            }
            if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Thousand ";
            }
            if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                words_string += "Hundred and ";
            } else if (i == 6 && value != 0) {
                words_string += "Hundred ";
            }
        }
        words_string = words_string.split("  ").join(" ");
    }
    return words_string;
}
</script>



<script>

$("body").on("click",".email_pdf",function(){
     quotation=$(this).val();
     var email_id=$(this).attr("email_id")+",info@armolpolymers.com,sales@armolpolymers.com";
     var name=$(this).attr("name");
     $("#email_id_send").val(email_id);
     $("#email_div").dialog({"width":"70%","title":name});
    
});

$("body").on("click","#send_email",function(){
   
     var email_id=$("#email_id_send").val();
     var cc=$("#cc_send").val();
        var sub=$("#subject_send").val();
           var message=$(".nicEdit-main").html();
     
     $.ajax({
          url:"get_sum_quotation_proforma.php" ,
          data:{qtn:quotation},
          type:"POST",
          dataType:"JSON",
          success:function(data1){
               get_pdf_email(quotation,data1.total,data1.gst,data1.grand_total,email_id,cc,sub,message);
          },error:function(xhr){
              notify1(xhr.reponseText);
          }
       });
});


function   get_pdf_email(quotation,t1_base_price,t1_gst,t2_grand,email_id,cc,sub,message){
   
    $.ajax({
      url:"get_data_for_pdf.php",
      data:{quotation_no:quotation},
      type:"POST",
      dataType:"JSON",
      success:function(data1){
         var customer_data=data1.data;
         var product_data=data1.production;
             var date=customer_data[0].created_on;
        var y=date.split("-")[0];
        var m=date.split("-")[1];
        var d=date.split("-")[2];
        var dt=d+"-"+m+"-"+y;
       var  pdf_data=pdf_data1;
       pdf_data=pdf_data.replace("#shipment_mode",customer_data[0].shipment_mode);
         pdf_data=pdf_data.replace("#customer_name",customer_data[0].customer_name);
         pdf_data=pdf_data.replace("#adr1",customer_data[0].adr1);
         pdf_data=pdf_data.replace("#adr2",customer_data[0].adr2);
         pdf_data=pdf_data.replace("#cust_gst",customer_data[0].gst);
         pdf_data=pdf_data.replace("#gst_no","29AASFA7701N1ZT");
         pdf_data=pdf_data.replace("#quotation_no",customer_data[0].quotation_no);
          pdf_data=pdf_data.replace("#date",dt);
           pdf_data=pdf_data.replace("#enquiry_ref_no",customer_data[0].enquiry_ref_no);
            pdf_data=pdf_data.replace("#kind_attn",customer_data[0].kind_atn);
             pdf_data=pdf_data.replace("#grand_total",addZeroes(t1_base_price));
              pdf_data=pdf_data.replace("#grand_gst",addZeroes(t1_gst));
               pdf_data=pdf_data.replace("#grand_total1",addZeroes(t2_grand));
                pdf_data=pdf_data.replace("#amount_in_words",number2words(Math.round(t2_grand))+' Only');
                pdf_data=pdf_data.replace("#adr3",customer_data[0].adr3);
                 pdf_data=pdf_data.replace("#email",customer_data[0].email_id);
                  pdf_data=pdf_data.replace("#msisdn",customer_data[0].msisdn);
             pdf_data=pdf_data.replace("#delivery",customer_data[0].delivery);
                  pdf_data=pdf_data.replace("#terms",customer_data[0].payment_terms);
                   pdf_data=pdf_data.replace("#discount",customer_data[0].discount);
               if(customer_data[0].to_pay_basis=="yes"){
                       pdf_data=pdf_data.replace("#to_pay_basis","Shipment Charges:to pay basis");
                  }else{
                       pdf_data=pdf_data.replace("#to_pay_basis","");
                  }
                      pdf_data=pdf_data.replace("#net_total",customer_data[0].net_total);
                       pdf_data=pdf_data.replace("#discount_val",customer_data[0].discount_val);
                  
                  if(customer_data[0].test_report!="yes"){
                       pdf_data=pdf_data.replace("*TEST REPORTS: All testing will be carried out in-house and Test Certificates will be sent along with invoice.","");
                  }
                  if(customer_data[0].warranty!="yes"){
                       pdf_data=pdf_data.replace("*WARRANTY: All products of Armol Polymers are warranted against failure due to defective materials or poor workmanship. If the buyer wants to claim warranty against these, the buyer needs to inform Armol Polymers within 30 days of product delivery. After the stipulated time of 30 days, no warranty claim is accepted. Warranty claim will not be accepted for any reasons except reasons mentioned above.","");
                  }
            var html="";var k=1;
        for(var i=0;i<product_data.length;i++){
             if(product_data[i].fda=="yes"){
                pd="<td style='text-align:left'>"+product_data[i].product_name+"<br></br><span >Conforming to FDA 21. CFR. 177. 2600</span></td>"
            }else{
                  pd="<td style='text-align:left'>"+product_data[i].product_name+"</td>"
            }
            
            html=html+"<tr class='pagebreak1' style='text-align:center;padding:3px'><td>"+ k++ +"</td>"
            +pd
   //  +"<td ><div>"+product_data[i].specification+"</div></td>"
   // +"<td>"+product_data[i].hsn+"</td>"
    +"<td>"+product_data[i].qty+"</td>"
    +"<td>"+product_data[i].unit+"</td>"
     +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].gst))+"%</td>"
    +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].base_price))+"</td>"
    
    +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].grand_total))+"</td>"
   
    
            +"</tr>"
        }
          pdf_data=pdf_data.replace("<tr><td>#sl_no</td><td>#description</td><td>#qty</td><td>#unit</td><td>#gst</td><td>#base_price</td><td>#total</td></tr>",html);
    //pdf_data=pdf_data+"<style type='text/css'> .pagebreak1{page-break-after: always !important;page-break-before: always !important;page-break-inside:     avoid !important;}</style>";   
 $.ajax({
      url:"get_pdf_email.php",
      data:{pdf_data:pdf_data,email_id:email_id,cc:cc,sub:sub,message:message,quotation:quotation},
      type:"POST",
     dataType:"JSON",
      success:function(data1){
         
         notify("Email Sent Successfully");
          get_cusomers("","","");
      },
       error:function(xhr){
              notify1(xhr.responseText);
          }
  });  
    
      },
          error:function(xhr){
              notify1(xhr.responseText);
          }
   });
    
    
}
</script>
    <div id='email_div' style='display:none'>
        To:<span style="font-size:9px">(Put comma in between for multiple email ids)</span><br>
        <input type='text' id='email_id_send' style="width:300px"><br>
        CC:<span style="font-size:9px">(Put comma in between for multiple email ids)</span><br> <input type='text' id='cc_send' style="width:300px"><br>
        Subject:<br>
         <input type='text' id='subject_send' value='PURCHASE ORDER'><br>
          Message:<br>
        <textarea class="form-control" cols='1000' rows="5" id="comment" name="message" style="display: none;width:70vw">Dear Sir/Madam,&lt;br&gt;&lt;br&gt;Greeting from &lt;strong&gt;Armol Polymers&lt;/strong&gt;.

Please find herewith attached  PO inline  for your kind perusal.&lt;br&gt;&lt;br&gt;

Kindly call on us for any further clarifications / changes if needed.&lt;br&gt;&lt;br&gt;
Regards,&lt;br&gt;
Armol Polymers<br>
www.armolpolymers.com&lt;br&gt; 
</textarea>
          <br><br>
          <button class='btn btn-primary' id='send_email'>Send</button>
</div>
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script>
<script type="text/javascript">
//<![CDATA[
  bkLib.onDomLoaded(function() {
        new nicEditor({fullPanel : true,maxHeight : 200}).panelInstance('comment');
       
  });
  //]]>
  

   
  
    
   
   
  
   
   
   
    $("body").on("input","#edit_shipment_charges",function(){
       var shipment_charges=$(this).val();
      if(isNaN(shipment_charges)){
           $(this).val("0");
       }
         var shipment_charges=$(this).val();
       var value=(shipment_charges*18/100);
     
       $("#edit_shipment_charges_val").html(shipment_charges);
        //var t1_gst=$(".edit_t1_gst").html();
       // alert(edit_grand_t1_gst);
        var t1_gst=parseFloat(t1_gst_ship)+parseFloat(value);
       var discount=$("#edit_discount").val();
        var t1_total=$(".edit_t1_total").html();
        var net_total=$(".edit_net_total").html();
      //  alert(t1_gst);
        var t=parseFloat(shipment_charges)+parseFloat(t1_gst)+parseFloat(t1_total-t1_total*discount/100);
        $(".edit_t1_grand_total").html(t);
      // edit_calculate_discount(0,0,shipment_charges);
        $(".edit_t1_gst").html(t1_gst);
         
   });
  
   
   function calculate_discount(discount,discount_val,shipment_charges){
    var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
             var i=0;
     $("#table1 tr").each(function(){
     if(i>=1){
        var product= $(this).find("td:nth-child(2)").find("input").val();
         //var hsn= $(this).find("td:nth-child(4)").find("input").val();
          var qty= $(this).find("td:nth-child(3)").find("input").val();
          // var unit= $(this).find("td:nth-child(6)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
        t1_gst=parseFloat(t1_gst)+parseFloat(gst/100)*(grand_total-(grand_total*discount/100));
         t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
    
        }
    i++;
});

var t2=parseFloat(t1_gst)+parseFloat(t1_grand_total)-discount_val;
$(".t1_total").html(t1_base_price);
      $(".t1_gst").html(t1_gst);
      grand_t1_gst=t1_gst;
       $(".t1_grand_total").html(t2);
}
   
   function edit_calculate_discount(discount,discount_val,shipment_charges){
    var t1_base_price=0;
           var t1_gst=0;
             var t1_grand_total=0;
             var i=0;
     $("#edit_table1 tr").each(function(){
     if(i>=1){
        var product= $(this).find("td:nth-child(2)").find("input").val();
         //var hsn= $(this).find("td:nth-child(4)").find("input").val();
          var qty= $(this).find("td:nth-child(3)").find("input").val();
          // var unit= $(this).find("td:nth-child(6)").find("input").val();
            var base_price= $(this).find("td:nth-child(5)").find("input").val();
             var gst= $(this).find("td:nth-child(6)").find("select").val();
         var grand_total= $(this).find("td:nth-child(7)").find("input").val();
        t1_base_price=parseFloat(t1_base_price)+parseFloat(grand_total);
        t1_gst=parseFloat(t1_gst)+parseFloat(gst/100)*(grand_total-(grand_total*discount/100));
         t1_grand_total=parseFloat(t1_grand_total)+parseFloat(grand_total);
    
        }
    i++;
});

var t2=parseFloat(t1_gst)+parseFloat(t1_grand_total)-discount_val;
$(".edit_t1_total").html(t1_base_price);
      $(".edit_t1_gst").html(t1_gst);
      grand_t1_gst=t1_gst;
       $(".edit_t1_grand_total").html(t2);
}
   
   
  function  get_pdf_edit(quotation,t1_base_price,t1_gst,t2_grand){
   
    $.ajax({
      url:"get_data_for_pdf.php",
      data:{quotation_no:quotation},
      type:"POST",
      dataType:"JSON",
      success:function(data1){
         var customer_data=data1.data;
         var product_data=data1.production;
        var pdf_data=pdf_data1;
        var date=customer_data[0].created_on;
        var y=date.split("-")[0];
        var m=date.split("-")[1];
        var d=date.split("-")[2];
        var dt=d+"-"+m+"-"+y;
        pdf_data=pdf_data.replace("#shipment_mode",customer_data[0].shipment_mode);
         pdf_data=pdf_data.replace("#customer_name",customer_data[0].customer_name);
         pdf_data=pdf_data.replace("#adr1",customer_data[0].adr1);
         pdf_data=pdf_data.replace("#adr2",customer_data[0].adr2);
          pdf_data=pdf_data.replace("#your",customer_data[0].gst_no);
         pdf_data=pdf_data.replace("#gst_no","29AASFA7701N1ZT");
         pdf_data=pdf_data.replace("#quotation_no",customer_data[0].quotation_no);
          pdf_data=pdf_data.replace("#date",dt);
           pdf_data=pdf_data.replace("#enquiry_ref_no",customer_data[0].enquiry_ref_no);
            pdf_data=pdf_data.replace("#kind_attn",customer_data[0].kind_atn);
             pdf_data=pdf_data.replace("#grand_total",addZeroes(t1_base_price));
              pdf_data=pdf_data.replace("#grand_gst",addZeroes(t1_gst));
               var t=parseFloat(t2_grand);
              pdf_data=pdf_data.replace("#discount",customer_data[0].discount);
                    // pdf_data=pdf_data.replace("#shipment_charges",customer_data[0].shipment_charges);
                      pdf_data=pdf_data.replace("#net_total",customer_data[0].net_total);
                       pdf_data=pdf_data.replace("#discount_val",customer_data[0].discount_val);
                  if(customer_data[0].to_pay_basis=="yes"){
                       pdf_data=pdf_data.replace("#to_pay_basis","Shipment Charges:to pay basis");
                  }else{
                       pdf_data=pdf_data.replace("#to_pay_basis","");
                  }
               pdf_data=pdf_data.replace("#grand_total1",addZeroes(t));
                pdf_data=pdf_data.replace("#adr3",customer_data[0].adr3);
                 pdf_data=pdf_data.replace("#email",customer_data[0].email_id);
                  pdf_data=pdf_data.replace("#msisdn",customer_data[0].msisdn);
                  pdf_data=pdf_data.replace("#delivery",customer_data[0].delivery);
                  pdf_data=pdf_data.replace("#terms",customer_data[0].payment_terms);
                  
                  if(customer_data[0].test_report!="yes"){
                       pdf_data=pdf_data.replace("*TEST REPORTS: All testing will be carried out in-house and Test Certificates will be sent along with invoice.","");
                  }
                  if(customer_data[0].warranty!="yes"){
                       pdf_data=pdf_data.replace("*WARRANTY: All products of Armol Polymers are warranted against failure due to defective materials or poor workmanship. If the buyer wants to claim warranty against these, the buyer needs to inform Armol Polymers within 30 days of product delivery. After the stipulated time of 30 days, no warranty claim is accepted. Warranty claim will not be accepted for any reasons except reasons mentioned above.","");
                  }
                pdf_data=pdf_data.replace("#amount_in_words",number2words(Math.round(t))+' Only');
               
            
            var html="";var k=1;
        for(var i=0;i<product_data.length;i++){
            
            if(product_data[i].fda=="yes"){
                pd="<td style='text-align:left'>"+product_data[i].product_name+"<br></br><span>Conforming to FDA 21. CFR. 177. 2600</span></td>"
            }else{
                  pd="<td style='text-align:left'>"+product_data[i].product_name+"</td>"
            }
            html=html+"<tr class='pagebreak1' style='text-align:center;padding:3px'><td>"+ k++ +"</td>"
            +pd
    // +"<td ><div>"+product_data[i].specification+"</div></td>"
    //+"<td>"+product_data[i].hsn+"</td>"
    +"<td>"+product_data[i].qty+"</td>"
    +"<td>"+product_data[i].unit+"</td>"
      +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].gst))+"%</td>"
    +"<td  style='text-align:right'>"+addZeroes(parseFloat(product_data[i].base_price))+"</td>"
   
    +"<td style='text-align:right'>"+addZeroes(parseFloat(product_data[i].grand_total))+"</td>"
   
    
            +"</tr>"
        }
          pdf_data=pdf_data.replace("<tr><td>#sl_no</td><td>#description</td><td>#qty</td><td>#unit</td><td>#gst</td><td>#base_price</td><td>#total</td></tr>",html);
    //pdf_data=pdf_data+"<style type='text/css'> .pagebreak1{page-break-after: always !important;page-break-before: always !important;page-break-inside:     avoid !important;}</style>";   
 //$("#pdf_div").html(pdf_data);
    $.ajax({
      url:"get_pdf.php",
      data:{pdf_data:pdf_data},
      type:"POST",
     
      success:function(data1){
         
          $("#pdf_link").attr("href","downloads/"+data1);
           $("#pdf_link")[0].click();
         
          
      }
  });   
         
      },
          error:function(xhr){
              notify1(xhr.responseText);
          }
   });
    
    
} 
</script>
<?php
include("get_login_modal.php");

?>
  