function initializeCashRegisterInvoiceList() {
    var getSearchData = function () {
        return {
            DateFrom: reformatDate($("#Filter_DateFrom").val()),
            DateTo: reformatDate($("#Filter_DateTo").val()),
            PartnerId: $("#Filter_PartnerId").val(),
            Fiscalized: $("#Filter_Fiscalized").val(),
            AcDocType: $("#Filter_AcDocType").val()
        };
    };

    function format(item) {
        return item.Text;
    };

    var select2AcDocType = $("#Filter_AcDocType").select2({
        allowClear: true,
        theme: "bootstrap",
        placeholder: "",
        language: $('#i18n').val()
    });

    var select2Partner = $("#Filter_PartnerId").select2({
        ajax: {
            url: $("#Filter_PartnerId").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: "",
        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: format, // omitted for brevity, see the source of this page
        language: $('#i18n').val()
    });

    var grid = $("#grid-data").bootgrid({
        ajax: true,
        type:'POST',
        formatters: {
            "Fiscalized": function(column, row)
            {
                var fiscalized = row.Fiscalized;
                if (fiscalized) {
                    return "<input type='checkbox' checked disabled />";
                }
                else {
                    return "<input type='checkbox' disabled/>";
                }
            },
            "FiscalizedControl": function (column, row) {
                var fiscalized = row.Fiscalized;
                if (!fiscalized) {
                    return "<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.Id + "\"><span class=\"fa fa-paper-plane-o\"> @Loc.Fiscalize</span></button> ";
                }
                else {
                    return "<input type='checkbox' checked disabled />";
                }
            },
            "clickable": function (column, row) {
                if (row[column.id])
                    return "<a href=\"CashRegister/PrintPreview?id=" + row.Id + " \">" + row[column.id] + "</a>";
                else
                    return "";
            }
        },
        templates: {
            search: "",
            header: ""
        },
        labels: {
            noResults: $("#bootgrid-noResults").val(),
            infos: $("#bootgrid-infos").val(),
            loading: $("#bootgrid-loading").val()

        },
        post: function () {
            /* To accumulate custom parameter with the request object */
            return getSearchData();
        },
    }).on("loaded.rs.jquery.bootgrid", function (e)
    {
        grid.find(".command-edit").on("click", function(e)
        {
            var id = $(this).data("row-id");
            var button = this;
            var url = '@Url.Action("FiscalizeInvoice", "CashRegister", new { id = "___id___" })';
            url = url.replace("___id___", id);
            swal({
                title: "@Loc.FiscalizeInvoice",
                type: "warning",
                showCancelButton: true,
                confirmButtonText: "@Loc.Fiscalize",
            }, function () {
                $(button).prop('disabled', true);
                $.post(url, function (data) {
                    if (data.Success) {
                        toastr.success("@Loc.Fiscalized!", "@Loc.Success", { timeOut: 2000, closeButton: true });
                        grid.bootgrid('reload');
                    }
                    else {
                        if (data.ErrorMessage)
                            toastr.error("@Loc.FiscalizationError!", data.ErrorMessage, { timeOut: 5000, closeButton: true });
                        else
                            toastr.error("@Loc.FiscalizationError!", "@Loc.Error", { timeOut: 5000, closeButton: true });
                        $(button).prop('disabled', false);
                    }
                });
            });

        });
    });

    $("#searchIssuedBills").on("click", function () {
        $('#grid-data').bootgrid('reload');
    });

    $('.datepicker').datepicker()
        .on('click', function (e) {
            $(this).select();
        });
}

function reformatDate(dateString) {
    var dateElements = dateString.split('.');
    return dateElements[2] + '-' + dateElements[1] + '-' + dateElements[0];
}

function format(item) {
    return item.Text;
};