<?php /*osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2003 osCommerce osCommRes, Services Oline http://www.oscommres.com Copyright (c) 2005 osCommRes Freeway eCommerce http://www.zac-ware.com/freeway Copyright (c) 2007 ZacWareReleased under the GNU General Public License */
//  require('includes/application_top.php');if (!tep_session_is_registered('customer_id')) {$navigation->set_snapshot();tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));}if (!isset($HTTP_POST_VARS['orders_products_id']) || (isset($HTTP_POST_VARS['orders_products_id']) 


// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	

  require('includes/application_top.php');

	if (!tep_session_is_registered('customer_id')) {
		$navigation->set_snapshot();
		tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
	}
	
	if (!isset($HTTP_POST_VARS['orders_products_id']) || (isset($HTTP_POST_VARS['orders_products_id']) && !is_array($HTTP_POST_VARS['orders_products_id']))){
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_RESERVATIONS, '', 'SSL'));	
	}
  
	$orders_id=(int)$HTTP_POST_VARS["orders_id"];
	$events_id=(int)$HTTP_POST_VARS["events_id"];
	$orders_products_id=tep_db_prepare_input($HTTP_POST_VARS["orders_products_id"]);
	
	$customer_info_query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id = '". tep_db_input($orders_id) . "'");
	$customer_info = tep_db_fetch_array($customer_info_query);

	if ($customer_info['customers_id'] != $customer_id) {
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_RESERVATIONS, '', 'SSL'));
	}

	$reservation_ids="";	
	for ($icnt=0;$icnt<sizeof($orders_products_id);$icnt++){
		$reservation_ids.="," . $orders_products_id[$icnt];
	}
	$reservation_ids=substr($reservation_ids,1);
	$reservation_query = tep_db_query("select op.orders_products_id,o.orders_id, o.date_purchased, op.products_id,op.products_name,op.final_price,op.products_tax,op.products_quantity, op.events_type,op.products_name ,time_format(e.events_start_time,'%h:%i %p') as events_start_time, time_format(e.events_end_time,'%h:%i %p') as events_end_time, e.events_instructor, s.start_date from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e where " .
					   "op.orders_products_id in (" . tep_db_input($reservation_ids) . ") and o.orders_id=op.orders_id and op.products_id=s.sessions_id and e.events_id=s.events_id order by s.start_date");

	if (tep_db_num_rows($reservation_query)<=0)
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_RESERVATIONS, '', 'SSL'));
     
	$action=isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'';
	if ($action=='withdraw')
	{
		while($reservation_result=tep_db_fetch_array($reservation_query))
		{
			/*$sql_data_array = array(
									'refund_order_id' => $reservation_result['orders_id'],
									'refund_sessions_id' => $reservation_result['products_id'],
									'refund_customers_id' => $customer_id,
									'refund_events_tax' =>$reservation_result['products_tax'],
									'refund_withdrawl'=>'W',
									'refund_date' => 'now()'
									);
			tep_db_perform(TABLE_REFUND_RESERVATIONS, $sql_data_array);*/
			
			tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='T' where orders_products_id='" . $reservation_result['orders_products_id'] . "'");
	    }
    	tep_check_waiting_list((int)$HTTP_POST_VARS['events_id']);
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_RESERVATIONS, '', 'SSL'));
	}
	$reservation_result[]=array('name'=>'','reserve_dates'=>'','amount'=>0,'start_time'=>'','end_time'=>'','instructor'=>'','tax'=>0);
	$cnt=0;
	$amount=0.0;
	$tax=0.0;
	while($reservation=tep_db_fetch_array($reservation_query))
	{
		if ($date_string!="") $date_string.=", ";
		if ($cnt%4==1 && $cnt>4) $date_string.='<br>';
		$date_string.=date(EVENTS_DATE_FORMAT,strtotime($reservation['start_date']));
		if ($cnt==0){
			$reservation_result['name']=$reservation['products_name'];
			$reservation_result['start_time']=$reservation['events_start_time'];
			$reservation_result['end_time']=$reservation['events_end_time'];
			$reservation_result['instructor']=$reservation['events_instructor'];
			$reservation_result['tax']=$reservation['products_tax'];
		}
		$amount+=$reservation['final_price'];
		$cnt++;
	}
	$reservation_result['amount']=$amount;
	$reservation_result['reserve_dates']=$date_string;
  
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_WITHDRAW_RESERVATIONS);
	
	$breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
	$breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_ACCOUNT_RESERVATIONS, '', 'SSL'));
	$breadcrumb->add(sprintf(NAVBAR_TITLE_3, $reservation_result['products_name'], tep_href_link(FILENAME_WITHDRAW_RESERVATIONS, tep_get_all_get_params(), 'SSL')));
	
	$content = CONTENT_WITHDRAW_RESERVATIONS;
	$javascript = 'popup_window.js';
	require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
	
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
