function reindex_booking_db()
 {
     global $wpdb;
     if ($_SERVER['QUERY_STRING'] == 'page=' . WPDEV_BK_PLUGIN_DIRNAME . '/' . WPDEV_BK_PLUGIN_FILENAME . 'wpdev-booking-option&reindex_sort_data=1') {
         $is_show_messages = true;
     } else {
         $is_show_messages = false;
     }
     if ($is_show_messages) {
         // Hide all settings
         ?>
         <style type="text/css" rel="stylesheet" >
             #post_option .meta-box {
                 display:none;
             }
             #post_option .button-primary {
                 display:none;
             }
             #post_option .technical-booking-section {
                 display:block;
             }
         </style>
         <?php 
     }
     if (wpbc_is_field_in_table_exists('booking', 'sort_date') == 0) {
         $simple_sql = "ALTER TABLE {$wpdb->prefix}booking ADD sort_date datetime AFTER booking_id";
         $wpdb->query($simple_sql);
     }
     // Refill the sort date index.
     if (wpbc_is_field_in_table_exists('booking', 'sort_date') != 0) {
         //1. Select  all bookings ID, where sort_date is NULL in wp_booking
         $sql = " SELECT booking_id as id";
         $sql .= " FROM {$wpdb->prefix}booking as bk";
         $sql .= " WHERE sort_date IS NULL";
         $bookings_res = $wpdb->get_results($sql);
         if ($is_show_messages) {
             printf(__('%s Found %s not indexed bookings %s', 'wpdev-booking'), ' ', count($bookings_res), '<br/>');
         }
         if (count($bookings_res) > 0) {
             $id_string = '';
             foreach ($bookings_res as $value) {
                 $id_string .= $value->id . ',';
             }
             $id_string = substr($id_string, 0, -1);
             //2. Select all (FIRST ??) booking_date, where booking_id = booking_id from #1 in wp_bookingdates
             $sql = " SELECT booking_id as id, booking_date as date";
             $sql .= " FROM {$wpdb->prefix}bookingdates as bdt";
             $sql .= " WHERE booking_id IN ( " . $id_string . " ) GROUP BY bdt.booking_id ORDER BY bdt.booking_date ";
             $sort_date_array = $wpdb->get_results($sql);
             if ($is_show_messages) {
                 printf(__('%s Finish getting sort dates. %s', 'wpdev-booking'), ' ', '<br/>');
             }
             //3. Insert  that firtst date into the bookings in wp_booking
             $ii = 0;
             foreach ($sort_date_array as $value) {
                 $ii++;
                 $sql = "UPDATE {$wpdb->prefix}booking as bdt ";
                 $sql .= " SET sort_date = '" . $value->date . "' WHERE booking_id  = " . $value->id . " ";
                 $wpdb->query($sql);
                 if ($is_show_messages) {
                     printf(__('Updated booking: %s', 'wpdev-booking'), $value->id . '  [' . $ii . ' / ' . count($bookings_res) . '] <br/>');
                 }
             }
         }
     }
 }
function renew_NumOfNewBookings($id_of_new_bookings, $is_new = '0', $user_id = 1)
{
    global $wpdb;
    if (count($id_of_new_bookings) > 0) {
        if (wpbc_is_field_in_table_exists('booking', 'is_new') == 0) {
            return 0;
        }
        // do not created this field, so return 0
        $id_of_new_bookings = implode(',', $id_of_new_bookings);
        $id_of_new_bookings = wpbc_clean_string_for_db($id_of_new_bookings);
        //debuge($id_of_new_bookings);
        if ($id_of_new_bookings == 'all') {
            $update_sql = "UPDATE {$wpdb->prefix}booking AS bk SET bk.is_new = {$is_new} ";
            //debuge($update_sql);
            $update_sql = apply_bk_filter('update_sql_for_checking_new_bookings', $update_sql, 0, $user_id);
        } else {
            $update_sql = "UPDATE {$wpdb->prefix}booking AS bk SET bk.is_new = {$is_new} WHERE bk.booking_id IN  ( {$id_of_new_bookings} ) ";
        }
        if (false === $wpdb->query($update_sql)) {
            bk_error('Error during updating status of bookings at DB', __FILE__, __LINE__);
            die;
        }
    }
}