mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-01 04:49:36 +00:00
feat: add supplier SPN lookup for BOM import
Add automatic part linking via supplier part numbers (SPNs) in the BOM importer. When a Part-DB ID is not provided, the importer now searches for existing parts by matching supplier SPNs from the CSV with orderdetail records in the database. This allows automatic part linking when KiCad schematic BOMs contain supplier information like LCSC SPN, Mouser SPN, etc., improving the import workflow for users who track parts by supplier part numbers.
This commit is contained in:
parent
ae4c0786b2
commit
ba4b8d072f
1 changed files with 39 additions and 1 deletions
|
|
@ -277,8 +277,11 @@ class BOMImporter
|
||||||
// Fetch suppliers once for efficiency
|
// Fetch suppliers once for efficiency
|
||||||
$suppliers = $this->entityManager->getRepository(\App\Entity\Parts\Supplier::class)->findAll();
|
$suppliers = $this->entityManager->getRepository(\App\Entity\Parts\Supplier::class)->findAll();
|
||||||
$supplierSPNKeys = [];
|
$supplierSPNKeys = [];
|
||||||
|
$suppliersByName = []; // Map supplier names to supplier objects
|
||||||
foreach ($suppliers as $supplier) {
|
foreach ($suppliers as $supplier) {
|
||||||
$supplierSPNKeys[] = $supplier->getName() . ' SPN';
|
$supplierName = $supplier->getName();
|
||||||
|
$supplierSPNKeys[] = $supplierName . ' SPN';
|
||||||
|
$suppliersByName[$supplierName] = $supplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($csv->getRecords() as $offset => $entry) {
|
foreach ($csv->getRecords() as $offset => $entry) {
|
||||||
|
|
@ -356,6 +359,41 @@ class BOMImporter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to link existing part based on supplier part number if no Part-DB ID is given
|
||||||
|
if ($part === null) {
|
||||||
|
// Check all available supplier SPN fields
|
||||||
|
foreach ($suppliersByName as $supplierName => $supplier) {
|
||||||
|
$supplier_spn = null;
|
||||||
|
|
||||||
|
if (isset($mapped_entry[$supplierName . ' SPN']) && !empty(trim($mapped_entry[$supplierName . ' SPN']))) {
|
||||||
|
$supplier_spn = trim($mapped_entry[$supplierName . ' SPN']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($supplier_spn !== null) {
|
||||||
|
// Query for orderdetails with matching supplier and SPN
|
||||||
|
$orderdetail = $this->entityManager->getRepository(\App\Entity\PriceInformations\Orderdetail::class)
|
||||||
|
->findOneBy([
|
||||||
|
'supplier' => $supplier,
|
||||||
|
'supplierpartnr' => $supplier_spn,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($orderdetail !== null && $orderdetail->getPart() !== null) {
|
||||||
|
$part = $orderdetail->getPart();
|
||||||
|
$name = $part->getName(); // Update name with actual part name
|
||||||
|
|
||||||
|
$this->logger->info('Linked BOM entry to existing part via supplier SPN', [
|
||||||
|
'supplier' => $supplierName,
|
||||||
|
'supplier_spn' => $supplier_spn,
|
||||||
|
'part_id' => $part->getID(),
|
||||||
|
'part_name' => $part->getName(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
break; // Stop searching once a match is found
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create unique key for this entry (name + part ID)
|
// Create unique key for this entry (name + part ID)
|
||||||
$entry_key = $name . '|' . ($part ? $part->getID() : 'null');
|
$entry_key = $name . '|' . ($part ? $part->getID() : 'null');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue