mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Invoicely import tests
This commit is contained in:
parent
20758256c9
commit
89d0cb7bb7
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
48
app/Import/Transformer/Invoicely/ClientTransformer.php
Normal file
48
app/Import/Transformer/Invoicely/ClientTransformer.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://clientninja.com).
|
||||
*
|
||||
* @link https://github.com/clientninja/clientninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Import\Transformer\Invoicely;
|
||||
|
||||
use App\Import\ImportException;
|
||||
use App\Import\Transformer\BaseTransformer;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Class ClientTransformer.
|
||||
*/
|
||||
class ClientTransformer extends BaseTransformer {
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function transform( $data ) {
|
||||
if ( isset( $data['Client Name'] ) && $this->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' ),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
59
app/Import/Transformer/Invoicely/InvoiceTransformer.php
Normal file
59
app/Import/Transformer/Invoicely/InvoiceTransformer.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* client Ninja (https://clientninja.com).
|
||||
*
|
||||
* @link https://github.com/clientninja/clientninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Import\Transformer\Invoicely;
|
||||
|
||||
use App\Import\ImportException;
|
||||
use App\Import\Transformer\BaseTransformer;
|
||||
use App\Models\Invoice;
|
||||
|
||||
/**
|
||||
* Class InvoiceTransformer.
|
||||
*/
|
||||
class InvoiceTransformer extends BaseTransformer {
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
public function transform( $data ) {
|
||||
if ( $this->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;
|
||||
}
|
||||
}
|
181
tests/Feature/Import/Invoicely/InvoicelyTest.php
Normal file
181
tests/Feature/Import/Invoicely/InvoicelyTest.php
Normal file
@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace Tests\Feature\Import\Invoicely;
|
||||
|
||||
use App\Import\Providers\BaseImport;
|
||||
use App\Import\Providers\Invoicely;
|
||||
use App\Import\Providers\Zoho;
|
||||
use App\Import\Transformer\BaseTransformer;
|
||||
use App\Models\Client;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Product;
|
||||
use App\Models\Vendor;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @covers App\Import\Providers\Invoicely
|
||||
*/
|
||||
class InvoicelyTest extends TestCase
|
||||
{
|
||||
use MakesHash;
|
||||
use MockAccountData;
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->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());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user