from django.db import models

# Create your models here.
from django.utils.translation import gettext_lazy as _
from company.models import Company
from django.utils.timezone import now
import os

def image_upload(instance, filename):
    return os.path.join(instance.vendor.company.name, 'vendor', filename) 
#class Category

class Vendor(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('企业名称'),related_name='vendor_company',on_delete=models.CASCADE)
    name=models.CharField(_('供应商名称'),max_length=100)
    address=models.CharField(_('详细地址'),max_length=100,blank=True,null=True)
    cellphone=models.CharField(_('联系电话'),max_length=20,blank=True,null=True)
    # contacts=models.CharField(_('联系人'),max_length=50,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    external_code=models.CharField(_('外部编码'),max_length=50,blank=True,null=True,unique=True)
    contents = models.TextField(_('介绍'), blank=True, null=True)
    is_delete=models.BooleanField(_('是否已删除'),default=False)

    class Meta:
        verbose_name = _('供应商')
        verbose_name_plural = _('供应商')
        unique_together = ("company", "name")
        
    def __str__(self):
        return self.name

class VendorPhotoCategory(models.Model):
    name=models.CharField(_('图片类型'),max_length=100,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)

    class Meta:
        verbose_name = _('图片类型')
        verbose_name_plural = _('图片类型')
        
    def __str__(self):
        return self.name

class VendorPhotos(models.Model):
    vendor = models.ForeignKey(Vendor,verbose_name=_('供应商'),on_delete=models.CASCADE)
    category=models.ForeignKey(VendorPhotoCategory,verbose_name=_('图片类型'),on_delete=models.CASCADE)
    photo = models.ImageField(
        _('图片'), upload_to=image_upload, blank=True, null=True)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)

    class Meta:
        verbose_name = _('供应商图片')
        verbose_name_plural = _('供应商图片')
        
    def __str__(self):
        return self.name

class Customer(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('企业名称'),related_name='customer_company',on_delete=models.CASCADE)
    name=models.CharField(_('客户名称'),max_length=100)
    address=models.CharField(_('详细地址'),max_length=100,blank=True,null=True)
    cellphone=models.CharField(_('联系电话'),max_length=20,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    is_delete=models.BooleanField(_('是否已删除'),default=False)

    class Meta:
        verbose_name = _('代工客户')
        verbose_name_plural = _('代工客户')
        unique_together = ("company", "name")
        
    def __str__(self):
        return self.name


#计量单位 类别 
class Measure(models.Model):
    name=models.CharField(_('类别名称'),max_length=20,unique=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name = _('计量类别')
        verbose_name_plural = _('计量类别')

    def __str__(self):
        return self.name


#计量单位
class Unit(models.Model):
    measure=models.ForeignKey(Measure,verbose_name=_('计量类别'),related_name='unit_measure',on_delete=models.CASCADE)
    name=models.CharField(_('单位名称'),max_length=20,unique=True)
    is_basis=models.BooleanField(_('是否为基准单位'),default=False)
    ratio=models.DecimalField(_('与最基准单位换算比'),default=1,max_digits=13,decimal_places=6)
    abbreviation=models.CharField(_('单位缩写'),max_length=20,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name = _('计量单位')
        verbose_name_plural = _('计量单位')        

    def __str__(self):
        return self.name


class CompanyUnit(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('使用企业'),related_name='companyunit_company',on_delete=models.CASCADE)
    measure=models.ForeignKey(Measure,verbose_name=_('计量类别'),related_name='companyunit_measure',on_delete=models.CASCADE)    
    invtype=models.ForeignKey('inventory.InvType',default=2,on_delete=models.CASCADE)
    unit=models.ForeignKey(Unit,verbose_name=_('计量单位'),related_name='companyunit_unit',on_delete=models.CASCADE)
    #category=models.SmallIntegerField( _('计量标的物'), choices=((1,_('原材料')), (2,_('半成品')), (3,_('包装材料'))) )    
    is_default=models.BooleanField(_('是否为默认计量单位'),default=False)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)  
    is_delete=models.BooleanField(_('是否已删除'),default=False)

    class Meta:
        verbose_name = _('企业使用计量单位')
        verbose_name_plural = _('企业使用计量单位')
        unique_together = ("company", "measure","invtype","unit","is_default")

    def __str__(self):
        return self.name
    
    @property
    def name(self):
        return str(self.unit)

    

#原材料
class Materials(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),related_name='materials_company',on_delete=models.CASCADE)
    measure=models.ForeignKey(Measure,verbose_name=_('计量类别'),related_name='materials_measure',on_delete=models.CASCADE)
    name=models.CharField(_('原材料名称'),max_length=50)
    code=models.CharField(_('材料编码'),unique=True,max_length=20,blank=True,null=True)    
    units=models.JSONField(_('计量单位'),default=list,blank=True,null=True) 
    safety_qty=models.IntegerField(_('基准单位安全库存量'),blank=True,null=True)
    # specification=models.CharField(_('规格'),max_length=50,blank=True,null=True)
    # base_unit=models.CharField(_('基础单位'),max_length=50,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True) 
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    class Meta:
        verbose_name = _('原材料')
        verbose_name_plural = _('原材料')
        unique_together = ("company", "name")

    def __str__(self):
        return self.name


#半成品
class SemiFinishedGoods(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),related_name='semifinishedgoods_company',on_delete=models.CASCADE)
    name=models.CharField(_('半成品名称'),max_length=50)
    code=models.CharField(_('半成品编码'),unique=True,max_length=20,blank=True,null=True)
    measure=models.ForeignKey(Measure,verbose_name=_('计量类别'),related_name='semifinishedgoods_measure',on_delete=models.CASCADE)
    units=models.JSONField(_('计算单位'),default=list,blank=True,null=True) 
    safety_qty=models.IntegerField(_('基准单位安全库存量'),blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True) 
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    class Meta:
        verbose_name = _('半成品')
        verbose_name_plural = _('半成品')
        unique_together = ("company", "name")

    def __str__(self):
        return self.name


