<div class="modal inmodal fade" id="identSerialNoModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-ident-serial-no" role="document">
        <div class="modal-content partialIdentSerialNoModalContent">
            <div class="modal-body partialIdentSerialNoModalBody p-sm">
                @Html.Hidden("IdentSerialNoType")
                @Html.Hidden("IdentAnNo")
                @Html.Hidden("InvoiceType")
                @Html.Hidden("IdentSerialDateDue")
                @Html.Hidden("IdentSerialInvoiceDate")
                <div class="row border-bottom">
                    <div class="col-xs-10">
                        <h3>@Loc.EnterNumbers</h3>
                    </div>
                    <div class="col-xs-2 text-right">
                        <i class="fa fa-times clickable-icon" data-dismiss="modal" title="@Loc.Close"></i>
                    </div>
                </div>
                <div class="row document-head">
                    <div class="col-xs-3 p-r-0">
                        <div class="form-group cancel-invoice-typeahead-container">
                            <label class="control-label">@Loc.Ident</label>
                            <input class="form-control" id="identSerialNoIdent" autocomplete="off" />
                        </div>
                    </div>
                    <div class="col-xs-2">
                        <div class="form-group">
                            <label class="control-label">@Loc.Quantity</label>
                            <input class="form-control" id="identSerialNoQuantity" />
                        </div>
                    </div>
                    <div class="col-xs-7 text-right">
                        <div class="form-group m-t-sm">
                            <button type="button" class="btn btn-primary" onclick="deleteAll()">@Loc.DeleteAll</button>
                        </div>
                    </div>
                </div>
                <div id="multiplySerialsContainer" class="row hidden">
                    <div class="col-xs-6">
                        <table id="identSerialMultiplyTable" class="table table-bordered ident-search m-b-xs" role="grid">
                            <thead>
                                <tr role="row">
                                    <th style="width: 40%">@Loc.Prefix</th>
                                    <th style="width: 35%">@Loc.SerialNumber</th>
                                    <th class="InputField-Quantity" style="width: 25%">@Loc.Quantity</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr role="row">
                                    <td>
                                        <input type="text" class="form-control custom-dirty-checker-ignore" id="identSerialNoPrefix" autocomplete="off" />
                                    </td>
                                    <td>
                                        <input type="text" class="form-control input-border-left custom-dirty-checker-ignore" step="0" id="identSerialNoSerial" autocomplete="off" />
                                    </td>
                                    <td class="InputField-Quantity">
                                        <input type="text" class="form-control numberInput input-border-left custom-dirty-checker-ignore" step="0" id="identSerialNoQty" autocomplete="off" />
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-success createNewButton" id="multiplySerialsBtn" onclick="onMultiplySerialNumber()" title="@Loc.Multiply">
                                            @Loc.Multiply
                                        </button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <div class="project-list">
                            <table id="identSerialNoGrid" class="table table-bordered setwh-table">
                                <thead>
                                    <tr>
                                        <th width="60%" class="serialNumberCol">@Loc.SerialNumberGrid</th>
                                        <th width="20%" class="dt-head-right qty-col">@Loc.Quantity</th>
                                        <th width="20%" class="dateDue-col">@Loc.StockDateDue</th>
                                        <th style="width: 34px"></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr id="tr-serial-0" class="sortable-row">
                                        <td class="no-padding" label="@Loc.SerialNumber">
                                            <input id="input-serial-no"
                                                   maxlength="4000"
                                                   type="text"
                                                   class="form-control border-bottom-none"
                                                   autocomplete="off"
                                                   placeholder="@Loc.SerialNumber" />
                                        </td>
                                        <td class="no-padding qty-col" label="@Loc.Quantity">
                                            <input id="input-serial-quantity"
                                                   type="text"
                                                   class="form-control numberInput border-bottom-none"
                                                   step="0"
                                                   value="1"
                                                   autocomplete="off"
                                                   placeholder="@Loc.Quantity" />
                                        </td>
                                        <td class="no-padding dateDue-col" label="@Loc.StockDateDue">
                                            <input id="input-serial-dateDue"
                                                   type="text"
                                                   class="form-control datepicker border-bottom-none"
                                                   autocomplete="off"
                                                   placeholder="@Loc.StockDateDue" />
                                        </td>
                                        <td class="td-action">
                                            <p class="btn btn-success" id="saveSerialNoBtn" style="width:38px" onclick="saveSerialNo()">
                                                <span class="far fa-plus cursor-pointer" aria-hidden="true"></span>
                                            </p>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer m-t-xs">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-check m-r-xs"></i>@Loc.Confirm</button>
            </div>
        </div>
    </div>
