1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Importing documents (#3348)

* Importing documents

* Add polymorphic types

* Update documentable namespace & tests
This commit is contained in:
Benjamin Beganović 2020-02-18 21:53:12 +01:00 committed by GitHub
parent 961788e59f
commit 96a250edac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5749 additions and 2 deletions

View File

@ -21,6 +21,7 @@ use App\Mail\MigrationFailed;
use App\Models\Client;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Document;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Product;
@ -63,7 +64,7 @@ class Import implements ShouldQueue
* @var array
*/
private $available_imports = [
'company', 'users', 'tax_rates', 'clients', 'products', 'invoices', 'quotes', 'payments', 'credits',
'company', 'users', 'tax_rates', 'clients', 'products', 'invoices', 'quotes', 'payments', 'credits', 'documents',
];
/**
@ -507,6 +508,52 @@ class Import implements ShouldQueue
}
}
private function processDocuments(array $data): void
{
/** No validators since data provided by database is already valid. */
foreach ($data as $resource) {
$modified = $resource;
if (array_key_exists('invoice_id', $resource) && !array_key_exists('invoices', $this->ids)) {
throw new ResourceDependencyMissing(array_key_first($data), 'invoices');
}
if (array_key_exists('expense_id', $resource) && !array_key_exists('expenses', $this->ids)) {
throw new ResourceDependencyMissing(array_key_first($data), 'expenses');
}
/** Remove because of polymorphic joins. */
unset($modified['invoice_id']);
unset($modified['expense_id']);
if(array_key_exists('invoice_id', $resource) && $resource['invoice_id']) {
$modified['documentable_id'] = $this->transformId('invoices', $resource['invoice_id']);
$modified['documentable_type'] = 'App\\Models\\Invoice';
}
if(array_key_exists('expense_id', $resource) && $resource['expense_id']) {
$modified['documentable_id'] = $this->transformId('expense', $resource['expense_id']);
$modified['documentable_type'] = 'App\\Models\\Expense';
}
$modified['user_id'] = $this->processUserId($resource);
$modified['company_id'] = $this->company->id;
$payment = Document::create($modified);
$old_user_key = array_key_exists('user_id', $resource) ?? $this->user->id;
$this->ids['payments'] = [
"payments_{$old_user_key}" => [
'old' => $old_user_key,
'new' => $payment->id,
]
];
}
}
/**
* |--------------------------------------------------------------------------
* | Additional migration methods.

View File

@ -12,6 +12,7 @@ use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Document;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\Payment;
@ -422,6 +423,15 @@ class ImportTest extends TestCase
}
}*/
foreach ($this->migration_array['documents'] as $key => $document) {
$record = Document::whereHash('5a81aa656c8aaf77dca259b7defdda1dc5ae7901')
->first();
if (!$record) {
$differences['documents']['missing'][] = $document['id'];
}
}
$this->assertCount(0, $differences);
}
@ -439,4 +449,20 @@ class ImportTest extends TestCase
$this->assertGreaterThan($original, ClientContact::count());
}
public function testDocumentsImport()
{
$this->invoice->forceDelete();
$original = Document::count();
Import::dispatchNow($this->migration_array, $this->company, $this->user);
$this->assertGreaterThan($original, Document::count());
$document = Document::first();
$this->assertNotNull(Invoice::find($document->documentable_id)->documents);
$this->assertNotNull($document->documentable);
}
}

File diff suppressed because one or more lines are too long