diff --git a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php index 50d3cf4e601a..7b98e44c4134 100644 --- a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php +++ b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php @@ -16,7 +16,7 @@ use App\Models\User; use Illuminate\Contracts\Validation\Rule; /** - * Class NewUniqueUserRule + * Class PaymentAmountsBalanceRule * @package App\Http\ValidationRules */ class PaymentAmountsBalanceRule implements Rule @@ -42,6 +42,17 @@ class PaymentAmountsBalanceRule implements Rule private function calculateAmounts() :bool { + /** + * Sometimes the request may not contain the amount or it may be zero, + * and this is a valid use case, only compare the amounts if they + * have been presented! + */ + + if(!request()->has('amount')) + return true; + + if(request()->has('amount') && request()->input('amount') == 0) + return true $payment_amounts = 0; $invoice_amounts = 0; diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 9ffd07caafe3..e144b6745ec6 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -69,8 +69,8 @@ class PaymentRepository extends BaseRepository private function applyPayment(array $data, Payment $payment): ?Payment { - $payment->fill($data); + $payment->status_id = Payment::STATUS_COMPLETED; $payment->save(); @@ -127,9 +127,9 @@ class PaymentRepository extends BaseRepository $invoice_totals -= $credit_totals; if ($invoice_totals == $payment->amount) - $payment->applied = $payment->amount; + $payment->applied += $payment->amount; elseif ($invoice_totals < $payment->amount) - $payment->applied = $invoice_totals; + $payment->applied += $invoice_totals; //UpdateInvoicePayment::dispatchNow($payment); $payment->save(); diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 4b25663edf4c..f1ead2eff128 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -945,7 +945,11 @@ class PaymentTest extends TestCase } $response->assertStatus(200); - + + $arr = $response->json(); + + $this->assertEquals(20, $arr['data']['applied']); + }