From 59e3ab999388974b3cc385690b9b4a61f61457b6 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 22 Aug 2022 08:48:52 +1000 Subject: [PATCH] Working on client credit balance field --- app/Repositories/BaseRepository.php | 5 +++ tests/Feature/ClientTest.php | 47 ++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 3d1f623e5d0f..969bfc57363a 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -339,6 +339,11 @@ class BaseRepository else event('eloquent.updated: App\Models\Credit', $model); + if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Credit::STATUS_DRAFT)) { + + $model->client->service()->adjustCreditBalance(($state['finished_amount'] - $state['starting_amount']))->save(); + } + } if ($model instanceof Quote) { diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index e17378dee5d8..e321b29c6a75 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -63,21 +63,18 @@ class ClientTest extends TestCase $this->makeTestData(); } - private function buildLineItems() + private function buildLineItems($number = 2) { $line_items = []; - $item = InvoiceItemFactory::create(); - $item->quantity = 1; - $item->cost = 10; + for($x=0; $x<$number; $x++) + { + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; - $line_items[] = $item; - - $item = InvoiceItemFactory::create(); - $item->quantity = 1; - $item->cost = 10; - - $line_items[] = $item; + $line_items[] = $item; + } return $line_items; } @@ -127,6 +124,34 @@ class ClientTest extends TestCase $this->assertEquals(20, $credit->balance); $this->assertEquals(20, $credit->client->fresh()->credit_balance); + //lets now update the credit and increase its balance, this should also increase the credit balance + + $data = [ + 'number' => 'dfdfd', + 'discount' => 0, + 'is_amount_discount' => 1, + 'number' => '34343xx43', + 'public_notes' => 'notes', + 'is_deleted' => 0, + 'custom_value1' => 0, + 'custom_value2' => 0, + 'custom_value3' => 0, + 'custom_value4' => 0, + 'status' => 1, + 'client_id' => $this->encodePrimaryKey($this->client->id), + 'line_items' => $this->buildLineItems(3) + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/credits/'.$credit->hashed_id, $data) + ->assertStatus(200); + + $credit = $credit->fresh(); + + $this->assertEquals(30, $credit->balance); + $this->assertEquals(30, $credit->client->fresh()->credit_balance); } public function testStoreClientUsingCountryCode()