From c5c5ff3458fed9919d040115c140bdbc1e4dfcda Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 14 Dec 2015 22:07:48 +0200 Subject: [PATCH] Working on data import --- app/Ninja/Import/BaseTransformer.php | 5 ++- .../Import/Nutcache/ClientTransformer.php | 35 +++++++++++++++++ .../Import/Nutcache/InvoiceTransformer.php | 38 +++++++++++++++++++ .../Import/Nutcache/PaymentTransformer.php | 19 ++++++++++ app/Ninja/Import/Nutcache/TaskTransformer.php | 29 ++++++++++++++ app/Ninja/Serializers/ArraySerializer.php | 6 ++- app/Services/ImportService.php | 2 +- database/seeds/PaymentLibrariesSeeder.php | 2 +- 8 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 app/Ninja/Import/Nutcache/ClientTransformer.php create mode 100644 app/Ninja/Import/Nutcache/InvoiceTransformer.php create mode 100644 app/Ninja/Import/Nutcache/PaymentTransformer.php create mode 100644 app/Ninja/Import/Nutcache/TaskTransformer.php diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index 375f489e8f..c816993d68 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -45,7 +45,10 @@ class BaseTransformer extends TransformerAbstract protected function getDate($date, $format = 'Y-m-d') { - $date = DateTime::createFromFormat($format, $date); + if ( ! $date instanceof DateTime) { + $date = DateTime::createFromFormat($format, $date); + } + return $date ? $date->format('Y-m-d') : null; } diff --git a/app/Ninja/Import/Nutcache/ClientTransformer.php b/app/Ninja/Import/Nutcache/ClientTransformer.php new file mode 100644 index 0000000000..b123ec3129 --- /dev/null +++ b/app/Ninja/Import/Nutcache/ClientTransformer.php @@ -0,0 +1,35 @@ +hasClient($data->name)) { + return false; + } + + return new Item($data, function ($data) { + return [ + 'name' => $data->name, + 'city' => isset($data->city) ? $data->city : '', + 'state' => isset($data->city) ? $data->stateprovince : '', + 'id_number' => isset($data->registration_number) ? $data->registration_number : '', + 'postal_code' => isset($data->postalzip_code) ? $data->postalzip_code : '', + 'private_notes' => isset($data->notes) ? $data->notes : '', + 'work_phone' => isset($data->phone) ? $data->phone : '', + 'contacts' => [ + [ + 'first_name' => isset($data->contact_name) ? $this->getFirstName($data->contact_name) : '', + 'last_name' => isset($data->contact_name) ? $this->getLastName($data->contact_name) : '', + 'email' => $data->email, + 'phone' => isset($data->mobile) ? $data->mobile : '', + ], + ], + 'country_id' => isset($data->country) ? $this->getCountryId($data->country) : null, + ]; + }); + } +} diff --git a/app/Ninja/Import/Nutcache/InvoiceTransformer.php b/app/Ninja/Import/Nutcache/InvoiceTransformer.php new file mode 100644 index 0000000000..dfe4dc21f0 --- /dev/null +++ b/app/Ninja/Import/Nutcache/InvoiceTransformer.php @@ -0,0 +1,38 @@ +getClientId($data->client)) { + return false; + } + + if ($this->hasInvoice($data->document_no)) { + return false; + } + + return new Item($data, function ($data) { + return [ + 'client_id' => $this->getClientId($data->client), + 'invoice_number' => $this->getInvoiceNumber($data->document_no), + 'paid' => (float) $data->paid_to_date, + 'po_number' => $data->purchase_order, + 'terms' => $data->terms, + 'public_notes' => $data->notes, + 'invoice_date_sql' => $this->getDate($data->date), + 'due_date_sql' => $this->getDate($data->due_date), + 'invoice_items' => [ + [ + 'notes' => $data->description, + 'cost' => (float) $data->total, + 'qty' => 1, + ] + ], + ]; + }); + } +} \ No newline at end of file diff --git a/app/Ninja/Import/Nutcache/PaymentTransformer.php b/app/Ninja/Import/Nutcache/PaymentTransformer.php new file mode 100644 index 0000000000..04e783361f --- /dev/null +++ b/app/Ninja/Import/Nutcache/PaymentTransformer.php @@ -0,0 +1,19 @@ + (float) $data->paid_to_date, + 'payment_date_sql' => $this->getDate($data->date), + 'client_id' => $data->client_id, + 'invoice_id' => $data->invoice_id, + ]; + }); + } +} \ No newline at end of file diff --git a/app/Ninja/Import/Nutcache/TaskTransformer.php b/app/Ninja/Import/Nutcache/TaskTransformer.php new file mode 100644 index 0000000000..8c1363edcf --- /dev/null +++ b/app/Ninja/Import/Nutcache/TaskTransformer.php @@ -0,0 +1,29 @@ +hours * 3600); + $timeLogFinish = strtotime($data->date); + $timeLogStart = intval($timeLogFinish - $seconds); + $timeLog[] = []; + $timelog[] = $timeLogStart; + $timelog[] = $timeLogFinish; + $timeLog = json_encode(array($timelog)); + + return [ + 'action' => 'stop', + 'time_log' => $timeLog, + 'description' => $data->task, + ]; + } + +} +*/ \ No newline at end of file diff --git a/app/Ninja/Serializers/ArraySerializer.php b/app/Ninja/Serializers/ArraySerializer.php index 021ab81281..bdbbbfe999 100644 --- a/app/Ninja/Serializers/ArraySerializer.php +++ b/app/Ninja/Serializers/ArraySerializer.php @@ -6,11 +6,13 @@ class ArraySerializer extends FractalArraySerializer { public function collection($resourceKey, array $data) { - return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data; + return $data; + //return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data; } public function item($resourceKey, array $data) { - return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data; + return $data; + //return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data; } } diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index b1dbd6bbbe..1bf19bbfdb 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -37,7 +37,7 @@ class ImportService IMPORT_HARVEST, IMPORT_HIVEAGE, IMPORT_INVOICEABLE, - //IMPORT_NUTCACHE, + IMPORT_NUTCACHE, IMPORT_RONIN, IMPORT_WAVE, IMPORT_ZOHO, diff --git a/database/seeds/PaymentLibrariesSeeder.php b/database/seeds/PaymentLibrariesSeeder.php index 3fef8c8e73..ff16708fa1 100644 --- a/database/seeds/PaymentLibrariesSeeder.php +++ b/database/seeds/PaymentLibrariesSeeder.php @@ -113,7 +113,7 @@ class PaymentLibrariesSeeder extends Seeder ['name' => 'Hong Kong Dollar', 'code' => 'HKD', 'symbol' => '$', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Indonesian Rupiah', 'code' => 'IDR', 'symbol' => 'Rp', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Mexican Peso', 'code' => 'MXN', 'symbol' => '$', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], - ['name' => 'Egyptian Pound', 'code' => 'EGP', 'symbol' => '£', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], + ['name' => 'Egyptian Pound', 'code' => 'EGP', 'symbol' => 'E£', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Colombian Peso', 'code' => 'COP', 'symbol' => '$', 'precision' => '2', 'thousand_separator' => '.', 'decimal_separator' => ','], ['name' => 'West African Franc', 'code' => 'XOF', 'symbol' => 'CFA ', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Chinese Renminbi', 'code' => 'CNY', 'symbol' => 'RMB ', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'],