from django.db import models

# Create your models here.
from company.models import *
from django.utils.translation import gettext_lazy as _



class Category(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='category_company',on_delete=models.PROTECT)    
    name=models.CharField(_('类别名称'),max_length=50)
    sn=models.SmallIntegerField(_('序号'),default=0)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)

    class Meta:
        verbose_name = _('用酒类别')
        verbose_name_plural = _('用酒类别')

    def __str__(self):
        return self.name



class EntApplication(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='entapp_company',on_delete=models.PROTECT)
    #2021-07-14 招待用酒也用于经销商
    agent = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='entapp_agent',on_delete=models.PROTECT,blank=True,null=True)
    category=models.ForeignKey(Category,verbose_name=_('用酒类别'),on_delete=models.PROTECT,default=4)
    #2021-07-14 招待用酒也用于经销商
    department=models.ForeignKey('company.Department',verbose_name=_('部门名称'),on_delete=models.PROTECT,blank=True,null=True)
    subject=models.CharField(_('接待对象'),max_length=50)
    headcount=models.IntegerField(_('接待人数'))
    reason=models.CharField(_('接待原因'),max_length=100)
    location=models.CharField(_('接待地点'),max_length=100)
    #2021-07-14 招待用酒也用于经销商
    staff=models.ForeignKey('company.Staff',verbose_name=_('经办人'),related_name='entapp_staff',on_delete=models.PROTECT,blank=True,null=True)    
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),related_name='entapp_user',on_delete=models.PROTECT)
    aprv_status=models.IntegerField(_('审批状态'),blank=True,null=True)
    aprv_department=models.BooleanField(_('部门负责人'),default=False,blank=True,null=True)
    aprv_deputy=models.BooleanField(_('分管领导'),default=False,blank=True,null=True)
    aprv_gm=models.BooleanField(_('总经理'),default=False,blank=True,null=True)    
    qty_box=models.IntegerField(_('箱数'),default=0,blank=True,null=True)
    qty_bottle=models.IntegerField(_('瓶数'),default=0,blank=True,null=True)
    cnt_bottles=models.IntegerField(_('合计瓶数'),default=0,blank=True,null=True)
    #2021-06-29
    user_confirmed=models.ForeignKey('company.User', verbose_name=_('确认人'),blank=True,null=True,related_name='entapp_cfm_user',on_delete=models.PROTECT)
    tm_confirmed=models.DateTimeField(_('确认时间'),blank=True,null=True)
    log_print=models.JSONField(_('打印记录'),blank=True,null=True,default=list)
    dt=models.DateField(_('创建日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('创建时间'),auto_now_add=True)    
    is_writeoff=models.BooleanField(_('是否已核销'),default=False)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    is_closed=models.BooleanField(_('已关闭'),default=False,blank=True,null=True)
    schema_no=models.CharField(_('总案流水号'),max_length=100,blank=True,null=True)
   

    class Meta:
        verbose_name = _('用酒审批单')
        verbose_name_plural = _('用酒审批单')

    def set_cnt_bottles(self):
        cnt=0
        for p in self.entproduct_app.all():
            cnt=cnt+p.qty_x*p.product.qty_p+p.qty_p
        self.cnt_bottles=cnt
        self.save()
    
    def check_writeoff(self):
        cnt=0
        for w in self.entwriteoff_app.all():
            cnt=cnt+1
        if cnt>=self.cnt_bottles:
            self.is_writeoff=True            
            self.save()



class EntAppProduct(models.Model):
    entapplication=models.ForeignKey(EntApplication,verbose_name=_('用酒审批单'),related_name='entproduct_app',on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品名称'),on_delete=models.CASCADE)   
    qty_x=models.IntegerField(_('箱数'))
    qty_p=models.IntegerField(_('瓶数'))

    class Meta:
        verbose_name = _('用酒单产品')
        verbose_name_plural = _('用酒单产品')       
    
    def get_bottles(self):
        cnt=0
        if self.qty_x:
            cnt=cnt+self.qty_x*self.product.qty_p
        if self.qty_p:
            cnt=cnt+self.qty_p
        return cnt

class EntAppAprv(models.Model):
    entapplication=models.ForeignKey(EntApplication,verbose_name=_('用酒审批单'),related_name='entappaprv_app',on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('审批人'),related_name='entappaprv_user',on_delete=models.CASCADE)
    position=models.CharField(_('职务'),max_length=20,blank=True,null=True)
    dt=models.DateField(_('创建日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('创建时间'),auto_now_add=True)      
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    
    class Meta:
        verbose_name = _('用酒单审批记录')
        verbose_name_plural = _('用酒单审批记录')     


class ActApplication(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'), related_name='actapp_company',on_delete=models.PROTECT)
    category=models.ForeignKey(Category,verbose_name=_('用酒类别'),related_name='actapp_category',on_delete=models.PROTECT,default=5)
    is_gift=models.BooleanField(_('是否为赠品核销'),blank=True,null=True,default=False)
    schema_no=models.CharField(_('总案流水号'),max_length=100,blank=True,null=True)
    app_no=models.CharField(_('申请单流水号'),max_length=30,blank=True,null=True)
    subject=models.CharField(_('会议活动主题'),max_length=100,blank=True,null=True)
    hotel=models.CharField(_('招待酒店/餐厅'),max_length=100,blank=True,null=True)
    dinner_dt=models.DateField(_('用餐日期'),blank=True,null=True)
    dinner_address=models.CharField(_('酒店/餐厅地址'),max_length=100,blank=True,null=True)
    dinner_headcount=models.IntegerField(_('用餐人数'),blank=True,null=True)
    cellphone=models.CharField(_('联系电话'),max_length=20,blank=True,null=True)
    contact=models.CharField(_('联系人'),max_length=20,blank=True,null=True)
    invitee=models.CharField(_('招待对象'),max_length=50,blank=True,null=True)
    user=models.ForeignKey('company.User',verbose_name=_('提交人'),related_name='actapp_user',on_delete=models.PROTECT)    
    agent=models.ForeignKey('company.Company',verbose_name=_('申报经销商/战区分区'),related_name='actapplication_agent',blank=True,null=True,on_delete=models.PROTECT)
    division=models.ForeignKey('company.SalesDivision',verbose_name=_('所属战区'),blank=True,null=True,on_delete=models.PROTECT)
    subdivision=models.ForeignKey('company.SubSalDivision',verbose_name=_('所属分区'),blank=True,null=True,on_delete=models.PROTECT)
    parent=models.ForeignKey('company.Company',verbose_name=_('规属平台商/战区'),related_name='actapplication_parent',blank=True,null=True,on_delete=models.PROTECT)
    department_id=models.IntegerField(_('部门ID'),blank=True,null=True)       
    salesman=models.CharField(_('业务人员'),max_length=20,blank=True,null=True)
    salesman_id=models.IntegerField(_('业务人员'),blank=True,null=True)
    dt=models.DateField(_('创建日期'),auto_now_add=True,db_index=True)
    #2021-06-29
    user_confirmed=models.ForeignKey('company.User', verbose_name=_('确认人'),blank=True,null=True,related_name='actapp_cfm_user',on_delete=models.PROTECT)
    tm_confirmed=models.DateTimeField(_('确认时间'),blank=True,null=True)
    log_print=models.JSONField(_('打印记录'),blank=True,null=True,default=list)
    tm=models.DateTimeField(_('创建时间'),auto_now_add=True)   
    qty_box=models.IntegerField(_('箱数'),default=0,blank=True,null=True)
    qty_bottle=models.IntegerField(_('瓶数'),default=0,blank=True,null=True)
    cnt_bottles=models.IntegerField(_('合计瓶数'),default=0,blank=True,null=True)
    is_writeoff=models.BooleanField(_('是否已核销'),default=False)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)   
    is_closed=models.BooleanField(_('已关闭'),default=False,blank=True,null=True)


    class Meta:
        verbose_name = _('会议活动用酒申报')
        verbose_name_plural = _('会议活动用酒申报')




    def set_cnt_bottles(self):
        cnt=0
        #for p in self.actproduct_app.all():
        #    cnt=cnt+p.qty_x*p.product.qty_p+p.qty_p
        #self.cnt_bottles=cnt

        #self.cnt_bottles=self.qty_box

        #self.save()

    def check_writeoff(self):
        cnt=0
        for w in self.entwriteoff_act.all():
            cnt=cnt+1
        if cnt>=self.cnt_bottles:
            self.is_writeoff=True            
            self.save()



class ActAppProduct(models.Model):
    actapplication=models.ForeignKey(ActApplication,verbose_name=_('会议活动用酒'),related_name='actproduct_app',on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品名称'),on_delete=models.CASCADE)   
    qty_x=models.IntegerField(_('箱数'))
    qty_p=models.IntegerField(_('瓶数'))

    class Meta:
        verbose_name = _('会议活动产品')
        verbose_name_plural = _('会议活动产品')       

   
#2021-05-07
class GiftApplication(models.Model):
    category=models.ForeignKey(Category,verbose_name=_('用酒类别'),on_delete=models.CASCADE,default=10)    
    code=models.CharField(_('单号/批次/总案流水号'),max_length=100)
    company = models.ForeignKey('company.Company', verbose_name=_('公司名称'),related_name='giftapp_company',on_delete=models.CASCADE)
    agent = models.ForeignKey('company.Company', verbose_name=_('公司名称'),related_name='giftapp_agent',on_delete=models.CASCADE,blank=True,null=True)    
    department=models.ForeignKey('company.Department',related_name='giftapp_deptment',verbose_name=_('部门名称'),on_delete=models.CASCADE,blank=True,null=True)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),related_name='giftapp_user',on_delete=models.CASCADE)
    actapplication=models.ForeignKey(ActApplication,verbose_name=_('会议活动'),related_name='giftapp_act',on_delete=models.CASCADE,blank=True,null=True)
    receiver=models.CharField(_('姓名'),max_length=20)
    cellphone=models.CharField(_('联系电话'),max_length=20)
    entity=models.CharField(_('单位名称'),max_length=50)
    #2021-06-29
    user_confirmed=models.ForeignKey('company.User', verbose_name=_('确认人'),blank=True,null=True,related_name='gftapp_cfm_user',on_delete=models.PROTECT)
    tm_confirmed=models.DateTimeField(_('确认时间'),blank=True,null=True)
    log_print=models.JSONField(_('打印记录'),blank=True,null=True,default=list)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True)
    is_closed=models.BooleanField(_('已关闭'),default=False,blank=True,null=True)
    dt=models.DateField(_('创建日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('创建时间'),auto_now_add=True)     
    schema_no=models.CharField(_('总案流水号'),max_length=100,blank=True,null=True)
    
    class Meta:
        verbose_name= _('赠品申报')
        verbose_name_plural = _('赠品申报')


class EntWriteoff(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    entapplication=models.ForeignKey(EntApplication,verbose_name=_('用酒审批单'),related_name='entwriteoff_app',on_delete=models.CASCADE,blank=True,null=True)
    actapplication=models.ForeignKey(ActApplication,verbose_name=_('会议活动用酒'),related_name='entwriteoff_act',on_delete=models.CASCADE,blank=True,null=True)
    giftapplication=models.ForeignKey(GiftApplication,verbose_name=_('礼品申报'),related_name='entwriteoff_gift',on_delete=models.CASCADE,blank=True,null=True)    
    category_id=models.IntegerField(_('用酒类别'),blank=True,null=True)
    label=models.CharField(_('标签号码'),max_length=20,db_index=True)
    label_code=models.CharField(_('物流号码'),max_length=20,db_index=True,unique=True)
    package_id=models.SmallIntegerField(_('标签类型'),default=1,db_index=True)
    box_code=models.CharField(_('主箱标'),max_length=20,blank=True,null=True)
    label_pk_id=models.BigIntegerField(_('主箱标ID'),blank=True,null=True)
    is_bottlecap=models.BooleanField(_('是否为瓶盖码'),default=False,blank=True,null=True)
    is_gift=models.BooleanField(_('是否为赠品'),default=False,blank=True,null=True)
    product_id=models.IntegerField(_('产品ID'),blank=True,null=True)
    department_id=models.IntegerField(_('部门ID'),blank=True,null=True)
    agent_id=models.IntegerField(_('经销商/战区分区'),blank=True,null=True)
    parent_id=models.IntegerField(_('规属平台商/战区'),blank=True,null=True)
    division_id=models.IntegerField(_('战区ID'),blank=True,null=True)
    subdivision_id=models.IntegerField(_('分区ID'),blank=True,null=True)
    user=models.ForeignKey('company.User',verbose_name=_('核销人'),related_name='entappwriteoff_user',on_delete=models.PROTECT)
    dt=models.DateField(_('创建日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('创建时间'),auto_now_add=True)     
    address=models.CharField(_('扫码地址'),max_length=200,blank=True,null=True)
    
    class Meta:
        verbose_name = _('用酒销记录')
        verbose_name_plural = _('用酒核销记录')     

#class Tasting(models.Modeld):


    
