From 7b92eb62fe9a4babd5d455ba33d703acc6aa8543 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 8 Jan 2017 16:02:52 +0200 Subject: [PATCH] Improve export CSV import --- app/Ninja/Import/BaseTransformer.php | 12 ++++++------ app/Services/ImportService.php | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index 3400d5dade..528c5fc2bc 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -79,7 +79,7 @@ class BaseTransformer extends TransformerAbstract */ protected function getClientId($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null; } @@ -89,7 +89,7 @@ class BaseTransformer extends TransformerAbstract */ protected function getProductId($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null; } @@ -99,7 +99,7 @@ class BaseTransformer extends TransformerAbstract */ protected function getCountryId($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null; } @@ -109,7 +109,7 @@ class BaseTransformer extends TransformerAbstract */ protected function getCountryIdBy2($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null; } @@ -196,7 +196,7 @@ class BaseTransformer extends TransformerAbstract */ public function getVendorId($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps[ENTITY_VENDOR][$name]) ? $this->maps[ENTITY_VENDOR][$name] : null; } @@ -207,7 +207,7 @@ class BaseTransformer extends TransformerAbstract */ public function getExpenseCategoryId($name) { - $name = strtolower($name); + $name = strtolower(trim($name)); return isset($this->maps[ENTITY_EXPENSE_CATEGORY][$name]) ? $this->maps[ENTITY_EXPENSE_CATEGORY][$name] : null; } diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 8d9214acf5..09305a91b4 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -286,6 +286,8 @@ class ImportService */ private function transformRow($source, $entityType, $row) { + // TODO skip import if row is blank + $transformer = $this->getTransformer($source, $entityType, $this->maps); // Create expesnse category @@ -297,10 +299,9 @@ class ImportService $this->addExpenseCategoryToMaps($category); } } - if ( ! empty($row->vendor)) { - $vendorId = $transformer->getVendorId($row->vendor); - if ( ! $vendorId) { - $vendor = $this->vendorRepo->save(['name' => $row->vendor, 'vendor_contact' => []]); + if ( ! empty($row->vendor) && ($vendorName = trim($row->vendor))) { + if ( ! $transformer->getVendorId($vendorName)) { + $vendor = $this->vendorRepo->save(['name' => $vendorName, 'vendor_contact' => []]); $this->addVendorToMaps($vendor); } }