Bom export with substitute part numbers (#4203)

* Added BOM export option to export substitute part numbers

* Small documentation fix

* Fixed the behaviour for multiple lines with different amount of substitutes

* Changed UI naming to "alternative parts"
This commit is contained in:
bloemp 2023-01-24 23:35:26 +01:00 committed by GitHub
parent 8316086c61
commit 61e5244789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 3 deletions

View File

@ -12,7 +12,7 @@ from InvenTree.helpers import (DownloadFile, GetExportFormats, normalize,
str2bool)
from .admin import BomItemResource
from .models import BomItem, Part
from .models import BomItem, BomItemSubstitute, Part
def IsValidBOMFormat(fmt):
@ -58,6 +58,7 @@ def ExportBom(part: Part, fmt='csv', cascade: bool = False, max_levels: int = No
supplier_data (bool, optional): Additonal data that should be added. Defaults to False.
manufacturer_data (bool, optional): Additonal data that should be added. Defaults to False.
pricing_data (bool, optional): Include pricing data in exported BOM. Defaults to False
substitute_part_data (bool, optional): Include substitute part numbers in exported BOM. Defaults to False
Returns:
StreamingHttpResponse: Response that can be passed to the endpoint
@ -68,6 +69,7 @@ def ExportBom(part: Part, fmt='csv', cascade: bool = False, max_levels: int = No
supplier_data = str2bool(kwargs.get('supplier_data', False))
manufacturer_data = str2bool(kwargs.get('manufacturer_data', False))
pricing_data = str2bool(kwargs.get('pricing_data', False))
substitute_part_data = str2bool(kwargs.get('substitute_part_data', False))
if not IsValidBOMFormat(fmt):
fmt = 'csv'
@ -112,6 +114,37 @@ def ExportBom(part: Part, fmt='csv', cascade: bool = False, max_levels: int = No
except AttributeError:
pass
if substitute_part_data:
"""If requested, add extra columns for all substitute part numbers associated with each line item."""
col_index = 0
substitute_cols = {}
for bom_item in bom_items:
substitutes = BomItemSubstitute.objects.filter(bom_item=bom_item)
for s_idx, substitute in enumerate(substitutes):
"""Create substitute part name column"""
name = f'{_("Substitute Part")}{s_idx + 1}'
value = substitute.part.name
try:
substitute_cols[name].update({col_index: value})
except KeyError:
substitute_cols[name] = {col_index: value}
"""Create substitute part description column"""
name = f'{_("Substitute Description")}{s_idx + 1}'
value = substitute.part.description
try:
substitute_cols[name].update({col_index: value})
except KeyError:
substitute_cols[name] = {col_index: value}
col_index = col_index + 1
# Add substitute columns to dataset
add_columns_to_dataset(substitute_cols, len(bom_items))
if parameter_data:
"""If requested, add extra columns for each PartParameter associated with each line item."""

View File

@ -468,6 +468,8 @@ class BomDownload(AjaxView):
parameter_data = str2bool(request.GET.get('parameter_data', False))
substitute_part_data = str2bool(request.GET.get('substitute_part_data', False))
stock_data = str2bool(request.GET.get('stock_data', False))
supplier_data = str2bool(request.GET.get('supplier_data', False))
@ -500,10 +502,11 @@ class BomDownload(AjaxView):
supplier_data=supplier_data,
manufacturer_data=manufacturer_data,
pricing_data=pricing_data,
substitute_part_data=substitute_part_data,
)
def get_data(self):
"""Return a cutsom message"""
"""Return a custom message"""
return {
'info': 'Exported BOM'
}

View File

@ -330,6 +330,12 @@ function exportBom(part_id, options={}) {
required: true,
min_value: 0,
},
substitute_part_data: {
label: '{% trans "Include Alternative Parts" %}',
help_text: '{% trans "Include alternative parts in exported BOM" %}',
type: 'boolean',
value: inventreeLoad('bom-export-substitute_part_data', false),
},
parameter_data: {
label: '{% trans "Include Parameter Data" %}',
help_text: '{% trans "Include part parameter data in exported BOM" %}',
@ -366,6 +372,7 @@ function exportBom(part_id, options={}) {
// Extract values from the form
var field_names = [
'format', 'cascade', 'levels',
'substitute_part_data',
'parameter_data',
'stock_data',
'manufacturer_data',