# Cron Setup for Fetch Both Types

This document explains how to set up cron jobs to automatically fetch both individual hotel details and positions.

## Files Created

1. **`fetch_both_types.sh`** - Main shell script that runs both fetches
2. **`fetch_positions_cli.php`** - CLI wrapper for position fetching

## Script Usage

### Basic Usage (with defaults)
```bash
cd /var/www/mssql.danmarcrm.com/openai/v14With1CRM
./fetch_both_types.sh
```

### With Custom Parameters
```bash
./fetch_both_types.sh [from] [days] [stay_nights] [pers] [limit] [checkin] [checkout]
```

**Parameters:**
- `from` - Start date option: `today`, `onemonth`, `twomonth`, etc. (default: `today`)
- `days` - Number of days to fetch (default: `7`)
- `stay_nights` - Number of nights for stay (default: `2`)
- `pers` - Number of persons (default: `2`)
- `limit` - Maximum number of hotels (default: `200`)
- `checkin` - Check-in date (YYYY-MM-DD). If omitted, uses today
- `checkout` - Check-out date (YYYY-MM-DD). If omitted, uses today + stay_nights

**Examples:**
```bash
# Use defaults
./fetch_both_types.sh

# Fetch for 14 days, 2 nights, 2 persons
./fetch_both_types.sh today 14 2 2 200

# Fetch for specific dates
./fetch_both_types.sh today 7 2 2 200 2025-11-01 2025-11-03
```

## Cron Setup

### Edit Crontab
```bash
crontab -e
```

### Example Cron Entries

#### Run daily at 2:00 AM
```cron
0 2 * * * cd /var/www/mssql.danmarcrm.com/openai/v14With1CRM && ./fetch_both_types.sh >> logs2/fetch_cron.log 2>&1
```

#### Run every 6 hours
```cron
0 */6 * * * cd /var/www/mssql.danmarcrm.com/openai/v14With1CRM && ./fetch_both_types.sh >> logs2/fetch_cron.log 2>&1
```

#### Run twice daily (morning and evening)
```cron
0 8,20 * * * cd /var/www/mssql.danmarcrm.com/openai/v14With1CRM && ./fetch_both_types.sh >> logs2/fetch_cron.log 2>&1
```

#### Run with specific parameters (fetch next month, 14 days, 2 nights)
```cron
0 3 * * * cd /var/www/mssql.danmarcrm.com/openai/v14With1CRM && ./fetch_both_types.sh onemonth 14 2 2 200 >> logs2/fetch_cron.log 2>&1
```

### Cron Format
```
* * * * *
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, where 0 and 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
```

## Log Files

The script creates/updates the following log files:

- **`logs2/fetch_both_types.log`** - Main execution log for the shell script
- **`logs2/fetch_booking.log`** - Individual hotel fetch logs
- **`logs2/fetch_positions.log`** - Position fetch logs
- **`logs2/scraper_error.log`** - Puppeteer scraper errors
- **`logs2/__dailystats_error.log`** - Position fetch Puppeteer errors

## What the Script Does

1. **Individual Hotel Fetch** (Runs in background):
   - Executes `daily_booking_scraper_v14.php` using Puppeteer (`scraper.js`)
   - Fetches detailed hotel information and room availability
   - Runs asynchronously (nohup)

2. **Position Fetch** (Runs synchronously):
   - Executes `fetch_positions_cli.php` using Puppeteer (`__dailystats.js`)
   - Fetches hotel search positions
   - Saves to `properties_order` table

## Monitoring

Check if the script is running:
```bash
ps aux | grep fetch_both_types
```

Check recent logs:
```bash
tail -50 logs2/fetch_both_types.log
tail -50 logs2/fetch_booking.log
tail -50 logs2/fetch_positions.log
```

## Troubleshooting

### Script not executing
- Check file permissions: `chmod +x fetch_both_types.sh`
- Ensure PHP path is correct: `which php` (should be `/usr/bin/php`)
- Check cron logs: `grep CRON /var/log/syslog`

### Puppeteer errors
- Check if Node.js is installed: `node --version`
- Check if Puppeteer dependencies are installed: `npm list puppeteer`
- Review error logs: `logs2/scraper_error.log` and `logs2/__dailystats_error.log`

### Database errors
- Verify database credentials in `fetch_positions_cli.php`
- Check database connection: `mysql -u root -p booking_analysis`
- Ensure database exists: `booking_analysis`






