From 3ee7be1d5845760d41f690d298bc5c8ebd6f92e6 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 5 Oct 2020 00:42:09 +1100 Subject: [PATCH] Add "optional" field to BomItem - Defaults to False - Indicates that the BomItem is "optional" for a build - Will be used in the future when calculating if a Build output is fully allocated! --- InvenTree/part/api.py | 16 +++++++++++++--- InvenTree/part/forms.py | 3 ++- .../part/migrations/0051_bomitem_optional.py | 18 ++++++++++++++++++ InvenTree/part/models.py | 3 +++ InvenTree/part/serializers.py | 1 + InvenTree/templates/js/bom.html | 4 ++++ 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 InvenTree/part/migrations/0051_bomitem_optional.py diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 9a86bb98d5..6834503466 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -765,20 +765,30 @@ class BomList(generics.ListCreateAPIView): queryset = super().filter_queryset(queryset) + params = self.request.query_params + + # Filter by "optional" status? + optional = params.get('optional', None) + + if optional is not None: + optional = str2bool(optional) + + queryset = queryset.filter(optional=optional) + # Filter by part? - part = self.request.query_params.get('part', None) + part = params.get('part', None) if part is not None: queryset = queryset.filter(part=part) # Filter by sub-part? - sub_part = self.request.query_params.get('sub_part', None) + sub_part = params.get('sub_part', None) if sub_part is not None: queryset = queryset.filter(sub_part=sub_part) # Filter by "trackable" status of the sub-part - trackable = self.request.query_params.get('trackable', None) + trackable = params.get('trackable', None) if trackable is not None: trackable = str2bool(trackable) diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 0a15d598bd..c64bbb8362 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -231,7 +231,8 @@ class EditBomItemForm(HelperForm): 'quantity', 'reference', 'overage', - 'note' + 'note', + 'optional', ] # Prevent editing of the part associated with this BomItem diff --git a/InvenTree/part/migrations/0051_bomitem_optional.py b/InvenTree/part/migrations/0051_bomitem_optional.py new file mode 100644 index 0000000000..04920b3c10 --- /dev/null +++ b/InvenTree/part/migrations/0051_bomitem_optional.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-10-04 13:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0050_auto_20200917_2315'), + ] + + operations = [ + migrations.AddField( + model_name='bomitem', + name='optional', + field=models.BooleanField(default=False, help_text='This BOM item is optional'), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 5f29c8cf80..d427409c71 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1500,6 +1500,7 @@ class BomItem(models.Model): part: Link to the parent part (the part that will be produced) sub_part: Link to the child part (the part that will be consumed) quantity: Number of 'sub_parts' consumed to produce one 'part' + optional: Boolean field describing if this BomItem is optional reference: BOM reference field (e.g. part designators) overage: Estimated losses for a Build. Can be expressed as absolute value (e.g. '7') or a percentage (e.g. '2%') note: Note field for this BOM item @@ -1533,6 +1534,8 @@ class BomItem(models.Model): # Quantity required quantity = models.DecimalField(default=1.0, max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], help_text=_('BOM quantity for this BOM item')) + optional = models.BooleanField(default=False, help_text=_("This BOM item is optional")) + overage = models.CharField(max_length=24, blank=True, validators=[validators.validate_overage], help_text=_('Estimated build wastage quantity (absolute or percentage)') ) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 7c73e9f98b..847216e957 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -403,6 +403,7 @@ class BomItemSerializer(InvenTreeModelSerializer): 'quantity', 'reference', 'price_range', + 'optional', 'overage', 'note', 'validated', diff --git a/InvenTree/templates/js/bom.html b/InvenTree/templates/js/bom.html index db5eafa0c1..b804453ca2 100644 --- a/InvenTree/templates/js/bom.html +++ b/InvenTree/templates/js/bom.html @@ -169,6 +169,10 @@ function loadBomTable(table, options) { // Let's make it a bit more pretty text = parseFloat(text); + if (row.optional) { + text += " ({% trans "Optional" %})"; + } + if (row.overage) { text += " (+" + row.overage + ") "; }