From 3aad70934b2a7199f5caaa063e61d33337ef58a6 Mon Sep 17 00:00:00 2001 From: Niklas <44636701+MayNiklas@users.noreply.github.com> Date: Sun, 25 Jan 2026 21:32:14 +0100 Subject: [PATCH] Support dynamic supplier SPNs in BOM import comments (#1208) * Fix: Use correct field name for LCSC supplier part numbers in BOM import The field mapping system uses 'LCSC SPN' as the target field name for LCSC supplier part numbers (following the pattern SupplierName + ' SPN'), but the code in parseKiCADSchematic() was incorrectly checking for 'LCSC'. This caused LCSC supplier part numbers to be silently ignored and not included in the BOM entry comments during schematic import. Changed isset($mapped_entry['LCSC']) to isset($mapped_entry['LCSC SPN']) to match the actual field name produced by the field mapping system. * regression test: check for LCSC SPN in comment * Support dynamic supplier SPNs in BOM import comments Replace hardcoded LCSC SPN handling with dynamic supplier lookup to support all configured suppliers in BOM import. This allows any supplier defined in Part-DB to have their SPN fields recognized and included in the BOM entry comments during BOM import. * Optimize BOM import by only calculating supplier SPN keys once --- .../ImportExportSystem/BOMImporter.php | 16 +++++++++++++-- .../ImportExportSystem/BOMImporterTest.php | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Services/ImportExportSystem/BOMImporter.php b/src/Services/ImportExportSystem/BOMImporter.php index e511c04d..33a402cb 100644 --- a/src/Services/ImportExportSystem/BOMImporter.php +++ b/src/Services/ImportExportSystem/BOMImporter.php @@ -274,6 +274,13 @@ class BOMImporter $entries_by_key = []; // Track entries by name+part combination $mapped_entries = []; // Collect all mapped entries for validation + // Fetch suppliers once for efficiency + $suppliers = $this->entityManager->getRepository(\App\Entity\Parts\Supplier::class)->findAll(); + $supplierSPNKeys = []; + foreach ($suppliers as $supplier) { + $supplierSPNKeys[] = $supplier->getName() . ' SPN'; + } + foreach ($csv->getRecords() as $offset => $entry) { // Apply field mapping to translate column names $mapped_entry = $this->applyFieldMapping($entry, $field_mapping, $field_priorities); @@ -400,9 +407,14 @@ class BOMImporter if (isset($mapped_entry['Manufacturer'])) { $comment_parts[] = 'Manf: ' . $mapped_entry['Manufacturer']; } - if (isset($mapped_entry['LCSC'])) { - $comment_parts[] = 'LCSC: ' . $mapped_entry['LCSC']; + + // Add supplier part numbers dynamically + foreach ($supplierSPNKeys as $spnKey) { + if (isset($mapped_entry[$spnKey]) && !empty($mapped_entry[$spnKey])) { + $comment_parts[] = $spnKey . ': ' . $mapped_entry[$spnKey]; + } } + if (isset($mapped_entry['Supplier and ref'])) { $comment_parts[] = $mapped_entry['Supplier and ref']; } diff --git a/tests/Services/ImportExportSystem/BOMImporterTest.php b/tests/Services/ImportExportSystem/BOMImporterTest.php index 52c633d0..47ddcc24 100644 --- a/tests/Services/ImportExportSystem/BOMImporterTest.php +++ b/tests/Services/ImportExportSystem/BOMImporterTest.php @@ -353,6 +353,16 @@ class BOMImporterTest extends WebTestCase public function testStringToBOMEntriesKiCADSchematic(): void { + // Create test suppliers for this test + $lcscSupplier = new Supplier(); + $lcscSupplier->setName('LCSC'); + $mouserSupplier = new Supplier(); + $mouserSupplier->setName('Mouser'); + + $this->entityManager->persist($lcscSupplier); + $this->entityManager->persist($mouserSupplier); + $this->entityManager->flush(); + $input = <<assertStringContainsString('Value: 10k', $bom_entries[0]->getComment()); $this->assertStringContainsString('MPN: CRCW080510K0FKEA', $bom_entries[0]->getComment()); $this->assertStringContainsString('Manf: Vishay', $bom_entries[0]->getComment()); + $this->assertStringContainsString('LCSC SPN: C123456', $bom_entries[0]->getComment()); + $this->assertStringContainsString('Mouser SPN: 123-M10K', $bom_entries[0]->getComment()); + // Check second entry $this->assertEquals('C1', $bom_entries[1]->getMountnames()); $this->assertEquals(1.0, $bom_entries[1]->getQuantity()); + $this->assertStringContainsString('LCSC SPN: C789012', $bom_entries[1]->getComment()); + $this->assertStringContainsString('Mouser SPN: 80-CL21A104KOCLRNC', $bom_entries[1]->getComment()); + + // Clean up + $this->entityManager->remove($lcscSupplier); + $this->entityManager->remove($mouserSupplier); + $this->entityManager->flush(); } public function testStringToBOMEntriesKiCADSchematicWithPriority(): void