Working on data import

This commit is contained in:
Hillel Coren 2015-12-08 12:10:20 +02:00
parent e46d804cd7
commit 6fc8f76ea8
20 changed files with 235 additions and 104 deletions

View File

@ -47,6 +47,7 @@ class ImportController extends BaseController
} }
} }
} catch (Exception $exception) { } catch (Exception $exception) {
Utils::logError($exception);
Session::flash('error', $exception->getMessage()); Session::flash('error', $exception->getMessage());
} }
@ -72,6 +73,7 @@ class ImportController extends BaseController
Session::flash('message', trans('texts.imported_file')); Session::flash('message', trans('texts.imported_file'));
} }
} catch (Exception $exception) { } catch (Exception $exception) {
Utils::logError($exception);
Session::flash('error', $exception->getMessage()); Session::flash('error', $exception->getMessage());
} }

View File

@ -7,6 +7,7 @@ use App;
use Schema; use Schema;
use Session; use Session;
use Request; use Request;
use Exception;
use View; use View;
use DateTimeZone; use DateTimeZone;
use Input; use Input;
@ -35,7 +36,7 @@ class Utils
if (Schema::hasTable('accounts')) { if (Schema::hasTable('accounts')) {
return true; return true;
} }
} catch (\Exception $e) { } catch (Exception $e) {
return false; return false;
} }
} }
@ -198,6 +199,10 @@ class Utils
public static function logError($error, $context = 'PHP') public static function logError($error, $context = 'PHP')
{ {
if ($error instanceof Exception) {
$error = self::getErrorString($error);
}
$count = Session::get('error_count', 0); $count = Session::get('error_count', 0);
Session::put('error_count', ++$count); Session::put('error_count', ++$count);
if ($count > 100) { if ($count > 100) {

View File

@ -0,0 +1,58 @@
<?php namespace App\Ninja\Import;
use Utils;
use DateTime;
use League\Fractal\TransformerAbstract;
class BaseTransformer extends TransformerAbstract
{
protected $maps;
public function __construct($maps)
{
$this->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]);
}
}

View File

@ -1,22 +1,17 @@
<?php namespace App\Ninja\Import\CSV; <?php namespace App\Ninja\Import\CSV;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use App\Models\Country;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class ClientTransformer extends TransformerAbstract class ClientTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_CLIENT][$data->name])) { if (isset($data->name) && $this->hasClient($data->name)) {
return false; return false;
} }
if (isset($maps['countries'][$data->country])) { return new Item($data, function ($data) {
$data->country_id = $maps['countries'][$data->country];
}
return new Item($data, function ($data) use ($maps) {
return [ return [
'name' => isset($data->name) ? $data->name : null, 'name' => isset($data->name) ? $data->name : null,
'work_phone' => isset($data->work_phone) ? $data->work_phone : 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, '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,
]; ];
}); });
} }

View File

@ -1,28 +1,25 @@
<?php namespace App\Ninja\Import\CSV; <?php namespace App\Ninja\Import\CSV;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use App\Models\Client;
class InvoiceTransformer extends TransformerAbstract class InvoiceTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_INVOICE][$data->invoice_number])) { if ( ! $this->getClientId($data->name)) {
return false; return false;
} }
if (isset($maps[ENTITY_CLIENT][$data->name])) { if (isset($data->invoice_number) && $this->hasInvoice($data->invoice_number)) {
$data->client_id = $maps[ENTITY_CLIENT][$data->name];
} else {
return false; return false;
} }
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
return [ return [
'invoice_number' => isset($data->invoice_number) ? $data->invoice_number : null, 'invoice_number' => isset($data->invoice_number) ? $data->invoice_number : null,
'paid' => isset($data->paid) ? (float) $data->paid : 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, 'po_number' => isset($data->po_number) ? $data->po_number : null,
'terms' => isset($data->terms) ? $data->terms : null, 'terms' => isset($data->terms) ? $data->terms : null,
'public_notes' => isset($data->notes) ? $data->notes : null, 'public_notes' => isset($data->notes) ? $data->notes : null,

View File

@ -1,13 +1,13 @@
<?php namespace App\Ninja\Import\CSV; <?php namespace App\Ninja\Import\CSV;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class PaymentTransformer extends TransformerAbstract class PaymentTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
return [ return [
'amount' => $data->paid, 'amount' => $data->paid,
'payment_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null, 'payment_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null,

View File

@ -1,22 +1,17 @@
<?php namespace App\Ninja\Import\FreshBooks; <?php namespace App\Ninja\Import\FreshBooks;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use App\Models\Country;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class ClientTransformer extends TransformerAbstract class ClientTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_CLIENT][$data->organization])) { if ($this->hasClient($data->organization)) {
return false; return false;
} }
if (isset($maps['countries'][$data->country])) { return new Item($data, function ($data) {
$data->country_id = $maps['countries'][$data->country];
}
return new Item($data, function ($data) use ($maps) {
return [ return [
'name' => $data->organization, 'name' => $data->organization,
'work_phone' => $data->busphone, 'work_phone' => $data->busphone,
@ -34,7 +29,7 @@ class ClientTransformer extends TransformerAbstract
'phone' => $data->mobphone ?: $data->homephone, 'phone' => $data->mobphone ?: $data->homephone,
], ],
], ],
'country_id' => $data->country_id, 'country_id' => $this->getCountryId($data->country),
]; ];
}); });
} }

