1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Tests for rules

This commit is contained in:
David Bomba 2024-08-27 10:06:11 +10:00
parent 999ccfd990
commit 47311c5f2c
2 changed files with 84 additions and 16 deletions

View File

@ -142,13 +142,20 @@ class ProcessBankRules extends AbstractService
if (($bank_transaction_rule['matches_on_all'] && $this->checkMatchSetForKey($match_set, $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && count($match_set) > 0)) {
$this->bank_transaction->vendor_id = $bank_transaction_rule->vendor_id;
$this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
// $this->bank_transaction->vendor_id = $bank_transaction_rule->vendor_id;
// $this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
$first_result = reset($match_set);
if($first_result[0] == Payment::class) {
$payment_id = $first_result[1][0];
$this->bank_transaction->payment_id = $payment_id;
}
$this->bank_transaction->save();
//auto-convert
if ($bank_transaction_rule['auto_convert']) {
@ -221,13 +228,13 @@ class ProcessBankRules extends AbstractService
private function searchInvoiceResource(string $column, array $rule, $invoices)
{
return $invoices->when($rule['search_key'] == 'description', function ($q) use ($rule, $column) {
return $q->cursor()->filter(function ($record) use ($rule, $column) {
return $invoices->when($column != 'amount', function ($q) use ($rule, $column) {
return $q->filter(function ($record) use ($rule, $column) {
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
});
})
->when($rule['search_key'] == 'amount', function ($q) use ($rule, $column) {
return $q->cursor()->filter(function ($record) use ($rule, $column) {
->when($column == 'amount', function ($q) use ($rule, $column) {
return $q->filter(function ($record) use ($rule, $column) {
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
});
})->pluck("id");
@ -236,14 +243,13 @@ class ProcessBankRules extends AbstractService
private function searchPaymentResource(string $column, array $rule, $payments)
{
return $payments->when($rule['search_key'] == 'description', function ($q) use ($rule, $column) {
return $q->cursor()->filter(function ($record) use ($rule, $column) {
return $payments->when($column != 'amount', function ($q) use ($rule, $column) {
return $q->filter(function ($record) use ($rule, $column) {
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
});
})
->when($rule['search_key'] == 'amount', function ($q) use ($rule, $column) {
return $q->cursor()->filter(function ($record) use ($rule, $column) {
->when($column == 'amount', function ($q) use ($rule, $column) {
return $q->filter(function ($record) use ($rule, $column) {
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
});
})->pluck("id");

View File

@ -13,10 +13,12 @@
namespace Tests\Feature\Bank;
use Tests\TestCase;
use App\Models\Payment;
use Tests\MockAccountData;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\BankTransactionRule;
use App\Services\Bank\ProcessBankRules;
use Illuminate\Validation\ValidationException;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -39,6 +41,66 @@ class BankTransactionRuleTest extends TestCase
$this->withoutExceptionHandling();
}
public function testNewCreditMatchingRules()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = md5(time());
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$payment.amount',
'operator' => '=',
]
]
]);
$p = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'transaction_reference' => 'nein'
]);
nlog($p->id);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($p->id);
$this->assertNotNull($bt->payment_id);
$this->assertEquals($p->id, $bt->payment_id);
}
public function testMatchCreditOnInvoiceNumber()
{