HEX
Server: Apache
System: Linux 136-243-153-58.cprapid.com 4.18.0-553.81.1.el8_10.x86_64 #1 SMP Mon Oct 27 11:29:19 EDT 2025 x86_64
User: mytest (1001)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/mytest/.trash/test-weekly-availability-debug.php.19
<?php
/**
 * Debug weekly availability - test API responses
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/plain; charset=utf-8');

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/database/Database.php';
require_once __DIR__ . '/api/services/BookingManagerService.php';

echo "=== Testing Weekly Availability API ===\n\n";

$bmService = new BookingManagerService();
$db = Database::getInstance();

// Get one yacht for testing
$stmt = $db->prepare("SELECT id, booking_manager_id, company_id, name FROM yachts WHERE booking_manager_id IS NOT NULL AND booking_manager_id != '' LIMIT 1");
$result = $stmt->execute();
$yacht = $result->fetchArray(SQLITE3_ASSOC);

if (!$yacht) {
    echo "❌ No yachts found with booking_manager_id\n";
    exit;
}

echo "Testing with yacht: {$yacht['name']} (ID: {$yacht['booking_manager_id']})\n";
echo "Company ID: {$yacht['company_id']}\n\n";

// Test 1: Check next Saturday
$startDate = new DateTime();
$startDate->modify('next Saturday');
$weekFrom = $startDate->format('Y-m-d');
$weekTo = clone $startDate;
$weekTo->modify('+7 days');
$weekToStr = $weekTo->format('Y-m-d');

echo "=== TEST 1: Next week ===\n";
echo "From: $weekFrom\n";
echo "To: $weekToStr\n\n";

echo "Calling getOffers() with:\n";
$params = [
    'yachtId' => $yacht['booking_manager_id'],
    'dateFrom' => $weekFrom,
    'dateTo' => $weekToStr,
    'tripDuration' => 7,
    'flexibility' => 6  // FIXED: Use 6 instead of 0
];
print_r($params);
echo "\n";

$offers = $bmService->getOffers($params);

echo "Response:\n";
if (isset($offers['error'])) {
    echo "❌ ERROR: " . $offers['error'] . "\n";
    if (isset($offers['response'])) {
        echo "API Response: " . substr($offers['response'], 0, 500) . "\n";
    }
} else {
    echo "✅ Offers count: " . count($offers) . "\n";
    if (count($offers) > 0) {
        echo "\nFirst offer:\n";
        print_r($offers[0]);
        
        // Check what we're extracting
        echo "\n=== What we extract ===\n";
        $offer = $offers[0];
        echo "available: TRUE (we have offers)\n";
        echo "price: " . ($offer['price'] ?? 'NULL') . "\n";
        echo "currency: " . ($offer['currency'] ?? 'NULL') . "\n";
        echo "discount: " . ($offer['discount'] ?? 'NULL') . "\n";
        echo "totalPrice: " . ($offer['totalPrice'] ?? 'NULL') . "\n";
    } else {
        echo "❌ No offers returned\n";
        echo "\nFull response:\n";
        print_r($offers);
    }
}

echo "\n\n=== TEST 2: Summer date (2025-06-28) ===\n";
$params2 = [
    'yachtId' => $yacht['booking_manager_id'],
    'dateFrom' => '2025-06-28',
    'dateTo' => '2025-07-05',
    'tripDuration' => 7,
    'flexibility' => 6  // FIXED: Use 6 instead of 0
];
echo "Parameters:\n";
print_r($params2);
echo "\n";

$offers2 = $bmService->getOffers($params2);
if (isset($offers2['error'])) {
    echo "❌ ERROR: " . $offers2['error'] . "\n";
} else {
    echo "✅ Offers count: " . count($offers2) . "\n";
    if (count($offers2) > 0) {
        echo "Price: " . ($offers2[0]['price'] ?? 'NULL') . "\n";
    }
}

echo "\n\n=== TEST 3: What does the cron do? ===\n";
echo "The cron script makes 52 API calls like above for each yacht.\n";
echo "If all return 0 offers, all weeks will be marked as unavailable.\n";
echo "\n";

echo "=== TEST 4: Get ALL offers (no yacht filter) ===\n";
$params3 = [
    'dateFrom' => '2025-06-28',
    'dateTo' => '2025-07-05',
    'tripDuration' => 7,
    'flexibility' => 6
];
echo "Parameters:\n";
print_r($params3);
echo "\n";

$offers3 = $bmService->getOffers($params3);
echo "Result: " . (isset($offers3['error']) ? 'ERROR' : count($offers3) . " offers") . "\n";

if (!isset($offers3['error']) && count($offers3) > 0) {
    echo "\nChecking if Lemon is in results...\n";
    $found = false;
    foreach ($offers3 as $offer) {
        $yid = $offer['yachtId'] ?? $offer['yacht']['id'] ?? 'unknown';
        if ($yid == $yacht['booking_manager_id']) {
            $found = true;
            echo "✅ FOUND Lemon in general offers!\n";
            echo "Price: " . ($offer['price'] ?? 'N/A') . "\n";
            break;
        }
    }
    if (!$found) {
        echo "❌ Lemon NOT found in general offers\n";
        echo "First 5 yacht IDs in results:\n";
        $count = 0;
        foreach ($offers3 as $offer) {
            if ($count >= 5) break;
            $yid = $offer['yachtId'] ?? $offer['yacht']['id'] ?? 'unknown';
            echo "  - $yid\n";
            $count++;
        }
    }
}

echo "\n=== CONCLUSION ===\n";
if (count($offers) > 0) {
    echo "✅ API is working! The issue is elsewhere.\n";
} elseif (!isset($offers3['error']) && count($offers3) > 0) {
    echo "⚠️ API works but filtering by yachtId returns nothing\n";
    echo "   Possible issues:\n";
    echo "   1. Yacht ID is wrong or doesn't match Booking Manager\n";
    echo "   2. Yacht is not published/active\n";
    echo "   3. Yacht belongs to different company\n";
} else {
    echo "❌ API returns no offers at all\n";
    echo "   1. No availability in this date range\n";
    echo "   2. API configuration issue\n";
}

?>