diff --git a/apps/wpm/serializers.py b/apps/wpm/serializers.py index 17144e96..6cd931a4 100644 --- a/apps/wpm/serializers.py +++ b/apps/wpm/serializers.py @@ -580,7 +580,6 @@ class HandoverSerializer(CustomModelSerializer): wm:WMaterial = attrs.get('wm', None) handoverb = attrs.get('handoverb', []) if wm: - material = wm.material attrs['handoverb'] = [{"wm": wm, "count": attrs["count"] }] handoverb = attrs['handoverb'] elif handoverb: @@ -615,6 +614,11 @@ class HandoverSerializer(CustomModelSerializer): attrs['recive_mgroup'] = attrs['send_mgroup'] attrs['recive_dept'] = attrs['send_dept'] attrs['recive_user'] = attrs['send_user'] + if attrs['type'] == Handover.H_REPAIR: + recive_mgroup = attrs.get("recive_mgroup", None) + if recive_mgroup is None: + raise ParseError('返工交接需指定工段') + attrs['material_changed'] = find_material_can_change(attrs['material'], recive_mgroup) return attrs class Meta: @@ -634,18 +638,25 @@ class HandoverSerializer(CustomModelSerializer): } def create(self, validated_data): - type = validated_data['type'] handoverb = validated_data.pop('handoverb', []) - if type == Handover.H_REPAIR: - recive_mgroup = validated_data.get("recive_mgroup", None) - if recive_mgroup is None: - raise ParseError('返工交接需指定工段') - validated_data['material_changed'] = find_material_can_change(validated_data['material'], recive_mgroup) with transaction.atomic(): ins = super().create(validated_data) for item in handoverb: Handoverb.objects.get_or_create(handover=ins, wm=item["wm"], count=item["count"]) return ins + + def update(self, instance, validated_data): + handoverb = validated_data.pop('handoverb', []) + with transaction.atomic(): + super().update(instance, validated_data) + wmIds = [] + for item in handoverb: + wmIds.append(item["wm"].id) + hb, _ = Handoverb.objects.get_or_create(handover=instance, wm=item["wm"]) + hb.count = item["count"] + hb.save() + Handoverb.objects.filter(handover=instance).exclude(wm__in=wmIds).delete() + return instance class HandoverUpdateSerializer(CustomModelSerializer): class Meta: