feat: Handoverb 支持拆批

This commit is contained in:
caoqianming 2025-03-06 14:15:45 +08:00
parent afdcc5374e
commit 74f0db77ed
5 changed files with 83 additions and 24 deletions

View File

@ -0,0 +1,29 @@
# Generated by Django 3.2.12 on 2025-03-06 06:14
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wpm', '0093_alter_handover_recive_user'),
]
operations = [
migrations.AddField(
model_name='handover',
name='mtype',
field=models.PositiveSmallIntegerField(choices=[(10, '正常'), (20, '分批'), (30, '合批')], default=10, verbose_name='合并类型'),
),
migrations.AddField(
model_name='handoverb',
name='batch',
field=models.TextField(blank=True, null=True, verbose_name='批次号'),
),
migrations.AddField(
model_name='handoverb',
name='wm_to',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='handoverb_wm_to', to='wpm.wmaterial', verbose_name='所到车间库存'),
),
]

View File

@ -440,8 +440,14 @@ class Handover(CommonADModel):
H_TEST = 30 H_TEST = 30
H_SCRAP = 40 H_SCRAP = 40
H_CHANGE = 50 H_CHANGE = 50
H_MERGE = 30
H_DIV = 20
new_batch = models.TextField('新批次号', null=True, blank=True) new_batch = models.TextField('新批次号', null=True, blank=True)
type = models.PositiveSmallIntegerField('交接类型', choices=[(H_NORMAL, '正常交接'), (H_REPAIR, '返修交接'), (H_TEST, '检验交接'), (H_SCRAP, '报废交接'), (H_CHANGE, '改版交接')], default=H_NORMAL) mtype = models.PositiveSmallIntegerField("合并类型", default=H_NORMAL, choices=
[(H_NORMAL, '正常'), (H_DIV, '分批'), (H_MERGE, '合批')])
type = models.PositiveSmallIntegerField('交接类型', choices=[
(H_NORMAL, '正常交接'), (H_REPAIR, '返修交接'),
(H_TEST, '检验交接'), (H_SCRAP, '报废交接'), (H_CHANGE, '改版交接')], default=H_NORMAL)
send_date = models.DateField('送料日期') send_date = models.DateField('送料日期')
send_user = models.ForeignKey( send_user = models.ForeignKey(
User, verbose_name='交送人', on_delete=models.CASCADE, related_name='handover_send_user') User, verbose_name='交送人', on_delete=models.CASCADE, related_name='handover_send_user')
@ -480,8 +486,11 @@ class Handoverb(BaseModel):
"""TN: 子级交接记录 """TN: 子级交接记录
""" """
handover = models.ForeignKey(Handover, verbose_name='关联交接记录', on_delete=models.CASCADE) handover = models.ForeignKey(Handover, verbose_name='关联交接记录', on_delete=models.CASCADE)
batch = models.TextField("批次号", null=True, blank=True)
wm = models.ForeignKey(WMaterial, verbose_name='关联车间库存', on_delete=models.SET_NULL, wm = models.ForeignKey(WMaterial, verbose_name='关联车间库存', on_delete=models.SET_NULL,
null=True, blank=True, related_name='handoverb_wm') null=True, blank=True, related_name='handoverb_wm')
wm_to = models.ForeignKey(WMaterial, verbose_name='所到车间库存', on_delete=models.SET_NULL,
null=True, blank=True, related_name='handoverb_wm_to')
count = models.PositiveIntegerField('送料数', default=0) count = models.PositiveIntegerField('送料数', default=0)
@property @property

View File

