mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-15 06:39:33 +00:00
BOMImporter: Verbesserung des CSV-Parsing
Das Parsing wurde angepasst, um sowohl Komma- als auch Semikolon-getrennte CSV-Dateien zu unterstützen. Zudem werden Spaltennamen in Kleinbuchstaben konvertiert und zusätzliche Fallback-Logik für bestimmte Felder hinzugefügt.
This commit is contained in:
parent
5c54191488
commit
d7e4db38e8
1 changed files with 27 additions and 4 deletions
|
|
@ -433,14 +433,27 @@ class BOMImporter
|
||||||
function parseCsv(string $csvData, string $objectType = ProjectBOMEntry::class): ImporterResult
|
function parseCsv(string $csvData, string $objectType = ProjectBOMEntry::class): ImporterResult
|
||||||
{
|
{
|
||||||
$result = new ImporterResult();
|
$result = new ImporterResult();
|
||||||
$rows = explode("\n", trim($csvData));
|
$rows = explode("\r\n", trim($csvData));
|
||||||
$headers = str_getcsv(array_shift($rows), ';');
|
$headers = str_getcsv(array_shift($rows), ',');
|
||||||
|
|
||||||
|
if (count($headers) === 1 && isset($headers[0])) {
|
||||||
|
//If only one column was recognized, try fallback with semicolon as a separator
|
||||||
|
$headers = str_getcsv($headers[0], ';');
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($rows as $key => $row) {
|
foreach ($rows as $key => $row) {
|
||||||
$entry = [];
|
$entry = [];
|
||||||
$values = str_getcsv($row, ';');
|
$values = str_getcsv($row, ',');
|
||||||
|
|
||||||
|
if (count($values) === 1 || count($values) !== count($headers)) {
|
||||||
|
//If only one column was recognized, try fallback with semicolon as a separator
|
||||||
|
$values = str_getcsv($row, ';');
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($headers as $index => $column) {
|
foreach ($headers as $index => $column) {
|
||||||
|
//Change the column names in small letters
|
||||||
|
$column = strtolower($column);
|
||||||
|
|
||||||
//Convert column name into hierarchy
|
//Convert column name into hierarchy
|
||||||
$path = explode('_', $column);
|
$path = explode('_', $column);
|
||||||
$temp = &$entry;
|
$temp = &$entry;
|
||||||
|
|
@ -493,6 +506,11 @@ class BOMImporter
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($entry['id']) && is_numeric($entry['id'])) {
|
||||||
|
//Use id column as a fallback for the expected part_id column
|
||||||
|
$entry['part']['id'] = (int) $entry['id'];
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($entry['part'])) {
|
if (isset($entry['part'])) {
|
||||||
$this->processPart($entry, $result, $key, $objectType, self::IMPORT_TYPE_CSV);
|
$this->processPart($entry, $result, $key, $objectType, self::IMPORT_TYPE_CSV);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -775,7 +793,12 @@ class BOMImporter
|
||||||
$bomEntry->setQuantity((float) $entry['quantity']);
|
$bomEntry->setQuantity((float) $entry['quantity']);
|
||||||
|
|
||||||
if (isset($entry['name'])) {
|
if (isset($entry['name'])) {
|
||||||
$bomEntry->setName(trim($entry['name']) === '' ? null : trim ($entry['name']));
|
$givenName = trim($entry['name']) === '' ? null : trim ($entry['name']);
|
||||||
|
|
||||||
|
if ($givenName !== null && $bomEntry->getPart() !== null && $bomEntry->getPart()->getName() !== $givenName) {
|
||||||
|
//Apply different names for parts list entry
|
||||||
|
$bomEntry->setName(trim($entry['name']) === '' ? null : trim ($entry['name']));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$bomEntry->setName(null);
|
$bomEntry->setName(null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue