Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • BKAP
    • Addons
    • Admin-Bookings
    • Attribute-Lockout
    • Block-Pricing
    • Booking-Box
    • Booking-Confirmation
    • Booking-Process
    • Cancel-Order
    • Cart
    • Checkout-Process
    • Core
    • Date-Time
    • Emails
    • Functions
    • Global-Function
    • Global-Settings
    • Google-Calendar-Sync
    • ICS
    • Import-Booking
    • Labels
    • License
    • Menus
    • Meta-Boxes
    • Reschedule
    • Resources
    • Search-Widget
    • Special-Price
    • Uninstall
    • Validation
    • Variation-Lockout
    • Vendors
      • Dokan
      • WC-Vendors
    • View-Bookings
  • None

Classes

  • bkap_addon_compatibility_class
  • bkap_admin_bookings
  • bkap_approve_booking
  • bkap_attributes
  • bkap_block_booking
  • BKAP_Booking
  • bkap_booking_box_class
  • bkap_booking_confirmation
  • bkap_booking_process
  • BKAP_Bookings_View
  • bkap_calendar_sync
  • bkap_cancel_order
  • bkap_cart
  • bkap_checkout
  • bkap_common
  • BKAP_Customer_Meta_Box
  • BKAP_Details_Meta_Box
  • bkap_dokan_calendar_class
  • bkap_dokan_class
  • bkap_dokan_orders_class
  • bkap_dokan_products_class
  • bkap_edit_bookings_class
  • BKAP_Email_Booking_Cancelled
  • BKAP_Email_Booking_Confirmed
  • BKAP_Email_Booking_Rescheduled_Admin
  • BKAP_Email_Imported_Event
  • BKAP_Email_Manager
  • BKAP_Email_New_Booking
  • BKAP_Gcal
  • BKAP_Gcal_Event
  • bkap_gcal_sync_settings
  • bkap_global_settings
  • BKAP_Google_Events_View
  • bkap_ics
  • bkap_license
  • bkap_load_scripts_class
  • BKAP_OPC_Addon
  • BKAP_Payment_Gateway
  • BKAP_Product_Resource
  • bkap_rescheduled_order_class
  • BKAP_Resource_Details_Meta_Box
  • BKAP_Save_Meta_Box
  • bkap_special_booking_price
  • bkap_timeslot_price
  • bkap_validation
  • bkap_variations
  • BKAP_Vendors
  • bkap_wc_vendors
  • bkap_wcv_product
  • Booking_Information_In_Voucher_Template
  • Class_Bkap_Product_Resource
  • Custom_WooCommerce_Widget_Product_Search
  • EDD_BOOK_Plugin_Updater
  • global_menu
  • import_bookings
  • product_gcal_settings
  • view_bookings
  • WAPBK_Import_Bookings_Table
  • WAPBK_View_Bookings_Table
  • woocommerce_booking

Functions

  • addDayswithdate
  • bkap_400_create_meta
  • bkap_400_recurring_data
  • bkap_400_update_db_tab
  • bkap_400_update_enable_specific
  • bkap_400_update_enable_week_blocking
  • bkap_400_update_holidays
  • bkap_400_update_ranges
  • bkap_400_update_recurring_lockout
  • bkap_400_update_settings
  • bkap_400_update_special
  • bkap_400_update_specific
  • bkap_410_create_meta
  • bkap_410_update_block_pricing_option_values_in_new_way
  • bkap_410_update_fixed_blocks
  • bkap_410_update_price_ranges
  • bkap_410_update_settings
  • bkap_420_add_gcal_posts
  • bkap_420_add_gcal_tour_posts
  • bkap_420_gcal_batch_size
  • bkap_420_gcal_meta
  • bkap_420_update_settings
  • bkap_add_days_to_date
  • bkap_array_of_given_date_range
  • bkap_booked_resources
  • bkap_booking_posts_for_resource
  • bkap_calculate_bookings_for_resource
  • bkap_check_booking_available
  • bkap_check_day_booking_available
  • bkap_check_for_max_date
  • bkap_check_for_min_date
  • bkap_check_holiday
  • bkap_check_in_custom_holiday_range
  • bkap_check_resource_available
  • bkap_check_resource_booked_in_date_range
  • bkap_db_420_1
  • bkap_db_420_2
  • bkap_delete_event_from_gcal
  • bkap_delete_folder_and_files
  • bkap_delete_options
  • bkap_delete_tables
  • bkap_find_matching_product_variation
  • bkap_generate_bookable_data
  • bkap_get_book_arrays
  • bkap_get_book_t
  • bkap_get_booked
  • bkap_get_booked_checkout
  • bkap_get_custom_posts
  • bkap_get_dateToCal
  • bkap_get_day_between_Week
  • bkap_get_db_count
  • bkap_get_escapeString
  • bkap_get_lockout
  • bkap_get_month_range
  • bkap_get_post_meta
  • bkap_get_posted_availability
  • bkap_get_total_timeslot_maximum_booking
  • bkap_get_week_range
  • bkap_import_events_cron
  • bkap_insert_event_to_gcal
  • bkap_manual_db_update_v420
  • bkap_orderby_date_key
  • bkap_save_resources
  • bkap_sort_by_from_time
  • bkap_update_po_file
  • bkap_vendors_get_bookings
  • bkap_widgets_init
  • bkapSortByPriority
  • calback_bkap_max_date
  • check_in_fixed_block_booking
  • check_in_range
  • check_in_range_abp
  • check_in_range_holidays
  • check_in_range_weekdays
  • create_posts
  • get_availability_for_date
  • get_booked_multiple
  • get_bookings_for_date
  • get_bookings_for_range
  • get_date_lockout
  • get_slot_availability
  • get_slot_lockout
  • get_standard_decimal_thousand_separator_price
  • is_booking_active
  • woocommerce_bkap_add_cron_schedule
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 
<?php

/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for operations related to Checkout process
 *
 * @author   Tyche Softwares
 * @package  BKAP/Checkout-Process
 * @category Classes
 */

include_once( 'bkap-common.php' );

/**
 * Class for all the operations related to order completion process
 * 
 * @since 1.7.0
 */
class bkap_checkout{

    /**
     * Hide the hardcoded item meta records frm being 
     * displayed on the admin orders page
     * 
     * @param array $arr array containing meta fields that are hidden on Admin Order Page
     * 
     * @return array Hidden Fields Array modified
     * 
     * @since 1.7.0
     * @since 4.5.0 Added field for WooCommerce Product Addons
     * @since 4.7.0 Added field for Resources
     * 
     * @hook woocommerce_hidden_order_itemmeta
     */ 

