diff --git a/InvenTree/build/forms.py b/InvenTree/build/forms.py index 8b99f627a6..cf30b3eaf9 100644 --- a/InvenTree/build/forms.py +++ b/InvenTree/build/forms.py @@ -7,7 +7,7 @@ from __future__ import unicode_literals from InvenTree.forms import HelperForm -from .models import Build +from .models import Build, BuildItem class EditBuildForm(HelperForm): @@ -26,3 +26,15 @@ class EditBuildForm(HelperForm): 'status', # 'completion_date', ] + + +class EditBuildItemForm(HelperForm): + """ Form for adding a new BuildItem to a Build """ + + class Meta: + model = BuildItem + fields = [ + 'build', + 'stock_item', + 'quantity', + ] diff --git a/InvenTree/build/templates/build/allocate.html b/InvenTree/build/templates/build/allocate.html index 365931a3ec..aca7a3ebba 100644 --- a/InvenTree/build/templates/build/allocate.html +++ b/InvenTree/build/templates/build/allocate.html @@ -28,6 +28,7 @@ { build: {{ build.pk }}, part: {{ build.part.pk }}, + new_item_url: "{% url 'build-item-create' %}", } ); diff --git a/InvenTree/build/urls.py b/InvenTree/build/urls.py index fea3b6596c..ef7a1369f8 100644 --- a/InvenTree/build/urls.py +++ b/InvenTree/build/urls.py @@ -6,6 +6,10 @@ from django.conf.urls import url, include from . import views +build_item_urls = [ + url('^new/', views.BuildItemCreate.as_view(), name='build-item-create'), +] + build_detail_urls = [ url(r'^edit/?', views.BuildUpdate.as_view(), name='build-edit'), url(r'^allocate/?', views.BuildAllocate.as_view(), name='build-allocate'), @@ -15,7 +19,9 @@ build_detail_urls = [ ] build_urls = [ - url(r'new/?', views.BuildCreate.as_view(), name='build-create'), + url(r'item/', include(build_item_urls)), + + url(r'new/', views.BuildCreate.as_view(), name='build-create'), url(r'^(?P\d+)/', include(build_detail_urls)), diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index dc7b3141c3..05044ec40d 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -10,8 +10,8 @@ from django.shortcuts import get_object_or_404 from django.views.generic import DetailView, ListView from part.models import Part -from .models import Build -from .forms import EditBuildForm +from .models import Build, BuildItem +from .forms import EditBuildForm, EditBuildItemForm from InvenTree.views import AjaxView, AjaxUpdateView, AjaxCreateView @@ -127,3 +127,30 @@ class BuildUpdate(AjaxUpdateView): return { 'info': 'Edited build', } + + +class BuildItemCreate(AjaxCreateView): + """ View for allocating a new part to a build """ + + model = BuildItem + form_class = EditBuildItemForm + ajax_template_name = 'modal_form.html' + ajax_form_title = 'Allocate new Part' + + def get_initial(self): + """ Provide initial data for BomItem. Look for the folllowing in the GET data: + + - build: pk of the Build object + """ + + initials = super(AjaxCreateView, self).get_initial().copy() + + build_id = self.get_param('build') + + if build_id: + try: + initials['build'] = Build.objects.get(pk=build_id) + except Build.DoesNotExist: + pass + + return initials \ No newline at end of file diff --git a/InvenTree/static/script/inventree/build.js b/InvenTree/static/script/inventree/build.js index 4a9ceae443..c644aa4061 100644 --- a/InvenTree/static/script/inventree/build.js +++ b/InvenTree/static/script/inventree/build.js @@ -6,6 +6,7 @@ function makeBuildTable(table, options) { * options: * build - ID of the build object * part - ID of the part object for the build + * new_item_url - URL to create a new BuildItem * */ @@ -20,17 +21,28 @@ function makeBuildTable(table, options) { onExpandRow: function(index, row, $detail) { fillAllocationTable( $("#part-table-" + row.pk), + index, row, + table, { build: options.build }, ); }, columns: [ + { + field: 'pk', + title: 'ID', + visible: false, + }, { field: 'sub_part_detail.name', title: 'Part', }, + { + field: 'note', + title: 'Note', + }, { field: 'quantity', title: 'Required', @@ -38,7 +50,28 @@ function makeBuildTable(table, options) { { field: 'allocated', title: 'Allocated', - } + formatter: function(value, row, index, field) { + var html = ""; + + var url = options.new_item_url; + + url += "?build=" + options.build + "&part=" + row.sub_part; + + if (value) { + html = value; + } else { + html = "0"; + } + + html += "
"; + + html += ""; + + html += "
"; + + return html; + } + }, ], }); @@ -48,6 +81,16 @@ function makeBuildTable(table, options) { }).then(function(response) { table.bootstrapTable('load', response) }); + + // Button callbacks + table.on('click', '.new-item-button', function() { + var button = $(this); + + launchModalForm(button.attr('url'), { + success: function() { + } + }); + }); } @@ -68,12 +111,14 @@ function makeAllocationTable(options) { return table; } -function fillAllocationTable(table, parent_row, options) { +function fillAllocationTable(table, index, parent_row, parent_table, options) { /* Load data into an allocation table, * and update the total stock allocation count in the parent row. * * table - the part allocation table - * row - parent row in the build allocation table + * index - row index in the parent table + * parent_row - parent row data in the build allocation table + * parent_table - the parent build table * * options: * build - pk of the Build object @@ -113,5 +158,15 @@ function fillAllocationTable(table, parent_row, options) { for (var i = 0; i < allocationData.length; i++) { allocated += allocationData[i].quantity; } + + // Update the parent_row data + parent_row.quantity = allocated; + + /*parent_table.bootstrapTable('updateRow', + { + index: index, + row: parent_row + } + );*/ }); } \ No newline at end of file