#包装材料
class Packaging(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),related_name='packing_company',on_delete=models.CASCADE)
    name=models.CharField(_('包材名称'),max_length=50)
    code=models.CharField(_('包材编码'),unique=True,max_length=20,blank=True,null=True)
    measure=models.ForeignKey(Measure,verbose_name=_('计量类别'),related_name='packaging_measure',on_delete=models.CASCADE)
    units=models.JSONField(_('计量单位'),default=list,blank=True,null=True) 
    safety_qty=models.IntegerField(_('基准单位安全库存量'),blank=True,null=True)
    specification=models.CharField(_('规格'),max_length=50,blank=True,null=True)
    unit=models.ForeignKey(Unit,verbose_name=_('基础单位'),on_delete=models.CASCADE)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True) 
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    class Meta:
        verbose_name = _('包装材料')
        verbose_name_plural = _('包装材料')
        unique_together = ("company", "name")

    def __str__(self):
        return self.name
    
#包材供应商 lisin 2024-08-20
class PackagingVendor(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包装材料'),on_delete=models.CASCADE)
    vendor=models.ForeignKey(Vendor,verbose_name=_('供应商'),on_delete=models.CASCADE)
    unit_price=models.DecimalField(_('单价'), max_digits=8, decimal_places=2)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True) 
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    class Meta:
      verbose_name = _('包装供应商')
      verbose_name_plural = _('包装供应商')
      unique_together = ("company", "packaging", "vendor")

    def __str__(self):
        return self.name
    
