From 89d0cb7bb75910d81e5633831f8618b696cc688d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Feb 2022 12:12:51 +1100 Subject: [PATCH] Invoicely import tests --- app/Import/Providers/Invoicely.php | 96 +++++++++- .../Invoicely/ClientTransformer.php | 48 +++++ .../Invoicely/InvoiceTransformer.php | 59 ++++++ .../Import/Invoicely/InvoicelyTest.php | 181 ++++++++++++++++++ 4 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 app/Import/Transformer/Invoicely/ClientTransformer.php create mode 100644 app/Import/Transformer/Invoicely/InvoiceTransformer.php create mode 100644 tests/Feature/Import/Invoicely/InvoicelyTest.php diff --git a/app/Import/Providers/Invoicely.php b/app/Import/Providers/Invoicely.php index 55feff8f76cc..09ed7f1cba15 100644 --- a/app/Import/Providers/Invoicely.php +++ b/app/Import/Providers/Invoicely.php @@ -10,7 +10,101 @@ */ 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\Invoicely\ClientTransformer; +use App\Import\Transformer\Invoicely\InvoiceTransformer; +use App\Repositories\ClientRepository; +use App\Repositories\InvoiceRepository; + class Invoicely extends BaseImport { - + 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, false); + + $this->entity_count['invoices'] = $invoice_count; + + $this->company->update_products = $initial_update_products_value; + $this->company->save(); + + } + } \ No newline at end of file diff --git a/app/Import/Transformer/Invoicely/ClientTransformer.php b/app/Import/Transformer/Invoicely/ClientTransformer.php new file mode 100644 index 000000000000..05e22eb1fe8c --- /dev/null +++ b/app/Import/Transformer/Invoicely/ClientTransformer.php @@ -0,0 +1,48 @@ +hasClient( $data['Client Name'] ) ) { + throw new ImportException('Client already exists'); + } + + return [ + 'company_id' => $this->company->id, + 'name' => $this->getString( $data, 'Client Name' ), + 'phone' => $this->getString( $data, 'Phone' ), + 'country_id' => isset( $data['Country'] ) ? $this->getCountryIdBy2( $data['Country'] ) : null, + 'credit_balance' => 0, + 'settings' => new \stdClass, + 'client_hash' => Str::random( 40 ), + 'contacts' => [ + [ + 'email' => $this->getString( $data, 'Email' ), + 'phone' => $this->getString( $data, 'Phone' ), + ], + ], + ]; + } +} diff --git a/app/Import/Transformer/Invoicely/InvoiceTransformer.php b/app/Import/Transformer/Invoicely/InvoiceTransformer.php new file mode 100644 index 000000000000..ee433689383f --- /dev/null +++ b/app/Import/Transformer/Invoicely/InvoiceTransformer.php @@ -0,0 +1,59 @@ +hasInvoice( $data['Details'] ) ) { + throw new ImportException( 'Invoice number already exists' ); + } + + $transformed = [ + 'company_id' => $this->company->id, + 'client_id' => $this->getClient( $this->getString( $data, 'Client' ), null ), + 'number' => $this->getString( $data, 'Details' ), + 'date' => isset( $data['Date'] ) ? date( 'Y-m-d', strtotime( $data['Date'] ) ) : null, + 'due_date' => isset( $data['Due'] ) ? date( 'Y-m-d', strtotime( $data['Due'] ) ) : null, + 'status_id' => Invoice::STATUS_SENT, + 'line_items' => [ + [ + 'cost' => $amount = $this->getFloat( $data, 'Total' ), + 'quantity' => 1, + ], + ], + ]; + + if ( strtolower( $data['Status'] ) === 'paid' ) { + $transformed['payments'] = [ + [ + 'date' => date( 'Y-m-d' ), + 'amount' => $amount, + ], + ]; + } +nlog($transformed); + + return $transformed; + } +} diff --git a/tests/Feature/Import/Invoicely/InvoicelyTest.php b/tests/Feature/Import/Invoicely/InvoicelyTest.php new file mode 100644 index 000000000000..50c0f428e801 --- /dev/null +++ b/tests/Feature/Import/Invoicely/InvoicelyTest.php @@ -0,0 +1,181 @@ +withoutMiddleware(ThrottleRequests::class); + + config(['database.default' => config('ninja.db.default')]); + + $this->makeTestData(); + + $this->withoutExceptionHandling(); + } + + public function testClientInvoicelyImport() + { + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/invoicely_clients.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Client Name', + 1 => 'Email', + 2 => 'Phone', + 3 => 'Country', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['client' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'invoicely', + ]; + + Cache::put($hash . '-client', base64_encode($csv), 360); + + $csv_importer = new Invoicely($data, $this->company); + + $count = $csv_importer->import('client'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasClient('Alexander Hamilton')); + $this->assertTrue($base_transformer->hasClient('Bruce Wayne')); + + $client_id = $base_transformer->getClient('', 'alexander@iamhamllton.com'); + + $client = Client::find($client_id); + + $this->assertInstanceOf(Client::class, $client); + $this->assertEquals('5558675309', $client->phone); + + + } + + public function testInvoiceInvoicelyImport() + { + + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/invoicely_clients.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Client Name', + 1 => 'Email', + 2 => 'Phone', + 3 => 'Country', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['client' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'invoicely', + ]; + + Cache::put($hash . '-client', base64_encode($csv), 360); + + $csv_importer = new Invoicely($data, $this->company); + + $count = $csv_importer->import('client'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasClient('Alexander Hamilton')); + $this->assertTrue($base_transformer->hasClient('Bruce Wayne')); + + $client_id = $base_transformer->getClient('', 'alexander@iamhamllton.com'); + + $client = Client::find($client_id); + + $this->assertInstanceOf(Client::class, $client); + $this->assertEquals('5558675309', $client->phone); + //now import the invoices + + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/invoicely_invoices.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Date', + 1 => 'Due', + 2 => 'Details', + 3 => 'Client', + 4 => 'Status', + 5 => 'Total', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['invoice' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'invoicely', + ]; + + Cache::put($hash . '-invoice', base64_encode($csv), 360); + + $csv_importer = new Invoicely($data, $this->company); + + $count = $csv_importer->import('invoice'); + + $base_transformer = new BaseTransformer($this->company); +nlog($count); + $this->assertTrue($base_transformer->hasInvoice("INV-1")); + + $invoice_id = $base_transformer->getInvoiceId("INV-1"); + $invoice = Invoice::find($invoice_id); + + $this->assertEquals(1020 , $invoice->amount); + $this->assertEquals(2 , $invoice->status_id); + $this->assertEquals(1020 , $invoice->balance); + $this->assertEquals(1 , count($invoice->line_items)); + + $this->assertFalse($invoice->payments()->exists()); + + } + + +} +