diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index 4f9c3fee6cd0..6432b11f90e7 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -198,6 +198,7 @@ class CreateSingleAccount extends Command $btr = BankTransactionRule::factory()->create([ 'user_id' => $user->id, 'company_id' => $company->id, + 'applies_to' => (bool)rand(0,1) ? 'CREDIT' : 'DEBIT', ]); $this->info('Creating '.$this->count.' clients'); diff --git a/database/factories/BankTransactionRuleFactory.php b/database/factories/BankTransactionRuleFactory.php index 172bc5644626..af30349dffcc 100644 --- a/database/factories/BankTransactionRuleFactory.php +++ b/database/factories/BankTransactionRuleFactory.php @@ -26,7 +26,7 @@ class BankTransactionRuleFactory extends Factory { return [ 'name' => $this->faker->name(), - 'applies_to' => (bool)rand(0,1) ? 'CREDIT' : 'DEBIT', + // 'applies_to' => (bool)rand(0,1) ? 'CREDIT' : 'DEBIT', 'matches_on_all' => (bool)rand(0,1), 'auto_convert' => (bool)rand(0,1), ]; diff --git a/tests/Feature/Bank/BankTransactionRuleTest.php b/tests/Feature/Bank/BankTransactionRuleTest.php index 3be7ce68538e..2f3b8c215ee8 100644 --- a/tests/Feature/Bank/BankTransactionRuleTest.php +++ b/tests/Feature/Bank/BankTransactionRuleTest.php @@ -25,7 +25,6 @@ use Tests\TestCase; class BankTransactionRuleTest extends TestCase { - use DatabaseTransactions; use MockAccountData; @@ -44,8 +43,21 @@ class BankTransactionRuleTest extends TestCase public function testValidationContainsRule() { - //[{"search_key":"description","operator":"contains","value":"hello"}] + $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' => 'HellO ThErE CowBoY', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); $br = BankTransactionRule::factory()->create([ 'company_id' => $this->company->id, @@ -63,24 +75,12 @@ class BankTransactionRuleTest extends TestCase 'value' => 'hello', ] ] - ]); + ]); - $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' => 'HellO ThErE CowBoY', - 'base_type' => 'DEBIT', - 'amount' => 100 - ]); - + $bt = $bt->refresh(); + $debit_rules = $bt->company->debit_rules(); + $bt->service()->processRules(); $bt = $bt->fresh(); @@ -89,7 +89,7 @@ class BankTransactionRuleTest extends TestCase $this->assertNotNull($bt->expense->category_id); $this->assertNotNull($bt->expense->vendor_id); - + $bt = null; } @@ -152,6 +152,21 @@ class BankTransactionRuleTest extends TestCase public function testMatchingBankTransactionExpenseAmountLessThanEqualTo() { + $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' => 'xx', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + $br = BankTransactionRule::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, @@ -168,28 +183,17 @@ class BankTransactionRuleTest extends TestCase ] ] ]); - - $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' => '', - 'base_type' => 'DEBIT', - 'amount' => 100 - ]); + + $bt->company->refresh(); - $bt->service()->processRules(); + $bt->refresh()->service()->processRules(); $bt = $bt->fresh(); $this->assertNotNull($bt->expense_id); + + $bt=null; } @@ -272,7 +276,7 @@ class BankTransactionRuleTest extends TestCase ]); - $bt->service()->processRules(); + $bt->refresh()->service()->processRules(); $bt = $bt->fresh(); @@ -402,6 +406,7 @@ class BankTransactionRuleTest extends TestCase 'amount' => 100 ]); + $bt = $bt->refresh(); $bt->service()->processRules(); @@ -413,6 +418,22 @@ class BankTransactionRuleTest extends TestCase public function testMatchingBankTransactionExpenseIsEmptyMiss() { + $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' => 'asdadsa', + 'base_type' => 'DEBIT', + 'amount' => 100 + ]); + + $br = BankTransactionRule::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, @@ -430,21 +451,8 @@ class BankTransactionRuleTest extends TestCase ] ]); - $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' => 'asdadsa', - 'base_type' => 'DEBIT', - 'amount' => 100 - ]); + $bt->load('company'); $bt->service()->processRules();