from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from plata.discount.models import AppliedDiscount
from plata.shop import models


class OrderItemInline(admin.TabularInline):
    model = models.OrderItem
    raw_id_fields = ('product',)

class AppliedDiscountInline(admin.TabularInline):
    model = AppliedDiscount

class OrderStatusInline(admin.TabularInline):
    model = models.OrderStatus
    extra = 1


class OrderAdmin(admin.ModelAdmin):
    date_hierarchy = 'created'
    fieldsets = (
        (None, {'fields': ('created', 'confirmed', 'user', 'email', 'status')}),
        (_('Billing address'), {'fields': models.Order.address_fields('billing_')}),
        (_('Shipping address'), {'fields': ['shipping_same_as_billing'] +\
            models.Order.address_fields('shipping_')}),
        (_('Order items'), {'fields': ('items_subtotal', 'items_discount', 'items_tax')}),
        (_('Shipping'), {'fields': ('shipping_cost', 'shipping_discount', 'shipping_tax')}),
        (_('Total'), {'fields': ('currency', 'total', 'paid')}),
        (_('Additional fields'), {'fields': ('notes',)}),
        )
    inlines = [OrderItemInline, AppliedDiscountInline, OrderStatusInline]
    list_display = ('admin_order_id', 'created', 'user', 'status', 'total',
        'balance_remaining', 'is_paid', 'admin_invoice_pdf', 'admin_packing_slip_pdf')
    list_filter = ('status',)
    ordering = ['-created']
    raw_id_fields = ('user',)
    search_fields = (['_order_id', 'email', 'total', 'notes'] +
        models.Order.address_fields('billing_') +
        models.Order.address_fields('shipping_'))

    def admin_order_id(self, instance):
        return instance.order_id
    admin_order_id.short_description = _('order ID')
    admin_order_id.admin_order_field = '_order_id'

    def admin_invoice_pdf(self, instance):
        from django.core.urlresolvers import reverse
        return u'<a href="%(url)s">%(title)s</a>' % {
            'url': reverse('plata_reporting_invoice_pdf',
                args=(),
                kwargs={'order_id': instance.id}
                ),
            'title': _('PDF'),
            }
    admin_invoice_pdf.allow_tags = True
    admin_invoice_pdf.short_description = _('Invoice')

    def admin_packing_slip_pdf(self, instance):
        from django.core.urlresolvers import reverse
        return u'<a href="%(url)s">%(title)s</a>' % {
            'url': reverse('plata_reporting_packing_slip_pdf',
                args=(),
                kwargs={'order_id': instance.id}
                ),
            'title': _('PDF'),
            }
    admin_packing_slip_pdf.allow_tags = True
    admin_packing_slip_pdf.short_description = _('Packing slip')

admin.site.register(models.Order, OrderAdmin)


class OrderPaymentAdmin(admin.ModelAdmin):
    date_hierarchy = 'timestamp'
    list_display = ('order', 'timestamp', 'currency', 'amount', 'status', 'authorized',
        'payment_module_key', 'notes_short')
    list_display_links = ('timestamp',)
    list_filter = ('status', 'payment_module_key')
    raw_id_fields = ('order',)
    search_fields = ('amount', 'payment_module', 'payment_method', 'transaction_id',
        'notes', 'data_json')

    notes_short = lambda self, obj: (len(obj.notes) > 50) and obj.notes[:40]+'...' or obj.notes
    notes_short.short_description = _('notes')


admin.site.register(models.OrderPayment, OrderPaymentAdmin)


admin.site.register(models.TaxClass,
    list_display=('name', 'rate', 'priority'),
    )

@@ -270,16 +270,30 @@
 oduct',)
+%0A    extra = 0
 %0A%0Aclass 
@@ -363,16 +363,30 @@
 Discount
+%0A    extra = 0
 %0A%0Aclass 
@@ -472,9 +472,9 @@
 a = 
-1
+0
 %0A%0A%0Ac

Stop showing extra forms in order inline formsets