From 4cc56c399577b2da88b3bfe2401b9a1baa212574 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Feb 2022 10:47:17 +1100 Subject: [PATCH] Fresh Books Import Tests --- app/Import/Providers/BaseImport.php | 2 +- app/Import/Providers/Freshbooks.php | 99 +++++++++ .../Freshbooks/ClientTransformer.php | 2 +- .../Freshbooks/InvoiceTransformer.php | 3 +- .../Import/Freshbooks/FreshbooksTest.php | 207 ++++++++++++++++++ tests/Feature/Import/Wave/WaveTest.php | 2 +- tests/Feature/Import/Zoho/ZohoTest.php | 2 +- 7 files changed, 312 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/Import/Freshbooks/FreshbooksTest.php diff --git a/app/Import/Providers/BaseImport.php b/app/Import/Providers/BaseImport.php index de59c5698b35..fea2b00048f4 100644 --- a/app/Import/Providers/BaseImport.php +++ b/app/Import/Providers/BaseImport.php @@ -216,7 +216,7 @@ class BaseImport try { $invoice_data = $invoice_transformer->transform($raw_invoice); - nlog($invoice_data); + $invoice_data['line_items'] = $this->cleanItems( $invoice_data['line_items'] ?? [] ); diff --git a/app/Import/Providers/Freshbooks.php b/app/Import/Providers/Freshbooks.php index bb7759489141..881d91c26295 100644 --- a/app/Import/Providers/Freshbooks.php +++ b/app/Import/Providers/Freshbooks.php @@ -10,6 +10,105 @@ */ namespace App\Import\Providers; +use App\Factory\ClientFactory; +use App\Factory\InvoiceFactory; +use App\Http\Requests\Client\StoreClientRequest; +use App\Http\Requests\Invoice\StoreInvoiceRequest; +use App\Import\Transformer\Freshbooks\ClientTransformer; +use App\Import\Transformer\Freshbooks\InvoiceTransformer; +use App\Repositories\ClientRepository; +use App\Repositories\InvoiceRepository; + class Freshbooks extends BaseImport { + + public array $entity_count = []; + + public function import(string $entity) + { + if ( + in_array($entity, [ + 'client', + 'invoice', + // 'product', + // 'payment', + // 'vendor', + // 'expense', + ]) + ) { + $this->{$entity}(); + } + + //collate any errors + + $this->finalizeImport(); + } + + public function client() + { + $entity_type = 'client'; + + $data = $this->getCsvData($entity_type); + + $data = $this->preTransform($data, $entity_type); + + if (empty($data)) { + $this->entity_count['clients'] = 0; + return; + } + + $this->request_name = StoreClientRequest::class; + $this->repository_name = ClientRepository::class; + $this->factory_name = ClientFactory::class; + + $this->repository = app()->make($this->repository_name); + $this->repository->import_mode = true; + + $this->transformer = new ClientTransformer($this->company); + + $client_count = $this->ingest($data, $entity_type); + + $this->entity_count['clients'] = $client_count; + + } + + + public function invoice() { + + //make sure we update and create products with wave + $initial_update_products_value = $this->company->update_products; + $this->company->update_products = true; + + $this->company->save(); + + $entity_type = 'invoice'; + + $data = $this->getCsvData($entity_type); + + $data = $this->preTransform($data, $entity_type); + + if (empty($data)) { + $this->entity_count['invoices'] = 0; + return; + } + + $this->request_name = StoreInvoiceRequest::class; + $this->repository_name = InvoiceRepository::class; + $this->factory_name = InvoiceFactory::class; + + $this->repository = app()->make($this->repository_name); + $this->repository->import_mode = true; + + $this->transformer = new InvoiceTransformer($this->company); + + $invoice_count = $this->ingestInvoices($data, 'Invoice #'); + + $this->entity_count['invoices'] = $invoice_count; + + $this->company->update_products = $initial_update_products_value; + $this->company->save(); + + } + + } diff --git a/app/Import/Transformer/Freshbooks/ClientTransformer.php b/app/Import/Transformer/Freshbooks/ClientTransformer.php index 81c1bb790baf..d5b053232ad0 100644 --- a/app/Import/Transformer/Freshbooks/ClientTransformer.php +++ b/app/Import/Transformer/Freshbooks/ClientTransformer.php @@ -32,7 +32,7 @@ class ClientTransformer extends BaseTransformer { return [ 'company_id' => $this->company->id, 'name' => $this->getString( $data, 'Organization' ), - 'work_phone' => $this->getString( $data, 'Phone' ), + 'phone' => $this->getString( $data, 'Phone' ), 'address1' => $this->getString( $data, 'Street' ), 'city' => $this->getString( $data, 'City' ), 'state' => $this->getString( $data, 'Province/State' ), diff --git a/app/Import/Transformer/Freshbooks/InvoiceTransformer.php b/app/Import/Transformer/Freshbooks/InvoiceTransformer.php index 8e4d6bf435e9..458df12869fc 100644 --- a/app/Import/Transformer/Freshbooks/InvoiceTransformer.php +++ b/app/Import/Transformer/Freshbooks/InvoiceTransformer.php @@ -42,7 +42,7 @@ class InvoiceTransformer extends BaseTransformer { 'client_id' => $this->getClient( $this->getString( $invoice_data, 'Client Name' ), null ), 'number' => $this->getString( $invoice_data, 'Invoice #' ), 'date' => isset( $invoice_data['Date Issued'] ) ? date( 'Y-m-d', strtotime( $invoice_data['Date Issued'] ) ) : null, - 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ), + // 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ), 'amount' => 0, 'status_id' => $invoiceStatusMap[ $status = strtolower( $this->getString( $invoice_data, 'Invoice Status' ) ) ] ?? Invoice::STATUS_SENT, @@ -73,6 +73,7 @@ class InvoiceTransformer extends BaseTransformer { 'amount' => $transformed['amount'], ]]; } +nlog($transformed); return $transformed; } diff --git a/tests/Feature/Import/Freshbooks/FreshbooksTest.php b/tests/Feature/Import/Freshbooks/FreshbooksTest.php new file mode 100644 index 000000000000..3244ef264abf --- /dev/null +++ b/tests/Feature/Import/Freshbooks/FreshbooksTest.php @@ -0,0 +1,207 @@ +withoutMiddleware(ThrottleRequests::class); + + config(['database.default' => config('ninja.db.default')]); + + $this->makeTestData(); + + $this->withoutExceptionHandling(); + } + + public function testClientFreshbooksImport() + { + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/freshbooks_clients.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Organization', + 1 => 'First Name', + 2 => 'Last Name', + 3 => 'Email', + 4 => 'Phone', + 5 => 'Street', + 6 => 'City', + 7 => 'Province/State', + 8 => 'Country', + 9 => 'Postal Code', + 10 => 'Notes', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['client' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'freshbooks', + ]; + + Cache::put($hash . '-client', base64_encode($csv), 360); + + $csv_importer = new Freshbooks($data, $this->company); + + $count = $csv_importer->import('client'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasClient('Marge Simpson')); + $this->assertTrue($base_transformer->hasClient('X-Men')); + + $client_id = $base_transformer->getClient('', 'marge@simpsons.com'); + + $client = Client::find($client_id); + + $this->assertInstanceOf(Client::class, $client); + $this->assertEquals('840', $client->country_id); + $this->assertEquals('867-5309', $client->phone); + + + } + + public function testInvoiceZohoImport() + { + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/freshbooks_clients.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Organization', + 1 => 'First Name', + 2 => 'Last Name', + 3 => 'Email', + 4 => 'Phone', + 5 => 'Street', + 6 => 'City', + 7 => 'Province/State', + 8 => 'Country', + 9 => 'Postal Code', + 10 => 'Notes', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['client' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'freshbooks', + ]; + + Cache::put($hash . '-client', base64_encode($csv), 360); + + $csv_importer = new Freshbooks($data, $this->company); + + $count = $csv_importer->import('client'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasClient('Marge Simpson')); + $this->assertTrue($base_transformer->hasClient('X-Men')); + + $client_id = $base_transformer->getClient('', 'marge@simpsons.com'); + + $client = Client::find($client_id); + + $this->assertInstanceOf(Client::class, $client); + $this->assertEquals('840', $client->country_id); + $this->assertEquals('867-5309', $client->phone); + + //now import the invoices + + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/freshbooks_invoices.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Client Name', + 1 => 'Invoice #', + 2 => 'Date Issued', + 3 => 'Invoice Status', + 4 => 'Date Paid', + 5 => 'Item Name', + 6 => 'Item Description', + 7 => 'Rate', + 8 => 'Quantity', + 9 => 'Discount Percentage', + 10 => 'Line Subtotal', + 11 => 'Tax 1 Type', + 12 => 'Tax 1 Amount', + 13 => 'Tax 2 Type', + 14 => 'Tax 2 Amount', + 15 => 'Line Total', + 16 => 'Currency', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['invoice' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'freshbooks', + ]; + + Cache::put($hash . '-invoice', base64_encode($csv), 360); + + $csv_importer = new Freshbooks($data, $this->company); + + $count = $csv_importer->import('invoice'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasInvoice("0000001")); + $invoice_id = $base_transformer->getInvoiceId("0000001"); + $invoice = Invoice::find($invoice_id); + + $this->assertEquals(7890 , $invoice->amount); + $this->assertEquals(2 , $invoice->status_id); + $this->assertEquals(7890 , $invoice->balance); + $this->assertEquals(3 , count($invoice->line_items)); + + $this->assertFalse($invoice->payments()->exists()); + + } + + +} + diff --git a/tests/Feature/Import/Wave/WaveTest.php b/tests/Feature/Import/Wave/WaveTest.php index 50fe6122da69..8951ae25c28e 100644 --- a/tests/Feature/Import/Wave/WaveTest.php +++ b/tests/Feature/Import/Wave/WaveTest.php @@ -9,7 +9,7 @@ * @license https://opensource.org/licenses/AAL */ -namespace Tests\Feature\Import\CSV; +namespace Tests\Feature\Import\Wave; use App\Import\Providers\BaseImport; use App\Import\Providers\Wave; diff --git a/tests/Feature/Import/Zoho/ZohoTest.php b/tests/Feature/Import/Zoho/ZohoTest.php index 7f10e39b13ec..f4e6c862c686 100644 --- a/tests/Feature/Import/Zoho/ZohoTest.php +++ b/tests/Feature/Import/Zoho/ZohoTest.php @@ -9,7 +9,7 @@ * @license https://opensource.org/licenses/AAL */ -namespace Tests\Feature\Import\CSV; +namespace Tests\Feature\Import\Zoho; use App\Import\Providers\BaseImport; use App\Import\Providers\Zoho;