<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class fbbttmCart
{
	public function fbbttm_get_basic_cartinfo(){

		global $wpdb, $fbbttm_helperobj;
		$currentuserid = '0';
		$tokenid  = '';

		$carttablename = FBBTTM_TB_FOODBOOKING_CART;
		$cartitemtablename = FBBTTM_TB_FOODBOOKING_CART_ITEMINFO;

		$tokenid = $fbbttm_helperobj->fbbttm_get_user_cookie_data();
		
		if ( is_user_logged_in() ) 
		 {
			$currentuserid = get_current_user_id();
		 }
	
		$fbbttm_fullcartinfo = $this->fbbttm_get_full_cartinfo($currentuserid,$tokenid);

		return $fbbttm_fullcartinfo;

		}

	public	function fbbttm_date_range($begin, $end, $interval = null)
		{
		  $begin = new DateTime($begin);
		  $end = new DateTime($end);

		  $end = $end->modify('+1 day');
		  $interval = new DateInterval($interval ? $interval : 'P1D');

		  return iterator_to_array(new DatePeriod($begin, $interval, $end));
		}

		public function fbbttm_calculate_enddate($startDate, $specificDays, $quantity)
		{
		 	$startDate = new DateTime($startDate);
		    $endDate = clone $startDate;
		    $count = 0;

		    // Normalize day names to lowercase
		    $specificDays = array_map('strtolower', $specificDays);

		    // Check if the start date itself matches one of the specific days
		    if (in_array(strtolower($startDate->format('l')), $specificDays)) {
		        $count++;
		    }

		    while ($count < $quantity) {
		        // Move to the next day
		        $endDate->modify('+1 day');
		        
		        // Check if this day is in the specific days array
		        if (in_array(strtolower($endDate->format('l')), $specificDays)) {
		            $count++;
		        }
	   		 }

    		return $endDate->format('Y-m-d');
		}	

	public function fbbttm_add_tocart($cartdata=null){

		global $wpdb, $fbbttm_helperobj,$fbbttm_orderinfoobj;

		check_ajax_referer( 'fbbttm_nonce_action', 'security' );

		$message = array();

		$carttablename = FBBTTM_TB_FOODBOOKING_CART;
		$cartitemtablename = FBBTTM_TB_FOODBOOKING_CART_ITEMINFO;
		$currentuserid = '0';
		$tokenid  = '';
		
		if ( is_user_logged_in() ) 
		 {
			$currentuserid = get_current_user_id();
			$tokenid = $fbbttm_helperobj->fbbttm_get_user_cookie_data();
		 }
		else
		 {
			$tokenid = $fbbttm_helperobj->fbbttm_get_user_cookie_data();
		 }																
			
		$message['tokenkey'] = $tokenid;  
		$packid = $deliveryday = $startdate ='';
		$foodtime = $deliday=array();
		if(isset($_POST['fbbttm-packid'])){

		$packid = sanitize_text_field(wp_unslash($_POST['fbbttm-packid']));
		}

		/* using this info need to change start date and end date*/
		$ordinfo = $fbbttm_orderinfoobj->fbbttm_get_order_with_samepackid($currentuserid,$packid);

		$cartinfo = $this->fbbttm_get_cart_info($currentuserid,$tokenid);

		$fbbttm_fullcartinfo = $this->fbbttm_get_full_cartinfo($currentuserid,$tokenid);

		$packinfo = $fbbttm_helperobj->fbbttm_get_packinfo($packid);

		$pack_name  = $packinfo['pack_name'];

		$fooditemid = $packinfo['fooditemid'];

		$content_post = get_post($fooditemid);

		$foodname = $content_post->post_title;

		$post_meta = get_post_meta($fooditemid); 
  
    	$fbbttmdetails =  (!empty($post_meta['fbbttmdetails'][0]))? $post_meta['fbbttmdetails'][0] : '';

		$foodprice  = $packinfo['fooditemprice_without'];
		$packageday = $packinfo['packageday'];
		$pack_specialprice = $packinfo['pack_specialprice'];
		$pack_price = $packinfo['pack_price_withoutsymbol'];
		$savemoney_without = '';
		if(!empty($packinfo['savemoney_without'])){
			$savemoney_without = $packinfo['savemoney_without'];
		}
		$packdescription   = $packinfo['packdescription'];

		if ( isset( $_POST['food_time'] ) && is_array( $_POST['food_time'] ) ) {	
		// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized 	
			 $foodtime = $this->sanitize_array( wp_unslash($_POST['food_time'] ));
		}

		//if(isset($_POST['deliday'])){
		if ( isset( $_POST['deliday'] ) && is_array( $_POST['deliday'] ) ) {	
			// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized	
			 $deliday = $this->sanitize_array( wp_unslash($_POST['deliday'] ));
			
		}

		
		
		$lunchqty = 0;
		$dinnerqty = 0;
		$deliverytime = $fbbttm_datemessge  = $fbbttm_datemessge_blank  ='';

		if(isset($_POST['datemessge'])){
			$fbbttm_datemessge  = sanitize_text_field(wp_unslash($_POST['datemessge']));
		}

		if(isset($_POST['datemessge_blank'])){
			$fbbttm_datemessge_blank  = sanitize_text_field(wp_unslash($_POST['datemessge_blank']));
		}

		if(!empty($foodtime)){
			$deliverytime = implode (",",$foodtime);

			if (in_array("lunch", $foodtime)){
					$lunchqty = 1;
			}

			if (in_array("dinner", $foodtime)){
					$dinnerqty = 1;
			}
		}

		if(isset($_POST['deliveryday'])){
			$deliveryday = sanitize_text_field(wp_unslash($_POST['deliveryday']));
		}

		

		$lastday = $packageday; 

		$lastdayinfo = $selectstartdate = $enddate = $checkenddate = '';
		
		if($lunchqty=='1' && $dinnerqty == 1)
		{

			$lastdayinfo = (is_float($packageday/2)) ? ' lastday only lunch ': '';

			$lastday = round( $packageday/2 );

			$lastday = $lastday - 1;
	
		}
		else
		{
			$lastday = $lastday - 1;
		}



		if(empty($ordinfo)){
			$startdate ='';
			if(isset($_POST['startdate'])){
				$startdate = sanitize_text_field(wp_unslash($_POST['startdate']));
			}
			
			$enddate = gmdate('Y-m-d', strtotime($startdate .' +'.$lastday.' day'));   	
		}	
		else
		{	
			$startdate ='';
			if(isset($_POST['startdate'])){
				$startdate = sanitize_text_field(wp_unslash($_POST['startdate']));
			}

			$selectstartdate  = strtotime($startdate);

			foreach ($ordinfo as $value) {
				// code...
				$startdate = $value->fbbttm_food_startdate;
				$enddate = $value->fbbttm_food_enddate;

			}

			$currentdate = gmdate('Y-m-d');
			$checkcurrentdate = strtotime($currentdate);
			$checkenddate = strtotime($enddate);


			if($selectstartdate > $checkenddate)
			{
				$startdate ='';
				if(isset($_POST['startdate'])){
					$startdate = sanitize_text_field(wp_unslash($_POST['startdate']));
				}
				$enddate = gmdate('Y-m-d', strtotime($startdate .' +'.$lastday.' day'));   
			}
			else{

				if($checkenddate > $checkcurrentdate)		
				{
					$startdate = gmdate('Y-m-d', strtotime($enddate .' +1 day')); 
					$enddate   = gmdate('Y-m-d', strtotime($startdate .' +'.$lastday.' day'));   
				}	
				else
				{
					$startdate ='';
					if(isset($_POST['startdate'])){
						$startdate = sanitize_text_field(wp_unslash($_POST['startdate']));
					}
					$enddate 	 = gmdate('Y-m-d', strtotime($startdate .' +'.$lastday.' day')); 
				}

			}

		}


		

		$error_message = array();
		$message = array();

		

		if ( ! isset( $_POST[ 'fbbttm_meta_box_noncename' ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash($_POST['fbbttm_meta_box_noncename'] ) ), 'fbbttm_meta_box_nonce' ) ) {

			$error_message['fbbttm-phperror'] = esc_html__('Sorry, Your request can not be processed due to security reason.','foodboxbooker'); 
		
  		 }

		if(empty($packid))
		{
			$error_message['fbbttm-packid'] = esc_html__('Invalid package. Please select other package','foodboxbooker'); 
		}

		if(empty($deliverytime))
		{
			$error_message['fbbttm-deliverytime'] = esc_html__('Please select atleast one delivery time lunch or dinner','foodboxbooker'); 
		}

		if(empty($deliveryday))
		{
			$error_message['fbbttm-deliveryday'] = esc_html__('Please select your delivery day','foodboxbooker'); 
		}

		if(!empty($deliveryday) && $deliveryday=='custom')
		{
			if(empty($deliday)){

				$error_message['fbbttm-deliveryday'] = esc_html__('Please select your delivery day','foodboxbooker'); 
			}
			
		}

		if(empty($startdate))
		{
			$error_message['fbbttm-startdate'] = esc_html__('Please select your food startdate','foodboxbooker'); 
		}

		if( !empty($startdate) && !empty($ordinfo))
		{	
			if(!empty($selectstartdate) && !empty($checkenddate)  && empty($fbbttm_datemessge))
			{

				if($selectstartdate <= $checkenddate)
				{

				 $seldate = gmdate('Y-m-d',$selectstartdate);
				 $selenddate = gmdate('Y-m-d',$checkenddate);	
				
				 $error_message['fbbttm-datemessge'] = sprintf(
				 /* translators: 1: package end date, 2: package start gmdate */
				    esc_html__('This package is already added in your account and that package end date is %1$s. So this package new date is %2$s. Please click on add to cart again.', 'foodboxbooker'),
				    esc_html( $selenddate ),
				    esc_html( $startdate )
				  );
				}
			}
			elseif((empty($deliverytime) || empty($deliveryday)) && !empty($selectstartdate) && !empty($checkenddate))
			{
				if($selectstartdate <= $checkenddate)
				{

				 $seldate = gmdate('Y-m-d',$selectstartdate);
				 $selenddate = gmdate('Y-m-d',$checkenddate);	
				
				  $error_message['fbbttm-datemessge'] = sprintf(
				  	/* translators: 1: package end date, 2: package start date */
					    esc_html__( 'This package is already added in your account and that package end date is %1$s. So this package new date is %2$s. Please click on add to cart again.', 'foodboxbooker' ),
					    esc_html( $selenddate ),
					    esc_html( $startdate )
							);	
				}
			}
			elseif((!empty($deliveryday) && $deliveryday=='custom') &&  !empty($selectstartdate) && !empty($checkenddate) )
				{
				  if(empty($deliday)){

				  	if($selectstartdate <= $checkenddate)
						{

						 $seldate = gmdate('Y-m-d',$selectstartdate);
						 $selenddate = gmdate('Y-m-d',$checkenddate);	
						 
						 $error_message['fbbttm-datemessge'] = sprintf(
						 	/* translators: 1: package end date, 2: package new start date */
					     esc_html__('This package is already added in your account and that package end date is %1$s. So this package new date is %2$s. Please click on add to cart again.', 'foodboxbooker'),
					     esc_html($selenddate),
					     esc_html($startdate)
						);
						
						}

				  }
				}



			if(empty($selectstartdate) && empty($fbbttm_datemessge_blank))
			{

				
			
				$error_message['fbbttm-datemessge-blank'] = sprintf(
					/* translators: 1: package start date */
					    esc_html__('Package is already available in your account so for your this package start date is %1$s. Please click on add to cart again.', 'foodboxbooker'),
					    esc_html($startdate)
					    );

			}	
			elseif((empty($deliverytime) || empty($deliveryday)) &&  empty($selectstartdate))
			{
			
				$error_message['fbbttm-datemessge-blank'] = sprintf(
					/* translators: 1: package start date */
					    esc_html__('Package is already available in your account so for your this package start date is %1$s. Please click on add to cart again.', 'foodboxbooker'),
					    esc_html($startdate)
					    );
			}

			elseif((!empty($deliveryday) && $deliveryday=='custom') &&  empty($selectstartdate))
				{
				  if(empty($deliday)){
						
						$error_message['fbbttm-datemessge-blank'] = sprintf(
							/* translators: 1: package start date */
					    esc_html__('Package is already available in your account so for your this package start date is %1$s. Please click on add to cart again.', 'foodboxbooker'),
					    esc_html($startdate)
					    );
				 }
			  }


		}

		$message['error_message'] = $error_message;
		/*set enddate as per delivery day select*/

		if($deliveryday=='Mon-Sat')
		{
			$specificDays = ['Monday','Tuesday','Wednesday', 'Thursday', 'Friday','Saturday'];
 			$startDate = $startdate; // starting date
 			$qty = $packageday;
			if($lunchqty=='1' && $dinnerqty == 1)
			{
				$lastdayinfo = (is_float($packageday/2)) ? ' lastday only lunch ': '';
				$qty = $packageday/2;
				$qty = (is_float($qty)) ? round( $qty ) : $qty;	
			}	
			// specific days to include
			$enddate = $this->fbbttm_calculate_enddate($startDate, $specificDays, $qty);
		}
		else if($deliveryday=='Mon-Fri')
		{

			$specificDays = ['Monday','Tuesday','Wednesday', 'Thursday', 'Friday'];
 			$startDate = $startdate; // starting date
 			$qty = $packageday;
			if($lunchqty=='1' && $dinnerqty == 1)
			{
				$lastdayinfo = (is_float($packageday/2)) ? ' lastday only lunch ': '';
				$qty = $packageday/2;
				$qty = (is_float($qty)) ? round( $qty ) : $qty;	
			}	

		
			// specific days to include
		
			$enddate = $this->fbbttm_calculate_enddate($startDate, $specificDays, $qty);
		

			
		}

		else if($deliveryday=='custom')
		{	
			$qty = $packageday;
			if($lunchqty=='1' && $dinnerqty == 1)
			{

				$lastdayinfo = (is_float($packageday/2)) ? ' lastday only lunch ': '';
				
				$qty = $packageday/2;

				$qty = (is_float($qty)) ? round( $qty ) : $qty;	
			}	

			$startDate = $startdate; // starting date
			$specificDays = $deliday; // specific days to include
			$enddate = $this->fbbttm_calculate_enddate($startDate, $specificDays, $qty);
	
		}


		if(empty($error_message)){


			if(!empty($deliday) && $deliveryday=='custom'){
				$deliveryday = implode(',',$deliday);
			 }		

			$fbbttm_grand_total = $pack_price;

			if(empty($cartinfo)){


			$data = array(
				'fbbttm_user_id'=> $currentuserid,
				'fbbttm_token_id'=> $tokenid,
				'fbbttm_grand_total'=> $fbbttm_grand_total,
				'fbbttm_sub_total'=> $fbbttm_grand_total
			);

			$cartid = $fbbttm_helperobj->fbbttm_common_insert( $carttablename,  $data );

			$cartiteminfo = array(
				'fbbttm_fdcartid' => $cartid,
				'fbbttm_pack_id' => $packid,
				'fbbttm_fd_id' => $fooditemid,
				'fbbttm_food_name'=>$foodname,
				'fbbttm_food_details'=>$fbbttmdetails,
				'fbbttm_food_time' => $deliverytime,
				'fbbttm_lunch_qty' => $lunchqty,
				'fbbttm_dinner_qty' => $dinnerqty,
				'fbbttm_total_food' => $packageday,
				'fbbttm_booking_deliveryday' => $deliveryday,
				'fbbttm_food_startdate' => $startdate,
				'fbbttm_food_enddate' => $enddate,
				'fbbttm_lastday_info' => $lastdayinfo,
				'fbbttm_pack_name' => $pack_name,
				'fbbttm_package_price' => $pack_price,
				'fbbttm_special_price' => $pack_specialprice,
				'fbbttm_fitem_price' => $foodprice,
				'fbbttm_package_day' => $packageday,
				'fbbttm_package_description' => $packdescription
			);


			$cartiteminfo = $fbbttm_helperobj->fbbttm_common_insert( $cartitemtablename,  $cartiteminfo );

			$cartinfo = $this->fbbttm_get_basic_cartinfo();

			$countcart = '('.count($cartinfo).')';

			$cartinfoafteradd = '<div class="fbbttm-foodboxbooker-cartinfo">
			<a href="'.FBBTTM_CART_PAGE_LINK.'">'. esc_html__('Go to Cart ','foodboxbooker').$countcart.'</a></div>';
			$message['cartinfoafteradd'] = $cartinfoafteradd;

			$message['successmessage'] = esc_html__( 'Package is added to cart you can visit cart to proceed', 'foodboxbooker' );
			
		}
		else
		{
			
			 if(!empty($fbbttm_fullcartinfo)){	
				   	$packthere = $this->fbbttm_check_pack_available($fbbttm_fullcartinfo,'fbbttm_pack_id');

					$fbcartitem_id = $packthere['fbbttm_fbcartitem_id'];

					if(in_array($packid,$packthere['columndata']))
					{

						$foodtimecheck = $this->fbbttm_check_pack_available($fbbttm_fullcartinfo,'fbbttm_food_time');
						$cartid='';
						for($j=0;$j<count($fbcartitem_id);$j++){ 
							$cartid= $fbcartitem_id[$j];					
						}


							$data = array(
						  		'fbbttm_food_time' => $deliverytime,
								'fbbttm_lunch_qty' => $lunchqty,
								'fbbttm_dinner_qty' => $dinnerqty,
								'fbbttm_total_food' => $packageday,
								'fbbttm_booking_deliveryday' => $deliveryday,
								'fbbttm_food_startdate' => $startdate,
								'fbbttm_food_enddate' => $enddate,
								'fbbttm_lastday_info' => $lastdayinfo,
								'fbbttm_package_day' => $packageday,);

						  
	  				$fbbttm_helperobj->fbbttm_common_updatetable($cartitemtablename,$data,'fbbttm_fbcartitem_id',$cartid,'fbbttm_iteminfo_update_date');

					$message['successmessage'] = esc_html__('Update package successfully.','foodboxbooker');


				}
				else
				{
				$cartid = $cartinfo[0]->fbbttm_fdcartid;
					
					$cartiteminfo = array(
						'fbbttm_fdcartid' => $cartid,
						'fbbttm_pack_id' => $packid,
						'fbbttm_fd_id' => $fooditemid,
						'fbbttm_food_name'=>$foodname,
						'fbbttm_food_details'=>$fbbttmdetails,
						'fbbttm_food_time' => $deliverytime,
						'fbbttm_lunch_qty' => $lunchqty,
						'fbbttm_dinner_qty' => $dinnerqty,
						'fbbttm_total_food' => $packageday,
						'fbbttm_booking_deliveryday' => $deliveryday,
						'fbbttm_food_startdate' => $startdate,
						'fbbttm_food_enddate' => $enddate,
						'fbbttm_lastday_info' => $lastdayinfo,
						'fbbttm_pack_name' => $pack_name,
						'fbbttm_package_price' => $pack_price,
						'fbbttm_special_price' => $pack_specialprice,
						'fbbttm_fitem_price' => $foodprice,
						'fbbttm_package_day' => $packageday,
						'fbbttm_package_description' => $packdescription
					);


					$cartiteminfo = $fbbttm_helperobj->fbbttm_common_insert($cartitemtablename,  $cartiteminfo );

					$cartinfo = $this->fbbttm_get_basic_cartinfo();

					$cartinfocount = count($cartinfo);

					$countcart = '('.$cartinfocount.')';

					$cartinfoafteradd = '<div class="fbbttm-foodboxbooker-cartinfo">
					<a href="'.FBBTTM_CART_PAGE_LINK.'">'. esc_html__('Go to Cart ','foodboxbooker').$countcart.'</a></div>';

					$message['cartinfoafteradd'] = $cartinfoafteradd;

				    $message['successmessage'] = esc_html__('Package is added to cart you can visit cart to proceed', 'foodboxbooker' );

			      }
			    }
			    else
			    {
			    	$cartid = $cartinfo[0]->fbbttm_fdcartid;
					
					$cartiteminfo = array(
						'fbbttm_fdcartid' => $cartid,
						'fbbttm_pack_id' => $packid,
						'fbbttm_fd_id' => $fooditemid,
						'fbbttm_food_name'=>$foodname,
						'fbbttm_food_details'=>$fbbttmdetails,
						'fbbttm_food_time' => $deliverytime,
						'fbbttm_lunch_qty' => $lunchqty,
						'fbbttm_dinner_qty' => $dinnerqty,
						'fbbttm_total_food' => $packageday,
						'fbbttm_booking_deliveryday' => $deliveryday,
						'fbbttm_food_startdate' => $startdate,
						'fbbttm_food_enddate' => $enddate,
						'fbbttm_lastday_info' => $lastdayinfo,
						'fbbttm_pack_name' => $pack_name,
						'fbbttm_package_price' => $pack_price,
						'fbbttm_special_price' => $pack_specialprice,
						'fbbttm_fitem_price' => $foodprice,
						'fbbttm_package_day' => $packageday,
						'fbbttm_package_description' => $packdescription
					);


					$cartiteminfo = $fbbttm_helperobj->fbbttm_common_insert($cartitemtablename,  $cartiteminfo );

					$cartinfo = $this->fbbttm_get_basic_cartinfo();

					$countcart = count($cartinfo);
					$countcart = '('.$countcart.')';

					$cartinfoafteradd = '<div class="fbbttm-foodboxbooker-cartinfo">
					<a href="'.FBBTTM_CART_PAGE_LINK.'">'. esc_html__('Go to Cart ','foodboxbooker').$countcart.'</a></div>';
					$message['cartinfoafteradd'] = $cartinfoafteradd;
				    $message['successmessage'] = esc_html__('Package is added to cart you can visit cart to proceed', 'foodboxbooker' );
			    }
			}
		}
		return $message;
	}


	public function sanitize_array( $array ) {

	    foreach ( $array as $key => $value ) {
	        if ( is_array( $value ) ) {
	            $array[ $key ] = sanitize_array( $value ); // Recursive call for nested arrays
	        } else {
	            $array[ $key ] = sanitize_text_field( $value ); // Adjust sanitization as needed
	        }
	    }
	    return $array;
	}


	/**
	 * check pack is already added or not in cart
	 */
	public function fbbttm_check_pack_available($cartinfo, $columnname=null){
		$avail =array();

		if(!empty($cartinfo)){
			for($i=0;$i<count($cartinfo);$i++){
				$avail['columndata'][] = $cartinfo[$i]->$columnname;
				$avail['fbbttm_fbcartitem_id'][] = $cartinfo[$i]->fbbttm_fbcartitem_id;
				$avail['fbbttm_fdcartid'] = $cartinfo[$i]->fbbttm_fdcartid;
			}		
		}

		return $avail;

	}
	/*
	Cart information 
	*/

	public function fbbttm_get_cart_info($uid=null,$tokenid=null)
	{

		global $wpdb;

		$tablename = FBBTTM_TB_FOODBOOKING_CART;

		$returnresult =array();
		
		$order_placed = 0;
		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
    	// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching 
		$fbbttm_result = $wpdb->get_results(
			$wpdb->prepare(
		   // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		    "SELECT * FROM $tablename WHERE (`fbbttm_user_id` = %d AND `fbbttm_token_id` = %s) AND `order_placed` = %d ORDER BY fbbttm_user_id",
		    $uid,
		    $tokenid,
		    $order_placed
		), OBJECT);;

		//$fbbttm_result = $wpdb->get_results($query, OBJECT);


		return $fbbttm_result;

	}
	/*
	Get full cart information like cart and cart item 
	*/
	public function fbbttm_get_full_cartinfo($uid=null,$tokenid=null)
	{
		
		global $wpdb;

		$carttablename = FBBTTM_TB_FOODBOOKING_CART;
		$cartitemtablename = FBBTTM_TB_FOODBOOKING_CART_ITEMINFO;

		if ($uid == '0') {
		    $whereClause = $wpdb->prepare("ct.fbbttm_token_id = %s", $tokenid);
		} else {
		    $whereClause = $wpdb->prepare("ct.fbbttm_user_id = %d", $uid);
		}

		$order_placed = 0;
		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
    	// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching 
		$fbbttm_result = $wpdb->get_results(

			$wpdb->prepare(
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				"SELECT * FROM $carttablename AS ct 
		    INNER JOIN $cartitemtablename AS cit
		    ON ct.fbbttm_fdcartid = cit.fbbttm_fdcartid
		    WHERE $whereClause AND ct.order_placed = %d
		    ORDER BY cit.fbbttm_fbcartitem_id", $order_placed), OBJECT);

		//$fbbttm_result = $wpdb->get_results($sql, OBJECT);

		return $fbbttm_result;

	}

	public function fbbttm_remove_item_intocart($id=null)
	{
		global $wpdb;
		$cartitemtablename = FBBTTM_TB_FOODBOOKING_CART_ITEMINFO;

			if(!empty($id))
				{
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
    				// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching 
					$wpdb->delete( $cartitemtablename, array( 'fbbttm_fbcartitem_id' => $id ) );	
					return '1';
				}
		return '';		
	}


	public function fbbttm_get_cart_summaryata($cartdata=null){

		$cartsummary = array();

		$tax = 0;
		$taxpercent = FBBTTM_DEFAULT_TAX_PERCENT;
		$subtotal = 0;
		$cartgrandtotal = 0;	
		  if(!empty($cartdata)){

			for($i=0; $i<count($cartdata);$i++)
				{

					$subtotal =  $subtotal + $cartdata[$i]->fbbttm_package_price;

					$tax  = $subtotal * $taxpercent / 100;

					$cartgrandtotal = $cartgrandtotal + $tax +$cartdata[$i]->fbbttm_package_price;

				}
				
			$cartsummary['grandtotal'] = number_format((float)$cartgrandtotal, 2, '.', '');
			$cartsummary['subtotal']   = number_format((float)$subtotal, 2, '.', ''); 
			$cartsummary['tax']		   = number_format((float)$tax, 2, '.', ''); 

		 }

		return $cartsummary;

	  }

	
	 public function fbbttm_validate_packin_order_iteminfo($userid=null)
	 {

	 	global $fbbttm_orderinfoobj,$fbbttm_helperobj;

	 	$fbbttm_fullcartinfo = $this->fbbttm_get_full_cartinfo($userid,'');

	 	foreach ($fbbttm_fullcartinfo as $val)
	 	{
	 		$cartid = $val->fbbttm_fdcartid;	
	 		$cartitemid =$val->fbbttm_fbcartitem_id; 
	 		$packid = $val->fbbttm_pack_id;	
	 		$cartstartdate = $val->fbbttm_food_startdate;	
	 		$cartenddate = $val->fbbttm_food_enddate;	
	 		$packageday = $val->fbbttm_package_day;	
	 		$lunchqty = $val->fbbttm_lunch_qty;	
	 		$dinnerwty = $val->fbbttm_dinner_qty;	


	 		$orderinfo = $fbbttm_orderinfoobj->fbbttm_get_order_with_samepackid($userid,$packid);
	 		
	 		if(!empty($orderinfo)){

	 			
	 			$startdate = '';
	 			$enddate = '';

		 		foreach ($orderinfo as $value) {
					// code...
					$startdate = $value->fbbttm_food_startdate;
					
					$enddate = $value->fbbttm_food_enddate;

				}

				if($lunchqty==1 && $dinnerwty == 1)
				{

					
					$lastday = round( $packageday/2 );

					$lastday = $packageday - 1;
			
				}	
				else
				{
					$lastday = $packageday - 1;
				}

				$currentdate = gmdate('Y-m-d');
				$checkcurrentdate = strtotime($currentdate);
				$checkenddate = strtotime($enddate);

				if($checkenddate > $checkcurrentdate)		
				{

					$startdate = gmdate('Y-m-d', strtotime($enddate .' +1 day')); 

					$enddate = gmdate('Y-m-d', strtotime($startdate .' +'.$lastday.' day'));   
				}	


				$datanew = array(
					'fbbttm_food_startdate' =>$startdate,
					'fbbttm_food_enddate' =>$enddate
				);


				$updatedata = $fbbttm_helperobj->fbbttm_common_updatetable(FBBTTM_TB_FOODBOOKING_CART_ITEMINFO,$datanew,'fbbttm_fbcartitem_id',$cartitemid,'fbbttm_iteminfo_update_date');
			
			}
	 		
	 	}

    }	

}