function initializeReportsIssuedBillsDetailed() {
    $("#downloadIssuedBills").show();
    $("#confirmBillsDropdown").addClass('hidden');
    $(".dateFilter").removeClass('hide');

    var urlString = window.location.href;
    var url = new URL(urlString);
    var acKey = decodeURI(url.searchParams.get("acKey"));
    var typingTimer;
    var doneTypingInterval = 600;
    $('#Keywords').off('keyup');
    $('#Keywords').on('keyup', function () {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(function () { $.fn.dataTable.reload($dataTable); }, doneTypingInterval);
    });

    var getSearchData = function () {
        return {
            DateFrom: reformatDate($("#Filter_DateFrom").val()),
            DateTo: reformatDate($("#Filter_DateTo").val()),
            SubjectId: $("#Filter_SubjectId").val(),
            DocType: $("#Filter_DocType").val(),
            ProductId: $("#Filter_ProductId").val(),
            IsWholesale: $("#Filter_IsWholesale").val(),
            Author: $("#Filter_Author").val()
        };
    };

    var select2Product = $("#Filter_ProductId").select2({
        ajax: {
            url: $("#Filter_ProductId").data("url"),
            dataType: 'json',
            type: "POST",
            delay: 250,
            data: function (params) {
                return {
                    filter: params.term, // search term
                    page: params.page
                };
            },
            id: function (data) {
                return data.Value;
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                var select2Data = $.map(data.Items, function (obj) {
                    obj.id = obj.Value,
                        obj.text = obj.Text;

                    return obj;
                });

                return {
                    results: select2Data,
                    pagination: {
                        more: (params.page * 10) < data.TotalItems
                    }
                };
            },
            cache: true
        },
        allowClear: true,
        placeholder: $('#productPlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        templateResult: format, // omitted for brevity, see the source of this page
        templateSelection: function (item) { return item.name || item.text; }, // omitted for brevity, see the source of this page
        language: $('#i18n').val()
    }).on('change', function () {
        $.fn.dataTable.reload($dataTable);
    }).on("select2:unselecting", function (e) {
        $(this).select2("val", "");
        e.preventDefault();
    });

    var select2Partner = $("#Filter_SubjectId").select2({
        ajax: {
            url: $("#Filter_SubjectId").data("url"),
            dataType: 'json',
            type: "POST",
            delay: 250,
            data: function (params) {
                return {
                    filter: params.term, // search term
                    page: params.page
                };
            },
            id: function (data) {
                return data.Value;
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                var select2Data = $.map(data.Items, function (obj) {
                    obj.id = obj.Value,
                        obj.text = obj.Text;

                    return obj;
                });

                return {
                    results: select2Data,
                    pagination: {
                        more: (params.page * 10) < data.TotalItems
                    }
                };
            },
            cache: true
        },
        allowClear: true,
        placeholder: $('#partnerPlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        templateResult: format, // omitted for brevity, see the source of this page
        templateSelection: function (item) { return item.name || item.text; }, // omitted for brevity, see the source of this page
        language: $('#i18n').val()
    }).on('change', function () {
        $.fn.dataTable.reload($dataTable);
    }).on("select2:unselecting", function (e) {
        $(this).select2("val", "");
        e.preventDefault();
    });

    $dataTable = $('#grid-data');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = encodeURIComponent($("#Keywords").val());
                d.DateFrom = reformatDate($("#Filter_DateFrom").val());
                d.DateTo = reformatDate($("#Filter_DateTo").val());
                d.DocType = $("#Filter_DocType").val();
                d.ProductId = $("#Filter_ProductId").val();
                d.SubjectId = $("#Filter_SubjectId").val();
                d.Author = $("#Filter_Author").val();
                d.IssuedBillsFilter = $("#itemsDisplayFilterTypeCustomDate").val();
            }
        },
        order: [[2, 'desc']],
        columns: [
            { data: "", name: "", className: 'dt-body-center dt-head-center', render: $.fn.dataTable.render.previewReportTreeOptions("AcKey", $("#Filter_Author").val() === $("#AuthorTypePOS").val() ? 0 : 1) },
            {
                data: "FiscalNum", name: "AcKeyNavigation.THeMoveFiscalisation.AcWorkPlace", filtertype: "text",
                visible: $("#Filter_Author").val() === $("#AuthorTypeRB").val() || $("#IsInFiscalPrinterLocalization").val() === "True" ? false : true
            },
            {
                data: "FiscalCode", name: "AcKeyNavigation.THeMoveFiscalisation.FiscalCode", filtertype: "text",
                visible: $("#Filter_Author").val() === $("#AuthorTypeRB").val() || $("#userLoc").val() !== $("#RSLoc").val() ? false : true
            },
            {
                data: "AcKeyDisplay", name: "AcKey", filtertype: "text", render: $.fn.dataTable.render.link("AcKey", false, $("#Filter_Author").val() === $("#AuthorTypePOS").val() ? 0 :
                    ($("#Filter_Author").val() === $("#AuthorTypeWS").val() ? 1 : 2))
            },
            {
                data: "Protocol", name: "AcKeyNavigation.AcProtocol", filtertype: "text", render: $.fn.dataTable.render.link("AcKey", false, $("#Filter_Author").val() === $("#AuthorTypePOS").val() ? 0 :
                    ($("#Filter_Author").val() === $("#AuthorTypeWS").val() ? 1 : 2)),
                visible: $("#userLoc").val() !== $("#BGLoc").val() ? false : true
            },
            { data: "InvoiceDate", name: "AcKeyNavigation.AdDateInv", filtertype: "dateTime" },
            {
                data: "Receiver", name: "AcKeyNavigation.AcReceiver", filtertype: "text", render: $.fn.dataTable.render.link("Receiver"),
                visible: $("#OurCompIsName2").val() === "False" || ($("#OurCompIsName2").val() === "True" && $("#Filter_Author").val() === $("#AuthorTypeRB").val())
            },
            {
                data: "ReceiverName", name: "AcKeyNavigation.AcReceiverNavigation.AcName2", filtertype: "text", render: $.fn.dataTable.render.link("Receiver", false, null, 40),
                visible: $("#OurCompIsName2").val() === "True" && $("#Filter_Author").val() !== $("#AuthorTypeRB").val()
            },
            {
                data: "Issuer", name: "AcKeyNavigation.AcIssuer", filtertype: "text",
                visible: $("#OurCompIsName2").val() === "False" || $("#Filter_Author").val() !== $("#AuthorTypeRB").val()
            },
            {
                data: "IssuerName", name: "AcKeyNavigation.AcIssuerNavigation.AcName2", filtertype: "text", render: $.fn.dataTable.render.link("Issuer", false, null, 40),
                visible: $("#OurCompIsName2").val() === "True" && $("#Filter_Author").val() === $("#AuthorTypeRB").val()
            },
            { data: "Ident", name: "AcIdent", filtertype: "text" },
            { data: "Quantity", name: "AnQty", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesQuantity").val()) },
            { data: "MeasurementUnit", name: "AcUm", filtertype: "text" },
            { data: "TotalPriceWithVAT", name: "AnPvocforPay", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "Rabate", name: "AnRebate", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "Discount", name: "AnPvdiscount", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "VAT", name: "AnVat", filtertype: "number", className: 'dt-body-right', render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()) },
            { data: "VATPrice", name: "AnPvocincVat", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            {
                data: "IsFiscalized", name: "AcKeyNavigation.AcFiscStatus",
                render: function(data) {
                    if (data === true) {
                        return '<span class="label label-gray">' + $("#lblConfirmed").val() + '</span>';
                    }
                    else {
                        return '<span class="label label-warning"> ' + $("#lblNotConfirmed").val() + '</span>';
                    }
                },
                className: 'dt-body-center dt-head-center',
                visible: $("#Filter_Author").val() === $("#AuthorTypeRB").val() ? false : true
            }
        ],
        colReorder: {
            fixedColumnsLeft: 1,
        },
        createdRow: function (row, data, dataIndex) {
            if (data.IsCanceled === true) {
                $(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('.', ',', $("#DefaultDecimalPlacesPrice").val()).display(json.prop.SumAmount));
            $('#grid-data-vat-sum').html($.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()).display(json.prop.SumVat));
            $('#grid-data-vat-total-sum').html($.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()).display(json.prop.SumAmountWithVat));
            $('.grid-data-start-items').html(json.recordsTotal === 0 ? '0' : '1');
            $('.grid-data-total-items').html(json.recordsTotal);
        }
    });

    $("#downloadIssuedBills").off("click");
    $("#downloadIssuedBills").on("click", function () {
        var params = getSearchData();
        var form = $('<form id="exportForm" method="POST" style="display:none;"></form>');
        form.attr("action", $("#ExportUrl").val());
        var form_data = new FormData();

        for (var key in params) {
            var input = $("<input>")
                .attr("name", key).val(params[key]);
            form.append(input);
        }

        $("body").append(form);
        form.submit();
        $("#exportForm").remove();
    });
}