From 6fc8f76ea8a2e2475f565858d40a2b77a7d50a8a Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 8 Dec 2015 12:10:20 +0200 Subject: [PATCH] Working on data import --- app/Http/Controllers/ImportController.php | 2 + app/Libraries/Utils.php | 7 ++- app/Ninja/Import/BaseTransformer.php | 58 +++++++++++++++++++ app/Ninja/Import/CSV/ClientTransformer.php | 17 ++---- app/Ninja/Import/CSV/InvoiceTransformer.php | 19 +++--- app/Ninja/Import/CSV/PaymentTransformer.php | 8 +-- .../Import/FreshBooks/ClientTransformer.php | 17 ++---- .../Import/FreshBooks/InvoiceTransformer.php | 17 +++--- .../Import/FreshBooks/PaymentTransformer.php | 4 +- .../Import/FreshBooks/TaskTransformer.php | 6 +- .../Import/Harvest/ClientTransformer.php | 11 ++-- .../Import/Harvest/ContactTransformer.php | 15 ++--- .../Import/Harvest/InvoiceTransformer.php | 25 +++----- .../Import/Harvest/PaymentTransformer.php | 14 ++--- .../Import/Hiveage/ClientTransformer.php | 35 +++++++++++ .../Import/Hiveage/InvoiceTransformer.php | 35 +++++++++++ .../Import/Hiveage/PaymentTransformer.php | 19 ++++++ app/Ninja/Repositories/InvoiceRepository.php | 5 +- app/Services/ImportService.php | 23 +++++--- readme.md | 2 +- 20 files changed, 235 insertions(+), 104 deletions(-) create mode 100644 app/Ninja/Import/BaseTransformer.php create mode 100644 app/Ninja/Import/Hiveage/ClientTransformer.php create mode 100644 app/Ninja/Import/Hiveage/InvoiceTransformer.php create mode 100644 app/Ninja/Import/Hiveage/PaymentTransformer.php diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 53c25ed5d78a..9f28cfad6181 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -47,6 +47,7 @@ class ImportController extends BaseController } } } catch (Exception $exception) { + Utils::logError($exception); Session::flash('error', $exception->getMessage()); } @@ -72,6 +73,7 @@ class ImportController extends BaseController Session::flash('message', trans('texts.imported_file')); } } catch (Exception $exception) { + Utils::logError($exception); Session::flash('error', $exception->getMessage()); } diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index 4d19f958f7d8..3051d5a0fcd5 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -7,6 +7,7 @@ use App; use Schema; use Session; use Request; +use Exception; use View; use DateTimeZone; use Input; @@ -35,7 +36,7 @@ class Utils if (Schema::hasTable('accounts')) { return true; } - } catch (\Exception $e) { + } catch (Exception $e) { return false; } } @@ -198,6 +199,10 @@ class Utils public static function logError($error, $context = 'PHP') { + if ($error instanceof Exception) { + $error = self::getErrorString($error); + } + $count = Session::get('error_count', 0); Session::put('error_count', ++$count); if ($count > 100) { diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php new file mode 100644 index 000000000000..f39d81587bbb --- /dev/null +++ b/app/Ninja/Import/BaseTransformer.php @@ -0,0 +1,58 @@ +maps = $maps; + } + + protected function hasClient($name) + { + $name = strtolower($name); + return isset($this->maps[ENTITY_CLIENT][$name]); + } + + protected function getClientId($name) + { + $name = strtolower($name); + return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null; + } + + protected function getCountryId($name) + { + $name = strtolower($name); + return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null; + } + + protected function getFirstName($name) + { + $name = Utils::splitName($name); + return $name[0]; + } + + protected function getDate($date, $format = 'Y-m-d') + { + $date = DateTime::createFromFormat($format, $date); + return $date ? $date->format('Y-m-d') : null; + } + + protected function getLastName($name) + { + $name = Utils::splitName($name); + return $name[1]; + } + + protected function hasInvoice($invoiceNumber) + { + $invoiceNumber = strtolower($invoiceNumber); + return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]); + } + +} \ No newline at end of file diff --git a/app/Ninja/Import/CSV/ClientTransformer.php b/app/Ninja/Import/CSV/ClientTransformer.php index 8a0d4ab0130b..11cac64c6093 100644 --- a/app/Ninja/Import/CSV/ClientTransformer.php +++ b/app/Ninja/Import/CSV/ClientTransformer.php @@ -1,22 +1,17 @@ name])) { + if (isset($data->name) && $this->hasClient($data->name)) { return false; } - if (isset($maps['countries'][$data->country])) { - $data->country_id = $maps['countries'][$data->country]; - } - - return new Item($data, function ($data) use ($maps) { + return new Item($data, function ($data) { return [ 'name' => isset($data->name) ? $data->name : null, 'work_phone' => isset($data->work_phone) ? $data->work_phone : null, @@ -33,7 +28,7 @@ class ClientTransformer extends TransformerAbstract 'phone' => isset($data->phone) ? $data->phone : null, ], ], - 'country_id' => isset($data->country_id) ? $data->country_id : null, + '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 d18d4d7b1559..e85adf90493f 100644 --- a/app/Ninja/Import/CSV/InvoiceTransformer.php +++ b/app/Ninja/Import/CSV/InvoiceTransformer.php @@ -1,28 +1,25 @@ invoice_number])) { + if ( ! $this->getClientId($data->name)) { return false; } - if (isset($maps[ENTITY_CLIENT][$data->name])) { - $data->client_id = $maps[ENTITY_CLIENT][$data->name]; - } else { + if (isset($data->invoice_number) && $this->hasInvoice($data->invoice_number)) { return false; } - - return new Item($data, function ($data) use ($maps) { + + return new Item($data, function ($data) { return [ 'invoice_number' => isset($data->invoice_number) ? $data->invoice_number : null, 'paid' => isset($data->paid) ? (float) $data->paid : null, - 'client_id' => (int) $data->client_id, + 'client_id' => $this->getClientId($data->name), '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, diff --git a/app/Ninja/Import/CSV/PaymentTransformer.php b/app/Ninja/Import/CSV/PaymentTransformer.php index 87936ccbc65c..7acd3d88f839 100644 --- a/app/Ninja/Import/CSV/PaymentTransformer.php +++ b/app/Ninja/Import/CSV/PaymentTransformer.php @@ -1,13 +1,13 @@ $data->paid, 'payment_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null, diff --git a/app/Ninja/Import/FreshBooks/ClientTransformer.php b/app/Ninja/Import/FreshBooks/ClientTransformer.php index ea82a1e4e27c..77200a2f364d 100644 --- a/app/Ninja/Import/FreshBooks/ClientTransformer.php +++ b/app/Ninja/Import/FreshBooks/ClientTransformer.php @@ -1,22 +1,17 @@ organization])) { + if ($this->hasClient($data->organization)) { return false; } - if (isset($maps['countries'][$data->country])) { - $data->country_id = $maps['countries'][$data->country]; - } - - return new Item($data, function ($data) use ($maps) { + return new Item($data, function ($data) { return [ 'name' => $data->organization, 'work_phone' => $data->busphone, @@ -34,7 +29,7 @@ class ClientTransformer extends TransformerAbstract 'phone' => $data->mobphone ?: $data->homephone, ], ], - 'country_id' => $data->country_id, + 'country_id' => $this->getCountryId($data->country), ]; }); } diff --git a/app/Ninja/Import/FreshBooks/InvoiceTransformer.php b/app/Ninja/Import/FreshBooks/InvoiceTransformer.php index 5793ae053477..de8c0a0520c5 100644 --- a/app/Ninja/Import/FreshBooks/InvoiceTransformer.php +++ b/app/Ninja/Import/FreshBooks/InvoiceTransformer.php @@ -1,28 +1,25 @@ invoice_number])) { + if ( ! $this->getClientId($data->organization)) { return false; } - if (isset($maps[ENTITY_CLIENT][$data->organization])) { - $data->client_id = $maps[ENTITY_CLIENT][$data->organization]; - } else { + if ($this->hasInvoice($data->invoice_number)) { return false; } - return new Item($data, function ($data) use ($maps) { + return new Item($data, function ($data) { return [ 'invoice_number' => $data->invoice_number, 'paid' => (float) $data->paid, - 'client_id' => (int) $data->client_id, + 'client_id' => $this->getClientId($data->organization), 'po_number' => $data->po_number, 'terms' => $data->terms, 'public_notes' => $data->notes, diff --git a/app/Ninja/Import/FreshBooks/PaymentTransformer.php b/app/Ninja/Import/FreshBooks/PaymentTransformer.php index 1adb09df0634..1f69fdbacf41 100644 --- a/app/Ninja/Import/FreshBooks/PaymentTransformer.php +++ b/app/Ninja/Import/FreshBooks/PaymentTransformer.php @@ -1,9 +1,9 @@ client_name])) { + if ($this->hasClient($data->client_name)) { return false; } - return new Item($data, function ($data) use ($maps) { + return new Item($data, function ($data) { return [ 'name' => $data->client_name, ]; diff --git a/app/Ninja/Import/Harvest/ContactTransformer.php b/app/Ninja/Import/Harvest/ContactTransformer.php index 5e5dd68909d7..b7804cd0abbe 100644 --- a/app/Ninja/Import/Harvest/ContactTransformer.php +++ b/app/Ninja/Import/Harvest/ContactTransformer.php @@ -1,22 +1,19 @@ client])) { - $data->client_id = $maps[ENTITY_CLIENT][$data->client]; - } else { + if ( ! $this->hasClient($data->client)) { return false; } - return new Item($data, function ($data) use ($maps) { + return new Item($data, function ($data) { return [ - 'client_id' => $data->client_id, + 'client_id' => $this->getClientId($data->client), 'first_name' => $data->first_name, 'last_name' => $data->last_name, 'email' => $data->email, diff --git a/app/Ninja/Import/Harvest/InvoiceTransformer.php b/app/Ninja/Import/Harvest/InvoiceTransformer.php index cc9ece282f1b..216d074695f5 100644 --- a/app/Ninja/Import/Harvest/InvoiceTransformer.php +++ b/app/Ninja/Import/Harvest/InvoiceTransformer.php @@ -1,36 +1,27 @@ id])) { + if ( ! $this->getClientId($data->client)) { return false; } - if (isset($maps[ENTITY_CLIENT][$data->client])) { - $data->client_id = $maps[ENTITY_CLIENT][$data->client]; - } else { + if ($this->hasInvoice($data->id)) { return false; } - return new Item($data, function ($data) use ($maps) { - - $invoiceDate = DateTime::createFromFormat('m/d/Y', $data->issue_date); - + return new Item($data, function ($data) { return [ 'invoice_number' => $data->id, 'paid' => (float) $data->paid_amount, - 'client_id' => (int) $data->client_id, + 'client_id' => $this->getClientId($data->client), 'po_number' => $data->po_number, - 'invoice_date_sql' => $invoiceDate->format('Y-m-d'), - 'tax_rate' => $data->tax ?: null, - 'tax_name' => $data->tax ? trans('texts.tax') : null, + 'invoice_date_sql' => $this->getDate($data->issue_date, 'm/d/Y'), 'invoice_items' => [ [ 'notes' => $data->subject, diff --git a/app/Ninja/Import/Harvest/PaymentTransformer.php b/app/Ninja/Import/Harvest/PaymentTransformer.php index 78cca42a54a1..0efd442886cc 100644 --- a/app/Ninja/Import/Harvest/PaymentTransformer.php +++ b/app/Ninja/Import/Harvest/PaymentTransformer.php @@ -1,20 +1,16 @@ last_payment_date); - + return new Item($data, function ($data) { return [ 'amount' => $data->paid_amount, - 'payment_date_sql' => $paymentDate ? $paymentDate->format('Y-m-d') : null, + 'payment_date_sql' => $this->getDate($data->last_payment_date, 'm/d/Y'), 'client_id' => $data->client_id, 'invoice_id' => $data->invoice_id, ]; diff --git a/app/Ninja/Import/Hiveage/ClientTransformer.php b/app/Ninja/Import/Hiveage/ClientTransformer.php new file mode 100644 index 000000000000..7b4fcdc7bf79 --- /dev/null +++ b/app/Ninja/Import/Hiveage/ClientTransformer.php @@ -0,0 +1,35 @@ +hasClient($data->name)) { + return false; + } + + return new Item($data, function ($data) { + return [ + 'name' => $data->name, + 'contacts' => [ + [ + 'first_name' => $this->getFirstName($data->primary_contact), + 'last_name' => $this->getLastName($data->primary_contactk), + 'email' => $data->business_email, + ], + ], + 'address1' => $data->address_1, + 'address2' => $data->address_2, + 'city' => $data->city, + 'state' => $data->state_name, + 'postal_code' => $data->zip_code, + 'work_phone' => $data->phone, + 'website' => $data->website, + 'country_id' => $this->getCountryId($data->country), + ]; + }); + } +} diff --git a/app/Ninja/Import/Hiveage/InvoiceTransformer.php b/app/Ninja/Import/Hiveage/InvoiceTransformer.php new file mode 100644 index 000000000000..e1a110698f6b --- /dev/null +++ b/app/Ninja/Import/Hiveage/InvoiceTransformer.php @@ -0,0 +1,35 @@ +getClientId($data->client)) { + return false; + } + + if ($this->hasInvoice($data->statement_no)) { + return false; + } + + return new Item($data, function ($data) { + return [ + 'invoice_number' => $data->statement_no, + 'paid' => (float) $data->paid_total, + 'client_id' => $this->getClientId($data->client), + 'invoice_date_sql' => $this->getDate($data->date), + 'due_date_sql' => $this->getDate($data->due_date), + 'invoice_items' => [ + [ + 'notes' => $data->summary, + 'cost' => (float) $data->billed_total, + 'qty' => 1, + ] + ], + ]; + }); + } +} \ No newline at end of file diff --git a/app/Ninja/Import/Hiveage/PaymentTransformer.php b/app/Ninja/Import/Hiveage/PaymentTransformer.php new file mode 100644 index 000000000000..d6232d05bcc9 --- /dev/null +++ b/app/Ninja/Import/Hiveage/PaymentTransformer.php @@ -0,0 +1,19 @@ + $data->paid_total, + 'payment_date_sql' => $this->getDate($data->last_paid_on), + 'client_id' => $data->client_id, + 'invoice_id' => $data->invoice_id, + ]; + }); + } +} \ No newline at end of file diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 99597dd24634..74e01cc721da 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -262,7 +262,10 @@ class InvoiceRepository extends BaseRepository $invoice->invoice_footer = Utils::processVariables($invoice->invoice_footer); $invoice->public_notes = Utils::processVariables($invoice->public_notes); - $invoice->po_number = trim($data['po_number']); + if (isset($data['po_number'])) { + $invoice->po_number = trim($data['po_number']); + } + $invoice->invoice_design_id = isset($data['invoice_design_id']) ? $data['invoice_design_id'] : $account->invoice_design_id; if (isset($data['tax_name']) && isset($data['tax_rate']) && $data['tax_name']) { diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 49b80ad32e16..2b8e273b722b 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -35,7 +35,7 @@ class ImportService IMPORT_CSV, IMPORT_FRESHBOOKS, IMPORT_HARVEST, - //IMPORT_HIVEAGE, + IMPORT_HIVEAGE, //IMPORT_INVOICEABLE, //IMPORT_NUTCACHE, //IMPORT_RONIN, @@ -85,7 +85,7 @@ class ImportService private function saveData($source, $entityType, $row, $maps) { - $transformer = $this->getTransformer($source, $entityType); + $transformer = $this->getTransformer($source, $entityType, $maps); $resource = $transformer->transform($row, $maps); if (!$resource) { @@ -135,16 +135,16 @@ class ImportService return 'App\\Ninja\\Import\\'.$source.'\\'.ucwords($entityType).'Transformer'; } - public static function getTransformer($source, $entityType) + public static function getTransformer($source, $entityType, $maps) { $className = self::getTransformerClassName($source, $entityType); - return new $className(); + return new $className($maps); } private function createPayment($source, $data, $maps, $clientId, $invoiceId) { - $paymentTransformer = $this->getTransformer($source, ENTITY_PAYMENT); + $paymentTransformer = $this->getTransformer($source, ENTITY_PAYMENT, $maps); $data->client_id = $clientId; $data->invoice_id = $invoiceId; @@ -190,25 +190,32 @@ class ImportService $clientMap = []; $clients = $this->clientRepo->all(); foreach ($clients as $client) { - $clientMap[$client->name] = $client->id; + $clientMap[strtolower($client->name)] = $client->id; } $invoiceMap = []; $invoices = $this->invoiceRepo->all(); foreach ($invoices as $invoice) { - $invoiceMap[$invoice->invoice_number] = $invoice->id; + $invoiceMap[strtolower($invoice->invoice_number)] = $invoice->id; } $countryMap = []; $countries = Cache::get('countries'); foreach ($countries as $country) { - $countryMap[$country->name] = $country->id; + $countryMap[strtolower($country->name)] = $country->id; + } + + $currencyMap = []; + $currencies = Cache::get('currencies'); + foreach ($currencies as $currency) { + $currencyMap[strtolower($currency->code)] = $currency->id; } return [ ENTITY_CLIENT => $clientMap, ENTITY_INVOICE => $invoiceMap, 'countries' => $countryMap, + 'currencies' => $currencyMap, ]; } diff --git a/readme.md b/readme.md index 2289653e6811..dea2f2c518ce 100644 --- a/readme.md +++ b/readme.md @@ -23,7 +23,7 @@ There are two options: ### Requirements * PHP >= 5.4.0 -* MCrypt Extension +* MCrypt PHP Extension * MySQL ### Features