diff --git a/app/Ninja/Repositories/PaymentRepository.php b/app/Ninja/Repositories/PaymentRepository.php index f75f9a1867..1e3ac8a930 100644 --- a/app/Ninja/Repositories/PaymentRepository.php +++ b/app/Ninja/Repositories/PaymentRepository.php @@ -180,12 +180,11 @@ class PaymentRepository extends BaseRepository if ($paymentTypeId == PAYMENT_TYPE_CREDIT) { $credits = Credit::scope()->where('client_id', '=', $clientId) ->where('balance', '>', 0)->orderBy('created_at')->get(); - $applied = 0; + $remaining = $amount; foreach ($credits as $credit) { - $applied += $credit->apply($amount); - - if ($applied >= $amount) { + $remaining -= $credit->apply($remaining); + if ( ! $remaining) { break; } }