</div>

<script>
    var $identSerialNoDataTable;

    $(document).ready(function () {
        $('#identSerialNoModal .modal-dialog').draggable();

        $('.partialIdentSerialNoModalContent').resizable({
            alsoResize: ".partialIdentSerialNoModalBody",
            minHeight: 400
        });

        $('#identSerialNoModal').on('show.bs.modal', function () {
            $(this).find('.partialIdentSerialNoModalBody').css({
                'max-height': '100%',
                'min-height': '50%',
                'min-width': '100%'
            });
        });

        $(".partialIdentSerialNoModalContent").resize(function () {
            $(".partialIdentSerialNoModalBody").css("height", $(".partialIdentSerialNoModalContent").height() - 80);
            jsRenderDataTable("identSerialNoGrid", "partialIdentSerialNoModalBody");
        });

        displayModalByType();

        $('#identSerialNoModal').on('hidden.bs.modal', async function (e) {
            if ($("#FiscalStatus").val() > 1 || $("#IsCanceled").val() === "True")
                return;

            $("#input-serial-no").val("");
            $("#input-serial-quantity").val("");
            $("#input-serial-dateDue").val("");

            if ($("#IdentSerialNoType").val() === "P") {
                await deleteUnusedItemSerials();
            }

            var anNo = toNumber($("#IdentAnNo").val());
            var serialNoQtySum = getSerialNoQtySum();

            $("#quantity-" + anNo).val($("#quantity-" + anNo).val() > 0 ? toNumber(serialNoQtySum) : toNumber(serialNoQtySum) * -1);
            formatNumberInput($("#quantity-" + anNo));

            if (typeof serialNoIsPOS === 'undefined' || !serialNoIsPOS) {
                changeIdentData(anNo);
                save();
            } else {
                recalculateItem(anNo);
                onlySave($("#saveButton"));
            }
        });
    });

    function checkIdentSerialNo(anNo, type, serialNo = null, quantity = null, open = null) {
        serialNo = serialNo ? serialNo : $("#serialNo-" + anNo).val();
        quantity = quantity ? toNumber(quantity) : toNumber($("#quantity-" + anNo).val());

        if ($("#DocTypeSelect option:selected").attr("data-acEnterSn" + serialNo) === "F")
            return;

        if ((serialNo !== "N" && (quantity > 0 || $("#IsCanceled").val() === "True")) || open == "true") {
            $("#IdentSerialNoType").val(serialNo);
            $("#InvoiceType").val(type);
            $("#IdentSerialDateDue").val($("#dateDue-" + anNo).val());
            if (type === "M")
                $("#IdentSerialInvoiceDate").val($("#TakeoverDate").val());
            else
                $("#IdentSerialInvoiceDate").val($("#Date").val());
            openIdentSerialNoModal(toNumber(anNo));
        }
    }

    function displayModalByType() {
        if ($("#IdentSerialNoType").val() === "O" || $("#IdentSerialNoType").val() === "P") {
            $("#multiplySerialsContainer").addClass("hidden");
        } else {
            $("#multiplySerialsContainer").removeClass("hidden");
        }

        if ($("#IdentSerialNoType").val() === "S" || $("#IdentSerialNoType").val() === "E") {
            if ($("#IdentSerialDateDue").val() === "true") {
                $(".serialNumberCol").attr('width', "80%");
                $(".dateDue-col").show();
            } else {
                $(".serialNumberCol").attr('width', "96%");
                $(".dateDue-col").hide();
            }
            $(".qty-col").hide();
        } else {
            if ($("#IdentSerialDateDue").val() === "true") {
                $(".serialNumberCol").attr('width', "60%");
                $(".dateDue-col").show();
            } else {
                $(".serialNumberCol").attr('width', "80%");
                $(".dateDue-col").hide();
            }
            $(".qty-col").show();
        }

        if ($("#IdentSerialNoType").val() === "P") {
            $("#input-serial-no").attr("disabled", true);
            $("#input-serial-quantity").attr("disabled", true);
            $("#saveSerialNoBtn").attr("disabled", true);
        }
    }

    async function openIdentSerialNoModal(anNo) {
        displayModalByType();
        $("#IdentAnNo").val(anNo);
        $("#identSerialNoIdent").val($("#id-" + anNo).val());
        $("#identSerialNoQuantity").val(Math.abs(toNumber($("#quantity-" + anNo).val())));
        formatNumberInput($("#identSerialNoQuantity"));
        $("#identSerialNoPrefix").val("");
        $("#identSerialNoSerial").val(0);
        $("#identSerialNoQty").val(0);

        $("#input-serial-serial-no").val("");
        $("#input-serial-quantity").val(1);
        $("#input-serial-dateDue").datepicker("setDate", $("#IdentSerialInvoiceDate").datepicker("getDate"));

        if ($("#IdentSerialNoType").val() === "P")
            await moveSerialAddSales(anNo);
        else
            await getSerialNumbersForIdent(anNo);

        if (($("#FiscalStatus").val() > 1 || $("#IsCanceled").val() === "True") && $("#InvoiceType").val() === "M") {
            $("#identSerialNoModal :input").prop("disabled", true);
            $("#identSerialNoModal :input").closest("div:not(.form-group)").addClass("document-head-disabled-input");
            $("#identSerialNoModal :button").prop("disabled", true);
            $("#saveSerialNoBtn").attr("disabled", true);
        }

        $("#identSerialNoModal").modal("show");
    }

    async function moveSerialAddSales(anNo) {
        var data = {
            AcKey: $("#AcKey").val(),
            AnNo: $("#IdentAnNo").val(),
            Ident: $("#identSerialNoIdent").val(),
            Type: "K",
            Column: "",
        };

        await fetch('@Url.Action("MoveSerialNoAddSales", "SerialNumbers")', {
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        }).then((response) => response.json()
        ).then(async (response) => {
            getSerialNumbersForIdent(anNo);
        });
    }

    async function getSerialNumbersForIdent(anNo) {
        await fetch('@Url.Action("GetByAcKeyAndAnNo", "SerialNumbers")' + "?acKey=" + $("#AcKey").val() + "&anNo=" + anNo, {
            method: "GET",
            headers: { 'Content-Type': 'application/json' }
        }).then((response) => response.json()
        ).then(async (data) => {
            $('#identSerialNoGrid tbody tr:not(:first)').remove();
            if (data && data.length) {
                for (var i = 0; i < data.length; i++) {
                    addSerialNumberRow(data[i].SerialNo, data[i].Qty, data[i].DateDue);
                }
            }
        });
    }

    function addSerialNumberRow(serialNo, qty, dateDue) {
        var id = serialNo.replace(/ /g, '');
        var isQtyHidden = $("#IdentSerialNoType").val() === "S" || $("#IdentSerialNoType").val() === "E" ? true : false;
        var isDateDueHidden = $("#IdentSerialDateDue").val() === "true" ? false : true;
        var isDisabled = $("#FiscalStatus").val() > 1 ? true : false;

        $('#identSerialNoGrid > tbody:last-child').append(
            '<tr id="tr-serial-' + id + '" class="sortable-row">' +
            '<td class="serial-numbers-td"><input class="form-control border-bottom-none serialNoCode" id="serial-no-id-' + id + '" value="' + serialNo + '" disabled /></td>' +
            '<td class="serial-numbers-td qty-col ' + (isQtyHidden ? "hidden" : "") + '"><input class="form-control border-bottom-none serialNoQty" id="serialNoQty-' + id + '" value="' + qty + '" onchange="saveSerialNo(\'' + serialNo + '\')" />' +
            '<input id="savedSerialNoQty-' + id + '" value="' + qty + '" class="savedSerialNoQty" hidden/></td>' +
            '<td class="serial-numbers-td dateDue-col ' + (isDateDueHidden ? "hidden" : "") + '"><input class="form-control border-bottom-none datepicker serialNoDateDue" id="serialNoDateDue-' + id + '" value="' + dateDue + '" onchange="saveSerialNo(\'' + serialNo + '\')" />' +
            '<input id="savedSerialNoDateDue-' + id + '" value="' + dateDue + '" hidden/></td>' +
            '<td class="td-action"><button type="button" onclick="deleteSerialNo(\'' + serialNo + '\')" class="btn btn-link editableInput" ' + (isDisabled ? "disabled" : "") + '><span class="far fa-trash-alt"></span></button></td>' +
            '</tr>'
        );

        $('#serialNoDateDue-' + id).datepicker()
            .on('click', function (e) {
                $(this).select();
            }).on('changeDate', function () {
                $(this).datepicker('hide');
            });
    }

    async function saveSerialNo(serialNo = null) {
        var $serialCodes = $(".serialNoCode");
        if ($serialCodes && $serialCodes.length) {
            for (var i = 0; i < $serialCodes.length; i++) {
                if ($serialCodes[i].value === $("#input-serial-no").val()) {
                    $().customModal({
                        destroyOnClose: true,
                        showCloseButton: true,
                        type: "error",
                        text: "@Loc.SerialNumberValueAlreadyExist",
                        buttons: [
                            {
                                icon: 'fa fa-check',
                                text: $("#ConfirmLoc").val(),
                                closeOnAction: true,
                                type: 'primary'
                            }
                        ]
                    });

                    return;
                }
            }
        }

        $("#saveSerialNoBtn").find("i").addClass("fa-spinner fa-spin");
        $("#saveSerialNoBtn").prop("disabled", true);

        var serialNoQtySum = getSerialNoQtySum();
        if (!serialNo)
            serialNoQtySum = serialNoQtySum + toNumber($("#input-serial-quantity").val());

        serialNo = serialNo ? serialNo : $("#input-serial-no").val();
        var id = serialNo.replace(/ /g, '');
        serialNoDateDue = $("#serialNoDateDue-" + id).val() ? $("#serialNoDateDue-" + id).datepicker("getDate") : $("#input-serial-dateDue").datepicker("getDate");

        var data = {
            AcKey: $("#AcKey").val(),
            AnNo: $("#IdentAnNo").val(),
            Receiver: $("#ReceiverId").val(),
            Ident: $("#identSerialNoIdent").val(),
            SerialNo: serialNo,
            Quantity: serialNoQtySum,
            QuantityOld: $("#savedSerialNoQty-" + id).val() ? $("#savedSerialNoQty-" + id).val() : 0,
            QuantityNew: $("#serialNoQty-" + id).val() ? $("#serialNoQty-" + id).val() : $("#input-serial-quantity").val(),
            DateDueNew: $("#IdentSerialDateDue").val() === "true" ? serialNoDateDue : null,
            DateDue: $("#IdentSerialDateDue").val() === "true" && $("#savedSerialNoDateDue-" + id).val() ? $("#savedSerialNoDateDue-" + id).val() : null,
            Type: "K",
            SNWOType: "",
            Package: "",
            PackQty: 0,
            InvoiceType: $("#InvoiceType").val(),
            SNType: $("#IdentSerialNoType").val()
        };

        var currDate = (new Date()).toISOString().split('T')[0];
        if ($("#IdentSerialDateDue").val() === "true" && new Date(moment(serialNoDateDue).format('YYYY-MM-DD')) < new Date(currDate)) {
            $().customModal({
                destroyOnClose: true,
                showCloseButton: true,
                type: "success",
                text: "@Loc.DateDueExpiredForSerialNumber " + serialNo,
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: $("#ConfirmLoc").val(),
                        action: async function () {
                            checkSerialNumberQtySum(serialNoQtySum);
                        },
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
        } else
            checkSerialNumberQtySum(serialNoQtySum);

        await fetch('@Url.Action("MoveUpdateSerialNo", "SerialNumbers")', {
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        }).then((response) => response.json()
        ).then(async (response) => {
            $("#saveSerialNoBtn").find("i").removeClass("fa-spinner fa-spin");
            $("#saveSerialNoBtn").prop("disabled", false);

            if (!response || response === false) {
                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: true,
                    type: "alert",
                    text: "@Loc.ErrorSaving",
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#ConfirmLoc").val(),
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });
            } else {
                if ($("#input-serial-no").val()) {
                    addSerialNumberRow($("#input-serial-no").val(), $("#input-serial-quantity").val(), $("#input-serial-dateDue").val());
                    $("#input-serial-no").val("");
                    $("#input-serial-dateDue").datepicker("setDate", $("#IdentSerialInvoiceDate").val());
                    $("#input-serial-quantity").val(1);
                } else {
                    $("#savedSerialNoQty-" + id).val(data.QuantityNew);
                    $("#savedSerialNoDateDue-" + id).val(new Date($("#serialNoDateDue-" + id).datepicker("getDate")).toISOString());
                }
            }
        });
    }

    function checkSerialNumberQtySum(serialNoQtySum) {
        if (toNumber(serialNoQtySum) === Math.abs(toNumber($("#identSerialNoQuantity").val()))) {
            $().customModal({
                destroyOnClose: true,
                showCloseButton: true,
                type: "success",
                text: "@Loc.SerialNumberQuantityIsReached",
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: $("#ConfirmLoc").val(),
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
        }
    }

    function onMultiplySerialNumber() {
        if ((toNumber($("#identSerialNoQty").val()) > toNumber($("#identSerialNoQuantity").val())) || ($('#identSerialNoGrid > tbody > tr').length > toNumber($("#identSerialNoQuantity").val()))) {
            $().customModal({
                title: "",
                destroyOnClose: true,
                showCloseButton: true,
                type: "question",
                text: "@Loc.NumberOfSerialNumbersIsGreaterThanOnDocument",
                buttons: [
                    {
                        icon: 'fa fa-times',
                        text: '@Loc.Cancel',
                        action: async function () {
                            $("#identSerialNoQty").focus();
                        },
                        closeOnAction: true,
                        type: 'ghost'
                    },
                    {
                        icon: 'fa fa-check',
                        text: '@Loc.Confirm',
                        action: async function () {
                            multiplySerialNumbers();
                        },
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
        } else
            multiplySerialNumbers();
    }

    async function multiplySerialNumbers() {
        if ($("#identSerialNoQty").val() > 0) {
            $("#multiplySerialsBtn").find("i").addClass("fa-spinner fa-spin");
            $("#multiplySerialsBtn").prop("disabled", true);

            var serialNoQtySum = getSerialNoQtySum();
            var serial = $("#identSerialNoSerial").val();
            var serialLength = serial.length;

            for (var i = 0; i < $("#identSerialNoQty").val(); i++) {
                var data = {
                    AcKey: $("#AcKey").val(),
                    AnNo: $("#IdentAnNo").val(),
                    Receiver: $("#ReceiverId").val(),
                    Ident: $("#identSerialNoIdent").val(),
                    SerialNo: $("#identSerialNoPrefix").val() + serial,
                    Quantity: serialNoQtySum,
                    QuantityOld: 0,
                    QuantityNew: $("#IdentSerialNoType").val() === "S" || $("#IdentSerialNoType").val() === "E" ? 1 : 0,
                    Type: "K",
                    SNWOType: "",
                    Package: "",
                    PackQty: 0,
                    InvoiceType: $("#InvoiceType").val(),
                    DateDue: $("#IdentSerialDateDue").val() === "true" ? $("#IdentSerialInvoiceDate").val() : null,
                    SNType: $("#IdentSerialNoType").val()
                };

                await fetch('@Url.Action("SerialNoUpd", "SerialNumbers")', {
                    method: "POST",
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                }).then((response) => response.json()
                ).then(async (response) => {
                    if (!response || response === false) {
                        $().customModal({
                            destroyOnClose: true,
                            showCloseButton: true,
                            type: "alert",
                            text: "@Loc.ErrorSaving",
                            buttons: [
                                {
                                    icon: 'fa fa-check',
                                    text: $("#ConfirmLoc").val(),
                                    closeOnAction: true,
                                    type: 'primary'
                                }
                            ]
                        });
                    } else {
                        addSerialNumberRow($("#identSerialNoPrefix").val() + serial, data.QuantityNew, data.DateDue);
                    }
                });

                serial = pad(toNumber(serial) + 1, serialLength);
            }

            $("#multiplySerialsBtn").find("i").removeClass("fa-spinner fa-spin");
            $("#multiplySerialsBtn").prop("disabled", false);
        }
    }

    async function deleteSerialNo(serialNo) {
        var serialNoQtySum = getSerialNoQtySum();
        var id = serialNo.replace(/ /g, '');
        serialNoDateDue = $("#serialNoDateDue-" + id).val() ? $("#serialNoDateDue-" + id).datepicker("getDate") : $("#input-serial-dateDue").datepicker("getDate");

        var data = {
            AcKey: $("#AcKey").val(),
            AnNo: $("#IdentAnNo").val(),
            Issuer: $("#ReceiverId").val(),
            Ident: $("#identSerialNoIdent").val(),
            SerialNo: serialNo,
            Quantity: serialNoQtySum,
            QuantityOld: 0,
            QuantityNew: $("#serialNoQty-" + id).val(),
            DateDueNew: $("#IdentSerialDateDue").val() === "true" ? serialNoDateDue : null,
            DateDue: $("#IdentSerialDateDue").val() === "true" ? serialNoDateDue : null,
            Type: "K",
            SNWOType: "",
            Package: "",
            PackQty: 0,
            InvoiceType: $("#InvoiceType").val(),
            SNType: $("#IdentSerialNoType").val()
        };

        await fetch('@Url.Action("DeleteByAcKeyAndSerialNo", "SerialNumbers")', {
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        }).then((response) => response.json()
        ).then(async (response) => {
            if (response && response === true) {
                if ($("#IdentSerialNoType").val() === "P") {
                    $("#serialNoQty-" + serialNo).val(0);
                    $("#savedSerialNoQty-" + serialNo).val(0);
                } else {
                    $("#tr-serial-" + serialNo).remove();
                }

                var serialNoQtySum = getSerialNoQtySum();
                checkSerialNumberQtySum(serialNoQtySum);
            }
        });
    }

    async function deleteAll() {
        var data = {
            AcKey: $("#AcKey").val(),
            AnNo: $("#IdentAnNo").val(),
            Issuer: $("#ReceiverId").val(),
            Ident: $("#identSerialNoIdent").val(),
            InvoiceType: $("#InvoiceType").val(),
            SNType: $("#IdentSerialNoType").val()
        };

        await fetch('@Url.Action("DeleteAllByAcKey", "SerialNumbers")', {
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        }).then((response) => response.json()
        ).then((data) => {
            if (data && data === true) {
                if ($("#IdentSerialNoType").val() === "P") {
                    $(".serialNoQty").val(0);
                    $(".savedSerialNoQty").val(0);
                } else {
                    $('#identSerialNoGrid tbody tr:not(:first)').remove();
                }
            }
        });
    }

    async function deleteUnusedItemSerials() {
        await fetch('@Url.Action("DeleteUnusedItemSerials", "SerialNumbers")' + "?acKey=" + $("#AcKey").val() + "&anNo=" + $("#IdentAnNo").val() + "&type=" + "K" + "&invoiceType=" + $("#InvoiceType").val(), {
            method: "GET",
            headers: { 'Content-Type': 'application/json' }
        }).then((response) => response.json()
        ).then(async (data) => {
            if (data && data === true) {
                var items = $(".serialNoQty");
                for (var i = 0; i < items.length; i++) {
                    if (items[i].value === 0) {
                        items[i].closest("tr").remove();
                    }
                }
            }
        });
    }

    function getSerialNoQtySum() {
        var serialNoQtySum = 0;
        var $qtyElements = $(".serialNoQty");
        if ($qtyElements && $qtyElements.length) {
            for (var i = 0; i < $qtyElements.length; i++) {
                serialNoQtySum = serialNoQtySum + toNumber($qtyElements[i].value);
            }
        }
        return toNumber(serialNoQtySum);
    }
</script>