# End-of-day compile — fully automatic every day

**Simple:** One automatic job = compile the report + send the 24h daily closing email to all paid distributors. No separate email setup. When the nightly compile runs, the email is sent right after.

---

## Quick setup (choose one)

**A) Your host has “Cron Jobs” (cPanel / Plesk / etc.) and you can run PHP:**

- **Time:** 23:59 every day (or 20:59 if server is UTC).
- **Command:**  
  `cd /home/afrinerr/domains/afrinex.shop/public_html/api-backend/admin && php cron-end-of-day-compile.php`  
  (adjust path to your server.)

**B) Your host only allows “URL” cron (no PHP command):**

1. In `api-backend/.env` add: `CRON_ARCHIVE_KEY=choose-a-long-secret-string-here`
2. In Cron Jobs, add: **Minute** 59, **Hour** 23, **Command:**  
   `curl -s "https://www.afrinex.shop/api-backend/admin/cron-end-of-day-compile.php?key=choose-a-long-secret-string-here"`  
   (use the same secret as in step 1.)

After that, every day at 23:59 the system will compile the report and send the 24h daily closing emails to all paid distributors automatically.

---

## What runs automatically every day (no human action)

- **Time:** 11:59 PM (23:59) Addis Ababa time  
- **Script:** `cron-end-of-day-compile.php`  
- **All steps run in one go; all emails are sent automatically:**
  1. Compiles that day’s payments into one file (`payments_YYYY-MM-DD.json`) and paid-transferred archive; removes timestamped files for that date.
  2. Populates the special 24h financial email table with closed-day amounts.
  3. Saves the financial archive (`financial_YYYY-MM-DD.json` with `email_table_sent`).
  4. **Sends 3 financial report emails** (Tax, System out source, Training & Packaging) from the archive — one per account. No button click.
  5. **Sends MLM daily commission paid report** — one email per paid distributor for that date, plus a summary email to admin. No button click.

**No human action is required** once the cron job is scheduled. Do not rely on manual “Send 24h daily closing report” or “Send emails from this table” for the daily run; those are for resend or test only.

## One-time setup (choose one)

### Option 1: Linux / Mac — run the setup script (recommended)

```bash
cd /path/to/public_html/api-backend/admin
bash setup-end-of-day-cron.sh
```

Answer `y` when asked to add the cron job. Done.

### Option 2: Linux / Mac — add cron manually

```bash
crontab -e
```

Add this line (replace `/path/to/admin` with the real path to the `admin` folder):

```
59 23 * * * cd /path/to/public_html/api-backend/admin && php cron-end-of-day-compile.php >> /path/to/public_html/api-backend/admin/payment-archives/cron-compile.log 2>&1
```

If the server uses UTC, use 20:59 UTC (= 23:59 Addis Ababa):

```
59 20 * * * cd /path/to/admin && php cron-end-of-day-compile.php >> /path/to/admin/payment-archives/cron-compile.log 2>&1
```

### Option 3: cPanel / hosting “Cron Jobs”

1. Open **Cron Jobs** in cPanel.  
2. **Minute:** 59  
3. **Hour:** 23 (or 20 if server is UTC)  
4. **Day, Month, Weekday:** * * *  
5. **Command:**  
   `cd /home/youruser/public_html/api-backend/admin && php cron-end-of-day-compile.php`  
   (use your real path).

### Option 4: HTTP (curl) — if PHP CLI is not available (e.g. shared hosting)

Use the host’s “Cron Jobs” to call the script by URL at 23:59 every day:

```
59 23 * * * curl -s "https://www.afrinex.shop/api-backend/admin/cron-end-of-day-compile.php?key=YOUR_SECRET"
```

Replace `YOUR_SECRET` with a long random string (e.g. `a1b2c3d4e5f6...`). Set the same value in one of these places:

- **In `api-backend/.env`** (recommended): add a line:
  ```
  CRON_ARCHIVE_KEY=your-secret-string
  ```
- Or in `api-backend/config.php`: `define('CRON_ARCHIVE_KEY', 'your-secret-string');`
- Or server environment variable: `CRON_ARCHIVE_KEY=your-secret-string`

### Option 5: Windows Task Scheduler

1. Open **Task Scheduler**.  
2. Create Basic Task: **Daily**, repeat at **11:59 PM**.  
3. Action: **Start a program**.  
4. Program: `php.exe` (or full path to php.exe).  
5. Arguments: `"C:\path\to\public_html\api-backend\admin\cron-end-of-day-compile.php"`  
6. Start in: `C:\path\to\public_html\api-backend\admin`

Adjust time if the server is not in Addis Ababa timezone.

## Check that it’s working

- **Log file:** `admin/payment-archives/cron-compile.log` — last run time and result.  
- **Archive page:** Open **View payment archive** in admin; the “End-of-day auto-run” box shows the last run and the cron line to use if not set up.

After setup, the system runs **compile + 24h report emails** by itself every day at the end of day; no manual run and no need to click “Send 24h daily closing report”.
