Skip to content

Commit 3aad709

Browse files
authored
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
1 parent e15d12c commit 3aad709

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Services/ImportExportSystem/BOMImporter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ private function parseKiCADSchematic(string $data, array $options = []): array
274274
$entries_by_key = []; // Track entries by name+part combination
275275
$mapped_entries = []; // Collect all mapped entries for validation
276276

277+
// Fetch suppliers once for efficiency
278+
$suppliers = $this->entityManager->getRepository(\App\Entity\Parts\Supplier::class)->findAll();
279+
$supplierSPNKeys = [];
280+
foreach ($suppliers as $supplier) {
281+
$supplierSPNKeys[] = $supplier->getName() . ' SPN';
282+
}
283+
277284
foreach ($csv->getRecords() as $offset => $entry) {
278285
// Apply field mapping to translate column names
279286
$mapped_entry = $this->applyFieldMapping($entry, $field_mapping, $field_priorities);
@@ -400,9 +407,14 @@ private function parseKiCADSchematic(string $data, array $options = []): array
400407
if (isset($mapped_entry['Manufacturer'])) {
401408
$comment_parts[] = 'Manf: ' . $mapped_entry['Manufacturer'];
402409
}
403-
if (isset($mapped_entry['LCSC'])) {
404-
$comment_parts[] = 'LCSC: ' . $mapped_entry['LCSC'];
410+
411+
// Add supplier part numbers dynamically
412+
foreach ($supplierSPNKeys as $spnKey) {
413+
if (isset($mapped_entry[$spnKey]) && !empty($mapped_entry[$spnKey])) {
414+
$comment_parts[] = $spnKey . ': ' . $mapped_entry[$spnKey];
415+
}
405416
}
417+
406418
if (isset($mapped_entry['Supplier and ref'])) {
407419
$comment_parts[] = $mapped_entry['Supplier and ref'];
408420
}

tests/Services/ImportExportSystem/BOMImporterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ public function testValidateFieldMappingInvalidTarget(): void
353353

354354
public function testStringToBOMEntriesKiCADSchematic(): void
355355
{
356+
// Create test suppliers for this test
357+
$lcscSupplier = new Supplier();
358+
$lcscSupplier->setName('LCSC');
359+
$mouserSupplier = new Supplier();
360+
$mouserSupplier->setName('Mouser');
361+
362+
$this->entityManager->persist($lcscSupplier);
363+
$this->entityManager->persist($mouserSupplier);
364+
$this->entityManager->flush();
365+
356366
$input = <<<CSV
357367
"Reference","Value","Footprint","Quantity","MPN","Manufacturer","LCSC SPN","Mouser SPN"
358368
"R1,R2","10k","R_0805_2012Metric",2,"CRCW080510K0FKEA","Vishay","C123456","123-M10K"
@@ -386,10 +396,20 @@ public function testStringToBOMEntriesKiCADSchematic(): void
386396
$this->assertStringContainsString('Value: 10k', $bom_entries[0]->getComment());
387397
$this->assertStringContainsString('MPN: CRCW080510K0FKEA', $bom_entries[0]->getComment());
388398
$this->assertStringContainsString('Manf: Vishay', $bom_entries[0]->getComment());
399+
$this->assertStringContainsString('LCSC SPN: C123456', $bom_entries[0]->getComment());
400+
$this->assertStringContainsString('Mouser SPN: 123-M10K', $bom_entries[0]->getComment());
401+
389402

390403
// Check second entry
391404
$this->assertEquals('C1', $bom_entries[1]->getMountnames());
392405
$this->assertEquals(1.0, $bom_entries[1]->getQuantity());
406+
$this->assertStringContainsString('LCSC SPN: C789012', $bom_entries[1]->getComment());
407+
$this->assertStringContainsString('Mouser SPN: 80-CL21A104KOCLRNC', $bom_entries[1]->getComment());
408+
409+
// Clean up
410+
$this->entityManager->remove($lcscSupplier);
411+
$this->entityManager->remove($mouserSupplier);
412+
$this->entityManager->flush();
393413
}
394414

395415
public function testStringToBOMEntriesKiCADSchematicWithPriority(): void

0 commit comments

Comments
 (0)