# -*- coding: utf-8 -*-
from __future__ import absolute_import
from import_export import resources
from import_export.fields import Field
from apps.base import ExcelImporter
from apps.base import Formater

class PurchasePlanResource(resources.Resource):

    def __init__(self):
        super(PurchasePlanResource, self).__init__()
        self.fields['no'] = Field(attribute='no')
        self.fields['name'] = Field(attribute='name')
        self.fields['total_count'] = Field(attribute='total_count')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['demend_user_text'] = Field(attribute='demend_user_text')
        self.fields['status_text'] = Field(attribute='status_text')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['notes'] = Field(attribute='notes')
        self.fields['purchase_users'] = Field(attribute='purchase_users')

    def get_export_headers(self):
        return [u'单号', u'名称', u'合计数量', u'需求人', u'创建时间', u'创建人', u'审核状态', u'审核时间',u'审核人', u'采购员', u'备注']

    class Meta:
        fields = ('no', 'name','total_count', 'demend_user_text', 'create_time', 'create_user_text', 'status_text','check_time', 'check_user_text',
                   'purchase_users','notes')
        export_order = fields


class PurchasePlanDetailResource(resources.Resource):

    def __init__(self):
        super(PurchasePlanDetailResource, self).__init__()
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['quality_request_text'] = Field(attribute='quality_request_text')
        self.fields['purchase_count'] = Field(attribute='purchase_count')
        self.fields['product_time'] = Field(attribute='product_time')
        self.fields['stock_count'] = Field(attribute='stock_count')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'产品名称', u'产品代码', u'质量要求', u'数量', u'需求时间', u'当前库存', u'备注']

    class Meta:
        fields = ('name', 'model', 'quality_request_text', 'purchase_count', 'product_time', 'stock_count',
                  'notes',)
        export_order = fields

class PurchasePriceResource(resources.Resource):

    def __init__(self):
        super(PurchasePriceResource, self).__init__()
        self.fields['purchase_user_text'] = Field(attribute='purchase_user_text')
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['quality_request_text'] = Field(attribute='quality_request_text')
        self.fields['purchase_count'] = Field(attribute='purchase_count')
        self.fields['supplier_text'] = Field(attribute='supplier_text')
        self.fields['price'] = Field(attribute='price')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'员工', u'产品', u'代码', u'质量要求', u'数量', u'供应商', u'价格', u'备注']

    class Meta:
        fields = ('purchase_user_text', 'name', 'model', 'quality_request_text', 'purchase_count', 'supplier_text', 'price',
                  'notes',)
        export_order = fields

class PurchasePlanImporter(ExcelImporter):
    fields = {
        u'产品类别': (True, ExcelImporter.formatUnicode),
        u'产品代码': (True, ExcelImporter.formatUnicode),
        u'数量': (True, ExcelImporter.formatFloatGtZ),
        u'质量要求': (False, ExcelImporter.formatUnicode),
        u'需求时间': (False, ExcelImporter.formatDateTime),
        u'备注': (False, ExcelImporter.formatUnicode),
    }

class PurchaseOrderResource(resources.Resource):
    def __init__(self):
        super(PurchaseOrderResource, self).__init__()
        self.fields['order_no'] = Field(attribute='order_no')
        self.fields['no'] = Field(attribute='no')
        self.fields['supplier_name'] = Field(attribute='supplier_name')
        self.fields['products'] = Field(attribute='products')
        self.fields['payment_type'] = Field(attribute='payment_type')
        self.fields['count'] = Field(attribute='count')
        self.fields['amount'] = Field(attribute='amount')
        self.fields['apply_amount'] = Field(attribute='apply_amount')
        self.fields['status_text'] = Field(attribute='status_text')
        self.fields['arrval_text'] = Field(attribute='arrval_text')
        self.fields['entries'] = Field(attribute='entries')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'单号', u'合同号', u'供应商', u'产品', u'付款方式', u'合计数量', u'合计金额',u'已申请金额', u'状态', u'到货状态', u'批次',
                 u'创建时间', u'创建人', u'审核时间', u'审核人',  u'备注',]

    class Meta:
        fields = ('order_no', 'no', 'supplier_name', 'products', 'payment_type', 'count', 'amount', 'apply_amount', 'status_text', 'arrval_text',
                  'entries', 'create_time', 'create_user_text', 'check_time', 'check_user_text', 'notes')
        export_order = fields

