Adjust credit balance on client record

This commit is contained in:
= 2022-08-22 08:24:36 +10:00
parent 77d489211e
commit 5070b2745e
3 changed files with 74 additions and 0 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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 = [