diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index e0ab42bdc038..b612c6388aa5 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -683,8 +683,6 @@ class PaymentController extends BaseController { $payment = $request->payment(); -// nlog($request->all()); - $payment = $payment->refund($request->all()); return $this->itemResponse($payment); diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index e3a6e3c7dfa5..292712a1d738 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -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) { diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 9e531e19b371..626327f803a7 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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() diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 2b44eae951e8..0dfa6fa355eb 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -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; } diff --git a/tests/Feature/Payments/UnappliedPaymentDeleteTest.php b/tests/Feature/Payments/UnappliedPaymentDeleteTest.php index 91794770cd63..bc93dcfa51d5 100644 --- a/tests/Feature/Payments/UnappliedPaymentDeleteTest.php +++ b/tests/Feature/Payments/UnappliedPaymentDeleteTest.php @@ -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([ diff --git a/tests/Feature/Payments/UnappliedPaymentRefundTest.php b/tests/Feature/Payments/UnappliedPaymentRefundTest.php new file mode 100644 index 000000000000..bd4cceb96e5b --- /dev/null +++ b/tests/Feature/Payments/UnappliedPaymentRefundTest.php @@ -0,0 +1,110 @@ +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); + + } + + + + } + +} \ No newline at end of file