From e8802c09b2add64f031763a9bbcc039f906892c5 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 8 Apr 2024 16:30:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=9F=AB=E6=AD=A3mioitem=20count=5Fnoto?= =?UTF-8?q?k=E9=94=99=E8=AF=AF=E7=9A=84=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/inm/correct.py | 23 ++++++++++++++++++++++- apps/inm/filters.py | 4 +++- apps/inm/serializers.py | 7 +++++++ apps/inm/services.py | 37 ++++++++++++++++++++++++++++++------- apps/inm/views.py | 20 ++++---------------- 5 files changed, 66 insertions(+), 25 deletions(-) diff --git a/apps/inm/correct.py b/apps/inm/correct.py index c597f74f..4ff8fa9f 100644 --- a/apps/inm/correct.py +++ b/apps/inm/correct.py @@ -1,7 +1,11 @@ -from .models import MaterialBatch +from .models import MaterialBatch, MIOItem from apps.mtm.models import Material, Mgroup from apps.system.models import Dept from rest_framework.exceptions import ParseError +from django.db.models import F +from django.db import transaction +from .services import InmService + def correct_material_batch(): """矫正物料批次 """ @@ -23,3 +27,20 @@ def correct_material_batch(): mb.production_dept = p_dict[processId] mb.save() + +def correct_mb_count_notok(): + """矫正因count_notok未记录导致的错误数据 + """ + mis = MIOItem.objects.filter(mio__state=20, count_notok=0).exclude( + test_date=None, + count_notok=F('count_n_zw') + F('count_n_tw') + F('count_n_qp') + F('count_n_wq') + F('count_n_dl') + F('count_n_pb') + F('count_n_dxt') + F('count_n_js') + F('count_n_qx') + F('count_n_zz') + F('count_n_ysq') + F('count_n_hs') + F('count_n_b') + F('count_n_qt') + ) + for mi in mis: + count_notok = mi.count_n_zw + mi.count_n_tw + mi.count_n_qp + mi.count_n_wq + mi.count_n_dl + mi.count_n_pb + mi.count_n_dxt + mi.count_n_js + mi.count_n_qx + mi.count_n_zz + mi.count_n_ysq + mi.count_n_hs + mi.count_n_b + mi.count_n_qt + with transaction.atomic(): + MIOItem.objects.filter(id=mi.id).update(count_notok=count_notok) + # 先处理库存 + try: + InmService.update_mb_after_test() + except ParseError as e: + MIOItem.objects.filter(id=mi.id).update(test_date=None) \ No newline at end of file diff --git a/apps/inm/filters.py b/apps/inm/filters.py index ac89822e..82321bed 100644 --- a/apps/inm/filters.py +++ b/apps/inm/filters.py @@ -28,7 +28,9 @@ class MioFilter(filters.FilterSet): 'state': ["exact", "in"], "type": ["exact", "in"], "pu_order": ["exact"], - "order": ["exact"] + "order": ["exact"], + "item_mio__test_date": ["isnull"], + "item_mio__test_user": ["isnull"], } def filter_materials__type(self, queryset, name, value): diff --git a/apps/inm/serializers.py b/apps/inm/serializers.py index a86fffe5..025488c4 100644 --- a/apps/inm/serializers.py +++ b/apps/inm/serializers.py @@ -256,6 +256,13 @@ class MIOItemTestSerializer(CustomModelSerializer): 'test_user': {'required': True} } + def validate(self, attrs): + count_notok = 0 + for i in attrs: + if 'count_n_' in i: + count_notok = count_notok + attrs[i] + attrs['count_notok'] = count_notok + return attrs class MioItemAnaSerializer(serializers.Serializer): start_date = serializers.DateField(label='开始日期', required=True) diff --git a/apps/inm/services.py b/apps/inm/services.py index c584161c..ce2ce8df 100644 --- a/apps/inm/services.py +++ b/apps/inm/services.py @@ -40,7 +40,16 @@ class InmService: do_in(instance) else: do_out(instance) - + + @classmethod + def cal_mat_count(cls, material: Material): + material_count = MaterialBatch.objects.filter( + material=material).aggregate(total=Sum('count'))['total'] + if material_count is None: + material_count = 0 + Material.objects.filter(id=material.id).update( + count=material_count) + @classmethod def update_mb(cls, instance: MIO, in_or_out: int = 1): """ @@ -84,13 +93,27 @@ class InmService: mb.save() else: raise ParseError('不支持的操作') - material_count = MaterialBatch.objects.filter( - material=material).aggregate(total=Sum('count'))['total'] - if material_count is None: - material_count = 0 - Material.objects.filter(id=material.id).update( - count=material_count) + cls.cal_mat_count(material) + @classmethod + def update_mb_after_test(cls, ins: MIOItem): + count_notok = ins.count_notok + batch = ins.batch + material = ins.material + warehouse = ins.warehouse + try: + mb = MaterialBatch.objects.get( + material=material, batch=batch, warehouse=warehouse) + count_new = mb.count - count_notok + if count_new < 0: + raise ParseError('库存扣减失败,请确认!') + mb.count = count_new + mb.save() + except MaterialBatch.DoesNotExist: + # 库存不存在已被消耗了 + if count_notok != 0: + raise ParseError('库存已全消耗!') + cls.cal_mat_count(material) def daoru_mb(path: str): """ diff --git a/apps/inm/views.py b/apps/inm/views.py index 7a906f6a..e2c58abc 100644 --- a/apps/inm/views.py +++ b/apps/inm/views.py @@ -244,28 +244,16 @@ class MIOItemViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin 半成品检验 """ ins: MIOItem = self.get_object() + mio = ins.mio if ins.test_date: raise ParseError('该明细已检验') + if mio.state != MIO.MIO_SUBMITED: + raise ParseError('该出入库记录还未提交') sr = MIOItemTestSerializer(instance=ins, data=request.data) sr.is_valid(raise_exception=True) sr.save() # 开始变动库存 - count_notok = ins.count_notok - batch = ins.batch - material = ins.material - warehouse = ins.warehouse - try: - mb = MaterialBatch.objects.get( - material=material, batch=batch, warehouse=warehouse) - count_new = mb.count - count_notok - if count_new < 0: - raise ParseError('库存扣减失败,请确认!') - mb.count = count_new - mb.save() - except MaterialBatch.DoesNotExist: - # 库存不存在已被消耗了 - if count_notok != 0: - raise ParseError('库存已全消耗!') + InmService.update_mb_after_test(ins) return Response() @action(methods=['post'], detail=True, perms_map={'post': 'mioitem.test'}, serializer_class=MIOItemPurInTestSerializer)