diff --git a/app/Services/Bank/ProcessBankRules.php b/app/Services/Bank/ProcessBankRules.php index 3bfef47086a2..ef21459245ee 100644 --- a/app/Services/Bank/ProcessBankRules.php +++ b/app/Services/Bank/ProcessBankRules.php @@ -43,6 +43,7 @@ class ProcessBankRules extends AbstractService private function matchCredit() { + $this->credit_rules = $this->bank_transaction->company->credit_rules(); $this->invoices = Invoice::where('company_id', $this->bank_transaction->company_id) @@ -52,7 +53,7 @@ class ProcessBankRules extends AbstractService $invoice = $this->invoices->first(function ($value, $key){ - return str_contains($this->bank_transaction, $value->number); + return str_contains($this->bank_transaction->description, $value->number); }); @@ -88,10 +89,6 @@ class ProcessBankRules extends AbstractService { $rule_count = count($bank_transaction_rule['rules']); - -nlog($rule_count); -nlog($rule); - if($rule['search_key'] == 'description') { nlog("searching key"); @@ -193,14 +190,9 @@ nlog($rule); $rule_value = strtolower(str_replace(" ", "", $rule_value)); $rule_length = iconv_strlen($rule_value); -nlog($bt_value); -nlog($rule_value); -nlog($rule_length); -nlog($operator); - return match ($operator) { 'is' => $bt_value == $rule_value, - 'contains' => str_contains($bt_value, $rule_value), + 'contains' => stripos($bt_value, $rule_value) !== false, 'starts_with' => substr($bt_value, 0, $rule_length) == $rule_value, 'is_empty' => empty($bt_value), default => false, diff --git a/tests/Feature/Bank/BankTransactionRuleTest.php b/tests/Feature/Bank/BankTransactionRuleTest.php index 8981bc0bf0ce..21567187604e 100644 --- a/tests/Feature/Bank/BankTransactionRuleTest.php +++ b/tests/Feature/Bank/BankTransactionRuleTest.php @@ -39,12 +39,227 @@ class BankTransactionRuleTest extends TestCase ); } + + public function testMatchingBankTransactionExpenseStartsWithMiss() + { + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'DEBIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'starts_with', + 'value' => 'chesst', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'ChESSSty coughs are terrible', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertNull($bt->expense_id); + } + + + + public function testMatchingBankTransactionExpenseStartsWith() + { + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'DEBIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'starts_with', + 'value' => 'chess', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'ChESSSty coughs are terrible', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertNotNull($bt->expense_id); + } + + + public function testMatchingBankTransactionExpenseContainsMiss() + { + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'DEBIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'contains', + 'value' => 'asdddfd', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'Something asd bizarre', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertNull($bt->expense_id); + } + + + public function testMatchingBankTransactionExpenseContains() + { + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'DEBIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'contains', + 'value' => 'asd', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'Something asd bizarre', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertNotNull($bt->expense_id); + } + + public function testMatchingBankTransactionExpenseMiss() + { + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'DEBIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'is', + 'value' => 'wallaby', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'Wall', + 'base_type' => 'DEBIT', + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertNull($bt->expense_id); + } + public function testMatchingBankTransactionExpense() { - // $this->expense->public_notes = "WaLLaBy"; - // $this->expense->save(); - - // $this->assertEquals('WaLLaBy', $this->expense->public_notes); $br = BankTransactionRule::factory()->create([ 'company_id' => $this->company->id, @@ -85,4 +300,53 @@ class BankTransactionRuleTest extends TestCase $this->assertNotNull($bt->expense_id); } + + public function testMatchingBankTransactionInvoice() + { + + $this->invoice->number = "MUHMUH"; + $this->invoice->save(); + + $br = BankTransactionRule::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'matches_on_all' => false, + 'auto_convert' => true, + 'applies_to' => 'CREDIT', + 'client_id' => $this->client->id, + 'vendor_id' => $this->vendor->id, + 'rules' => [ + [ + 'search_key' => 'description', + 'operator' => 'is', + 'value' => 'MUHMUH', + ] + ] + ]); + + $bi = BankIntegration::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'account_id' => $this->account->id, + ]); + + $bt = BankTransaction::factory()->create([ + 'bank_integration_id' => $bi->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'description' => 'MUHMUH', + 'base_type' => 'CREDIT', + 'amount' => 100 + ]); + + + $bt->service()->processRules(); + + $bt = $bt->fresh(); + + $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); + } + + + } \ No newline at end of file