# Comprehensive Report System

## Overview

The comprehensive report system provides a unified reporting interface at `/report` that allows users to generate detailed reports for matters and tasks with advanced filtering capabilities.

## URL

- **Main Report Page**: `http://localhost:8000/report`

## Features

### Report Types

1. **Matters Report** - Filter and report on legal matters
2. **Tasks Report** - Filter and report on tasks
3. **Summary Report** - Overview statistics for matters and tasks

### Filtering Capabilities

#### Matters Report Filters

- **Client Filter**: Filter matters by specific client
- **Department Filter**: Filter matters by department
- **Status Filter**: Filter by matter status (Active, Completed, Closed, On Hold, Cancelled)
- **Assigned To**: Filter by assigned user
- **Date Range**: Filter by start date, due date, or creation date
- **Search**: Text search across subject, description, and client name

#### Tasks Report Filters

- **Matter Filter**: Filter tasks by specific matter
- **Department Filter**: Filter tasks by department (through matter)
- **Status Filter**: Filter by task status (Pending, In Progress, On Hold, Completed, Cancelled)
- **Assigned To**: Filter by assigned user
- **Category Filter**: Filter by task category
- **Date Range**: Filter by start date, due date, or creation date
- **Search**: Text search across title, description, and notes

#### Summary Report

- **Matters Summary**: Total, Active, Completed, and Closed counts
- **Tasks Summary**: Total, Pending, In Progress, and Completed counts

## Usage Examples

### Example 1: Filter Matters by Client During a Period

1. Navigate to `http://localhost:8000/report`
2. Select "Matters Report"
3. Choose a client from the "Client" dropdown
4. Set "Date From" and "Date To" fields
5. Click "Export" to download the filtered report

### Example 2: Filter Matters by Department with Status Closed During a Period

1. Navigate to `http://localhost:8000/report`
2. Select "Matters Report"
3. Choose a department from the "Department" dropdown
4. Select "Closed" from the "Status" dropdown
5. Set "Date From" and "Date To" fields
6. View results or export the report

### Example 3: Filter Tasks from Department or Matter

1. Navigate to `http://localhost:8000/report`
2. Select "Tasks Report"
3. Choose either:
   - A specific matter from the "Matter" dropdown, OR
   - A department from the "Department" dropdown
4. Optionally add additional filters (status, assigned user, etc.)
5. View results or export the report

## Export Functionality

### Export Options

- **Excel Export**: Download filtered data as Excel (.xlsx) files
- **Field Selection**: Choose which fields to include in the export
- **Custom Naming**: Files are automatically named with timestamp

### Export Permissions

- Users must have `data.export` permission to export reports
- Users must have `reports.view` permission to view reports

## Technical Details

### Component Structure

- **Livewire Component**: `App\Livewire\ComprehensiveReport`
- **View**: `resources/views/livewire/comprehensive-report.blade.php`
- **Route**: `/report` (defined in `routes/web.php`)

### Database Queries

The system uses optimized queries with proper relationships:

- **Matters**: Includes client, assigned user, and creator relationships
- **Tasks**: Includes matter, client, category, assigned user, and creator relationships
- **Filtering**: Uses efficient database queries with proper indexing

### Performance Considerations

- **Pagination**: Results are paginated (10, 25, 50, 100 items per page)
- **Sorting**: Click column headers to sort results
- **Query Optimization**: Uses eager loading to prevent N+1 queries
- **Caching**: Consider implementing caching for frequently accessed reports

## Permissions

### Required Permissions

- `reports.view` - Required to access the report page
- `data.export` - Required to export data

### Role-Based Access

The report system respects role-based permissions:

- **Admin/Superadmin**: Can see all data
- **Senior Lawyers**: Can see data from their departments
- **Associates/Trainees**: Can see their assigned tasks and matters

## Customization

### Adding New Filters

To add new filters:

1. Add filter properties to the Livewire component
2. Update the query building methods
3. Add filter UI elements to the view
4. Update the export data methods if needed

### Adding New Report Types

To add new report types:

1. Add the report type to the `reportType` property options
2. Create corresponding query and export methods
3. Add UI elements for the new report type
4. Update the view to handle the new report type

## Troubleshooting

### Common Issues

1. **No Results**: Check filter criteria and ensure data exists
2. **Export Fails**: Verify user has export permissions
3. **Slow Performance**: Consider adding database indexes for frequently filtered fields

### Debug Mode

Enable debug mode to see query details:

```php
// In the component
\DB::enableQueryLog();
// After query execution
dd(\DB::getQueryLog());
```

## Future Enhancements

### Planned Features

- **Saved Reports**: Save frequently used filter combinations
- **Scheduled Reports**: Automatically generate and email reports
- **Advanced Analytics**: Charts and graphs for data visualization
- **Report Templates**: Predefined report templates for common use cases
- **API Integration**: REST API endpoints for programmatic access

### Performance Improvements

- **Database Indexing**: Add indexes for frequently filtered columns
- **Caching**: Implement Redis caching for report data
- **Background Processing**: Use queues for large report generation
- **Lazy Loading**: Implement infinite scroll for large datasets
