diff --git a/apps/inm/services.py b/apps/inm/services.py index f424aeab..f7617c04 100644 --- a/apps/inm/services.py +++ b/apps/inm/services.py @@ -247,10 +247,8 @@ class InmService: BatchLog.clear(mio=instance) else: for item in MIOItem.objects.filter(mio=instance): - if item.mb: # 说明录入到已有批次 - BatchSt.g_create(batch=item.batch) - else: - BatchSt.g_create(batch=item.batch, mio=instance, material_start=item.material) + BatchSt.g_create( + batch=item.batch, mio=instance, material_start=item.material, reuse_node=True) from apps.pum.services import PumService if is_reverse: cls.update_mb(instance, -1) @@ -262,10 +260,8 @@ class InmService: BatchLog.clear(mio=instance) else: for item in MIOItem.objects.filter(mio=instance): - if item.mb: - BatchSt.g_create(batch=item.batch) - else: - BatchSt.g_create(batch=item.batch, mio=instance, material_start=item.material) + BatchSt.g_create( + batch=item.batch, mio=instance, material_start=item.material, reuse_node=True) if is_reverse: cls.update_mb(instance, -1) else: diff --git a/apps/wpm/models.py b/apps/wpm/models.py index 486be373..c0e39396 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -616,18 +616,32 @@ class BatchSt(BaseModel): unique_together = [("batch", "version")] @classmethod - def g_create(cls, batch:str, mio=None, handover=None, mlog=None, material_start=None): + def g_create(cls, batch:str, mio=None, handover=None, mlog=None, material_start=None, reuse_node=False): """ 创建新的批次 """ if mio is None and handover is None and mlog is None: - return cls.objects.get_or_create(batch=batch, version=1) + try: + node = cls.objects.get(batch=batch) + except cls.DoesNotExist: + return cls.objects.create(batch=batch), True + except cls.MultipleObjectsReturned: + # 兼容性处理 + node = cls.objects.filter(batch=batch).order_by('-version').first() + return node, False else: version = 1 # 带有来源的批次获取,需检查批次号是否可用 - if cls.objects.filter(batch=batch, version=1).exists(): - latest_version = BatchSt.objects.filter(batch=batch).aggregate(Max("version"))["version__max"] - version = latest_version + 1 + if cls.objects.filter(batch=batch).exists(): + if reuse_node: + node = cls.objects.filter(batch=batch).order_by('-version').first() + if node.material_start is not None and node.material_start != material_start: + raise ParseError(f"{batch}-该批次号因物料不同不可引用") + return node, False + else: + raise ParseError(f"{batch}-该批次号不可使用") + # latest_version = BatchSt.objects.filter(batch=batch).aggregate(Max("version"))["version__max"] + # version = latest_version + 1 if mio is None and handover is None and mlog is None: raise ParseError("mio or handover or mlog must be provided") ins = cls.objects.create(batch=batch, mio=mio, handover=handover, mlog=mlog, material_start=material_start, version=version)