<?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');  
  tep_get_last_access_file();
  require('includes/classes/currencies.php');
  require(DIR_WS_FUNCTIONS . 'events_tree.php');
  require('includes/classes/pdfTable.php');
  require(DIR_WS_CLASSES . 'split_page_results_report.php');
  define(BOX_WIDTH1,'125');
	// get initial parameters, try to load from the session
	if (isset($HTTP_GET_VARS["return"]) && isset($HTTP_SESSION_VARS["rep_params"])){
		$input_params=&$HTTP_SESSION_VARS["rep_params"];
	} else {
		$input_params=&$HTTP_POST_VARS;
		if (isset($input_params["post_action"])){
			tep_set_session_value("rep_params",$HTTP_POST_VARS);
			$GLOBALS["rep_params"]["post_action"]="screen";
		} else {
			tep_set_session_value("rep_params",array());		}
	}
	$date=isset($input_params['txtDate'])?tep_convert_date_raw($input_params['txtDate']):getServerDate();
	$instructor=isset($input_params['sel_instructor'])?$input_params['sel_instructor']:-1;
	$view=isset($input_params['sel_view'])?$input_params['sel_view']:1;
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$event=isset($input_params['sel_event'])?$input_params['sel_event']:-1;
	$post_action = isset($input_params['post_action'])?$input_params['post_action']:"";
	$output_pdf=isset($input_params['chk_output_pdf'])?true:false;
	$output_excel=isset($input_params['chk_output_excel'])?true:false;
	$show_pending = isset($input_params['chk_pending'])?true:false;
	$show_waiting = isset($input_params['chk_waiting'])?true:false;
	$page = isset($input_params['page'])?$input_params['page']:1;

if ($post_action==""){
	$show_pending=true;
	$show_waiting=true;
	$output_pdf=true;
		$post_action="screen";
}

