# Receipts Table & Notification Filtering - Fixes Applied

## Issues Found

### 1. Missing `receipts` Table Error
**Error**: `ProgrammingError: Table 'acbptxvs_branch_system.receipts' doesn't exist`

**Root Cause**: The `receipts_list` view in `utils/views.py` was trying to use a `Receipt` model that doesn't have a corresponding database table. The system uses `Repayment` model for payment receipts.

**Fix Applied**:
- Changed `utils/views.py` line 1373 from `from utils.models import Receipt` to `from loans.models import Repayment`
- Updated the queryset to use `Repayment.objects` instead of `Receipt.objects`
- Fixed all field references from `borrower` to `loan__borrower` (since Repayment relates to Loan, which relates to borrower)

### 2. Loan Officers Seeing All Notifications
**Issue**: Loan officers are seeing notifications for loan applications not assigned to them.

**Expected Behavior**: 
- Loan officers should only see notifications for:
  - Applications from clients assigned to them (portfolio_manager)
  - System-wide alerts (user=null)

**Files Modified**:
1. `utils/views.py` - Fixed receipts_list view to use Repayment model
2. `users/portfolio_views.py` - Added `is_active=True` filter and ordering for portfolio managers

## What Still Needs to Be Done

### Notification Filtering
The dashboard that shows "System Alerts" and "Your Notifications" needs to be updated to filter notifications based on:

```python
# For loan officers/team leaders
if request.user.role in ['loan_officer', 'team_leader']:
    notifications = Notification.objects.filter(
        Q(user=request.user) |  # Personal notifications
        Q(user__isnull=True) |  # System alerts
        Q(loan_app__borrower__portfolio_manager=request.user)  # Applications from their clients
    ).order_by('-created_at')
```

### Pending Applications Filtering
The pending applications list should also be filtered:

```python
# For loan officers/team leaders
if request.user.role in ['loan_officer', 'team_leader']:
    pending_applications = LoanApplication.objects.filter(
        status='pending',
        borrower__portfolio_manager=request.user
    ).order_by('-submitted_at')
```

## Testing

1. **Test Receipts Page**:
   - Go to `/utils/receipts/`
   - Should now load without the table error
   - Should show payment receipts (Repayments)

2. **Test Client Assignment**:
   - Go to any client detail page
   - Click "Reassign Client"
   - Should now see 5 portfolio managers in the dropdown

3. **Test Notifications** (Still needs implementation):
   - Login as loan officer
   - Should only see notifications for their assigned clients
   - Should see system-wide alerts

## Next Steps

To complete the notification filtering, we need to:
1. Find the view that renders the dashboard with "System Alerts" and "Your Notifications"
2. Add portfolio-based filtering to the notifications queryset
3. Add portfolio-based filtering to the pending applications list
4. Test with multiple loan officers to ensure they only see their assigned clients' data