#包材采购记录 lisin 2024-08-23
class PackagingPurchasRecord(models.Model):
    code=models.CharField(_('采购单编码'),max_length=20,blank=True,null=True)    
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包材'),on_delete=models.CASCADE)
    vendor=models.ForeignKey(Vendor,verbose_name=_('供应商'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))
    unit_price=models.DecimalField(_('单价'), max_digits=8, decimal_places=2)
    total_price=models.DecimalField(_('总价'), max_digits=8, decimal_places=2)
    dt = models.DateField(_('采购日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('采购时间'), default=now,blank=True,null=True) 

    class Meta:
      verbose_name = _('包材采购记录')
      verbose_name_plural = _('包材采购记录')
      unique_together = ("company", "packaging", "code")

    def __str__(self):
        return self.name

#包材入库记录 lisin 2024-08-24
class PackagingInstore(models.Model):
    code=models.CharField(_('入库编码'),max_length=20,blank=True,null=True)    
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包材'),on_delete=models.CASCADE)
    packaging_purchas=models.ForeignKey(PackagingPurchasRecord,verbose_name=_('包材采购记录'),on_delete=models.CASCADE)
    instore_num = models.IntegerField(_('入库数量'))
    dt = models.DateField(_('入库日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('入库时间'), default=now,blank=True,null=True) 

    class Meta:
      verbose_name = _('包材入库记录')
      verbose_name_plural = _('包材入库记录')
      unique_together = ("code","company")

    def __str__(self):
        return self.code 
    
#包材领用记录 lisin 2024-08-24
class PackagingReceive(models.Model):
    code=models.CharField(_('领用编码'),max_length=20,blank=True,null=True)    
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包材'),on_delete=models.CASCADE)
    receive_num=models.IntegerField(_('领用数量')) 
    dt = models.DateField(_('领用日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('领用时间'), default=now,blank=True,null=True) 

    class Meta:
      verbose_name = _('包材领用记录')
      verbose_name_plural = _('包材领用记录')
      unique_together = ("code","company")

#包材库存
class PackagingInventory(models.Model):
    code=models.CharField(_('库存编码'),max_length=20,blank=True,null=True)    
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包材'),on_delete=models.CASCADE)
    num=models.IntegerField(_('库存数量')) 
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
      verbose_name = _('包材库存')
      verbose_name_plural = _('包材库存')
      unique_together = ("code","company")


#物料单
class BOM(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),related_name='bom_company',on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产成品'),related_name='bom_product',on_delete=models.CASCADE)
    name=models.CharField(_('物料单名称'),max_length=50,blank=True,null=True)
    code=models.CharField(_('物料单编码'),max_length=20,blank=True,null=True)    
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    class Meta:
        verbose_name = _('物料单')
        verbose_name_plural = _('物料单')
        unique_together = ("company", "product")

    def __str__(self):
        return self.name


#物料单-原材料
class BOMmaterials(models.Model):
    bom=models.ForeignKey(BOM,verbose_name=_('物料单'),related_name='bommaterials_bom',on_delete=models.CASCADE)
    materials=models.ForeignKey(Materials,verbose_name=_('原材料'),related_name='bommaterials_mat',on_delete=models.CASCADE)
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='bommaterials_unit',on_delete=models.CASCADE)
    qty=models.DecimalField(_('数量'), max_digits=8, decimal_places=2)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)

    class Meta:
        verbose_name = _('原材料用料单')
        verbose_name_plural = _('原材料用料单')     
        unique_together = ("bom", "materials")

    def __str__(self):
        return self.name



#物料单-半成品
class BOMsemifinishedgoods(models.Model):
    bom=models.ForeignKey(BOM,verbose_name=_('物料单'),related_name='bomsemifinishedgoods_bom',on_delete=models.CASCADE)
    semifinishedgoods=models.ForeignKey(SemiFinishedGoods,verbose_name=_('半成品'),related_name='bomsemifinishedgoods_semi',on_delete=models.CASCADE)
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='bomsemifinishedgoods_unit',on_delete=models.CASCADE)
    qty=models.DecimalField(_('数量'), max_digits=8, decimal_places=2)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)

    class Meta:
        verbose_name = _('半成品用料单')
        verbose_name_plural = _('半成品用料单')     
        unique_together = ("bom", "semifinishedgoods")

    def __str__(self):
        return self.name


#物料单-包材
class BOMpackaging(models.Model):
    bom=models.ForeignKey(BOM,verbose_name=_('物料单'),related_name='bompackaging_bom',on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包装物'),related_name='bompackaging_pkg',on_delete=models.CASCADE)
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='bompackaging_unit',on_delete=models.CASCADE)
    qty=models.DecimalField(_('数量'), max_digits=8, decimal_places=2)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)
    forecast = models.DecimalField(_('预估单价'), max_digits=8, decimal_places=2,blank=True,null=True)
    specification=models.CharField(_('规格'),max_length=50,blank=True,null=True)
    
    class Meta:
        verbose_name = _('包材用料单')
        verbose_name_plural = _('包材用料单')     
        unique_together = ("bom", "packaging")

    def __str__(self):
        return self.name



#原材料仓库 
class Storehouse(models.Model):
    company=models.ForeignKey(Company,verbose_name=_('所属企业'),related_name='messtore_company',on_delete=models.CASCADE)
    name=models.CharField(_('名称'),max_length=50,blank=True,null=True)
    code=models.CharField(_('编码'),max_length=20,blank=True,null=True)   
    location=models.CharField(_('仓库位置'),max_length=50,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)    
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    class Meta:
        verbose_name = _('原材料仓库')
        verbose_name_plural = _('原材料仓库')
        unique_together = ("company", "name")

    def __str__(self):
        return self.name


class OrderStatus(models.Model):
    name=models.CharField(_('状态名'),max_length=50,unique=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True) 

    class Meta:
        verbose_name = _('订单状态')
        verbose_name_plural = _('订单状态')


class Order(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('公司'), related_name='order_company', on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),related_name='order_user',on_delete=models.CASCADE,blank=True,null=True)
    agent=models.ForeignKey('company.Company',verbose_name=_('客户'),related_name='order_agent',on_delete=models.CASCADE,blank=True,null=True)
    status=models.ForeignKey(OrderStatus,verbose_name=_('订单状态'),on_delete=models.CASCADE,default=1)
    customer=models.CharField(_('客户名称'),max_length=100,blank=True,null=True)
    code=models.CharField(_('订单编号'),max_length=20)
    dt_delivery=models.DateField(_('交货日期'),blank=True,null=True) 
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True) 
    is_delete = models.BooleanField(_('是否已删除'), blank=True, null=True, db_index=True, default=False)
    
    class Meta:
        verbose_name = _('生产订单')
        verbose_name_plural = _('生产订单')
        unique_together = ("company", "code")


