# Booking Evolution Tracker

## Overview
Tracks properties and rooms that got booked between consecutive availability checks on Booking.com.

## Files Created
1. **booking_evolution_analytics.php** - Backend analytics logic
2. **booking_evolution_api.php** - JSON API endpoint
3. **report_booking_evolution.php** - Frontend HTML report page

## How It Works

### Property-Level Tracking
- Monitors `total_rooms` field in `properties` table
- Compares availability across multiple `query_date` timestamps
- Negative deltas = rooms got booked (decreased availability)
- Positive deltas = rooms became available (increased availability)

### Room-Level Tracking
- Monitors `has_inventory` in `room_details` table  
- Counts available room types per hotel
- Tracks changes for each specific room type (e.g., "One-Bedroom Apartment")

## API Endpoints

### Get Available Query Dates
```
GET booking_evolution_api.php?action=query_dates&qdf=2025-10-01&qdt=2025-11-01
```

### Get Property Evolution
```
GET booking_evolution_api.php?action=property_evolution&qdf=2025-10-01&qdt=2025-11-01&ci=2025-11-05&co=2025-11-10
```

### Get Room Evolution  
```
GET booking_evolution_api.php?action=room_evolution&qdf=2025-10-01&qdt=2025-11-01&ci=2025-11-05&co=2025-11-10
```

## Filter Parameters
- `qdf` / `qdt` - Query date range (when we checked Booking.com)
- `ci` / `co` - Check-in date range (the actual stay period)
- `name` - Property name search
- `type` - Property type filter (hotel/apartment)
- `ids` - Specific property IDs (comma-separated)
- `selectedDates` - Specific query dates to compare (comma-separated)

## Response Format

### Property Evolution
```json
{
  "ok": true,
  "data": {
    "query_dates": ["2025-10-30", "2025-11-01", "2025-11-02"],
    "properties": [
      {
        "id": 39010,
        "name": "Hotel Name",
        "checkin": "2025-11-05",
        "checkout": "2025-11-06",
        "values": [5, 3, 2],
        "deltas": [null, -2, -1],
        "total_change": -3,
        "type": "hotel"
      }
    ]
  }
}
```

## Usage

1. **Access the Report**
   ```
   http://yourdomain.com/openai/v14With1CRM/reportsV3/report_booking_evolution.php
   ```

2. **Set Filters**
   - Query Date Range: When you want to compare availability checks
   - Check-in Range: Which stay periods to analyze
   - Property filters: Name search, type, etc.

3. **Select Query Dates**
   - Use checkboxes to select specific dates to compare
   - Leave all unchecked to compare all consecutive dates

4. **View Results**
   - Property-level section shows total room changes
   - Room-level section shows changes per room type
   - Red/negative = booked rooms
   - Green/positive = newly available rooms

## Data Requirements

For the report to show meaningful results, the database needs:
- Same properties checked multiple times for the same stay period
- Properties with `total_rooms` values (not NULL)
- Room details with `has_inventory` flags

## Current Database Status
The current database has limited multi-date tracking for the same stay periods. Results may be empty if:
- Properties are only scraped once per stay period
- `total_rooms` is NULL for most properties
- No changes detected between available query dates

## PHP 7 Compatibility
All code is PHP 7 compatible:
- Uses `substr()` instead of `str_starts_with()` / `str_ends_with()`
- Uses `strpos()` instead of `str_contains()`
- Compatible with existing codebase patterns

