From 47311c5f2cef091ac4f75deeaccba5c42dbd58bc Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Aug 2024 10:06:11 +1000 Subject: [PATCH] Tests for rules --- app/Services/Bank/ProcessBankRules.php | 38 +++++++----- .../Feature/Bank/BankTransactionRuleTest.php | 62 +++++++++++++++++++ 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/app/Services/Bank/ProcessBankRules.php b/app/Services/Bank/ProcessBankRules.php index 6199368d7d..fa27416742 100644 --- a/app/Services/Bank/ProcessBankRules.php +++ b/app/Services/Bank/ProcessBankRules.php @@ -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,17 +243,16 @@ 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) { - return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']); - }); - })->pluck("id"); + ->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"); } diff --git a/tests/Feature/Bank/BankTransactionRuleTest.php b/tests/Feature/Bank/BankTransactionRuleTest.php index 9cd6069626..21bb15734c 100644 --- a/tests/Feature/Bank/BankTransactionRuleTest.php +++ b/tests/Feature/Bank/BankTransactionRuleTest.php @@ -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() {