class OrderDetails(models.Model):
    order=models.ForeignKey(Order,verbose_name =_('生产订单'),on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=('产成品名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='orderdetails_unit',on_delete=models.CASCADE,blank=True,null=True)

    class Meta:
        verbose_name = _('生产订单明细')
        verbose_name_plural = _('生产订单明细')
        unique_together = ("order", "product")


#MES批进出存处理
class Batch(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('企业名称'), related_name='mesbatch_company',on_delete=models.CASCADE)    
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),related_name='mesbatch_user',on_delete=models.CASCADE)
    vtype=models.ForeignKey('inventory.voucher',verbose_name=_('单据类别ID'),related_name='mesbatch_vtype',on_delete=models.CASCADE)
    store_fr=models.ForeignKey(Storehouse,verbose_name=_('仓库名称'),related_name='mesbatch_store_fr',blank=True,null=True,on_delete=models.CASCADE)
    store_to=models.ForeignKey(Storehouse,verbose_name=_('转货目的仓库'),related_name='mesbatch_store_to',blank=True, null=True,on_delete=models.CASCADE)   
    order=models.ForeignKey(Order,verbose_name=_('生产订单'),related_name='mesbatch_order',blank=True,null=True,on_delete=models.CASCADE)
    customer=models.ForeignKey(Customer,verbose_name=_('代工客户'),related_name='mesbatch_customer',blank=True,null=True,on_delete=models.CASCADE)
    vendors=models.JSONField(_('供应商'),blank=True,null=True,default=list)    
    code = models.CharField(_('单据号码'), max_length=20, db_index=True) 
    dt_voucher=models.DateField(_('单据日期'),blank=True,null=True)
    dt = models.DateField(_('生成日期'), auto_now_add=True, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('生成时间'), auto_now_add=True,blank=True,null=True)  
    remark = models.CharField(_('备注说明'), max_length=50, blank=True, null=True)    
    is_delete=models.BooleanField(_('是否已删除'),default=False,blank=True,null=True)

    def __str__(self):
        return self.code

    class Meta:
        verbose_name = _('MES进出存批处理')
        verbose_name_plural = _('MES进出存批处理')    


