diff --git a/app/Jobs/Credit/ApplyCreditPayment.php b/app/Jobs/Credit/ApplyCreditPayment.php index 8c3f671391c9..398e5a7eae8d 100644 --- a/app/Jobs/Credit/ApplyCreditPayment.php +++ b/app/Jobs/Credit/ApplyCreditPayment.php @@ -85,6 +85,13 @@ class ApplyCreditPayment implements ShouldQueue ->save(); } + //22-08-2022 + $this->credit + ->client + ->service() + ->adjustCreditBalance($this->amount * -1) + ->save(); + /* Update Payment Applied Amount*/ $this->payment->save(); } diff --git a/app/Repositories/CreditRepository.php b/app/Repositories/CreditRepository.php index d9876e4ba28d..b506fda26b9b 100644 --- a/app/Repositories/CreditRepository.php +++ b/app/Repositories/CreditRepository.php @@ -43,4 +43,31 @@ class CreditRepository extends BaseRepository { 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; + } + } diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 1bdb4942949b..833c58ba4f81 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -166,7 +166,7 @@ class PaymentRepository extends BaseRepository { if ($credit) { $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))); return $payment; - //return parent::delete($payment); + } public function restore($payment) diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index 092f6ef98535..4a1c5e52d2f4 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -257,6 +257,29 @@ class CreditService 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. * @return Credit object diff --git a/tests/Unit/CreditBalanceTest.php b/tests/Unit/CreditBalanceTest.php index ea780f8d5fd9..322ce031a311 100644 --- a/tests/Unit/CreditBalanceTest.php +++ b/tests/Unit/CreditBalanceTest.php @@ -70,4 +70,54 @@ class CreditBalanceTest extends TestCase $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); + } }