<?php  include "header.php"; ?>

<!--<section class="content-header">
            <h1>Add Sales</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add Sales</li>            
            </ol>
        </section>-->

<div class="content-wrapper">
        


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                <div class="box-body">
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                                                
                                                
                                                                 
                        <div class="row">
                            
                           
                            
                            
                            
                            <div class="col-md-8">
                                
                                
                                
                                
                    <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="customer_id">Customers</label>                                   
                                    <select name="customer_id" class="form-control select2" style="width:100%" id="customer_id" required="required">
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customers` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?php if(isset($_GET['cus_id']) && $_GET['cus_id']==$result['id']){echo "selected";}?>><?=$result['customer_name']?> - <?=$result['mobile_no']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                                
                            </div>
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="invoice_no">Estimation No</label>                                    
                                    <input type="invoice_no" name="invoice_no" value="" placeholder="Enter the Estimation No" class="form-control" id="invoice_no"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="date">Date</label>                                    
                                    <input type="date" name="date" value="<?=date('Y-m-d')?>"  class="form-control" id="date"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="sales_status">Sales Status</label>                                   
                                    <select name="sales_status" class="form-control select2" style="width:100%" id="sales_status" required="required">
                                        <option value="1">Sales</option>
                                        <option value="2">Quotation</option>
                                    </select>
                                </div>
                            </div>
                    </div> 
                                
                                <!-- Old Gold Section Starts Here -->
                                <div class="col-md-12" style="margin: 10px; background: #9db6e3; padding: 16px; border-radius: 7px;">
                                <span id="old_gold_button" class="btn btn-warning btn-xs"><b style="font-family: system-ui;">Old Gold Section (Show/Hide)</b></span>&nbsp;&nbsp;&nbsp;
                                
                                <div id="old_gold_section" style="margin-top: 10px;">
                                    
                                    <table style="width: 100%;">
                                        <thead>
                                            <tr>
                                                <th style="width: 15%;">Product</th>
                                                <th style="width: 15%;">Purity</th>
                                                <th style="width: 10%;">Unit</th>
                                                <th style="width: 10%;">Type</th>
                                                <th style="width: 10%;">Weight</th>
                                                
                                                <th style="width: 10%;">Price</th>
                                                <th style="width: 10%;">Total</th>
                                                <th style="width: 10%;">Actions <span id="add_row_btn" class="btn btn-success btn-xs"><i class="fa fa-plus"></i></span></th>
                                            </tr>
                                        </thead>
                                        <tbody id="old_append_tbody">
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                                <!-- Old Gold Section Endsss Here -->
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <div class="form-group search-box">
                                    <label class="" for="search_products">Search Products</label>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                       
                                    <input type="text" name="search_products" placeholder="Enter the Products" value=""  class="form-control" id="search_products"  />
                                    <div class="result" style="max-height: 200px; overflow-y: scroll;"></div>
                                </div>
                                
                                <table style="width: 100%;" >
                                    <thead>
                                        <tr>
                                            <th style="width: 10%">Product&nbsp;Name</th>
                                            <th style="width: 10%">HSN</th>
                                            <th style="width: 10%">Unit</th>
                                            <th style="width: 7%">Qty</th>
                                            <th style="width: 15%">Cross - Stone - Net</th>
                                            <th style="width: 10%">Rates</th>
                                            <th style="width: 8%">Gold</th>
                                            <th style="width: 8%">Stone</th>
                                            <th style="width: 8%">Wastage</th>
                                            <th style="width: 10%">Total</th>
                                            <th style="width: 5%"><i class="fa fa-times"></i></th>
                                        </tr>
                                    </thead>
                                    <tbody id="product_tbody">
                                        
                                    </tbody>
                                </table>
                            </div>
                            
                            
                            <div class="col-md-4">
                                <div class="col-md-12" hidden>
                                    <div class="form-group">
                                        <label class="required" for="total_amount">Total Amount</label>                                   
                                         <input type="text" name="total_amount" placeholder="Total Amount" value="0"  class="form-control" id="total_amount"  required="required" />
                                    </div>
                                </div>
                                
                                <!-- GST Starts Here -->
                                <div class="col-md-12">
                                    <div class="col-lg-12" style="background: #ebebe1; padding-top: 10px;margin-top: 20px;padding-bottom: 10px;border-radius: 15px;box-shadow: 2px 1px 2px 2px;">
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <b>Total Items</b>
                                        </div>
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <span id="total_product_items" style="font-weight: bold;">0</span>
                                        </div>
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <b>Total</b>
                                        </div>
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <i class="fa fa-inr"></i> <span id="total_product_amount" style="font-weight: bold;">0.00</span>
                                        </div>
                                        <div class="col-lg-6" style="padding-top: 15px;">
                                            <b>Select Tax (%)</b>
                                        </div>
                                        
                                        <div class="col-lg-6" style="padding-top: 15px;">
                                            <select name="gst_percentage"  class="form-control" id="tax" style="width: 100%;" onkeyup="calculate()" onchange="calculate()">
                                                <option value="0">0% GST</option>
                                            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_gst` WHERE status=1");
                                                $statement->execute();
                                                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                foreach ($resultss as $ress) { ?>
                                                    <option value="<?=$ress['gst']?>"><?=$ress['name']?></option>
                                            <?php } ?>
                                            </select>
                                        </div>
                                        
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <div class="form-group">
                                                <label class="" for="insurance_policy">Select Tax Type</label>
                                            </div>
                                         </div>
                                        
                                        <div class="col-lg-6" style="padding-top: 10px;">
                                            <div class="form-group">
                                                <select name="tax_type" id="tax_type" onkeyup="calculate()" style="width: 100%;" onchange="calculate()" required="required" class="form-control">
                                                    <option value="1" selected="selected">Exclusive</option>
                                                    <option value="2">Inclusive</option>
                                                </select>
                                            </div>
                                         </div>
                                        
                                         
                                        <div class="col-lg-6">
                                            <b>Select GST Type</b>
                                         </div>
                                        
                                        <div class="col-lg-6">
                                            <div class="form-group">
                                                <select name="gst_type" id="gst_type" onkeyup="calculate()" onchange="calculate()" required="required" class="form-control">
                                                    <option value="1" selected="selected">GST</option>
                                                    <option value="2">IGST</option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        
                                        <!-- GST HERE -->
                                        <div class="row" id="div-cgst">
                                                <div class="col-md-8" >
                                                            <div class="form-group" style="margin-bottom: 6px;">
                                                                <label for="code">Total CGST &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='cgst_tax_per' name='cgst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                                                            </div>
                                                </div>
                                                <div class="col-md-4">
                                                        <div class="form-group" style="margin-bottom: 6px;">
                                                            <span style="color: blue;font-weight: 900;font-size: 18px;" id='cgst_tax_amount'>0.00</span>
                                                        </div>
                                                </div>
                                            </div>
                                            
                                            <div class="row" id="div-sgst">
                                                <div class="col-md-8" >
                                                            <div class="form-group" style="margin-bottom: 6px;">
                                                                <label for="code">Total SGST &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='sgst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                                                            </div>
                                                </div>
                                                <div class="col-md-4">
                                                        <div class="form-group" style="margin-bottom: 6px;">
                                                            <span style="color: blue;font-weight: 900;font-size: 18px;" id='sgst_tax_amount'>0.00</span>
                                                        </div>
                                                </div>
                                            </div>
                                            
                                            <div class="row" id="div-sgst">
                                                <div class="col-md-8" >
                                                            <div class="form-group" style="margin-bottom: 6px;">
                                                                <label for="code">Total &nbsp; <span id='gst_type_span'></span> &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='gst_tax_per' name='gst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                            </div>
                                                </div>
                                                <div class="col-md-4">
                                                        <div class="form-group" style="margin-bottom: 6px;">
                                                            <span style="color: blue;font-weight: 900;font-size: 18px;" id='total_tax'>0.00</span>
                                                        </div>
                                                </div>
                                            </div>
                                            
                                            
                                            <!-- Hidden GST Values Here -->
                                            <input type="hidden" name="gst_or_igst_amount" id="total_tax_input" value="">
                                            <input type="hidden" name="cgst_or_sgst_amount" id="cgst_tax_per_input" value="">
                                            <!-- Hidden GST Values Here -->
                                            
                                            
                                            <div class="row" id="" style="padding-top: 10px;">
                                                <div class="col-md-6" >
                                                        <div class="form-group">
                                                            <div class="form-group">
                                                                <label for="code">Total Amount After Tax:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                            </div>
                                                        </div>
                                                </div>
                                                <div class="col-md-6">
                                                        <div class="form-group">
                                                            <input type="text" name="tax_after_total" id="tax_after_total" class="form-control" value="" required="required" style="width: 90%; background: #fd0a36;; color: white;font-weight: 600;font-size: 20px;" readonly>
                                                            <!--<span style="color: blue;font-weight: 900;font-size: 18px;" id='tax_after_total'>0.00</span> -->
                                                        </div>
                                                </div>
                                            </div>
                                        <!-- GST HERE -->
                                                     
                                    </div>
                                </div>
                                <!-- GST Endsss Here -->
                                
                                
                                
                                
                                <div class="col-md-6" style="margin-top: 10px;">
                                    <div class="form-group" style="">
                                        <label for="discount_type">Discount Type <span style="color: red">*</span></label>
                                        <select name="discount_type" id="discount_type" onchange="discountType()" class="form-control select22" style="width: 100%;">
                                            <option value="1">Percentage</option>
                                            <option value="2">Fixed Amount</option>
                                        </select>
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-6" style="margin-top: 10px;">
                                    <div class="form-group">
                                        <label class="optional" for="discount">Discount <span id="discount_span"></span></label>                                   
                                         <input type="text" name="discount" placeholder="Discount" value="0"  oninput="calculateTotal()" class="form-control" id="discount"  />
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-12"><b style="color: blue;">Discount (<i class="fa fa-inr"></i>) : <span id="discount_amount_span">0.00</span></b></div>
                                <div class="col-md-12"><b style="color: blue;">Old Product Amount (<i class="fa fa-inr"></i>) : <span id="old_product_amount_span">0.00</span></b></div>
                                <input type="hidden" name="discount_amount" id="discount_amount" value="" >
                                <input type="hidden" name="old_product_amount" id="old_product_amount" value="" >
                                
                                
                                
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="grand_amount">Grand Amount</label>                                   
                                         <input type="text" name="grand_amount" placeholder="Grand Amount" value=""  class="form-control" id="grand_amount"  required="required" readonly />
                                    </div>
                                </div>
                                
                                
                                
                                
                                
                                
                                
                                
                            </div>
                            
                            
                           
                            
                            
                            
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Add Sales" class="btn btn-success" />
                        </form>
                        </div>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <a href="pos.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                            <a href="sales-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>