$search_header="";
$date_begin = $date;
$date_end = $date;	
$days= date('t',strtotime($date));	
	
	if($view==2) {
		$sql =  "select date_add('$date', interval -(dayofweek('$date')-1) day) begin, date_add('$date', interval (7-dayofweek('$date')) day) end";	
	} else if($view==3) {
		$sql =  "select date_add('$date', interval -(dayofmonth('$date')-1) day) begin, date_add('$date', interval ('$days'-dayofmonth('$date')) day) end";	
	}
	if($view==2 || $view==3) {
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$date_end = $row["end"];	
	}
	// create header text for pdf page
	$display_header=TEXT_DAY .  ":&nbsp;&nbsp;" . $date . "&nbsp;&nbsp;&nbsp;&nbsp;";
	if ($view==1) {
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_DAY . "\t";
	} else if ($view==2){
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_WEEK . "\t";
	} else {
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_MONTH . "\t";
	}
	if ($instructor=="-1" && $login_groups_type!=TEXT_INSTRUCTOR_ENTRY) $display_header.=TEXT_INSTRUCTOR . ":&nbsp;&nbsp;" . TEXT_ALL_INSTRUCTORS ."\t";
	if ($location=="-1") $display_header.=TEXT_LOCATION . ":&nbsp;&nbsp;" . TEXT_ALL_LOCATIONS  ."\t";
	if ($event=="-1") $display_header.=TEXT_EVENT . ":&nbsp;&nbsp;" . TEXT_ALL_EVENTS  ."\t";
	
	$instructors_array = array();
	$events_array = array();
	$locations_array=array();

	$view_array=array(array("id"=>1,"text"=>TEXT_DAY),array("id"=>2,"text"=>TEXT_WEEK),array("id"=>3,"text"=>TEXT_MONTH));	
	
	if ($login_groups_type!=TEXT_INSTRUCTOR_ENTRY){
		// get instructor names			
		$sql = "select a.admin_id admin_id, admin_firstname, admin_lastname  from events_instructors ei, admin a where ei.admin_id=a.admin_id order by admin_firstname,admin_lastname";
		$sql_result = tep_db_query($sql);
		$num_rows = tep_db_num_rows($sql_result);
		$instructors_array[]=array("id"=>-1,"text"=>TEXT_ALL_INSTRUCTORS);
		while($row=tep_db_fetch_array($sql_result)) {
			$instructors_array[]=array("id"=>$row["admin_id"],"text"=>$row["admin_firstname"]." ".$row["admin_lastname"]);
			if ($row["admin_id"]==$instructor){
				$display_header.=TEXT_INSTRUCTOR  . ":&nbsp;&nbsp;" . $row["admin_firstname"]." ".$row["admin_lastname"] . "\t";
			}
		}
		mysql_free_result($sql_result);
	}

		
	// get location names
	$sql = "select distinct location_id, location_name from events,events_locations where location_id=events_location order by location_name";
	$sql_result = tep_db_query($sql);
	$locations_array[]=array("id"=>-1,"text"=>TEXT_ALL_LOCATIONS);
	while($row=tep_db_fetch_array($sql_result)) {
		$locations_array[]=array("id"=>$row["location_id"],"text"=>$row["location_name"]);
		if ($row["location_id"]==$location){
			$display_header.=TEXT_LOCATION  . ":&nbsp;&nbsp;" . $row["location_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);
	
	// Events List
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
	$sql = "select e.events_id events_id, Events_name  events_name from events e, events_description ed where e.events_id = ed.events_id and ed.language_id='" . $languages_id . "'" . EVENTS_CONDITION_STATUS . $events_option . "order by events_name";
	$sql_result = tep_db_query($sql);
	$events_array[]=array("id"=>-1,"text"=>TEXT_ALL_EVENTS);
	while($row=tep_db_fetch_array($sql_result)) {
		if($row["events_name"]) $events_array[]=array("id"=>$row["events_id"],"text"=>$row["events_name"]);
		if ($row["events_id"]==$event){
			if($row["events_name"])$display_header.=TEXT_EVENT  . ":&nbsp;&nbsp;" . $row["events_name"] . "\t";
		}
	}
	
	mysql_free_result($sql_result);
	

	if ($show_waiting || $show_pending){
		$display_header.=TEXT_SHOW . "&nbsp;&nbsp;";
	}
	if ($show_pending){
		$display_header.=TEXT_PENDING . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	if ($show_waiting){
		$display_header.=TEXT_WAITING;
	}
	$found_results=false;
	// build the output if needed

	$where = " ";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
		$instructor=$login_id;
	}
	if($instructor!="-1") {
		$where.= "and e.events_instructor='".$instructor."' ";
	} 
	if($location!="-1") {
		$where.= "and e.events_location='".$location."' ";
	}
	if($event!="-1") {
		$where.= "and e.events_id='".$event."' ";
	}
	//if(!$show_pending)
	//	$where_status.= "(events_type='R' and orders_status<>1) ";
	if($show_pending) {
		$where_status.= "(op.events_type='R') ";
	} else {
		$where_status.= "(op.events_type='R' and op.orders_products_status>1 and op.orders_products_status<5) ";
	}
	if($show_waiting) {
		$where_status.= "or (op.events_type='W' and (op.waitlist_type='F' or op.waitlist_type='C' or op.waitlist_type='V')) ";
	}
	
	// create query to fetch reservation details
	$sql = "select e.events_id events_id, es.sessions_id, o.orders_id orders_id, c.customers_id,LTRIM(c.customers_firstname) as customers_firstname,LTRIM(customers_lastname) as customers_lastname,e.events_renewal_start,e.events_renewal_end,o.payment_method,
	e.events_instructor events_instructor, e.events_location events_location, ed.events_name events_name,op.products_tax,op.final_price as product_price,
	es.start_date as end_date,es.start_date as start_date, op.events_type, op.waitlist_type, o.orders_status,
	o.customers_id,e.events_group_bookings,op.products_quantity from " . TABLE_EVENTS_SESSIONS . " es, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS ." o," .
	TABLE_CUSTOMERS . " c, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS . " e LEFT JOIN " . TABLE_ADMIN . " a on a.admin_id=e.events_instructor " .
	" where es.sessions_id = op.products_id and op.orders_id=o.orders_id and o.customers_id=c.customers_id and e.events_id=es.events_id 
	and op.products_type='E' and ed.events_id=e.events_id and ( date_format(es.start_date,'%Y-%m-%d')>='$date_begin' and  date_format(es.start_date,'%Y-%m-%d')<='$date_end')
	and ( ".$where_status.") ".$where." and ed.language_id='" . $languages_id ."'   order by op.products_id,es.start_date";
	
	//die($sql);
;

	if ($post_action=="screen"){
	// split the report
		$query_split = new splitPageResultsReport($page, REPORT_MAX_ROWS_PAGE, $sql, $query_split_numrows);
	}

	$sql_result=tep_db_query($sql);
	$num_rows=tep_db_num_rows($sql_result);
	if ($num_rows>0) $found_results=true;

	$currencies=new currencies();
	
	$output_array=array();
	if ($found_results){
		mysql_data_seek($sql_result,0);
		$i=1;
		while($row = mysql_fetch_assoc($sql_result)) {
			$eventId =  $row["events_id"]; 

			// fetch customer answer details
			$question_query=tep_db_query("SELECT answer_type,answer_value from " . TABLE_ORDERS_EVENTS_ANSWERS . " where orders_id='" . $row["orders_id"] . "' and events_id='" . $row["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"])));
			if ($row['events_type'] == 'R') $status= TEXT_RESERVED;
			if ($row['events_type']=='W'){
				if ($row['waitlist_type']=='V') $status= '<font color="blue">' . TEXT_VIP_GROUP . '</font>';
				if ($row['waitlist_type']=='F') $status= '<font color="blue">' . TEXT_FIRSTCOME_GROUP . '</font>';
				if ($row['waitlist_type']=='C') $status= '<font color="blue">' . TEXT_CUSTOM_GROUP . '</font>';
			}
			if ($row['events_type']=="T") $status= '<font color="red">' . TEXT_WITHDRAW . '</font>';
			if ($row['events_type']=="F") $status= '<font color="red">' . TEXT_REFUND . '</font>';
			$payment_method=substr($row["payment_method"],0,2);
			$paid_text=sprintf("%s",$currencies->format(tep_add_tax($row["product_price"],$row["products_tax"])));
			$renewal_details=tep_get_renewal_details($row,$row["customers_id"]);
			if (sizeof($renewal_details)>0 && $renewal_details['status']=='yes') $renewal= 'Y';
			else $renewal='N';
			
			$paid=$row["orders_status"]==1?"P":"";
			$output_array[]=array("no"=>$i,"customers_firstname"=>$row["customers_firstname"],"customers_id"=>$row["customers_id"],
									"customers_lastname"=>$row["customers_lastname"],"answer_value"=>$question_result["answer_value"],
									"events_name"=>$row["events_name"],"start_date"=>format_date($row["start_date"],true),'events_id' =>$eventId,
									"end_date"=>format_date($row["end_date"],true),"paid"=>$paid,"paid_text"=>$paid_text,"amount"=>$amount,"payment_method"=>$payment_method,
									"status"=>$status,"renewal"=>$renewal,
									"group_booking"=>$row['events_group_bookings'],"pro_quantity"=>$row['products_quantity'],"orders_id"=>$row['orders_id']);
			unset($question_result);
			$i++;
		}
		mysql_free_result($sql_result);
	}
	$report_filename=sprintf("daily_reservations_%s_%s",$login_id,time());	
	// output to pdf
	if ($post_action=="pdf"){
		generate_pdf();
		tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".pdf");
		return;
	}
	// output to excel
	if ($post_action=="excel"){
		generate_excel();
		tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".csv");
		return;
	}
	tep_delete_temp_files("daily_reservations_" . $login_id);
