function initializeIssuedBillsSum(dateFilter, cashRegType, acWayOfSaleTypes) {
    var urlString = window.location.href;
    var url = new URL(urlString);
    var acKey = decodeURI(url.searchParams.get("acKey"));
    var dateISO = decodeURI(url.searchParams.get("date"));

    if (urlString.includes("date")) {
        var date = fromISOToStandardDateFormatString(dateISO);
        $("#Filter_DateFrom").val(date);
    }

    $('.datepicker').datepicker()
        .on('click', function (e) {
            $(this).select();
        }).on('changeDate', function () {
            $(this).datepicker('hide');
            $.fn.dataTable.reload($dataTable);
        });

    $dataTable = $('#mainGrid');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.DateFrom = reformatDate($("#Filter_DateFrom").val());
                d.DateTo = reformatDate($("#Filter_DateTo").val());
                d.DocType = $("#DocType").val() ? $("#DocType").val().join(",") : $("#DocType").val();
                d.WholesaleFilter = dateFilter;
                d.Currency = $("#Currency").val();
                d.cashRegType = cashRegType;
            }
        },
        columns: [
            { data: "", name: "", className: 'dt-body-center dt-head-center', render: $.fn.dataTable.render.previewReportTreeOptions("AcKey", null, null, $("#countryLocale").val() === $("#locRS").val() ? "CanFiscalize" : null) },
            { data: "AcKeyDisplay", name: "AcKey", filtertype: "text", render: $.fn.dataTable.render.link("AcKey") },
            { data: "Protocol", name: "Protocol", filtertype: "text", render: $.fn.dataTable.render.link("AcKey") },
            { data: "Receiver", name: "Receiver", filtertype: "text", render: $.fn.dataTable.render.link("Receiver"), visible: $("#OurCompIsName2").val() === "False" },
            { data: "ReceiverName", name: "ReceiverName", filtertype: "text", render: $.fn.dataTable.render.link("Receiver", false, null, 30), visible: $("#OurCompIsName2").val() === "True" },
            { data: "ThirdPartyId", name: "ThirdPartyId", filtertype: "text", render: $.fn.dataTable.render.link("ThirdPartyId"), visible: $("#OurCompIsName2").val() === "False" },
            { data: "ThirdPartyName", name: "ThirdPartyName", filtertype: "text", render: $.fn.dataTable.render.link("ThirdPartyId", false, null, 30), visible: $("#OurCompIsName2").val() === "True" },
            {
                data: "Price",
                name: "Price",
                filtertype: "number",
                render: function (data, type, full) {
                    return $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val(), '', " " + full.Currency).display(data);
                },
                className: 'dt-body-right'
            },
            {
                data: "PriceWithVAT",
                name: "PriceWithVAT",
                filtertype: "number",
                render: function (data, type, full) {
                    return $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val(), '', " " + full.Currency).display(data);
                },
                className: 'dt-body-right'
            },
            {
                data: "Date", name: "Date", filtertype: "dateTime", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('L');
                    }
                }
            },
            {
                data: "InvoiceDate", name: "InvoiceDate", filtertype: "dateTime", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('L');
                    }
                }
            },
            {
                data: "ExpiryDate", name: "ExpiryDate", filtertype: "dateTime", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('L');
                    }
                }
            },
            { data: "Order", name: "Order", filtertype: "text" },
            { data: "OrderForm", name: "OrderForm", filtertype: "text" },
            {
                data: "Sales", name: "Sales", filtertype: "text", render: function (data, type) {
                    if (type === 'display') {
                        return acWayOfSaleTypes[data] ? acWayOfSaleTypes[data] : "";
                    }

                    return data;
                }
            },
            { data: "StatusDisplay", name: "StatusDisplay", filtertype: "text" }
        ],
        colReorder: {
            fixedColumnsLeft: 1,
        },
        createdRow: function (row, data, dataIndex) {
            if (data.CanceledInvoice) {
                $(row).addClass('danger');
            }

            if (data.AcKey === acKey) {
                $(row).addClass('side-menu-item-active').children(":first").addClass('side-menu-item-active');
            }

            $(row).find(".custom-dropdown").customDropdown();
            setPreviewDropdownToggle($(row).find(".custom-dropdown"), data.AcKey);
        }
    }).on('xhr.dt', function (e, settings, json, xhr) {
        if (json && json.prop) {
            $('#grid-data-total-sum').html($.fn.dataTable.render.number('.', ',', 2).display(json.prop.SumAmount));
            $('#grid-data-vat-sum').html($.fn.dataTable.render.number('.', ',', 2).display(json.prop.SumVat));
            $('#grid-data-vat-total-sum').html($.fn.dataTable.render.number('.', ',', 2).display(json.prop.SumAmountWithVat));
            $('.grid-data-start-items').html(json.recordsTotal === 0 ? '0' : '1');
            $('.grid-data-total-items').html(json.recordsTotal);
        }
    });

    $("select").off("change.tableReload");
    $("select").on("change.tableReload", function () {
        $.fn.dataTable.reload($dataTable);
    });

    var typingTimer;
    var doneTypingInterval = 600;
    $('#Keywords').off('keyup');
    $('#Keywords').on('keyup', function () {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(function () { $.fn.dataTable.reload($dataTable); }, doneTypingInterval);
    });
}

function invoiceSelectionChanged() {
    //TODO: implement when functionallity is defined
}

function openPdfPreview(url) {
    loadPdfPanelContentPAAS($("#pdfPanel"), url, true);
}

function openPrintOptions(formName) {
    window.location = $("#IndexMoveUrl").val() + '?formName=' + formName + '&docType=' + $("#DocType option:selected").val();
}