From b38fde85f2b1d6a39f8e3fab52c1643442fd2c9a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 26 Oct 2020 22:34:40 +1100 Subject: [PATCH] Added some more buttons, etc --- InvenTree/build/forms.py | 12 +++++- InvenTree/build/models.py | 18 +++++++-- InvenTree/build/templates/build/allocate.html | 4 +- .../templates/build/allocation_card.html | 5 +-- InvenTree/build/test_build.py | 2 +- InvenTree/build/views.py | 31 +++++++++++--- InvenTree/templates/js/build.js | 40 +++++++++++-------- 7 files changed, 78 insertions(+), 34 deletions(-) diff --git a/InvenTree/build/forms.py b/InvenTree/build/forms.py index 62e5816e1f..f671324195 100644 --- a/InvenTree/build/forms.py +++ b/InvenTree/build/forms.py @@ -90,18 +90,26 @@ class UnallocateBuildForm(HelperForm): model = Build fields = [ 'confirm', + 'output_id', + 'part_id', ] -class ConfirmBuildForm(HelperForm): +class AutoAllocateForm(HelperForm): """ Form for auto-allocation of stock to a build """ confirm = forms.BooleanField(required=False, help_text=_('Confirm')) + output_id = forms.IntegerField( + required=False, + widget=forms.HiddenInput() + ) + class Meta: model = Build fields = [ - 'confirm' + 'confirm', + 'output_id', ] diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 4d42d6cad4..5567856c2d 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -306,7 +306,7 @@ class Build(MPTTModel): self.status = BuildStatus.CANCELLED self.save() - def getAutoAllocations(self): + def getAutoAllocations(self, output): """ Return a list of parts which will be allocated using the 'AutoAllocate' function. @@ -316,12 +316,18 @@ class Build(MPTTModel): - Take as many parts as available (up to the quantity required for the BOM) - If there are multiple StockItems available, ignore (leave up to the user) + Args: + output: A stock item (build output) to auto-allocate against + Returns: A list object containing the StockItem objects to be allocated (and the quantities) """ allocations = [] + """ + Iterate through each item in the BOM + """ for item in self.part.bom_items.all().prefetch_related('sub_part'): # How many parts required for this build? @@ -409,8 +415,12 @@ class Build(MPTTModel): output.delete() @transaction.atomic - def autoAllocate(self): - """ Run auto-allocation routine to allocate StockItems to this Build. + def auto_allocate(self, output=None): + """ + Run auto-allocation routine to allocate StockItems to this Build. + + Args: + output: If specified, only auto-allocate against the given built output Returns a list of dict objects with keys like: @@ -422,7 +432,7 @@ class Build(MPTTModel): See: getAutoAllocations() """ - allocations = self.getAutoAllocations() + allocations = self.getAutoAllocations(output) for item in allocations: # Create a new allocation diff --git a/InvenTree/build/templates/build/allocate.html b/InvenTree/build/templates/build/allocate.html index c27d8f17de..1d6a99525c 100644 --- a/InvenTree/build/templates/build/allocate.html +++ b/InvenTree/build/templates/build/allocate.html @@ -25,13 +25,13 @@ InvenTree | Allocate Parts
--> -

{% trans "Untracked Parts" %}

+

{% trans "Allocated Stock" %}

{% include "build/allocation_card.html" %}
{% if build.incomplete_outputs %} -

{% trans "Tracked Build Ouputs" %}

+

{% trans "Build Ouputs" %}

