Class: Dscf::Credit::RepaymentService
- Inherits:
-
Object
- Object
- Dscf::Credit::RepaymentService
- Defined in:
- app/services/dscf/credit/repayment_service.rb
Overview
RepaymentService handles loan repayment processing with automatic payment allocation across facilitation fees, penalties, interest, and principal.
Instance Attribute Summary collapse
-
#loan ⇒ Object
readonly
Returns the value of attribute loan.
-
#payment_amount ⇒ Object
readonly
Returns the value of attribute payment_amount.
Instance Method Summary collapse
-
#initialize(loan, payment_amount) ⇒ RepaymentService
constructor
Initialize the repayment service.
-
#process_repayment ⇒ Hash
Process loan repayment with automatic allocation and status updates.
Constructor Details
#initialize(loan, payment_amount) ⇒ RepaymentService
Initialize the repayment service
25 26 27 28 |
# File 'app/services/dscf/credit/repayment_service.rb', line 25 def initialize(loan, payment_amount) @loan = loan @payment_amount = payment_amount.to_f end |
Instance Attribute Details
#loan ⇒ Object (readonly)
Returns the value of attribute loan.
19 20 21 |
# File 'app/services/dscf/credit/repayment_service.rb', line 19 def loan @loan end |
#payment_amount ⇒ Object (readonly)
Returns the value of attribute payment_amount.
19 20 21 |
# File 'app/services/dscf/credit/repayment_service.rb', line 19 def payment_amount @payment_amount end |
Instance Method Details
#process_repayment ⇒ Hash
Process loan repayment with automatic allocation and status updates
Payment allocation priority (waterfall method):
-
Facilitation Fee (from loan_accruals where accrual_type=‘facilitation_fee’ and status=‘pending’)
-
Penalties (from loan_accruals where accrual_type=‘penalty’ and status=‘pending’)
-
Interest (from loan_accruals where accrual_type=‘interest’ and status=‘pending’)
-
Principal (from loan.remaining_amount)
Process steps:
-
Calculate payment allocation across loan components
-
Update loan record (remaining_amount only)
-
Mark accruals as paid (with splitting for partial payments)
-
Update loan status based on remaining balance
-
Reactivate credit facilities if loan fully paid
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/services/dscf/credit/repayment_service.rb', line 72 def process_repayment return error_result("Loan not found") unless loan return error_result("Invalid payment amount") unless payment_amount > 0 return error_result("Loan is not active") unless loan.status.in?([ "active", "overdue", "disbursed" ]) payment_allocation = calculate_payment_allocation return error_result("Payment amount exceeds total outstanding balance") if payment_amount > payment_allocation[:total_outstanding] ActiveRecord::Base.transaction do process_payment_allocation(payment_allocation) update_loan_status reactivate_facilities_if_paid_off loan_transaction = create_repayment_transaction(payment_allocation) success_result(payment_allocation, loan_transaction) end rescue StandardError => e error_result("Repayment processing failed: #{e.}") end |