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,58 +201,74 @@ 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: {},
accept_overallocated: {}, accept_overallocated: {},
accept_incomplete: {}, 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) { // Hide "accept incomplete" field if the build has been completed
html += ` if (!ctx.remaining || ctx.remaining == 0) {
<div class='alert alert-block alert-success'> delete fields.accept_incomplete;
{% trans "Build order is ready to be completed" %} }
</div>`;
} else {
html += `
<div class='alert alert-block alert-danger'>
<strong>{% trans "Build Order is incomplete" %}</strong>
</div>
`;
if (!options.allocated) { // Hide "accept unallocated" field if the build is fully allocated
html += `<div class='alert alert-block alert-warning'>{% trans "Required stock has not been fully allocated" %}</div>`; if (ctx.allocated) {
} delete fields.accept_unallocated;
}
if (!options.completed) { return fields;
html += `<div class='alert alert-block alert-warning'>{% trans "Required build quantity has not been completed" %}</div>`; },
} preFormContent: function(opts) {
} var ctx = opts.context || {};
// Hide particular fields if they are not required var html = '';
if (options.allocated) { if (ctx.allocated && ctx.remaining == 0 && ctx.incomplete == 0) {
delete fields.accept_unallocated; html += `
} <div class='alert alert-block alert-success'>
{% trans "Build order is ready to be completed" %}'
</div>`;
} else {
if (options.completed) { if (ctx.incomplete > 0) {
delete fields.accept_incomplete; 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>`;
} else {
html += `
<div class='alert alert-block alert-danger'>
<strong>{% trans "Build Order is incomplete" %}</strong>
</div>
`;
}
if (!options.overallocated) { if (!ctx.allocated) {
delete fields.accept_overallocated; html += `<div class='alert alert-block alert-warning'>{% trans "Required stock has not been fully allocated" %}</div>`;
} }
constructForm(url, { if (ctx.remaining > 0) {
fields: fields, html += `<div class='alert alert-block alert-warning'>{% trans "Required build quantity has not been completed" %}</div>`;
}
}
return html;
},
reload: true, reload: true,
confirm: true, confirm: true,
method: 'POST',
title: '{% trans "Complete Build Order" %}', title: '{% trans "Complete Build Order" %}',
preFormContent: html, method: 'POST',
}); });
} }