Data matching for bank integration

This commit is contained in:
David Bomba 2022-08-12 15:25:18 +10:00
parent de33548908
commit 2da7f4e17c
6 changed files with 141 additions and 1 deletions

View File

@ -12,6 +12,7 @@
namespace App\Jobs\Bank;
use App\Helpers\Bank\Yodlee\Yodlee;
use App\Libraries\MultiDB;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Services\Bank\BankService;
@ -112,6 +113,11 @@ class ProcessBankTransactions implements ShouldQueue
BankService::dispatch($company->id, $company->db);
MultiDB::setDb($company->db);
$this->bank_integration->from_date = now();
$this->bank_integration->save();
}
}

View File

@ -12,6 +12,7 @@
namespace App\Services\Bank;
use App\Libraries\MultiDB;
use App\Models\BankTransaction;
use App\Models\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -55,6 +56,7 @@ class BankService implements ShouldQueue
{
BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false)
->where('provisional_match', false)
->cursor()
->each(function ($bt){
@ -67,6 +69,7 @@ class BankService implements ShouldQueue
if($invoice)
{
$bt->invoice_id = $invoice->id;
$bt->provisional_match = true;
$bt->save();
}

View File

@ -68,6 +68,7 @@ return new class extends Migration
$table->unsignedInteger('invoice_id')->nullable();
$table->unsignedInteger('expense_id')->nullable();
$table->boolean('is_matched')->default(0);
$table->boolean('provisional_match')->default(0);
$table->boolean('is_deleted')->default(0);
$table->timestamps(6);

View File

@ -487,5 +487,4 @@ nlog($accounts);
}

View File

@ -0,0 +1,126 @@
<?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
*/
namespace Tests\Feature\Bank;
use App\Models\BankTransaction;
use App\Models\Invoice;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData;
use Tests\TestCase;
class YodleeBankTransactionTest extends TestCase
{
use DatabaseTransactions;
use MockAccountData;
protected function setUp() :void
{
parent::setUp();
if(!config('ninja.yodlee.client_id'))
$this->markTestSkipped('Skip test no Yodlee API credentials found');
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testDataMatching1()
{
$this->invoice->number = "super-funk-1234";
$this->invoice->save();
$bank_transaction = BankTransaction::where('company_id', $this->company->id)->first();
$bank_transaction->description = "super-funk-1234";
$bank_transaction->save();
$this->assertNotNull($this->invoice);
$this->assertNotNull($bank_transaction);
$invoices = Invoice::where('company_id', $this->company->id)->get();
BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false)
->where('provisional_match', false)
->cursor()
->each(function ($bt) use($invoices){
$invoice = $invoices->first(function ($value, $key) use ($bt){
return str_contains($value->number, $bt->description);
});
if($invoice)
{
$bt->invoice_id = $invoice->id;
$bt->provisional_match = $invoice->id;
$bt->save();
}
});
$this->assertTrue(BankTransaction::where('invoice_id', $this->invoice->id)->exists());
}
public function testDataMatching2()
{
$this->invoice->number = "super-funk-1234";
$this->invoice->save();
$bank_transaction = BankTransaction::where('company_id', $this->company->id)->first();
$bank_transaction->description = "super-funk-123";
$bank_transaction->save();
$this->assertNotNull($this->invoice);
$this->assertNotNull($bank_transaction);
$invoices = Invoice::where('company_id', $this->company->id)->get();
BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false)
->where('provisional_match', false)
->cursor()
->each(function ($bt) use($invoices){
$invoice = $invoices->first(function ($value, $key) use ($bt){
return str_contains($value->number, $bt->description);
});
if($invoice)
{
$bt->invoice_id = $invoice->id;
$bt->provisional_match = $invoice->id;
$bt->save();
}
});
$this->assertTrue(BankTransaction::where('invoice_id', $this->invoice->id)->exists());
}
}

View File

@ -545,26 +545,31 @@ trait MockAccountData
$this->bank_transaction = BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'bank_integration_id' => $this->bank_integration->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'bank_integration_id' => $this->bank_integration->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'bank_integration_id' => $this->bank_integration->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'bank_integration_id' => $this->bank_integration->id,
]);
BankTransaction::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
'bank_integration_id' => $this->bank_integration->id,
]);
$invitations = CreditInvitation::whereCompanyId($this->credit->company_id)