<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  include('includes/application_top.php');
  require(DIR_WS_CLASSES . '/currencies.php');
  
  $currencies=new currencies;
  $server_date = getServerDate(true);
  
  if (!isset($HTTP_SESSION_VARS['refund_details']))
    tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS, '', 'SSL'));

	$refund=$HTTP_SESSION_VARS["refund_details"];
	$reserve_ids=isset($refund["ids"])?$refund["ids"]:array();
	$type=(isset($refund['type']) ? $refund['type'] : 'single');
	$fetch_type=(isset($refund['search']) ? $refund['search'] : array());

	// if no reservation ids found just return to search reservation page
	if (sizeof($reserve_ids)<=0) {
		tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
	}
	$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");


	$search_get_params=tep_prepare_hidden_params($params,"var","get",$fetch_type);


	if ($type=='single') { // if indivitual event items selected
		$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$dates_query="SELECT op.orders_id,op.orders_products_id,s.events_id from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where " . $select_string . " and op.products_type='E' and op.events_type in('R','W','T') and op.products_id=s.sessions_id";
		$dates_fetch=tep_db_query($dates_query);
	} else { // if a single order is selected
		$split_details=split("_",$reserve_ids[0]);
		$orders_id=0;
		$events_id=0;
		if (sizeof($split_details)>1){
			$orders_id=(int)$split_details[0];
			$events_id=(int)$split_details[1];
		}
		$dates_query="SELECT op.orders_id,op.orders_products_id,s.events_id from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id='" . (int)$orders_id . "' and s.events_id='" . (int)$events_id . "' and op.products_type='E' and op.events_type in('R','W','T') and op.products_id=s.sessions_id";
		$dates_fetch=tep_db_query($dates_query);
	}	

	$dates_cnt=tep_db_num_rows($dates_fetch);

	$mail_order=array();
	while ($dates=tep_db_fetch_array($dates_fetch)){
	    tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='F' where orders_products_id='" . (int)$dates["orders_products_id"] . "'");
		if (!isset($mail_order[$dates["orders_id"]])) $mail_order[$dates["orders_id"]]=array();
		$mail_order[$dates["orders_id"]][]=$dates["orders_products_id"];
	}
	if (!isset($refund["payment"])) $refund["payment"]="manual";
	
	if (sizeof($mail_order)>0){
		reset($mail_order);

		while(list($oID,)=each($mail_order)){
			if (sizeof($mail_order[$oID])>0){
				$id_string=join(",",$mail_order[$oID]);
				$sql_array=array(	"refund_date"=>$server_date,
									"orders_id"=>$oID,
									"amount"=>$refund["amount"],
									"payment_method"=>$refund["payment"],
									"session_ids"=>$id_string
								);
								
				tep_db_perform(TABLE_EVENTS_REFUND_RESERVATIONS,$sql_array);
				$id_string=join("','",$mail_order[$oID]);
				tep_send_event_email($id_string,'WTR');
			}
		}
	}
	
    tep_session_unregister("refund_details");
    tep_session_unregister("payment");
    tep_session_unregister("payment_page");
    tep_session_unregister("payment_params");
    tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS, $search_get_params . 'action=search'));
?>