@if(auth()->check() && !auth()->user()->hasRole(['branch_manager', 'registrar_hq', 'Registrar', 'superadmin']))
@php
$user = auth()->user();
$role = $user->getRoleNames()->first();
// Use same logic as NotificationController and header
$myEnquiriesNotifications = \App\Models\Notification::query();
switch ($role) {
case 'Registrar':
case 'registrar':
$myEnquiriesNotifications->whereHas('enquiry', function ($q) use ($user) {
$q->where('registered_by', $user->id);
});
break;
case 'registrar_hq':
case 'public_relation_officer':
case 'superuser':
case 'general_manager':
// See everything - no filter
break;
case 'assistant_manager':
$myEnquiriesNotifications->where(function ($q) {
$q->where('type', 'payment_initiated')
->orWhere('type', 'loan_processed');
});
break;
case 'branch_manager':
$myEnquiriesNotifications->whereHas('enquiry', function ($q) use ($user) {
$q->whereHas('registeredBy', function ($subQ) use ($user) {
$subQ->where('branch_id', $user->branch_id);
});
});
break;
case 'loan_officer':
case 'loanofficer':
case 'Loan_Officer':
case 'accountant':
case 'Accountant':
// Show ONLY assigned enquiries and relevant notification types
$myEnquiriesNotifications->where(function($q) use ($user) {
// Notifications for enquiries assigned to this user
$q->whereHas('enquiry', function ($subQ) use ($user) {
$subQ->whereHas('assignedUsers', function ($assignQ) use ($user) {
$assignQ->where('user_id', $user->id);
});
})
// OR notifications of specific types related to their work
->orWhereIn('type', [
'payment_initiated',
'payment_approved',
'loan_processed',
'loan_approved',
'enquiry_assigned'
]);
});
break;
default:
$myEnquiriesNotifications->where('user_id', $user->id);
}
// Get UNREAD notifications count only
$myEnquiriesCount = $myEnquiriesNotifications
->where('is_read', false)
->count();
@endphp