fixes for refunding unapplied payments

This commit is contained in:
David Bomba 2021-10-27 05:01:10 +11:00
parent faf77d8bd9
commit 7e23de8e5a
6 changed files with 126 additions and 6 deletions

View File

@ -683,8 +683,6 @@ class PaymentController extends BaseController
{
$payment = $request->payment();
// nlog($request->all());
$payment = $payment->refund($request->all());
return $this->itemResponse($payment);

View File

@ -51,7 +51,7 @@ class PaymentAppliedValidAmount implements Rule
$payment_amounts = 0;
$invoice_amounts = 0;
$payment_amounts = $payment->amount - $payment->applied;
$payment_amounts = $payment->amount - $payment->refunded - $payment->applied;
if (request()->input('credits') && is_array(request()->input('credits'))) {
foreach (request()->input('credits') as $credit) {

View File

@ -122,6 +122,8 @@ class DeletePayment
}
else {
/* If there are no invoices - then we need to still adjust the total client->paid_to_date amount*/
$this->payment
->client
->service()

View File

@ -267,9 +267,17 @@ class RefundPayment
// $this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string);
$client = $this->payment->client->fresh();
//$client->service()->updatePaidToDate(-1 * $this->total_refund)->save();
$client->service()->updatePaidToDate(-1 * $refunded_invoice['amount'])->save();
}
else{
//if we are refunding and no payments have been tagged, then we need to decrement the client->paid_to_date by the total refund amount.
$client = $this->payment->client->fresh();
$client->service()->updatePaidToDate(-1 * $this->total_refund)->save();
}
return $this;
}

View File

@ -48,7 +48,6 @@ class UnappliedPaymentDeleteTest extends TestCase
public function testUnappliedPaymentDelete()
{
$data = [
'amount' => 1000,
'client_id' => $this->client->hashed_id,
@ -74,9 +73,12 @@ class UnappliedPaymentDeleteTest extends TestCase
$arr = $response->json();
$response->assertStatus(200);
$this->assertEquals(0, $this->client->paid_to_date);
$payment_id = $arr['data']['id'];
$payment = Payment::with('client')->find($this->decodePrimaryKey($payment_id));
$this->assertEquals(1000, $payment->amount);
$this->assertEquals(1000, $payment->client->paid_to_date);
try {
$response = $this->withHeaders([

View File

@ -0,0 +1,110 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\Payments;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutEvents;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\MockUnitData;
use Tests\TestCase;
/**
* @test
*/
class UnappliedPaymentRefundTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockUnitData;
use WithoutEvents;
public function setUp() :void
{
parent::setUp();
$this->faker = \Faker\Factory::create();
$this->makeTestData();
$this->withoutExceptionHandling();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testUnappliedPaymentRefund()
{
$data = [
'amount' => 1000,
'client_id' => $this->client->hashed_id,
'invoices' => [
],
'date' => '2020/12/12',
];
$response = null;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
if ($response){
$arr = $response->json();
$response->assertStatus(200);
$this->assertEquals(1000, $this->client->fresh()->paid_to_date);
$payment_id = $arr['data']['id'];
$this->assertEquals(1000, $arr['data']['amount']);
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$data = [
'id' => $this->encodePrimaryKey($payment->id),
'amount' => 500,
'date' => '2020/12/12',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/refund', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
}
$response->assertStatus(200);
$this->assertEquals(500, $this->client->fresh()->paid_to_date);
}
}
}