diff --git a/InvenTree/build/forms.py b/InvenTree/build/forms.py index de56ca34f7..57c31ff1db 100644 --- a/InvenTree/build/forms.py +++ b/InvenTree/build/forms.py @@ -27,6 +27,18 @@ class EditBuildForm(HelperForm): ] +class ConfirmBuildForm(HelperForm): + """ Form for auto-allocation of stock to a build """ + + confirm = forms.BooleanField(required=False, help_text='Confirm') + + class Meta: + model = Build + fields = [ + 'confirm' + ] + + class CompleteBuildForm(HelperForm): """ Form for marking a Build as complete """ diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index fedcb76403..0b658dee1a 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -127,10 +127,10 @@ class Build(models.Model): - If there are multiple StockItems available, ignore (leave up to the user) Returns: - A dict object containing the StockItem objects to be allocated (and the quantities) + A list object containing the StockItem objects to be allocated (and the quantities) """ - allocations = {} + allocations = [] for item in self.part.bom_items.all(): @@ -151,19 +151,37 @@ class Build(models.Model): # Are there any parts available? if stock_item.quantity > 0: + # Only take as many as are available if stock_item.quantity < q_required: q_required = stock_item.quantity - # Add the item to the allocations list - allocations[stock_item] = q_required + allocation = { + 'stock_item': stock_item, + 'quantity': q_required, + } + + allocations.append(allocation) return allocations + @transaction.atomic + def unallocateStock(self): + """ Deletes all stock allocations for this build. """ + + BuildItem.objects.filter(build=self.id).delete() + @transaction.atomic def autoAllocate(self): """ Run auto-allocation routine to allocate StockItems to this Build. + Returns a list of dict objects with keys like: + + { + 'stock_item': item, + 'quantity': quantity, + } + See: getAutoAllocations() """ @@ -173,8 +191,8 @@ class Build(models.Model): # Create a new allocation build_item = BuildItem( build=self, - stock_item=item, - quantity=allocations[item]) + stock_item=item['stock_item'], + quantity=item['quantity']) build_item.save() diff --git a/InvenTree/build/templates/build/allocate.html b/InvenTree/build/templates/build/allocate.html index 1aa5338551..15061a18b4 100644 --- a/InvenTree/build/templates/build/allocate.html +++ b/InvenTree/build/templates/build/allocate.html @@ -18,7 +18,8 @@ InvenTree | Allocate Parts
- + +
@@ -54,12 +55,20 @@ InvenTree | Allocate Parts {% endfor %} - $("#complete-build").on('click', function() { + $("#auto-allocate-build").on('click', function() { launchModalForm( - "{% url 'build-complete' build.id %}", + "{% url 'build-auto-allocate' build.id %}", + { + reload: true, + } + ); + }); + + $('#unallocate-build').on('click', function() { + launchModalForm( + "{% url 'build-unallocate' build.id %}", { reload: true, - submit_text: "Complete Build", } ); }); diff --git a/InvenTree/build/templates/build/auto_allocate.html b/InvenTree/build/templates/build/auto_allocate.html new file mode 100644 index 0000000000..ba789b9ff7 --- /dev/null +++ b/InvenTree/build/templates/build/auto_allocate.html @@ -0,0 +1,43 @@ +{% extends "modal_form.html" %} + +{% block pre_form_content %} + +{{ block.super }} + +Build: {{ build.title }} - {{ build.quantity }} x {{ build.part.name }} +

+Automatically allocate stock to this build? +
+ +{% if allocations %} + + + + + + + + +{% for item in allocations %} + + + + + + +{% endfor %} +
PartQuantityLocation
+ + + + + + {{ item.stock_item.part.name }}
+ {{ item.stock_item.part.description }} +
{{ item.quantity }}{{ item.stock_item.location }}
+ +{% else %} +No stock could be selected for automatic build allocation. +{% endif %} + +{% endblock %} \ No newline at end of file diff --git a/InvenTree/build/templates/build/complete.html b/InvenTree/build/templates/build/complete.html index 506f766c44..5e9338d656 100644 --- a/InvenTree/build/templates/build/complete.html +++ b/InvenTree/build/templates/build/complete.html @@ -7,18 +7,42 @@ Are you sure you want to mark this build as complete?
{% if taking %} The following items will be removed from stock: -