feat: variable interest loan amortization
Draft
JA
2mo ago+809+809+012345678901234567890123456789-9-9-0123456789
Summary
This PR implements comprehensive loan amortization support for both fixed and variable interest rate loans. Users can now generate detailed amortization schedules, track rate changes over time, and view accurate payment breakdowns directly in the app.
Changes
Backend - Models & Logic
- LoanAmortizationSchedule: Stores detailed payment schedule entries with:
- Payment number and date
- Beginning/ending balance
- Principal and interest breakdown
- Interest rate at time of payment
Database Migrations
create_loan_amortization_schedules- New table for amortization schedule entriesadd_rate_change_support_to_loans- Add variable rate tracking:rate_change_schedule(JSONB) - Maps effective dates to interest ratesnext_rate_change_date(Date) - Index for efficient querying
Loan Model Enhancements
-
generate_amortization_schedule() - Calculates full payment schedule
- Supports rate changes via
rate_for_date() - Handles variable rate recalculation for remaining balance
- Generates accurate principal/interest split for each payment
- Supports rate changes via
-
add_rate_change() - Add scheduled rate changes
- Updates rate_change_schedule
- Automatically regenerates schedule if it exists
-
rate_for_date() - Retrieves applicable rate for any date
-
monthly_payment - Now works for both fixed and variable rates
- Fixed: Standard amortization formula
- Variable: Returns current payment from schedule
Frontend - UI & Components
ViewComponent:
- Loans::AmortizationSchedule - Displays amortization schedule inline
- Shows first 12 payments in a responsive table
- Summary cards: total payments, principal, interest
- Generate/regenerate buttons for on-demand calculation
- Link to view full schedule
Controller:
- LoanAmortizationSchedulesController
index- Browse full amortization schedule with paginationcreate- Generate schedule with optional start date
Views:
-
Loan Overview Tab - Integrated schedule display
- Shows summary statistics
- First 12 payments in table format
- Quick action buttons
-
Full Schedule Page - Detailed view at
/accounts/:account_id/loan_amortization_schedules- Complete payment history
- Loan summary statistics
- Color-coded columns (principal: success, interest: warning)
- Pagination support
Internationalization:
- Added English locale strings for all UI elements
- Follows project i18n conventions
Routes
resources :accounts do
resources :loan_amortization_schedules, only: [:index, :create]
end
Tests
Comprehensive test coverage:
- Fixed rate amortization schedule generation
- Variable rate loans with rate change tracking
- Rate lookup by date
- Zero-interest loan calculations
- Edge cases (missing data, zero balances)
User Experience
For Fixed-Rate Loans:
- View loan overview with estimated monthly payment
- Click "Generate Schedule" to create amortization table
- See detailed breakdown of principal vs interest for each payment
- View total interest over loan lifetime
For Variable-Rate Loans:
- Set initial rate and rate change schedule
- Generate schedule - automatically applies rate changes
- View how payment adjustments affect remaining balance
- Track interest costs with rate fluctuations
Features
- ✅ Fixed-rate loan support (existing + enhanced)
- ✅ Variable interest rate tracking with schedule updates
- ✅ Automatic rate change handling
- ✅ Detailed amortization schedules with full pagination
- ✅ Payment breakdowns (principal vs interest)
- ✅ Schedule regeneration on rate changes
- ✅ Responsive table design with color coding
- ✅ Summary statistics (total interest, principal, payments)
- ✅ Full i18n support
Summary by CodeRabbit
- New Features
- Added amortization schedules for loans, including payment breakdowns, balances, interest, principal, and totals.
- Added controls to generate or regenerate schedules, with validation and optional start dates.
- Added paginated schedule views and navigation from loan overviews.
- Added support for variable-rate loans and payment calculations across rate changes.
- Bug Fixes
- Improved payment handling for missing loan details and zero-interest loans.