Update CompleteBuildOrder form (#3702)

- Load required data dynamically via the API
- Hide form fields according to dynamic data
- Show warnings according to dynamic data
This commit is contained in:
Oliver 2022-09-21 18:19:31 +10:00 committed by GitHub
parent 2a846c7030
commit 33326f6eaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 44 deletions

View File

@ -490,6 +490,21 @@ class OverallocationChoice():
class BuildCompleteSerializer(serializers.Serializer): class BuildCompleteSerializer(serializers.Serializer):
"""DRF serializer for marking a BuildOrder as complete.""" """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( accept_overallocated = serializers.ChoiceField(
label=_('Overallocated Stock'), label=_('Overallocated Stock'),
choices=list(OverallocationChoice.OPTIONS.items()), choices=list(OverallocationChoice.OPTIONS.items()),

View File

@ -228,11 +228,7 @@ src="{% static 'img/blank_image.png' %}"
}); });
$("#build-complete").on('click', function() { $("#build-complete").on('click', function() {
completeBuildOrder({{ build.pk }}, { 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 %},
});
}); });
{% endif %} {% endif %}

View File

@ -201,7 +201,9 @@ function cancelBuildOrder(build_id, options={}) {
/* Construct a form to "complete" (finish) a build order */ /* Construct a form to "complete" (finish) a build order */
function completeBuildOrder(build_id, options={}) { 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 = { var fields = {
accept_unallocated: {}, accept_unallocated: {},
@ -209,12 +211,40 @@ function completeBuildOrder(build_id, options={}) {
accept_incomplete: {}, accept_incomplete: {},
}; };
// Hide "accept overallocated" field if the build is *not* overallocated
if (!ctx.overallocated) {
delete fields.accept_overallocated;
}
// Hide "accept incomplete" field if the build has been completed
if (!ctx.remaining || ctx.remaining == 0) {
delete fields.accept_incomplete;
}
// Hide "accept unallocated" field if the build is fully allocated
if (ctx.allocated) {
delete fields.accept_unallocated;
}
return fields;
},
preFormContent: function(opts) {
var ctx = opts.context || {};
var html = ''; var html = '';
if (options.allocated && options.completed) { if (ctx.allocated && ctx.remaining == 0 && ctx.incomplete == 0) {
html += ` html += `
<div class='alert alert-block alert-success'> <div class='alert alert-block alert-success'>
{% trans "Build order is ready to be completed" %} {% trans "Build order is ready to be completed" %}'
</div>`;
} else {
if (ctx.incomplete > 0) {
html += `
<div class='alert alert-block alert-danger'>
<strong>{% trans "Build order has incomplete outputs" %}</strong><br>
{% trans "This build order cannot be completed as there are incomplete outputs" %}
</div>`; </div>`;
} else { } else {
html += ` html += `
@ -222,37 +252,23 @@ function completeBuildOrder(build_id, options={}) {
<strong>{% trans "Build Order is incomplete" %}</strong> <strong>{% trans "Build Order is incomplete" %}</strong>
</div> </div>
`; `;
}
if (!options.allocated) { if (!ctx.allocated) {
html += `<div class='alert alert-block alert-warning'>{% trans "Required stock has not been fully allocated" %}</div>`; html += `<div class='alert alert-block alert-warning'>{% trans "Required stock has not been fully allocated" %}</div>`;
} }
if (!options.completed) { if (ctx.remaining > 0) {
html += `<div class='alert alert-block alert-warning'>{% trans "Required build quantity has not been completed" %}</div>`; html += `<div class='alert alert-block alert-warning'>{% trans "Required build quantity has not been completed" %}</div>`;
} }
} }
// Hide particular fields if they are not required return html;
},
if (options.allocated) {
delete fields.accept_unallocated;
}
if (options.completed) {
delete fields.accept_incomplete;
}
if (!options.overallocated) {
delete fields.accept_overallocated;
}
constructForm(url, {
fields: fields,
reload: true, reload: true,
confirm: true, confirm: true,
method: 'POST',
title: '{% trans "Complete Build Order" %}', title: '{% trans "Complete Build Order" %}',
preFormContent: html, method: 'POST',
}); });
} }