From 06c1534093073adce478ca27be6df8249ed3798f Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 31 Dec 2015 17:09:45 +0200 Subject: [PATCH] Added getString helper to CSV import --- app/Ninja/Import/BaseTransformer.php | 5 ++ app/Ninja/Import/CSV/ClientTransformer.php | 22 ++++----- app/Ninja/Import/CSV/InvoiceTransformer.php | 8 ++-- app/Services/ImportService.php | 52 +++++++++++---------- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index d89d31bd030c..2d6885b81d00 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -19,6 +19,11 @@ class BaseTransformer extends TransformerAbstract return isset($this->maps[ENTITY_CLIENT][$name]); } + protected function getString($data, $field) + { + return (isset($data->$field) && $data->$field) ? $data->$field : null; + } + protected function getClientId($name) { $name = strtolower($name); diff --git a/app/Ninja/Import/CSV/ClientTransformer.php b/app/Ninja/Import/CSV/ClientTransformer.php index 11cac64c6093..b480d5e88f4e 100644 --- a/app/Ninja/Import/CSV/ClientTransformer.php +++ b/app/Ninja/Import/CSV/ClientTransformer.php @@ -13,19 +13,19 @@ class ClientTransformer extends BaseTransformer return new Item($data, function ($data) { return [ - 'name' => isset($data->name) ? $data->name : null, - 'work_phone' => isset($data->work_phone) ? $data->work_phone : null, - 'address1' => isset($data->address1) ? $data->address1 : null, - 'city' => isset($data->city) ? $data->city : null, - 'state' => isset($data->state) ? $data->state : null, - 'postal_code' => isset($data->postal_code) ? $data->postal_code : null, - 'private_notes' => isset($data->notes) ? $data->notes : null, + 'name' => $this->getString($data, 'name'), + 'work_phone' => $this->getString($data, 'work_phone'), + 'address1' => $this->getString($data, 'address1'), + 'city' => $this->getString($data, 'city'), + 'state' => $this->getString($data, 'state'), + 'postal_code' => $this->getString($data, 'postal_code'), + 'private_notes' => $this->getString($data, 'notes'), 'contacts' => [ [ - 'first_name' => isset($data->first_name) ? $data->first_name : null, - 'last_name' => isset($data->last_name) ? $data->last_name : null, - 'email' => isset($data->email) ? $data->email : null, - 'phone' => isset($data->phone) ? $data->phone : null, + 'first_name' => $this->getString($data, 'first_name'), + 'last_name' => $this->getString($data, 'last_name'), + 'email' => $this->getString($data, 'email'), + 'phone' => $this->getString($data, 'phone'), ], ], 'country_id' => isset($data->country) ? $this->getCountryId($data->country) : null, diff --git a/app/Ninja/Import/CSV/InvoiceTransformer.php b/app/Ninja/Import/CSV/InvoiceTransformer.php index 108763c4baf7..e58bfe335ed2 100644 --- a/app/Ninja/Import/CSV/InvoiceTransformer.php +++ b/app/Ninja/Import/CSV/InvoiceTransformer.php @@ -20,14 +20,14 @@ class InvoiceTransformer extends BaseTransformer 'client_id' => $this->getClientId($data->name), 'invoice_number' => isset($data->invoice_number) ? $this->getInvoiceNumber($data->invoice_number) : null, 'paid' => isset($data->paid) ? (float) $data->paid : null, - 'po_number' => isset($data->po_number) ? $data->po_number : null, - 'terms' => isset($data->terms) ? $data->terms : null, - 'public_notes' => isset($data->notes) ? $data->notes : null, + 'po_number' => $this->getString($data, 'po_number'), + 'terms' => $this->getString($data, 'terms'), + 'public_notes' => $this->getString($data, 'public_notes'), 'invoice_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null, 'invoice_items' => [ [ 'product_key' => '', - 'notes' => isset($data->notes) ? $data->notes : null, + 'notes' => $this->getString($data, 'notes'), 'cost' => isset($data->amount) ? (float) $data->amount : null, 'qty' => 1, ] diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 567c7d2969b4..b557e067b2bf 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -64,7 +64,7 @@ class ImportService foreach ($files as $entityType => $file) { $results[$entityType] = $this->execute($source, $entityType, $file); } - + return $results; } @@ -76,26 +76,27 @@ class ImportService ]; // Convert the data - $row_list = array(); + $row_list = array(); $maps = $this->createMaps(); Excel::load($file, function ($reader) use ($source, $entityType, $maps, &$row_list, &$results) { $this->checkData($entityType, count($reader->all())); $reader->each(function ($row) use ($source, $entityType, $maps, &$row_list, &$results) { $data_index = $this->transformRow($source, $entityType, $row, $maps); - - if ($data_index !== false){ - if($data_index !== true){// Wasn't merged with another row - $row_list[] = array('row'=>$row, 'data_index'=>$data_index); + + if ($data_index !== false) { + if ($data_index !== true) { + // Wasn't merged with another row + $row_list[] = array('row' => $row, 'data_index' => $data_index); } } else { $results[RESULT_FAILURE][] = $row; } }); }); - + // Save the data - foreach($row_list as $row_data){ + foreach ($row_list as $row_data) { $result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index'], $maps); if ($result) { $results[RESULT_SUCCESS][] = $result; @@ -124,29 +125,29 @@ class ImportService $invoice = Invoice::createNew(); $data['invoice_number'] = $account->getNextInvoiceNumber($invoice); } - + if ($this->validate($source, $data, $entityType) !== true) { return false; } - - if($entityType == ENTITY_INVOICE){ - if(empty($this->processedRows[$data['invoice_number']])){ + + if ($entityType == ENTITY_INVOICE) { + if (empty($this->processedRows[$data['invoice_number']])) { $this->processedRows[$data['invoice_number']] = $data; - } - else{ + } else { // Merge invoice items $this->processedRows[$data['invoice_number']]['invoice_items'] = array_merge($this->processedRows[$data['invoice_number']]['invoice_items'], $data['invoice_items']); + return true; } + } else { + $this->processedRows[] = $data; } - else{ - $this->processedRows[] = $data; - } - + end($this->processedRows); + return key($this->processedRows); } - + private function saveData($source, $entityType, $row, $data_index, $maps) { $data = $this->processedRows[$data_index]; @@ -421,20 +422,21 @@ class ImportService $row = $this->convertToObject($entityType, $row, $map); $data_index = $this->transformRow($source, $entityType, $row, $maps); - + if ($data_index !== false) { - if($data_index !== true){// Wasn't merged with another row - $row_list[] = array('row'=>$row, 'data_index'=>$data_index); + if ($data_index !== true) { + // Wasn't merged with another row + $row_list[] = array('row' => $row, 'data_index' => $data_index); } } else { $results[RESULT_FAILURE][] = $row; } } - + // Save the data - foreach($row_list as $row_data){ + foreach ($row_list as $row_data) { $result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index'], $maps); - + if ($result) { $results[RESULT_SUCCESS][] = $result; } else {