#MES进出存明细
class StkDetails(models.Model):    
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='messtkdetails_company', on_delete=models.CASCADE)   
    vtype= models.ForeignKey('inventory.Voucher',verbose_name=_('单据类别'),related_name='messtkdetails_vtype',on_delete=models.CASCADE,blank=True,null=True)
    batch = models.ForeignKey(Batch,verbose_name=_('MESbatch'),blank=True,null=True,on_delete=models.CASCADE)
    store_fr = models.ForeignKey(Storehouse, verbose_name=_('仓库名称'), related_name='messtkdetials_store_fr',on_delete=models.CASCADE,blank=True,null=True)
    store_to = models.ForeignKey(Storehouse, verbose_name=_('转货目的仓库'),related_name='messtkdetials_store_to',on_delete=models.CASCADE,blank=True,null=True)
    materials=models.ForeignKey(Materials,verbose_name=_('原材料'),related_name='messtkdetials_materials',blank=True,null=True,on_delete=models.CASCADE)
    packaging=models.ForeignKey(Packaging,verbose_name=_('包装材料'),related_name='messtkdetials_packaging',blank=True,null=True,on_delete=models.CASCADE)
    semifinishedgoods=models.ForeignKey(SemiFinishedGoods,verbose_name=_('半成品'),related_name='messtkdetials_semifinishedgoods',blank=True,null=True,on_delete=models.CASCADE)     
    product=models.ForeignKey('product.Product',verbose_name=_('产成品'),related_name='messtkdetials_product',blank=True,null=True,on_delete=models.CASCADE)
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='messtkdetails_unit',on_delete=models.CASCADE)    
    qc=models.ForeignKey('company.User',verbose_name=_('质检员'),related_name='messtkdetails_user',blank=True,null=True,on_delete=models.CASCADE)
    qc_name=models.CharField(_('质检员姓名'),max_length=20,blank=True,null=True,db_index=True)
    qc_tm=models.DateTimeField(_('检验时间'), blank=True,null=True)
    qc_passed=models.BooleanField(_('质检通过'),blank=True,null=True)
    qc_results=models.JSONField(_('质检结论'),blank=True,null=True,default=dict)
    batch_no = models.CharField(_('生产批次'), blank=True, null=True, max_length=20) 
    qty=models.IntegerField(_('数量'))  
    dt = models.DateField(_('日期'), default=now, db_index=True,blank=True,null=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)
    is_delete = models.BooleanField(_('是否已删除'), blank=True, null=True, db_index=True, default=False)

    class Meta:
        verbose_name = _('MES进出存明细')
        verbose_name_plural = _('MES进出存明细')



#2022-04-21 促销物料
class Accessories(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='accessories_company', on_delete=models.CASCADE) 
    code=models.CharField(_('编码'),max_length=50,blank=True,null=True,db_index=True)
    name=models.CharField(_('名称'),max_length=100)
    unit=models.ForeignKey(Unit,verbose_name=_('单位'),related_name='accessories_unit',blank=True,null=True,on_delete=models.CASCADE)
    unit_name=models.CharField(_('单位名称'),max_length=50)
    remark=models.CharField(_('备注'),max_length=100,blank=True,null=True)    
    is_delete = models.BooleanField(_('是否已删除'), blank=True, null=True, db_index=True, default=False)
    tm = models.DateTimeField(_('时间'), default=now,blank=True,null=True)

    class Meta:
        verbose_name = _('促销物料')
        verbose_name_plural = _('促销物料')
        unique_together = ("company", "code")   


# 2022-07-21
class LabelBindPH(models.Model):
    """
    标签在线绑定-瓶盒
    """
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品'),blank=True,null=True,on_delete=models.CASCADE)    
    store=models.ForeignKey('inventory.Storehouse',verbose_name=_('仓库'),blank=True,null=True,on_delete=models.CASCADE)
    qc=models.CharField(_('质检'),max_length=20,blank=True,null=True)
    batch_no=models.CharField(_('生产批次'),max_length=20,blank=True,null=True)    
    prd_shop=models.CharField(_('车间'),max_length=20,blank=True,null=True)
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    prd_team=models.CharField(_('班组'),max_length=20,blank=True,null=True)    
    box_code=models.CharField(_('盒标'),max_length=20,db_index=True)
    bottle_code=models.CharField(_('瓶标'),max_length=20,db_index=True)
    bottle_cap_label=models.CharField(_('瓶盖码(原码)'),max_length=20,db_index=True,blank=True,null=True)
    bottle_cap_code=models.CharField(_('瓶盖码(解密码)'),max_length=20,db_index=True,blank=True,null=True)    
    is_delete=models.BooleanField(_('是否删除'),default=False,blank=True,null=True,db_index=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)   
    # 2023-10-17
    origin_adm_id=models.IntegerField(_('原产地adm_id'),blank=True,null=True,db_index=True)

    class Meta:
        verbose_name = _('瓶盒在线绑定')
        verbose_name_plural = _('瓶盒在线绑定')


