feat: 批次追踪链优化
This commit is contained in:
parent
89aca50f44
commit
17284dcf4f
|
@ -278,7 +278,7 @@ class InmService:
|
||||||
else:
|
else:
|
||||||
for item in MIOItem.objects.filter(mio=instance):
|
for item in MIOItem.objects.filter(mio=instance):
|
||||||
BatchSt.g_create(
|
BatchSt.g_create(
|
||||||
batch=item.batch, mio=instance, material_start=item.material, reuse_node=True)
|
batch=item.batch, mio=instance, material_start=item.material, check_mat_start=True)
|
||||||
from apps.pum.services import PumService
|
from apps.pum.services import PumService
|
||||||
if is_reverse:
|
if is_reverse:
|
||||||
cls.update_mb(instance, -1)
|
cls.update_mb(instance, -1)
|
||||||
|
@ -291,7 +291,7 @@ class InmService:
|
||||||
else:
|
else:
|
||||||
for item in MIOItem.objects.filter(mio=instance):
|
for item in MIOItem.objects.filter(mio=instance):
|
||||||
BatchSt.g_create(
|
BatchSt.g_create(
|
||||||
batch=item.batch, mio=instance, material_start=item.material, reuse_node=True)
|
batch=item.batch, mio=instance, material_start=item.material, check_mat_start=True)
|
||||||
if is_reverse:
|
if is_reverse:
|
||||||
cls.update_mb(instance, -1)
|
cls.update_mb(instance, -1)
|
||||||
else:
|
else:
|
||||||
|
@ -448,13 +448,16 @@ class InmService:
|
||||||
if mio.type == MIO.MIO_TYPE_PUR_IN:
|
if mio.type == MIO.MIO_TYPE_PUR_IN:
|
||||||
from apps.pum.services import PumService
|
from apps.pum.services import PumService
|
||||||
cls.update_mb_item(mioitem, -1)
|
cls.update_mb_item(mioitem, -1)
|
||||||
|
BatchLog.clear(mioitem=mioitem)
|
||||||
PumService.mio_purin(mio=mio, is_reverse=True, mioitem=mioitem)
|
PumService.mio_purin(mio=mio, is_reverse=True, mioitem=mioitem)
|
||||||
mioitem.delete()
|
mioitem.delete()
|
||||||
elif mio.type == MIO.MIO_TYPE_OTHER_IN:
|
elif mio.type == MIO.MIO_TYPE_OTHER_IN:
|
||||||
cls.update_mb_item(mioitem, -1)
|
cls.update_mb_item(mioitem, -1)
|
||||||
|
BatchLog.clear(mioitem=mioitem)
|
||||||
mioitem.delete()
|
mioitem.delete()
|
||||||
elif mio.type == MIO.MIO_TYPE_DO_OUT:
|
elif mio.type == MIO.MIO_TYPE_DO_OUT:
|
||||||
do_in(mioitem)
|
do_in(mioitem)
|
||||||
|
BatchLog.clear(mioitem=mioitem)
|
||||||
mioitem.delete()
|
mioitem.delete()
|
||||||
else:
|
else:
|
||||||
raise ParseError("不支持该出入库单明细撤销")
|
raise ParseError("不支持该出入库单明细撤销")
|
|
@ -14,6 +14,7 @@ from django.db.models import Count
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import Max
|
from django.db.models import Max
|
||||||
import re
|
import re
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
class SfLog(CommonADModel):
|
class SfLog(CommonADModel):
|
||||||
|
@ -655,45 +656,57 @@ class BatchSt(BaseModel):
|
||||||
unique_together = [("batch", "version")]
|
unique_together = [("batch", "version")]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def g_create(cls, batch:str, mio=None, mioitem=None, handover=None, mlog=None, material_start=None, reuse_node=False, exclude_batchst_ids=[]):
|
def g_create(cls, batch:str, mio=None, mioitem=None, handover=None, mlog=None, material_start=None, check_mat_start=False, exclude_batchst_ids=[]):
|
||||||
"""
|
"""
|
||||||
创建新的批次
|
创建新的批次
|
||||||
"""
|
"""
|
||||||
if mioitem:
|
if mioitem:
|
||||||
mio = mioitem.mio
|
mio = mioitem.mio
|
||||||
if mio is None and handover is None and mlog is None:
|
try:
|
||||||
try:
|
node = cls.objects.get(batch=batch, version=1)
|
||||||
node = cls.objects.exclude(id__in=exclude_batchst_ids).get(batch=batch)
|
if check_mat_start:
|
||||||
except cls.DoesNotExist:
|
if node.material_start is None:
|
||||||
return cls.objects.create(batch=batch), True
|
node.material_start = material_start
|
||||||
except cls.MultipleObjectsReturned:
|
node.save(update_fields = ["material_start"])
|
||||||
# 兼容性处理
|
if node.material_start != material_start:
|
||||||
node = cls.objects.filter(batch=batch).exclude(id__in=exclude_batchst_ids).order_by('-version').first()
|
raise ParseError(f"{batch}-该批次号因物料不同不可引用")
|
||||||
if node is None:
|
|
||||||
raise ParseError(f"{node.batch}-该批次号本次操作不可引用")
|
|
||||||
return node, False
|
return node, False
|
||||||
else:
|
except cls.DoesNotExist:
|
||||||
version = 1
|
return cls.objects.create(batch=batch, mio=mio, mioitem=mioitem, handover=handover, mlog=mlog, material_start=material_start, version=1), True
|
||||||
if mio is None and handover is None and mlog is None:
|
|
||||||
raise ParseError("mio or handover or mlog must be provided")
|
# if mio is None and handover is None and mlog is None:
|
||||||
# 带有来源的批次获取,需检查批次号是否可用
|
# try:
|
||||||
cls_qs = cls.objects.filter(batch=batch)
|
# node = cls.objects.exclude(id__in=exclude_batchst_ids).get(batch=batch)
|
||||||
if cls_qs.exists():
|
# except cls.DoesNotExist:
|
||||||
if reuse_node:
|
# return cls.objects.create(batch=batch), True
|
||||||
node:BatchSt = (cls_qs.filter(mio__isnull=False)|cls_qs.filter(handover=None, mio=None, mlog=None)).order_by('-version').first()
|
# except cls.MultipleObjectsReturned:
|
||||||
if node is None:
|
# # 兼容性处理
|
||||||
raise ParseError(f"{batch}-该批次号因物料不同不可引用")
|
# node = cls.objects.filter(batch=batch).exclude(id__in=exclude_batchst_ids).order_by('-version').first()
|
||||||
elif node.material_start is None:
|
# if node is None:
|
||||||
node.material_start = material_start
|
# raise ParseError(f"{node.batch}-该批次号本次操作不可引用")
|
||||||
node.save(update_fields = ["material_start"])
|
# return node, False
|
||||||
# elif node.material_start is not None and node.material_start != material_start:
|
# else:
|
||||||
# raise ParseError(f"{batch}-该批次号因物料不同不可引用-{str(node.material_start)} vs {str(material_start)}")
|
# version = 1
|
||||||
return node, False
|
# if mio is None and handover is None and mlog is None:
|
||||||
else:
|
# raise ParseError("mio or handover or mlog must be provided")
|
||||||
latest_version = BatchSt.objects.filter(batch=batch).aggregate(Max("version"))["version__max"]
|
# # 带有来源的批次获取,需检查批次号是否可用
|
||||||
version = latest_version + 1
|
# cls_qs = cls.objects.filter(batch=batch)
|
||||||
ins = cls.objects.create(batch=batch, mio=mio, mioitem=mioitem, handover=handover, mlog=mlog, material_start=material_start, version=version)
|
# if cls_qs.exists():
|
||||||
return ins, True
|
# if reuse_node:
|
||||||
|
# node:BatchSt = (cls_qs.filter(mio__isnull=False)|cls_qs.filter(handover=None, mio=None, mlog=None)).order_by('-version').first()
|
||||||
|
# if node is None:
|
||||||
|
# raise ParseError(f"{batch}-该批次号因物料不同不可引用")
|
||||||
|
# elif node.material_start is None:
|
||||||
|
# node.material_start = material_start
|
||||||
|
# node.save(update_fields = ["material_start"])
|
||||||
|
# # elif node.material_start is not None and node.material_start != material_start:
|
||||||
|
# # raise ParseError(f"{batch}-该批次号因物料不同不可引用-{str(node.material_start)} vs {str(material_start)}")
|
||||||
|
# return node, False
|
||||||
|
# else:
|
||||||
|
# latest_version = BatchSt.objects.filter(batch=batch).aggregate(Max("version"))["version__max"]
|
||||||
|
# version = latest_version + 1
|
||||||
|
# ins = cls.objects.create(batch=batch, mio=mio, mioitem=mioitem, handover=handover, mlog=mlog, material_start=material_start, version=version)
|
||||||
|
# return ins, True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
@ -738,11 +751,25 @@ class BatchLog(BaseModel):
|
||||||
@classmethod
|
@classmethod
|
||||||
def clear(cls, handover=None, mlog=None, mio=None, mioitem=None):
|
def clear(cls, handover=None, mlog=None, mio=None, mioitem=None):
|
||||||
if handover:
|
if handover:
|
||||||
cls.objects.filter(handover=handover).delete()
|
logs = cls.objects.filter(handover=handover)
|
||||||
BatchSt.objects.filter(handover=handover).delete()
|
for log in logs:
|
||||||
|
source = log.source
|
||||||
|
target = log.target
|
||||||
|
log.delete()
|
||||||
|
if not BatchLog.objects.filter(Q(source=source) | Q(target=source)).exists():
|
||||||
|
source.delete()
|
||||||
|
if not BatchLog.objects.filter(Q(source=target) | Q(target=target)).exists():
|
||||||
|
target.delete()
|
||||||
if mlog:
|
if mlog:
|
||||||
cls.objects.filter(mlog=mlog).delete()
|
logs = cls.objects.filter(mlog=mlog)
|
||||||
BatchSt.objects.filter(mlog=mlog).delete()
|
for log in logs:
|
||||||
|
source = log.source
|
||||||
|
target = log.target
|
||||||
|
log.delete()
|
||||||
|
if not BatchLog.objects.filter(Q(source=source) | Q(target=source)).exists():
|
||||||
|
source.delete()
|
||||||
|
if not BatchLog.objects.filter(Q(source=target) | Q(target=target)).exists():
|
||||||
|
target.delete()
|
||||||
if mio:
|
if mio:
|
||||||
BatchSt.objects.filter(mio=mio).delete()
|
BatchSt.objects.filter(mio=mio).delete()
|
||||||
if mioitem:
|
if mioitem:
|
||||||
|
|
Loading…
Reference in New Issue