diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index eeac8f404766..092f6ef98535 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -142,6 +142,7 @@ class CreditService $client = $this->credit->client->fresh(); $client->service() ->updatePaidToDate($adjustment) + ->adjustCreditBalance($adjustment * -1) ->save(); event('eloquent.created: App\Models\Payment', $payment); diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index 8773c939c5db..40daa3edc3a8 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -45,6 +45,11 @@ class MarkSent ->touchPdf() ->save(); + $this->client + ->service() + ->adjustCreditBalance($this->credit->amount) + ->save(); + event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); return $this->credit; diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 934bd32418e2..e17378dee5d8 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -13,11 +13,13 @@ namespace Tests\Feature; use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; +use App\Factory\InvoiceItemFactory; use App\Models\Account; use App\Models\Client; use App\Models\ClientContact; use App\Models\Company; use App\Models\CompanyToken; +use App\Models\Credit; use App\Models\User; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; @@ -61,6 +63,72 @@ class ClientTest extends TestCase $this->makeTestData(); } + private function buildLineItems() + { + $line_items = []; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + + $line_items[] = $item; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + + $line_items[] = $item; + + return $line_items; + } + + public function testCreditBalance() + { + $this->client->credit_balance = 0; + $this->client->save(); + + $this->assertEquals(0, $this->client->credit_balance); + + $credit = [ + 'status_id' => 1, + '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() + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/credits/', $credit) + ->assertStatus(200); + + $arr = $response->json(); + + $credit_id = $arr['data']['id']; + + $credit = Credit::find($this->decodePrimaryKey($credit_id)); + + $this->assertNotNull($credit); + + $this->assertEquals(0, $credit->balance); + + $credit->service()->markSent()->save(); + + $this->assertEquals(20, $credit->balance); + $this->assertEquals(20, $credit->client->fresh()->credit_balance); + + } + public function testStoreClientUsingCountryCode() { $data = [