103 lines
4.3 KiB
PHP
103 lines
4.3 KiB
PHP
<?php
|
|
require_once("wp-load.php");
|
|
global $wpdb;
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$current_date = current_time('mysql');
|
|
$data = json_decode(file_get_contents("php://input"));
|
|
|
|
$id = $data->id;
|
|
$reserved_days = $data->reserved_days;
|
|
$reserved_days_count = count($reserved_days);
|
|
|
|
foreach ($reserved_days as &$timestamp) {
|
|
$seconds = $timestamp / 1000;
|
|
$formatted_date = date( "Y-m-d", strtotime( '+2 hours', strtotime( date("Y-m-d H:i:s", $seconds ) ) ) );
|
|
$timestamp = $formatted_date;
|
|
}
|
|
|
|
$all_booking_types_id = $wpdb->get_results($wpdb->prepare("SELECT booking_type_id FROM wp_bookingtypes WHERE booking_type_id != %d", $id));
|
|
// $booking_dates = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_bookingdates WHERE booking_date > %s", $current_date));
|
|
$booking_dates = $wpdb->get_results($wpdb->prepare("SELECT booking_date, booking_type FROM wp_bookingdates AS wbd INNER JOIN wp_booking AS wb ON wb.booking_id = wbd.booking_id WHERE booking_date > %s", $current_date));
|
|
|
|
$other_rooms_id = [];
|
|
|
|
foreach ($all_booking_types_id as $item_id) {
|
|
$found = false;
|
|
$booking_type_id = $item_id->booking_type_id;
|
|
|
|
if(empty($booking_dates)){
|
|
if(!in_array($booking_type_id, $other_rooms_id)) {
|
|
array_push($other_rooms_id, $booking_type_id);
|
|
}
|
|
} else {
|
|
foreach ($booking_dates as $booking_date) {
|
|
if($booking_type_id == $booking_date->booking_type) {
|
|
$currentDate = new DateTime($booking_date->booking_date);
|
|
$currentDate = $currentDate->format('Y-m-d');
|
|
if(in_array($currentDate, $reserved_days)) {
|
|
$found = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ( !$found ) {
|
|
if(!in_array($booking_type_id, $other_rooms_id)) {
|
|
array_push($other_rooms_id, $booking_type_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
$other_rooms = [];
|
|
|
|
if (!empty($other_rooms_id)) {
|
|
foreach($other_rooms_id as $item) {
|
|
$room_price = 0;
|
|
$room_name = $wpdb->get_var($wpdb->prepare("SELECT title FROM wp_bookingtypes WHERE booking_type_id = %s", $item));
|
|
$room_url = '';
|
|
$room_thumb = '';
|
|
|
|
switch ($item) {
|
|
case 1:
|
|
$room_price = get_field('apartament_prima', 'option') * $reserved_days_count;
|
|
$room_url = '/apartament-prima';
|
|
$room_thumb = '/wp-content/uploads/2024/04/apartament-prima.png';
|
|
break;
|
|
case 2:
|
|
$room_price = get_field('apartament_prima_2', 'option') * $reserved_days_count;
|
|
$room_url = '/apartament-prima-2';
|
|
$room_thumb = '/wp-content/uploads/2024/04/apartament-prima.png';
|
|
break;
|
|
case 3:
|
|
$room_price = get_field('apartament_barcelona', 'option') * $reserved_days_count;
|
|
$room_url = '/apartament-barcelona';
|
|
$room_thumb = '/wp-content/uploads/2024/04/apartament-barcelona.png';
|
|
break;
|
|
case 4:
|
|
$room_price = get_field('apartament_mira', 'option') * $reserved_days_count;
|
|
$room_url = '/apartament-mira';
|
|
$room_thumb = '/wp-content/uploads/2024/04/apartament-mira.png';
|
|
break;
|
|
case 6:
|
|
$room_price = get_field('apartament_new_space', 'option') * $reserved_days_count;
|
|
$room_url = '/apartament-new-space';
|
|
$room_thumb = '/wp-content/uploads/2024/04/apartament-new-space.png';
|
|
break;
|
|
case 7:
|
|
$room_price = get_field('cosy_studio', 'option') * $reserved_days_count;
|
|
$room_url = '/cosy-studio';
|
|
$room_thumb = '/wp-content/uploads/2024/04/20240301_185724-scaled.jpg';
|
|
break;
|
|
}
|
|
|
|
array_push($other_rooms, [
|
|
'name' => $room_name,
|
|
'price' => $room_price,
|
|
'url' => $room_url,
|
|
'room_thumb' => $room_thumb,
|
|
]);
|
|
}
|
|
}
|
|
|
|
echo json_encode($other_rooms);
|
|
} |