    public static function bkap_hidden_order_itemmeta( $arr ){
        $arr[] = '_wapbk_checkout_date';
        $arr[] = '_wapbk_booking_date';
        $arr[] = '_wapbk_time_slot';
        $arr[] = '_wapbk_booking_status';
        $arr[] = '_gcal_event_reference';
        $arr[] = '_wapbk_wpa_prices';       // This item meta is used for calculating addon prices while rescheduling
        $arr[] = '_resource_id';
        
        return $arr;
    }
    
    /**
     * Updated the availability/bookings left for a product when an order is placed.
     * 
     * @param int $order_id Order ID
     * @param int $post_id Product ID
     * @param int $parent_id Parent Product ID in case of Grouped Products
     * @param int $quantity Quantity
     * @param array $booking_data Booking data array
     * @return array An array containing the list of products IDs for whom availability was updated.
     * 
     * @globals mixed $wpdb
     * 
     * @since 1.7.0
     */

    public static function bkap_update_lockout( $order_id, $post_id, $parent_id, $quantity, $booking_data, $called_from = '' ) {
        global $wpdb;
        
        $details = array();
        
        if ( isset( $booking_data[ 'hidden_date' ] ) && '' != $booking_data[ 'hidden_date' ] ) {
            $hidden_date   =   $booking_data[ 'hidden_date' ];
            $date_query    =   date( 'Y-m-d', strtotime( $hidden_date ) );
            
            $booking_settings   = get_post_meta( $post_id, 'woocommerce_booking_settings', true );
            if( array_key_exists( 'hidden_date_checkout', $booking_data ) ) {
                $date_checkout        =   $booking_data[ 'hidden_date_checkout' ];
                $date_checkout_query  =   date( 'Y-m-d', strtotime( $date_checkout ) );
            }
            
            if ( isset( $booking_settings[ 'booking_enable_multiple_day' ] )&& $booking_settings[ 'booking_enable_multiple_day' ] == 'on' ) {
                for ( $i = 0; $i < $quantity; $i++ ) {
                    $query = "INSERT INTO `" . $wpdb->prefix."booking_history`
                                                     (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                                     VALUES (
                                                     '".$post_id."',
                                                     '',
                                                     '".$date_query."',
                                                     '".$date_checkout_query."',
                                                     '',
                                                     '',
                                                     '0',
                                                     '0' )";
                    $wpdb->query( $query );
                    $new_booking_id  = $wpdb->insert_id;
                    //Insert records for parent products - Grouped Products
                    if ( isset( $parent_id ) && $parent_id != '' ) {
                        $query_parent   =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                        (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                                        VALUES (
                                                        '".$parent_id."',
                                                        '',
                                                        '".$date_query."',
                                                        '".$date_checkout_query."',
                                                        '',
                                                        '',
                                                        '0',
                                                        '0' )";
                        $wpdb->query( $query_parent );
                    }
                }
                if( $called_from != 'admin' ) {
                    bkap_checkout::bkap_update_booking_order_history( $order_id, $new_booking_id );
                } else {
                    bkap_checkout::bkap_update_booking_order_history( $order_id, $new_booking_id, 'update' );
                }
            } else {
                if( isset( $booking_data[ 'time_slot' ] ) && $booking_data[ 'time_slot' ] != "" ) {
                    $time_select              =   $booking_data[ 'time_slot' ];
                    $time_exploded            =   explode( "-", $time_select );
                    $query_from_time  =   date( 'G:i', strtotime( $time_exploded[0] ) );
                    $from_hi = date( 'H:i', strtotime( $query_from_time ) );
                    
                    if( isset( $time_exploded[1] ) ){
                        $query_to_time       = date( 'G:i', strtotime( $time_exploded[1] ) );
                        $to_hi = date( 'H:i', strtotime( $query_to_time ) );
                    }
                    
                    if( isset( $query_to_time ) && $query_to_time != "" ) {
                        // same product time slots over lapping
                        $booking_settings   =   get_post_meta( $post_id, 'woocommerce_booking_settings' , true );
                        $query              =   "SELECT from_time, to_time  FROM `".$wpdb->prefix."booking_history`
                                                            WHERE post_id = '".$post_id."' AND
                                                            start_date = '".$date_query."' AND
                                                            status !=  'inactive' ";
                        $get_all_time_slots = $wpdb->get_results( $query );
                    
                        // this is possible when we are trying to create an order while importing events by GCal
                        if ( ! isset( $get_all_time_slots ) || ( isset( $get_all_time_slots ) && count( $get_all_time_slots ) == 0 ) ) {
                    
                            $weekday = date( 'w', strtotime( $date_query ) );
                            $weekday = 'booking_weekday_' . $weekday;
                    
                            $base_query = "SELECT * FROM `" . $wpdb->prefix . "booking_history`
                                               WHERE post_id = %d
                                               AND weekday = %s
                                               AND start_date = '0000-00-00'
                                               AND status != 'inactive'";
                    
                            $get_base = $wpdb->get_results( $wpdb->prepare( $base_query, $post_id, $weekday ) );
                    
                            foreach ( $get_base as $key => $value ) {
                                $insert_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                        (post_id,weekday,start_date,from_time,to_time,total_booking,available_booking)
                                                        VALUES (
                                                       '" . $post_id . "',
                                                       '" . $weekday . "',
                                                       '" . $date_query . "',
                                                       '" . $value->from_time ."',
                                                       '" . $value->to_time . "',
                                                       '" . $value->total_booking . "',
                                                       '" . $value->available_booking . "' ) ";
                    
                                $wpdb->query( $insert_records );
                                if ( isset( $parent_id ) && $parent_id != '' ) {
                                    $insert_parent_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                        (post_id,weekday,start_date,from_time,to_time,total_booking,available_booking)
                                                        VALUES (
                                                       '" . $parent_id . "',
                                                       '" . $weekday . "',
                                                       '" . $date_query . "',
                                                       '" . $value->from_time ."',
                                                       '" . $value->to_time . "',
                                                       '" . $value->total_booking . "',
                                                       '" . $value->available_booking . "' ) ";
                    
                                    $wpdb->query( $insert_parent_records );
                                }
                            }
                        }
                        
                        foreach( $get_all_time_slots as $time_slot_key => $time_slot_value ){
                    
                            $query_from_time_time_stamp         = strtotime( $query_from_time );
                            $query_to_time_time_stamp           = strtotime( $query_to_time );
                            $time_slot_value_from_time_stamp    = strtotime( $time_slot_value->from_time );
                            $time_slot_value_to_time_stamp      = strtotime( $time_slot_value->to_time );
                    
                            if( $query_to_time_time_stamp > $time_slot_value_from_time_stamp && $query_from_time_time_stamp < $time_slot_value_to_time_stamp ){
                    
                                if ( $time_slot_value_from_time_stamp != $query_from_time_time_stamp || $time_slot_value_to_time_stamp != $query_to_time_time_stamp ) {
                                    $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                            SET available_booking = available_booking - ".$quantity."
                                                            WHERE post_id = '".$post_id."' AND
                                                            start_date = '".$date_query."' AND
                                                            from_time = '".$time_slot_value->from_time."' AND
                                                            to_time = '".$time_slot_value->to_time."' AND
                                                            status != 'inactive' AND
                                                            total_booking > 0";
                                    $wpdb->query( $query );
                                }
                            }
                        }
                    
                        $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET available_booking = available_booking - ".$quantity."
                                                WHERE post_id = '".$post_id."' AND
                                                start_date = '".$date_query."' AND
                                                TIME_FORMAT( from_time, '%H:%i' ) = '".$from_hi."' AND
                                                TIME_FORMAT( to_time, '%H:%i' ) = '".$to_hi."' AND
                                                status != 'inactive' AND
                                                total_booking > 0";
                        $wpdb->query( $query );
                    
                        //Update records for parent products - Grouped Products
                        if ( isset( $parent_id ) && $parent_id != '' ) {
                            $query     =   "UPDATE `".$wpdb->prefix."booking_history`
                                                        SET available_booking = available_booking - ".$quantity."
                                                        WHERE post_id = '".$parent_id."' AND
                                                        start_date = '".$date_query."' AND
                                                        from_time = '".$query_from_time."' AND
                                                        to_time = '".$query_to_time."' AND
                                                        status != 'inactive' AND
                                                        total_booking > 0";
                            $wpdb->query( $query );
                        }
                        $select         =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                        WHERE post_id = %d AND
                                                        start_date = %s AND
                                                        from_time = %s AND
                                                        to_time = %s AND
                                                        status != 'inactive' ";
                        $select_results =   $wpdb->get_results( $wpdb->prepare( $select, $post_id, $date_query, $query_from_time, $query_to_time ) );
                        foreach( $select_results as $k => $v ) {
                            $details[ $post_id ] = $v;
                        }
                    } else {

                        //
                        $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET available_booking = available_booking - ".$quantity."
                                                WHERE post_id = '".$post_id."' AND
                                                start_date = '".$date_query."' AND
                                                from_time = '".$query_from_time."' AND
                                                status != 'inactive' AND
                                                total_booking > 0";
                        $updated = $wpdb->query( $query );
                        if ( 0 == $updated ) {
                            $weekday = date( 'w', strtotime( $date_query ) );
                            $weekday = 'booking_weekday_' . $weekday;
                            $base_query = "SELECT * FROM `" . $wpdb->prefix . "booking_history`
                                               WHERE post_id = %d
                                               AND weekday = %s
                                               AND start_date = '0000-00-00'
                                               AND status != 'inactive'
                                               AND total_booking > 0";
                    
                            $get_base = $wpdb->get_results( $wpdb->prepare( $base_query, $post_id, $weekday ) );
                    
                            foreach ( $get_base as $key => $value ) {
                                $insert_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                        (post_id,weekday,start_date,from_time,total_booking,available_booking)
                                                        VALUES (
                                                       '" . $post_id . "',
                                                       '" . $weekday . "',
                                                       '" . $date_query . "',
                                                       '" . $value->from_time ."',
                                                       '" . $value->total_booking . "',
                                                       '" . $value->available_booking . "' ) ";
                    
                                $wpdb->query( $insert_records );
                    
                                if ( isset( $parent_id ) && $parent_id != '' ) {
                    
                                    $insert_parent_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                        (post_id,weekday,start_date,from_time,total_booking,available_booking)
                                                        VALUES (
                                                       '" . $parent_id . "',
                                                       '" . $weekday . "',
                                                       '" . $date_query . "',
                                                       '" . $value->from_time ."',
                                                       '" . $value->total_booking . "',
                                                       '" . $value->available_booking . "' ) ";
                    
                                    $wpdb->query( $insert_parent_records );
                    
                                }
                            }
                            
                            $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET available_booking = available_booking - ".$quantity."
                                                WHERE post_id = '".$post_id."' AND
                                                start_date = '".$date_query."' AND
                                                TIME_FORMAT( from_time, '%H:%i' ) = '".$from_hi."' AND                                                
                                                status != 'inactive' AND
                                                total_booking > 0";
                            $wpdb->query( $query );
                        }
                        
                        //Update records for parent products - Grouped Products
                        if ( isset( $parent_id ) && $parent_id != '' ) {
                            $query     =   "UPDATE `".$wpdb->prefix."booking_history`
                                                        SET available_booking = available_booking - ".$quantity."
                                                        WHERE post_id = '".$parent_id."' AND
                                                        start_date = '".$date_query."' AND
                                                        from_time = '".$query_from_time."' AND
                                                        status != 'inactive' AND
                                                        total_booking > 0";
                            $wpdb->query( $query );
                        }
                        $select         =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                        WHERE post_id =  %d AND
                                                        start_date = %s AND
                                                        from_time = %s AND
                                                        status != 'inactive'";
                        $select_results =   $wpdb->get_results( $wpdb->prepare( $select, $post_id, $date_query, $query_from_time ) );
                        foreach( $select_results as $k => $v ) {
                            $details[$post_id] = $v;
                        }
                    }
                } else {
                    $query   =   "UPDATE `".$wpdb->prefix."booking_history`
                                             SET available_booking = available_booking - ".$quantity."
                                             WHERE post_id = '".$post_id."' AND
                                             start_date = '".$date_query."' AND
                                             status != 'inactive' AND
                                             total_booking > 0";
                    $updated = $wpdb->query( $query );
                    if ( $updated == 0 ) {
                    
                        $weekday = date( 'w', strtotime( $date_query ) );
                        $weekday = 'booking_weekday_' . $weekday;
                    
                        $base_query = "SELECT * FROM `" . $wpdb->prefix . "booking_history`
                                               WHERE post_id = %d
                                               AND weekday = %s
                                               AND start_date = '0000-00-00'
                                               AND status != 'inactive'
                                               AND total_booking > 0";
                    
                        $get_base = $wpdb->get_results( $wpdb->prepare( $base_query, $post_id, $weekday ) );
    
                        if ( isset( $get_base ) && count( $get_base ) > 0 ) {
                            foreach ( $get_base as $key => $value ) {
                                $new_availability = $value->available_booking - $quantity;
                                $insert_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                            (post_id,weekday,start_date,total_booking,available_booking)
                                                            VALUES (
                                                           '" . $post_id . "',
                                                           '" . $weekday . "',
                                                           '" . $date_query . "',
                                                           '" . $value->total_booking . "',
                                                           '" . $new_availability . "' ) ";
                        
                                $wpdb->query( $insert_records );
                        
                                if ( isset( $parent_id ) && $parent_id != '' ) {
                        
                                    $insert_parent_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                            (post_id,weekday,start_date,total_booking,available_booking)
                                                            VALUES (
                                                           '" . $parent_id . "',
                                                           '" . $weekday . "',
                                                           '" . $date_query . "',
                                                           '" . $value->total_booking . "',
                                                           '" . $new_availability . "' ) ";
                        
                                    $wpdb->query( $insert_parent_records );
                        
                                }
                            }
                        } else {
                            // this might happen when gcal is being used and the date has unlimited booking lockout
                             
                            $unlimited_query = "SELECT * FROM `" . $wpdb->prefix . "booking_history`
                                       WHERE post_id = %d
                                       AND start_date = %s
                                       AND status != 'inactive'
                                       AND total_booking = 0";
                             
                            $unlimited_results = $wpdb->get_results( $wpdb->prepare( $unlimited_query, $post_id, $date_query ) );
                            
                            if ( isset( $unlimited_results ) && count( $unlimited_results ) > 0 ) {
                            } else {
                            
                                $base_query = "SELECT * FROM `" . $wpdb->prefix . "booking_history`
                                               WHERE post_id = %d
                                               AND weekday = %s
                                               AND start_date = '0000-00-00'
                                               AND status != 'inactive'
                                               AND total_booking = 0";
                                 
                                $get_base = $wpdb->get_results( $wpdb->prepare( $base_query, $post_id, $weekday ) );
                                 
                                if ( isset( $get_base ) && count( $get_base ) > 0 ) {
                                    foreach( $get_base as $base_key => $value ) {
                                        $insert_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                (post_id,weekday,start_date,total_booking,available_booking)
                                                                VALUES (
                                                               '" . $post_id . "',
                                                               '" . $weekday . "',
                                                               '" . $date_query . "',
                                                               '" . $value->total_booking . "',
                                                               '" . $value->available_booking . "' ) ";
                                         
                                        $wpdb->query( $insert_records );
                                         
                                        if ( isset( $parent_id ) && $parent_id != '' ) {
                                             
                                            $insert_parent_records = "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                (post_id,weekday,start_date,total_booking,available_booking)
                                                                VALUES (
                                                               '" . $parent_id . "',
                                                               '" . $weekday . "',
                                                               '" . $date_query . "',
                                                               '" . $value->total_booking . "',
                                                               '" . $value->available_booking . "' ) ";
                                             
                                            $wpdb->query( $insert_parent_records );
                                             
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //Update records for parent products - Grouped Products
                    if ( isset( $parent_id ) && $parent_id != '' ) {
                        $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET available_booking = available_booking - ".$quantity."
                                                WHERE post_id = '".$parent_id."' AND
                                                start_date = '".$date_query."' AND
                                                status != 'inactive' AND
                                                total_booking > 0";
                        $wpdb->query( $query );
                        $update_one_time_singe_day_parent = 'true';
                    }
                }
            } 
            
            if ( isset ( $booking_settings['booking_enable_multiple_day'] ) && $booking_settings['booking_enable_multiple_day'] != 'on'  ) {
                if( $called_from != 'admin' ) {
                    
                    if ( isset ( $booking_data['time_slot'] ) && $booking_data['time_slot'] != "" && ( array_key_exists ( 'date', $booking_data ) || array_key_exists ( 'booking_date', $booking_data ) ) ) {
                        if ( isset ( $query_to_time ) && $query_to_time != '' ) {
                            $order_select_query     =   "SELECT id FROM `".$wpdb->prefix."booking_history`
                                                                    WHERE post_id = %d AND
                                                                    start_date = %s AND
                                                                    TIME_FORMAT( from_time,'%H:%i' ) = %s AND
                                                                    TIME_FORMAT ( to_time, '%H:%i' ) = %s AND
                                                                    status = ''";
                            $order_results = $wpdb->get_results ( $wpdb->prepare ( $order_select_query, $post_id, $date_query, $from_hi, $to_hi ) );
                        }  else {
                            $order_select_query     =   "SELECT id FROM `".$wpdb->prefix."booking_history`
                                                                    WHERE post_id = %d AND
                                                                    start_date = %s AND
                                                                    TIME_FORMAT( from_time,'%H:%i' ) = %s AND
                                                                    status = ''";
                            $order_results = $wpdb->get_results ( $wpdb->prepare ( $order_select_query, $post_id, $date_query, $from_hi ) );
                        }
                    } else {
                        $order_select_query  =   "SELECT id FROM `".$wpdb->prefix."booking_history`
                                                             WHERE post_id = %d AND
                                                             start_date = %s AND
                                                             status = ''";
                        $order_results = $wpdb->get_results ( $wpdb->prepare ( $order_select_query, $post_id, $date_query ) );
                    }
                    
                    $j = 0;

                    $update_or_insert = "";
                    // Rescheduling from My Account page hence we have to update the records.
                    if ( $called_from == 'view-order' ){
                        $update_or_insert = 'update';
                    }
                    foreach ( $order_results as $k => $v ) {
                        $booking_id  =   $order_results[$j]->id;
                        bkap_checkout::bkap_update_booking_order_history( $order_id, $booking_id, $update_or_insert );
                        $j++;
                    }
                }
            }

            do_action( 'bkap_update_lockout', $order_id, $post_id, $parent_id, $quantity, $booking_data );
        }
        return $details;
    }
    
    /**
     * Update Booking Order History Table
     * 
     * @param int $order_id Order ID
     * @param int $booking_id Booking ID
     * @param string $query type of query to perform
     * 
     * @globals mixed $wpdb
     * 
     * @since 2.5.0
     */

    public static function bkap_update_booking_order_history( $order_id, $booking_id, $query = 'insert' ) {
        global $wpdb;
        
        if ( isset( $query ) && 'update' == $query ) {
            $order_query = "UPDATE `" . $wpdb->prefix. "booking_order_history`
                           SET booking_id = '" . $booking_id . "'
                           WHERE order_id = '" . $order_id . "'";
        } else {
            $order_query  =   "INSERT INTO `" . $wpdb->prefix . "booking_order_history`
                                              (order_id,booking_id)
                                              VALUES (
                                              '" . $order_id . "',
                                              '" . $booking_id . "' )";
        }
        $wpdb->query( $order_query );
    }

    /**
    * Creates & returns a booking post meta record
    * array to be inserted in post meta.
    * 
    * @param int $item_id Order Item ID
    * @param int $product_id Product ID
    * @param array $booking_details Booking Data Array
    * @param int $variation_id Variation ID if exists else pass 0
    * @param string $status Status of Booking to be created
    * 
    * @return BKAP_Booking Booking Object
    * 
    * @globals mixed $wpdb
    * 
    * @since 4.0.0
    */

    static function bkap_create_booking_post( $item_id, $product_id, $qty, $booking_details, $variation_id = 0, $status = 'confirmed' ) {

        global $wpdb;

        $new_booking_data = array();

        // Merge booking data
        $defaults = array(
            'product_id'       => $product_id, // Booking ID
            'order_item_id'    => $item_id,
            'start_date'       => '',
            'end_date'         => '',
            'resource_id'      => '',
            'persons'          => array(),
            'qty'              => $qty,
            'variation_id'     => $variation_id,
            'gcal_event_uid'   => false
        );

        $new_booking_data = wp_parse_args( $new_booking_data, $defaults );

        // order ID
        $query_order_id = "SELECT order_id FROM `". $wpdb->prefix."woocommerce_order_items`
                            WHERE order_item_id = %d";

        $get_order_id = $wpdb->get_results( $wpdb->prepare( $query_order_id, $item_id ) );
        
        $order_id = 0;
        if ( isset( $get_order_id ) && is_array( $get_order_id ) && count( $get_order_id ) > 0 ) {
            $order_id = $get_order_id[0]->order_id;
        }

        $all_day = 0;
        if ( isset( $booking_details[ 'hidden_date_checkout' ] ) && '' != $booking_details[ 'hidden_date_checkout' ] ) { // multiple day

            $start_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date' ] ) );
            $end_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date_checkout' ] ) );

            // check if rental addon is enabled and per day booking is allowed
            $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );

            // for now, we default the start and end times to 000000
            $start_time = '000000';
            $end_time = '000000';
            if ( isset( $booking_settings[ 'booking_charge_per_day' ] ) && 'on' == $booking_settings[ 'booking_charge_per_day' ] ) {
                // if  rental addon flat charge per day is enabled, then the entire checkout date is considered booked, hence 235959
                $end_time = '235959';
            }

            // when checkin and checkout time will be introduced, they will be taken as is set by the user

        } elseif ( isset( $booking_details[ 'time_slot' ] ) && '' != $booking_details[ 'time_slot' ] ) { // date & time
            $start_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date' ] ) );
            $end_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date' ] ) );

            $explode_time = explode( '-', $booking_details[ 'time_slot' ] );

            $start_time = date( 'His', strtotime( trim( $explode_time[ 0 ] ) ) );

            if ( isset( $explode_time[ 1 ] ) && '' != $explode_time[ 1 ] ) {
                $end_time = date( 'His', strtotime( trim( $explode_time[ 1 ] ) ) );
            } else {
                $end_time = '000000';
            }
        } else { // only day
            $all_day = 1;
            if ( isset( $booking_details[ 'hidden_date' ] ) ) {
                $start_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date' ] ) );
                $end_date = date( 'Ymd', strtotime( $booking_details[ 'hidden_date' ] ) );
                $start_time = '000000';
                $end_time = '000000';
            }else {
                $start_date = '000000';
                $end_date = '000000';
                $start_time = '000000';
                $end_time = '000000';
            }

        }
        
        $event_uid = '';
        
        if( isset( $booking_details[ 'uid' ] ) && '' != $booking_details[ 'uid' ] ) {
            $event_uid =  $booking_details[ 'uid' ];
        }
        
        $resource_id = '';
        if( isset( $booking_details[ 'resource_id' ] ) && '' != $booking_details[ 'resource_id' ] ) {
            $resource_id =  $booking_details[ 'resource_id' ];
        }

        $booking_price = $booking_details[ 'price' ];

        if( isset( $booking_details[ 'Deposit' ] ) && '' != $booking_details[ 'Deposit' ] ) {
            $booking_price = $booking_price + $booking_details[ 'Deposit' ];
        }
        
        $new_booking_data[ 'start_date' ]      = $start_date . $start_time;
        $new_booking_data[ 'end_date' ]        = $end_date . $end_time;
        $new_booking_data[ 'cost' ]            = $booking_price;
        $new_booking_data[ 'user_id' ]         = get_post_meta( $order_id, '_customer_user', true );
        $new_booking_data[ 'all_day' ]         = $all_day;
        $new_booking_data[ 'parent_id' ]       = $order_id; // order ID
        $new_booking_data[ 'gcal_event_uid' ]  = $event_uid;
        $new_booking_data[ 'resource_id' ]     = $resource_id;
        
        $status = wc_get_order_item_meta( $item_id, '_wapbk_booking_status' );

        // Create it
        $new_booking = bkap_checkout::get_bkap_booking( $new_booking_data );
        $new_booking->create( $status );

        do_action( 'bkap_update_booking_post_meta', $new_booking->id );
        
        return $new_booking;

    }

    /**
     * Get Booking Object
     * 
     * @param int $id ID for Booking to fetch
     * @return BKAP_Booking Booking Object
     * @since 4.0.0
     */

    static function get_bkap_booking( $id ) {
        return new BKAP_Booking( $id );
    }

    /**
     * This function updates the database for the booking 
     * details and adds booking fields on the Order Received page,
     * Woocommerce->Orders when an order is placed for Woocommerce
     * version breater than 2.0.
     * 
     * @param int $item_meta Order ID
     * @param mixed $cart_item Cart Item data
     * 
     * @globals mixed $wpdb
     * @globals mixed $woocommerce
     * 
     * @since 1.7.0
     * 
     * @hook woocommerce_checkout_update_order_meta
     */

    public static function bkap_order_item_meta( $item_meta, $cart_item ) {
        if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) < 0 ) {
            return;
        }
        
        // Add the fields
        global $wpdb;   
        global $woocommerce;
        
        $booking_data_present =   "NO";
        
        // check if data is already present for this order in the booking_order_history table
        // if yes, then set variable to ensure we don't re-create records for the plugin tables.
        $check_data           =   "SELECT * FROM `".$wpdb->prefix."booking_order_history`
                                  WHERE order_id = %s";
        $results_check        =   $wpdb->get_results ( $wpdb->prepare ( $check_data, $item_meta ) );
        
        if ( count ( $results_check ) > 0 ) {
            $booking_data_present = "YES";
        }
        
        $order_item_ids   =   array();
        $sub_query        =   "";
        $ticket_content   =   array();
        $i                =   0;
        
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product    =   $values['data'];
            $parent_id = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $_product->get_parent() : bkap_common::bkap_get_parent_id( $values[ 'product_id' ] );
            
            $variation_id = ( array_key_exists( "variation_id", $values ) ) ? $values[ 'variation_id' ] : 0;
            
            if ( isset( $values['bkap_booking'] ) ) {
                $booking = $values['bkap_booking'];
            }
            
            $post_id     =   bkap_common::bkap_get_product_id( $values['product_id'] );
            
            $quantity    =   $values['quantity'];
            
            // to accomodate the change where attribute values are taken as quantity
                
            // Product Attributes - Booking Settings
            $attribute_booking_data = get_post_meta( $post_id, '_bkap_attribute_settings', true );
                
            // attribute values in the order
            $order_attr = $values[ 'variation' ];
                
            if ( is_array( $attribute_booking_data ) && count( $attribute_booking_data ) > 0 ) {
                $attr_qty = 0;
                foreach ( $attribute_booking_data as $attr_name => $attr_settings ) {
                    $attr_name = 'attribute_' . $attr_name;
                    // check if the setting is on
                    if ( isset( $attr_settings[ 'booking_lockout_as_value' ] ) && 'on' == $attr_settings[ 'booking_lockout_as_value' ] ) {
                        if ( array_key_exists( $attr_name, $order_attr ) && $order_attr[ $attr_name ]  != 0 ) {
                            $attr_qty += $order_attr[ $attr_name ];
                        }
                    }
                }
                if ( isset( $attr_qty ) && $attr_qty > 0 ){
                    $attr_qty = $attr_qty * $values[ 'quantity' ];
                }
            }
                
            if ( isset( $attr_qty ) && $attr_qty > 0 ){
                $quantity = $attr_qty;
            }
                
            $post_title  =   ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $_product->get_title() : strip_tags( $_product->get_name() );
            $post_title  =   preg_replace( '/\s\s+/', ' ', $post_title );
            // Fetch line item
            if ( count( $order_item_ids ) > 0 ) {
                $order_item_ids_to_exclude  = implode( ",", $order_item_ids );
                $sub_query                  = " AND order_item_id NOT IN (".$order_item_ids_to_exclude.")";
            }
                
            $query               =   "SELECT order_item_id,order_id FROM `".$wpdb->prefix."woocommerce_order_items`
                                      WHERE order_id = %s AND order_item_name LIKE %s".$sub_query;

            $results             =   $wpdb->get_results( $wpdb->prepare( $query, $item_meta, trim($post_title," ") . '%' ) );
        
            $order_item_ids[]    =   $results[0]->order_item_id;
            $order_id            =   $results[0]->order_id;
            $order_obj           =   new WC_order( $order_id );
            $details             =   array();
            $product_ids         =   array();
        
            $order_items = $order_obj->get_items();
                
            $type_of_slot = apply_filters( 'bkap_slot_type', $post_id );
            do_action( 'bkap_update_order', $values,$results[0] );
        
            if( $type_of_slot == 'multiple' ) {
                $hidden_date = $booking[0]['hidden_date'];
                do_action( 'bkap_update_booking_history', $values, $results[0] );
            }
            else {
                if ( isset( $values['bkap_booking'] ) && isset( $values['bkap_booking'][0]['date'] ) ) :
                    $booking_settings  =   get_post_meta( $post_id, 'woocommerce_booking_settings', true );
                    $details           =   array();
                    
                    // Add booking data as item meta
                    bkap_common::bkap_update_order_item_meta( $results[0]->order_item_id, $post_id, $booking[0], false );

                    // if yes, then execute code above for all items. Below code is not needed as it has already been executed once
                    if ( $booking_data_present == "YES" ) {
                        continue;
                    }
                    
                    // Update the availability for that product
                    $details = bkap_checkout::bkap_update_lockout( $order_id, $post_id, $parent_id, $quantity, $booking[0] );
                    
                    // Add the booking as a post
                    $booking_data = $booking[0];
                    $booking_data[ 'gcal_uid' ] = false;
                    bkap_checkout::bkap_create_booking_post( $results[0]->order_item_id, $post_id, $quantity, $booking_data, $variation_id );
                        
                endif;
            }
            
            // update the global time slot lockout
            if( isset( $booking[0]['time_slot'] ) && $booking[0]['time_slot'] != "" ) {
                bkap_checkout::bkap_update_global_lockout( $post_id, $quantity, $details, $booking[0] );
            }
            
            $ticket          =   array( apply_filters( 'bkap_send_ticket', $values,$order_obj ) );
            $ticket_content  =   array_merge( $ticket_content, $ticket );
            $i++;
            
            $start_date_label     = get_option( 'book_item-meta-date' );
            $checkout_date_label  = get_option( 'checkout_item-meta-date' );
            $time_slot_lable      = get_option( 'book_item-meta-time' );
                
            // The below code needs to be run only if WooCommerce verison is > 2.5
            if ( version_compare( WOOCOMMERCE_VERSION, "2.5" ) < 0 ) {
                continue;
            } else {
                // Code where the Booking dates and time slots dates are not displayed in the customer new order email from WooCommerce version 2.5
                $cache_key       = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'item_meta_array_' . $results[ 0 ]->order_item_id;
                $item_meta_array = wp_cache_get( $cache_key, 'orders' );
                if ( false !== $item_meta_array ) {
                    $metadata        = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d AND meta_key IN (%s,%s,%s,%s,%s,%s) ORDER BY meta_id", absint( $results[ 0 ]->order_item_id ), $start_date_label, '_wapbk_booking_date',  $checkout_date_label, '_wapbk_checkout_date', $time_slot_lable, '_wapbk_time_slot'  ) );
                    foreach ( $metadata as $metadata_row ) {
                        $item_meta_array[ $metadata_row->meta_id ] = (object) array( 'key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value );
                    }
                    wp_cache_set( $cache_key, $item_meta_array, 'orders' );
                }
            }
            
        }
    
        do_action( 'bkap_send_email', $ticket_content );
    }

    /**
     * This function updates the database resords for the booking which has globally same timeslots
     * 
     * @param int $post_id Post ID
     * @param mixed $quantity quantity
     * @param array $details array of all matching records for booking
     * @param array $booking_data array of selected booking details
     * 
     * @global mixed $wpdb Global wp Object
     * 
     * @since 1.7.0
     */  
    
    public static function bkap_update_global_lockout( $post_id, $quantity, $details, $booking_data ) {
         
        global $wpdb;
         
        $book_global_settings    = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
        $booking_settings        = get_post_meta( $post_id, 'woocommerce_booking_settings' , true );
    
        $hidden_date   =   $booking_data['hidden_date'];
        $date_query    =   date( 'Y-m-d', strtotime( $hidden_date ) );
        
        $week_day  =   date( 'l', strtotime( $hidden_date ) );
        $weekdays  =   bkap_get_book_arrays( 'bkap_weekdays' );
        $weekday   =   array_search( $week_day, $weekdays );
    
        if( isset( $booking_settings['booking_time_settings'] ) && isset( $hidden_date ) ){
             
            if ( isset( $booking_settings['booking_time_settings'][ $hidden_date ] ) ) $lockout_settings = $booking_settings['booking_time_settings'][ $hidden_date ];
            else $lockout_settings = array();
             
            if ( count( $lockout_settings ) == 0 ){
                
    
                if ( isset( $booking_settings['booking_time_settings'][$weekday] ) ) $lockout_settings = $booking_settings['booking_time_settings'][ $weekday ];
                else $lockout_settings = array();
            }
             
            $from_hours = $from_minute = $to_hours = $to_minute = '';
             
            if( isset( $booking_data['time_slot'] ) && $booking_data['time_slot'] != "" ) {
                 
                $time_select              =   $booking_data['time_slot'];
                $time_exploded            =   explode( "-", $time_select );
                 
                $query_from_time  =   date( 'G:i', strtotime( $time_exploded[0] ) );
                if( isset( $time_exploded[1] ) ){
                    $query_to_time       = date( 'G:i', strtotime( $time_exploded[1] ) );
                }
            }
            if ( isset( $query_from_time ) ) {
                $from_lockout_time =   explode( ":", $query_from_time );
                $from_hours        =   $from_lockout_time[0];
                $from_minute       =   $from_lockout_time[1];
    
                if ( isset( $query_to_time ) && $query_to_time != '' ) {
                    $to_lockout_time  = explode( ":", $query_to_time );
                    $to_hours         = $to_lockout_time[0];
                    $to_minute        = $to_lockout_time[1];
                }
            }
             
            foreach ( $lockout_settings as $l_key => $l_value ) {
    
                if ( $l_value['from_slot_hrs'] == $from_hours && $l_value['from_slot_min'] == $from_minute && $l_value['to_slot_hrs'] == $to_hours && $l_value['to_slot_min'] == $to_minute ) {
                     
                    if ( isset( $l_value['global_time_check'] ) ){
                        $global_timeslot_lockout = $l_value['global_time_check'];
                    } else{
                        $global_timeslot_lockout = '';
                    }
                     
                }
            }
        }
         
        if ( isset( $book_global_settings->booking_global_timeslot ) && $book_global_settings->booking_global_timeslot == 'on' || isset( $global_timeslot_lockout ) && $global_timeslot_lockout == 'on' ) {
            $args       = array( 'post_type' => 'product', 'posts_per_page' => -1 );
            $product    = query_posts( $args );
             
            foreach($product as $k => $v){
                $product_ids[] = $v->ID;
            }
             
            foreach( $product_ids as $k => $v ){
                $duplicate_of  = bkap_common::bkap_get_product_id( $v );
    
                $booking_settings = get_post_meta( $v, 'woocommerce_booking_settings' , true );
    
                if( isset( $booking_settings['booking_enable_time'] ) && $booking_settings['booking_enable_time'] == 'on' ) {
                     
                    if( !array_key_exists( $duplicate_of,$details ) ) {
    
                        foreach( $details as $key => $val ){
                            $booking_settings   = get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );
                             
                            $start_date         = $val->start_date;
                            $from_time          = $val->from_time;
                            $to_time            = $val->to_time;

                            $from_hi = date( 'H:i', strtotime( $from_time ) );
                            
                            if( $to_time != "" ){
                                 
                                // global time slots over lapping
                                $get_all_time_slots =   array();
                                 
                                $insert             =   "NO";
                                 
                                $query              =   "SELECT *  FROM `".$wpdb->prefix."booking_history`
                                                            WHERE post_id = '".$duplicate_of."' AND
                                                            start_date = '".$start_date."' AND
                                                            status !=  'inactive' ";
                                $get_all_time_slots =   $wpdb->get_results( $query );
                                 
                                if( !$get_all_time_slots ){
                                    $insert             =   "YES";
                                    $query              =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                                WHERE post_id = %d
                                                                AND weekday = %s
                                                                AND start_date = '0000-00-00'
                                                                AND status !=  'inactive'";
                                    $get_all_time_slots =   $wpdb->get_results( $wpdb->prepare( $query, $duplicate_of, $weekday ) );
                                }
                                 
                                foreach( $get_all_time_slots as $time_slot_key => $time_slot_value){
                                     
                                    if( "YES" == $insert ){
                                         
                                        $query_insert   =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                (post_id,weekday,start_date,from_time,to_time,total_booking,available_booking)
                                                                VALUES (
                                                                '".$duplicate_of."',
                                                                '".$weekday."',
                                                                '".$start_date."',
                                                                '".$time_slot_value->from_time."',
                                                                '".$time_slot_value->to_time."',
                                                                '".$time_slot_value->total_booking."',
                                                                '".$time_slot_value->available_booking."' ) ";
                                         
                                        $wpdb->query( $query_insert );
                                         
                                    }
                                     
                                    $query_from_time_time_stamp         = strtotime( $from_time );
                                    $query_to_time_time_stamp           = strtotime( $to_time );
                                    $time_slot_value_from_time_stamp    = strtotime( $time_slot_value->from_time );
                                    $time_slot_value_to_time_stamp      = strtotime( $time_slot_value->to_time );
                                     
                                    if( $query_to_time_time_stamp > $time_slot_value_from_time_stamp && $query_from_time_time_stamp < $time_slot_value_to_time_stamp ){
                                         
                                        if ( $time_slot_value_from_time_stamp != $query_from_time_time_stamp || $time_slot_value_to_time_stamp != $query_to_time_time_stamp ) {
                                             
                                            $query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                            SET available_booking = available_booking - ".$quantity."
                                                            WHERE post_id = '".$duplicate_of."' AND
                                                            start_date = '".$start_date."' AND
                                                            from_time = '".$time_slot_value->from_time."' AND
                                                            to_time = '".$time_slot_value->to_time."' AND
                                                            status != 'inactive' AND
                                                            total_booking > 0";
                                            $wpdb->query( $query );
                                        }
                                    }
                                }

                                $to_hi = date( 'H:i', strtotime( $to_time ) );
                                
                                $check_record = "SELECT id FROM `" . $wpdb->prefix . "booking_history`
                                                   WHERE post_id = %d
                                                   AND start_date = %s
                                                   AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                   AND TIME_FORMAT( to_time, '%H:%i' ) = %s";
                                $get_results = $wpdb->get_col( $wpdb->prepare( $check_record, $duplicate_of, $date_query, $from_hi, $to_hi ) );
                                
                                $query      =   "UPDATE `".$wpdb->prefix."booking_history`
                                                    SET available_booking = available_booking - ".$quantity."
                                                    WHERE post_id = '".$duplicate_of."' AND
                                                    start_date = '".$date_query."' AND
                                                    TIME_FORMAT( from_time, '%H:%i' ) = '".$from_hi."' AND
                                                    TIME_FORMAT( to_time, '%H:%i' ) = '".$to_hi."' AND
                                                    total_booking > 0 AND
                                                    status != 'inactive' ";
                                $updated   = $wpdb->query( $query );
    
                                if( isset( $get_results ) && count( $get_results ) == 0 && $updated == 0 ) {
                                     
                                    if($val->weekday == '') {
                                        $week_day    =   date( 'l', strtotime( $date_query ) );
                                        $weekdays    =   bkap_get_book_arrays( 'bkap_weekdays' );
                                        $weekday     =   array_search( $week_day, $weekdays );
                                        //echo $weekday;exit;
                                    } else {
                                        $weekday = $val->weekday;
                                    }
                                     
                                    $results  =   array();
                                    $query    =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                      WHERE post_id = %s
                                                      AND weekday = %s
                                                      AND start_date = '0000-00-00'
                                                      AND status !=  'inactive' ";
                                     
                                    $results =    $wpdb->get_results( $wpdb->prepare( $query, $duplicate_of, $weekday ) );
                                     
                                    if ( !$results ) break;
                                    else {
    
                                        foreach( $results as $r_key => $r_val ) {
                                             
                                            if( $from_time == $r_val->from_time && $to_time == $r_val->to_time ) {
                                                $available_booking =   ( $r_val->total_booking > 0 ) ? $r_val->available_booking - $quantity : $r_val->available_booking;
                                                $query_insert      =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                            (post_id,weekday,start_date,from_time,to_time,total_booking,available_booking)
                                                                            VALUES (
                                                                            '".$duplicate_of."',
                                                                            '".$weekday."',
                                                                            '".$start_date."',
                                                                            '".$r_val->from_time."',
                                                                            '".$r_val->to_time."',
                                                                            '".$r_val->total_booking."',
                                                                            '".$available_booking."' )";
                                                 
                                                $wpdb->query( $query_insert );
                                                 
                                            } else {
                                                $from_lockout_time = explode ( ":", $r_val->from_time );
    
                                                if ( isset( $from_lockout_time[0] ) ){
                                                    $from_hours = $from_lockout_time[0];
                                                }else{
                                                    $from_hours= " ";
                                                }
    
                                                if ( isset( $from_lockout_time[1] ) ){
                                                    $from_minute = $from_lockout_time[1];
                                                }else{
                                                    $from_minute = " ";
                                                }
                                                // default to blanks
                                                $to_hours = $to_minute = '';
    
                                                if ( isset( $r_val->to_time ) && $r_val->to_time != '' ) {
                                                    $to_lockout_time = explode( ":", $r_val->to_time );
                                                     
                                                    if ( isset ( $to_lockout_time[0] ) ) {
                                                        $to_hours = $to_lockout_time[0];
                                                    }
                                                     
                                                    if ( isset ( $to_lockout_time[1] ) ) {
                                                        $to_minute = $to_lockout_time[1];
                                                    }
                                                }
                                                foreach ( $lockout_settings as $l_key => $l_value ) {
                                                     
                                                    if( $l_value['from_slot_hrs'] == $from_hours && $l_value['from_slot_min'] == $from_minute && $l_value['to_slot_hrs'] == $to_hours && $l_value['to_slot_min'] == $to_minute ) {
                                                        $query_insert    =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                                 (post_id,weekday,start_date,from_time,to_time,total_booking,available_booking)
                                                                                 VALUES (
                                                                                 '".$duplicate_of."',
                                                                                 '".$weekday."',
                                                                                 '".$start_date."',
                                                                                 '".$r_val->from_time."',
                                                                                 '".$r_val->to_time."',
                                                                                 '".$r_val->total_booking."',
                                                                                 '".$r_val->available_booking."' )";
                                                        $wpdb->query( $query_insert );
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }else {
                                
                                $check_record = "SELECT id FROM `" . $wpdb->prefix . "booking_history`
                                                   WHERE post_id = %d
                                                   AND start_date = %s
                                                   AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                   AND to_time = ''";
                                $get_results = $wpdb->get_col( $wpdb->prepare( $check_record, $duplicate_of, $date_query, $from_hi ) );
                                 
                                $query     =   "UPDATE `".$wpdb->prefix."booking_history`
                                                    SET available_booking = available_booking - ".$quantity."
                                                    WHERE post_id = '".$duplicate_of."' AND
                                                    start_date = '".$date_query."' AND
                                                    TIME_FORMAT( from_time, '%H:%i' ) = '".$from_hi."'
                                                    AND to_time = ''
                                                    AND total_booking > 0
                                                    AND status != 'inactive' ";
    
                                $updated   =   $wpdb->query( $query );
    
                                if ( isset( $get_results ) && count( $get_results ) == 0 && $updated == 0 ) {
                                    if ( $val->weekday == '' ) {
                                        $week_day    =   date( 'l', strtotime( $date_query ) );
                                        $weekdays    =   bkap_get_book_arrays( 'bkap_weekdays' );
                                        $weekday     =   array_search( $week_day, $weekdays );
                                         
                                    } else {
                                        $weekday = $val->weekday;
                                    }
                                     
                                    $results  =   array();
                                    $query    =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                      WHERE post_id = %d
                                                      AND weekday = %s
                                                      AND start_date = '0000-00-00'
                                                      AND to_time = '' 
                                                      AND status !=  'inactive' ";
                                    $results  =   $wpdb->get_results( $wpdb->prepare( $query, $duplicate_of, $weekday ) );
                                     
                                    if ( !$results ) break;
                                    else {
                                        foreach( $results as $r_key => $r_val ) {
                                             
                                            if ( $from_time == $r_val->from_time ) {
                                                $available_booking     =   ( $r_val->total_booking > 0 ) ? $r_val->available_booking - $quantity : $r_val->available_booking;
                                                $query_insert          =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                                (post_id,weekday,start_date,from_time,total_booking,available_booking)
                                                                                VALUES (
                                                                                '".$duplicate_of."',
                                                                                '".$weekday."',
                                                                                '".$start_date."',
                                                                                '".$r_val->from_time."',
                                                                                '".$r_val->total_booking."',
                                                                                '".$available_booking."' )";
                                                $wpdb->query( $query_insert );
                                            }else {
                                                $from_lockout_time     = explode( ":", $r_val->from_time );
                                                $from_hours = 0;
                                                $from_minute = 0;
                                                if ( isset( $from_lockout_time[0] ) ) {
                                                   $from_hours            = $from_lockout_time[0];
                                                }
                                                if ( isset( $from_lockout_time[1] ) ) {
                                                   $from_minute           = $from_lockout_time[1];
                                                }
    
                                                foreach( $lockout_settings as $l_key => $l_value ) {
                                                     
                                                    if( $l_value['from_slot_hrs'] == $from_hours && $l_value['from_slot_min'] == $from_minute ) {
                                                        $query_insert    =   "INSERT INTO `".$wpdb->prefix."booking_history`
                                                                                (post_id,weekday,start_date,from_time,total_booking,available_booking)
                                                                                VALUES (
                                                                                '".$duplicate_of."',
                                                                                '".$weekday."',
                                                                                '".$start_date."',
                                                                                '".$r_val->from_time."',
                                                                                '".$r_val->available_booking."',
                                                                                '".$r_val->available_booking."' )";
                                                        $wpdb->query( $query_insert );
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } 
    }   
}
?>
API documentation generated by ApiGen