# -*- 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.goods.models import Goods, GoodsGodownEntry, GoodsGodownEntryDetail


class GoodsResource(resources.Resource):

    def __init__(self):
        super(GoodsResource, 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['standard'] = Field(attribute='standard')
        self.fields['goods_pack'] = Field(attribute='goods_pack')
        self.fields['unit'] = Field(attribute='unit')
        self.fields['code'] = Field(attribute='code')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['retail_price'] = Field(attribute='retail_price')
        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', 'standard', 'goods_pack', 'unit', 'code', 'warehouse_place', 'retail_price',
                  'enabled_text', 'notes',)
        export_order = fields


class GoodsImporter(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.formatFloatGeZ),
        u'备注': (False, ExcelImporter.formatUnicode),
    }

class GoodsGodownEntryResource(resources.Resource):

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

class GoodsGodownEntryDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GoodsGodownEntryDetailResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['goods_text'] = Field(attribute='goods_text')
        self.fields['goods_model'] = Field(attribute='goods_model')
        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')
            self.fields['invoice_amount'] = Field(attribute='invoice_amount')
            fields = ('goods_text', 'goods_model', 'count', 'price', 'amount', 'invoice_amount', 'notes')
        else:
            fields = ('goods_text', 'goods_model', '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'备注']


class GoodsGodownEntryImporter(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),
    }


class GoodsProductResource(resources.Resource):

    def __init__(self):
        super(GoodsProductResource, 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['standard'] = Field(attribute='standard')
        self.fields['stock_count'] = Field(attribute='stock_count')
        self.fields['warehouse_place'] = Field(attribute='warehouse_place')
        self.fields['unit'] = Field(attribute='unit')
        self.fields['enabled_text'] = Field(attribute='enabled_text')
        self.fields['code'] = Field(attribute='code')
        self.fields['notes'] = Field(attribute='notes')

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

    class Meta:
        fields = ('name', 'model', 'option_type_text', 'standard', 'stock_count', 'warehouse_place', 'unit',
                  'enabled_text', 'code', 'notes',)
        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['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['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', 'product_name', 'product_model', 'product_unit', 'product_type', 'warehouse',
            'price', 'count', 'amount', 'deliver_count', 'surplus_count', 'create_user', 'check_time',
            'warehouse_place', 'notes')
        else:
            fields = (
            'no', 'type', 'product_name', 'product_model', 'product_unit', 'product_type', 'warehouse',
            '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'备注']

        return [u'入库单号', u'入库类别', u'成品名称', u'成品代码', u'单位', u'产品类别', u'仓别', u'数量',
                u'出库数量', u'剩余数量', u'创建人', u'审核时间', u'存放库位', u'备注']