<?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("html2fpdf/html2fpdf.php");
  
	$currencies = new currencies();
	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	if ($action==""){
		$action = (isset($HTTP_GET_VARS['firstaction']) ? $HTTP_GET_VARS['firstaction'] : '');
	}

	$orders_id=(isset($HTTP_GET_VARS['orders_id'])?(int)$HTTP_GET_VARS['orders_id']:'');
	$mode=(isset($HTTP_GET_VARS['mode'])?(int)$HTTP_GET_VARS['mode_id']:0);
	$error=(isset($HTTP_GET_VARS['error'])?$HTTP_GET_VARS['error']:'');
	$view_type=(isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'');
	if ($action=="search")
	{
		if (isset($HTTP_GET_VARS['page']) && ($HTTP_GET_VARS['page'] > 1)) $rows = $HTTP_GET_VARS['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
		$rows=0;
		$search_heading="";
		$search_group='A'; // all users both reserved and waitlist
		$query_field1=$query_field2=$query_field3=$query_field4=$query_field5=$query_field6=$query_field7=$query_field8=$query_field9=$query_field10=$query_field11="";
		if (tep_not_null($HTTP_GET_VARS['tx_fname'])){
			$query_field1=" and c.customers_firstname like '%" . tep_db_input($HTTP_GET_VARS['tx_fname']) . "%'";
			$search_heading.="First Name=" . $HTTP_GET_VARS['tx_fname'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_lname'])){
			$query_field2= " and c.customers_lastname like '%" . tep_db_input($HTTP_GET_VARS['tx_lname']) . "%'";
			$search_heading="Last Name=" . $HTTP_GET_VARS['tx_lname'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_code'])){
			$query_field3=" and o.customers_postcode='" . tep_db_input($HTTP_GET_VARS['tx_code']) . "'";
			$search_heading.="Post Code=" . $HTTP_GET_VARS['tx_code'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_words'])){
			$query_field4=" and (c.customers_firstname like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or c.customers_lastname like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or c.customers_email_address like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or e.events_location like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%')";
			$search_heading.="Keywords=" . $HTTP_GET_VARS['tx_words'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_from'])){
			$query_field5=" and s.start_date>='" . tep_db_input($HTTP_GET_VARS['tx_from']) . "'";
			$search_heading="Date From=" . $HTTP_GET_VARS['tx_from'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_until'])){
			$query_field6=" and s.start_date<='" . tep_db_input($HTTP_GET_VARS['tx_until']) . "'";
			$search_heading.="Date Until=" . $HTTP_GET_VARS['tx_until'] . "<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_waitlist'])) $search_group='W';
		if (tep_not_null($HTTP_GET_VARS['tx_reserve'])){
			if ($search_group=='W') $search_group='A';
			else $search_group='R';
		}

		if ($search_group=='W' || $search_group=='R') {
			$query_field7=" and op.events_type='" . $search_group . "'";
			$search_heading.="Events Type=" . ($search_group=='R'?'Reserved<br>':'Waitlist<br>');
		}
		
		if($HTTP_GET_VARS['tx_event']==-1)$HTTP_GET_VARS['tx_event']="";
		if (tep_not_null($HTTP_GET_VARS['tx_event'])){
			$query_field8=" and e.events_id='" . tep_db_input($HTTP_GET_VARS['tx_event']) . "'";
			$search_heading.="Event Name=" . tep_get_events_name($HTTP_GET_VARS['tx_event']) . "<br>";
		}

		if (tep_not_null($HTTP_GET_VARS['tx_session'])){
			$date_select=$HTTP_GET_VARS['tx_session'];
			$date_split=split("-",$date_select);
			$format_split=split("-",EVENTS_DATE_FORMAT);
			$date_final=array();
			// convert date to database format
			if (sizeof($date_split)==3 && sizeof($format_split)==3){
				for ($icnt=0;$icnt<sizeof($date_split);$icnt++){
					$date_final[$format_split[$icnt]]=$date_split[$icnt];
				}
				$date_select=$date_final['Y'] . '-' . $date_final['m'] . '-' . $date_final['d'];
			}
			$query_field9=" and s.start_date='" . tep_db_input($date_select) . "'";
			$search_heading.="Session Date=" . $HTTP_GET_VARS['tx_session'] . "<br>";
		}
		$query_field10="";
		if (tep_not_null($HTTP_GET_VARS['tx_rtype'])){
			$tx_rtype=$HTTP_GET_VARS['tx_rtype'];
			if ($tx_rtype=='R'){
				$query_field10=" and op.events_type='R' ";
				$search_heading.="Events Type=Reserved<br>";
			} else {
				$query_field10=" and op.events_type='W' and op.waitlist_type='" . $tx_rtype . "'";
				$search_heading.="Events Type=Waitlist<br>";
			}
		}
		if (tep_not_null($HTTP_GET_VARS['tx_current'])){
	     	$query_field11=" and s.start_date>=curdate() ";
			$search_heading.="Only Current<br>";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_pending'])){
			$cDate="curdate()";
			$pending_option=" and (o.orders_status>1 or e.events_pending_period=0 or date_add(o.date_purchased,interval e.events_pending_period day)>=" . $cDate . ")";
			$search_heading.="Hide Pending Payments=Yes<br>";
		}
	 
	$query_field12="";

	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $query_field12=" and e.events_instructor='" . $login_id ."' ";
	 
     if ($orders_id>0){
        $fields=",s.start_date,s.start_date as end_date, op.final_price as product_price,op.products_tax ";
        $query=" and o.orders_id='" . (int)$HTTP_GET_VARS['orders_id'] . "' and e.events_id='" . (int)$HTTP_GET_VARS['events_id'] ."' order by s.start_date";
	} else {
        $fields=",min(s.start_date) as start_date,count(s.start_date) as total_count,max(s.start_date) as end_date,op.products_tax,sum(op.final_price) as product_price,sum(ascii(events_type)) as refund_flag ";
		$query=$query_field1 . $query_field2 . $query_field3 . $query_field4 . $query_field5 . $query_field6 . $query_field7 . $query_field8 . $query_field9 . $query_field10 . $query_field11 . $query_field12 . ' group by o.orders_id,e.events_id order by s.start_date';
	}
		

     	
     $search_query="select o.orders_id,op.orders_products_id,op.products_id,op.waitlist_type,op.events_type,o.customers_id,c.customers_firstname,c.customers_lastname,o.customers_postcode,e.events_location,e.events_renewal_start,e.events_renewal_end,e.events_instructor,ed.events_name,e.events_id,o.payment_method,o.orders_status " . $fields . " from " . 
                     TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed " . 
		     " where op.products_type='E' and  op.events_type!='' and op.events_type!='C'" .
		     " and  op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ed.events_id=e.events_id and ed.language_id='" . (int)$languages_id . "'" . $pending_option . $query;

     $result_query=tep_db_query($search_query);
     $cnt=tep_db_num_rows($result_query);
     $action="search";
	 // print some search headings
  }
  //ob_start();
?>
<!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">
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr>
	<td width=100% align=left valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td>
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td class="pageHeading" align="center" colspan="2"><?php echo  STORE_NAME;?></td>
			</tr>
			<tr>
				<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
			</tr>
			<?php if ($search_heading!="") {?>
			<tr>
				<td class="tableHeading" width="150" valign="top"><?php echo TEXT_SEARCH_NAME;?></td>
				<td class="main"><?php echo $search_heading ;?></td>
			</tr>
			<tr>
				<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
			</tr>
			<?php } ?>
			<tr>
				<td class="tableHeading" width="80"><?php echo TEXT_DATE; ?></td>
				<td class="main"><?php echo date(EVENTS_DATE_FORMAT);?></td>
			</tr>
		</table>
		</td>
	</tr>
	<?php if ($cnt>0) {?>
		<tr>
			<td valign="top">
			<table border="0" width='100%'cellspacing="1" cellpadding="1">
				<tr class='dataTableHeadingRow'>
					<td class='dataTableHeadingContent' align="center"><?php echo '*'; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_SNO; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_FIRSTNAME; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_LASTNAME; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_POQ; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_EVENT; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_STARTDATE; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_ENDDATE; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_PAID; ?></td>
					<td class='dataTableHeadingContent' align='center'><?php echo TEXT_RESULT_WAITLIST; ?></td>
					<td class='dataTableHeadingContent' align='center'><?php echo TEXT_RESULT_RENEWAL; ?></td>
				</tr>
				<?php 
				$rows=0;$currow=0;
				$max_orders=array();
				$max_order_query=tep_db_query("SELECT max(s.start_date) as end_date,customers_id from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id=o.orders_id and op.products_id=s.sessions_id group by customers_id");
				while($max_order_result=tep_db_fetch_array($max_order_query)){
					$max_orders[$max_order_result["customers_id"]]=$max_order_result["end_date"];
				}
				while($result=tep_db_fetch_array($result_query)) { 
					$result['total_count']=(tep_not_null($result['total_count'])?$result['total_count']:1);
					$rows++;
					$currow++;
					if (strlen($rows) < 2) {
						$rows = '0' . $rows;
					}
					$question_query=tep_db_query("SELECT answer_type,answer_value from " . TABLE_ORDERS_EVENTS_ANSWERS . " where orders_id='" . (int)$result["orders_id"] . "' and events_id='" . (int)$result["events_id"] . "'");
					$question_result=tep_db_fetch_array($question_query);
					if ($question_result["answer_type"]=="D" && $question_result["answer_value"]!="") $question_result["answer_value"]=format_date(date("Y-m-d",strtotime($question_result["answer_value"])));

					$payment_method=substr($result["payment_method"],0,2);
				
				
				$paid_text=sprintf("%s",$currencies->format(tep_add_tax($result["product_price"],$result["products_tax"])));
			   ?>
				<tr class='dataTableRow' height=20>
					<td class='dataTableContent' width="15"><?php echo tep_draw_checkbox_field('attendance','y',false); ?></td>
					<td class='dataTableContent' width="40" align="right"><?php echo $rows ?></td>
					<td class='dataTableContent'><?php echo $result['customers_firstname']; ?></td>
					<td class='dataTableContent'><?php echo $result['customers_lastname']; ?></td>
					<td class='dataTableContent'><?php echo $question_result["answer_value"]; ?></td>
					<td class='dataTableContent'><?php echo $result['events_name']; ?></td>
					<td class='dataTableContent' width=60><?php echo format_date($result['start_date'],true); ?></td>
					<td class='dataTableContent' width=60><?php echo format_date($result['end_date'],true); ?></td>
					<td align="left" width="100">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td class='dataTableContent' align="right" width="10"><?php echo ($result["orders_status"]==1?"P":""); ?></td>
							<td class='dataTableContent' align="right" width="40">&nbsp;<?php echo $paid_text; ?></td>
							<td class='dataTableContent' align="left" width="10">&nbsp;<?php echo $payment_method; ?></td>
						</tr>
					</table>
					</td>
					
					<td class='dataTableContent' align='center'  width=20>
					<?php 
						if ($result['events_type'] == 'R') echo TEXT_RESERVED;
						if ($result['events_type']=='W'){
							if ($result['waitlist_type']=='V') echo '<font color="blue">' . TEXT_VIP_GROUP . '</font>';
							if ($result['waitlist_type']=='F') echo '<font color="blue">' . TEXT_FIRSTCOME_GROUP . '</font>';
							if ($result['waitlist_type']=='C') echo '<font color="blue">' . TEXT_CUSTOM_GROUP . '</font>';
						}
						if ($result['events_type']=="T") echo '<font color="red">' . TEXT_WITHDRAW . '</font>';
						if ($result['events_type']=="F") echo '<font color="red">' . TEXT_REFUND . '</font>';
					?>
					</td>
					<td class='dataTableContent' align='center'  width=10>
					<?php
						$cur_date=getServerDate();
						if (isset($max_orders[$result["customers_id"]]) && $max_orders[$result["customers_id"]]==$result["end_date"]){
							$renewal_details=tep_get_renewal_details($result,$result["customers_id"]);
							if (sizeof($renewal_details)>0 && $renewal_details['status']=='yes') echo 'Y';
							else echo 'N';
						} else {
							echo 'N';
						}
					?>
					</td>
				</tr>
				<?php } ?>
	       </table>
		   </td>
		</tr> 
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
		</tr>
		<tr>
			<td class="main"><?php echo '* - Attendance';?></td>
		</tr>
		<tr>
			<td class="main"><?php echo '<a target="_blank" href="' . DIR_WS_CATALOG . 'images/reservations.pdf">Download PDF</a>'; 
				//echo '<a href="html2fpdf/convert_pdf.php">Download PDF</a>';
				?>
			
			</td>
		</tr>
	   <?php } else { ?>
		<tr>
			<td align=left class='main' colspan=8><?php echo TEXT_NO_RESULTS ?></td>
		</tr>
	   <?php } ?>
	</table>
	</td>
</tr>
</table>
<!-- body_text_eof //-->
</body>
</html>
<?php 
	$check_dated=false;
	if ($cnt>0){
require(FILENAME_SEARCH_RESERVATION_PRINT_PDF);
}
	/*$content=ob_get_contents();
	ob_end_flush();
	$handle=fopen("html2fpdf/reservations.html","w");
	fwrite($handle,$content,strlen($content));
	fclose($handle);*/
	
require(DIR_WS_INCLUDES . 'application_bottom.php'); 

?>
