<?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 );

  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require(DIR_WS_CLASSES . 'payment.php');
	$refund_flag=false;

	if ($refund_flag){
		$payment=(isset($HTTP_POST_VARS['payment'])?$HTTP_POST_VARS['payment']:'');
		if (!tep_session_is_registered('payment')) tep_session_register('payment');
		
		$payment = $HTTP_POST_VARS['payment']; 
	
	
	  
		$payment_modules = new payment($payment);
	}
	$reserve_ids=isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array();
	
	$type=(isset($HTTP_POST_VARS['process_type']) ? $HTTP_POST_VARS['process_type'] : 'single');
	// 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_params=tep_prepare_hidden_params($params,"post","post");

	if ($type=='single') { // if indivitual event items selected
		$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$customer_query="select o.orders_id,c.customers_id,c.customers_firstname,c.customers_lastname,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS  . " c " .
						" where " . $select_string . " and op.orders_id=o.orders_id and o.customers_id=c.customers_id group by customers_id ";
		$customer_fetch=tep_db_query($customer_query);
		$dates_query="SELECT op.orders_products_id,s.start_date 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 order by s.start_date";
		$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];
		}
		$customer_query="select o.orders_id,c.customers_id,c.customers_firstname,c.customers_lastname,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS  . " c " .
						" where op.orders_id='" . (int)$orders_id . "' and op.orders_id=o.orders_id and o.customers_id=c.customers_id group by customers_id ";
		$customer_fetch=tep_db_query($customer_query);

		$dates_query="SELECT op.orders_products_id,s.start_date 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 order by s.start_date";
		$dates_fetch=tep_db_query($dates_query);
	}	
	$customer_cnt=tep_db_num_rows($customer_fetch);
	$dates_cnt=tep_db_num_rows($dates_fetch);
	
	if ($customer_cnt<=0 || $dates_cnt<=0) tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));

	if (!tep_session_is_registered("refund_details")) tep_session_register("refund_details");
	
	$search_details=array("tx_fname"=>'',"tx_lname"=>'',"page"=>1,"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"=>'');
	reset($search_details);	
	while(list($key,)=each($search_details)){
		if (isset($HTTP_POST_VARS[$key])) $search_details[$key]=tep_db_prepare_input($HTTP_POST_VARS[$key]);
	}
	
	$refund_details=array("ids"=>$reserve_ids,'type'=>$type,'search'=>$search_details,'amount'=>0,'payment'=>'');
	
	if ($refund_flag){
		if ( (is_array($payment_modules->modules)) && (sizeof($payment_modules->modules) > 1) && (!is_object($$payment))) {
			/*echo tep_draw_form('post_form',FILENAME_REFUND_RESERVATIONS,'&error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED,'SSL'));
			echo $search_params;
			for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
				echo '<input type="hidden" name="reservation_id[]" value="' . $reserve_ids[$icnt] . '">';
			}
			echo '</form>';
			echo '<script language="javscript">document.post_form.submit();</script>';*/
			tep_redirect(tep_href_link(FILENAME_REFUND_RESERVATIONS,'action=back&error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED)));
		}
	}
	tep_session_register('payment_page');
	tep_session_register('payment_params');
	$payment_page='refund';
	$payment_params='action=back&';

	if ($refund_flag){
		if (is_array($payment_modules->modules)) {
			$payment_modules->pre_confirmation_check();
		}
		$refund_details["payment"]=$payment;
	}
	$customer=tep_db_fetch_array($customer_fetch);
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<?php  $currencies = new currencies();
?>
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->	  
    <td width=100% align=left valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">	
	<tr>
		<td>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td align=left class='pageHeading' colspan='2'><?php echo HEADING_REFUND_CONFIRM_TITLE ?></td>
			</tr>
		</table>
		</td>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>	  
		<tr>
	    	<td>
			<table border="0" cellspacing="4" cellpadding="2">
			<?php  
			if ($refund_flag && isset($$payment->form_action_url)) {
				echo "<form name='checkout_confirmation' action='" . $$payment->form_action_url . "' method=post>";
			} else {
				echo tep_draw_form('checkout_confirmation', FILENAME_REFUND_PROCESS);
			} 
			?>
	        <tr align="left">
				<td class="tableHeading"><?php echo TEXT_REFUND_FIRST_NAME ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_firstname']; ?></td>
			</tr>
			<tr align="left">
				<td class="tableHeading"><?php echo TEXT_REFUND_LAST_NAME; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_lastname']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_ADDRESS; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_street_address']; ?></td>
			</tr> 
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_SUBRUB; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_suburb'];?></td>
			</tr> 
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_POSTCODE; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_postcode']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_CITY; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_city']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_STATE; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_state']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_COUNTRY; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_country']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading" valign="top"><?php echo TEXT_REFUND_DATES; ?></td>
				<td class="main" nowrap>
				<?php 	
						echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
						$icnt=1;
						$date_string="";
						while($dates=tep_db_fetch_array($dates_fetch)){
							$date_string.=format_date($dates["start_date"]) . ", ";
							$icnt++;
							if ($icnt%4==0) $date_string.='<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
						}
						if (substr($date_string,-2)==", ") $date_string=substr($date_string,0,-2);
						echo $date_string;
				?>
				</td>
			</tr>
			<?php echo  tep_get_attendees($customer['orders_id'],$customer['customers_id']);?>
	        <tr>
			   <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
	        </tr>
			<?php
			if ($refund_flag && tep_not_null($payment)){
				$confirmation = $payment_modules->confirmation();
			?>
				<tr>
					<td class="tableHeading"><b><?php echo TEXT_PAYMENT_METHOD; ?></b></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $GLOBALS[$payment_modules->selected_module]->title;//echo $confirmation['title']; ?></b></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
				</tr>
				<?php
					for ($j=0, $n2=sizeof($confirmation['fields']); $j<$n2; $j++) {
				?>
					<tr>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $confirmation['fields'][$j]['title']; ?></td>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' .$confirmation['fields'][$j]['field']; ?></td>
					</tr>
					<?php
					}
				}
				?>
					<tr>
						<td class="tableHeading"><?php echo TEXT_REFUND_AMOUNT; ?></td>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $currencies->format($HTTP_POST_VARS['refund_amount']); ?></td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
					</tr>
					<tr>
						<td align=right colspan="2" valign="middle">
						<?php
							echo '<a href="' . tep_href_link(FILENAME_REFUND_RESERVATIONS,'action=back') . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
							$amount=$HTTP_POST_VARS['refund_amount'];
							$order_detail=array('info'=>array('total'=>tep_get_rounded_amount($amount)));
							$order=new objectInfo($order_detail);
							if (is_array($payment_modules->modules)) {
								echo $payment_modules->process_button();
							}
							if ($order->info['total']>0) 
								echo tep_image_submit('button_confirm_refund.gif', IMAGE_REFUND_CONFIRM);
							$refund_details["amount"]=$order->info["total"];
						?>
						</td>
					</tr>
					</form>
					</table>
				</td>
			</tr>
      </table>
      </td>
</tr>
</table>
<!-- body_text_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 
function  tep_get_attendees($orders_id,$customers_id){
	if($orders_id=='' || $customers_id=='') return;
	$multiple_attendees_name="";	
	$query=tep_db_query("select concat(c.customers_lastname,' ',c.customers_firstname) as name from ".TABLE_CUSTOMERS ." c, " .	TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c where c2c.customer_id=c.customers_id and c2c.group_customer_id=".(int)$customers_id." and  c2c.orders_id=".(int)$orders_id." and c2c.group_customer_id!=c.customers_id");
	if(tep_db_num_rows($query)>0){	
		while($result=tep_db_fetch_array($query)){
			$multiple_attendees_name.=$result['name'].",";
		}
	}
	if($multiple_attendees_name)	$multiple_attendees_name="<tr><td class='TableHeading' valign='top'>".TEXT_ATTENDEES."</td><td class='main' nowrap>".substr($multiple_attendees_name,0,-1)."</td></tr>";
	return $multiple_attendees_name;
}

?>