From 5dc543618eed2960ca07a8e2d14a1e826aeffa15 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 2 Mar 2022 00:31:50 +1100 Subject: [PATCH] Allow duplication of subtitute parts when copying a BOM --- InvenTree/part/models.py | 14 ++++++++++++++ InvenTree/part/serializers.py | 10 ++++++++++ InvenTree/templates/js/translated/part.js | 1 + 3 files changed, 25 insertions(+) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 33ad8bf612..b945acfc94 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1906,6 +1906,9 @@ class Part(MPTTModel): include_inherited = kwargs.get('include_inherited', False) + # Should substitute parts be duplicated? + copy_substitutes = kwargs.get('copy_substitutes', True) + # Copy existing BOM items from another part # Note: Inherited BOM Items will *not* be duplicated!! for bom_item in other.get_bom_items(include_inherited=include_inherited).all(): @@ -1928,11 +1931,22 @@ class Part(MPTTModel): if not bom_item.sub_part.check_add_to_bom(self, raise_error=raise_error): continue + # Obtain a list of direct substitute parts against this BomItem + substitutes = BomItemSubstitute.objects.filter(bom_item=bom_item) + # Construct a new BOM item bom_item.part = self bom_item.pk = None bom_item.save() + bom_item.refresh_from_db() + + if copy_substitutes: + for sub in substitutes: + # Duplicate the substitute (and point to the *new* BomItem object) + sub.pk = None + sub.bom_item = bom_item + sub.save() @transaction.atomic def copy_parameters_from(self, other, **kwargs): diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index d3956f3c6c..5d63448bb7 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -656,6 +656,9 @@ class PartCopyBOMSerializer(serializers.Serializer): fields = [ 'part', 'remove_existing', + 'copy_substitutes', + 'include_inherited', + 'skip_invalid', ] part = serializers.PrimaryKeyRelatedField( @@ -692,6 +695,12 @@ class PartCopyBOMSerializer(serializers.Serializer): default=False, ) + copy_substitutes = serializers.BooleanField( + label=_('Copy Substitute Parts'), + help_text=_('Copy substitute parts when duplicate BOM items'), + default=True, + ) + def save(self): """ Actually duplicate the BOM @@ -706,6 +715,7 @@ class PartCopyBOMSerializer(serializers.Serializer): clear=data.get('remove_existing', True), skip_invalid=data.get('skip_invalid', False), include_inherited=data.get('include_inherited', False), + copy_substitutes=data.get('copy_substitutes', True), ) diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js index 56b1ca6b75..d42755a0f0 100644 --- a/InvenTree/templates/js/translated/part.js +++ b/InvenTree/templates/js/translated/part.js @@ -474,6 +474,7 @@ function duplicateBom(part_id, options={}) { } }, include_inherited: {}, + copy_substitutes: {}, remove_existing: {}, skip_invalid: {}, },