New Form and View to create a new BuildItem object

- Allocates stock to a build
This commit is contained in:
Oliver Walters 2019-04-30 15:35:35 +10:00
parent 1b32f9d650
commit 0208c6efe6
5 changed files with 108 additions and 7 deletions

View File

@ -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',
]

View File

@ -28,6 +28,7 @@
{
build: {{ build.pk }},
part: {{ build.part.pk }},
new_item_url: "{% url 'build-item-create' %}",
}
);

View File

@ -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<pk>\d+)/', include(build_detail_urls)),

View File

@ -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

View File

@ -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 += "<div class='btn-group' style='float: right;'>";
html += "<button class='btn btn-success btn-sm new-item-button' type='button' url='" + url + "'>Allocate</button>";
html += "</div>";
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
}
);*/
});
}