# Rollover Interest Rate → Amount Change Guide

## Overview
Changed the rollover request form from accepting interest as a **percentage (%)** to accepting interest as an **actual amount in KES**.

## Changes Made

### 1. Database Changes
- Changed field `requested_interest_rate` from `DECIMAL(5,2)` to `DECIMAL(12,2)`
- Updated help text from "Custom interest rate %" to "Custom interest amount in KES"
- Migration file: `loans/migrations/0031_change_interest_to_amount.py`

### 2. Model Changes (`loans/models.py`)
- **RolloverRequest.requested_interest_rate**: Now stores amount in KES instead of percentage
- **RolloverRequest.approve()**: Changed calculation logic:
  ```python
  # OLD (percentage-based):
  interest_amount = principal_amount * (new_interest_rate / Decimal('100')) * Decimal(str(months))
  
  # NEW (amount-based):
  interest_amount = new_interest_rate  # Direct amount in KES
  ```

### 3. View Changes (`loans/views.py`)
- **request_rollover()**: Updated validation to accept amounts (no max limit of 999.99%)
  - Removed percentage range validation (0-999.99%)
  - Now only checks that amount is not negative
  - Field can accept any positive decimal value

### 4. Template Changes
- **templates/loans/loan_detail.html**:
  - Label changed: "Custom Interest Rate (%)" → "Custom Interest Amount (KES)"
  - Removed `max="999.99"` attribute from input field
  
- **templates/loans/loans.html**:
  - Same changes as loan_detail.html
  
- **templates/loans/rollovers.html**:
  - Display changed: `{{ rollover.requested_interest_rate }}%` → `KES {{ rollover.requested_interest_rate|floatformat:2|intcomma }}`
  - Help text: "Default interest rate" → "Default interest amount"

## Deployment Steps

### For Production (cPanel/SSH)

1. **Upload changed files to server:**
   ```bash
   # Upload these files:
   - loans/models.py
   - loans/views.py
   - loans/migrations/0031_change_interest_to_amount.py
   - templates/loans/loan_detail.html
   - templates/loans/loans.html
   - templates/loans/rollovers.html
   ```

2. **Run the deployment script:**
   ```bash
   cd ~/public_html  # or your project directory
   python deploy_interest_amount_change.py
   ```

3. **Or manually run migration:**
   ```bash
   python manage.py migrate loans 0031
   ```

4. **Restart the Django application:**
   - Through cPanel: Application → Python Apps → Restart
   - Or via command line: `touch tmp/restart.txt`

5. **Clear any cached templates (if using caching):**
   ```bash
   python manage.py clear_cache  # if you have this command
   ```

## Testing After Deployment

1. **Create a new rollover request:**
   - Go to any active loan
   - Click "Request Rollover"
   - Enter a custom interest **amount** (e.g., 5000 for KES 5,000)
   - Submit the request
   - Verify the request is created successfully

2. **View rollover requests list:**
   - Go to Loans → Rollovers
   - Verify that custom interest amounts display as "KES X,XXX.XX" (not "X%")

3. **Approve a rollover:**
   - Approve a rollover request with custom interest
   - Verify the new loan is created with correct interest amount
   - Check that the interest amount on the new loan matches what was entered

## Important Notes

### For Existing Pending Rollovers
⚠️ **If you have pending rollover requests before this change:**
- Those requests have interest values stored as percentages (e.g., 8.5 means 8.5%)
- After the migration, the system will treat those values as amounts in KES
- **Recommendation**: 
  - Review and reject any old pending rollovers before deployment
  - OR manually convert them: If a request has `8.5` (meant 8.5%), calculate the actual amount and update the database directly

### Backward Compatibility
- ❌ **Not backward compatible** with old percentage values
- Old values stored as percentages will be interpreted as amounts
- Example: `10` (10%) would be read as KES 10 (not 10% interest)

### Database Query Example
To check if there are pending rollovers that need attention:
```sql
SELECT id, loan_id, requested_amount, requested_interest_rate, status, created_at
FROM loans_rolloverrequest
WHERE status = 'pending' AND requested_interest_rate IS NOT NULL;
```

## Examples

### Before (Percentage)
- Form: "Custom Interest Rate (%): 8.5"
- Stored: `8.5`
- Calculation: `principal × 8.5 / 100 × months`
- For KES 100,000 over 2 months: `100000 × 8.5 / 100 × 2 = KES 17,000`

### After (Amount)
- Form: "Custom Interest Amount (KES): 17000"
- Stored: `17000.00`
- Calculation: Direct amount
- For any loan: Interest is exactly `KES 17,000`

## Rollback Plan (If Needed)

If you need to rollback:

1. Revert the migration:
   ```bash
   python manage.py migrate loans 0030
   ```

2. Restore old files from backup:
   - loans/models.py
   - loans/views.py
   - All template files

3. Restart Django application

## Questions or Issues?

- Check Django logs for migration errors
- Verify database column changed: 
  ```sql
  DESCRIBE loans_rolloverrequest;
  -- Check that requested_interest_rate is DECIMAL(12,2)
  ```
- Test with small amounts first before going live
