from apps.utils.models import CommonADModel, CommonBDModel, CommonAModel
from django.db import models

class AssetCate(CommonAModel):
    name = models.CharField('类别名称',max_length=50, unique=True)
    code = models.CharField('类别编码',max_length=50, unique=True, null=True, blank=True)
    default_unit = models.CharField('默认单位', max_length=20, default='台')

class Asset(CommonAModel):
    """
    TN:固定资产台账
    """
    card_number = models.CharField('卡片编号',max_length=50, unique=True, blank=True, null=True)
    name = models.CharField('固定资产名称',max_length=100)
    specification = models.CharField("规格型号", max_length=100, blank=True, null=True)
    cate = models.ForeignKey(AssetCate, verbose_name='资产类别', on_delete=models.PROTECT)
    quantity = models.PositiveIntegerField("数量", default=1)
    start_date = models.DateField("启用日期")
    canuse_year = models.PositiveSmallIntegerField("可用年限", null=True, blank=True)
    original_value = models.DecimalField('资产原值', max_digits=15, decimal_places=2)
    storage_location = models.CharField("存放地点", max_length=100, blank=True, null=True)
    keeper = models.ForeignKey('system.user', verbose_name='保管人', on_delete=models.SET_NULL, null=True, blank=True)
    keep_dept = models.ForeignKey('system.dept', verbose_name='保管部门', on_delete=models.SET_NULL, null=True, blank=True)
    state = models.CharField("使用状态", max_length=50, help_text="在用/闲置")
    supplier = models.ForeignKey('pum.supplier', verbose_name='供应商', on_delete=models.SET_NULL, null=True, blank=True)
    unit = models.CharField("计量单位", max_length=50, default='台')
    note = models.TextField("备注", blank=True, null=True)

class AssetLog(CommonADModel):
    """
    TN:资产操作日志
    """
    type = models.CharField("流水类型", max_length=50, help_text="入库/出库")
    keep_dept = models.ForeignKey('system.dept', verbose_name='保管部门', on_delete=models.SET_NULL, null=True, blank=True)
    keeper = models.ForeignKey('system.user', verbose_name='保管人', on_delete=models.SET_NULL, null=True, blank=True)
    start_date = models.DateField("启用日期", null=True, blank=True)
    items = models.JSONField(verbose_name='资产明细', default=list, null=True, blank=True)
    ticket = models.OneToOneField('wf.ticket', verbose_name='关联工单', 
        on_delete=models.PROTECT, related_name='assetlog_ticket', null=True, blank=True)