<?php include "footer.php"; ?>


<script>

$(document).ready(function() {
    
var count = 1;
$("#add_row_btn").click(function(){
    
    $("#add_row_btn").hide();
    
                var row=`<tr class="old_gold_row">
                                                <td>
                                                    <select name="old_product_ids[]" id="product_ids_`+count+`" class="form-control select22" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="old_purity[]" id="purity_ids_`+count+`" onchange="GetPriceValues(`+count+`)" class="form-control purity_ids select22" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <select name="old_unit[]" id="unit_`+count+`" class="form-control select22 old_unit" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_units` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <select name="old_product_type[]" id="old_product_type_`+count+`" class="form-control select22 old_product_type" style="width: 100%;">
                                                        <option value="1">OWN</option>
                                                        <option value="2">OTHER</option>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <input type="text" name="old_weight[]" class="form-control table_input old_weight " id="weight_`+count+`" placeholder="Weight" onkeypress="return isNumberKey(this, event);" required data-count="`+count+`">
                                                </td>
                                                
                                                
                                                <td>
                                                    <input type="text" name="old_price[]" class="form-control table_input old_price" id="old_price_`+count+`" placeholder="Price" onkeypress="return isNumberKey(this, event);" required>
                                                </td>
                                                <td>
                                                    <input type="text" name="old_sub_total[]" class="form-control table_input old_total" id="old_sub_total_`+count+`" placeholder="Total" onkeypress="return isNumberKey(this, event);" required>
                                                </td>
                                                
                                                <td style="background: white;">
                                                    <span class="btn btn-danger btn-sm remove_product"  style="margin-top: 0px;"><i class="bi bi-trash3-fill"></i></span>
                                                </td>
                                            </tr>`;
    
        $("#old_append_tbody").append(row);
        $("#add_row_btn").show();
        $(".select22").select2();
        GetPurity(count);
        count++;
});
});

function GetPurity(count){
    console.log("Get Purity");
    $('#purity_ids_'+count).empty();  
    var product_ids = $("#product_ids_"+count).val();
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_purity',product_ids:product_ids},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    var m='';
                    for(var n=0;n<response['data'].length;n++){
                        m+='<option value='+response['data'][n]['id']+'>'+response['data'][n]['name']+'</option>';
                    }
                 $('#purity_ids_'+count).append(m);  
                 $('#purity_ids_'+count).trigger('change');
                 
                }
                GetPriceValues(count);
             
            }
        });
    
}
$(document).on('click', '.remove_product', function() {
        $(this).closest('.old_gold_row').remove();
        grand_old_pro_total();
        calculateTotal();
});

function GetPriceValues(count){
    console.log("Get Values");
    var product_ids = $("#product_ids_"+count).val();
    var purity_ids = $("#purity_ids_"+count).val();
    
    $.ajax({
        url: 'api/get-values-api.php',
        type: 'post',
        dataType: 'json',
        data: {type:'get_today_rates',product_ids:product_ids, purity_ids:purity_ids},
        success: function (response) {
            console.log(response);
            if(response['error']==false){
                console.log(response);
                $("#old_price_"+count).val(response['today_rate']);
                OldSectionTotal(count);
            }
        }
    });
    
    
    
}



function OldSectionTotal(count){
    var unit = $("#unit_"+count).val();
    var weight = $("#weight_"+count).val();
    var price = $("#old_price_"+count).val();
    
    if(unit==1){
        cal_s = price*weight;
        $("#old_sub_total_"+count).val(cal_s.toFixed(2));
    }else if(unit==2){
        cal_s = price*(weight*1000);
        $("#old_sub_total_"+count).val(cal_s.toFixed(2));
    }
    grand_old_pro_total();
    calculateTotal();
}

$("body").on("keyup",".old_weight",function(){
    var count = $(this).data("count");
    OldSectionTotal(count);
    calculateTotal();
});

$("body").on("change", ".purity_ids", function(){
    var count = $(this).data("count");
    OldSectionTotal(count);
    calculateTotal();
});


</script>
<script>

/*$("body").on("keyup",".price",function(){
  var price =Number($(this).val());
  var stock_qty = $(this).data("stockqty");
  var qty = Number($(this).closest("tr").find(".qty").val());
  $(this).closest("tr").find(".total").val(qty*price);
  grand_total();
  calculateTotal();
});

$("body").on("keyup",".qty",function(){
  var qty =Number($(this).val());
  var stock_qty = $(this).data("stockqty");
  var price = Number($(this).closest("tr").find(".price").val());
  var qty = Number($(this).closest("tr").find(".qty").val());
  $(this).closest("tr").find(".total").val(qty*price);
  grand_total();
  calculateTotal();
});*/
        
    function grand_total(){
        var tot=0;
          $(".total").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_product_amount").text(tot);
          $("#total_amount").val(tot);
          calculate();
    }
    
    
    function grand_old_pro_total(){
        var tot=0;
          $(".old_total").each(function(){
            tot+=Number($(this).val());
          });
          $("#old_product_amount_span").text(tot.toFixed(2));
          $("#old_product_amount").val(tot.toFixed(2));
          calculate();
          calculateTotal();
    }
    
    
    
    
    $(document).on('click', '.remove_product', function() {
        $(this).closest('.porow').remove();
        productCount();
        calculate();
        calculateTotal();
        grand_total();
    });
    

    
        


function productCount(){
    var rowCount = $('#product_tbody tr').length;
    console.log("Product Row Count: "+rowCount);
    $('#total_product_items').text(rowCount);
    $('#payment_total_items').text(rowCount);
    calculate();
}
</script>



<script>
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_sales');
                    $.ajax({
                            url: 'apis/sales.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     alert(response['message']);
                                     /*$('#form_details').trigger("reset");*/
                                     $("#submit").prop('disabled', false);
                                     $('#content-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
    });
});
</script>


<style>

    .search-box{
        width: auto;
        display: inline-block;
        font-size: 14px;
    }
    .search-box {
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        /*position: absolute; */       
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: white;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result div:hover{
        background: blue;
        color: white;
    }
</style>
<script>
function product_selection(id, quantity){ 
    $('.search-box input[type="text"]').val('');
    $(".result").empty();
    $('.search-box input[type="text"]').focus();
    console.log("Product Selection ID: "+id);
    var product_present = 0;
    $('.product_ids').each(function(){
        if(id==$(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
    $.ajax({
        url: 'api/product-search-append-api.php',
        type: 'post',
        dataType: 'text',
        data:{pro_id:id, quantity:quantity },
        success: function (response) {
            console.log('Id   :'+id);
            $(".select22").select2();
            console.log('Response   :'+response);
            $('#product_tbody').append(response);
            $('.search-box input[type="text"]').val('');
            $(".result").empty();
            $('.search-box input[type="text"]').focus();
            productCount();
            grand_total();
            calculate();
            discountType();
        }
    });
    }else{
        alert("Product Already Added");
    }
}

$(document).ready(function(){
    var n='';
    n=parseFloat(n) || 0;

    
    $('.search-box input[type="text"]').on("keyup input", function(){
        console.log('bn Value='+n);
        var search_text = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(search_text.length>1){
            $.get("api/product-search-api.php", {search_text: search_text}).done(function(data){
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
});



</script>
<style>
    thead{
        background: antiquewhite;
    }
    th{
        padding: 5px;
        text-align: center;
    }
    td{
        text-align: center;
    }
</style>
<script>
    function calculate(){
        
      var sub_total=0;
      $sub=0;
      sub_total = $('#total_amount').val();
      
       var sub_charges=0;
      sub_charges=$('#charges').val() || 0;
      

var payable=0;
var tax=$('#tax').val(); //tax peerc
var tax_type=$('#tax_type').val(); //tax type
if(tax==0){
    
  $('#tax_after_total').val(sub_total);
  $('#grand_amount').val(sub_total);
  $('#total_amount').val(sub_total);
  $('#div-cgst').hide();
  $('#div-sgst').hide();
  $("#cgst_tax_amount").html(0);
  $("#sgst_tax_amount").html(0);
  $("#total_tax").html(0);
  
  //New One
  $('#cgst_tax_per_input').val(0);
  $('#total_tax_input').val(0);
  
  $('#cgst_tax_per').text(0);
  $('#sgst_tax_per').text(0);
  $('#gst_tax_per').text(0);
  
  
}else{

if(tax_type==1){ //exclusive

    $('#tax_before_total').text(sub_total);
    $('#tax_after_total').val(parseFloat(sub_total)+(parseFloat(sub_total)*tax/100));
    $('#grand_amount').val(parseFloat(sub_total)+(parseFloat(sub_total)*tax/100));
    
    $('#total_tax').text((sub_total*tax/100));
    $('#total_tax_input').val((sub_total*tax/100));
    payable = sub_total+(sub_total*tax/100);
    $('#gst_tax_per').text(tax);
    $('#sgst_tax_per').text(tax/2);
    $('#cgst_tax_per').text(tax/2);
    $('#cgst_tax_amount').text((sub_total*tax/100)/2);
    $('#cgst_tax_per_input').val(parseFloat((sub_total*tax/100)/2).toFixed(2));
    $('#sgst_tax_amount').text((sub_total*tax/100)/2);
}else{
  
  $('#tax_before_total').text((parseFloat(sub_total)-(parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#tax_after_total').val((parseFloat(sub_total)).toFixed(2));
  $('#grand_amount').val((parseFloat(sub_total)).toFixed(2));
  
  $('#total_tax').text(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#total_tax_input').val(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  payable = sub_total;
  $('#gst_tax_per').text(tax);
  $('#sgst_tax_per').text(tax/2);
  $('#cgst_tax_per').text(tax/2);
  
  
  var tax_sub=((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax)))/2;
  
  $('#cgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
  $('#cgst_tax_per_input').val(parseFloat(tax_sub).toFixed(2));
  $('#sgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
}  
 
  $('#div-cgst').show();
  $('#div-sgst').show();
  
  
  

}

var gst_type=$('#gst_type').val();

console.log('GST Type: '+gst_type);
  if(gst_type==1){
      $('#gst_type_span').text('GST');
       $('#div-cgst').show();
        $('#div-sgst').show();
  }else{
    $('#gst_type_span').text('IGST');
    $('#div-cgst').hide();
    $('#div-sgst').hide();
  }
  

}


function discountType(){
    var discount_type = $("#discount_type").val();
    
    if(discount_type == 1){
        $("#discount_span").text("(%)");
    } else if(discount_type == 2){
        $("#discount_span").text("Amount");
    }
    calculateTotal();
}

function calculateTotal() {
    var totalAmount = parseFloat($("#tax_after_total").val()) || 0;
    var discount = parseFloat($("#discount").val()) || 0;
    var discount_type = $("#discount_type").val();
    var old_product_amount = $("#old_product_amount").val() || 0;
    
    if (discount_type == 1) {
        var discountAmount = (totalAmount * discount / 100);
        totalAmount -= discountAmount+old_product_amount;
        $("#discount_amount_span").text(discountAmount.toFixed(2));
        $("#discount_amount").val(discountAmount);
    } else if (discount_type == 2) {
        totalAmount -= discount+old_product_amount;
        $("#discount_amount_span").text(discount.toFixed(2));
        $("#discount_amount").val(discount);
    }
    
    $("#grand_amount").val(totalAmount.toFixed(2));
}

$(document).ready(function(){
        $('#old_gold_section').hide();
        $('#old_gold_button').click(function(){
            $("#old_gold_section").slideToggle();
            return false;
        });
});
</script>