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

class fbbttmOrderinfo
{

	public function fbbttm_get_all_orderlist_packend(){

		global $wpdb, $fbbttm_helperobj;

		$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
		$wpusers_table  =FBBTTM_WP_USER;
		$table_fbbttm_delivery_address  = FBBTTM_DELIVERY_ADDRESS;

		$orderinfo =array();

		$mainlastorderdata = array();

		$iteminfo =array();
		
		$orderinfojak = array();
		$packwise = array();
		$mainpackarray = array();

		$packwise['packid'] = array();
		$packwise['userid'] = array();
		$packwise['packwise'] = array();

		$users = get_users( array( 'fields' => array( 'ID' ) ) );

		 if(!empty($users)){
			foreach($users as $user){

					$id = $user->ID;

					// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
						$fbbttm_result = $wpdb->get_results(
					// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					
						$wpdb->prepare(
					    "SELECT * FROM $foodbooking_table AS FB 
					    LEFT JOIN $wpusers_table AS wu ON wu.ID = FB.fbbttm_user_id 
					    WHERE FB.fbbttm_user_id = %d 
					    ORDER BY FB.fbbttm_fdbid DESC"

					    ,
					    $id
					), OBJECT);

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

					 if(!empty($fbbttm_result)){		  	
					

						$iteminfo = $wpdb->get_results( 
				//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
							// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
						$wpdb->prepare(
							// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
							   "SELECT * FROM $foodbooking_table AS FB 
							   LEFT JOIN $foodbookingitem_table AS FBITEM ON FBITEM.fbbttm_fdbid = FB.fbbttm_fdbid 
							   WHERE FB.fbbttm_user_id = %d 
							   ORDER BY FBITEM.fbbttm_fbitem_id DESC",
							   $id
						), OBJECT);

	
						
						foreach ($iteminfo as $datat)
						{

							if(!in_array($datat->fbbttm_user_id,$packwise['userid'])){
								$packwise['userid'][] = $datat->fbbttm_user_id;
							}
							if(!in_array($datat->fbbttm_pack_id,$packwise['packid'][$datat->fbbttm_user_id])){
								$packwise['packid'][$datat->fbbttm_user_id][] = $datat->fbbttm_pack_id;
								$packwise['packwise'][$datat->fbbttm_user_id][] = $datat;
							}
							$mainpackarray['mainpackarray'][] = $packwise;
						}
				 	 }
				  }
				}
				$orderinfo['orderiteminfo'] = $packwise;
				$mainlastorderdata = $orderinfo;
				return $orderinfo;
			 }

	public function fbbttm_get_full_orderinfo_byid($id=null){

		global $wpdb, $fbbttm_helperobj;

		$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
		$wpusers_table  =FBBTTM_WP_USER;
		$table_fbbttm_delivery_address  = FBBTTM_DELIVERY_ADDRESS;

		$orderinfo = array();

	
		$fbbttm_result = $wpdb->get_results(

		//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
		$wpdb->prepare(
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

			    "SELECT * FROM $foodbooking_table AS FB 
			    LEFT JOIN $wpusers_table AS wu ON wu.ID = FB.fbbttm_user_id 
			    WHERE FB.fbbttm_fdbid = %d 
			    ORDER BY FB.fbbttm_fdbid DESC",
			    $id
			)
		, OBJECT);

		

		$orderinfo['orderuser'] = $fbbttm_result;

	  
		$iteminfo = $wpdb->get_results(
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->prepare(

		    "SELECT * FROM $foodbookingitem_table WHERE fbbttm_fdbid = %d ORDER BY fbbttm_fbitem_id DESC", $id		)
		    , OBJECT);

	
		$orderinfo['orderiteminfo'] = $iteminfo;

		return $orderinfo;
	}


	/**
	 * Order list by user id
	 *  
	 */

	public function fbbttm_get_orderlist_byuserid($id=null)
	{
		global $wpdb, $fbbttm_helperobj;

		$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
		$wpusers_table  =FBBTTM_WP_USER;
		$orderinfo =array();
		
		$fbbttm_result = $wpdb->get_results(
			//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
		  // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
		 $wpdb->prepare("SELECT * FROM $foodbooking_table AS FB LEFT JOIN $wpusers_table AS wu ON wu.ID = FB.fbbttm_user_id WHERE FB.fbbttm_user_id = %d ORDER BY FB.fbbttm_fdbid DESC",$id),
		  OBJECT);

	
		if(empty($fbbttm_result)) return $fbbttm_result;	

		$orderinfo['orderinfo'] = $fbbttm_result;

		 $iteminfo = $wpdb->get_results(
		 //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
		// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching	
		 $wpdb->prepare("SELECT * FROM $foodbooking_table AS FB  LEFT JOIN $foodbookingitem_table AS FBITEM ON FBITEM.fbbttm_fdbid = FB.fbbttm_fdbid   WHERE FB.fbbttm_user_id = %d ORDER BY FBITEM.fbbttm_fbitem_id DESC",$id),
		  OBJECT);

		if(empty($iteminfo)) return $iteminfo;
		$orderinfo['orderiteminfo'] = $iteminfo;
		return $orderinfo;
	}

	/**
	 * Order list by user id and groupby
	 *  
	 */

