# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from import_export.admin import ImportExportModelAdmin

from import_export.resources import ModelResource
from import_export.fields import Field

from .models import PurchaseOrder, PurchaseOrderLineItem
from .models import SalesOrder, SalesOrderLineItem
from .models import SalesOrderAllocation


class PurchaseOrderLineItemInlineAdmin(admin.StackedInline):
    model = PurchaseOrderLineItem
    extra = 0


class PurchaseOrderAdmin(ImportExportModelAdmin):

    exclude = [
        'reference_int',
    ]

    list_display = (
        'reference',
        'supplier',
        'status',
        'description',
        'creation_date'
    )

    search_fields = [
        'reference',
        'supplier__name',
        'description',
    ]

    inlines = [
        PurchaseOrderLineItemInlineAdmin
    ]


class SalesOrderAdmin(ImportExportModelAdmin):

    exclude = [
        'reference_int',
    ]

    list_display = (
        'reference',
        'customer',
        'status',
        'description',
        'creation_date',
    )

    search_fields = [
        'reference',
        'customer__name',
        'description',
    ]


class POLineItemResource(ModelResource):
    """ Class for managing import / export of POLineItem data """

    part_name = Field(attribute='part__part__name', readonly=True)

    manufacturer = Field(attribute='part__manufacturer', readonly=True)

    MPN = Field(attribute='part__MPN', readonly=True)

    SKU = Field(attribute='part__SKU', readonly=True)

    class Meta:
        model = PurchaseOrderLineItem
        skip_unchanged = True
        report_skipped = False
        clean_model_instances = True


class SOLineItemResource(ModelResource):
    """ Class for managing import / export of SOLineItem data """

    part_name = Field(attribute='part__name', readonly=True)

    IPN = Field(attribute='part__IPN', readonly=True)

    description = Field(attribute='part__description', readonly=True)

    fulfilled = Field(attribute='fulfilled_quantity', readonly=True)

    class Meta:
        model = SalesOrderLineItem
        skip_unchanged = True
        report_skipped = False
        clean_model_instances = True


class PurchaseOrderLineItemAdmin(ImportExportModelAdmin):

    resource_class = POLineItemResource

    list_display = (
        'order',
        'part',
        'quantity',
        'reference'
    )


class SalesOrderLineItemAdmin(ImportExportModelAdmin):

    resource_class = SOLineItemResource

    list_display = (
        'order',
        'part',
        'quantity',
        'reference'
    )


class SalesOrderAllocationAdmin(ImportExportModelAdmin):

    list_display = (
        'line',
        'item',
        'quantity'
    )


admin.site.register(PurchaseOrder, PurchaseOrderAdmin)
admin.site.register(PurchaseOrderLineItem, PurchaseOrderLineItemAdmin)

admin.site.register(SalesOrder, SalesOrderAdmin)
admin.site.register(SalesOrderLineItem, SalesOrderLineItemAdmin)

admin.site.register(SalesOrderAllocation, SalesOrderAllocationAdmin)

@@ -1781,32 +1781,36 @@
 source):%0A    %22%22%22
+%0A   
  Class for manag
@@ -1839,32 +1839,36 @@
  SOLineItem data
+%0A   
  %22%22%22%0A%0A    part_n
@@ -2041,24 +2041,24 @@
 only=True)%0A%0A
-
     fulfille
@@ -2107,32 +2107,357 @@
 readonly=True)%0A%0A
+    def dehydrate_sale_price(self, item):%0A        %22%22%22%0A        Return a string value of the 'sale_price' field, rather than the 'Money' object.%0A        Ref: https://github.com/inventree/InvenTree/issues/2207%0A        %22%22%22%0A%0A        if item.sale_price:%0A            return str(item.sale_price)%0A        else:%0A            return ''%0A%0A
     class Meta:%0A

convert sale_price to string before exporting