# Complete Rollover Fix - Deployment Package

## Problem Summary
1. ❌ Rollover modal missing date field
2. ❌ Rollover overwrites original disbursed amount
3. ❌ Disbursement reports show rolled-over amounts instead of original

## Solution

### Part 1: Database Migration (Run on Server)
Upload and run: `implement_original_disbursed_tracking.py`

This adds `original_disbursed_amount` field and sets Faith's to KES 200,000.

### Part 2: Update Loan Model
File: `loans/models.py`

Add field after line with `principal_amount`:
```python
original_disbursed_amount = models.DecimalField(
    max_digits=15,
    decimal_places=2,
    null=True,
    blank=True,
    help_text="Original amount disbursed - never changes"
)
```

### Part 3: Fix Rollover to Preserve Original Amount
File: `loans/models.py`, in `RolloverRequest.approve()` method (around line 1780)

Add BEFORE any loan updates:
```python
# Preserve original disbursed amount
if not self.loan.original_disbursed_amount:
    self.loan.original_disbursed_amount = self.loan.principal_amount
```

Make sure these lines DON'T update original_disbursed_amount:
```python
# Update loan amounts - but DON'T touch original_disbursed_amount!
self.loan.principal_amount = self.requested_amount
self.loan.interest_amount = new_interest
self.loan.total_amount = self.loan.principal_amount + new_interest
# original_disbursed_amount stays unchanged!
```

### Part 4: Add Date Field to Rollover Modal
See attached: `rollover_modal_with_date.html` (partial)

### Part 5: Update Disbursement Report
File: `reports/views.py`, in `disbursed_loans_report()` function

Change line ~7370:
```python
'principal_amount': loan.original_disbursed_amount or loan.principal_amount,
```

Change line ~7400 (summary totals):
```python
total_principal += (loan.original_disbursed_amount or loan.principal_amount)
```

## Files to Deploy
1. ✅ `implement_original_disbursed_tracking.py` - Run on server
2. ✅ `loans/models.py` - Updated model
3. ✅ `loans/views.py` - Rollover date handling
4. ✅ `reports/views.py` - Use original amount
5. ✅ `templates/loans/loan_detail.html` - Rollover modal with date
6. ✅ `templates/loans/loan_detail.html` - Record Payment button fix
7. ✅ `templates/loans/loans_table.html` - Record Payment button fix

## Testing Checklist
- [ ] Run database migration
- [ ] Create new loan KES 100,000
- [ ] Check original_disbursed_amount = 100,000
- [ ] Make payment reducing to KES 80,000
- [ ] Rollover the loan
- [ ] Verify original_disbursed_amount STILL = 100,000 (not 80,000)
- [ ] Check disbursement report shows 100,000
- [ ] Verify Record Payment button appears on rolled loans
