| 1 |
<?php |
| 2 |
// Api for get available capacity counts |
| 3 |
class ESHB_Rest_Apis { |
| 4 |
|
| 5 |
|
| 6 |
public function __construct() { |
| 7 |
add_action('rest_api_init', array($this, 'register_routes')); |
| 8 |
} |
| 9 |
|
| 10 |
public function register_routes() { |
| 11 |
register_rest_route('eshb/v1', '/get-available-capacity-counts', array( |
| 12 |
'methods' => 'POST', |
| 13 |
'callback' => array($this, 'get_accomodation_available_capacity_counts'), |
| 14 |
'permission_callback' => '__return_true', |
| 15 |
)); |
| 16 |
} |
| 17 |
|
| 18 |
public function get_accomodation_available_capacity_counts($request){ |
| 19 |
|
| 20 |
|
| 21 |
$params = $request->get_params(); |
| 22 |
|
| 23 |
$type = $params['type']; |
| 24 |
$accomodation_id = $params['accomodationId']; |
| 25 |
$accomodation_metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 26 |
$total_guest_quantity = $accomodation_metaboxes['total_capacity']; |
| 27 |
$selected_rooms_count = !empty($params['rooms']) ? sanitize_text_field( wp_unslash($params['rooms']) ) : 1; |
| 28 |
$start_date = $params['startDate']; |
| 29 |
$end_date = $params['endDate']; |
| 30 |
$start_date = new DateTime($start_date); |
| 31 |
$end_date = new DateTime($end_date); |
| 32 |
|
| 33 |
|
| 34 |
// Verify nonce for security |
| 35 |
// if (!isset($params['nonce']) || !wp_verify_nonce( sanitize_text_field(wp_unslash($params['nonce'])), 'eshb_nonce')) { |
| 36 |
// return rest_ensure_response(array( |
| 37 |
// 'message' => __('Invalid nonce', 'easy-hotel'), |
| 38 |
// 'available' => 0, |
| 39 |
// 'type' => $type, |
| 40 |
// )); |
| 41 |
// } |
| 42 |
|
| 43 |
if(empty($type) || empty($accomodation_id) || empty($start_date) || empty($end_date)){ |
| 44 |
return rest_ensure_response(array( |
| 45 |
'message' => __('Invalid request', 'easy-hotel'), |
| 46 |
'available' => 0, |
| 47 |
'type' => $type, |
| 48 |
)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
$available = 1; |
| 53 |
|
| 54 |
$msg = __('Maximum Capacity', 'easy-hotel'); |
| 55 |
|
| 56 |
if($type == 'room_quantity'){ |
| 57 |
$total = $accomodation_metaboxes['total_rooms']; |
| 58 |
|
| 59 |
$available = $this->get_available_room_count_by_date_range($accomodation_id, $start_date->format('Y-m-d'), $end_date->format('Y-m-d')); |
| 60 |
|
| 61 |
if($available < 1){ |
| 62 |
$msg = __('Available Room', 'easy-hotel'); |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
if($type == 'adult_quantity'){ |
| 68 |
|
| 69 |
if(!empty($accomodation_metaboxes['adult_capacity'])){ |
| 70 |
$available = $accomodation_metaboxes['adult_capacity']; |
| 71 |
$available = $available * $selected_rooms_count; |
| 72 |
}else{ |
| 73 |
$childrenQuantity = !empty($_POST['childrenQuantity']) ? sanitize_text_field( wp_unslash( $_POST['childrenQuantity'] ) ) : 0; |
| 74 |
|
| 75 |
$total_capacity = $accomodation_metaboxes['total_capacity']; |
| 76 |
|
| 77 |
$available = $total_capacity - $childrenQuantity; |
| 78 |
|
| 79 |
$available = $available * $selected_rooms_count; |
| 80 |
|
| 81 |
$msg = __('Maximum Adult and Children Capacity', 'easy-hotel'); |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
if($type == 'children_quantity'){ |
| 87 |
|
| 88 |
$total_capacity = $accomodation_metaboxes['total_capacity']; |
| 89 |
|
| 90 |
if(!empty($accomodation_metaboxes['children_capacity'])){ |
| 91 |
$available = $accomodation_metaboxes['children_capacity']; |
| 92 |
$available = $available * $selected_rooms_count; |
| 93 |
}else{ |
| 94 |
$childrenQuantity = !empty($_POST['adultQuantity']) ? sanitize_text_field( wp_unslash( $_POST['adultQuantity'] ) ) : 0; |
| 95 |
|
| 96 |
$available = $total_capacity - $childrenQuantity; |
| 97 |
|
| 98 |
$available = $available * $selected_rooms_count; |
| 99 |
|
| 100 |
$msg = __('Maximum Adult and Children Capacity', 'easy-hotel'); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
if($type == 'extra_bed_quantity'){ |
| 106 |
$available = $accomodation_metaboxes['total_extra_beds']; |
| 107 |
$available = $available * $selected_rooms_count; |
| 108 |
} |
| 109 |
|
| 110 |
if($type == 'service-quantity'){ |
| 111 |
$available = $total_guest_quantity; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if(($type != 'room_quantity') && (empty($available) || $available < 1)){ |
| 116 |
$available = $total; |
| 117 |
} |
| 118 |
|
| 119 |
$msg = $type != 'adult_quantity' || $type != 'children_quantity' ? $msg . ' ' . $available : $msg; |
| 120 |
|
| 121 |
return rest_ensure_response(array( |
| 122 |
'message' => $msg, |
| 123 |
'available' => $available, |
| 124 |
'type' => $type, |
| 125 |
)); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public function get_available_room_count_by_date_range($accomodation_id, $start_date, $end_date) { |
| 132 |
$accomodation_id = intval($accomodation_id); |
| 133 |
$start_date_obj = new DateTime($start_date); |
| 134 |
$end_date_obj = new DateTime($end_date); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if ($end_date_obj < $start_date_obj) { |
| 140 |
return new WP_Error('invalid_date_range', 'End date cannot be earlier than start date.'); |
| 141 |
} |
| 142 |
|
| 143 |
// Fetch total rooms |
| 144 |
$accomodation_metas = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 145 |
$total_rooms = isset($accomodation_metas['total_rooms']) ? intval($accomodation_metas['total_rooms']) : 1; |
| 146 |
|
| 147 |
// Initialize bookings count array |
| 148 |
$bookings_per_date = []; |
| 149 |
|
| 150 |
// Query all bookings |
| 151 |
$bookings_args = [ |
| 152 |
'post_type' => 'eshb_booking', |
| 153 |
'posts_per_page' => -1, |
| 154 |
'post_status' => 'publish', |
| 155 |
'meta_query' => [ |
| 156 |
[ |
| 157 |
'key' => 'eshb_booking_metaboxes', |
| 158 |
'compare' => 'EXISTS', |
| 159 |
], |
| 160 |
], |
| 161 |
'fields' => 'ids', |
| 162 |
]; |
| 163 |
$bookings = new WP_Query($bookings_args); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if ($bookings->have_posts()) { |
| 168 |
while ($bookings->have_posts()) { |
| 169 |
$bookings->the_post(); |
| 170 |
$booking_id = get_the_ID(); |
| 171 |
$booking_metas = get_post_meta($booking_id, 'eshb_booking_metaboxes', true); |
| 172 |
$booking_accomodation_id = $booking_metas['booking_accomodation_id']; |
| 173 |
$booking_status = isset($booking_metas['booking_status']) ? $booking_metas['booking_status'] : ''; |
| 174 |
$booked_accomodation_ids = []; |
| 175 |
$valid_statuses = array('pending', 'processing', 'on-hold', 'completed'); |
| 176 |
if ( |
| 177 |
is_array($booking_metas) && |
| 178 |
intval($booking_accomodation_id) === $accomodation_id && |
| 179 |
isset($booking_metas['booking_start_date'], $booking_metas['booking_end_date']) && |
| 180 |
in_array($booking_status, $valid_statuses) |
| 181 |
) { |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
$booking_start = new DateTime($booking_metas['booking_start_date']); |
| 187 |
$booking_end = new DateTime($booking_metas['booking_end_date']); |
| 188 |
$booked_rooms = isset($booking_metas['room_quantity']) ? intval($booking_metas['room_quantity']) : 1; |
| 189 |
|
| 190 |
$period = new DatePeriod($booking_start, new DateInterval('P1D'), (clone $booking_end)->modify('+1 day')); |
| 191 |
|
| 192 |
foreach ($period as $date) { |
| 193 |
$formatted = $date->format('Y-m-d'); |
| 194 |
if (!isset($bookings_per_date[$formatted])) { |
| 195 |
$bookings_per_date[$formatted] = 0; |
| 196 |
} |
| 197 |
$bookings_per_date[$formatted] += $booked_rooms; |
| 198 |
} |
| 199 |
if(count($bookings_per_date) > 1){ |
| 200 |
array_pop($bookings_per_date); |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
} |
| 205 |
wp_reset_postdata(); |
| 206 |
} |
| 207 |
|
| 208 |
// Check max booked room in the given range |
| 209 |
$range_period = new DatePeriod($start_date_obj, new DateInterval('P1D'), (clone $end_date_obj)->modify('+1 day')); |
| 210 |
$max_booked_in_range = 0; |
| 211 |
|
| 212 |
foreach ($range_period as $date) { |
| 213 |
$formatted = $date->format('Y-m-d'); |
| 214 |
$booked = isset($bookings_per_date[$formatted]) ? $bookings_per_date[$formatted] : 0; |
| 215 |
if ($booked > $max_booked_in_range) { |
| 216 |
$max_booked_in_range = $booked; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$available_rooms = max(0, $total_rooms - $max_booked_in_range); |
| 221 |
|
| 222 |
return $available_rooms; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
new ESHB_Rest_Apis(); |
| 229 |
|
| 230 |
|