# 🕐 Cron Jobs - Info Hadir

Folder ini berisi script untuk automated tasks yang dijalankan secara terjadwal.

## 📁 Files

### `reset_attendance.php`
**Fungsi:** Reset status kehadiran setiap hari  
**Jadwal:** Setiap hari jam 00:00 (tengah malam)  
**Status:** ✅ Production Ready

**Cara Kerja:**
1. Cek apakah data kehadiran untuk hari ini sudah ada
2. Jika belum, create record baru untuk semua jabatan dengan status "absent"
3. Cleanup data lama (> 30 hari) untuk menghemat storage
4. Log semua aktivitas ke `reset_attendance.log`

### `reset_attendance.log`
**Fungsi:** Log file untuk monitoring cron job  
**Format:** `[YYYY-MM-DD HH:MM:SS] Message`  
**Auto-created:** Ya, otomatis dibuat saat cron pertama kali jalan

## 🚀 Quick Setup

### Via cPanel:
```
Minute: 0
Hour: 0
Day: *
Month: *
Weekday: *
Command: /usr/bin/php /home/username/public_html/cron/reset_attendance.php
```

### Via SSH:
```bash
crontab -e
```
Tambahkan:
```bash
0 0 * * * /usr/bin/php /path/to/project/cron/reset_attendance.php >> /path/to/project/cron/reset_attendance.log 2>&1
```

## 📖 Dokumentasi Lengkap

Lihat: [`docs/CRON_SETUP_GUIDE.md`](../docs/CRON_SETUP_GUIDE.md)

## ✅ Testing

**Test via browser:**
```
https://yourdomain.com/cron/reset_attendance.php
```

**Test via command line:**
```bash
php reset_attendance.php
```

**Expected output:**
```
[2026-02-18 00:00:01] === Starting Daily Attendance Reset ===
[2026-02-18 00:00:01] Database connected successfully
[2026-02-18 00:00:01] Reset date: 2026-02-18
[2026-02-18 00:00:01] Found 9 positions
[2026-02-18 00:00:02] Successfully created 9 attendance records with 'absent' status
[2026-02-18 00:00:02] === Daily Attendance Reset Completed Successfully ===
```

## 🔒 Security

File ini dapat diakses via browser untuk testing, tapi sebaiknya di-restrict untuk production:

**Option 1: Edit `reset_attendance.php`**
```php
// Uncomment line 15-18 untuk restrict akses
```

**Option 2: Buat `.htaccess`**
```apache
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
```

## 📊 Monitoring

**Cek log terakhir:**
```bash
tail -n 20 reset_attendance.log
```

**Cek apakah cron berjalan:**
```bash
grep CRON /var/log/syslog | grep reset_attendance
```

**Cek database:**
```sql
SELECT attendance_date, COUNT(*) as total 
FROM attendance 
GROUP BY attendance_date 
ORDER BY attendance_date DESC 
LIMIT 7;
```

## 🆘 Troubleshooting

### Cron tidak jalan?
1. Cek syntax crontab: `crontab -l`
2. Cek permission file: `chmod +x reset_attendance.php`
3. Cek path PHP: `which php`
4. Cek log: `cat reset_attendance.log`

### Database error?
1. Cek koneksi database di `config/database.php`
2. Cek permission user database
3. Cek apakah tabel `attendance` dan `positions` ada

### Log file tidak dibuat?
1. Cek permission folder: `chmod 755 cron/`
2. Cek permission write: `touch reset_attendance.log`
3. Jalankan manual untuk test

---

**Last Updated:** 18 Februari 2026  
**Version:** 1.0
