From f5530a30b290ea5fc4e91f5f2a6bdbbb14fb7871 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 31 Dec 2015 15:36:53 +0200 Subject: [PATCH] Revert "Re-revert "Improve Import Support""" --- app/Ninja/Import/BaseTransformer.php | 12 --- app/Ninja/Import/CSV/PaymentTransformer.php | 2 +- .../Import/FreshBooks/PaymentTransformer.php | 2 +- .../Import/Harvest/PaymentTransformer.php | 2 +- .../Import/Hiveage/PaymentTransformer.php | 2 +- .../Import/Invoiceable/PaymentTransformer.php | 2 +- .../Import/Nutcache/PaymentTransformer.php | 2 +- app/Ninja/Import/Ronin/PaymentTransformer.php | 2 +- app/Ninja/Import/Wave/InvoiceTransformer.php | 37 --------- app/Ninja/Import/Wave/PaymentTransformer.php | 23 ----- app/Services/ImportService.php | 83 ++++--------------- resources/lang/en/texts.php | 1 - 12 files changed, 21 insertions(+), 149 deletions(-) delete mode 100644 app/Ninja/Import/Wave/InvoiceTransformer.php delete mode 100644 app/Ninja/Import/Wave/PaymentTransformer.php diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index d89d31bd03..c816993d68 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -64,22 +64,10 @@ class BaseTransformer extends TransformerAbstract return str_pad($number, 4, '0', STR_PAD_LEFT); } - protected function getInvoiceId($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE][$invoiceNumber] : null; - } - protected function hasInvoice($invoiceNumber) { $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]); } - protected function getInvoiceClientId($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - return isset($this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber])? $this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber] : null; - } - } \ No newline at end of file diff --git a/app/Ninja/Import/CSV/PaymentTransformer.php b/app/Ninja/Import/CSV/PaymentTransformer.php index 428db37fc7..7acd3d88f8 100644 --- a/app/Ninja/Import/CSV/PaymentTransformer.php +++ b/app/Ninja/Import/CSV/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => $data->paid, 'payment_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null, 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/FreshBooks/PaymentTransformer.php b/app/Ninja/Import/FreshBooks/PaymentTransformer.php index def45a5eac..1f69fdbacf 100644 --- a/app/Ninja/Import/FreshBooks/PaymentTransformer.php +++ b/app/Ninja/Import/FreshBooks/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => $data->paid, 'payment_date_sql' => $data->create_date, 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Harvest/PaymentTransformer.php b/app/Ninja/Import/Harvest/PaymentTransformer.php index cdb53c6d52..0efd442886 100644 --- a/app/Ninja/Import/Harvest/PaymentTransformer.php +++ b/app/Ninja/Import/Harvest/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => $data->paid_amount, 'payment_date_sql' => $this->getDate($data->last_payment_date, 'm/d/Y'), 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Hiveage/PaymentTransformer.php b/app/Ninja/Import/Hiveage/PaymentTransformer.php index c1debef5bf..d6232d05bc 100644 --- a/app/Ninja/Import/Hiveage/PaymentTransformer.php +++ b/app/Ninja/Import/Hiveage/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => $data->paid_total, 'payment_date_sql' => $this->getDate($data->last_paid_on), 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Invoiceable/PaymentTransformer.php b/app/Ninja/Import/Invoiceable/PaymentTransformer.php index 453c658f63..c52494cdc6 100644 --- a/app/Ninja/Import/Invoiceable/PaymentTransformer.php +++ b/app/Ninja/Import/Invoiceable/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => $data->paid, 'payment_date_sql' => $data->date_paid, 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Nutcache/PaymentTransformer.php b/app/Ninja/Import/Nutcache/PaymentTransformer.php index 42c9e1dee6..04e783361f 100644 --- a/app/Ninja/Import/Nutcache/PaymentTransformer.php +++ b/app/Ninja/Import/Nutcache/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => (float) $data->paid_to_date, 'payment_date_sql' => $this->getDate($data->date), 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Ronin/PaymentTransformer.php b/app/Ninja/Import/Ronin/PaymentTransformer.php index e336c5ce65..c041014562 100644 --- a/app/Ninja/Import/Ronin/PaymentTransformer.php +++ b/app/Ninja/Import/Ronin/PaymentTransformer.php @@ -12,7 +12,7 @@ class PaymentTransformer extends BaseTransformer 'amount' => (float) $data->total - (float) $data->balance, 'payment_date_sql' => $data->date_paid, 'client_id' => $data->client_id, - 'invoice_id' => $this->getInvoiceId($data->invoice_id), + 'invoice_id' => $data->invoice_id, ]; }); } diff --git a/app/Ninja/Import/Wave/InvoiceTransformer.php b/app/Ninja/Import/Wave/InvoiceTransformer.php deleted file mode 100644 index 9d1cde0d6e..0000000000 --- a/app/Ninja/Import/Wave/InvoiceTransformer.php +++ /dev/null @@ -1,37 +0,0 @@ -getClientId($data->customer)) { - return false; - } - - if ($this->hasInvoice($data->invoice_num)) { - return false; - } - - return new Item($data, function ($data) { - return [ - 'client_id' => $this->getClientId($data->customer), - 'invoice_number' => $this->getInvoiceNumber($data->invoice_num), - 'po_number' => $data->po_so, - 'invoice_date_sql' => $this->getDate($data->invoice_date), - 'due_date_sql' => $this->getDate($data->due_date), - 'paid' => 0, - 'invoice_items' => [ - [ - 'product_key' => $data->product, - 'notes' => $data->description, - 'cost' => (float) $data->amount, - 'qty' => (float) $data->quantity, - ] - ], - ]; - }); - } -} \ No newline at end of file diff --git a/app/Ninja/Import/Wave/PaymentTransformer.php b/app/Ninja/Import/Wave/PaymentTransformer.php deleted file mode 100644 index 522fe8ff92..0000000000 --- a/app/Ninja/Import/Wave/PaymentTransformer.php +++ /dev/null @@ -1,23 +0,0 @@ -getInvoiceClientId($data->invoice_num)) { - return false; - } - - return new Item($data, function ($data) use ($maps) { - return [ - 'amount' => (float) $data->amount, - 'payment_date_sql' => $this->getDate($data->payment_date), - 'client_id' => $this->getInvoiceClientId($data->invoice_num), - 'invoice_id' => $this->getInvoiceId($data->invoice_num), - ]; - }); - } -} \ No newline at end of file diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 567c7d2969..c5efffb3ff 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -23,13 +23,11 @@ class ImportService protected $invoiceRepo; protected $clientRepo; protected $contactRepo; - protected $processedRows = array(); public static $entityTypes = [ ENTITY_CLIENT, ENTITY_CONTACT, ENTITY_INVOICE, - ENTITY_PAYMENT, ENTITY_TASK, ]; @@ -75,45 +73,31 @@ class ImportService RESULT_FAILURE => [], ]; - // Convert the data - $row_list = array(); - $maps = $this->createMaps(); - Excel::load($file, function ($reader) use ($source, $entityType, $maps, &$row_list, &$results) { + Excel::load($file, function ($reader) use ($source, $entityType, &$results) { $this->checkData($entityType, count($reader->all())); + $maps = $this->createMaps(); - $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); - } + $reader->each(function ($row) use ($source, $entityType, $maps, &$results) { + $result = $this->saveData($source, $entityType, $row, $maps); + + if ($result) { + $results[RESULT_SUCCESS][] = $result; } else { $results[RESULT_FAILURE][] = $row; } }); }); - - // Save the 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 { - $results[RESULT_FAILURE][] = $row_data['row']; - } - } return $results; } - private function transformRow($source, $entityType, $row, $maps) + private function saveData($source, $entityType, $row, $maps) { $transformer = $this->getTransformer($source, $entityType, $maps); $resource = $transformer->transform($row, $maps); if (!$resource) { - return false; + return; } $data = $this->fractal->createData($resource)->toArray(); @@ -124,32 +108,11 @@ class ImportService $invoice = Invoice::createNew(); $data['invoice_number'] = $account->getNextInvoiceNumber($invoice); } - + if ($this->validate($source, $data, $entityType) !== true) { - return false; + return; } - - if($entityType == ENTITY_INVOICE){ - if(empty($this->processedRows[$data['invoice_number']])){ - $this->processedRows[$data['invoice_number']] = $data; - } - 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; - } - - end($this->processedRows); - return key($this->processedRows); - } - - private function saveData($source, $entityType, $row, $data_index, $maps) - { - $data = $this->processedRows[$data_index]; + $entity = $this->{"{$entityType}Repo"}->save($data); // if the invoice is paid we'll also create a payment record @@ -241,12 +204,10 @@ class ImportService } $invoiceMap = []; - $invoiceClientMap = []; $invoices = $this->invoiceRepo->all(); foreach ($invoices as $invoice) { if ($number = strtolower(trim($invoice->invoice_number))) { $invoiceMap[$number] = $invoice->id; - $invoiceClientMap[$number] = $invoice->client_id; } } @@ -267,7 +228,6 @@ class ImportService return [ ENTITY_CLIENT => $clientMap, ENTITY_INVOICE => $invoiceMap, - ENTITY_INVOICE.'_'.ENTITY_CLIENT => $invoiceClientMap, 'countries' => $countryMap, 'countries2' => $countryMap2, 'currencies' => $currencyMap, @@ -411,8 +371,6 @@ class ImportService $this->checkData($entityType, count($data)); $maps = $this->createMaps(); - // Convert the data - $row_list = array(); foreach ($data as $row) { if ($hasHeaders) { $hasHeaders = false; @@ -420,21 +378,8 @@ 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); - } - } else { - $results[RESULT_FAILURE][] = $row; - } - } - - // Save the data - foreach($row_list as $row_data){ - $result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index'], $maps); - + $result = $this->saveData($source, $entityType, $row, $maps); + if ($result) { $results[RESULT_SUCCESS][] = $result; } else { diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 6820588eb4..feebbe03c3 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -240,7 +240,6 @@ return array( 'deleted_invoices' => 'Successfully deleted :count invoices', 'created_payment' => 'Successfully created payment', - 'created_payments' => 'Successfully created :count payment(s)', 'archived_payment' => 'Successfully archived payment', 'archived_payments' => 'Successfully archived :count payments', 'deleted_payment' => 'Successfully deleted payment',