from django.contrib import admin

from oscar.core.loading import get_model

from oscar_invoices.admin import InvoiceAdmin as BaseInvoiceAdmin

Invoice = get_model("oscar_invoices", "Invoice")


class InvoiceAdmin(BaseInvoiceAdmin):
    search_fields = (
        "number",
        "order__number",
        "order__user__email",
        "order__guest_email",
    )
    date_hierarchy = "date"
    list_display = ["number", "order", "email"]

    def email(self, invoice):
        if invoice.order:
            return invoice.order.email
        return None


admin.site.unregister(Invoice)
admin.site.register(Invoice, InvoiceAdmin)
