<?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;
	tep_session_unregister('payment'); 
	
	$currencies = new currencies();
	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	
	tep_session_unregister($payment);
	tep_session_unregister('payment_page');
	tep_session_unregister('payment_params');

	if ($action!="back"){
		$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');
		$fetch_type=array();
		$method="post";
		if (tep_session_is_registered("refund_details")) tep_session_unregister("refund_details");
	} else {
		$refund=isset($HTTP_SESSION_VARS["refund_details"])?$HTTP_SESSION_VARS["refund_details"]:array();
		$reserve_ids=isset($refund["ids"])?$refund["ids"]:array();
		$type=(isset($refund['type']) ? $refund['type'] : 'single');
		$fetch_type=(isset($refund['search']) ? $refund['search'] : array());
		$method="var";
	}
	// 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_post_params=tep_prepare_hidden_params($params,$method,"post",$fetch_type);
	$search_get_params=tep_prepare_hidden_params($params,$method,"get",$fetch_type);
	

	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,op.final_price,op.products_tax 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,op.final_price,op.products_tax 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);

     /*$temp_query=tep_db_query("select date_sub('" . $result['start_date'] . "',interval '14' day) as date1,date_sub('" . $result['start_date'] . "',interval '7' day) as date2,date_sub('" . $result['start_date'] . "',interval '2' day) as date3");
     $date_result=tep_db_fetch_array($temp_query);
     $amount=0;
     $amount=tep_add_tax($result['final_price']*$result['products_quantity'],$result['products_tax']);
     if ($result['date_purchased']<=$date_result['date3'])
        $refund_amount=$amount-($amount*((int)$result['refund_percentage3'])/100.0);
     else if ($result['date_purchased']<=$date_result['date2'])
        $refund_amount=$amount-($amount*((int)$result['refund_percentage2'])/100.0);
     else if ($result['date_purchased']<=$date_result['date1'])
        $refund_amount=$amount-($amount*((int)$result['refund_percentage1'])/100.0);
     else 
        $refund_amount=$amount;*/
	if ($refund_flag){
     $payment_modules = new payment();
	}
