From 0ddc6692acec7cca76c2e7b1ed48184562a0847e Mon Sep 17 00:00:00 2001 From: caoqianming Date: Wed, 26 Mar 2025 13:19:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20handover=20=E5=90=88=E5=B9=B6=E5=8F=AF?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E7=8E=B0=E6=9C=89=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/wpm/migrations/0105_handover_new_wm.py | 19 ++++++++++ apps/wpm/models.py | 12 ++++++ apps/wpm/serializers.py | 8 +++- apps/wpm/services.py | 41 +++++++++++---------- 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 apps/wpm/migrations/0105_handover_new_wm.py diff --git a/apps/wpm/migrations/0105_handover_new_wm.py b/apps/wpm/migrations/0105_handover_new_wm.py new file mode 100644 index 00000000..6e760422 --- /dev/null +++ b/apps/wpm/migrations/0105_handover_new_wm.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.12 on 2025-03-26 03:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('wpm', '0104_auto_20250325_0921'), + ] + + operations = [ + migrations.AddField( + model_name='handover', + name='new_wm', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='wpm.wmaterial'), + ), + ] diff --git a/apps/wpm/models.py b/apps/wpm/models.py index c6a5d9d5..16bc3e92 100644 --- a/apps/wpm/models.py +++ b/apps/wpm/models.py @@ -483,6 +483,7 @@ class Handover(CommonADModel): H_MERGE = 30 H_DIV = 20 new_batch = models.TextField('新批次号', null=True, blank=True) + new_wm = models.ForeignKey(WMaterial, on_delete=models.SET_NULL, null=True, blank=True) mtype = models.PositiveSmallIntegerField("合并类型", default=H_NORMAL, choices= [(H_NORMAL, '正常'), (H_DIV, '分批'), (H_MERGE, '合批')]) type = models.PositiveSmallIntegerField('交接类型', choices=[ @@ -600,11 +601,22 @@ class BatchSt(BaseModel): if mio is None and handover is None and mlog is None and material_start is None: return cls.objects.get_or_create(batch=batch) else: + # 带有来源的批次获取,需检查批次号是否可用 + if cls.objects.filter(batch=batch).exists(): + raise ParseError(f"{batch}-该批次号不可用") 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) return ins, True + @classmethod + def init_dag(cls, batch:str, force_init=False): + """ + 更新批次数据 + """ + pass + + class BatchLog(BaseModel): """ diff --git a/apps/wpm/serializers.py b/apps/wpm/serializers.py index 61b609bb..b0bd0957 100644 --- a/apps/wpm/serializers.py +++ b/apps/wpm/serializers.py @@ -953,8 +953,12 @@ class HandoverSerializer(CustomModelSerializer): if attrs["type"] == Handover.H_CHANGE: if "material_changed" not in attrs: raise ParseError("必须指定改版后的物料") - if mtype == Handover.H_MERGE and not attrs.get("new_batch", None): - raise ParseError("必须指定合并后的批次") + if mtype == Handover.H_MERGE: + new_wm = attrs.get("new_wm", None) + if new_wm: + attrs['new_batch'] = new_wm.batch + if not attrs.get("new_batch", None): + raise ParseError("必须指定合并后的批次") wm:WMaterial = attrs.get('wm', None) handoverb = attrs.get('handoverb', []) if wm: diff --git a/apps/wpm/services.py b/apps/wpm/services.py index 8e9872c1..2151b91f 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -712,18 +712,18 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime, batches = [new_batch] else: raise ParseError("合并批次时请提供新批次号") - create_new_batch = False + + new_target = None for item in handoverb_list: wm_from, xcount, handover_or_b = item # 合并为新批 if mtype == Handover.H_MERGE: batch = new_batch - if create_new_batch is False: - target, _ = BatchSt.g_create(batch=batch, handover=handover, material_start=material) - create_new_batch = True + if new_target is None: + new_target, _ = BatchSt.g_create(batch=batch, handover=handover, material_start=material) source, _ = BatchSt.g_create(batch=wm_from.batch) - BatchLog.g_create(source=source, target=target, handover=handover, relation_type="merge") + BatchLog.g_create(source=source, target=new_target, handover=handover, relation_type="merge") elif mtype == Handover.H_DIV: batch = handover_or_b.batch target, _ = BatchSt.g_create(batch=batch, handover=handover, material_start=material) @@ -747,20 +747,23 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime, if need_add: # 开始变动 if handover.type == Handover.H_NORMAL: - wm_to, _ = WMaterial.objects.get_or_create( - batch=batch, - material=material, - mgroup=recive_mgroup, - belong_dept=recive_dept, - state=WMaterial.WM_OK, - notok_sign=wm_from.notok_sign, - defect=wm_from.defect, - defaults={ - "batch_ofrom": wm_from.batch_ofrom, - "material_ofrom": wm_from.material_ofrom, - "create_by": user - } - ) + if mtype == Handover.H_MERGE and handover.new_wm: + wm_to = handover.new_wm + else: + wm_to, _ = WMaterial.objects.get_or_create( + batch=batch, + material=material, + mgroup=recive_mgroup, + belong_dept=recive_dept, + state=WMaterial.WM_OK, + notok_sign=wm_from.notok_sign, + defect=wm_from.defect, + defaults={ + "batch_ofrom": wm_from.batch_ofrom, + "material_ofrom": wm_from.material_ofrom, + "create_by": user + } + ) elif handover.type == Handover.H_REPAIR: # 返修交接 recive_mgroup = handover.recive_mgroup