From f7b6c68237cf21ae6994a74b2bfcc7a1175af40f Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 22:37:54 +1100 Subject: [PATCH 1/5] Pre-fill the form with the remaining allocation quantity --- InvenTree/templates/js/translated/order.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 7cadfe453d..93445afea8 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -1641,6 +1641,13 @@ function loadSalesOrderLineItemTable(table, options={}) { var line_item = $(table).bootstrapTable('getRowByUniqueId', pk); + // Quantity remaining to be allocated + var remaining = (line_item.quantity || 0) - (line_item.allocated || 0); + + if (remaining < 0) { + remaining = 0; + } + var fields = { // SalesOrderLineItem reference line: { @@ -1657,6 +1664,7 @@ function loadSalesOrderLineItemTable(table, options={}) { } }, quantity: { + value: remaining, }, }; From 92568748cfadb317f52b2fc910eeb0d80365673e Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 23:03:03 +1100 Subject: [PATCH 2/5] Further improvements - Add callback for when a select2 form field is updated - Adjust selected quantity based on returned data - auto_fill the stock_item field --- InvenTree/templates/js/translated/forms.js | 5 +++++ InvenTree/templates/js/translated/order.js | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 1bfe196286..db2c8e46cc 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1426,6 +1426,11 @@ function initializeRelatedField(field, fields, options) { data = item.element.instance; } + // Run optional callback function + if (field.onSelect && data) { + field.onSelect(data, field, options); + } + if (!data.pk) { return field.placeholder || ''; } diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 93445afea8..dfac640fee 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -1661,7 +1661,23 @@ function loadSalesOrderLineItemTable(table, options={}) { in_stock: true, part: line_item.part, exclude_so_allocation: options.order, - } + }, + auto_fill: true, + onSelect: function(data, field, opts) { + // Quantity available from this stock item + + if (!("quantity" in data)) { + return; + } + + // Calculate the available quantity + var available = Math.max((data.quantity || 0) - (data.allocated || 0), 0); + + // Maximum amount that we need + var desired = Math.min(available, remaining); + + updateFieldValue('quantity', desired, {}, opts); + } }, quantity: { value: remaining, @@ -1760,7 +1776,7 @@ function loadSalesOrderLineItemTable(table, options={}) { showFooter: true, uniqueId: 'pk', detailView: show_detail, - detailViewByClick: show_detail, + detailViewByClick: false, detailFilter: function(index, row) { if (pending) { // Order is pending From 8805b0a553e42963417eb75bedd64b04e9ad5283 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 23:14:52 +1100 Subject: [PATCH 3/5] Add similar auto field capabilities to build order --- InvenTree/build/serializers.py | 3 +++ InvenTree/build/templates/build/detail.html | 4 ++++ InvenTree/templates/js/translated/build.js | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 8f76f3e603..0087a1e8b5 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -311,9 +311,12 @@ class BuildAllocationItemSerializer(serializers.Serializer): build = self.context['build'] + # TODO: Fix this validation - allow for variants and substitutes! + """ # BomItem must point to the same 'part' as the parent build if build.part != bom_item.part: raise ValidationError(_("bom_item.part must point to the same part as the build order")) + """ return bom_item diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index 908a7dfa4a..f87eec90a0 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -523,6 +523,10 @@ $('#allocate-selected-items').click(function() { var bom_items = $("#allocation-table-untracked").bootstrapTable("getSelections"); + if (bom_items.length == 0) { + bom_items = $("#allocation-table-untracked").bootstrapTable('getData'); + } + allocateStockToBuild( {{ build.pk }}, {{ build.part.pk }}, diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index 0c291dd8da..d29b81c0b8 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -1404,6 +1404,24 @@ function allocateStockToBuild(build_id, part_id, bom_items, options={}) { render_part_detail: true, render_location_detail: true, auto_fill: true, + onSelect: function(data, field, opts) { + // Adjust the 'quantity' field based on availability + + if (!("quantity" in data)) { + return; + } + + // Quantity remaining to be allocated + var remaining = Math.max((bom_item.required || 0) - (bom_item.allocated || 0), 0); + + // Calculate the available quantity + var available = Math.max((data.quantity || 0) - (data.allocated || 0), 0); + + // Maximum amount that we need + var desired = Math.min(available, remaining); + + updateFieldValue(`items_quantity_${bom_item.pk}`, desired, {}, opts); + }, adjustFilters: function(filters) { // Restrict query to the selected location var location = getFormFieldValue( From f83dc134ef0508a4107aa1d0585c842a18c7cfe8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 23:20:43 +1100 Subject: [PATCH 4/5] linting --- InvenTree/build/serializers.py | 4 ++-- InvenTree/templates/js/translated/build.js | 2 +- InvenTree/templates/js/translated/order.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 0087a1e8b5..f23e2a1db0 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -309,10 +309,10 @@ class BuildAllocationItemSerializer(serializers.Serializer): def validate_bom_item(self, bom_item): - build = self.context['build'] - # TODO: Fix this validation - allow for variants and substitutes! """ + build = self.context['build'] + # BomItem must point to the same 'part' as the parent build if build.part != bom_item.part: raise ValidationError(_("bom_item.part must point to the same part as the build order")) diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index d29b81c0b8..b6c98fc49e 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -1407,7 +1407,7 @@ function allocateStockToBuild(build_id, part_id, bom_items, options={}) { onSelect: function(data, field, opts) { // Adjust the 'quantity' field based on availability - if (!("quantity" in data)) { + if (!('quantity' in data)) { return; } diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index dfac640fee..67fef0b853 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -1666,7 +1666,7 @@ function loadSalesOrderLineItemTable(table, options={}) { onSelect: function(data, field, opts) { // Quantity available from this stock item - if (!("quantity" in data)) { + if (!('quantity' in data)) { return; } From 50a79770e5993eae315e27d28c6087eca8c0d8ca Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 23:37:26 +1100 Subject: [PATCH 5/5] Un-comment lines in build.serializers --- InvenTree/build/serializers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index f23e2a1db0..a18f58fb76 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -310,13 +310,12 @@ class BuildAllocationItemSerializer(serializers.Serializer): def validate_bom_item(self, bom_item): # TODO: Fix this validation - allow for variants and substitutes! - """ + build = self.context['build'] # BomItem must point to the same 'part' as the parent build if build.part != bom_item.part: raise ValidationError(_("bom_item.part must point to the same part as the build order")) - """ return bom_item