diff --git a/apps/inm/models.py b/apps/inm/models.py index 584fdb3f..6477a693 100644 --- a/apps/inm/models.py +++ b/apps/inm/models.py @@ -5,7 +5,7 @@ from apps.sam.models import Customer, Order from apps.mtm.models import Material, Mgroup from apps.system.models import User from datetime import datetime -from django.db.models import Max +from django.db.models import Max, Sum # Create your models here. @@ -39,6 +39,10 @@ class MaterialBatch(BaseModel): defect = models.ForeignKey('qm.defect', verbose_name='缺陷', on_delete=models.PROTECT, null=True, blank=True) + @property + def count_mioing(self): + return MIOItem.objects.filter(mb=self, mio__submit_time__isnull=True).aggregate(count=Sum('count'))['count'] or 0 + class MaterialBatchA(BaseModel): """ TN:组合件物料批次 diff --git a/apps/inm/serializers.py b/apps/inm/serializers.py index 454695ab..8c6ad5b3 100644 --- a/apps/inm/serializers.py +++ b/apps/inm/serializers.py @@ -53,6 +53,12 @@ class MaterialBatchSerializer(CustomModelSerializer): model = MaterialBatch fields = '__all__' read_only_fields = EXCLUDE_FIELDS_BASE + + def to_representation(self, instance): + ret = super().to_representation(instance) + if 'count' in ret and 'count_mioing' in ret: + ret['count_canmio'] = str(Decimal(ret['count']) - Decimal(ret['count_mioing'])) + return ret class MaterialBatchDetailSerializer(CustomModelSerializer):