?>
<!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>
<script language="JavaScript" src="includes/date-picker.js"></script>
<script language="JavaScript">
	function doReport(mode) {
		if (mode==3){
			document.f.post_action.value="excel"
			document.f.target="_blank";
		} else if (mode==2){
			document.f.post_action.value="pdf";
			document.f.target="_blank";
		} else {
			document.f.post_action.value="screen";
			document.f.target="_self";
		}
	document.f.submit();
}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- 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 height="10">
				<!--<td class="pageHeading"><?php //echo HEADING_DAILY_RESERVATIONS;?></td>!-->
			</tr>
			
			<tr >
				<TD>
				<?php echo tep_draw_form("f",FILENAME_DAILY_RESERVATIONS);?>
				<input type="hidden" name="post_action" value="screen"/>
				<!-- form -->
				<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
					<tr>
						
						<td nowrap="true"><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
						<?php echo "Date" . '&nbsp;' . tep_draw_input_field("txtDate",format_date($date),'maxlength="10" size="10"');?>
							
							<a href="javascript:show_calendar('f.txtDate',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>
						</td>
						

						<td nowrap="true">
						<?php 
						$chk_price=isset($HTTP_POST_VARS['chk_price'])?$HTTP_POST_VARS['chk_price']:'';
						if(isset($HTTP_GET_VARS['top']))
							$chk_price=1;
						if($chk_price==1)
							$check_flag=' checked ';
								echo TEXT_SHOW . '&nbsp;&nbsp;' . tep_draw_checkbox_field("chk_pending",1,$show_pending) . TEXT_PENDING;
								echo '&nbsp;' . tep_draw_checkbox_field("chk_waiting",1,$show_waiting) . TEXT_WAITING;
								echo '&nbsp;&nbsp;'.tep_draw_checkbox_field("chk_price",1,$check_flag).TEXT_PRICE;
						?>
							
						</td>
						<td valign="middle" align="right">
							<?php echo '<a href="javascript:doReport(1);">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_DETAILS) . '</a>'; ?>
						</td>
					</tr>
					<tr>
						<td nowrap="true">
							<?php echo TEXT_LOCATION . '&nbsp;' . tep_draw_pull_down_menu("sel_location",$locations_array,$location);?>
						</td>
						<td nowrap="true">
						<?php 
							$all_events_array=array();
							$all_events_array = tep_get_events_array_single();
							if(sizeof($all_events_array)<=0) echo 'Events are not Available';
							else echo TEXT_EVENT . '&nbsp;' . tep_draw_event_select_menu('sel_event',$all_events_array,$event,' id="sel_event"');
						?>
						&nbsp;&nbsp;
						<?php    
								if ($login_groups_type!=TEXT_INSTRUCTOR_ENTRY){
									echo TEXT_INSTRUCTOR . '&nbsp;' . tep_draw_pull_down_menu("sel_instructor",$instructors_array,$instructor);
								}
								echo '&nbsp;&nbsp;&nbsp;&nbsp;' . TEXT_VIEW . '&nbsp;' . tep_draw_pull_down_menu("sel_view",$view_array,$view);



							?>
						</td>
						<td class="main" valign="middle" align="right">
								<?php echo '<a href="javascript:doReport(2);">' . tep_image_button('button_export_pdf.gif', IMG_EXPORT_PDF) . '</a>';?>
						</td>
					</tr>
					<tr>
						<td class="main" nowrap="true">
						
						</td>
						<td class="main" valign="middle" align="right">
						</td>
						<td class="main" valign="middle" align="right">
								<?php echo '<a href="javascript:doReport(3);">' . tep_image_button('button_export_excel.gif', IMG_EXPORT_EXCEL) . '</a>';?>
						</td>
					</tr>
					<!-- report content -->
				</table>
				</form>
				</td>
			</tr>
			<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
			<?php if ($found_results) {
			
			?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
						<td class="smallText" align="left">
							<?php echo $query_split->display_links($query_split_numrows, REPORT_MAX_ROWS_PAGE, REPORT_MAX_LINKS_PAGE, $page,tep_get_report_params()); ?>&nbsp;
						</td>
					</tr>
				</table>
				</td>
				</tr>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="1" cellpadding="1">
					<tr class='dataTableHeadingTitleRow'>
						<td class='dataTableHeadingTitleContent' width="20"><?php echo TEXT_ID;?></td>
						<td class='dataTableHeadingTitleContent'><?php echo TEXT_LAST_NAME;?></td>
						<td class='dataTableHeadingTitleContent'><?php echo TEXT_FIRST_NAME;?></td>
						<td class='dataTableHeadingTitleContent'><?php echo TEXT_POQ;?></td>
						<td class='dataTableHeadingTitleContent'><?php echo TEXT_EVENT;?></td>
						<!-- <td class='dataTableHeadingTitleContent' width="60"><?php //echo TEXT_START;?></td>
						<td class='dataTableHeadingTitleContent' width="60"><?php //echo TEXT_END;?></td> -->
						<?php if($chk_price==1){ ?>
						<td class='dataTableHeadingTitleContent'	width="60" align="center"><?php echo TEXT_PAID;?></td>
						<?php } ?>
						<td class='dataTableHeadingTitleContent' width="30"><?php echo TEXT_STAT;?></td>
						<td class='dataTableHeadingTitleContent' width="10"><?php echo TEXT_RENEWAL;?></td>
						<td class='dataTableHeadingTitleContent' width="10"></td>
				   	</tr>
					<tr height="10"></tr>
				<?php 
					$i=1;
					$prev_events_id=0;
					$prev_start_date=0;
          			for($icnt=0;$icnt<sizeof($output_array);$icnt++){
						$row=&$output_array[$icnt];
						//print_r ($row);
						if($row['events_id'] != $prev_events_id){
						?>
						<tr>
							<td colspan="9"><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
						</tr>	
						<tr>
							<td colspan="9" class="dataTableContent" height="25"><b><?php echo $row['events_name'];?></b></td>
						</tr>
					<?php $prev_events_id=$row['events_id'];
					} if(strtotime($row['start_date']) != strtotime($prev_start_date)){?>
						<tr class="dataTableHeadingTitleRow">
							<td colspan="9" class="dataTableHeadingTitleContent" height="20"><?php echo $row['start_date'];?> </td>
						</tr>
					<?php $prev_start_date=$row['start_date'];}?>		
                  <tr class='dataTableRow' height="20" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="javascript:location.href='<?php echo tep_href_link(FILENAME_CUSTOMERS,'cID=' . $row["customers_id"] . '&action=edit&return=dr');?>';">
					<td class='dataTableContent' style="cursor:hand"><?php echo $row["no"];?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_lastname'];?> </td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_firstname']; ?> </td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $row['answer_value']?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $row['events_name']?></td>
					<!-- <td class='dataTableContent' style="cursor:hand"><?php //echo $row['start_date'];?></td>
					<td class='dataTableContent' style="cursor:hand"><?php //echo $row['end_date'];?></td> -->
					<?php if($chk_price==1){ ?>
					<td align="left" width="100">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td class='dataTableContent' align="right" width="10"><?php $row["paid"] ?></td>
							<td class='dataTableContent' align="right" width="40">&nbsp;<?php echo $row["paid_text"];?></td>
							<td class='dataTableContent' align="left" width="10">&nbsp;<?php echo $row["payment_method"]; ?></td>
						</tr>
					</table>
					</td>
					<?php } ?>
					<td class='dataTableContent' style="cursor:hand"><?php echo $row["status"];?></td>
               <td class='dataTableContent' style="cursor:hand"><?php echo $row["renewal"];?></td>
               <?php 
					 $group="";
					  if($row['group_booking']=='Y' && $row['pro_quantity']>1)
					 	$group="<a href=" . FILENAME_ORDERS . "?oID=".$row['orders_id']."&action=edit&return=dr>".tep_image(DIR_WS_IMAGES . 'multi_booking.gif',IMAGE_MODIFY_GROUP).'</a>';
               ?>   	
               <td class='dataTableContent' style="cursor:hand"><?php echo $group;?></td>
               </tr>
		      <?php  if($row['group_booking']=='Y' && $row['pro_quantity']>1){
			  				$attendees_name='';
			  				$attendee_names_query = tep_db_query("select distinct c.customers_firstname,c.customers_lastname from ". TABLE_CUSTOMERS ." c, ". TABLE_CUSTOMERS_TO_CUSTOMERS ." ctc where ctc.customer_id=c.customers_id and ctc.orders_id='".(int)$row['orders_id']."'");
			  			while($attendee_result=tep_db_fetch_array($attendee_names_query)){
							$attendees_name.=$attendee_result['customers_firstname'].' '.$attendee_result['customers_lastname'].',';
						}
						if ($attendees_name!=""){
							//$start_name=strpos($attendees_name,',',1);
							//$attendees_name=substr($attendees_name,$start_name+1,-1);
							$attendees_name=substr($attendees_name,0,-1);
						}?>
				<tr>
					<td colspan="9" class="dataTableContent"><?php echo tep_draw_separator('pixel_trans.gif',25,1).'<b>'.TEXT_ATTENDEES .'</b><br>'.tep_draw_separator('pixel_trans.gif',35,1) .'-'.$attendees_name; ?></td>
				</tr>
			 <?php 	  } //end of attendee loop
			  
			       } //end of for loop
			  	}
				else{
					echo '<tr><td class="main" align="center">' . TEXT_NO_RECORDS_FOUND . '</td></tr>';
				}
			   ?>
			</table>
			</TD>
		</tr>
	</table>
	</td>