?>
<!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">
	<!-- 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">
		<?php if ($refund_flag){ ?>
		<script language=javascript>	
		function selectRowEffectRefund(object, buttonSelect) {
			if (!selected) {
				if (document.getElementById) {
					selected = document.getElementById('defaultRow');
				} else {
					selected = document.all['defaultRow'];
				}
			}
		
			if (selected) selected.className = 'moduleRow';
			object.className = 'moduleRowSelected';
			selected = object;
		
		// one button is not an array
			if (document.refund_reservation.payment[0]) {
				document.refund_reservation.payment[buttonSelect].checked=true;
			} else {
				document.refund_reservation.payment.checked=true;
			}
		}
		
		function rowOverEffectRefund(object) {
			if (object.className == 'moduleRow') object.className = 'moduleRowOver';
		}
		
		function rowOutEffectRefund(object) {
			if (object.className == 'moduleRowOver') object.className = 'moduleRow';
		}
		</script>
		<?php 
		
		if (isset($HTTP_GET_VARS['payment_error']) && is_object(${$HTTP_GET_VARS['payment_error']}) && ($error = ${$HTTP_GET_VARS['payment_error']}->get_error())) {?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td align=left class='pageHeading' colspan='2'><?php echo tep_output_string_protected($error['title']); ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBoxNotice">
					<tr class="infoBoxNoticeContents">
						<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
								<td class="main" width="100%" valign="top"><?php echo tep_output_string($error['error']); ?></td>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
							</tr>
						</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		<?php } else if (isset($HTTP_GET_VARS['error_message'])) { ?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBoxNotice">
					<tr class="infoBoxNoticeContents">
						<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
								<td class="main" width="100%" valign="top"><?php echo tep_check_error(tep_output_string($HTTP_GET_VARS['error_message'])); ?></td>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
							</tr>
						</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		<?php }	
		} // if refund
		?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td align=left class='pageHeading' colspan='2'><?php echo HEADING_REFUND_TITLE ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
			</tr>	  
			<tr>
				<td>
				<table border="0" cellspacing="4" cellpadding="2" width=50%>
				<?php 
					if ($customer_cnt>0) {
						$customer=tep_db_fetch_array($customer_fetch);	
						echo tep_draw_form('refund_reservation',FILENAME_REFUND_CONFIRM_RESERVATIONS);
						echo $search_post_params;
						for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
							echo '<input type="hidden" name="reservation_id[]" value="' . $reserve_ids[$icnt] . '">';
						}
						echo '<input type="hidden" name="process_type" value="' . $type . '">';
				?>
						<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">
							<?php 	
									echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
									if ($dates_cnt>0){ // if dates found
										$icnt=1;
										$date_string="";
										$refund_amount=0;
										while($dates=tep_db_fetch_array($dates_fetch)){
											$date_string.=format_date($dates["start_date"]) . ", ";
											$refund_amount+=tep_add_tax($dates["final_price"],$dates["products_tax"]);
											$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;
									} else {
										echo TEXT_NO_REFUND_DATES;
									}
							?>
							</td>
						</tr>
						<?php
						echo tep_get_attendees($customer['orders_id'],$customer['customers_id']);
						 if ($dates_cnt>0){ 
						?>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
							</tr>
							<?php if ($refund_flag) {?>
							<tr>
								<td class=main><b><?php echo TEXT_PAYMENT_METHOD; ?></b></td>
							</tr>
							<tr>
							<td colspan=2>
							<table border="0" width="100%" cellspacing="1" cellpadding="2">
								<tr class="infoBoxContents">
								<td>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
								<?php
									$selection = $payment_modules->selection();
									$radio_buttons = 0;
									for ($i=0, $n=sizeof($selection); $i<$n; $i++) {
										if ( ($selection[$i]['id'] == $payment) || ($n == 1) ) {
											echo '                  <tr id="defaultRow" class="moduleRowSelected" onmouseover="rowOverEffectRefund(this)" onmouseout="rowOutEffectRefund(this)" onclick="selectRowEffectRefund(this, ' . $radio_buttons . ')">' . "\n";
										} else {
											echo '                  <tr class="moduleRow" onmouseover="rowOverEffectRefund(this)" onmouseout="rowOutEffectRefund(this)" onclick="selectRowEffectRefund(this, ' . $radio_buttons . ')">' . "\n";
										}
								?>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
									<td class="main"><b><?php echo $selection[$i]['module']; ?></b></td>
									<?php
										echo '<td>';
										if (sizeof($selection) > 1) {
											echo tep_draw_radio_field('payment', $selection[$i]['id']);
										} else {
											echo tep_draw_hidden_field('payment', $selection[$i]['id']);
										}
									?>
								</tr>
								<?php
									if (isset($selection[$i]['error'])) {
								?>
								<tr>
									<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
									<td class="main"><?php echo $selection[$i]['error']; ?></td>
								</tr>
								<?php
									} elseif (isset($selection[$i]['fields']) && is_array($selection[$i]['fields'])) {
								?>
									<tr>
										<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
										<td>
										<table border="0" cellspacing="0" cellpadding="2">
										<?php
										for ($j=0, $n2=sizeof($selection[$i]['fields']); $j<$n2; $j++) {
										?>
											<tr>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo $selection[$i]['fields'][$j]['title']; ?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo $selection[$i]['fields'][$j]['field']; ?></td>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
											</tr>
										<?php
										}
										if ($selection[$i]['module']=='Test') {
										?>
											<tr>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo ENTRY_CREDIT_CARD_CVV; ?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo tep_draw_input_field('update_info_cc_cvv','','size=10');?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
											</tr>
										<?php } ?>
										</table>
										</td>
									</tr>
								<?php
								}
								$radio_buttons++;
							  }
							?>
							</table>
							</td>
						</tr>
						</table>
						</td>
						</tr>
						<?php 
 						 echo tep_get_attendees($customer['orders_id'],$customer['customers_id']);
						 } // refund flag ?> 
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_AMOUNT; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . '<input type="text" name="refund_amount" value="' . $refund_amount . '" size=15 maxlength=15>'; ?></td>
						</tr>
				<?php  } // dates_cnt
					} else { // if customer cnt ?>
					<tr>
						<td class="tableHeading" colspan="2"><?php echo TEXT_NO_RESULTS; ?></td>
					</tr>
				<?php }?>
					<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_SEARCH_RESERVATIONS,$search_get_params . 'action=search') . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
									if ($customer_cnt>0 && $dates_cnt>0) echo tep_image_submit('button_refund.gif', IMAGE_REFUND_RESERVATION); 
							?>
						</td>
					</tr>
			</table>
			</form>
			</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;
}
 ?>
