diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 770b78a7f6..f05fbe74cf 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -1018,6 +1018,20 @@ class Build(MPTTModel, ReferenceIndexingMixin): """Returns True if the un-tracked parts are fully allocated for this BuildOrder.""" return self.is_fully_allocated(None) + def has_overallocated_parts(self, output): + """Check if parts have been 'over-allocated' against the specified output. + + Note: If output=None, test un-tracked parts + """ + + bom_items = self.tracked_bom_items if output else self.untracked_bom_items + + for bom_item in bom_items: + if self.allocated_quantity(bom_item, output) > self.required_quantity(bom_item, output): + return True + + return False + def unallocated_bom_items(self, output): """Return a list of bom items which have *not* been fully allocated against a particular output.""" unallocated = [] diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index daf142cf5d..7278c3f3af 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -466,6 +466,22 @@ class BuildCancelSerializer(serializers.Serializer): class BuildCompleteSerializer(serializers.Serializer): """DRF serializer for marking a BuildOrder as complete.""" + accept_overallocated = serializers.BooleanField( + label=_('Accept Overallocated'), + help_text=_('Accept stock items which have been overallocated to this build order'), + required=False, + default=False, + ) + + def validate_accept_overallocated(self, value): + """Check if the 'accept_overallocated' field is required""" + build = self.context['build'] + + if build.has_overallocated_parts(output=None) and not value: + raise ValidationError(_('Some stock items have been overallocated')) + + return value + accept_unallocated = serializers.BooleanField( label=_('Accept Unallocated'), help_text=_('Accept that stock items have not been fully allocated to this build order'), diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index b1315f1d35..fb1ca9c3ef 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -174,6 +174,7 @@ function completeBuildOrder(build_id, options={}) { var fields = { accept_unallocated: {}, + accept_overallocated: {}, accept_incomplete: {}, };