# Tickets Summary Email Command

## Overview
This Laravel command collects comprehensive statistics about tickets in the system and sends them via email as a beautifully formatted HTML report.

## Command Details

### Command Signature
```bash
php artisan tickets:send-summary {email}
```

### Parameters
- `email` (required): The recipient email address where the summary will be sent

### Description
The command collects and sends the following ticket information:

#### Statistics Included:
1. **Overview**
   - Total tickets count
   - Active tickets count
   - Deleted tickets count
   - Overdue tickets count
   - Tickets due today count

2. **Tickets by Status**
   - Breakdown of tickets grouped by their status
   - Count for each status

3. **Tickets by Priority**
   - Breakdown of tickets grouped by their priority
   - Count for each priority level

4. **Tickets by Severity**
   - Breakdown of tickets grouped by their severity
   - Count for each severity level

5. **Tickets by Category**
   - Breakdown of tickets grouped by their category
   - Count for each category

6. **Overdue Tickets**
   - Detailed list of all overdue tickets
   - Includes ticket ID, name, due date, status, priority, and description

7. **Tickets Due Today**
   - Detailed list of tickets due today
   - Includes ticket ID, name, due date, status, priority, and description

8. **Recent Tickets**
   - Last 10 tickets created in the past 7 days
   - Includes ticket ID, name, creation date, status, priority, and description

## Usage Examples

### Send summary to a single email
```bash
php artisan tickets:send-summary admin@example.com
```

### Send summary to manager
```bash
php artisan tickets:send-summary manager@company.com
```

### Test the command
```bash
php artisan tickets:send-summary test@localhost.com
```

## Email Configuration

The command uses the SMTP credentials configured in your `.env` file:

```env
MAIL_DRIVER=smtp
MAIL_HOST=smtppro.zoho.com
MAIL_PORT=465
MAIL_USERNAME=a.abdo@egytelecoms.com
MAIL_PASSWORD=U1zwpa2A258R
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS='info@localhost'
MAIL_FROM_NAME="Hatif"
```

### Email Template
The email is sent using the HTML template located at:
- Path: `resources/views/emails/tickets-summary.blade.php`
- Format: HTML with inline CSS
- Responsive: Yes, mobile-friendly design

## Scheduling (Optional)

You can schedule this command to run automatically using Laravel's task scheduler.

Add to `app/Console/Kernel.php` in the `schedule()` method:

```php
// Send daily summary at 9 AM
$schedule->command('tickets:send-summary manager@company.com')
         ->dailyAt('09:00');

// Send weekly summary every Monday at 8 AM
$schedule->command('tickets:send-summary admin@company.com')
         ->weeklyOn(1, '8:00');

// Send monthly summary on the 1st at 9 AM
$schedule->command('tickets:send-summary boss@company.com')
         ->monthlyOn(1, '09:00');
```

## Features

### HTML Email Template Features:
- **Responsive Design**: Mobile and desktop friendly
- **Color-coded Sections**: Different colors for different sections
- **Statistics Cards**: Visual cards showing key metrics
- **Data Tables**: Clean tables for statistics breakdown
- **Ticket Details**: Detailed information about overdue and due today tickets
- **Professional Styling**: Modern gradient design with shadows and hover effects

### Security Features:
- Uses Laravel's Mail facade for secure sending
- Respects .env configuration
- No hardcoded credentials
- Built-in error handling

## Error Handling

The command includes comprehensive error handling:
- Returns exit code 0 on success
- Returns exit code 1 on failure
- Displays error messages in the console
- Logs any exceptions that occur during email sending

## Troubleshooting

### Email not sending
1. Check your SMTP credentials in `.env`
2. Verify the SMTP server is reachable
3. Check firewall settings for port 465
4. Review Laravel logs in `storage/logs/laravel.log`

### Command not found
1. Clear cache: `php artisan cache:clear`
2. Clear config: `php artisan config:clear`
3. Verify command is registered in `app/Console/Kernel.php`

### No data in email
1. Check if tickets exist in database
2. Verify database connection
3. Check ticket relationships (status, priority, severity, category)

## File Locations

- **Command File**: `/var/www/ccsystem/app/Console/Commands/SendTicketsSummary.php`
- **Email Template**: `/var/www/ccsystem/resources/views/emails/tickets-summary.blade.php`
- **Kernel Registration**: `/var/www/ccsystem/app/Console/Kernel.php`

## Development Notes

### Modifying the Email Template
The email template uses Blade syntax and can be customized:
- Edit `resources/views/emails/tickets-summary.blade.php`
- CSS is inline for better email client compatibility
- Add new sections as needed
- Modify colors, fonts, and layout

### Extending the Command
To add more statistics:
1. Add queries in `SendTicketsSummary.php` handle() method
2. Pass data to the email view
3. Update the email template to display new data

## Dependencies
- Laravel Framework
- Laravel Mail
- Carbon (for date handling)
- Modules\Tickets\Entities (Ticket models)

## Support
For issues or questions, contact your system administrator.
