# -*- 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 SaleOrderResource(resources.Resource):
    def __init__(self):
        super(SaleOrderResource, self).__init__()
        self.fields['no'] = Field(attribute='no')
        self.fields['create_time'] = Field(attribute='create_time')
        self.fields['create_user_text'] = Field(attribute='create_user_text')
        self.fields['customer_name'] = Field(attribute='customer_name')
        self.fields['customer_tel'] = Field(attribute='customer_tel')
        self.fields['products'] = Field(attribute='products')
        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')
        self.fields['count'] = Field(attribute='count')
        self.fields['amount'] = Field(attribute='amount')
        self.fields['receive_count'] = Field(attribute='receive_count')
        self.fields['receive_amount'] = Field(attribute='receive_amount')
        self.fields['pay_amount'] = Field(attribute='pay_amount')
        self.fields['loss_amount'] = Field(attribute='loss_amount')
        self.fields['put_amount'] = Field(attribute='put_amount')
        self.fields['fare_amount'] = Field(attribute='fare_amount')
        self.fields['total_amount'] = Field(attribute='total_amount')
        self.fields['clear_text'] = Field(attribute='clear_text')

    def get_export_headers(self):
        return [u'单号', u'客户', u'客户电话', u'产品', u'合计数量', u'合计金额',
                u'收货数量', u'收货金额', u'扣减金额', u'应付金额', u'结算金额', u'装车费用', u'运输费用', u'结清',
                u'创建时间', u'创建人', u'审核状态', u'审核时间', u'审核人', u'备注']

    class Meta:
        fields = ('no', 'customer_name', 'customer_tel', 'products',
                  'count', 'amount', 'receive_count', 'receive_amount', 'loss_amount',
                  'total_amount', 'pay_amount', 'put_amount', 'fare_amount', 'clear_text',
                  'create_time', 'create_user_text', 'check_status_text',
                  'check_time', 'check_user_text', 'notes',)
        export_order = fields


class SaleOrderDetailResource(resources.Resource):
    def __init__(self):
        super(SaleOrderDetailResource, 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['receive_count'] = Field(attribute='receive_count')
        self.fields['price'] = Field(attribute='price')
        self.fields['amount'] = Field(attribute='amount')

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

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


class GoodsDeliverResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GoodsDeliverResource, self).__init__()
        self.is_show_cost = is_show_cost
        self.fields['no'] = Field(attribute='no')
        self.fields['sale_order_no'] = Field(attribute='sale_order_no')
        self.fields['agent_user_text'] = Field(attribute='agent_user_text')
        self.fields['agent_department_text'] = Field(attribute='agent_department_text')
        self.fields['customer_name'] = Field(attribute='customer_name')
        self.fields['customer_tel'] = Field(attribute='customer_tel')
        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['warehouse_text'] = Field(attribute='warehouse_text')
        self.fields['total_count'] = Field(attribute='total_count')

        self.fields['total_amount'] = Field(attribute='total_amount')
        self.fields['notes'] = Field(attribute='notes')
        if is_show_cost:
            self.fields['total_cost'] = Field(attribute='total_cost')
            fields = ('no', 'sale_order_no', 'customer_name', 'customer_tel', 'agent_user_text','agent_department_text', 'total_count', 'total_cost', 'total_amount',
                      'warehouse_text', 'create_time', 'create_user_text', 'status_text', 'check_time', 'check_user_text', 'notes')
        else:
            fields = ('no', 'sale_order_no', 'customer_name', 'customer_tel', 'agent_user_text','agent_department_text', 'total_count', 'total_amount',
                      'warehouse_text', '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'审核状态', u'审核时间', u'审核人', u'备注']
        else:
            return [u'出库单号', u'销售单号', u'客户姓名', u'客户电话', u'经办人',u'经办部门', u'合计数量', u'合计销售价', u'仓别',  u'创建时间', u'创建人', u'审核状态', u'审核时间', u'审核人', u'备注']


class GoodsDeliverDetailResource(resources.Resource):

    def __init__(self, is_show_cost=True):
        super(GoodsDeliverDetailResource, 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['price'] = Field(attribute='price')
        self.fields['no'] = Field(attribute='no')
        self.fields['warehouse_stock_count'] = Field(attribute='warehouse_stock_count')
        self.fields['total_amount'] = Field(attribute='total_amount')
        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', 'price', 'warehouse_stock_count', 'total_cost', 'total_amount', 'notes')
        else:
            fields = ('name', 'model', 'unit_text', 'no', 'count', 'price', 'warehouse_stock_count', 'total_amount', '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 GoodsDeliverQueryResource(resources.Resource):
    def __init__(self, is_show_cost=True):
        super(GoodsDeliverQueryResource, 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['cur_count'] = Field(attribute='cur_count')
        self.fields['count'] = Field(attribute='count')
        self.fields['total_amount'] = Field(attribute='total_amount')
        self.fields['return_count'] = Field(attribute='return_count')
        self.fields['check_user'] = Field(attribute='check_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['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', 'price',
                      'cur_count', 'total_cost', 'total_amount', 'return_count', 'return_cost', 'check_time','check_user',
                      'warehouse_place', 'notes')
        else:
            fields = ('no', 'type', 'name', 'model', 'unit', 'entry_no', 'product_type', 'warehouse',  'count',
                      'cur_count', 'total_amount', 'return_count', 'check_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'存放库位', u'备注']
        return [u'出库单号', u'出库类别', u'产品', u'代码', u'单位', u'入库单号', u'产品类别', u'仓别', u'数量', u'剩余数量',
                    u'合计销售价', u'退料数量', u'审核时间', u'审核人', u'存放库位', u'备注']



class GoodsDeliverReturnQueryResource(resources.Resource):
    def __init__(self, is_show_cost=True):
        super(GoodsDeliverReturnQueryResource, 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')
        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:
            self.fields['return_cost'] = Field(attribute='return_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.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'退货原因', u'产品备注']

class SaleOrderEntryImporter(ExcelImporter):

    fields = {
        u'客户姓名': (False, ExcelImporter.formatUnicode),
        u'客户电话': (False, ExcelImporter.formatUnicode),
        u'产品名称': (True, ExcelImporter.formatUnicode),
        u'产品代码': (True, ExcelImporter.formatUnicode),
        u'数量': (True, ExcelImporter.formatFloatGtZ),
        u'单价': (True, ExcelImporter.formatFloatGeZ),
        u'备注': (False, ExcelImporter.formatUnicode),
    }