@ -738,7 +738,7 @@ class MlogbOutUpdateSerializer(CustomModelSerializer):
model = Mlogb model = Mlogb
fields = "__all__" fields = "__all__"
read_only_fields = EXCLUDE_FIELDS_BASE + ['mlog', 'mtask', 'wm_in', 'material_in', 'material_out', read_only_fields = EXCLUDE_FIELDS_BASE + ['mlog', 'mtask', 'wm_in', 'material_in', 'material_out',
'count_use', 'count_break', 'count_pn_jgqbl', 'mlogbdefect', "qct"] 'count_use', 'count_break', 'count_pn_jgqbl', 'mlogbdefect', "qct", "batch"]
# def create(self, validated_data): # def create(self, validated_data):
# material_out:Material = validated_data["material_out"] # material_out:Material = validated_data["material_out"]
@ -837,7 +837,6 @@ class Handoverbwserializer(CustomModelSerializer):
extra_kwargs = {'wpr': {'required': True}} extra_kwargs = {'wpr': {'required': True}}
class HandoverbSerializer(CustomModelSerializer): class HandoverbSerializer(CustomModelSerializer):
batch = serializers.CharField(source='wm.batch', read_only=True)
notok_sign = serializers.CharField(source='wm.notok_sign', read_only=True) notok_sign = serializers.CharField(source='wm.notok_sign', read_only=True)
defect_name = serializers.CharField(source="wm.defect.name", read_only=True) defect_name = serializers.CharField(source="wm.defect.name", read_only=True)
handoverbw = Handoverbwserializer(many=True, required=False) handoverbw = Handoverbwserializer(many=True, required=False)
@ -867,11 +866,16 @@ class HandoverSerializer(CustomModelSerializer):
handoverb = HandoverbSerializer(many=True, required=False) handoverb = HandoverbSerializer(many=True, required=False)
def validate(self, attrs): def validate(self, attrs):
if "mtype" not in attrs:
attrs['mtype'] = Handover.H_NORMAL
mtype = attrs["mtype"]
if 'type' not in attrs: if 'type' not in attrs:
attrs['type'] = Handover.H_NORMAL attrs['type'] = Handover.H_NORMAL
if attrs["type"] == Handover.H_CHANGE: if attrs["type"] == Handover.H_CHANGE:
if "material_changed" not in attrs: if "material_changed" not in attrs:
raise ParseError("必须指定变更后的物料") raise ParseError("必须指定改版后的物料")
if mtype == Handover.H_MERGE and not attrs.get("new_batch", None):
raise ParseError("必须指定合并后的批次")
wm:WMaterial = attrs.get('wm', None) wm:WMaterial = attrs.get('wm', None)
handoverb = attrs.get('handoverb', []) handoverb = attrs.get('handoverb', [])
if wm: if wm:
@ -939,11 +943,17 @@ class HandoverSerializer(CustomModelSerializer):
handoverb = validated_data.pop('handoverb', []) handoverb = validated_data.pop('handoverb', [])
with transaction.atomic(): with transaction.atomic():
ins = super().create(validated_data) ins = super().create(validated_data)
for item in handoverb: mtype = validated_data["mtype"]
for ind, item in enumerate(handoverb):
wm = item["wm"] wm = item["wm"]
count = item["count"] count = item["count"]
handoverbw = item.pop("handoverbw", []) handoverbw = item.pop("handoverbw", [])
handoverb, _ = Handoverb.objects.get_or_create(handover=ins, wm=wm, count=count) if mtype == Handover.H_DIV:
if not item["batch"]:
raise ParseError(f'{ind+1}行-请提供拆批批次号')
handoverb = Handoverb.objects.create(handover=ins, batch=item["batch"], count=count)
else:
handoverb = Handoverb.objects.create(handover=ins, wm=wm, count=count, batch=wm.batch)
if wm.material.tracking == Material.MA_TRACKING_SINGLE: if wm.material.tracking == Material.MA_TRACKING_SINGLE:
if handoverbw: if handoverbw:
handoverb.count = len(handoverbw) handoverb.count = len(handoverbw)
@ -955,40 +965,40 @@ class HandoverSerializer(CustomModelSerializer):
for item in Wpr.get_qs_by_wm(wm): for item in Wpr.get_qs_by_wm(wm):
Handoverbw.objects.get_or_create(wpr=item, handoverb=handoverb, defaults={"number":item.number}) Handoverbw.objects.get_or_create(wpr=item, handoverb=handoverb, defaults={"number":item.number})
else: else:
raise ParseError(f'请提供交接物料明细编号') raise ParseError(f'{ind+1}行-请提供交接物料明细编号')
return ins return ins
def update(self, instance, validated_data): def update(self, instance, validated_data):
handoverb = validated_data.pop('handoverb', []) handoverb = validated_data.pop('handoverb', [])
with transaction.atomic(): with transaction.atomic():
super().update(instance, validated_data) super().update(instance, validated_data)
wmIds = [] Handoverb.objects.filter(handover=instance).delete()
for item in handoverb: for ind, item in enumerate(handoverb):
wm = item["wm"] wm = item["wm"]
count = item["count"] count = item["count"]
handoverbw = item.pop("handoverbw", []) handoverbw = item.pop("handoverbw", [])
wmIds.append(item["wm"].id) if validated_data["mtype"] == Handover.H_DIV:
hb, _ = Handoverb.objects.get_or_create(handover=instance, wm=wm) if not item["batch"]:
hb.count = count raise ParseError(f'{ind+1}行-请提供拆批批次号')
hb.save() hb, _ = Handoverb.objects.get_or_create(handover=instance, batch=item["batch"], defaults={"count": count})
else:
hb, _ = Handoverb.objects.get_or_create(handover=instance, wm=wm, defaults={"count": count,
"batch": wm.batch})
if wm.material.tracking == Material.MA_TRACKING_SINGLE: if wm.material.tracking == Material.MA_TRACKING_SINGLE:
Handoverbw.objects.filter(handoverb=hb).delete()
if handoverbw: if handoverbw:
hb.count = len(handoverbw) hb.count = len(handoverbw)
hb.save() hb.save()
wprIds = [item["wpr"].id for item in handoverbw]
Handoverbw.objects.filter(handoverb=hb).exclude(wpr__in=wprIds).delete()
for item in handoverbw: for item in handoverbw:
wpr = item["wpr"] wpr = item["wpr"]
Handoverbw.objects.get_or_create(wpr=wpr, handoverb=hb, defaults={"number": wpr.number, "note": item.get("note", None)}) Handoverbw.objects.get_or_create(wpr=wpr, handoverb=hb, defaults={"number": wpr.number, "note": item.get("note", None)})
elif count == wm.count: elif count == wm.count:
wpr_qs = Wpr.get_qs_by_wm(wm) wpr_qs = Wpr.get_qs_by_wm(wm)
Handoverbw.objects.filter(handoverb=hb).exclude(wpr__in=wpr_qs).delete()
for item in wpr_qs: for item in wpr_qs:
Handoverbw.objects.get_or_create(wpr=item, handoverb=handoverb, Handoverbw.objects.get_or_create(wpr=item, handoverb=handoverb,
defaults={"number": item.number}) defaults={"number": item.number})
else: else:
raise ParseError(f'请提供交接物料明细编号') raise ParseError(f'{ind+1}行-请提供交接物料明细')
Handoverb.objects.filter(handover=instance).exclude(wm__in=wmIds).delete()
return instance return instance
class HandoverUpdateSerializer(CustomModelSerializer): class HandoverUpdateSerializer(CustomModelSerializer):