View File

@ -1,28 +1,25 @@
<?php namespace App\Ninja\Import\FreshBooks; <?php namespace App\Ninja\Import\FreshBooks;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use App\Models\Client;
class InvoiceTransformer extends TransformerAbstract class InvoiceTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_INVOICE][$data->invoice_number])) { if ( ! $this->getClientId($data->organization)) {
return false; return false;
} }
if (isset($maps[ENTITY_CLIENT][$data->organization])) { if ($this->hasInvoice($data->invoice_number)) {
$data->client_id = $maps[ENTITY_CLIENT][$data->organization];
} else {
return false; return false;
} }
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
return [ return [
'invoice_number' => $data->invoice_number, 'invoice_number' => $data->invoice_number,
'paid' => (float) $data->paid, 'paid' => (float) $data->paid,
'client_id' => (int) $data->client_id, 'client_id' => $this->getClientId($data->organization),
'po_number' => $data->po_number, 'po_number' => $data->po_number,
'terms' => $data->terms, 'terms' => $data->terms,
'public_notes' => $data->notes, 'public_notes' => $data->notes,

View File

@ -1,9 +1,9 @@
<?php namespace App\Ninja\Import\FreshBooks; <?php namespace App\Ninja\Import\FreshBooks;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class PaymentTransformer extends TransformerAbstract class PaymentTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data, $maps)
{ {

View File

@ -1,10 +1,10 @@
<?php namespace App\Ninja\Import\FreshBooks; <?php namespace App\Ninja\Import\FreshBooks;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use Illuminate\Support\Facades\Auth; use League\Fractal\Resource\Item;
/* /*
class TaskTransformer extends TransformerAbstract class TaskTransformer extends BaseTransformer
{ {
public function transform($data) public function transform($data)

View File

@ -1,18 +1,17 @@
<?php namespace App\Ninja\Import\Harvest; <?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use App\Models\Country;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class ClientTransformer extends TransformerAbstract class ClientTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_CLIENT][$data->client_name])) { if ($this->hasClient($data->client_name)) {
return false; return false;
} }
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
return [ return [
'name' => $data->client_name, 'name' => $data->client_name,
]; ];

View File

@ -1,22 +1,19 @@
<?php namespace App\Ninja\Import\Harvest; <?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use App\Models\Country;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class ContactTransformer extends TransformerAbstract class ContactTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_CLIENT][$data->client])) { if ( ! $this->hasClient($data->client)) {
$data->client_id = $maps[ENTITY_CLIENT][$data->client];
} else {
return false; return false;
} }
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
return [ return [
'client_id' => $data->client_id, 'client_id' => $this->getClientId($data->client),
'first_name' => $data->first_name, 'first_name' => $data->first_name,
'last_name' => $data->last_name, 'last_name' => $data->last_name,
'email' => $data->email, 'email' => $data->email,

View File

@ -1,36 +1,27 @@
<?php namespace App\Ninja\Import\Harvest; <?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use App\Models\Client;
use DateTime;
class InvoiceTransformer extends TransformerAbstract class InvoiceTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
if (isset($maps[ENTITY_INVOICE][$data->id])) { if ( ! $this->getClientId($data->client)) {
return false; return false;
} }
if (isset($maps[ENTITY_CLIENT][$data->client])) { if ($this->hasInvoice($data->id)) {
$data->client_id = $maps[ENTITY_CLIENT][$data->client];
} else {
return false; return false;
} }
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
$invoiceDate = DateTime::createFromFormat('m/d/Y', $data->issue_date);
return [ return [
'invoice_number' => $data->id, 'invoice_number' => $data->id,
'paid' => (float) $data->paid_amount, 'paid' => (float) $data->paid_amount,
'client_id' => (int) $data->client_id, 'client_id' => $this->getClientId($data->client),
'po_number' => $data->po_number, 'po_number' => $data->po_number,
'invoice_date_sql' => $invoiceDate->format('Y-m-d'), 'invoice_date_sql' => $this->getDate($data->issue_date, 'm/d/Y'),
'tax_rate' => $data->tax ?: null,
'tax_name' => $data->tax ? trans('texts.tax') : null,
'invoice_items' => [ 'invoice_items' => [
[ [
'notes' => $data->subject, 'notes' => $data->subject,

View File

@ -1,20 +1,16 @@
<?php namespace App\Ninja\Import\Harvest; <?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract; use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use DateTime;
class PaymentTransformer extends TransformerAbstract class PaymentTransformer extends BaseTransformer
{ {
public function transform($data, $maps) public function transform($data)
{ {
return new Item($data, function ($data) use ($maps) { return new Item($data, function ($data) {
$paymentDate = DateTime::createFromFormat('m/d/Y', $data->last_payment_date);
return [ return [
'amount' => $data->paid_amount, '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, 'client_id' => $data->client_id,
'invoice_id' => $data->invoice_id, 'invoice_id' => $data->invoice_id,
]; ];

View File

@ -0,0 +1,35 @@
<?php namespace App\Ninja\Import\Hiveage;
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
class ClientTransformer extends BaseTransformer
{
public function transform($data)
{
if ($this->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),
];
});
}
}

View File

@ -0,0 +1,35 @@
<?php namespace App\Ninja\Import\Hiveage;
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
class InvoiceTransformer extends BaseTransformer
{
public function transform($data)
{
if ( ! $this->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,
]
],
];
});
}
}

View File

@ -0,0 +1,19 @@
<?php namespace App\Ninja\Import\Hiveage;
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
class PaymentTransformer extends BaseTransformer
{
public function transform($data, $maps)
{
return new Item($data, function ($data) use ($maps) {
return [
'amount' => $data->paid_total,
'payment_date_sql' => $this->getDate($data->last_paid_on),
'client_id' => $data->client_id,
'invoice_id' => $data->invoice_id,
];
});
}
}

View File

@ -262,7 +262,10 @@ class InvoiceRepository extends BaseRepository
$invoice->invoice_footer = Utils::processVariables($invoice->invoice_footer); $invoice->invoice_footer = Utils::processVariables($invoice->invoice_footer);
$invoice->public_notes = Utils::processVariables($invoice->public_notes); $invoice->public_notes = Utils::processVariables($invoice->public_notes);
if (isset($data['po_number'])) {
$invoice->po_number = trim($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; $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']) { if (isset($data['tax_name']) && isset($data['tax_rate']) && $data['tax_name']) {

View File

@ -35,7 +35,7 @@ class ImportService
IMPORT_CSV, IMPORT_CSV,
IMPORT_FRESHBOOKS, IMPORT_FRESHBOOKS,
IMPORT_HARVEST, IMPORT_HARVEST,
//IMPORT_HIVEAGE, IMPORT_HIVEAGE,
//IMPORT_INVOICEABLE, //IMPORT_INVOICEABLE,
//IMPORT_NUTCACHE, //IMPORT_NUTCACHE,
//IMPORT_RONIN, //IMPORT_RONIN,
@ -85,7 +85,7 @@ class ImportService
private function saveData($source, $entityType, $row, $maps) private function saveData($source, $entityType, $row, $maps)
{ {
$transformer = $this->getTransformer($source, $entityType); $transformer = $this->getTransformer($source, $entityType, $maps);
$resource = $transformer->transform($row, $maps); $resource = $transformer->transform($row, $maps);
if (!$resource) { if (!$resource) {
@ -135,16 +135,16 @@ class ImportService
return 'App\\Ninja\\Import\\'.$source.'\\'.ucwords($entityType).'Transformer'; 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); $className = self::getTransformerClassName($source, $entityType);
return new $className(); return new $className($maps);
} }
private function createPayment($source, $data, $maps, $clientId, $invoiceId) 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->client_id = $clientId;
$data->invoice_id = $invoiceId; $data->invoice_id = $invoiceId;
@ -190,25 +190,32 @@ class ImportService
$clientMap = []; $clientMap = [];
$clients = $this->clientRepo->all(); $clients = $this->clientRepo->all();
foreach ($clients as $client) { foreach ($clients as $client) {
$clientMap[$client->name] = $client->id; $clientMap[strtolower($client->name)] = $client->id;
} }
$invoiceMap = []; $invoiceMap = [];
$invoices = $this->invoiceRepo->all(); $invoices = $this->invoiceRepo->all();
foreach ($invoices as $invoice) { foreach ($invoices as $invoice) {
$invoiceMap[$invoice->invoice_number] = $invoice->id; $invoiceMap[strtolower($invoice->invoice_number)] = $invoice->id;
} }
$countryMap = []; $countryMap = [];
$countries = Cache::get('countries'); $countries = Cache::get('countries');
foreach ($countries as $country) { 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 [ return [
ENTITY_CLIENT => $clientMap, ENTITY_CLIENT => $clientMap,
ENTITY_INVOICE => $invoiceMap, ENTITY_INVOICE => $invoiceMap,
'countries' => $countryMap, 'countries' => $countryMap,
'currencies' => $currencyMap,
]; ];
} }

View File

@ -23,7 +23,7 @@ There are two options:
### Requirements ### Requirements
* PHP >= 5.4.0 * PHP >= 5.4.0
* MCrypt Extension * MCrypt PHP Extension
* MySQL * MySQL
### Features ### Features