<style>
    .cdy div {
        width: 31%;
        float: left;
        margin: 10px;
    }
    hr {
        border: 1px #e96125 solid !important;
    }
</style>
<?php 
include("includes/db_config.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_POST['psubmit'])) {
    date_default_timezone_set('Asia/Kolkata');
    $datetime = date('Y-m-d H:i:s');

    // Image upload handling
    $itm_img = '';
    if (!empty($_FILES['item_img']['name'])) {
        $tmp_file = $_FILES['item_img']['tmp_name'];
        $ext = pathinfo($_FILES["item_img"]["name"], PATHINFO_EXTENSION);
        $rand = md5(uniqid() . rand());
        $itm_img = $rand . "." . $ext;
        move_uploaded_file($tmp_file, "images/" . $itm_img);
    }

    // Fetch form data safely
    $payment_type = mysqli_real_escape_string($conn, $_POST['payment_type']);
    $customer_id = mysqli_real_escape_string($conn, $_POST['customer_id']);
    $invoice_no = mysqli_real_escape_string($conn, $_POST['invoice_no']);
    $invoice_date = mysqli_real_escape_string($conn, $_POST['invoice_date']);
    $dispatch_through = mysqli_real_escape_string($conn, $_POST['dispatch_through']);
    $total_amt = mysqli_real_escape_string($conn, $_POST['total_amt']);
    $received_amount = mysqli_real_escape_string($conn, $_POST['received_amount']);
    $payment_by = mysqli_real_escape_string($conn, $_POST['payment_by']);
    $desp = mysqli_real_escape_string($conn, $_POST['desp']);
    $balance = $total_amt - $received_amount;

    // Insert into main sale table
    $sql = "INSERT INTO tbl_ah_items 
        (type, customer_id, bill_no, bill_date,dispatch_through, total_amt, received_amount, balance, payment_by, desp, reference_no, image) 
        VALUES 
        ('Sale', '$customer_id', '$invoice_no', '$invoice_date','$dispatch_through', '$total_amt', '$received_amount', '$balance', '$payment_by', '$desp', '', '$itm_img')";

    if (mysqli_query($conn, $sql)) {
        $last_id = mysqli_insert_id($conn);

        // Insert item details
        if (!empty($_POST["item_name"])) {
            for ($i = 0; $i < count($_POST["item_name"]); $i++) {
                $item_name = mysqli_real_escape_string($conn, $_POST['item_name'][$i]);
                $unit_details = mysqli_real_escape_string($conn, $_POST['unit_details'][$i]);
                $qty = mysqli_real_escape_string($conn, $_POST['qty'][$i]);
                $unit = mysqli_real_escape_string($conn, $_POST['unit'][$i]);
                $unit_amt = mysqli_real_escape_string($conn, $_POST['unit_amt'][$i]);
                $discount = mysqli_real_escape_string($conn, $_POST['discount'][$i]);
                $amount = mysqli_real_escape_string($conn, $_POST['amount'][$i]);
                $tax_rate = mysqli_real_escape_string($conn, $_POST['tax_rate'][$i]);
                $ds_amt = mysqli_real_escape_string($conn, $_POST['ds_amt'][$i]);
                $ttl_amt = mysqli_real_escape_string($conn, $_POST['ttl_amt'][$i]);

                $sql_prd = "INSERT INTO sale_item_details 
                    (sale_id, item_name, unit_details, quantity, unit, price_unit, discount, tax_rate, amount) 
                    VALUES 
                    ('$last_id', '$item_name', '$unit_details', '$qty', '$unit', '$unit_amt', '$discount', '$tax_rate', '$amount')";
                    if (mysqli_query($conn, $sql_prd)) {
                //mysqli_query($conn, $sql_prd) or die(mysqli_error($conn));
                        $check_item = "SELECT id, opening_stock FROM ah_item_tracking_details WHERE item_id = '$item_name'";
                $result = mysqli_query($conn, $check_item);

                if ($result && mysqli_num_rows($result) > 0) {
                    $row = mysqli_fetch_assoc($result);
                    $sqlsl=mysqli_query($conn,"SELECT * FROM `ah_item_details` WHERE id='".$item_name."'");
                    $resl= mysqli_fetch_array($sqlsl);
                    $totalunit = $resl['conversion_rate']  * $qty; 
                    $existing_item_id = $row['id'];
                    $existing_qty = (int) $row['opening_stock'];
                    $updated_qty = $existing_qty - $totalunit; // Add purchased quantity to stock

                    $update_item = "UPDATE ah_item_tracking_details SET at_price_unit = '$unit_amt',opening_stock = '$updated_qty' , item_date = NOW()
                                    WHERE id = '$existing_item_id'";
                    mysqli_query($conn, $update_item) or die(mysqli_error($conn));
                    $addStock = " INSERT INTO stock_transactions (item_id, transaction_type, stock,  item_tracking_id, transaction_date) VALUES ('$item_name', 'sale', $qty, '$existing_item_id', CURDATE())";
                    mysqli_query($conn, $addStock) or die(mysqli_error($conn));
                }
                    }

            }
        }

        echo "<script>location.replace('all-sale-list.php');</script>";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?>
    <style>
        .modal-body .form-label {
            font-weight: 600;
        }

        .viewptdt .form-label {
            font-weight: 600;
        }

        .viewptdt label {
            margin-top: 10px;
        }

        .form-check-input {
            background-color: var(--bs-bg-level-1);
            border: 1px solid #6163f8;
        }

        .form-switch .form-check-input {
            margin-left: -2em;
            margin-right: 5px;
        }

        .tbladdt .form-control {
            background-color: #fff !important;
            padding: 8px 0.2rem !important;
            min-width: 45px !important;
        }

        .customer_records .select2-container {
            margin-top: 0px !important;
        }

        .customer_records .select2-container:nth-child(1) {
            margin-top: 0px !important;
        }

        .remove td:nth-child(1) {
            width: 30px;
        }

        .btn-remove-customer {
            margin-top: -32px;
            position: absolute;
            padding: 5px !important;
            ;
            color: #fff;
            background-color: red !important;
            ;
            border-radius: 2px !important;
        }
    </style>
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">
            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">
                    <form action="" method="post" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-12">
                                <div class="portlet">
                                    <div class="row portlet-header portlet-header-bordered">
                                        <h3 class="portlet-title">Add Sales Details</h3>
                                    </div>
                                </div>
                                <div class="portlet-body">
                                    <?php $sqlfts=mysqli_fetch_array(mysqli_query($conn,"select * from tbl_ah_items where type = 'Sale' order by id DESC"));
                                    if(!empty($sqlfts['bill_no'])){
                                    $in_no= (int)$sqlfts['bill_no'];
                                    $invoice_no= ++$in_no; }else{
                                    $invoice_no='1';
                                    } ?>
                                    <div class="row">
                                        <div class="col-md-8">
                                            <div class="row">
                                                <div class="col-md-2 form-check form-switch">
                                                    &nbsp; &nbsp; <input class="form-check-input" type="checkbox" role="switch" value="credit" id="flexSwitchCheckChecked" checked="checked" name="payment_type"> <label class="form-check-label" for="flexSwitchCheckChecked" style="margin-top:0px;">Credit</label>
                                                </div>

                                                <div class="col-md-2 form-check form-switch">
                                                    <input class="form-check-input" value="cash" type="checkbox" role="switch" id="flexSwitchCheckChecked" name="payment_type"> <label class="form-check-label" for="flexSwitchCheckChecked" style="margin-top:0px;"> Cash</label>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-4 mt-2">
                                                    <select class="form-control" name="customer_id" required>
                                                        <option value="">Select Customer Name</option>
                                                        <?php $sqlct=mysqli_query($conn,"select * from ah_party");
                                                        while($resct= mysqli_fetch_array($sqlct)){ ?>
                                                        <option value="<?= $resct['id'];?>"><?= $resct['party_name'];?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="mb-2">
                                                <label class="form-label">Invoice Number</label>
                                                <input type="text" name="invoice_no" value="<?= $invoice_no;?>" placeholder="Enter Invoice Number" class="form-control">
                                            </div>
                                            <div class="mb-2">
                                                <label class="form-label">Invoice Date</label>
                                                <input type="text" name="invoice_date" value="<?= date("d/m/Y");?>" placeholder="" class="form-control">
                                            </div>
                                        </div>
                                        <div class="col-md-4">
                                            <div class="mb-2">
                                                <label class="form-label">Dispatch Through</label>
                                                <input type="text" name="dispatch_through" value="" placeholder="Enter Dispatch Through" class="form-control">
                                            </div>
                                        </div>
                                    </div>
                                    <hr />
                                    <div class="portlet-body table-responsive">
                                        <table id="datatable" class="table table-bordered table-striped table-hover tbladdt">
                                           <thead>
                                            <tr>
                                                <th style="text-align:center;" rowspan="2">#</th>
                                                <th style="text-align:center;" rowspan="2">Item Name</th>
                                                <th style="text-align:center;" rowspan="2">Unit Detail</th>
                                                 <th style="text-align:center;" rowspan="2">Qty.</th>
                                                <th style="text-align:center;" rowspan="2">Unit</th>
                                                <th style="text-align:center;" rowspan="2">Price/Unit</th>
                                                 <th style="text-align:center;" colspan="2">Discount</th> 
                                                <th style="text-align:center;" colspan="2">Tax</th>
                                                <th style="text-align:center;" rowspan="2">Amount</th>
                                                  </tr>
                                          <tr>
                                                 <th><select class="form-control">
                                                    <option>WithTax</option>
                                                    <option>WithoutTax</option>
                                                </select>
                                                </th> 
                                                <th>%</th>
                                                <th>Amount</th>
                                                 <th>%</th>
                                                 
                                          </tr>  
                                     </thead>
                                            <tbody>
                                            <tr class="customer_records">
                                                <td>1.</td>
                                                <td> <select class="form-control it_id" name="item_name[]" id="item_name">
                                                    <option value="">Select Item Name</option>
                                                     <?php $sqldt=mysqli_query($conn,"SELECT * from ah_item_details");
                                                    while($resdt=mysqli_fetch_array($sqldt)){ ?>
                                                    <option value="<?= $resdt['id'];?>"><?= $resdt['item_name'];?></option>
                                                    <?php } ?>
                                                </select>
                                               </td>
                                                 <td width="180px"> <input type="text" name="unit_details[]" id="unit_details" class="form-control" readonly value="" style="background-color:#e2ecf1!important;border:0px solid #ccc;widhth:180px;font-size:12px;border-bottom:1px solid #ccc"></td>
                                                <td> <input type="text" name="qty[]" id="qty" class="form-control qty" value="1" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td> 
                                               
                                                <td> <select class="form-control" name="unit[]" id="utid" readonly>
                                                   <option value="">Select</option> 
                                                  <?php $sqlpr=mysqli_query($conn,"SELECT * from primery_unit");
                                                    while($respr=mysqli_fetch_array($sqlpr)){ ?>
                                                <option value="<?= $respr['id'];?>"><?= $respr['unit'];?></option><?php } ?>
                                                    </select> </td>
                                                <td><input type="text" name="unit_amt[]" id="unit_amt" class="form-control unit_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                
                                                <td><input type="text" name="discount[]" id="discount" class="form-control discount" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                <td><input type="text" name="amount[]" id="amount" class="form-control" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                <td> <select class="form-control tax_gsts" name="tax_rate[]" id="tax_gst">
                                                    <option value="">Select</option>
                                                    <?php $sql2="SELECT * from tax_rate";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                    <option value="<?= $res2['id'];?>" data-wd="<?= $res2['tax_gst']; ?>"><?= $res2['tax_gst'];?></option>
                                                    <?php } ?>
                                                    </select> </td>
                                                    <td><input type="text" id="ds_amt" name="ds_amt[]" class="form-control ds_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td> 
                                                    <td><input type="text" id="ttl_amt" name="ttl_amt[]" class="form-control ttl_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"> 
                                                    <input type="hidden" id="httl_amt" class="form-control httl_amt" >
                                                    <td>
                                                    </td>
                                            </tr>
                                                  <tr>
                                                       <th colspan="11" style="border: 0px;">
                                                       <div class="customer_records_dynamic" style="width:100%"></div>
                                                       </th>
                                                    </tr>
                                            </tbody>
                                            <tfoot>
                                                <tr>
                                                    <td colspan="2"><span class="btn btn-warning extra-fields-customesr">Add Row</span></td>
                                                    <td colspan="5"><strong>Total:</strong> </td>
                                                    <td class="text-center"> <input type="text" value="" id="" class="form-control" style="border:0px solid #ccc;border-bottom:1px solid #ccc" readonly></td>
                                                    <td colspan="3" class="text-center"><input type="text" id="total_amt" class="form-control total_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc" readonly></td>
                                                </tr>

                                            </tfoot>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="mb-1 col-md-4">
                                <h6 style="margin-top:5px;"><strong>Payment By:</strong></h6>
                                <select class="form-control" name="payment_by">
                                    <option>Cash</option>
                                    <option>Cheque</option>
                                </select>
                            </div>
                            <div class="mb-1 col-md-2"></div>
                            <div class="mb-1 col-md-2">
                                <!--  <div class="form-check"><input class="form-check-input" type="checkbox" value="" id="flexCheckChecked" checked="checked"> <label class="form-check-label" for="flexCheckChecked">Round Off</label></div> -->
                            </div>
                            <div class="mb-1 col-md-4 ttl-cls"><strong>Total:</strong><br /><input type="text" name="total_amt" id="stotal_amts" class="form-control stotal_amts" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></div>

                        </div>
                        <div class="row">
                            <div class="mb-1 col-md-4">
                                <h6 style="margin-top:5px;"><strong>Upload Images:</strong></h6>
                                <input class="form-control" type="file" name="item_img">
                            </div>
                            <div class="mb-1 col-md-4"></div>
                            <div class="mb-1 col-md-4" style="margin-top:5px;"><strong>Recieved:</strong><br /><input type="text" class="form-control" name="received_amount" id="received_amount" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></div>
                        </div>
                        <div class="row">
                            <div class="mb-1 col-md-4">
                                <h6 style="margin-top:5px;"><strong>Add Description:</strong></h6><br />
                                <textarea class="form-control" name="desp" rows="1">  </textarea>
                            </div>
                            <div class="mb-1 col-md-4"></div>

                            <div class="mb-1 col-md-4" style="margin-top:5px;"><strong>Balance:</strong> <br />
                                <input type="text" id="total_amts" name="total_amts" class="form-control total_amts" style="border:0px solid #ccc; background-color:transparent;" readonly>
                            </div>
                        </div>
                        <div class="row">

                            <div class="mb-1 col-md-9"></div>
                            <div class="mb-1 mt-2 col-md-3">
                                <button class="btn btn-primary btn-lg mb-3" name="psubmit" type="submit"> Save</button>
                            </div>
                        </div>
                    </form>
                </div>

                <?php include("includes/footer.php");?>
            </div>
        </div>
        <?php include("includes/js.php");?>
        <script type="text/javascript">
            $(document).ready(function() {
                var i = 1; // Start row counter

    // Add new row dynamically
    $(document).on('click', '.extra-fields-customesr', function () {
        i++;
        var newRow = `
            <tr id="row${i}" class="customer_records" >
                <td class="row-count" style="padding: .75rem .75rem;">${i}.</td> 
                <td style="padding: .75rem .75rem;">
                    <select class="form-control it_id" name="item_name[]" id="item_name">
                        <option value="">Select Item Name</option>
                         <?php $sqldt=mysqli_query($conn,"SELECT * from ah_item_details");
                        while($resdt=mysqli_fetch_array($sqldt)){ ?>
                        <option value="<?= $resdt['id'];?>"><?= $resdt['item_name'];?></option>
                        <?php } ?>
                    </select>
                </td>
                <td width="170px" style="padding: .75rem .75rem;"> <input type="text" name="unit_details[]" id="unit_details" class="form-control" readonly value="" style="background-color:#e2ecf1!important;border:0px solid #ccc;widhth:180px;font-size:12px;border-bottom:1px solid #ccc"></td>
                <td style="padding: .75rem .75rem;" width="140px"> <input type="text" name="qty[]" id="qty" class="form-control qty" value="1" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td> 
                                               
                                                <td style="padding: .75rem .75rem;" width="90px"> <select class="form-control" name="unit[]" id="utid" readonly>
                                                   <option value="">Select</option> 
                                                  <?php $sqlpr=mysqli_query($conn,"SELECT * from primery_unit");
                                                    while($respr=mysqli_fetch_array($sqlpr)){ ?>
                                                <option value="<?= $respr['id'];?>"><?= $respr['unit'];?></option><?php } ?>
                                                    </select> </td>
                                                <td width="150px" style="padding: .75rem .75rem;"><input type="text" name="unit_amt[]" id="unit_amt" class="form-control unit_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                
                                                <td style="padding: .75rem .75rem;" width="140px"> <input type="text" name="discount[]" id="discount" class="form-control discount" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                <td style="padding: .75rem .75rem;" width="140px"><input type="text" name="amount[]" id="amount" class="form-control" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td>
                                                <td style="padding: .75rem .75rem;" width="90px"> <select class="form-control tax_gsts" name="tax_rate[]" id="tax_gst">
                                                    <option value="">Select</option>
                                                    <?php $sql2="SELECT * from tax_rate";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                    <option value="<?= $res2['id'];?>" data-wd="<?= $res2['tax_gst']; ?>"><?= $res2['tax_gst'];?></option>
                                                    <?php } ?>
                                                    </select> </td>
                                                    <td style="padding: .75rem .75rem;" width="140px"><input type="text" id="ds_amt" name="ds_amt[]" class="form-control ds_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"></td> 
                                                    <td style="padding: .75rem .75rem;" width="140px"><input type="text" id="ttl_amt" name="ttl_amt[]" class="form-control ttl_amt" style="border:0px solid #ccc;border-bottom:1px solid #ccc"> 
                                                    <input type="hidden" id="httl_amt" class="form-control httl_amt" >
                                                    <td style="padding: .75rem .75rem;">
                    <button type="button" id="${i}" class="btn btn-danger btn_remove">X</button>
                </td>
            </tr>`;

        $('.customer_records_dynamic').append(newRow);
    });

    // Remove row dynamically
    $(document).on('click', '.btn_remove', function () {
        var button_id = $(this).attr("id");
        $('#row' + button_id).remove();
        updateRowNumbers();
        updateTotal();
    });
                $('#submit').click(function() {
                    $.ajax({
                        url: "add-product.php",
                        method: "POST",
                        data: $('#add_name').serialize(),
                        success: function(data) {
                            $('#add_name')[0].reset();
                        }
                    });
                });
            });
            $(document).ready(function() {
                $('#addsl').on('click', function() {
                    var action = "item";
                    $.ajax({
                        type: 'POST',
                        url: 'ajax_get_item.php',
                        data: 'action=' + action,
                        success: function(html) {
                            $('.itemss').html(html);
                            console.log(html);
                            // $('#city').html('<option value="">Select Division</option>'); 
                        }
                    });
                    // }
                });
            });
            $(document).ready(function() {
                $('#addsl').on('click', function() {
                    var action = "addtm";
                    $.ajax({
                        type: 'POST',
                        url: 'ajax_get_tax.php',
                        data: 'action=' + action,
                        success: function(html) {
                            $('.tgst').html(html);
                            console.log(html);
                            // $('#city').html('<option value="">Select Division</option>'); 
                        }
                    });
                    // }
                });
            });
            $(document).ready(function() {
                $('#addsl').on('click', function() {
                    var action = "unit";
                    $.ajax({
                        type: 'POST',
                        url: 'ajax_get_tax.php',
                        data: 'action=' + action,
                        success: function(html) {
                            $('.utidd').html(html);
                            console.log(html);
                            // $('#city').html('<option value="">Select Division</option>'); 
                        }
                    });
                    // }
                });
            });
            $(document).ready(function() {
                $('#addsl').on('click', function() {
                    var action = "units";
                    $.ajax({
                        type: 'POST',
                        url: 'ajax_get_tax.php',
                        data: 'action=' + action,
                        success: function(html) {
                            $('.utidds').html(html);
                            console.log(html);
                            // $('#city').html('<option value="">Select Division</option>'); 
                        }
                    });
                    // }
                });
            });
            (function() {
                "use strict";
                $("table").on("keyup", ".discount,.qty", function() {
                    var totals = 0;
                    var row = $(this).closest("tr");
                    var price = parseFloat(row.find("#unit_amt").val());
                    var qty = parseFloat(row.find("#qty").val());
                    var discount = parseFloat(row.find("#discount").val());
                    var total = price * qty;
                    if (!isNaN(discount)) {
                        var disamt = (total * (discount / 100));
                        var rtotal = total - disamt;
                    } else {
                        var rtotal = total;
                    }
                    // row.find("#ttl_amt").val(isNaN(total) ? "" : total);
                    row.find("#ttl_amt").val(isNaN(rtotal) ? "" : rtotal);
                    row.find("#httl_amt").val(isNaN(rtotal) ? "" : rtotal);
                    row.find("#amount").val(isNaN(disamt) ? "" : disamt);
                });
            })();
            (function() {
                $('body').on('keyup', '#received_amount', function() {
                    var ramt = $(this).val();
                    var $container = $(this).closest(".container-fluid");
                    var prc = $container.find("#stotal_amts").val();
                    var total = prc - ramt;
                    $("#total_amts").val(isNaN(total) ? "" : total);
                });
            })();
            $(document).on("keyup", ".discount,.qty", function() {
                var sum = 0;
                $(".ttl_amt").each(function() {
                    sum += +$(this).val();
                });
                $("#total_amt").val(sum);
                $("#total_amts").val(sum);
                $("#stotal_amts").val(sum);
            });

            function someFunction() {
                var sum = 0;
                $(".ttl_amt").each(function() {
                    sum += +$(this).val();
                });
                $("#total_amt").val(sum);
                $("#total_amts").val(sum);
                $("#stotal_amts").val(sum);
            }
            (function() {
                "use strict";
                $("table").on("change", ".it_id", function() {
                    var row = $(this).closest("tr");
                    var it_id = $(this).val();
                    $.ajax({
                        type: 'post',
                        url: 'get_unit_price.php',
                        data: {
                            it_id: it_id
                        },
                        dataType: 'json',
                        success: function(data) {
                           // var res=jQuery.parseJSON(data);
                            if(data.status == "success"){
                                row.find('#unit_details').val(data.items.unit_details);
                                row.find('#unit_amt').val(data.items.sale_price);
                                row.find('#tax_gst').val(data.items.tax_rate);
                                row.find('#utid').val(data.items.punit);
                                row.find('#discount').val(data.items.disc_sale);
                                row.find('#ttl_amt').val(data.items.total_amt);
                                row.find('#httl_amt').val(data.items.total_amt);
                                row.find('#ds_amt').val(data.items.gst_amt);
                            }
                            else if (data.status == "error") {
                                Swal.fire('Error!', data.msg, 'error');
                            }
                            console.log(data);
                            
                            //$('#container').container('show'); 
                            someFunction();
                        },
                        error: function() {

                        }
                    });
                });
            })();
            (function() {
                "use strict";
                $("table").on("keyup", ".qty", function() {
                    var row = $(this).closest("tr");
                    var qty = $(this).val();
                    var it_id = parseFloat(row.find(".it_id").val());
                    $.ajax({
                        type: 'post',
                        url: 'get_qty.php',
                        data: {
                            it_id: it_id,
                            qty: qty
                        },
                        dataType: 'json',
                        success: function(data) {
                            console.log(data);
                            //someFunction();
                        },
                        error: function() {}
                    });
                });
            })();

            (function() {
                "use strict";
                $("table").on("change", ".tax_gsts", function() {
                    var row = $(this).closest("tr");
                    var gst_id = $(this).val();
                    var price = parseFloat(row.find(".httl_amt").val());
                    $.ajax({
                        type: 'post',
                        url: 'get_gst_price.php',
                        data: {
                            gst_id: gst_id,
                            price: price
                        },
                        dataType: 'json',
                        success: function(data) {
                            console.log(data);
                            row.find('#ttl_amt').val(data.total);
                            row.find('#ds_amt').val(data.taxs);
                            // row.find('#amount').val(data.taxs); 
                            //$('#container').container('show'); 
                            someFunction();
                        },
                        error: function() {}
                    });
                });
            })();
        </script>
</body>

</html>