# Rollover Loan Exclusion Fix - Deployment Checklist

## Pre-Deployment

### 1. Backup Current Files ✓
- [ ] Download `reports/simple_reports_service.py` → save as `.backup`
- [ ] Download `reports/comprehensive_reports.py` → save as `.backup`
- [ ] Download `reports/views.py` → save as `.backup`

### 2. Verify Issue Exists ✓
- [ ] Go to `/reports/loans-due/enhanced/`
- [ ] Check if rolled-over loans appear (e.g., LOAN-000128)
- [ ] Note the current count of "loans due"

### 3. Check Database State ✓
Run in phpMyAdmin:
```sql
-- Count rolled-over loans
SELECT COUNT(*) FROM loans 
WHERE (is_rolled_over = 1 OR status = 'rolled_over');
```
- [ ] Note the count: _______

## Deployment

### 4. Upload Modified Files ✓
- [ ] Upload `reports/simple_reports_service.py`
- [ ] Upload `reports/comprehensive_reports.py`
- [ ] Upload `reports/views.py`
- [ ] Verify files uploaded successfully (check file sizes/dates)

### 5. Restart Application ✓
- [ ] Create/touch `tmp/restart.txt` file
- [ ] Wait 30 seconds for application to restart
- [ ] Check application is running (visit homepage)

### 6. Clear Cache (Optional) ✓
If using Django cache:
- [ ] Run `python manage.py shell`
- [ ] Execute: `from django.core.cache import cache; cache.clear()`

## Post-Deployment Verification

### 7. Verify Reports ✓

#### Dashboard
- [ ] Go to main dashboard
- [ ] Check "Total Active Loans" count
- [ ] Check "Loans Due Today" widget
- [ ] Verify counts exclude rolled-over loans

#### Loans Due Enhanced Report
- [ ] Go to `/reports/loans-due/enhanced/`
- [ ] Search for LOAN-000128 (or known rolled-over loan)
- [ ] Verify it does NOT appear
- [ ] Check total count is correct

#### Other Reports
- [ ] Check `/reports/delinquent-loans/`
- [ ] Check `/reports/loans-in-arrears/`
- [ ] Check `/reports/portfolio-details/`
- [ ] Verify no rolled-over loans appear

### 8. SQL Verification ✓
Run in phpMyAdmin:
```sql
-- This should match the report counts
SELECT COUNT(*) FROM loans 
WHERE status = 'active' 
  AND is_deleted = 0 
  AND is_rolled_over = 0;
```
- [ ] Count matches dashboard: _______

### 9. Test Specific Loan ✓
For LOAN-000128 (or your rolled-over loan):
```sql
SELECT loan_number, status, is_rolled_over, is_deleted
FROM loans 
WHERE loan_number = 'LOAN-000128';
```
- [ ] Verify: `is_rolled_over = 1` OR `status = 'rolled_over'`
- [ ] Confirm it doesn't appear in any report

### 10. Check for Errors ✓
- [ ] Check application error logs
- [ ] Check for any 500 errors
- [ ] Test report exports (PDF/Excel)
- [ ] Verify all report pages load correctly

## Data Consistency (Optional)

### 11. Fix Inconsistent States ✓
If you find loans with inconsistent rollover states:
- [ ] Run `python fix_inconsistent_rollover_states.py`
- [ ] Review and fix any inconsistencies
- [ ] Re-verify reports after fixing

## Rollback (If Needed)

### 12. Rollback Procedure ✓
If issues occur:
- [ ] Upload backup files
- [ ] Restart application: `touch tmp/restart.txt`
- [ ] Verify application is working
- [ ] Document the issue

## Sign-Off

### Deployment Completed By
- Name: _______________________
- Date: _______________________
- Time: _______________________

### Verification Completed By
- Name: _______________________
- Date: _______________________
- Time: _______________________

### Issues Encountered
- [ ] None
- [ ] Issues (describe below):

_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

### Final Status
- [ ] ✅ Deployment successful - all tests passed
- [ ] ⚠️ Deployment successful - minor issues noted
- [ ] ❌ Deployment failed - rolled back

## Notes
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
