# Rollover Report Outstanding Column Fix

## Issue
The Outstanding column in the rollover report was showing the **same value** for all rollover entries of the same loan, instead of showing the outstanding amount **at each specific rollover time**.

### Example (Loan 25112026-000004):
**BEFORE FIX:**
```
Rollover #1: Principal 76,200 + Interest 300 = Total 76,500 | Amount Paid: 50,000 | Outstanding: 76,500 (same! ❌)
Rollover #2: Principal 76,000 + Interest 200 = Total 76,200 | Amount Paid: 50,000 | Outstanding: 76,500 (same! ❌)
Rollover #3: Principal 75,000 + Interest 1,000 = Total 76,000 | Amount Paid: 50,000 | Outstanding: 76,500 (same! ❌)
```

All three showed 76,500 outstanding because it was pulling from `loan.outstanding_amount` (the current loan state).

**AFTER FIX:**
```
Rollover #1: Principal 76,200 + Interest 300 = Total 76,500 | Amount Paid: 50,000 | Outstanding: 76,500 ✅
Rollover #2: Principal 76,000 + Interest 200 = Total 76,200 | Amount Paid: 50,000 | Outstanding: 76,200 ✅
Rollover #3: Principal 75,000 + Interest 1,000 = Total 76,000 | Amount Paid: 50,000 | Outstanding: 76,000 ✅
```

Each rollover now shows its own outstanding amount = Total Owed at that rollover moment.

## Column Meanings

| Column | Shows |
|--------|-------|
| **Total Owed** | What was owed at THAT SPECIFIC rollover time (Principal + Interest) |
| **Amount Paid** | Current payments made on the loan (reflects current state) |
| **Outstanding** | What was owed at THAT SPECIFIC rollover time (equals Total Owed) |

The Outstanding column now varies per rollover entry (showing what was owed at that moment), while Amount Paid shows current payment status (same across all entries for one loan).

## Root Cause
The view was using:
```python
'outstanding': loan.outstanding_amount,  # Current loan state - WRONG!
```

This pulled the **current** loan's outstanding balance for all rollover entries, making them all show the same value.

## Solution
Changed to calculate outstanding at the time of each rollover:
```python
outstanding_at_rollover = new_total  # Outstanding = what was owed at rollover time
'outstanding': outstanding_at_rollover,
```

## Files Modified

### loans/views.py (Lines ~3858-3879)

**BEFORE:**
```python
entry = {
    'rollover_request': rr,
    'loan': loan,
    'borrower': loan.borrower,
    'product': loan.application.loan_product if loan.application else None,
    'original_disbursement': loan.disbursement_date,
    'original_principal': original_principal,
    'new_principal': new_principal,
    'interest_added': new_interest,
    'total_owed': new_total,
    'amount_paid': loan.amount_paid,          # ❌ Current loan state
    'outstanding': loan.outstanding_amount,    # ❌ Current loan state
    'renewed_on': rr.reviewed_at,
    'renewed_by': rr.reviewed_by,
}

# Accumulate totals
total_paid += loan.amount_paid
total_outstanding += loan.outstanding_amount
```

**AFTER:**
```python
# At rollover time, outstanding equals the total owed
# But Amount Paid should show current payments (how much has been paid on this loan NOW)
outstanding_at_rollover = new_total

entry = {
    'rollover_request': rr,
    'loan': loan,
    'borrower': loan.borrower,
    'product': loan.application.loan_product if loan.application else None,
    'original_disbursement': loan.disbursement_date,
    'original_principal': original_principal,
    'new_principal': new_principal,
    'interest_added': new_interest,
    'total_owed': new_total,
    'amount_paid': loan.amount_paid,          # ✅ Current amount paid on the loan
    'outstanding': outstanding_at_rollover,    # ✅ Outstanding = total owed at rollover time
    'renewed_on': rr.reviewed_at,
    'renewed_by': rr.reviewed_by,
}

# Accumulate totals
total_paid += loan.amount_paid                # ✅ Current amount paid
total_outstanding += outstanding_at_rollover  # ✅ Outstanding at rollover time
```

## Testing

### Local Test Results (test_outstanding_fix.py)
```
Loan: 19112026-000004 (5 rollovers)

Rollover   Date         Principal       Interest     Total Owed      OLD Outstanding    NEW Outstanding
#1         2026-11-19   KES  80,000.00  KES    10.00  KES  80,010.00  KES     43,133.01  KES     80,010.00  ✅
#2         2026-11-25   KES  59,510.00  KES     0.00  KES  59,510.00  KES     43,133.01  KES     59,510.00  ✅
#3         2026-11-25   KES  56,863.00  KES     0.00  KES  56,863.00  KES     43,133.01  KES     56,863.00  ✅
#4         2026-11-25   KES  53,421.90  KES     0.00  KES  53,421.90  KES     43,133.01  KES     53,421.90  ✅
#5         2026-11-25   KES  48,948.47  KES     0.00  KES  48,948.47  KES     43,133.01  KES     48,948.47  ✅
```

**OLD:** All showed 43,133.01 (current loan state) ❌  
**NEW:** Each shows its own outstanding amount ✅

## Expected Production Result

For loan **25112026-000004**, the report should now show (assuming current amount paid is 50,000):

| Rollover | Principal | Interest | Total Owed | Amount Paid | Outstanding |
|----------|-----------|----------|------------|-------------|-------------|
| #1 | 76,200 | 300 | 76,500 | 50,000 | **76,500** |
| #2 | 76,000 | 200 | 76,200 | 50,000 | **76,200** |
| #3 | 75,000 | 1,000 | 76,000 | 50,000 | **76,000** |

- Each row has its own correct **Outstanding** amount (what was owed at that rollover)
- **Amount Paid** is the same across entries (current payment status)
- **Outstanding** values differ per rollover (historical snapshot of what was owed)

## Notes

### Why Amount Paid is the same?
Amount Paid shows the **current** payments made on the loan across all its rollovers. This is useful because:
- You can see how much has been paid overall
- Combined with Outstanding, you see both historical rollover amounts and current payment progress

### Why Outstanding varies?
Outstanding shows what was owed **at that specific rollover time**. Each rollover creates a new amount owed (Principal + Interest), so each entry shows its own Outstanding value.

### Report Purpose
The rollover report now shows:
- **Historical rollover data**: What was rolled over at each point (Principal + Interest = Total Owed = Outstanding)
- **Current payment status**: How much has been paid on the loan overall (Amount Paid)
- **Comparison**: You can compare multiple rollovers and see payment progress
