# UUID JSON Serialization Fix

## Issue
TypeError: Object of type UUID is not JSON serializable

**Error Location:** `/reports/loans-due/enhanced/?today_only=true`

## Root Cause
Multiple models use `UUIDField` as primary keys:
- `Loan` model: `id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)`
- `LoanProduct` model: `id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)`

When these UUID objects were included in dictionaries or querysets that were eventually serialized to JSON (via `JsonResponse` or Django templates), they couldn't be serialized directly.

## Solution
1. Convert all UUID fields to strings when creating data dictionaries: `'id': str(loan.id)`
2. Convert querysets with UUID primary keys to lists of dictionaries with string IDs
3. Convert UUID parameters in context dictionaries to strings

## Files Fixed

### 1. `reports/views.py`
Fixed **18 occurrences** across multiple functions:

#### Loan ID Conversions (10 fixes):
- Line ~816: `processing_fees_report()` - processing fees section
- Line ~1085: `enhanced_loans_due_report_v1()` - loans data section  
- Line ~1261: `enhanced_delinquent_loans_report()` - delinquent loans section
- Line ~2015: `generate_enhanced_loans_due_data()` - enhanced loans data
- Line ~2343: Enhanced loans data function
- Line ~2493: Processing fees data function
- Line ~5659: `enhanced_loans_due_report()` - loans due today section
- Line ~6002: `enhanced_loans_due_report()` - loan_data dictionary
- Line ~6677: Interest income report section

#### LoanProduct Queryset Conversions (7 fixes):
- Line ~383: `comprehensive_dashboard()` - loan products dropdown
- Line ~511: Dashboard view - loan products dropdown
- Line ~726: Reports dashboard - loan products dropdown
- Line ~6128: `enhanced_loans_due_report()` - loan products dropdown
- Line ~6301: Processing fees report - loan products dropdown
- Line ~6877: Interest income report - loan products dropdown
- Line ~7071: Completed loans report - loan products dropdown
- Line ~7336: Disbursed loans report - loan products dropdown

#### product_id Parameter Conversions (3 fixes):
- Line ~6160: Export filters - product_id
- Line ~6208: Context filters - product_id
- Line ~7346: Disbursed loans context - loan_product_id

### 2. `reports/fixed_interest_income_view.py`
Fixed 1 occurrence:
- Line ~97: Interest income loans data - loan ID conversion

### 3. `reports/comprehensive_reports.py`
Fixed 1 occurrence:
- Line ~350: Comprehensive loans data - loan ID conversion

## Total Changes
- **Files Modified:** 3
- **UUID to String Conversions:** 20
- **Queryset to Dict List Conversions:** 7
- **Parameter Conversions:** 3
- **Total Fixes:** 30

## Pattern Applied

### For Loan IDs:
```python
# ❌ Wrong
loan_data = {'id': loan.id}

# ✅ Correct
loan_data = {'id': str(loan.id)}
```

### For LoanProduct Querysets:
```python
# ❌ Wrong
loan_products = LoanProduct.objects.filter(is_active=True)
context = {'loan_products': loan_products}

# ✅ Correct
loan_products_qs = LoanProduct.objects.filter(is_active=True)
loan_products = [
    {'id': str(p.id), 'name': p.name, 'product_type': p.product_type}
    for p in loan_products_qs
]
context = {'loan_products': loan_products}
```

### For UUID Parameters:
```python
# ❌ Wrong
'product_id': product_id  # Could be UUID

# ✅ Correct
'product_id': str(product_id) if product_id else None
```

## Testing
After these changes, all report endpoints should work correctly with:
- `?today_only=true` parameter
- `?format=json` parameter  
- `?product_id=<uuid>` parameter
- Any other query parameters that return JSON responses
- Template rendering with loan product dropdowns

## Verification Steps
1. Visit `http://127.0.0.1:8000/reports/loans-due/enhanced/?today_only=true`
2. Verify no TypeError occurs
3. Check that loan IDs are displayed correctly
4. Test filter dropdowns with loan products
5. Test JSON export endpoints
6. Verify PDF/Excel exports work

## Related Models
Other models that might have UUID fields should also be checked if they're serialized to JSON:
- `LoanApplication`
- Any custom models with `UUIDField`

## Prevention
When creating dictionaries or context for JSON serialization or template rendering:
1. Always convert UUID fields to strings
2. Convert querysets with UUID PKs to lists of dictionaries
3. Test with `?format=json` to ensure serializability

