from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils.timezone import now
from django.contrib.postgres.fields import ArrayField



# Create your models here.
class XfBanquet(models.Model):
    subject = models.CharField(_('活动名称'), max_length=100)
    code = models.CharField(_('活动编码'), max_length=20, unique=True)
    department = models.IntegerField(_('归属战区'), blank=True, null=True)
    salesman = models.CharField(_('现场业务员'), max_length=50, blank=True, null=True)
    location = models.CharField(_('活动地点'), max_length=100, blank=True, null=True )
    scene_img = models.CharField(_('现场图片链接'), max_length=100, blank=True, null=True)
    host = models.CharField(_('主办人'), max_length=50, blank=True, null=True)
    host_tel = models.CharField(_('主办人电话'), max_length=20, blank=True, null=True)
    tm = models.DateTimeField(_('创建时间'), auto_now_add=True)
    remark = models.CharField(_('备注说明'), max_length=50, blank=True, null=True)

    def qty(self):
        return self.xfbqtdetails_set.all().count()

    class Meta:
        verbose_name = _('西凤婚宴升学宴活动')
        verbose_name_plural = _('西凤婚宴升学宴活动')


class XfBqtDetails(models.Model):
    bqt = models.ForeignKey(XfBanquet, verbose_name=_('归属活动'), on_delete=models.CASCADE)
    bottle_code = models.CharField(_('瓶标号码'), max_length=20)
    box_code = models.CharField(_('箱标号码'), max_length=20, blank=True, null=True)
    fhkey = models.CharField(_('发货KEY'), max_length=40, blank=True, null=True)
    delivery_details_id = models.IntegerField(_('发货明细ID'), blank=True, null=True)
    remark = models.CharField(_('备注说明'), max_length=50, blank=True, null=True)
    tm = models.DateTimeField(_('创建时间'), default=now)

    class Meta:
        verbose_name = _('西凤庆宴活动标签')
        verbose_name_plural = _('西凤庆宴活动标签')

    def __str__(self):
        return self.label_code


#class AgentWriteOff(models.Model):
#    activity_id=models.IntegerField(_('活动名称'), blank=True, null=True)







class Material(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('公司名称'), on_delete=models.CASCADE)
    code=models.CharField(_('物料编码'),max_length=10,blank=True,null=True)
    name=models.CharField(_('物料名称'),max_length=100)
    specification=models.CharField(_('规格型号'),max_length=50,blank=True,null=True)
    unit=models.CharField(_('单位'),max_length=20,blank=True,null=True)
    remark=models.CharField(_('备注'),max_length=100,blank=True,null=True)

    class Meta:
        verbose_name=_('促销物料')
        verbose_name_plural=_('促销物料')
        unique_together = ('company', 'name')
        ordering = ['name']

    def __str__(self):
        return self.name


class GiftsStatus(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('公司名称'), on_delete=models.CASCADE,blank=True,null=True)
    name=models.CharField(_('状态名'),max_length=20)
    sn=models.SmallIntegerField(_('序号'),blank=True,null=True)
    action=models.CharField(_('操作'),blank=True,null=True,max_length=50)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('礼品核销状态')
        verbose_name_plural=_('礼品核销状态')
    


class GiftsScheme(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('公司名称'), on_delete=models.CASCADE)    
    code=models.CharField(_('方案编号'),max_length=100,blank=True,null=True)
    name=models.CharField(_('方案名称'),max_length=100)
    scope=models.CharField(_('有效范围'),max_length=100,blank=True,null=True)
    region=models.CharField(_('地区'),max_length=100,blank=True,null=True)
    div_ids=ArrayField(models.IntegerField(_('战区')),blank=True,null=True)
    subdiv_ids=ArrayField(models.IntegerField(_('分区')),blank=True,null=True)
    agent_type=models.CharField(_('客户类型'),max_length=100)
    condition=models.CharField(_('发放条件'),max_length=100)    
    means=models.SmallIntegerField(_('发放方式'),default=1,choices=(
        (1,_('总部配送')),
        (2,_('大区分区提交申请')),        
    ))
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)  
    tm = models.DateTimeField(_('创建时间'), auto_now_add=True)
    tm_app_latest=models.DateTimeField(_('最晚申请时间'),blank=True,null=True)  
    
    class Meta:
        verbose_name=_('促销礼品方案')
        verbose_name_plural=_('促销礼品方案')
        

    def __str__(self):
        return self.name

        


class SchemeProducts(models.Model):    
    scheme=models.ForeignKey(GiftsScheme,verbose_name=_('促销礼品方案'),  on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('促销礼品酒'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))
    package_id=models.SmallIntegerField(_('单位'),choices=(
        (3,_('箱')),
        (1,_('瓶'))
    ))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    class Meta:
        verbose_name=_('方案礼品酒明细')
        verbose_name_plural=_('方案礼品酒明细')


