1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/tests/Feature/Import/CSV/CsvImportTest.php

322 lines
9.0 KiB
PHP
Raw Normal View History

2022-02-01 10:04:03 +01:00
<?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://www.elastic.co/licensing/elastic-license
2022-02-01 10:04:03 +01:00
*/
2022-02-03 09:43:28 +01:00
2022-02-01 10:04:03 +01:00
namespace Tests\Feature\Import\CSV;
use App\Import\Providers\Csv;
use App\Import\Transformer\BaseTransformer;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Vendor;
use App\Utils\Traits\MakesHash;
2023-01-18 08:56:15 +01:00
use App\Utils\TruthSource;
2022-02-01 10:04:03 +01:00
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\Csv
*/
class CsvImportTest extends TestCase
{
use MakesHash;
use MockAccountData;
protected function setUp(): void
2022-02-01 10:04:03 +01:00
{
parent::setUp();
2022-02-03 08:53:39 +01:00
$this->withoutMiddleware(ThrottleRequests::class);
2022-02-01 10:04:03 +01:00
config(['database.default' => config('ninja.db.default')]);
$this->makeTestData();
2022-02-03 08:53:39 +01:00
2022-02-01 10:04:03 +01:00
$this->withoutExceptionHandling();
2023-01-18 08:56:15 +01:00
auth()->login($this->user);
2022-02-01 10:04:03 +01:00
}
2022-02-03 08:53:39 +01:00
public function testExpenseCsvImport()
{
$csv = file_get_contents(
base_path().'/tests/Feature/Import/expenses.csv'
2022-02-03 08:53:39 +01:00
);
$hash = Str::random(32);
$column_map = [
0 => 'expense.client',
1 => 'expense.project',
2022-02-03 09:43:28 +01:00
2 => 'expense.public_notes',
2022-02-03 08:53:39 +01:00
3 => 'expense.amount',
];
$data = [
'hash' => $hash,
'column_map' => ['expense' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-expense', base64_encode($csv), 360);
2022-02-03 08:53:39 +01:00
$csv_importer = new Csv($data, $this->company);
$count = $csv_importer->import('expense');
2022-02-03 07:38:23 +01:00
2022-02-03 08:53:39 +01:00
$base_transformer = new BaseTransformer($this->company);
$this->assertTrue($base_transformer->hasProject('officiis'));
}
public function testVendorCsvImport()
{
$csv = file_get_contents(
base_path().'/tests/Feature/Import/vendors.csv'
2022-02-03 08:53:39 +01:00
);
$hash = Str::random(32);
2022-02-03 07:38:23 +01:00
$column_map = [
2022-02-03 08:53:39 +01:00
0 => 'vendor.name',
2022-02-03 07:38:23 +01:00
19 => 'vendor.currency_id',
20 => 'vendor.public_notes',
21 => 'vendor.private_notes',
22 => 'vendor.first_name',
23 => 'vendor.last_name',
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['vendor' => ['mapping' => $column_map]],
2022-02-03 07:38:23 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
$pre_import = Vendor::count();
Cache::put($hash.'-vendor', base64_encode($csv), 360);
2022-02-03 07:38:23 +01:00
$csv_importer = new Csv($data, $this->company);
$csv_importer->import('vendor');
$base_transformer = new BaseTransformer($this->company);
2022-02-03 08:53:39 +01:00
$this->assertTrue($base_transformer->hasVendor('Ludwig Krajcik DVM'));
2022-02-03 07:38:23 +01:00
}
2022-02-02 06:44:14 +01:00
public function testProductImport()
{
2022-02-03 08:53:39 +01:00
$csv = file_get_contents(
base_path().'/tests/Feature/Import/products.csv'
2022-02-03 08:53:39 +01:00
);
$hash = Str::random(32);
Cache::put($hash.'-product', base64_encode($csv), 360);
2022-02-02 06:44:14 +01:00
$column_map = [
1 => 'product.product_key',
2 => 'product.notes',
3 => 'product.cost',
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['product' => ['mapping' => $column_map]],
2022-02-02 06:44:14 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
$csv_importer = new Csv($data, $this->company);
2022-02-03 08:53:39 +01:00
2022-02-02 06:44:14 +01:00
$this->assertInstanceOf(Csv::class, $csv_importer);
$csv_importer->import('product');
$base_transformer = new BaseTransformer($this->company);
$this->assertTrue($base_transformer->hasProduct('officiis'));
}
public function testClientImport()
2022-02-01 10:04:03 +01:00
{
2022-02-03 08:53:39 +01:00
$csv = file_get_contents(
base_path().'/tests/Feature/Import/clients.csv'
2022-02-03 08:53:39 +01:00
);
2022-02-01 10:04:03 +01:00
$hash = Str::random(32);
$column_map = [
2022-02-03 08:53:39 +01:00
1 => 'client.balance',
2 => 'client.paid_to_date',
0 => 'client.name',
19 => 'client.currency_id',
20 => 'client.public_notes',
21 => 'client.private_notes',
22 => 'contact.first_name',
23 => 'contact.last_name',
24 => 'contact.email',
2022-02-01 10:04:03 +01:00
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['client' => ['mapping' => $column_map]],
2022-02-01 10:04:03 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-client', base64_encode($csv), 360);
2022-02-01 10:04:03 +01:00
$csv_importer = new Csv($data, $this->company);
2022-02-03 08:53:39 +01:00
2022-02-01 10:04:03 +01:00
$this->assertInstanceOf(Csv::class, $csv_importer);
$csv_importer->import('client');
$base_transformer = new BaseTransformer($this->company);
2022-02-03 08:53:39 +01:00
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
2022-02-10 03:55:44 +01:00
$this->assertTrue($base_transformer->hasClient('Bradly Jaskolski Sr.'));
2022-02-03 08:53:39 +01:00
$client_id = $base_transformer->getClient('Ludwig Krajcik DVM', null);
2022-02-01 10:04:03 +01:00
$c = Client::find($client_id);
$this->assertEquals($client_id, $c->id);
2022-02-03 08:53:39 +01:00
$client_id = $base_transformer->getClient(
'a non existent clent',
'brook59@example.org'
);
2022-02-01 10:04:03 +01:00
$this->assertEquals($client_id, $c->id);
}
2022-02-02 05:56:37 +01:00
public function testInvoiceImport()
{
/*Need to import clients first*/
2022-02-03 08:53:39 +01:00
$csv = file_get_contents(
base_path().'/tests/Feature/Import/clients.csv'
2022-02-03 08:53:39 +01:00
);
2022-02-02 05:56:37 +01:00
$hash = Str::random(32);
$column_map = [
1 => 'client.balance',
2 => 'client.paid_to_date',
0 => 'client.name',
19 => 'client.currency_id',
20 => 'client.public_notes',
21 => 'client.private_notes',
22 => 'contact.first_name',
23 => 'contact.last_name',
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['client' => ['mapping' => $column_map]],
2022-02-02 05:56:37 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-client', base64_encode($csv), 360);
2022-02-02 05:56:37 +01:00
$csv_importer = new Csv($data, $this->company);
2022-02-03 08:53:39 +01:00
2022-02-02 05:56:37 +01:00
$this->assertInstanceOf(Csv::class, $csv_importer);
$csv_importer->import('client');
$base_transformer = new BaseTransformer($this->company);
2022-02-03 08:53:39 +01:00
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
2022-02-02 05:56:37 +01:00
/* client import verified*/
/*Now import invoices*/
2022-02-03 08:53:39 +01:00
$csv = file_get_contents(
base_path().'/tests/Feature/Import/invoice.csv'
2022-02-03 08:53:39 +01:00
);
2022-02-02 05:56:37 +01:00
$hash = Str::random(32);
$column_map = [
1 => 'client.email',
3 => 'payment.amount',
5 => 'invoice.po_number',
8 => 'invoice.due_date',
9 => 'item.discount',
11 => 'invoice.partial_due_date',
12 => 'invoice.public_notes',
13 => 'invoice.private_notes',
0 => 'client.name',
2 => 'invoice.number',
7 => 'invoice.date',
14 => 'item.product_key',
15 => 'item.notes',
16 => 'item.cost',
17 => 'item.quantity',
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['invoice' => ['mapping' => $column_map]],
2022-02-02 05:56:37 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-invoice', base64_encode($csv), 360);
2022-02-02 05:56:37 +01:00
2023-01-18 08:56:15 +01:00
$truth = app()->make(TruthSource::class);
$truth->setCompanyUser($this->cu);
$truth->setUser($this->user);
$truth->setCompany($this->company);
2022-02-02 05:56:37 +01:00
$csv_importer = new Csv($data, $this->company);
$csv_importer->import('invoice');
2022-06-24 03:55:41 +02:00
$this->assertTrue($base_transformer->hasInvoice('780'));
2022-02-03 01:45:03 +01:00
/* Lets piggy back payments tests here to save rebuilding the test multiple times*/
2022-02-03 08:53:39 +01:00
$csv = file_get_contents(
base_path().'/tests/Feature/Import/payments.csv'
2022-02-03 08:53:39 +01:00
);
$hash = Str::random(32);
2022-02-03 01:45:03 +01:00
$column_map = [
0 => 'payment.client_id',
1 => 'payment.invoice_number',
2 => 'payment.amount',
3 => 'payment.date',
];
$data = [
2022-02-03 08:53:39 +01:00
'hash' => $hash,
'column_map' => ['payment' => ['mapping' => $column_map]],
2022-02-03 01:45:03 +01:00
'skip_header' => true,
'import_type' => 'csv',
];
Cache::put($hash.'-payment', base64_encode($csv), 360);
2022-02-03 01:45:03 +01:00
$csv_importer = new Csv($data, $this->company);
$csv_importer->import('payment');
2022-06-24 03:55:41 +02:00
$this->assertTrue($base_transformer->hasInvoice('780'));
2022-02-03 01:45:03 +01:00
2022-06-24 03:55:41 +02:00
$invoice_id = $base_transformer->getInvoiceId('780');
2022-02-03 01:45:03 +01:00
2022-06-24 03:55:41 +02:00
$invoice = Invoice::with('payments')->find($invoice_id);
2022-02-03 01:45:03 +01:00
$this->assertTrue($invoice->payments()->exists());
2022-06-24 03:55:41 +02:00
$this->assertEquals(1, $invoice->payments()->count());
2023-02-16 02:36:09 +01:00
$this->assertEquals(51.03, round($invoice->payments()->sum('payments.amount'), 2));
2022-02-02 05:56:37 +01:00
}
2022-02-01 10:04:03 +01:00
}