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

from apps.base import ExcelImporter


class MaterialResource(resources.Resource):

    def __init__(self):
        super(MaterialResource, self).__init__()
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['option_type_text'] = Field(attribute='option_type_text')
        self.fields['unit'] = Field(attribute='unit')
        self.fields['standard'] = Field(attribute='standard')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['purchase_suggest_text'] = Field(attribute='purchase_suggest_text')
        self.fields['stock_upper_limit'] = Field(attribute='stock_upper_limit')
        self.fields['stock_lower_limit'] = Field(attribute='stock_lower_limit')
        self.fields['enabled_text'] = Field(attribute='enabled_text')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'名称', u'代码', u'类别', u'单位', u'规格', u'库位', u'库存预警', u'库存上限',  u'库存下限', u'在用', u'备注',]

    class Meta:
        fields = ('name', 'model', 'option_type_text', 'unit', 'standard', 'warehouse_place', 'purchase_suggest_text',
                  'stock_upper_limit', 'stock_lower_limit', 'enabled_text', 'notes',)
        export_order = fields


class MaterialImporter(ExcelImporter):
    fields = {
        u'类别': (True, ExcelImporter.formatUnicode),
        u'名称': (True, ExcelImporter.formatUnicode),
        u'代码': (True, ExcelImporter.formatUnicode),
        u'单位': (False, ExcelImporter.formatUnicode),
        u'产品标识': (False, ExcelImporter.formatUnicode),
        u'规格': (False, ExcelImporter.formatUnicode),
        u'库位': (False, ExcelImporter.formatUnicode),
        u'库存预警': (True, ExcelImporter.formatUnicode),
        u'库存上限': (True, ExcelImporter.formatFloatGeZ),
        u'库存下限': (True, ExcelImporter.formatFloatGeZ),
        u'备注': (False, ExcelImporter.formatUnicode),
    }


class ConsumableResource(resources.Resource):

    def __init__(self):
        super(ConsumableResource, self).__init__()
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['option_type_text'] = Field(attribute='option_type_text')
        self.fields['unit'] = Field(attribute='unit')
        self.fields['standard'] = Field(attribute='standard')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['purchase_suggest_text'] = Field(attribute='purchase_suggest_text')
        self.fields['stock_upper_limit'] = Field(attribute='stock_upper_limit')
        self.fields['stock_lower_limit'] = Field(attribute='stock_lower_limit')
        self.fields['enabled_text'] = Field(attribute='enabled_text')
        self.fields['notes'] = Field(attribute='notes')

    def get_export_headers(self):
        return [u'名称', u'代码', u'类别', u'单位', u'规格', u'库位', u'库存预警', u'库存上限',  u'库存下限', u'在用', u'备注',]

    class Meta:
        fields = ('name', 'model', 'option_type_text', 'unit', 'standard', 'warehouse_place', 'purchase_suggest_text',
                   'stock_upper_limit', 'stock_lower_limit', 'enabled_text', 'notes',)
        export_order = fields


class ConsumableImporter(ExcelImporter):
    fields = {
        u'类别': (True, ExcelImporter.formatUnicode),
        u'名称': (True, ExcelImporter.formatUnicode),
        u'代码': (True, ExcelImporter.formatUnicode),
        u'单位': (False, ExcelImporter.formatUnicode),
        u'规格': (False, ExcelImporter.formatUnicode),
        u'库位': (False, ExcelImporter.formatUnicode),
        u'库存预警': (True, ExcelImporter.formatUnicode),
        u'库存上限': (True, ExcelImporter.formatFloatGeZ),
        u'库存下限': (True, ExcelImporter.formatFloatGeZ),
        u'备注': (False, ExcelImporter.formatUnicode),
    }


class DeliverResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(DeliverResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['status_text'] = Field(attribute='status_text')
        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['receiver_text'] = Field(attribute='receiver_text')
        self.fields['receiver_department_text'] = Field(attribute='receiver_department_text')
        self.fields['goods_text'] = Field(attribute='goods_text')
        self.fields['warehouse_text'] = Field(attribute='warehouse_text')
        self.fields['total_count'] = Field(attribute='total_count')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_cost'] = Field(attribute='total_cost')
            fields = ('no', 'goods_text', 'receiver_text', 'receiver_department_text','warehouse_text', 'total_count', 'total_cost', 'create_time',
                      'create_user_text', 'status_text', 'check_time', 'check_user_text', 'notes',)
        else:
            fields = ('no', 'goods_text', 'receiver_text', 'receiver_department_text', 'warehouse_text', 'total_count', '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'审核人' , u'备注']


class DeliverDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(DeliverDetailResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['unit_text'] = Field(attribute='unit_text')
        self.fields['count'] = Field(attribute='count')
        self.fields['no'] = Field(attribute='no')
        self.fields['warehouse_stock_count'] = Field(attribute='warehouse_stock_count')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_cost'] = Field(attribute='total_cost')
            fields = ('name', 'model', 'unit_text', 'no', 'count', 'warehouse_stock_count', 'total_cost', 'notes')
        else:
            fields = ('name', 'model', 'unit_text', 'no', 'count', 'warehouse_stock_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'成本合计', u'备注']
        return [u'名称', u'代码', u'单位', u'入库单号', u'数量', u'仓别库存', u'备注']

class InventoryResource(resources.Resource):

    def __init__(self, is_show_cost):
        super(InventoryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['type_text'] = Field(attribute='type_text')
        self.fields['total_count'] = Field(attribute='total_count')
        self.fields['warehouse_text'] = Field(attribute='warehouse_text')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['create_time'] = Field(attribute='create_time')
        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')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_amount'] = Field(attribute='total_amount')
            fields = (
            'no', 'type_text', 'total_count', 'total_amount','warehouse_text', 'create_time','create_user_text', 'check_status_text',
            'check_time', 'check_user_text', 'notes',)
        else:
            fields = (
            'no', 'type_text', 'total_count', 'warehouse_text', 'create_time', 'create_user_text',  'check_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'备注']
        return [u'盘存单号', u'类别', u'合计数量', u'仓别', u'创建时间', u'创建人', u'审核状态', u'审核时间', u'审核人', u'备注']



class InventoryDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(InventoryDetailResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['warehouse_text'] = Field(attribute='warehouse_text')
        self.fields['entry_no'] = Field(attribute='entry_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 = ('name', 'model', 'warehouse_text', 'entry_no', 'count', 'price', 'amount', 'notes')
        else:
            fields = ('name', 'model', 'warehouse_text', 'entry_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'金额', u'备注']
        return [u'产品名称', u'产品代码', u'仓别', u'入库单', u'备注']



class DeliverQueryResource(resources.Resource):
    def __init__(self, is_show_cost=True):
        super(DeliverQueryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['type'] = Field(attribute='type')
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['unit'] = Field(attribute='unit')
        self.fields['entry_no'] = Field(attribute='entry_no')
        self.fields['product_type'] = Field(attribute='product_type')
        self.fields['warehouse'] = Field(attribute='warehouse')
        self.fields['count'] = Field(attribute='count')
        self.fields['cur_count'] = Field(attribute='cur_count')
        self.fields['return_count'] = Field(attribute='return_count')
        self.fields['receiver'] = Field(attribute='receiver')
        self.fields['check_user'] = Field(attribute='check_user')
        self.fields['happen_time'] = Field(attribute='happen_time')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_cost'] = Field(attribute='total_cost')
            self.fields['return_cost'] = Field(attribute='return_cost')
            fields = ('no', 'type', 'name', 'model', 'unit', 'entry_no', 'product_type', 'warehouse', 'count',
                      'cur_count', 'total_cost', 'return_count', 'return_cost', 'receiver', 'happen_time', 'check_user'
                      , 'warehouse_place', 'notes')
        else:
            fields = ('no', 'type', 'name', 'model', 'unit', 'entry_no', 'product_type', 'warehouse', 'count',
                      'cur_count', 'return_count', 'receiver', 'happen_time', 'check_user',  '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 DeliverReturnQueryResource(resources.Resource):
    def __init__(self, is_show_cost=True):
        super(DeliverReturnQueryResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['return_no'] = Field(attribute='return_no')
        self.fields['type'] = Field(attribute='type')
        self.fields['name'] = Field(attribute='name')
        self.fields['model'] = Field(attribute='model')
        self.fields['product_type'] = Field(attribute='product_type')
        self.fields['warehouse'] = Field(attribute='warehouse')
        self.fields['cur_count'] = Field(attribute='cur_count')
        self.fields['return_count'] = Field(attribute='return_count')
        if is_show_cost:
            self.fields['return_cost'] = Field(attribute='return_cost')
        self.fields['create_user'] = Field(attribute='create_user')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['reason'] = Field(attribute='reason')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            fields = ('no', 'return_no', 'type', 'name', 'model', 'product_type', 'warehouse',
                      'cur_count', 'return_count', 'return_cost', 'create_user', 'create_time'
                      , 'warehouse_place', 'reason', 'notes')
        else:
            fields = ('no', 'return_no', 'type', 'name', 'model', 'product_type', 'warehouse',
                      'cur_count', 'return_count', 'create_user', 'create_time'
                      , 'warehouse_place', 'reason', 'notes')
        self._meta.fields = 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'退料原因', u'备注']

