From 84dd24c07025668f57fbdc4872cdbb4abe13cf2a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 2 Jul 2021 09:48:11 +1000 Subject: [PATCH] Tests for credit balances --- app/Services/Client/ClientService.php | 1 + tests/Unit/CreditBalanceTest.php | 79 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/Unit/CreditBalanceTest.php diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 7b1b27dcfcad..9289a53604f1 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -51,6 +51,7 @@ class ClientService $credits = $this->client->credits ->where('is_deleted', false) ->where('balance', '>', 0) + ->where('due_date', '<=', now()) ->sortBy('created_at'); return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); diff --git a/tests/Unit/CreditBalanceTest.php b/tests/Unit/CreditBalanceTest.php new file mode 100644 index 000000000000..d8fb81761ed5 --- /dev/null +++ b/tests/Unit/CreditBalanceTest.php @@ -0,0 +1,79 @@ +faker = \Faker\Factory::create(); + } + + public function testCreditBalance() + { + + $account = Account::factory()->create(); + $user = User::factory()->create( + ['account_id' => $account->id, 'email' => $this->faker->safeEmail] + ); + + $company = Company::factory()->create(['account_id' => $account->id]); + $client = Client::factory()->create(['company_id' => $company->id, 'user_id' => $user->id]); + + $credit = Credit::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'balance' => 10, + 'number' => 'testing-number-01', + 'status_id' => Credit::STATUS_SENT, + ]); + + $this->assertEquals($client->service()->getCreditBalance(), 10); + } + + + public function testExpiredCreditBalance() + { + + $account = Account::factory()->create(); + $user = User::factory()->create( + ['account_id' => $account->id, 'email' => $this->faker->safeEmail] + ); + + $company = Company::factory()->create(['account_id' => $account->id]); + $client = Client::factory()->create(['company_id' => $company->id, 'user_id' => $user->id]); + + $credit = Credit::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'balance' => 10, + 'due_date' => now()->addDays(5), + 'number' => 'testing-number-02', + 'status_id' => Credit::STATUS_SENT, + ]); + + $this->assertEquals($client->service()->getCreditBalance(), 0); + + } +}