# Branch & Portfolio Filtering - Quick Reference Card

## 🎯 At a Glance

| Role | Sees | Branch Filter | Portfolio Filter |
|------|------|---------------|------------------|
| **Admin** | Everything | ✓ Optional | ✗ No |
| **Loan Officer** | Own portfolio only | ✓ Optional | ✓ Always |
| **Team Leader** | Own portfolio only | ✓ Optional | ✓ Always |
| **Secretary** | Own branch only | ✗ Fixed | ✗ No |
| **Auditor** | Own branch only | ✗ Fixed | ✗ No |

---

## 📊 Dashboard Metrics - What's Filtered

### ✅ ALL sections respect filtering:

- Portfolio Overview (Active Loans, Portfolio Value, Outstanding, Collection Rate)
- Loans Due Today (Daily, Weekly, Monthly)
- Delinquent Loans (1-30, 31-60, 60+ days)
- Processing Fees (Current Month)
- Interest Income (Current Month)
- Registration Fees
- Customer Requests (Pending, In Progress, Resolved)
- Loans in Arrears
- Missed Payments
- Completed Loans
- Client Growth Analytics

---

## 🔧 For Developers

### Using Filtering in Views:

```python
from utils.filtering import get_filtered_loans, get_filtered_clients

@login_required
def my_view(request):
    selected_branch_id = request.session.get('selected_branch_id')
    
    # Get filtered data
    loans = get_filtered_loans(request.user, selected_branch_id)
    clients = get_filtered_clients(request.user, selected_branch_id)
    
    # Use normally
    total = loans.count()
```

### Available Functions:

- `get_filtered_clients(user, branch_id=None)`
- `get_filtered_loans(user, branch_id=None)`
- `get_filtered_applications(user, branch_id=None)`
- `get_filtered_repayments(user, branch_id=None)`
- `get_filtered_documents(user, branch_id=None)`
- `get_filtered_notifications(user, branch_id=None)`
- `apply_branch_and_portfolio_filters(queryset, user, branch_id, model_type)`
- `get_user_context(user)`

---

## ✅ Verification

### Run Tests:
```bash
python test_comprehensive_filtering.py
python verify_filtering_implementation.py
```

### Expected Results:
- ✓ All imports found
- ✓ Functions working
- ✓ Context correct
- ✓ Branch filtering working
- ✓ Portfolio filtering working

---

## 📁 Key Files

| File | Purpose |
|------|---------|
| `utils/filtering.py` | Core filtering logic |
| `reports/views.py` | Reports with filtering |
| `loans/views.py` | Loans with filtering |
| `utils/views.py` | Utils with filtering |
| `users/views.py` | Users with filtering |

---

## 🚨 Troubleshooting

| Problem | Solution |
|---------|----------|
| User sees no data | Check role, branch, portfolio assignments |
| User sees too much | Verify role is not admin/superuser |
| Branch filter not working | Check session has `selected_branch_id` |
| Portfolio not working | Verify clients assigned to loan officer |

---

## 📞 Quick Help

1. **Technical docs**: `COMPREHENSIVE_FILTERING_IMPLEMENTATION.md`
2. **User guide**: `STAFF_FILTERING_GUIDE.md`
3. **Summary**: `FILTERING_IMPLEMENTATION_SUMMARY.md`
4. **This card**: `FILTERING_QUICK_REFERENCE.md`

---

## ✨ Status: ✅ COMPLETE

All views, reports, and dashboards now have proper branch and portfolio filtering implemented and tested.
