# Cron Jobs Setup Guide

This system uses a **database-driven cron job system** - you only need to set up ONE cron job that runs `run-cron-jobs.php`, and it will handle all scheduled tasks automatically.

## Step 1: Create Database Tables

1. Open phpMyAdmin
2. Select your database (`returnadvisor`)
3. Go to the SQL tab
4. Run the SQL file: `database/create_cron_jobs_table.sql`

This creates:
- `cron_jobs` table - stores all scheduled jobs
- `cron_job_logs` table - stores execution history

## Step 2: Insert Default Cron Jobs

1. Still in phpMyAdmin SQL tab
2. Run the SQL file: `database/insert_default_cron_jobs.sql`

This inserts:
- **Email Campaign Sender** - Runs every minute to send campaign emails
- **Invoice Reminder Sender** - Runs daily at 9 AM to send invoice reminders

## Step 3: Set Up One Cron Job

You only need to set up **ONE cron job** on your server:

### Option A: Server Cron (Recommended)

**Linux/Unix:**
```bash
# Edit crontab
crontab -e

# Add this line (runs every minute)
* * * * * curl -s http://yourdomain.com/returnadvisor/run-cron-jobs.php?key=your_secret_key > /dev/null 2>&1
```

**Windows Task Scheduler:**
1. Open Task Scheduler
2. Create Basic Task
3. Set trigger to "Daily" and set to repeat every 1 minute
4. Action: Start a program
5. Program: `curl.exe` (or use PHP directly)
6. Arguments: `http://yourdomain.com/returnadvisor/run-cron-jobs.php?key=your_secret_key`

### Option B: External Cron Service (No Server Access)

Use a free service like:
- **cron-job.org** - Free cron service
- **EasyCron** - Free tier available
- **UptimeRobot** - Free monitoring that can trigger HTTP requests

Just point it to: `http://yourdomain.com/returnadvisor/run-cron-jobs.php?key=your_secret_key`

### Option C: Manual Testing

You can test by visiting in your browser:
```
http://localhost/returnadvisor/run-cron-jobs.php
```

For production, add a secret key for security:
```
http://localhost/returnadvisor/run-cron-jobs.php?key=your_secret_key
```

## Step 4: Set Secret Key (Optional but Recommended)

To protect your cron runner from unauthorized access:

1. Go to Admin Panel > Settings
2. Add a setting: `cron_secret_key` with a secure random value
3. Use this key in your cron job URL: `?key=your_secret_key`

## Step 5: Manage Cron Jobs

1. Go to **Admin Panel > Cron Jobs**
2. You can:
   - View all scheduled jobs
   - Activate/Deactivate jobs
   - Run jobs manually
   - View execution logs
   - See next run times

## How It Works

1. Your single cron job calls `run-cron-jobs.php` every minute
2. `run-cron-jobs.php` checks the database for jobs that are due to run
3. It executes each due job's script
4. It updates the database with execution results and sets the next run time
5. All logs are stored in the database

## Benefits

- ✅ Only ONE cron job needed (not multiple)
- ✅ Easy to manage via admin panel
- ✅ Full execution history and logs
- ✅ Can activate/deactivate jobs without touching server cron
- ✅ No need to modify server cron files to add new jobs
- ✅ Works even if you only have HTTP access (external cron services)

## Adding New Cron Jobs

1. Insert a new record in the `cron_jobs` table
2. Or use the admin panel (if we add that feature later)

Example SQL:
```sql
INSERT INTO cron_jobs (name, description, script_path, schedule_type, interval_seconds, next_run, is_active) 
VALUES (
    'My New Job',
    'Description of what this job does',
    'path/to/script.php',
    'hourly',
    3600,
    DATE_ADD(NOW(), INTERVAL 1 HOUR),
    1
);
```

