From 7835562396fc155f327a05505bfa515482c5fe7a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 9 May 2019 23:55:30 +1000 Subject: [PATCH 1/6] Auto-allocation form now working - Displays a list of stock items which will be allocated --- InvenTree/build/forms.py | 12 ++++ InvenTree/build/models.py | 24 ++++++-- InvenTree/build/templates/build/allocate.html | 12 +++- .../build/templates/build/auto_allocate.html | 43 ++++++++++++++ InvenTree/build/templates/build/complete.html | 2 +- InvenTree/build/urls.py | 1 + InvenTree/build/views.py | 57 ++++++++++++++++++- 7 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 InvenTree/build/templates/build/auto_allocate.html diff --git a/InvenTree/build/forms.py b/InvenTree/build/forms.py index de56ca34f7..d46138cd09 100644 --- a/InvenTree/build/forms.py +++ b/InvenTree/build/forms.py @@ -27,6 +27,18 @@ class EditBuildForm(HelperForm): ] +class AutoAllocateBuildForm(HelperForm): + """ Form for auto-allocation of stock to a build """ + + confirm = forms.BooleanField(required=False, help_text='Confirm stock allocation') + + 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..2c9077f3d7 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,12 +151,17 @@ 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 @@ -164,6 +169,13 @@ class Build(models.Model): 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 +185,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..b3d7dd1009 100644 --- a/InvenTree/build/templates/build/allocate.html +++ b/InvenTree/build/templates/build/allocate.html @@ -18,7 +18,8 @@ InvenTree | Allocate Parts
- + +
@@ -64,4 +65,13 @@ InvenTree | Allocate Parts ); }); + $("#auto-allocate-build").on('click', function() { + launchModalForm( + "{% url 'build-auto-allocate' build.id %}", + { + reload: true, + } + ); + }); + {% endblock %} 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..a2ebb26209 100644 --- a/InvenTree/build/templates/build/complete.html +++ b/InvenTree/build/templates/build/complete.html @@ -7,7 +7,7 @@ Are you sure you want to mark this build as complete?
{% if taking %} The following items will be removed from stock: -