From 8805b0a553e42963417eb75bedd64b04e9ad5283 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 20 Oct 2021 23:14:52 +1100 Subject: [PATCH] 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(