1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00

Tests for credit balance

This commit is contained in:
= 2022-08-22 10:27:11 +10:00
parent 59e3ab9993
commit b5f61d22a7
5 changed files with 109 additions and 2 deletions

View File

@ -85,6 +85,13 @@ class ApplyCreditPayment implements ShouldQueue
->save(); ->save();
} }
//22-08-2022
$this->credit
->client
->service()
->adjustCreditBalance($this->amount * -1)
->save();
/* Update Payment Applied Amount*/ /* Update Payment Applied Amount*/
$this->payment->save(); $this->payment->save();
} }

View File

@ -43,4 +43,31 @@ class CreditRepository extends BaseRepository
{ {
return CreditInvitation::where('key', $key)->first(); return CreditInvitation::where('key', $key)->first();
} }
public function delete($credit)
{
if ($credit->is_deleted) {
return;
}
$credit = $credit->service()->deleteCredit()->save();
return parent::restore($credit);
}
public function restore($credit)
{
//we cannot restore a deleted payment.
if ($credit->is_deleted) {
return;
}
parent::restore($credit);
$credit = $credit->service()->restoreCredit()->save();
return $credit;
}
} }

View File

@ -166,7 +166,7 @@ class PaymentRepository extends BaseRepository {
if ($credit) { if ($credit) {
$credit = $credit->service()->markSent()->save(); $credit = $credit->service()->markSent()->save();
ApplyCreditPayment::dispatchNow($credit, $payment, $paid_credit['amount'], $credit->company); (new ApplyCreditPayment($credit, $payment, $paid_credit['amount'], $credit->company))->handle();
} }
} }
} }
@ -245,7 +245,7 @@ class PaymentRepository extends BaseRepository {
event(new PaymentWasDeleted($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new PaymentWasDeleted($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
return $payment; return $payment;
//return parent::delete($payment);
} }
public function restore($payment) public function restore($payment)

View File

@ -257,6 +257,29 @@ class CreditService
return $this; return $this;
} }
public function deleteCredit()
{
$this->credit
->client
->service()
->adjustCreditBalance($this->credit->balance * -1)
->save();
return $this;
}
public function restoreCredit()
{
$this->credit
->client
->service()
->adjustCreditBalance($this->credit->balance)
->save();
return $this;
}
/** /**
* Saves the credit. * Saves the credit.
* @return Credit object * @return Credit object

View File

@ -70,4 +70,54 @@ class CreditBalanceTest extends TestCase
$this->assertEquals($this->client->service()->getCreditBalance(), 0); $this->assertEquals($this->client->service()->getCreditBalance(), 0);
} }
public function testCreditDeleteCheckClientBalance()
{
$credit = Credit::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'balance' => 10,
'number' => 'testing-number-01',
'status_id' => Credit::STATUS_SENT,
]);
$credit->client->credit_balance = 10;
$credit->push();
//delete invoice
$data = [
'ids' => [$credit->hashed_id],
];
//restore invoice
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=delete', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(0, $client->credit_balance);
//restore invoice
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=restore', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(10, $client->credit_balance);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits/bulk?action=archive', $data)->assertStatus(200);
$client = $credit->client->fresh();
$this->assertEquals(10, $client->credit_balance);
}
} }