class LabelBindHX(models.Model):
    """
    标签在线绑定-盒箱(箱箱)
    """
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品'),blank=True,null=True,on_delete=models.CASCADE)    
    store=models.ForeignKey('inventory.Storehouse',verbose_name=_('仓库'),blank=True,null=True,on_delete=models.CASCADE)
    qc=models.CharField(_('质检'),max_length=20,blank=True,null=True)
    batch_no=models.CharField(_('生产批次'),max_length=20,blank=True,null=True)    
    prd_shop=models.CharField(_('车间'),max_length=20,blank=True,null=True)
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    prd_team=models.CharField(_('班组'),max_length=20,blank=True,null=True)
    carton_code=models.CharField(_('箱标'),max_length=20,db_index=True)
    carton_related=models.CharField(_('关联箱标'),max_length=20,db_index=True,blank=True,null=True)
    box_code=models.CharField(_('盒标'),max_length=20,db_index=True) 
    is_delete=models.BooleanField(_('是否删除'),default=False,blank=True,null=True,db_index=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)
    inbound=models.BooleanField(_('是否已入库'),default=False,db_index=True)
    # 2023-10-17
    origin_adm_id=models.IntegerField(_('原产地adm_id'),blank=True,null=True,db_index=True)
    
    class Meta:
        verbose_name = _('盒箱(箱箱)在线绑定')
        verbose_name_plural = _('盒箱(箱箱)在线绑定')


class LabelBindPX(models.Model):
    """
    标签在线绑定-瓶箱(箱箱)
    """
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品'),blank=True,null=True,on_delete=models.CASCADE)    
    store=models.ForeignKey('inventory.Storehouse',verbose_name=_('仓库'),blank=True,null=True,on_delete=models.CASCADE)
    qc=models.CharField(_('质检'),max_length=20,blank=True,null=True)
    batch_no=models.CharField(_('生产批次'),max_length=20,blank=True,null=True)    
    prd_shop=models.CharField(_('车间'),max_length=20,blank=True,null=True)
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    prd_team=models.CharField(_('班组'),max_length=20,blank=True,null=True)
    carton_code=models.CharField(_('箱标'),max_length=20,db_index=True)
    carton_related=models.CharField(_('关联箱标'),max_length=20,db_index=True,blank=True,null=True)
    bottle_code=models.CharField(_('瓶标'),max_length=20,db_index=True) 
    bottle_cap_label=models.CharField(_('瓶盖码(原码)'),max_length=20,db_index=True,blank=True,null=True)
    bottle_cap_code=models.CharField(_('瓶盖码(解密码)'),max_length=20,db_index=True,blank=True,null=True)    
    is_delete=models.BooleanField(_('是否删除'),default=False,blank=True,null=True,db_index=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)
    inbound=models.BooleanField(_('是否已入库'),default=False,db_index=True)
    # 2023-10-17
    origin_adm_id=models.IntegerField(_('原产地adm_id'),blank=True,null=True,db_index=True)
    
    class Meta:
        verbose_name = _('瓶箱(箱箱)在线绑定')
        verbose_name_plural = _('瓶箱(箱箱)在线绑定')



# 2023-10-25
class BottlCap(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE,related_name='bottlecap_company')
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE,related_name='bottlecap_user')
    product=models.ForeignKey('product.Product',verbose_name=_('产品'),blank=True,null=True,on_delete=models.CASCADE,related_name='bottlecap_product')    
    store=models.ForeignKey('inventory.Storehouse',verbose_name=_('仓库'),blank=True,null=True,on_delete=models.CASCADE,related_name='bottlecap_store')
    qc=models.CharField(_('质检'),max_length=20,blank=True,null=True)
    batch_no=models.CharField(_('生产批次'),max_length=20,blank=True,null=True)    
    prd_shop=models.CharField(_('车间'),max_length=20,blank=True,null=True)
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    prd_team=models.CharField(_('班组'),max_length=20,blank=True,null=True) 
    label_code=models.CharField(_('标签号码'),max_length=20,db_index=True)
    label_pk_id=models.BigIntegerField(_('主箱标id'),db_index=True,blank=True,null=True)
    label_pkg=models.SmallIntegerField(_('标签类型'),default=1)  
    cap=models.CharField(_('盖内码(原码)'),max_length=20,db_index=True,blank=True,null=True)
    outter=models.CharField(_('盖外码(解密码)'),max_length=20,db_index=True,blank=True,null=True)    
    is_delete=models.BooleanField(_('是否删除'),default=False,blank=True,null=True,db_index=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)
    
    class Meta:
        verbose_name = _('瓶盖在线绑定')
        verbose_name_plural = _('瓶盖在线绑定')


