from django.db import models


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

class ContractType(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    code=models.CharField(_('编码'),max_length=10,blank=True,null=True)
    name=models.CharField(_('名称'),max_length=50)

    class Meta:
        verbose_name = _('合同类型')
        verbose_name_plural = _('合同类型')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.name   


class InvoiceType(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    code=models.CharField(_('编码'),max_length=10,blank=True,null=True)
    name=models.CharField(_('名称'),max_length=50)

    class Meta:
        verbose_name = _('发票类别')
        verbose_name_plural = _('发票类别')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.name

class OrderType(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    code=models.CharField(_('编码'),max_length=10,blank=True,null=True)
    name=models.CharField(_('名称'),max_length=50)

    class Meta:
        verbose_name = _('订单类别')
        verbose_name_plural = _('订单类别')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.name


#只写入必要的映射对像
class ObjCls(models.Model):
    app_name=models.CharField(_('app名称'),max_length=30)
    app_description=models.CharField(_('app说明'),max_length=50,blank=True,null=True)
    model_name=models.CharField(_('model名称'),max_length=30)
    model_description=models.CharField(_('model说明'),max_length=50,blank=True,null=True)
    obj_name=models.CharField(_('对像名称'),max_length=30)
    obj_description=models.CharField(_('对像说明'),max_length=50,blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name = _('对像映射表')
        verbose_name_plural = _('对像映射表')
        unique_together = ('app_name', 'obj_name')

    def __str__(self):
        return self.obj_name


class FinaceAccount(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    financecat=models.SmallIntegerField(_('科目分类'),choices=(
        (1,'资产类'),
        (2,'负债类'),
        (3,'共同类'),
        (4,'所有者权益类'),
        (5,'成本类'),
        (6,'损益类'),
    ))   
    code=models.CharField(_('编码'),max_length=10,blank=True,null=True)    
    name=models.CharField(_('名称'),max_length=30)

    class Meta:
        verbose_name = _('会计科目')
        verbose_name_plural = _('会计科目')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.obj_name



class DiscountItem(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    objcls=models.ForeignKey(ObjCls,verbose_name=_('对像映射'),on_delete=models.CASCADE)
    code=models.CharField(_('编码'),max_length=10,blank=True,null=True)
    name=models.CharField(_('名称'),max_length=50)

    class Meta:
        verbose_name = _('折扣项目')
        verbose_name_plural = _('折扣项目')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.name


class SalesOrder(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    code=models.CharField(_('订单编码'),max_length=50,blank=True,null=True) 
    customer_id=models.IntegerField(_('客户ID'),blank=True,null=True)
    customer_code=models.CharField(_('客户编码'),blank=True,null=True,max_length=100) 
    customer_name=models.CharField(_('客户名称'),blank=True,null=True,max_length=100)      
    ordertype=models.ForeignKey(OrderType,verbose_name=_('订单类别'),blank=True,null=True,on_delete=models.CASCADE)
    ordertype_code=models.CharField(_('订单类别编码'),blank=True,null=True,max_length=20)
    ordertype_name=models.CharField(_('订单类别名称'),blank=True,null=True,max_length=50)
    invoicetype=models.ForeignKey(InvoiceType,verbose_name=_('发票类别'),blank=True,null=True,on_delete=models.CASCADE)
    invoicetype_code=models.CharField(_('发票类别编码'),blank=True,null=True,max_length=20)
    invoicetype_name=models.CharField(_('发票类别名称'),blank=True,null=True,max_length=50)
    contract_id=models.IntegerField(_('合同ID'),blank=True,null=True)
    contract_code=models.CharField(_('合同编码'),blank=True,null=True,max_length=30)    
    contracttype=models.ForeignKey(ContractType,verbose_name=_('合同类型'),blank=True,null=True,on_delete=models.CASCADE)
    contracttype_code=models.CharField(_('合同类别编码'),blank=True,null=True,max_length=20)
    contracttype_name=models.CharField(_('合同类别名称'),blank=True,null=True,max_length=50)     
    delivery_address=models.CharField(_('送货地点'),max_length=100,blank=True,null=True)
    delivery_method=models.CharField(_('提货方式'),max_length=50,blank=True,null=True)
    delivery_date=models.DateField(_('期望交货日期'),blank=True,null=True)
    invoice_address=models.CharField(_('收票地址'),max_length=50,blank=True,null=True)
    contacts=models.CharField(_('联系人'),max_length=20,blank=True,null=True)
    cellphone=models.CharField(_('联系电话'),max_length=20,blank=True,null=True)    
    salesmen=models.JSONField(_('销售员'),blank=True,null=True,default=list)    
    dt=models.DateField(_('日期'), auto_now_add=True)
    tm=models.DateTimeField(_('时间'), auto_now_add=True)    
    #2022-01-11
    edit_logs=models.JSONField(_('修改日志'),blank=True,null=True,default=list)
    remark=models.CharField(_('备注说明'),max_length=100,blank=True,null=True)
    #2022-02-21 如内参喷码信息
    marking=models.CharField(_('喷码信息'),max_length=100,blank=True,null=True)

    class Meta:
        verbose_name = _('销售订单')
        verbose_name_plural = _('销售订单')
        unique_together = ('company_id', 'code')

    def __str__(self):
        return self.name
    

class SalesOrderDetails(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    salesorder=models.ForeignKey(SalesOrder,verbose_name=_('销售订单'),on_delete=models.CASCADE)
    ordertype=models.ForeignKey(OrderType,verbose_name=_('订单类别'),blank=True,null=True,on_delete=models.CASCADE)
    ordertype_code=models.CharField(_('订单类别编码'),blank=True,null=True,max_length=20)
    ordertype_name=models.CharField(_('订单类别名称'),blank=True,null=True,max_length=50)
    product=models.ForeignKey('product.Product',verbose_name=_('对应产品ID'),blank=True,null=True,on_delete=models.CASCADE)
    product_code=models.CharField(_('产品编码'),max_length=10,blank=True,null=True)
    product_name=models.CharField(_('产品名称'),max_length=50)
    product_aging=models.CharField(_('产品年份/库龄'),max_length=20,blank=True,null=True)
    store_name=models.CharField(_('仓库名称'),max_length=50,blank=True,null=True)
    store_code=models.CharField(_('仓库编码'),max_length=50,blank=True,null=True)
    store_id=models.IntegerField(_('仓库ID'),blank=True,null=True)
    qty=models.IntegerField(_('数量'))
    unit_id=models.IntegerField(_('计量单位ID'),blank=True,null=True)
    unit_name=models.CharField(_('单位名称'),max_length=20,blank=True,null=True)    
    qty0=models.IntegerField(_('最小计量单位的数量'),default=0)
    tax_rate=models.DecimalField(_('税率'),max_digits=10,decimal_places=4,blank=True,null=True)
    price=models.DecimalField(_('销售单价'),max_digits=10,decimal_places=4,blank=True,null=True)   
    price_discount=models.DecimalField(_('折扣单价'),max_digits=10,decimal_places=4,blank=True,null=True)    
    price_strategy=models.DecimalField(_('战略价'),max_digits=10,decimal_places=4,blank=True,null=True)  
    price_invoice=models.DecimalField(_('开票单价'),max_digits=10,decimal_places=4,blank=True,null=True) 
    amt_total=models.DecimalField(_('总金额'),max_digits=14,decimal_places=4,blank=True,null=True)
    amt_invoice=models.DecimalField(_('开票总金额'),max_digits=14,decimal_places=4,blank=True,null=True)        
    discountitem=models.ForeignKey(DiscountItem,verbose_name=_('折扣项目'),blank=True,null=True,on_delete=models.CASCADE)
    discountitem_code=models.CharField(_('折扣项目编码'),max_length=20,blank=True,null=True)
    discountitem_name=models.CharField(_('折扣项目名称'),max_length=50,blank=True,null=True)
    financeaccount=models.ForeignKey(FinaceAccount,verbose_name=_('对应财务费用科目'),blank=True,null=True,on_delete=models.CASCADE)
    financeaccount_code=models.CharField(_('费用科目编码'),max_length=20,blank=True,null=True)
    financeaccount_name=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)

    class Meta:
        verbose_name = _('销售订单明细')
        verbose_name_plural = _('销售订单明细')
        

    def __str__(self):
        return self.product_name


#2022-04-22
class SalesOrdeAccessories(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    salesorder=models.ForeignKey(SalesOrder,verbose_name=_('销售订单'),related_name='salesorer_accessories',on_delete=models.CASCADE)
    itype_id=models.IntegerField(_('物料类别ID'))   #inventory.models: InvType
    mat_id=models.IntegerField(_('物料ID'),blank=True,null=True)
    mat_name=models.CharField(_('物料名称'),max_length=50)
    unit_id=models.IntegerField(_('计量单位ID'),blank=True,null=True)
    unit_name=models.CharField(_('单位名称'),max_length=20,blank=True,null=True)    
    qty=models.DecimalField(_('数量'),max_digits=12,decimal_places=2) 
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name = _('销售订单辅料促销品')
        verbose_name_plural = _('销售订单辅料促销品')
        

    def __str__(self):
        return self.mat_name




#2022-01-12 销售订单发货（销售出库）
class SalesOrderDelivery(models.Model):
    company_id=models.IntegerField(_('企业ID'))
    salesorder=models.ForeignKey(SalesOrder,verbose_name=_('销售订单'),on_delete=models.CASCADE)
    ordertype=models.ForeignKey(OrderType,verbose_name=_('订单类别'),blank=True,null=True,on_delete=models.CASCADE)
    product=models.ForeignKey('product.Product',verbose_name=_('对应产品ID'),blank=True,null=True,on_delete=models.CASCADE)    
    batch=models.ForeignKey('inventory.Batch',verbose_name=_('销售出库batch'),on_delete=models.CASCADE)
    qty0=models.IntegerField(_('最小计量单位的数量'),default=0)
    dt=models.DateField(_('日期'), auto_now_add=True)
    tm=models.DateTimeField(_('时间'), auto_now_add=True)
    
    class Meta:
        verbose_name = _('销售订单销售出库明细')
        verbose_name_plural = _('销售订单销售出库明细')      


