mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Tests for rules
This commit is contained in:
parent
999ccfd990
commit
47311c5f2c
@ -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)) {
|
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->vendor_id = $bank_transaction_rule->vendor_id;
|
||||||
$this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
|
// $this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
|
||||||
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
||||||
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
|
$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();
|
$this->bank_transaction->save();
|
||||||
|
|
||||||
|
|
||||||
//auto-convert
|
//auto-convert
|
||||||
|
|
||||||
if ($bank_transaction_rule['auto_convert']) {
|
if ($bank_transaction_rule['auto_convert']) {
|
||||||
@ -221,13 +228,13 @@ class ProcessBankRules extends AbstractService
|
|||||||
private function searchInvoiceResource(string $column, array $rule, $invoices)
|
private function searchInvoiceResource(string $column, array $rule, $invoices)
|
||||||
{
|
{
|
||||||
|
|
||||||
return $invoices->when($rule['search_key'] == 'description', function ($q) use ($rule, $column) {
|
return $invoices->when($column != 'amount', function ($q) use ($rule, $column) {
|
||||||
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
return $q->filter(function ($record) use ($rule, $column) {
|
||||||
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
->when($rule['search_key'] == 'amount', function ($q) use ($rule, $column) {
|
->when($column == 'amount', function ($q) use ($rule, $column) {
|
||||||
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
return $q->filter(function ($record) use ($rule, $column) {
|
||||||
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
||||||
});
|
});
|
||||||
})->pluck("id");
|
})->pluck("id");
|
||||||
@ -236,14 +243,13 @@ class ProcessBankRules extends AbstractService
|
|||||||
|
|
||||||
private function searchPaymentResource(string $column, array $rule, $payments)
|
private function searchPaymentResource(string $column, array $rule, $payments)
|
||||||
{
|
{
|
||||||
|
return $payments->when($column != 'amount', function ($q) use ($rule, $column) {
|
||||||
return $payments->when($rule['search_key'] == 'description', function ($q) use ($rule, $column) {
|
return $q->filter(function ($record) use ($rule, $column) {
|
||||||
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
|
||||||
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
->when($rule['search_key'] == 'amount', function ($q) use ($rule, $column) {
|
->when($column == 'amount', function ($q) use ($rule, $column) {
|
||||||
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
return $q->filter(function ($record) use ($rule, $column) {
|
||||||
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
||||||
});
|
});
|
||||||
})->pluck("id");
|
})->pluck("id");
|
||||||
|
@ -13,10 +13,12 @@
|
|||||||
namespace Tests\Feature\Bank;
|
namespace Tests\Feature\Bank;
|
||||||
|
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use App\Models\Payment;
|
||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
use App\Models\BankIntegration;
|
use App\Models\BankIntegration;
|
||||||
use App\Models\BankTransaction;
|
use App\Models\BankTransaction;
|
||||||
use App\Models\BankTransactionRule;
|
use App\Models\BankTransactionRule;
|
||||||
|
use App\Services\Bank\ProcessBankRules;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
@ -39,6 +41,66 @@ class BankTransactionRuleTest extends TestCase
|
|||||||
$this->withoutExceptionHandling();
|
$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()
|
public function testMatchCreditOnInvoiceNumber()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user