class PurchaseOrderDetailResource(resources.Resource):
    def __init__(self):
        super(PurchaseOrderDetailResource, self).__init__()
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['quality_request_text'] = Field(attribute='quality_request_text')
        self.fields['count'] = Field(attribute='count')
        self.fields['price'] = Field(attribute='price')
        self.fields['amount'] = Field(attribute='amount')
        self.fields['arrval_count'] = Field(attribute='arrval_count')
        self.fields['invoice_no'] = Field(attribute='invoice_no')
        self.fields['invoice_amount'] = Field(attribute='invoice_amount')

    def get_export_headers(self):
        return [u'产品名称', u'产品代码', u'质量标准',u'数量', u'单价', u'金额',
                u'到货数量', u'发票', u'付款金额',]

    class Meta:
        fields = ('name', 'model', 'quality_request_text', 'count', 'price', 'amount','arrval_count', 'invoice_no', 'invoice_amount',)
        export_order = fields

class PurchaseInvoiceResource(resources.Resource):
    def __init__(self):
        super(PurchaseInvoiceResource, self).__init__()
        self.fields['order_no'] = Field(attribute='order_no')
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['supplier_name'] = Field(attribute='supplier_name')
        self.fields['amount'] = Field(attribute='amount')
        self.fields['invoice_no'] = Field(attribute='invoice_no')
        self.fields['invoice_amount'] = Field(attribute='invoice_amount')
        self.fields['invoice_date'] = Field(attribute='invoice_date')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['check_status_text'] = Field(attribute='check_status_text')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')

    def get_export_headers(self):
        return [u'合同号', u'产品名称', u'产品代码',u'供应商', u'金额', u'发票号',
                u'发票金额',u'开票日期', u'创建时间', u'创建人',u'审核状态', u'审核时间', u'审核人']

    class Meta:

        fields = ('order_no', 'name', 'model', 'supplier_name',
                  'amount', 'invoice_no', 'invoice_amount','invoice_date', 'create_time', 'create_user_text',
                  'check_status_text', 'check_time', 'check_user_text')
        export_order = fields


class GodownEntryResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['supplier_text'] = Field(attribute='supplier_text')
        self.fields['total_count'] = Field(attribute='total_count')
        self.fields['warehouse_text'] = Field(attribute='warehouse_text')
        self.fields['purchase_order_no'] = Field(attribute='purchase_order_no')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['status_text'] = Field(attribute='status_text')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_amount'] = Field(attribute='total_amount')
            self.fields['total_invoice_amount'] = Field(attribute='total_invoice_amount')
            fields = ('no', 'supplier_text', 'total_count', 'total_amount', 'total_invoice_amount', 'warehouse_text', 'purchase_order_no', 'create_time', 'create_user_text', 'status_text', 'check_time',
                'check_user_text', 'notes')
        else:
            fields = (
            'no', 'supplier_text', 'total_count', 'warehouse_text', 'purchase_order_no', 'create_time', 'create_user_text', 'status_text', 'check_time',
            'check_user_text', 'notes')
        self._meta.export_order = fields

    def get_export_headers(self):
        if self.is_show_cost:
            return [u'入库单号', u'供应商', u'合计数量', u'合计金额', u'合计发票金额', u'仓别',u'合同号', u'创建时间', u'创建人' , u'审核状态', u'审核时间',u'审核人', u'备注']
        return [u'入库单号', u'供应商', u'合计数量', u'仓别',u'合同号', u'创建时间', u'创建人', u'审核状态', u'审核时间', u'审核人', u'备注']



class GodownEntryDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryDetailResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['product_base_text'] = Field(attribute='product_base_text')
        self.fields['product_base_model'] = Field(attribute='product_base_model')
        self.fields['count'] = Field(attribute='count')
        self.fields['unit_text'] = Field(attribute='unit_text')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['price'] = Field(attribute='price')
            self.fields['amount'] = Field(attribute='amount')
            self.fields['invoice_amount'] = Field(attribute='invoice_amount')
            fields = ('product_base_text', 'product_base_model', 'count', 'price', 'amount','invoice_amount', 'unit_text', 'notes')
        else:
            fields = ('product_base_text', 'product_base_model', 'count', 'unit_text', 'notes')
        self._meta.export_order = fields

    def get_export_headers(self):
        if self.is_show_cost:
            return [u'原料名称', u'原料代码', u'数量', u'单价', u'金额',u'发票金额', u'计量单位', u'备注']
        return [u'原料名称', u'原料代码', u'数量', u'计量单位', u'备注']


class GodownEntryReturnResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryReturnResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['supplier_text'] = Field(attribute='supplier_text')
        self.fields['total_count'] = Field(attribute='total_count')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['status_text'] = Field(attribute='status_text')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_amount'] = Field(attribute='total_amount')
            fields = (
            'no', 'supplier_text', 'total_count', 'total_amount', 'create_user_text', 'create_time', 'status_text',
            'check_user_text',
            'check_time', 'notes',)
        else:
            fields = (
            'no', 'supplier_text', 'total_count', 'create_user_text', 'create_time', 'status_text',
            'check_user_text',
            'check_time', 'notes',)
        self._meta.export_order = fields


    def get_export_headers(self):
        if self.is_show_cost:
            return [u'退货单号', u'供应商', u'合计数量', u'合计金额', u'创建人', u'创建时间' , u'审核状态',u'审核人', u'审核时间', u'备注']
        return [u'退货单号', u'供应商', u'合计数量', u'创建人', u'创建时间' , u'审核状态',u'审核人', u'审核时间', u'备注']


class GodownEntryReturnDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryReturnDetailResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['product_base_text'] = Field(attribute='product_base_text')
        self.fields['product_base_model'] = Field(attribute='product_base_model')
        self.fields['godownentry_no'] = Field(attribute='godownentry_no')
        self.fields['count'] = Field(attribute='count')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['price'] = Field(attribute='price')
            self.fields['amount'] = Field(attribute='amount')
            fields = ('product_base_text', 'product_base_model', 'godownentry_no', 'count', 'price', 'amount', 'notes')
        else:
            fields = ('product_base_text', 'product_base_model', 'godownentry_no', 'count', 'notes')
        self._meta.export_order = fields

    def get_export_headers(self):
        if self.is_show_cost:
            return [u'原料名称', u'原料代码', u'入库单号', u'数量', u'单价', u'金额', u'备注']
        return [u'原料名称', u'原料代码', u'入库单号', u'数量', u'备注']


class PurchasePaymentResource(resources.Resource):
    def __init__(self):
        super(PurchasePaymentResource, self).__init__()
        self.fields['no'] = Field(attribute='no')
        self.fields['order_no'] = Field(attribute='order_no')
        self.fields['supplier_name'] = Field(attribute='supplier_name')
        self.fields['supplier_account'] = Field(attribute='supplier_account')
        self.fields['amount'] = Field(attribute='amount')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['status_text'] = Field(attribute='status_text')
        self.fields['check_user_text'] = Field(attribute='check_user_text')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['actual_amount'] = Field(attribute='actual_amount')
        self.fields['apply_amount'] = Field(attribute='apply_amount')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'付款单号', u'合同号',u'供应商',u'供应商账号', u'合同总金额', u'申请金额',u'实收金额', u'创建时间', u'创建人', u'审核状态', u'审核时间',
                u'审核人',  u'备注']

    class Meta:
        fields = ('no','order_no', 'supplier_name', 'supplier_account', 'amount', 'apply_amount','actual_amount', 'create_time', 'create_user_text', 'status_text',
                   'check_time','check_user_text', 'notes')
        export_order = fields


class PurchasePaymentDetailResource(resources.Resource):

    def __init__(self):
        super(PurchasePaymentDetailResource, self).__init__()
        self.fields['product__name'] = Field(attribute='product__name')
        self.fields['product__model'] = Field(attribute='product__model')
        self.fields['main__no'] = Field(attribute='main__no')
        self.fields['amount'] = Field(attribute='amount')

    def dehydrate_amount(self, instance):
        return Formater.formatAmountShowWithTwoDecimalPlaces(instance.amount)

    def get_export_headers(self):
        return [u'产品名称', u'产品代码', u'合同号', u'金额',]

    class Meta:
        fields = ('product__name', 'product__model', 'main__no', 'amount',)
        export_order = fields

class GodownEntryQueryResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryQueryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['type'] = Field(attribute='type')
        self.fields['supplier'] = Field(attribute='supplier')
        self.fields['product_name'] = Field(attribute='product_name')
        self.fields['product_model'] = Field(attribute='product_model')
        self.fields['product_unit'] = Field(attribute='product_unit')
        self.fields['product_type'] = Field(attribute='product_type')
        self.fields['warehouse'] = Field(attribute='warehouse')
        self.fields['count'] = Field(attribute='count')
        self.fields['return_count'] = Field(attribute='return_count')
        self.fields['deliver_count'] = Field(attribute='deliver_count')
        self.fields['surplus_count'] = Field(attribute='surplus_count')
        self.fields['create_user'] = Field(attribute='create_user')
        self.fields['check_time'] = Field(attribute='check_time')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['price'] = Field(attribute='price')
            self.fields['amount'] = Field(attribute='amount')
            fields = (
                'no', 'type', 'supplier', 'product_name', 'product_model', 'product_unit', 'product_type', 'warehouse',
               'price', 'count', 'return_count', 'amount', 'deliver_count', 'surplus_count', 'create_user',
                'check_time', 'warehouse_place', 'notes')
        else:
            fields = (
                'no', 'type', 'supplier', 'product_name', 'product_model', 'product_unit', 'product_type', 'warehouse',
                'count', 'return_count', 'deliver_count', 'surplus_count', 'create_user', 'check_time',
                'warehouse_place', 'notes')
        self._meta.export_order = fields

    def get_export_headers(self):
        if self.is_show_cost:
            return [u'入库单号', u'入库类别', u'供应商', u'产品', u'代码', u'单位', u'产品类别', u'仓别', u'入库单价', u'数量'
                , u'退货数量', u'合计金额', u'出库数量', u'剩余数量', u'创建人', u'审核时间', u'存放库位', u'备注']
        return [u'入库单号', u'入库类别', u'供应商', u'产品', u'代码', u'单位', u'产品类别', u'仓别', u'数量'
            , u'退货数量', u'出库数量', u'剩余数量', u'创建人', u'审核时间', u'存放库位', u'备注']


class GodownEntryReturnQueryResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GodownEntryReturnQueryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['main_no'] = Field(attribute='main_no')
        self.fields['godownentry_no'] = Field(attribute='godownentry_no')
        self.fields['main_supplier'] = Field(attribute='main_supplier')
        self.fields['product_base_text'] = Field(attribute='product_base_text')
        self.fields['product_base_model'] = Field(attribute='product_base_model')
        self.fields['main_warehouse'] = Field(attribute='main_warehouse')
        self.fields['count'] = Field(attribute='count')
        self.fields['main_create_user'] = Field(attribute='main_create_user')
        self.fields['main_create_time'] = Field(attribute='main_create_time')
        self.fields['main_check_user'] = Field(attribute='main_check_user')
        self.fields['main_check_time'] = Field(attribute='main_check_time')
        self.fields['main_notes'] = Field(attribute='main_notes')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['price'] = Field(attribute='price')
            self.fields['amount'] = Field(attribute='amount')
            fields = (
            'main_no', 'godownentry_no', 'main_supplier', 'product_base_text', 'product_base_model', 'main_warehouse',
            'price',
            'count','amount', 'main_create_user', 'main_create_time', 'main_check_user', 'main_check_time',
            'main_notes', 'notes')
        else:
            fields = (
            'main_no', 'godownentry_no', 'main_supplier', 'product_base_text', 'product_base_model', 'main_warehouse',
            'count', 'main_create_user', 'main_create_time', 'main_check_user', 'main_check_time',
            'main_notes', 'notes')
        self._meta.export_order = fields


    def get_export_headers(self):
        if self.is_show_cost:
            return [u'退货单号', u'入库单号',u'供应商', u'产品', u'代码', u'仓别', u'单价', u'数量',
                    u'合计金额', u'创建人', u'创建时间', u'审核人员', u'审核时间', u'退货单备注', u'产品备注']
        return [u'退货单号', u'入库单号',u'供应商', u'产品', u'代码', u'仓别', u'数量',
                    u'创建人', u'创建时间', u'审核人员', u'审核时间', u'退货单备注', u'产品备注']


class GodownEntryImporter(ExcelImporter):
    fields = {
        u'产品名称': (False, ExcelImporter.formatUnicode),
        u'产品代码': (True, ExcelImporter.formatUnicode),
        u'数量': (True, ExcelImporter.formatFloatGtZ),
        u'单价': (True, ExcelImporter.formatFloatGeZ),
        u'发票金额': (True, ExcelImporter.formatFloatGeZ),
        u'备注': (False, ExcelImporter.formatUnicode),
    }