View File

@ -672,6 +672,7 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
handoverb_qs = Handoverb.objects.filter(handover=handover) handoverb_qs = Handoverb.objects.filter(handover=handover)
need_add = True need_add = True
material:Material = handover.material material:Material = handover.material
mtype = handover.mtype
if '混料' in material.name: # hard code if '混料' in material.name: # hard code
need_add = False need_add = False
if handoverb_qs.exists(): if handoverb_qs.exists():
@ -683,15 +684,22 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
recive_dept = handover.recive_dept recive_dept = handover.recive_dept
batches = [] batches = []
new_batch = handover.new_batch new_batch = handover.new_batch
if mtype == Handover.H_MERGE:
if new_batch: if new_batch:
batches = [new_batch] batches = [new_batch]
else:
raise ParseError("合并批次时请提供新批次号")
for item in handoverb_list: for item in handoverb_list:
wm_from, xcount, handover_or_b = item wm_from, xcount, handover_or_b = item
# 合并为新批
if mtype == Handover.H_MERGE:
batch = new_batch
elif mtype == Handover.H_DIV:
batch = handover_or_b.batch
else:
batch = wm_from.batch batch = wm_from.batch
batches.append(batch) batches.append(batch)
# 合并为新批
if new_batch:
batch = new_batch
if wm_from is None: if wm_from is None:
raise ParseError('找不到车间库存') raise ParseError('找不到车间库存')
@ -713,6 +721,8 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
mgroup=recive_mgroup, mgroup=recive_mgroup,
belong_dept=recive_dept, belong_dept=recive_dept,
state=WMaterial.WM_OK, state=WMaterial.WM_OK,
notok_sign=wm_from.notok_sign,
defect=wm_from.defect,
defaults={ defaults={
"batch_ofrom": wm_from.batch_ofrom, "batch_ofrom": wm_from.batch_ofrom,
"material_ofrom": wm_from.material_ofrom, "material_ofrom": wm_from.material_ofrom,
@ -801,6 +811,8 @@ def handover_submit(handover:Handover, user: User, now: Union[datetime.datetime,
wm_to.count = wm_to.count + xcount wm_to.count = wm_to.count + xcount
wm_to.count_eweight = handover.count_eweight # 这行代码有隐患 wm_to.count_eweight = handover.count_eweight # 这行代码有隐患
wm_to.save() wm_to.save()
handover_or_b.wm_to = wm_to
handover_or_b.save()
if material.tracking == Material.MA_TRACKING_SINGLE: if material.tracking == Material.MA_TRACKING_SINGLE:
handoverbws = Handoverbw.objects.filter(handoverb=handover_or_b) handoverbws = Handoverbw.objects.filter(handoverb=handover_or_b)
if handoverbws.count() != xcount: if handoverbws.count() != xcount:

View File

@ -374,7 +374,6 @@ class HandoverViewSet(CustomModelViewSet):
""" """
queryset = Handover.objects.all() queryset = Handover.objects.all()
serializer_class = HandoverSerializer serializer_class = HandoverSerializer
update_serializer_class = HandoverUpdateSerializer
select_related_fields = ['send_user', 'send_mgroup', 'send_dept', 'recive_user', 'recive_mgroup', 'recive_dept', 'wm'] select_related_fields = ['send_user', 'send_mgroup', 'send_dept', 'recive_user', 'recive_mgroup', 'recive_dept', 'wm']
filterset_class = HandoverFilter filterset_class = HandoverFilter
search_fields = ['id', 'material__name', search_fields = ['id', 'material__name',