2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 03:30:54 +00:00

Check for (and reduce) overallocated stock when editing build orders. (#3237)

* Check for (and reduce) overallocated stock when editing build orders.

After a build order is edited, check whether any bom items are now
overallocated to the build, and if they are find appropriate build items to
reduce the quantity of or remove from the build.

Fixes: #3236

* Only trim overallocated stock if requested

Turns the complete with overallocated stock option into a choice, so the
user can reject (default), continue (existing option in the bool), or
now additionally instead choose to have the overallocated stock removed
before the build is completed.

* fix style errors

* Another style fix.

* Add tests for overallocation handling API logic

* Capitalize overallocation options.
This commit is contained in:
Matt Brown
2022-08-10 23:37:29 +12:00
committed by GitHub
parent 2d63122ebe
commit 139e059a45
4 changed files with 217 additions and 8 deletions

View File

@ -473,21 +473,36 @@ class BuildCancelSerializer(serializers.Serializer):
)
class OverallocationChoice():
"""Utility class to contain options for handling over allocated stock items."""
REJECT = 'reject'
ACCEPT = 'accept'
TRIM = 'trim'
OPTIONS = {
REJECT: ('Not permitted'),
ACCEPT: _('Accept as consumed by this build order'),
TRIM: _('Deallocate before completing this build order'),
}
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'),
accept_overallocated = serializers.ChoiceField(
label=_('Overallocated Stock'),
choices=list(OverallocationChoice.OPTIONS.items()),
help_text=_('How do you want to handle extra stock items assigned to the build order'),
required=False,
default=False,
default=OverallocationChoice.REJECT,
)
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:
if build.has_overallocated_parts(output=None) and value == OverallocationChoice.REJECT:
raise ValidationError(_('Some stock items have been overallocated'))
return value
@ -541,6 +556,10 @@ class BuildCompleteSerializer(serializers.Serializer):
request = self.context['request']
build = self.context['build']
data = self.validated_data
if data.get('accept_overallocated', OverallocationChoice.REJECT) == OverallocationChoice.TRIM:
build.trim_allocated_stock()
build.complete_build(request.user)