	public function fbbttm_get_orderlist_byuserid_groupby($id=null)
	{

		global $wpdb, $fbbttm_helperobj;

		$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
		$wpusers_table  =FBBTTM_WP_USER;

		$orderinfo =array();


		$iteminfo =array();
		$packwise = array();
		$packwise['packid'] = array();

		$fbbttm_result = $wpdb->get_results(
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching	
			$wpdb->prepare("SELECT * FROM $foodbooking_table AS FB LEFT JOIN $wpusers_table AS wu ON wu.ID = FB.fbbttm_user_id WHERE FB.fbbttm_user_id = %d ORDER BY FB.fbbttm_fdbid DESC",$id), OBJECT);


		if(empty($fbbttm_result)) return $fbbttm_result;	

		$orderinfo['orderinfo'] = $fbbttm_result;
		
		$iteminfo = $wpdb->get_results(
			$wpdb->prepare("SELECT * FROM $foodbooking_table AS FB  LEFT JOIN $foodbookingitem_table AS FBITEM ON FBITEM.fbbttm_fdbid = FB.fbbttm_fdbid   WHERE FB.fbbttm_user_id = %d  ORDER BY FBITEM.fbbttm_fbitem_id ASC",$id),
			 OBJECT);

		foreach ($iteminfo as $datat)
		{
			if(!in_array($datat->fbbttm_pack_id,$packwise['packid']))
			  {
				 $packwise['packid'][] = $datat->fbbttm_pack_id;
			  }
			$packwise['packwise'][$datat->fbbttm_pack_id][]= $datat;
		}

		if(empty($iteminfo)) return $iteminfo;

		$orderinfo['orderiteminfo'] = $packwise;

		return $orderinfo;
	}


	public function fbbttm_get_orderinfo_byorder_key($orderkey=null,$orderid=null)
	{

		global $wpdb, $fbbttm_helperobj;

		$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
		$wpusers_table  =FBBTTM_WP_USER;
		$table_fbbttm_delivery_address  = FBBTTM_DELIVERY_ADDRESS;
	
		$orderinfo =array();

				if (!empty($orderkey)) {
					$whereclause = "FB.fbbttm_order_key = %s";
					$wherevalue = $orderkey;
				} else {
					$whereclause = "FB.fbbttm_fdbid = %d";
					$wherevalue = $orderid;
				}

				$query = "
					SELECT * 
					FROM $foodbooking_table AS FB 
					LEFT JOIN $wpusers_table AS wu ON wu.ID = FB.fbbttm_user_id 
					LEFT JOIN $table_fbbttm_delivery_address AS da ON da.fbbttm_user_id = wu.ID 
					WHERE $whereclause 
					ORDER BY FB.fbbttm_fdbid DESC";

					$fbbttm_result = $wpdb->get_results(
					//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

					// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching	
					$wpdb->prepare(
						$query  // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
						, $wherevalue),
					OBJECT);

		if(empty($fbbttm_result)) return $fbbttm_result;	
	
		$orderinfo['orderinfo'] = $fbbttm_result;
		
		$query = "SELECT * FROM $foodbooking_table AS FB  LEFT JOIN $foodbookingitem_table AS FBITEM ON FBITEM.fbbttm_fdbid = FB.fbbttm_fdbid   WHERE $whereclause ORDER BY FBITEM.fbbttm_fbitem_id DESC";

		$iteminfo = $wpdb->get_results(
			$wpdb->prepare(
			$query // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
					// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			, $wherevalue),OBJECT);
		
		if(empty($iteminfo)) return $iteminfo;

		$orderinfo['orderiteminfo'] = $iteminfo;

		return $orderinfo;
	}

	/**
	  * function for check order available with same pack id and user id
	  * 
	  */ 
	public function fbbttm_get_order_with_samepackid($userid=null, $packid=null)
	  {

	  	global $fbbttm_helperobj,$wpdb;

	  	$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
		$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;

		$query = $wpdb->prepare("SELECT * FROM $foodbooking_table AS FB  LEFT JOIN $foodbookingitem_table AS FBITEM ON FBITEM.fbbttm_fdbid = FB.fbbttm_fdbid WHERE FB.fbbttm_user_id = %d and FBITEM.fbbttm_pack_id = %d ORDER BY FBITEM.fbbttm_fbitem_id DESC  LIMIT 1",$userid,$packid);

		$iteminfo = $wpdb->get_results(
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
			$query, OBJECT);

	  	return $iteminfo;
	  }
	  

	/**
	 * function for packinfo and packidreturn 
	 */ 

	public function fbbttm_iteminfo_forrenew($id=null)
	{

		global $fbbttm_helperobj,$wpdb;

		$bookingiteminfo = array();
		$iteminfo = array();

		$tableinfodata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_TB_FOODBOOKING_ITEMINFO,'fbbttm_fbitem_id',$id);

		if(empty($tableinfodata)){
			return $iteminfo;
		}

		foreach($tableinfodata as $tabdata)
		 {
			$iteminfo['fbbttm_pack_id'] = $tabdata->fbbttm_pack_id;
		 }
			
			$packid = $iteminfo['fbbttm_pack_id'];
			
			$packifo = $fbbttm_helperobj->fbbttm_get_packinfo($packid);
			

			$fooditemid = $packifo['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] : '';

			$packinfodiv = '<div class="fbbttm-packinfo">
			<h2>'.esc_attr('Package information','foodboxbooker').'</h2>
			<div class="fbbttm-packcontent">
				<p><span class="fbbttm-packtext">'.esc_html__('Product Name','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($foodname).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Product Description','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($fbbttmdetails).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Package Name','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['pack_name']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Package Description','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['packdescription']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Food Price for single day','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['fooditemprice']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Package day','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['packageday']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Package Special price','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['pack_specialprice_withsymbol']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('Package Price','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html($packifo['pack_price']).' </span>  </p>
				<p><span class="fbbttm-packtext">'.esc_html__('You save money','foodboxbooker').' 
				:-<span class="fbbttm-value">'.esc_html((!empty($packifo['savemoney']))?  $packifo['savemoney'] : 0).' </span>  </p>
			</div>
			</div>';

			$iteminfo['packinfodiv'] = $packinfodiv;

			return $iteminfo;	

	}

	
}