# Accounting Reports Menu Debugging Guide

## Overview
This guide helps diagnose why the Accounting Reports menu is not showing in the admin sidebar.

## Quick Checks

### 1. Check Module Installation
The module must be installed. Check the `system` table:
```sql
SELECT * FROM system WHERE `key` = 'accountingreports_version';
```

If no result, install the module via:
- Settings → Manage Modules → Find "AccountingReports" → Install

Or manually:
```sql
INSERT INTO system (`key`, `value`, `created_at`, `updated_at`) 
VALUES ('accountingreports_version', '1.0.3', NOW(), NOW());
```

### 2. Check User Permissions
The menu shows if the user has ANY of these:
- Admin role: `Admin#{business_id}`
- Superadmin permission
- Any accounting permission (accounting.view_all, accounting.view_trial_balance, etc.)

To grant permissions:
1. Go to Settings → Roles & Permissions
2. Edit the user's role
3. Check at least one accounting permission
4. Save

### 3. Check Laravel Logs
Check `storage/logs/laravel.log` for AccountingReports entries:
```bash
# Windows PowerShell
Get-Content storage\logs\laravel.log -Tail 100 | Select-String "AccountingReports"
```

Look for:
- `AccountingReports: modifyAdminMenu() method called` - Method is being invoked
- `AccountingReports: Module installed` - Module check passed
- `AccountingReports: User has permissions` - Permission check passed
- `AccountingReports: Menu instance found` - Menu instance retrieved
- `AccountingReports: Menu added successfully with X items` - Menu items added

### 4. Clear Cache
```bash
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
```

### 5. Verify Module Registration
Check if module is registered:
```bash
php artisan module:list
```

Should show "AccountingReports" in the list.

## Code Flow

1. **AdminSidebarMenu Middleware** (`app/Http/Middleware/AdminSidebarMenu.php`)
   - Line 25: Creates menu instance `admin-sidebar-menu`
   - Line 791: Calls `ModuleUtil::getModuleData('modifyAdminMenu')`

2. **ModuleUtil** (`app/Utils/ModuleUtil.php`)
   - Line 56-93: `getModuleData()` method
   - Iterates through installed modules
   - Calls `modifyAdminMenu()` on each module's DataController

3. **AccountingReports DataController** (`Modules/AccountingReports/Http/Controllers/DataController.php`)
   - Line 115: `modifyAdminMenu()` method
   - Checks module installation
   - Checks user permissions
   - Retrieves menu instance
   - Adds menu items

## Common Issues

### Issue 1: Method Not Called
**Symptom**: No log entry "AccountingReports: modifyAdminMenu() method called"
**Possible Causes**:
- Module not registered
- DataController class not found
- Method doesn't exist

**Solution**:
- Verify module is in `Modules/` directory
- Check `composer.json` autoload
- Run `composer dump-autoload`

### Issue 2: Module Not Installed
**Symptom**: Log shows "AccountingReports: Module not installed"
**Solution**: Install module via module manager or set system property

### Issue 3: No Permissions
**Symptom**: Log shows "AccountingReports: User does not have any accounting permissions"
**Solution**: Grant accounting permissions to user role

### Issue 4: Menu Instance Not Found
**Symptom**: Log shows "AccountingReports: Menu instance not found"
**Possible Causes**:
- AdminSidebarMenu middleware not loaded
- Menu created after module menu modification

**Solution**:
- Check `app/Http/Kernel.php` - AdminSidebarMenu should be in middleware groups
- Verify middleware order

### Issue 5: Empty Menu Dropdown
**Symptom**: Menu shows but dropdown is empty
**Possible Causes**:
- All menu items filtered out by permissions
- Routes don't exist

**Solution**:
- Check route names in `Routes/web.php`
- Verify route registration: `php artisan route:list | grep accounting-reports`

## Testing

### Manual Test
Add this to a controller temporarily:
```php
$menu = Menu::instance('admin-sidebar-menu');
if ($menu) {
    $menu->url(route('accounting-reports.index'), 'Test Accounting', ['icon' => 'fas fa-test']);
}
```

### Check Routes
```bash
php artisan route:list | grep accounting-reports
```

Should show all accounting-reports routes.

## Recent Changes Made

1. **Enhanced Logging**: Added comprehensive logging throughout the menu generation process
2. **Better Error Handling**: Added try-catch with detailed error logging
3. **Session Validation**: Added checks for authenticated user and business session
4. **Permission Debugging**: Added logging for permission checks
5. **Menu Instance Validation**: Added check and logging for menu instance retrieval

## Files Modified

- `Modules/AccountingReports/Http/Controllers/DataController.php`
  - Enhanced `modifyAdminMenu()` method with better logging and error handling

## Next Steps

If menu still doesn't show after following this guide:

1. Check browser console for JavaScript errors
2. Check network tab for failed requests
3. Verify menu rendering in `resources/views/layouts/partials/sidebar.blade.php`
4. Check if other modules' menus are showing (to isolate issue)
5. Review Laravel logs for any exceptions



