from django.db import models
from django.db.models.base import Model
import django.utils.timezone as timezone
from django.db.models.query import QuerySet
from apps.system.models import CommonAModel, CommonBModel, Organization, User, Dict, File
from utils.model import SoftModel, BaseModel
from simple_history.models import HistoricalRecords
from apps.mtm.models import Material
from apps.pm.models import SubProductionPlan
from django.core.validators import MinValueValidator, MaxValueValidator


class WareHouse(CommonAModel):
    """
    仓库信息
    """
    number = models.CharField('仓库编号', max_length=20, unique=True)
    name = models.CharField('仓库名称', max_length=20, unique=True)
    place = models.CharField('具体地点', max_length=50)
    class Meta:
        verbose_name = '仓库信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Inventory(BaseModel):
    """
    库存物料
    """
    material = models.ForeignKey(Material, on_delete=models.CASCADE, verbose_name='物料信息')
    count = models.PositiveIntegerField('仓库物料存量', default=0)
    warehouse = models.ForeignKey(WareHouse, on_delete=models.CASCADE, verbose_name='所在仓库')
    class Meta:
        verbose_name = '库存表'
        verbose_name_plural = verbose_name

class MaterialBatch(BaseModel):
    """
    物料批次
    """
    material = models.ForeignKey(Material, on_delete=models.CASCADE, verbose_name='物料信息')
    warehouse = models.ForeignKey(WareHouse, on_delete=models.CASCADE, verbose_name='所在仓库')
    count = models.PositiveIntegerField('存量', default=0)
    batch = models.CharField('批次号', max_length=100, default='')
    expiration_date = models.DateField('有效期', null=True, blank=True)
    class Meta:
        verbose_name = '库存表'
        verbose_name_plural = verbose_name



class FIFO(CommonAModel):
    """
    出入库记录
    """
    FIFO_TYPE_DO_OUT = 1 # 生产领料
    FIFO_TYPE_SALE_OUT = 2
    FIFO_TYPE_PUR_IN = 3
    FIFO_TYPE_DO_IN = 4
    type_choices = (
        (FIFO_TYPE_DO_OUT, '生产领料'),
        (FIFO_TYPE_SALE_OUT, '销售提货'),
        (FIFO_TYPE_PUR_IN, '采购入库'),
        (FIFO_TYPE_DO_IN, '生产入库')
    )
    type = models.IntegerField('出入库类型', default=1)
    is_audited = models.BooleanField('是否审核', default=False)
    auditor = models.ForeignKey(User, verbose_name='审核人', on_delete=models.CASCADE, null=True, blank=True)
    inout_date = models.DateField('出入库日期')
    remark = models.CharField('备注', max_length=1000, default='')


class FIFOItem(BaseModel):
    """
    出入库详细条目
    """
    is_tested = models.BooleanField('是否已检测', default=False)
    is_testok = models.BooleanField('是否检测合格', default=False)
    warehouse = models.ForeignKey(WareHouse, on_delete=models.CASCADE, verbose_name='仓库')
    material = models.ForeignKey(Material, verbose_name='物料类型', on_delete=models.CASCADE)
    count = models.PositiveIntegerField('数量', default=0)
    batch = models.CharField('批次号', max_length=100, default='')
    fifo = models.ForeignKey(FIFO, verbose_name='关联出入库', on_delete=models.CASCADE)
    subproduction_plan = models.ForeignKey(SubProductionPlan, verbose_name='关联子生产计划', on_delete=models.CASCADE, null=True, blank=True)

class IProduct(BaseModel):
    """
    具体产品条目
    """
    number = models.CharField('物品编号', unique=True, max_length=50)
    material = models.ForeignKey(Material, verbose_name='物料类型', on_delete=models.CASCADE)
    warehouse = models.ForeignKey(WareHouse, on_delete=models.CASCADE, verbose_name='所在仓库')
    batch = models.CharField('所属批次号', max_length=100, default='')
    wproduct = models.ForeignKey('wpm.wproduct', on_delete=models.CASCADE, verbose_name='关联的动态产品', db_constraint=False, null=True, blank=True)
    is_mtested = models.BooleanField('是否军检', default=False)
    is_mtestok = models.BooleanField('是否军检合格', null=True, blank=True)
    remark_mtest = models.TextField('军检备注', null=True, blank=True)
    is_saled = models.BooleanField('是否售出', default=False)

class FIFOItemProduct(BaseModel):
    """
    出入库产品
    """
    fifoitem = models.ForeignKey(FIFOItem, verbose_name='关联出入库具体产品', on_delete=models.CASCADE)
    wproduct = models.ForeignKey('wpm.wproduct', on_delete=models.CASCADE, verbose_name='关联的动态产品', db_constraint=False, null=True, blank=True)
    number = models.CharField('物品编号', max_length=50)
    material = models.ForeignKey(Material, verbose_name='物料类型', on_delete=models.CASCADE)
    iproduct = models.ForeignKey(IProduct, verbose_name='关联库存产品', null=True, blank=True, on_delete=models.SET_NULL)



    
