# Deleted Loan Status Display Fix

## Issue Description

Deleted loans were showing as "Active" in the loan detail page, causing confusion. For example:
- Loan: LOAN-000105DELETED
- Status badge: "Active" (green)
- But also showing: "This loan has been moved to deleted loans"

## Root Cause

The system uses two separate fields to track loan state:

1. **`status` field**: Tracks the loan lifecycle
   - Values: 'active', 'paid', 'defaulted', 'rolled_over', 'written_off'
   - Represents the business state of the loan

2. **`is_deleted` field**: Tracks soft deletion
   - Values: True/False
   - Represents whether the loan has been archived/deleted

### The Problem

When a loan is soft-deleted:
- ✅ `is_deleted` is set to `True`
- ✅ `deleted_at` timestamp is recorded
- ✅ `deleted_by` user is recorded
- ❌ BUT `status` remains unchanged (e.g., 'active')

The loan_detail.html template was showing the status badge based ONLY on the `status` field, ignoring the `is_deleted` flag. This caused deleted loans to display as "Active" even though they were deleted.

## The Fix

Updated `templates/loans/loan_detail.html` to:

1. **Check `is_deleted` first** before showing status badge
2. **Show "DELETED" badge** (red) when `is_deleted=True`
3. **Hide the status badge** when loan is deleted (since it's misleading)
4. **Add "DELETED" suffix** to the loan number in the title for clarity

### Before:
```html
<h1>Loan LOAN-000105</h1>
<span class="bg-gray-100">DELETED</span>
<span class="bg-green-100">Active</span>  <!-- Confusing! -->
```

### After:
```html
<h1>Loan LOAN-000105DELETED</h1>
<span class="bg-red-100">🗑️ DELETED</span>  <!-- Only show deleted badge -->
```

## Why This Design is Correct

The separation of `status` and `is_deleted` is intentional and correct:

- **`status`**: Business logic - tracks the loan's lifecycle state
  - A deleted loan can be 'active', 'paid', 'defaulted', etc.
  - This preserves the loan's state before deletion
  - Useful for reporting and audit trails

- **`is_deleted`**: System logic - tracks soft deletion
  - Used for filtering (exclude from main lists)
  - Used for UI display (show deletion warnings)
  - Used for permissions (allow restore/permanent delete)

## Files Changed

1. **templates/loans/loan_detail.html**
   - Updated status badge display logic
   - Now checks `is_deleted` before showing status
   - Shows red "DELETED" badge for deleted loans
   - Adds "DELETED" suffix to loan number in title

## Testing

Run the diagnostic script to verify:
```bash
python diagnose_deleted_loan_issue.py
```

Expected output:
- Shows all deleted loans (is_deleted=True)
- Shows any inconsistencies
- Confirms UI fix is working

## Related Files

- `loans/models.py` - Loan model with `is_deleted` and `status` fields
- `loans/views.py` - `delete_loan()` view that sets `is_deleted=True`
- `templates/loans/deleted_loans.html` - Lists deleted loans
- `templates/loans/loan_detail.html` - Shows individual loan details

## Future Improvements

Consider adding a custom property to the Loan model:

```python
@property
def display_status(self):
    """Get the display status considering deletion state"""
    if self.is_deleted:
        return 'Deleted'
    return self.get_status_display()
```

This would centralize the logic and make templates simpler.