# 2023-11-22
class LabelBindDuplicate(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE,related_name='labelbindduplicate_company')
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE,related_name='labelbindduplicate_user')
    portal=models.CharField(_('接口'),max_length=10,db_index=True)   
    label_parent=models.CharField(_('父标签'),max_length=20,db_index=True)
    label_child=models.CharField(_('子标签'),max_length=20,db_index=True)    
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)
    duplications=models.JSONField(_('重复数据'),default=list)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('重复绑定记录')
        verbose_name_plural=_('重复绑定记录')


# 2023-11-23
class BottlecapProduct(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE,related_name='bottlecapproduct_company')
    product=models.OneToOneField('product.Product',verbose_name=_('产品名称'),on_delete=models.CASCADE,related_name='bottlecapproduct_product')
    cap_cnt=models.SmallIntegerField(_('每箱瓶(盖)数'))
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('绑瓶盖产品')
        verbose_name_plural=_('绑瓶盖产品')

    


# 2023-12-21 考虑所有企业瓶盖码都放到这个表里做唯一性检查
class BottlecapUnique(models.Model):
    company_id=models.IntegerField(_('企业id'),db_index=True)
    box_code=models.CharField(_('主箱标'),max_length=20,db_index=True)
    bottle_code=models.CharField(_('盒/瓶标'),max_length=20,db_index=True,blank=True,null=True)
    cap=models.CharField(_('盖内码'),max_length=20,db_index=True)    
    dt = models.DateField(_('绑定日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('绑定时间'), default=now,blank=True)

    class Meta:
        verbose_name=_('瓶盖')
        verbose_name_plural=_('瓶盖绑定检查') 


class BottlecapDuplication(models.Model):
    company_id=models.IntegerField(_('企业id'),db_index=True)
    box_code=models.CharField(_('主箱标'),max_length=20,db_index=True)
    bottle_code=models.CharField(_('盒/瓶标'),max_length=20,db_index=True,blank=True,null=True)
    cap=models.CharField(_('盖内码'),max_length=20,db_index=True)   
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    product=models.CharField(_('产品'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('绑定日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('绑定时间'), default=now,blank=True)

    class Meta:
        verbose_name=_('瓶/盖重复记录')
        verbose_name_plural=_('瓶/盖重复记录')  

# 2024-05-20
class LabelBindXG(models.Model):
    """
    标签在线绑定-箱盖：通过瓶盖码，实现箱和瓶的绑定关系
    """
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品'),blank=True,null=True,on_delete=models.CASCADE)
    store=models.ForeignKey('inventory.Storehouse',verbose_name=_('仓库'),blank=True,null=True,on_delete=models.CASCADE)
    qc=models.CharField(_('质检'),max_length=20,blank=True,null=True)
    batch_no=models.CharField(_('生产批次'),max_length=20,blank=True,null=True)
    prd_shop=models.CharField(_('车间'),max_length=20,blank=True,null=True)
    prd_line=models.CharField(_('产线'),max_length=20,blank=True,null=True)
    prd_team=models.CharField(_('班组'),max_length=20,blank=True,null=True)
    carton_code=models.CharField(_('箱标'),max_length=20,db_index=True)
    carton_related=models.CharField(_('关联箱标'),max_length=20,db_index=True,blank=True,null=True)
    bottle_cap_label=models.CharField(_('瓶盖码(原码)'),max_length=20,db_index=True,blank=True,null=True)
    bottle_cap_code=models.CharField(_('瓶盖码(解密码)'),max_length=20,db_index=True,blank=True,null=True)
    outter = models.CharField(_('盖外码(解密码)'), max_length=20, db_index=True, blank=True, null=True)
    is_delete=models.BooleanField(_('是否删除'),default=False,blank=True,null=True,db_index=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), default=now, db_index=True)
    tm = models.DateTimeField(_('时间'), default=now,blank=True)

    class Meta:
        verbose_name = _('箱盖')
        verbose_name_plural = _('箱盖')