#coding=utf-8

from django.db import models
from django.utils import timezone

from apps.exceptions import CustomError
from apps.foundation.models import Option
from apps.product.manager import ProductBaseManager


class ProductBase(models.Model):
    MATERIAL = 0
    CONSUMABLE = 1
    GOODS = 2
    TYPE_CHOICES = (
        (MATERIAL,u'原料'),
        (CONSUMABLE,u'耗材'),
        (GOODS, u'成品'),
    )
    TYPE_CHOICES_DICT = [{'id':x, 'value':y} for x, y in TYPE_CHOICES]

    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类型")
    option_type = models.ForeignKey(Option, verbose_name=u"类别", null=True, blank=True, on_delete=models.PROTECT)
    name = models.CharField(max_length=100, verbose_name=u"名称")
    model = models.CharField(max_length=100, verbose_name=u"代码")
    barcode = models.CharField(max_length=100, verbose_name=u"条码", null=True, blank=True)
    unit = models.CharField(max_length=10, verbose_name=u"单位", null=True, blank=True)
    code = models.CharField(max_length=100, verbose_name=u"助记码", null=True, blank=True)
    standard = models.CharField(max_length=100, verbose_name=u"规格", null=True, blank=True)
    warehouse_place = models.CharField(max_length=100, verbose_name=u"存放库位", null=True, blank=True)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True, blank=True)
    enabled = models.BooleanField(verbose_name=u"在用", default=True)
    create_time = models.DateTimeField(verbose_name=u"创建时间", default=timezone.now)
    stock_count = models.BigIntegerField(verbose_name=u"库存数量", default=0)
    stock_amount = models.BigIntegerField(verbose_name=u"库存金额", default=0)
    stock_amount2 = models.BigIntegerField(verbose_name=u"库存金额2", default=0)
    avg_cost_price = models.BigIntegerField(verbose_name=u"平均成本价", default=0)
    avg_cost_price2 = models.BigIntegerField(verbose_name=u"平均成本价2", default=0)
    max_price = models.BigIntegerField(verbose_name=u'最高入库价', default=0)
    min_price = models.BigIntegerField(verbose_name=u'最低入库价', default=0)
    last_entry_price = models.BigIntegerField(verbose_name=u"最新入库价", default=0)
    last_entry_time = models.DateTimeField(verbose_name=u"最后入库时间", null=True, blank=True)
    last_deliverd_time = models.DateTimeField(verbose_name=u"最后出库时间", null=True, blank=True)
    objects = ProductBaseManager()

    class Meta:
        db_table = "product_base"
        verbose_name = u"其他"
        ordering = ('-id',)
        index_together = (
            'name',
            'model',
        )
        default_permissions = ()

        permissions = (# 产品
            ("view_material_stock_ledger", u"原料总账查看"),
            ("export_material_stock_ledger", u"原料总账导出"),
            ("view_consumable_stock_ledger", u"耗材总账查看"),
            ("export_consumable_stock_ledger", u"耗材总账导出"),
            ("view_goods_stock_ledger", u"成品总账查看"),
            ("export_goods_stock_ledger", u"成品总账导出"),
            ("view_department_ledger", u"部门总账查看"),
            ("export_department_ledger", u"部门总账导出"),
            ("view_goods_product", u"库存查询"),
        )

    @staticmethod
    def getTypeText(type):
        return ProductBase.TYPE_CHOICES[type][1]

    @staticmethod
    def getById(id):
        instance = ProductBase.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的产品')
        return instance