<?php
add_action( 'woocommerce_reduce_order_stock', 'exwf_update_tsl_nborders' );
function exwf_update_tsl_nborders( $order ){
	$dv_date = $order->get_meta( 'exwfood_date_deli_unix', true );
	$dv_time = $order->get_meta( 'exwfood_time_deli', true );
	$loc_ar = $order->get_meta( 'exwoofood_location', true );
	$order_method = $order->get_meta( 'exwfood_order_method', true );
	$order_method = $order_method !='' ? $order_method : 'delivery'; 
	$nb_orders = get_option('exwf_tsl_nborders'); 
	if($loc_ar ==''){
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$dv_time]) ? $nb_orders[$dv_date][$order_method][$dv_time] : 0 ;
		$nb_orders_tsl  = is_numeric($nb_orders_tsl) && $nb_orders_tsl >= 0 ? $nb_orders_tsl + 1 : 0;
		$nb_orders[$dv_date][$order_method][$dv_time] = $nb_orders_tsl;
		update_option('exwf_tsl_nborders',$nb_orders);
	}else{
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$loc_ar][$dv_time]) ? $nb_orders[$dv_date][$order_method][$loc_ar][$dv_time] : 0;
		$nb_orders_tsl  = is_numeric($nb_orders_tsl) && $nb_orders_tsl >= 0 ? $nb_orders_tsl + 1 : 0;
		$nb_orders[$dv_date][$order_method][$loc_ar][$dv_time] = $nb_orders_tsl;
		update_option('exwf_tsl_nborders',$nb_orders);
	}
}//echo '<pre>';print_r(get_option('exwf_tsl_nborders'));exit;
add_action( 'woocommerce_order_status_cancelled','exwf_update_tsl_nborders_ifcancel',99 );
function exwf_update_tsl_nborders_ifcancel( $order_id ){
	$order = new WC_Order( $order_id );
	$dv_date = $order->get_meta( 'exwfood_date_deli_unix', true );
	$_date_type = exwoofood_get_option('exwoofood_dd_display','exwoofood_advanced_options');
	$foodby_date = exwoofood_get_option('exwoofood_foodby_date');
	if($_date_type =='picker' && $foodby_date!='yes'){
		$dv_date = strtotime($dv_date);
	}
	$dv_time = $order->get_meta( 'exwfood_time_deli', true );
	$loc_ar = $order->get_meta( 'exwoofood_location', true );
	$order_method = $order->get_meta( 'exwfood_order_method', true );
	$order_method = $order_method !='' ? $order_method : 'delivery'; 
	$nb_orders = get_option('exwf_tsl_nborders'); 
	if($loc_ar ==''){
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$dv_time]) ? $nb_orders[$dv_date][$order_method][$dv_time] : 0 ;
		$nb_orders_tsl  = is_numeric($nb_orders_tsl) && $nb_orders_tsl >= 0 ? $nb_orders_tsl - 1 : 0;
		$nb_orders[$dv_date][$order_method][$dv_time] = $nb_orders_tsl;
		update_option('exwf_tsl_nborders',$nb_orders);
	}else{
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$loc_ar][$dv_time]) ? $nb_orders[$dv_date][$order_method][$loc_ar][$dv_time] : 0;
		$nb_orders_tsl  = is_numeric($nb_orders_tsl) && $nb_orders_tsl >= 0 ? $nb_orders_tsl - 1 : 0;
		$nb_orders[$dv_date][$order_method][$loc_ar][$dv_time] = $nb_orders_tsl;
		update_option('exwf_tsl_nborders',$nb_orders);
	}
}

add_filter( 'exwf_get_number_orders_timeslot', 'exwf_get_number_orders_op',12,6 );
function exwf_get_number_orders_op($dv_date,$dv_time,$loc_ar,$order_method,$tsl_method,$timeslots) {
	$get_orders = array();
	$get_orders['sql'] = 'yes';
	$nb_orders = get_option('exwf_tsl_nborders');
	if($loc_ar ==''){
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$dv_time]) ? $nb_orders[$dv_date][$order_method][$dv_time] : 0 ;
	}else{
		$nb_orders_tsl  = isset($nb_orders[$dv_date][$order_method][$loc_ar][$dv_time]) ? $nb_orders[$dv_date][$order_method][$loc_ar][$dv_time] : 0;
	}
	$get_orders['total'] = $nb_orders_tsl;
}

add_action('init','exwf_setup_schedule');
function exwf_setup_schedule() {
	if (!wp_next_scheduled('exwf_weekly_pruning') ) {
		wp_schedule_event( time(), 'weekly', 'exwf_weekly_pruning');
	}
}
add_action( 'exwf_weekly_pruning', 'exwf_remove_pass_tsl_nborders' );
function exwf_remove_pass_tsl_nborders() {
	$cure_time =  strtotime(' -7 day');
	$gmt_offset = exwf_df_gmt_offset();
	if($gmt_offset!=''){
		$cure_time = $cure_time + ($gmt_offset*3600);
	}
	$nb_orders = get_option('exwf_tsl_nborders');
	if(is_array($nb_orders) && !empty($nb_orders)){
		foreach ($nb_orders as $key => $item) {
			if($key < $cure_time){
				unset($nb_orders[$key]);
			}
		}
		update_option('exwf_tsl_nborders',$nb_orders);
	}
}