diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 9e0b2cea29..2e9e748e99 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -859,19 +859,48 @@ class Part(models.Model): self.save() def export_bom(self, **kwargs): + """ Export Bill of Materials to a spreadsheet file. + Includes a row for each item in the BOM. + Also includes extra information such as supplier data. + """ - data = tablib.Dataset(headers=[ + items = self.bom_items.all().order_by('id') + + supplier_names = set() + + headers = [ 'Part', 'Description', 'Quantity', 'Overage', 'Reference', 'Note', - ]) + '', + 'In Stock', + ] - for it in self.bom_items.all().order_by('id'): + # Contstruct list of suppliers for each part + for item in items: + part = item.sub_part + supplier_parts = part.supplier_parts.all() + item.suppliers = {} + + for sp in supplier_parts: + name = sp.supplier.name + supplier_names.add(name) + item.suppliers[name] = sp + + if len(supplier_names) > 0: + headers.append('') + for name in supplier_names: + headers.append(name) + + data = tablib.Dataset(headers=headers) + + for it in items: line = [] + # Information about each BOM item line.append(it.sub_part.full_name) line.append(it.sub_part.description) line.append(it.quantity) @@ -879,6 +908,20 @@ class Part(models.Model): line.append(it.reference) line.append(it.note) + # Extra information about the part + line.append('') + line.append(it.sub_part.available_stock) + + if len(supplier_names) > 0: + line.append('') # Blank column separates supplier info + + for name in supplier_names: + sp = it.suppliers.get(name, None) + if sp: + line.append(sp.SKU) + else: + line.append('') + data.append(line) file_format = kwargs.get('format', 'csv').lower()