$deal_data['room_id'], 'deal_type' => $deal_data['deal_type'], 'deal_name' => $deal_data['deal_name'], 'discount_type' => $deal_data['discount_type'], 'discount_value' => $deal_data['discount_value'], 'date_from' => $deal_data['date_from'], 'date_to' => $deal_data['date_to'], 'min_stay' => isset($deal_data['min_stay']) ? $deal_data['min_stay'] : 1, 'max_stay' => isset($deal_data['max_stay']) ? $deal_data['max_stay'] : 0, 'advance_booking_days' => isset($deal_data['advance_booking_days']) ? $deal_data['advance_booking_days'] : 0, 'is_active' => isset($deal_data['is_active']) ? $deal_data['is_active'] : 1, 'priority' => isset($deal_data['priority']) ? $deal_data['priority'] : 0, 'created_at' => current_time('mysql'), 'updated_at' => current_time('mysql') ); $result = $wpdb->insert( EB_AP_TABLE_DEALS, $data, array('%d', '%s', '%s', '%s', '%f', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s') ); return $result ? $wpdb->insert_id : false; } /** * Update an existing deal * * @param int $deal_id * @param array $deal_data * @return bool */ public function update_deal($deal_id, $deal_data) { global $wpdb; $deal_data['updated_at'] = current_time('mysql'); $result = $wpdb->update( EB_AP_TABLE_DEALS, $deal_data, array('id' => $deal_id), array('%d', '%s', '%s', '%s', '%f', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s'), array('%d') ); return $result !== false; } /** * Delete a deal * * @param int $deal_id * @return bool */ public function delete_deal($deal_id) { global $wpdb; $result = $wpdb->delete( EB_AP_TABLE_DEALS, array('id' => $deal_id), array('%d') ); return $result !== false; } /** * Get deal by ID * * @param int $deal_id * @return array|null */ public function get_deal($deal_id) { global $wpdb; $deal = $wpdb->get_row($wpdb->prepare( "SELECT * FROM " . EB_AP_TABLE_DEALS . " WHERE id = %d", $deal_id ), ARRAY_A); return $deal; } /** * Get deals for a specific room * * @param int $room_id * @param bool $active_only * @return array */ public function get_room_deals($room_id, $active_only = true) { global $wpdb; $where_clause = "room_id = %d"; $params = array($room_id); if ($active_only) { $where_clause .= " AND is_active = 1"; } $deals = $wpdb->get_results($wpdb->prepare( "SELECT * FROM " . EB_AP_TABLE_DEALS . " WHERE $where_clause ORDER BY priority DESC, discount_value DESC", $params ), ARRAY_A); return $deals; } /** * Get applicable deals for a booking * * @param int $room_id * @param string $checkin_date * @param string $checkout_date * @param int $nights * @param string $booking_date * @return array */ public function get_applicable_deals($room_id, $checkin_date, $checkout_date, $nights, $booking_date = null) { if (!$booking_date) { $booking_date = current_time('Y-m-d'); } global $wpdb; $applicable_deals = $wpdb->get_results($wpdb->prepare( "SELECT * FROM " . EB_AP_TABLE_DEALS . " WHERE room_id = %d AND is_active = 1 AND date_from <= %s AND date_to >= %s AND (min_stay = 0 OR min_stay <= %d) AND (max_stay = 0 OR max_stay >= %d) AND (advance_booking_days = 0 OR DATEDIFF(%s, %s) >= advance_booking_days) ORDER BY priority DESC, discount_value DESC", $room_id, $checkin_date, $checkout_date, $nights, $nights, $checkin_date, $booking_date ), ARRAY_A); return $applicable_deals; } /** * Apply deals to a price * * @param float $original_price * @param int $room_id * @param string $checkin_date * @param string $checkout_date * @param int $nights * @param string $booking_date * @return array */ public function apply_deals($original_price, $room_id, $checkin_date, $checkout_date, $nights, $booking_date = null) { $applicable_deals = $this->get_applicable_deals($room_id, $checkin_date, $checkout_date, $nights, $booking_date); if (empty($applicable_deals)) { return array( 'original_price' => $original_price, 'discounted_price' => $original_price, 'discount_amount' => 0, 'applied_deals' => array() ); } $deal_priority = get_option('eb_ap_deal_priority', 'highest_discount'); $best_deal = null; $best_discount = 0; foreach ($applicable_deals as $deal) { $discount_amount = $this->calculate_discount($original_price, $deal); if ($discount_amount > $best_discount) { $best_discount = $discount_amount; $best_deal = $deal; } } if ($best_deal) { $discounted_price = $original_price - $best_discount; return array( 'original_price' => $original_price, 'discounted_price' => max(0, $discounted_price), 'discount_amount' => $best_discount, 'applied_deals' => array($best_deal) ); } return array( 'original_price' => $original_price, 'discounted_price' => $original_price, 'discount_amount' => 0, 'applied_deals' => array() ); } /** * Calculate discount amount for a deal * * @param float $price * @param array $deal * @return float */ private function calculate_discount($price, $deal) { $discount_amount = 0; switch ($deal['discount_type']) { case 'percentage': $discount_amount = $price * ($deal['discount_value'] / 100); break; case 'fixed': $discount_amount = $deal['discount_value']; break; case 'fixed_per_night': // This would need nights parameter passed through $discount_amount = $deal['discount_value']; break; } return $discount_amount; } /** * Get deal types * * @return array */ public function get_deal_types() { return array( 'early_bird' => __('Early Bird Discount', 'eb-advanced-pricing'), 'last_minute' => __('Last Minute Deal', 'eb-advanced-pricing'), 'length_of_stay' => __('Length of Stay Discount', 'eb-advanced-pricing'), 'mobile_rate' => __('Mobile Rate', 'eb-advanced-pricing'), 'secret_deal' => __('Secret Deal', 'eb-advanced-pricing'), 'seasonal' => __('Seasonal Promotion', 'eb-advanced-pricing'), 'weekend_special' => __('Weekend Special', 'eb-advanced-pricing'), 'weekday_special' => __('Weekday Special', 'eb-advanced-pricing'), 'loyalty_discount' => __('Loyalty Discount', 'eb-advanced-pricing'), 'group_discount' => __('Group Discount', 'eb-advanced-pricing'), 'flash_sale' => __('Flash Sale', 'eb-advanced-pricing'), 'custom' => __('Custom Deal', 'eb-advanced-pricing') ); } /** * Get discount types * * @return array */ public function get_discount_types() { return array( 'percentage' => __('Percentage (%)', 'eb-advanced-pricing'), 'fixed' => __('Fixed Amount', 'eb-advanced-pricing'), 'fixed_per_night' => __('Fixed Amount per Night', 'eb-advanced-pricing') ); } /** * Create early bird deal * * @param int $room_id * @param string $name * @param int $advance_days * @param float $discount_value * @param string $discount_type * @param string $date_from * @param string $date_to * @return bool|int */ public function create_early_bird_deal($room_id, $name, $advance_days, $discount_value, $discount_type, $date_from, $date_to) { $deal_data = array( 'room_id' => $room_id, 'deal_type' => 'early_bird', 'deal_name' => $name, 'discount_type' => $discount_type, 'discount_value' => $discount_value, 'date_from' => $date_from, 'date_to' => $date_to, 'advance_booking_days' => $advance_days, 'min_stay' => 1, 'max_stay' => 0, 'is_active' => 1, 'priority' => 5 ); return $this->create_deal($deal_data); } /** * Create length of stay deal * * @param int $room_id * @param string $name * @param int $min_nights * @param float $discount_value * @param string $discount_type * @param string $date_from * @param string $date_to * @return bool|int */ public function create_length_of_stay_deal($room_id, $name, $min_nights, $discount_value, $discount_type, $date_from, $date_to) { $deal_data = array( 'room_id' => $room_id, 'deal_type' => 'length_of_stay', 'deal_name' => $name, 'discount_type' => $discount_type, 'discount_value' => $discount_value, 'date_from' => $date_from, 'date_to' => $date_to, 'min_stay' => $min_nights, 'max_stay' => 0, 'advance_booking_days' => 0, 'is_active' => 1, 'priority' => 3 ); return $this->create_deal($deal_data); } /** * Create mobile rate deal * * @param int $room_id * @param string $name * @param float $discount_value * @param string $discount_type * @param string $date_from * @param string $date_to * @return bool|int */ public function create_mobile_rate_deal($room_id, $name, $discount_value, $discount_type, $date_from, $date_to) { $deal_data = array( 'room_id' => $room_id, 'deal_type' => 'mobile_rate', 'deal_name' => $name, 'discount_type' => $discount_type, 'discount_value' => $discount_value, 'date_from' => $date_from, 'date_to' => $date_to, 'min_stay' => 1, 'max_stay' => 0, 'advance_booking_days' => 0, 'is_active' => 1, 'priority' => 2 ); return $this->create_deal($deal_data); } /** * Create secret deal * * @param int $room_id * @param string $name * @param float $discount_value * @param string $discount_type * @param string $date_from * @param string $date_to * @return bool|int */ public function create_secret_deal($room_id, $name, $discount_value, $discount_type, $date_from, $date_to) { $deal_data = array( 'room_id' => $room_id, 'deal_type' => 'secret_deal', 'deal_name' => $name, 'discount_type' => $discount_type, 'discount_value' => $discount_value, 'date_from' => $date_from, 'date_to' => $date_to, 'min_stay' => 1, 'max_stay' => 0, 'advance_booking_days' => 0, 'is_active' => 1, 'priority' => 8 ); return $this->create_deal($deal_data); } /** * Apply deals to search results * * @param array $results * @param array $search_params * @return array */ public function apply_deals_to_results($results, $search_params) { if (empty($results)) { return $results; } $checkin_date = $search_params['checkin_date']; $checkout_date = $search_params['checkout_date']; $nights = $search_params['nights']; $booking_date = current_time('Y-m-d'); foreach ($results as &$result) { $room_id = $result['room_id']; $original_price = $result['price']; $pricing_result = $this->apply_deals($original_price, $room_id, $checkin_date, $checkout_date, $nights, $booking_date); $result['original_price'] = $pricing_result['original_price']; $result['discounted_price'] = $pricing_result['discounted_price']; $result['discount_amount'] = $pricing_result['discount_amount']; $result['applied_deals'] = $pricing_result['applied_deals']; $result['has_deals'] = !empty($pricing_result['applied_deals']); // Update the main price to discounted price $result['price'] = $pricing_result['discounted_price']; } return $results; } /** * Get deal statistics * * @param int $room_id * @param string $start_date * @param string $end_date * @return array */ public function get_deal_statistics($room_id, $start_date, $end_date) { global $wpdb; $stats = $wpdb->get_results($wpdb->prepare( "SELECT deal_type, COUNT(*) as count, AVG(discount_value) as avg_discount, SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_count FROM " . EB_AP_TABLE_DEALS . " WHERE room_id = %d AND date_from >= %s AND date_to <= %s GROUP BY deal_type", $room_id, $start_date, $end_date ), ARRAY_A); return $stats; } /** * Activate/deactivate deal * * @param int $deal_id * @param bool $active * @return bool */ public function toggle_deal_status($deal_id, $active = true) { global $wpdb; $result = $wpdb->update( EB_AP_TABLE_DEALS, array( 'is_active' => $active ? 1 : 0, 'updated_at' => current_time('mysql') ), array('id' => $deal_id), array('%d', '%s'), array('%d') ); return $result !== false; } /** * Get all deals with optional filters * * @param array $filters * @return array */ public function get_deals($filters = array()) { global $wpdb; $where_conditions = array(); $params = array(); if (!empty($filters['room_id'])) { $where_conditions[] = "room_id = %d"; $params[] = $filters['room_id']; } if (!empty($filters['deal_type'])) { $where_conditions[] = "deal_type = %s"; $params[] = $filters['deal_type']; } if (!empty($filters['is_active'])) { $where_conditions[] = "is_active = %d"; $params[] = $filters['is_active']; } if (!empty($filters['date_from'])) { $where_conditions[] = "date_from >= %s"; $params[] = $filters['date_from']; } if (!empty($filters['date_to'])) { $where_conditions[] = "date_to <= %s"; $params[] = $filters['date_to']; } $where_clause = ""; if (!empty($where_conditions)) { $where_clause = "WHERE " . implode(" AND ", $where_conditions); } $query = "SELECT * FROM " . EB_AP_TABLE_DEALS . " $where_clause ORDER BY priority DESC, created_at DESC"; if (!empty($params)) { $query = $wpdb->prepare($query, $params); } $deals = $wpdb->get_results($query, ARRAY_A); return $deals; } }