</tr>	
</table>
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 

	// function to create pdf content
	function generate_pdf(){
		global $output_array,$display_header,$found_results,$login_id,$report_filename,$HTTP_POST_VARS,$HTTP_GET_VARS;
		$chk_price=isset($HTTP_POST_VARS['chk_price'])?$HTTP_POST_VARS['chk_price']:'';
		if(isset($HTTP_GET_VARS['top']))
			$chk_price=1;
				
		//create inital settings
		$table=new pdfTable("A4","l");
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		
		// Add styles for tablular data
		$table->AddStyle("heading","color:#727272;bgcolor:#ffffff;font:Arial;size:18;style:B");
		$table->AddStyle("user","color:#000000;bgcolor:#f1f9fe;font:Arial;size:14;style:B;border-color:#7b9ebd");
		$table->AddStyle("subhead","color:#000000;bgcolor:#7b9ebd;font:Arial;size:10;style:B");
		$table->AddStyle("headrow","color:#000000;bgcolor:#C9C9C9;font:Arial;size:10;style:B");
		$table->AddStyle("row","color:#000000;bgcolor:#F0F1F1;font:Arial;size:10");
		$table->AddStyle("query","color:#000000;bgcolor:#FFFFFF;font:Arial;size:11");
		$table->AddStyle("subrow","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10");
		$table->AddStyle("subrowhead","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10;style:B");

		$table->headers["text"]=$display_header;
		$table->headers["style"]="query";
		$table->headers["height"]=12;
		$table->headers["width"]="100%";
		$table->headers["cols"]=3;

		//define column width details
		$widths[0]=25;
		$widths[3]=60;
		$widths[4]=150;
		$widths[5]=25;
		$widths[6]=50;
		$widths[7]=20;
		$widths[8]=25;
		$widths[9]=50;
		$widths[10]=50;
		
		
		$tot_width=$widths[0]+$widths[3]+$widths[4]+$widths[8]+$widths[9]+$widths[10];
			if($chk_price==1)
				$tot_width=$tot_width+$widths[5]+$widths[6]+$widths[7];
		$temp_width=$table->width-($tot_width);
		$widths[1]=$temp_width/2;
		$widths[2]=$temp_width/2;
		$cols=array();
		
		
		if ($found_results){
		// define table header columns
		$cols[]=array("text"=>TEXT_ID,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_FIRST_NAME,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_LAST_NAME,"width"=>$widths[2],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_POQ,"width"=>$widths[3],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_EVENT,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		//$cols[]=array("text"=>TEXT_START,"width"=>$widths[5],"align"=>"L","style"=>"headrow","valign"=>"M");
		//$cols[]=array("text"=>TEXT_END,"width"=>$widths[6],"align"=>"L","style"=>"headrow","valign"=>"M");
		if($chk_price==1)
		$cols[]=array("text"=>TEXT_PAID,"width"=>$widths[5]+$widths[6]+$widths[7],"align"=>"C","style"=>"headrow","valign"=>"M");
		
		$cols[]=array("text"=>TEXT_STAT,"width"=>$widths[8],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_RENEWAL,"width"=>$widths[9],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>'',"width"=>$widths[10],"align"=>"L","style"=>"headrow","valign"=>"M");
		
		$table->tableheaders["text"]=$cols;
		unset($cols);
		$prev_events_id=0;
		$prev_start_date=0;
		// output content details
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			$group="";
			if($row['group_booking']=='Y' && $row['pro_quantity']>1)
			  $group='Group booking';
			
			if($row['events_id'] != $prev_events_id){
				$cols[]=array("text"=>'',"width"=>$tot_width+$widths[2]+$widths[1],"align"=>"L","style"=>"subrow","valign"=>"M");
				$table->OutputRowMultiple($cols,15);
				unset($cols);
				$cols[]=array("text"=>$row['events_name'],"width"=>$tot_width+$widths[2]+$widths[1],"align"=>"L","style"=>"subrow","valign"=>"M");
				$prev_events_id=$row['events_id'];
				$table->OutputRowMultiple($cols,15);
				unset($cols);
			}
			if(strtotime($row['start_date']) != strtotime($prev_start_date)){
				//$cols[]=array("text"=>$row['start_date'],"width"=>$tot_width+$widths[2]+$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>$row['start_date'],"width"=>$tot_width+$widths[2]+$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
				$prev_start_date=$row['start_date'];
				$table->OutputRowMultiple($cols,15);
				unset($cols);
			}
			
			$cols=array();
			$cols[]=array("text"=>$row["no"],"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['customers_firstname'],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['customers_lastname'],"width"=>$widths[2],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['answer_value'],"width"=>$widths[3],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["events_name"],"width"=>$widths[4],"align"=>"L","style"=>"row","valign"=>"M");
			//$cols[]=array("text"=>$row["start_date"],"width"=>$widths[5],"align"=>"L","style"=>"row","valign"=>"M");
			//$cols[]=array("text"=>$row["end_date"],"width"=>$widths[6],"align"=>"L","style"=>"row","valign"=>"M");
			if($chk_price==1){
			$cols[]=array("text"=>$row["paid"],"width"=>$widths[5],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["paid_text"],"width"=>$widths[6],"align"=>"R","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["payment_method"],"width"=>$widths[7],"align"=>"C","style"=>"row","valign"=>"M");
			}
			$cols[]=array("text"=>$row["status"],"width"=>$widths[8],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["renewal"],"width"=>$widths[9],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$group ,"width"=>$widths[10],"align"=>"L","style"=>"row","valign"=>"M");
			
			$table->OutputRowMultiple($cols,15);
			unset($cols);
		}
		} else {
			$cols[]=array("text"=>TEXT_NO_RECORDS_FOUND,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
		}
		// create pdf file
		$table->Render($report_filename.".pdf");
	}
	// function to create excel data
	function generate_excel(){
		global $output_array,$found_results,$login_id,$report_filename,$HTTP_POST_VARS,$HTTP_GET_VARS;
		$chk_price=isset($HTTP_POST_VARS['chk_price'])?$HTTP_POST_VARS['chk_price']:'';
		if(isset($HTTP_GET_VARS['top']))
			$chk_price=1;
		$prev_events_id=0;
		$prev_start_date=0;
				
		if ($found_results){
		$result.=TEXT_ID . "," . TEXT_FIRST_NAME . "," . TEXT_LAST_NAME . "," .
				 TEXT_POQ . "," . TEXT_EVENT . "," ;
		if($chk_price==1)		 
			$result.=  TEXT_PAID . ",,," ; else $result.=",";
		$result.= TEXT_STAT . "," . TEXT_RENEWAL .",". ''  . "\n";
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			
			$group="";
			if($row['group_booking']=='Y' && $row['pro_quantity']>1)
			  $group='Group booking';
			if($row['events_id'] != $prev_events_id){
				$prev_events_id=$row['events_id'];
				$result.="\n";
				$result.=$row['events_name']."\n";
			 }
			if(strtotime($row['start_date']) != strtotime($prev_start_date)){
				$result.= $row['start_date']."\n";
				$prev_start_date=$row['start_date'];
			  }
			$result.=$row["no"] . "," . $row['customers_firstname']. "," . $row['customers_lastname'] . "," .
					 $row['answer_value']. "," . $row['events_name'] . "," ;
			if($chk_price==1)		 
				$result.= $row["paid"] . "," .  $row["paid_text"] ."," .  $row["payment_method"];
			$result.= "," .  $row["status"] . "," . $row["renewal"] . "," .  $group . "\n";
					
		}
		} else {
			$result.=TEXT_NO_RECORDS_FOUND;
		}
		tep_write_text_file($report_filename.".csv",$result);
	}


?>