diff --git a/src/frontend/src/forms/StockForms.tsx b/src/frontend/src/forms/StockForms.tsx index 70476d591e..a7689e30e7 100644 --- a/src/frontend/src/forms/StockForms.tsx +++ b/src/frontend/src/forms/StockForms.tsx @@ -1038,6 +1038,30 @@ function stockMergeFields(items: any[]): ApiFormFieldSet { const records = Object.fromEntries(items.map((item) => [item.pk, item])); + // Extract all non-null location values from the items + const locationValues = [ + ...new Set( + items.filter((item) => item.location).map((item) => item.location) + ) + ]; + + // Extract all non-null default location values from the items + const defaultLocationValues = [ + ...new Set( + items + .filter((item) => item.part_detail?.default_location) + .map((item) => item.part_detail?.default_location) + ) + ]; + + // Select a default location value + const defaultLocation = + locationValues.length === 1 + ? locationValues[0] + : defaultLocationValues.length === 1 + ? defaultLocationValues[0] + : undefined; + const fields: ApiFormFieldSet = { items: { field_type: 'table', @@ -1067,7 +1091,7 @@ function stockMergeFields(items: any[]): ApiFormFieldSet { ] }, location: { - default: items[0]?.part_detail?.default_location, + default: defaultLocation, filters: { structural: false }