From 20758256c947784a98bd19b607ea53be82528e80 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Feb 2022 11:51:22 +1100 Subject: [PATCH] Invoice 2 Go tests --- app/Import/Providers/BaseImport.php | 11 +- app/Import/Providers/Freshbooks.php | 1 - app/Import/Providers/Invoice2Go.php | 70 ++++++++- .../Invoice2Go/InvoiceTransformer.php | 95 +++++++++++ .../Import/Invoice2Go/Invoice2GoTest.php | 147 ++++++++++++++++++ 5 files changed, 317 insertions(+), 7 deletions(-) create mode 100644 app/Import/Transformer/Invoice2Go/InvoiceTransformer.php create mode 100644 tests/Feature/Import/Invoice2Go/Invoice2GoTest.php diff --git a/app/Import/Providers/BaseImport.php b/app/Import/Providers/BaseImport.php index fea2b00048f4..26e063dc9746 100644 --- a/app/Import/Providers/BaseImport.php +++ b/app/Import/Providers/BaseImport.php @@ -123,6 +123,9 @@ class BaseImport private function groupInvoices($csvData, $key) { + if(!$key) + return $csvData; + // Group by invoice. $grouped = []; @@ -215,6 +218,7 @@ class BaseImport foreach ($invoices as $raw_invoice) { try { + $invoice_data = $invoice_transformer->transform($raw_invoice); $invoice_data['line_items'] = $this->cleanItems( @@ -242,10 +246,6 @@ class BaseImport unset($invoice_data['client']); } - // $validator = Validator::make( - // $invoice_data, - // (new StoreInvoiceRequest())->rules() - // ); $validator = $this->request_name::runFormRequest($invoice_data); if ($validator->fails()) { @@ -516,8 +516,9 @@ class BaseImport public function preTransform(array $data, $entity_type) { + if (empty($this->column_map[$entity_type])) { - return false; + return $data; } if ($this->skip_header) { diff --git a/app/Import/Providers/Freshbooks.php b/app/Import/Providers/Freshbooks.php index 881d91c26295..503d967f5bb3 100644 --- a/app/Import/Providers/Freshbooks.php +++ b/app/Import/Providers/Freshbooks.php @@ -49,7 +49,6 @@ class Freshbooks extends BaseImport $entity_type = 'client'; $data = $this->getCsvData($entity_type); - $data = $this->preTransform($data, $entity_type); if (empty($data)) { diff --git a/app/Import/Providers/Invoice2Go.php b/app/Import/Providers/Invoice2Go.php index d2e97435c13a..47336cc6f59b 100644 --- a/app/Import/Providers/Invoice2Go.php +++ b/app/Import/Providers/Invoice2Go.php @@ -10,7 +10,75 @@ */ 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\Invoice2Go\InvoiceTransformer; +use App\Repositories\ClientRepository; +use App\Repositories\InvoiceRepository; + class Invoice2Go 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 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, 'invoice'); + + 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/Invoice2Go/InvoiceTransformer.php b/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php new file mode 100644 index 000000000000..c55c1561dfd9 --- /dev/null +++ b/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php @@ -0,0 +1,95 @@ +hasInvoice( $invoice_data['DocumentNumber'] ) ) { + throw new ImportException( 'Invoice number already exists' ); + } + + $invoiceStatusMap = [ + 'unsent' => Invoice::STATUS_DRAFT, + 'sent' => Invoice::STATUS_SENT, + ]; + + $transformed = [ + 'company_id' => $this->company->id, + 'number' => $this->getString( $invoice_data, 'DocumentNumber' ), + 'notes' => $this->getString( $invoice_data, 'Comment' ), + 'date' => isset( $invoice_data['DocumentDate'] ) ? date( 'Y-m-d', strtotime( $invoice_data['DocumentDate'] ) ) : null, + // 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ), + 'amount' => $this->getFloat( $invoice_data, 'TotalAmount' ), + 'status_id' => $invoiceStatusMap[ $status = + strtolower( $this->getString( $invoice_data, 'DocumentStatus' ) ) ] ?? Invoice::STATUS_SENT, + // 'viewed' => $status === 'viewed', + 'line_items' => [ + [ + 'cost' => $this->getFloat( $invoice_data, 'TotalAmount' ), + 'quantity' => 1, + 'discount' => $this->getFloat( $invoice_data, 'DiscountValue' ), + 'is_amount_discount' => false, + ], + ], + ]; + + $client_id = + $this->getClient( $this->getString( $invoice_data, 'Name' ), $this->getString( $invoice_data, 'EmailRecipient' ) ); + + if ( $client_id ) { + $transformed['client_id'] = $client_id; + } else { + + $settings = new \stdClass; + $settings->currency_id = $this->getCurrencyByCode( $invoice_data, 'Currency' ); + + $transformed['client'] = [ + 'name' => $this->getString( $invoice_data, 'Name' ), + 'address1' => $this->getString( $invoice_data, 'DocumentRecipientAddress' ), + 'shipping_address1' => $this->getString( $invoice_data, 'ShipAddress' ), + 'credit_balance' => 0, + 'settings' => $settings, + 'client_hash' => Str::random( 40 ), + 'contacts' => [ + [ + 'email' => $this->getString( $invoice_data, 'EmailRecipient' ), + ], + ], + ]; + } + if ( ! empty( $invoice_data['Date Paid'] ) ) { + $transformed['payments'] = [ + [ + 'date' => date( 'Y-m-d', strtotime( $invoice_data['DatePaid'] ) ), + 'amount' => $this->getFloat( $invoice_data, 'Payments' ), + ], + ]; + } +nlog($transformed); + + return $transformed; + } +} diff --git a/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php b/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php new file mode 100644 index 000000000000..50b3db147d7a --- /dev/null +++ b/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php @@ -0,0 +1,147 @@ +withoutMiddleware(ThrottleRequests::class); + + config(['database.default' => config('ninja.db.default')]); + + $this->makeTestData(); + + $this->withoutExceptionHandling(); + } + + public function testInvoice2GoImport() + { + $csv = file_get_contents( + base_path() . '/tests/Feature/Import/i2g_invoices.csv' + ); + $hash = Str::random(32); + + $column_map = [ + 0 => 'Id', + 1 => 'AccountId', + 2 => 'State', + 3 => 'LastUpdated', + 4 => 'CreatedDate', + 5 => 'DocumentDate', + 6 => 'DocumentNumber', + 7 => 'Comment', + 8 => 'Name', + 9 => 'EmailRecipient', + 10 => 'DocumentStatus', + 11 => 'OutputStatus', + 12 => 'PartPayment', + 13 => 'DocumentRecipientAddress', + 14 => 'ShipDate', + 15 => 'ShipAddress', + 16 => 'ShipAmount', + 17 => 'ShipTrackingNumber', + 18 => 'ShipVia', + 19 => 'ShipFob', + 20 => 'StyleName', + 21 => 'DatePaid', + 22 => 'CustomField', + 23 => 'DiscountType', + 24 => 'Discount', + 25 => 'DiscountValue', + 26 => 'Taxes', + 27 => 'WithholdingTaxName', + 28 => 'WithholdingTaxRate', + 29 => 'TermsOld', + 30 => 'Payments', + 31 => 'Items', + 32 => 'CurrencyCode', + 33 => 'TotalAmount', + 34 => 'BalanceDueAmount', + 35 => 'AmountPaidAmount', + 36 => 'DiscountAmount', + 37 => 'SubtotalAmount', + 38 => 'TotalTaxAmount', + 39 => 'WithholdingTaxAmount', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => ['invoice' => ['mapping' => $column_map]], + 'skip_header' => true, + 'import_type' => 'invoice2go', + ]; + + Cache::put($hash . '-invoice', base64_encode($csv), 360); + + $csv_importer = new Invoice2Go($data, $this->company); + + $count = $csv_importer->import('invoice'); + + $base_transformer = new BaseTransformer($this->company); + + $this->assertTrue($base_transformer->hasClient('Barry Gordon')); + $this->assertTrue($base_transformer->hasClient('James Gordon')); + $this->assertTrue($base_transformer->hasClient('Deadpool Inc')); + $this->assertTrue($base_transformer->hasClient('Daily Planet')); + + $client_id = $base_transformer->getClient('', 'wade@projectx.net'); + + $client = Client::find($client_id); + + $this->assertInstanceOf(Client::class, $client); + $this->assertEquals('840', $client->country_id); + $this->assertEquals('2584 Sesame Street', $client->address1); + + $this->assertTrue($base_transformer->hasInvoice("1")); + $this->assertTrue($base_transformer->hasInvoice("2")); + $this->assertTrue($base_transformer->hasInvoice("3")); + $this->assertTrue($base_transformer->hasInvoice("4")); + $invoice_id = $base_transformer->getInvoiceId("1"); + $invoice = Invoice::find($invoice_id); + + $this->assertEquals(953.55, $invoice->amount); + $this->assertEquals(1 , $invoice->status_id); + $this->assertEquals(0, $invoice->balance); + + } + + +} +