# Automated Daily Reports - Cron Setup Guide

This guide explains how to set up automated daily email reports for Exhibit Media Manager.

## Prerequisites

1. SMTP email settings configured in EMM Admin Settings
2. Access to your server's scheduling system
3. PHP installed on your server

---

## Option 1: cPanel (Web Hosting)

### Step 1: Log into cPanel
Access your hosting control panel at `yourdomain.com/cpanel` or through your hosting provider.

### Step 2: Find Cron Jobs
In cPanel, search for "Cron Jobs" or navigate to **Advanced > Cron Jobs**.

### Step 3: Add New Cron Job

**Common Settings:**
- Minute: `0`
- Hour: `6` (runs at 6:00 AM server time)
- Day: `*`
- Month: `*`
- Weekday: `*`

**Command:**
```
/usr/local/bin/php /home/YOUR_USERNAME/public_html/emm/api/daily_report.php
```

Replace:
- `YOUR_USERNAME` with your cPanel username
- `/public_html/emm/` with your actual EMM installation path

**Alternative paths (try if the above doesn't work):**
```
/usr/bin/php /home/YOUR_USERNAME/public_html/emm/api/daily_report.php
php /home/YOUR_USERNAME/public_html/emm/api/daily_report.php
```

### Step 4: Save and Test
Click "Add New Cron Job" and wait for the next scheduled run, or run the command manually via SSH to test.

### Troubleshooting cPanel
- Check email for cron output notifications
- Verify PHP path with: `which php` in SSH
- Check `/home/YOUR_USERNAME/logs/` for error logs

---

## Option 2: Mac ServeBay

ServeBay is a local development environment for macOS. Use either crontab or launchd.

### Method A: Using crontab

#### Step 1: Open Terminal
Press `Cmd + Space`, type "Terminal", and press Enter.

#### Step 2: Edit crontab
```bash
crontab -e
```

#### Step 3: Add the cron job
```bash
0 6 * * * /Applications/ServeBay/php/8.2/bin/php /Applications/ServeBay/www/emm/api/daily_report.php >> /tmp/emm_daily_report.log 2>&1
```

Adjust paths based on your ServeBay installation:
- PHP path: Usually in `/Applications/ServeBay/php/VERSION/bin/php`
- Web root: Usually in `/Applications/ServeBay/www/`

#### Step 4: Save and exit
- nano: `Ctrl+O` to save, `Ctrl+X` to exit
- vim: `Esc`, type `:wq`, press Enter

### Method B: Using launchd (Recommended for Mac)

#### Step 1: Create a plist file
```bash
nano ~/Library/LaunchAgents/com.emm.dailyreport.plist
```

#### Step 2: Add this content
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.emm.dailyreport</string>
    
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/ServeBay/php/8.2/bin/php</string>
        <string>/Applications/ServeBay/www/emm/api/daily_report.php</string>
    </array>
    
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>6</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    
    <key>StandardOutPath</key>
    <string>/tmp/emm_daily_report.log</string>
    
    <key>StandardErrorPath</key>
    <string>/tmp/emm_daily_report_error.log</string>
</dict>
</plist>
```

Adjust paths to match your ServeBay installation.

#### Step 3: Load the job
```bash
launchctl load ~/Library/LaunchAgents/com.emm.dailyreport.plist
```

#### Step 4: Verify it's running
```bash
launchctl list | grep emm
```

### Finding ServeBay Paths

To find the correct PHP path in ServeBay:
1. Open ServeBay application
2. Go to Settings > PHP
3. Note the PHP version you're using
4. The PHP binary is typically at: `/Applications/ServeBay/php/VERSION/bin/php`

To find your web root:
1. Open ServeBay application
2. Go to Settings > General
3. Look for "Document Root" or "www" directory

---

## Option 3: Standard Linux Server (VPS/Dedicated)

### Using crontab

#### Step 1: Open terminal/SSH

#### Step 2: Edit crontab
```bash
crontab -e
```

#### Step 3: Add the cron job
```bash
0 6 * * * /usr/bin/php /var/www/html/emm/api/daily_report.php >> /var/log/emm_daily_report.log 2>&1
```

Adjust paths as needed for your installation.

---

## Testing Your Setup

### Manual Test
Run the script manually to verify it works:
```bash
php /path/to/emm/api/daily_report.php
```

### Check Logs
After the scheduled time, check the log file:
```bash
cat /tmp/emm_daily_report.log
# or
tail -f /tmp/emm_daily_report.log
```

---

## Common Issues

### "PHP not found"
- Find PHP: `which php` or `whereis php`
- Use the full path in your cron command

### "Permission denied"
- Make the script executable: `chmod +x api/daily_report.php`
- Check file ownership matches cron user

### "No email received"
- Verify SMTP settings in EMM Admin Settings
- Check spam folder
- Review log files for SMTP errors

### "Script runs but nothing happens"
- Ensure there's activity data to report
- Check that admin email is configured in settings
- Verify database connection works from CLI

---

## Uninstalling

### cPanel
Remove the cron job from the cPanel Cron Jobs interface.

### Mac launchd
```bash
launchctl unload ~/Library/LaunchAgents/com.emm.dailyreport.plist
rm ~/Library/LaunchAgents/com.emm.dailyreport.plist
```

### Linux crontab
```bash
crontab -e
# Delete the line with daily_report.php
```
