fix: 矫正mioitem count_notok错误的数据

This commit is contained in:
caoqianming 2024-04-08 16:30:28 +08:00
parent fe19847458
commit e8802c09b2
5 changed files with 66 additions and 25 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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)

View File

@ -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):
"""

View File

@ -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)