from django.db import models
from apps.utils.models import CommonBModel, BaseModel, CommonBDModel
from apps.mtm.models import Material


# Create your models here.
class Supplier(CommonBModel):
    """
    TN:供应商
    """
    name = models.CharField('供应商名称', max_length=50)
    number= models.CharField('供应商编号', max_length=20, null=True, blank=True)
    contact = models.CharField('联系人', max_length=20, default='', blank=True)
    contact_phone = models.CharField('联系电话', max_length=11, default='', blank=True)
    address = models.CharField('地址', max_length=200, default='', blank=True)
    can_outsource = models.BooleanField('是否可外协', default=False)


class PuPlan(CommonBModel):
    """
    TN:采购计划
    """
    PUPLAN_CREATE = 10
    PUPLAN_SUBMITED = 20
    PUPLAN_ORDERING = 30
    PUPLAN_ORDERED = 40
    PUPLAN_DONE = 50
    PUPLAN_STATES = (
        (PUPLAN_CREATE, '创建中'),
        (PUPLAN_SUBMITED, '已提交'),
        (PUPLAN_ORDERING, '下单中'),
        (PUPLAN_ORDERED, '下单完成'),
        (PUPLAN_DONE, '已完成'),
    )
    state = models.PositiveSmallIntegerField(
        '状态', choices=PUPLAN_STATES, default=10, help_text=str(PUPLAN_STATES))
    number = models.CharField('编号', max_length=20)
    name = models.CharField('名称', max_length=50, null=True, blank=True)
    submit_time = models.DateTimeField('提交时间', null=True, blank=True)
    submit_user = models.ForeignKey(
        'system.user', verbose_name='提交人', related_name='submit_user_puplan', on_delete=models.CASCADE, null=True, blank=True)
    total_price = models.DecimalField('总价', max_digits=14, decimal_places=2, null=True, blank=True)


class PuOrder(CommonBModel):
    """
    TN:采购订单
    """
    PUORDER_CREATE = 10
    PUORDER_SUBMITED = 20
    PUORDER_SHIP = 30
    PUORDER_DONE = 40
    PUORDER_STATES = (
        (PUORDER_CREATE, '创建中'),
        (PUORDER_SUBMITED, '已提交'),
        (PUORDER_SHIP, '到货中'),
        (PUORDER_DONE, '已完成'),
    )
    state = models.PositiveSmallIntegerField(
        '状态', choices=PUORDER_STATES, default=10, help_text=str(PUORDER_STATES))
    number = models.CharField('订单编号', max_length=20, null=True, blank=True)
    supplier = models.ForeignKey(
        Supplier, verbose_name='供应商', on_delete=models.CASCADE)
    delivery_date = models.DateField('截止到货日期', null=True, blank=True)
    submit_time = models.DateTimeField('提交时间', null=True, blank=True)
    submit_user = models.ForeignKey(
        'system.user', verbose_name='提交人', related_name='submit_user_puorder', on_delete=models.CASCADE, null=True, blank=True)
    materials = models.ManyToManyField(
        Material, verbose_name='多个物料', blank=True, through='pum.puorderitem', related_name='pu_order_materials')
    total_price = models.DecimalField('总价', max_digits=14, decimal_places=2, null=True, blank=True)


class PuOrderItem(BaseModel):
    """TN:物料信息表"""
    material = models.ForeignKey(
        Material, verbose_name='物料', on_delete=models.CASCADE, related_name='pu_orderitem_material')
    count = models.PositiveIntegerField('所需数量', default=0)
    delivered_count = models.PositiveIntegerField('已到货数量', default=0)
    unit_price = models.DecimalField('单价', max_digits=14, decimal_places=2, null=True, blank=True)
    total_price = models.DecimalField('总价', max_digits=14, decimal_places=2, null=True, blank=True)
    pu_order = models.ForeignKey(PuOrder, verbose_name='关联采购订单',
                                 on_delete=models.CASCADE, null=True, blank=True, related_name='item_puorder')


class PuPlanItem(CommonBDModel):
    """
    TN:采购计划明细(因为各部门填写所以需要belong_dept字段)
    """
    material = models.ForeignKey(
        Material, verbose_name='所需物料', on_delete=models.CASCADE)
    need_count = models.PositiveIntegerField('所属数量')
    need_date = models.DateField('需求日期')
    unit_price = models.DecimalField('单价', max_digits=14, decimal_places=2, null=True, blank=True)
    total_price = models.DecimalField('总价', max_digits=14, decimal_places=2, null=True, blank=True)
    note = models.TextField('备注', default='')
    pu_plan = models.ForeignKey(PuPlan, verbose_name='采购计划', on_delete=models.CASCADE,
                                null=True, blank=True, related_name='item_puplan')
    pu_order = models.ForeignKey(PuOrder, verbose_name='关联采购订单',
                                 on_delete=models.SET_NULL, null=True, blank=True, related_name='puplan_item_puorder')