{% for item in build.incomplete_outputs %} {% include "build/allocation_card.html" with item=item %} diff --git a/InvenTree/build/templates/build/allocation_card.html b/InvenTree/build/templates/build/allocation_card.html index dbf5350999..fa8a72139a 100644 --- a/InvenTree/build/templates/build/allocation_card.html +++ b/InvenTree/build/templates/build/allocation_card.html @@ -18,11 +18,10 @@ {% else %} {% trans "Untracked items" %} {% endif %} -
- + +
- {% trans "Completed lines" %}:
diff --git a/InvenTree/build/test_build.py b/InvenTree/build/test_build.py index e3c424a363..1d7a45fcf0 100644 --- a/InvenTree/build/test_build.py +++ b/InvenTree/build/test_build.py @@ -172,7 +172,7 @@ class BuildTest(TestCase): self.assertEqual(len(allocations), 1) - self.build.autoAllocate() + self.build.auto_allocate() self.assertEqual(BuildItem.objects.count(), 1) self.assertTrue(self.build.isPartFullyAllocated(self.sub_part_2)) diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index 450f676b4c..071a902bea 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -93,12 +93,24 @@ class BuildAutoAllocate(AjaxUpdateView): """ model = Build - form_class = forms.ConfirmBuildForm + form_class = forms.AutoAllocateForm context_object_name = 'build' ajax_form_title = _('Allocate Stock') ajax_template_name = 'build/auto_allocate.html' role_required = 'build.change' + def get_initial(self): + + initials = super().get_initial() + + # Pointing to a particular build output? + output = self.get_param('output') + + if output: + initials['output_id'] = output + + return initials + def get_context_data(self, *args, **kwargs): """ Get the context data for form rendering. """ @@ -125,14 +137,23 @@ class BuildAutoAllocate(AjaxUpdateView): confirm = request.POST.get('confirm', False) + output = None + output_id = request.POST.get('output_id', None) + + if output_id: + try: + output = StockItem.objects.get(pk=output_id) + except (ValueError, StockItem.DoesNotExist): + pass + valid = False - if confirm is False: + if confirm: + build.auto_allocate(output) + valid = True + else: form.errors['confirm'] = [_('Confirm stock allocation')] form.non_field_errors = [_('Check the confirmation box at the bottom of the list')] - else: - build.autoAllocate() - valid = True data = { 'form_valid': valid, diff --git a/InvenTree/templates/js/build.js b/InvenTree/templates/js/build.js index a614ef348c..22c950ce05 100644 --- a/InvenTree/templates/js/build.js +++ b/InvenTree/templates/js/build.js @@ -55,21 +55,23 @@ function makeBuildOutputActionButtons(output, buildInfo) { // Add a button to "auto allocate" against the particular build output html += makeIconButton( - 'fa-clipboard-check icon-blue', 'button-output-auto', outputId, - '{% trans "Allocate stock items to this output" %}', + 'fa-magic icon-blue', 'button-output-auto', outputId, + '{% trans "Auto-allocate stock items to this output" %}', { disabled: true, } ); - // Add a button to "complete" the particular build output - html += makeIconButton( - 'fa-tools icon-green', 'button-output-complete', outputId, - '{% trans "Complete build output" %}', - { - disabled: true - } - ); + if (output) { + // Add a button to "complete" the particular build output + html += makeIconButton( + 'fa-check icon-green', 'button-output-complete', outputId, + '{% trans "Complete build output" %}', + { + disabled: true + } + ); + } // Add a button to "cancel" the particular build output (unallocate) html += makeIconButton( @@ -77,14 +79,18 @@ function makeBuildOutputActionButtons(output, buildInfo) { '{% trans "Unallocate stock from build output" %}', ); - // Add a button to "delete" the particular build output - html += makeIconButton( - 'fa-trash-alt icon-red', 'button-output-delete', outputId, - '{% trans "Delete build output" %}', - ); + if (output) { + // Add a button to "delete" the particular build output + html += makeIconButton( + 'fa-trash-alt icon-red', 'button-output-delete', outputId, + '{% trans "Delete build output" %}', + ); + + // Add a button to "destroy" the particular build output (mark as damaged, scrap) + // TODO + + } - // Add a button to "destroy" the particular build output (mark as damaged, scrap) - // TODO html += '
';