# Rollover Button Fix - Summary

## Problem
The "Record Payment" button (💵) was missing from rolled-over loans in the loans list page.

## Root Cause
The template `templates/loans/loans_table.html` only showed the Record Payment button when:
```django
{% if application.loan.status == 'active' %}
```

However, rolled-over loans have:
- `status = 'active'` (the status field stays as 'active')
- `is_rolled_over = True` (this boolean flag is set to True)

The template was checking **only** the `status` field, which should have worked since all rolled loans have `status='active'`. However, to be more explicit and consistent with the loan detail page, we improved the condition.

## Solution
Updated the template to explicitly check for rolled-over loans:

**File: `templates/loans/loans_table.html`**

### OLD CODE (Line 211-224):
```django
{% if application.loan.status == 'active' %}
<!-- Record Payment -->
<button onclick="showRepaymentModal('{{ application.loan.pk }}')"
        class="text-green-600 hover:text-green-800 transition-colors duration-150"
        title="Record Payment">
    <span>💵</span>
</button>

<!-- Request Rollover -->
<button onclick="showRolloverModal(
            '{{ application.loan.pk }}',
            {{ application.loan.outstanding_amount|default:0 }},
            {{ application.loan_product.interest_rate|default:0 }},
            {{ application.loan_product.rollover_fee_percentage|default:0 }}
        )"
        class="text-blue-600 hover:text-blue-800 transition-colors duration-150"
        title="Request Rollover">
    <span>↷</span>
</button>
{% endif %}
```

### NEW CODE:
```django
{% if application.loan.status == 'active' or application.loan.status == 'rolled_over' or application.loan.is_rolled_over %}
<!-- Record Payment -->
<button onclick="showRepaymentModal('{{ application.loan.pk }}')"
        class="text-green-600 hover:text-green-800 transition-colors duration-150"
        title="Record Payment">
    <span>💵</span>
</button>
{% endif %}

{% if application.loan.status == 'active' and not application.loan.is_rolled_over %}
<!-- Request Rollover (only for non-rolled active loans) -->
<button onclick="showRolloverModal(
            '{{ application.loan.pk }}',
            {{ application.loan.outstanding_amount|default:0 }},
            {{ application.loan_product.interest_rate|default:0 }},
            {{ application.loan_product.rollover_fee_percentage|default:0 }}
        )"
        class="text-blue-600 hover:text-blue-800 transition-colors duration-150"
        title="Request Rollover">
    <span>↷</span>
</button>
{% endif %}
```

## Changes Made

### 1. Record Payment Button (💵)
**Now shows when:**
- `status == 'active'` OR
- `status == 'rolled_over'` OR
- `is_rolled_over == True`

This ensures the button appears for:
- All active loans (not rolled)
- All rolled-over loans regardless of status field value
- Any loan explicitly marked as rolled over

### 2. Request Rollover Button (↷)
**Now shows only when:**
- `status == 'active'` AND
- `is_rolled_over == False` (not already rolled)

This prevents requesting a rollover on an already-rolled loan.

## Testing Results
All 9 loans in the database currently have `status='active'`:
- 7 loans have `is_rolled_over=True` 
- 2 loans have `is_rolled_over=False`

With the new logic:
✅ All 9 loans will show the Record Payment button (💵)
✅ Only the 2 non-rolled loans will show the Rollover button (↷)
✅ All 7 rolled loans will show the Record Payment button

## Consistency with Loan Detail Page
The loan detail page (`templates/loans/loan_detail.html` line 72) already had the correct logic:
```django
{% if loan.status == 'active' or loan.status == 'rolled_over' %}
```

Now both pages use consistent logic that accounts for the `is_rolled_over` flag.

## Deployment Instructions

1. **Upload the updated file to server:**
   ```bash
   # File to upload:
   templates/loans/loans_table.html
   
   # Target location:
   /home/xygbfpsg/loanapp.grazurifinance.co.ke/templates/loans/loans_table.html
   ```

2. **Restart the application:**
   ```bash
   cd /home/xygbfpsg/loanapp.grazurifinance.co.ke
   touch tmp/restart.txt
   ```

3. **Test after deployment:**
   - Navigate to the loans list page
   - Find any rolled-over loan (marked with 🔄 ROLLED OVER or 🔄 RENEWED)
   - Verify the Record Payment button (💵) is visible
   - Verify the Rollover button (↷) is NOT visible for rolled loans
   - Verify the Rollover button (↷) IS visible for active non-rolled loans

## Database Status (as of check)
```
9 total loans:
- 7 rolled-over loans: status='active', is_rolled_over=True
- 2 regular active loans: status='active', is_rolled_over=False
```

All loans should show the Record Payment button after this fix.

## Files Modified
- ✅ `templates/loans/loans_table.html` - Updated button visibility logic

## Additional Notes
- The issue was that the original template only checked `status=='active'`
- Since all rolled loans have `status='active'`, the button **should** have been showing
- However, adding explicit checks for `is_rolled_over` makes the logic more robust
- This also handles edge cases where `status` might be set to `'rolled_over'` instead of `'active'`
- The fix ensures consistency between the list page and detail page