class SchemeMaterials(models.Model):
    scheme=models.ForeignKey(GiftsScheme,verbose_name=_('促销礼品方案'),  on_delete=models.CASCADE)
    material=models.ForeignKey(Material,verbose_name=_('物料名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    
    class Meta:
        verbose_name=_('方案促销物料明细')
        verbose_name_plural=_('方案促销物料明细')


class GiftsApplication(models.Model):    
    scheme=models.ForeignKey(GiftsScheme,verbose_name='礼品促销方案',on_delete=models.CASCADE)
    company=models.ForeignKey('company.Company',verbose_name=_('公司名称'), on_delete=models.CASCADE)
    ancestor_id=models.IntegerField(_('族系'),default=1644)
    reason=models.CharField('申请理由',max_length=100)
    approved_by=models.ForeignKey('company.User',verbose_name=_('批准人'),on_delete=models.CASCADE,related_name='approved_by',blank=True,null=True)
    tm_approved=models.DateTimeField(_('批准时间'),blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)  
    tm = models.DateTimeField(_('创建时间'), auto_now_add=True)
    
    class Meta:
        verbose_name=_('礼品申请单')
        verbose_name_plural=_('礼品申请单')
   

class ApplicationProducts(models.Model):    
    application=models.ForeignKey(GiftsApplication,verbose_name=_('礼品申请单'),  on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))
    package_id=models.SmallIntegerField(_('单位'),choices=(
        (3,_('箱')),
        (1,_('瓶'))
    ))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    class Meta:
        verbose_name=_('申请单礼品酒明细')
        verbose_name_plural=_('申请单礼品酒明细')




class ApplicationMaterials(models.Model):
    application=models.ForeignKey(GiftsApplication,verbose_name=_('礼品申请单'),  on_delete=models.CASCADE)
    material=models.ForeignKey(Material,verbose_name=_('物料名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    
    class Meta:
        verbose_name=_('申请单物料明细')
        verbose_name_plural=_('申请单物料明细')

    
    

class GiftsRequisition(models.Model):
    scheme=models.ForeignKey(GiftsScheme,verbose_name='礼品促销方案',on_delete=models.CASCADE)
    company=models.ForeignKey('company.Company',verbose_name=_('公司名称'), on_delete=models.CASCADE)
    ancestor_id=models.IntegerField(_('族系'),default=1644)
    status=models.ForeignKey(GiftsStatus,verbose_name=_('领用状态'),default=1, on_delete=models.CASCADE)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    agent=models.ForeignKey('company.Company',verbose_name=_('经销商'),related_name='requisition_agent',on_delete=models.CASCADE)
    salesman=models.ForeignKey('company.User',verbose_name=_('业务员'),related_name='requisition_salesman',blank=True,null=True,on_delete=models.CASCADE)
    saldiv=models.ForeignKey('company.SalesDivision',verbose_name=_('营销战区'),blank=True,null=True,on_delete=models.CASCADE)
    subdiv=models.ForeignKey('company.SubSalDivision',verbose_name=_('营销分区'),blank=True,null=True,on_delete=models.CASCADE)
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)  
    tm = models.DateTimeField(_('创建时间'), auto_now_add=True)
    
    class Meta:
        verbose_name=_('礼品领用单')
        verbose_name_plural=_('礼品领用单')

     

class RequisitionProducts(models.Model):    
    requisition=models.ForeignKey(GiftsRequisition,verbose_name=_('礼品申请单'),  on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('产品名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))
    package_id=models.SmallIntegerField(_('单位'),choices=(
        (3,_('箱')),
        (1,_('瓶'))
    ))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    class Meta:
        verbose_name=_('领用单礼品酒明细')
        verbose_name_plural=_('领用单礼品酒明细')



class RequisitionMaterials(models.Model):
    requisition=models.ForeignKey(GiftsRequisition,verbose_name=_('礼品申请单'),  on_delete=models.CASCADE)
    material=models.ForeignKey(Material,verbose_name=_('物料名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    
    class Meta:
        verbose_name=_('领用单物料明细')
        verbose_name_plural=_('领用单物料明细')




class RequisitionStatusLog(models.Model):
    requisition=models.ForeignKey(GiftsRequisition,verbose_name=_('礼品领用单'),on_delete=models.CASCADE)
    status=models.ForeignKey(GiftsStatus,verbose_name=_('核销状态'),on_delete=models.CASCADE)   
    user=models.ForeignKey('company.User',verbose_name=_('操作员'),on_delete=models.CASCADE)
    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 RequisitionLogProduct(models.Model):
    requisitionlog=models.ForeignKey(RequisitionStatusLog,verbose_name=_('领用状态日志'),on_delete=models.CASCADE)
    status=models.ForeignKey(GiftsStatus,verbose_name=_('核销状态'),on_delete=models.CASCADE,blank=True,null=True)
    product=models.ForeignKey('product.Product',verbose_name=_('礼品酒名称'),on_delete=models.CASCADE,blank=True,null=True)      
    label_code=models.CharField(_('标签号码'),max_length=20)
    package_id=models.SmallIntegerField(_('标签类型'),blank=True,null=True)
    label_id=models.BigIntegerField(_('标签ID'),blank=True,null=True)
    label_pk_id=models.BigIntegerField(_('主标签ID'),blank=True,null=True)    
    dt = models.DateField(_('日期'), auto_now_add=True,db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)

    class Meta:
        verbose_name=_('领用日志产品明细')
        verbose_name_plural=_('领用日志产品明细')


class RequisitionLogMaterial(models.Model):
    requisitionlog=models.ForeignKey(RequisitionStatusLog,verbose_name=_('领用状态日志'),on_delete=models.CASCADE)
    status=models.ForeignKey(GiftsStatus,verbose_name=_('核销状态'),on_delete=models.CASCADE,blank=True,null=True) 
    material=models.ForeignKey(Material,verbose_name=_('物料名称'),on_delete=models.CASCADE)
    qty=models.IntegerField(_('数量'))   
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)
    dt = models.DateField(_('日期'), auto_now_add=True,db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)
    
    class Meta:
        verbose_name=_('领用日志物料明细')
        verbose_name_plural=_('领用日志物料明细')





'''
class Membership(models.Model):
    openid=models.CharField(_('微信openid'),max_length=40,unique=True)
    wxapp=models.ForeignKey('wx.WxApp',verbose_name=_('微信小程序/公众号'),default=1)
'''




