diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 19371d4fba..d961e024a8 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -490,6 +490,21 @@ class OverallocationChoice(): class BuildCompleteSerializer(serializers.Serializer): """DRF serializer for marking a BuildOrder as complete.""" + def get_context_data(self): + """Retrieve extra context data for this serializer. + + This is so we can determine (at run time) whether the build is ready to be completed. + """ + + build = self.context['build'] + + return { + 'overallocated': build.has_overallocated_parts(), + 'allocated': build.are_untracked_parts_allocated(), + 'remaining': build.remaining, + 'incomplete': build.incomplete_count, + } + accept_overallocated = serializers.ChoiceField( label=_('Overallocated Stock'), choices=list(OverallocationChoice.OPTIONS.items()), diff --git a/InvenTree/build/templates/build/build_base.html b/InvenTree/build/templates/build/build_base.html index 2f3e339b7d..3bea18fe03 100644 --- a/InvenTree/build/templates/build/build_base.html +++ b/InvenTree/build/templates/build/build_base.html @@ -228,11 +228,7 @@ src="{% static 'img/blank_image.png' %}" }); $("#build-complete").on('click', function() { - completeBuildOrder({{ build.pk }}, { - overallocated: {% if build.has_overallocated_parts %}true{% else %}false{% endif %}, - allocated: {% if build.are_untracked_parts_allocated %}true{% else %}false{% endif %}, - completed: {% if build.remaining == 0 %}true{% else %}false{% endif %}, - }); + completeBuildOrder({{ build.pk }}); }); {% endif %} diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index deecfb9213..b09be32ffd 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -201,58 +201,74 @@ function cancelBuildOrder(build_id, options={}) { /* Construct a form to "complete" (finish) a build order */ function completeBuildOrder(build_id, options={}) { - var url = `/api/build/${build_id}/finish/`; + constructForm(`/api/build/${build_id}/finish/`, { + fieldsFunction: function(opts) { + var ctx = opts.context || {}; - var fields = { - accept_unallocated: {}, - accept_overallocated: {}, - accept_incomplete: {}, - }; + var fields = { + accept_unallocated: {}, + accept_overallocated: {}, + accept_incomplete: {}, + }; - var html = ''; + // Hide "accept overallocated" field if the build is *not* overallocated + if (!ctx.overallocated) { + delete fields.accept_overallocated; + } - if (options.allocated && options.completed) { - html += ` -