from apps.system.models import CommonADModel, CommonAModel
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.base import Model
import django.utils.timezone as timezone
from django.db.models.query import QuerySet


from utils.model import SoftModel, BaseModel
from apps.mtm.models import Material



class Customer(CommonAModel):
    """
    客户信息
    """
    name = models.CharField('客户名称', max_length=50, unique=True)
    address = models.CharField('详细地址', max_length=20, blank=True, null=True)
    contact = models.CharField('联系人', max_length=20)
    contact_phone = models.CharField('联系电话', max_length=11, unique=True,null=True)
    description = models.CharField('描述', max_length=200, blank=True, null=True)

    class Meta:
        verbose_name = '客户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Contract(CommonAModel):
    """
    合同信息
    """
    state_choices = (
         (0, '完好'),
        (1, '限用'),
        (2, '在修'),
         (3, '禁用')
    )
    name = models.CharField('合同名称', max_length=100)
    number = models.CharField('合同编号', max_length=100, unique=True)
    amount = models.IntegerField('合同金额', default=0)
    invoice = models.IntegerField('开票金额', default=0)
    #state = models.CharField('合同状态', choices= state_choices, max_length=20, default=1)
    customer = models.ForeignKey(Customer, verbose_name='关联客户', on_delete=models.CASCADE, related_name='contact_customer')
    sign_date = models.DateField('签订日期')
    description = models.CharField('描述', max_length=200, blank=True, null=True)
    
    class Meta:
        verbose_name = '合同信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Order(CommonAModel):
    """
    订单信息
    """
    number = models.CharField('订单编号', max_length=100, unique=True)
    customer = models.ForeignKey(Customer, verbose_name='客户', on_delete=models.CASCADE)
    contract = models.ForeignKey(Contract, verbose_name='所属合同', null=True, blank=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(Material, verbose_name='所需产品', on_delete=models.CASCADE)
    count = models.PositiveIntegerField('所需数量', default=0)
    planed_count = models.PositiveIntegerField('已排数量', default=0)
    delivered_count = models.PositiveIntegerField('已交货数量', default=0)
    delivery_date = models.DateField('交货日期')
    class Meta:
        verbose_name = '订单信息'
        verbose_name_plural = verbose_name
    

class Sale(CommonADModel):
    """
    销售记录
    """
    customer = models.ForeignKey(Customer, verbose_name='客户', on_delete=models.CASCADE)
    order = models.ForeignKey(Order, verbose_name='关联订单', on_delete=models.CASCADE, null=True, blank=True)
    product = models.ForeignKey(Material, verbose_name='所需产品', on_delete=models.CASCADE)
    count = models.PositiveIntegerField('交货数量', default=0)
    is_audited = models.BooleanField('是否审核', default=False)


class SaleProduct(BaseModel):
    """
    具体产品
    """
    sale = models.ForeignKey(Sale, verbose_name='关联销售记录', on_delete=models.CASCADE)
    number = models.CharField('物品编号', max_length=50)
    iproduct = models.ForeignKey('inm.iproduct', verbose_name='关联库存产品', on_delete=models.CASCADE, related_name='sale_iproduct')
    remark = models.TextField('备注', null=True, blank=True)

    class Meta:
        unique_together = (
            ('sale','iproduct'),  # 联合唯一
        )



