<?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/functions/message_functions.php');
	require(DIR_WS_FUNCTIONS . 'events_tree.php');
	require('includes/classes/currencies.php');
	require('includes/classes/pdfTable.php');
	require(DIR_WS_CLASSES . 'split_page_results_report.php');
	
	$currencies=new currencies();

	$input_params=&$HTTP_POST_VARS;
	$date_begin=isset($input_params['txt_start_date'])?tep_convert_date_raw($input_params['txt_start_date']):'';
	$date_end=isset($input_params['txt_end_date'])?tep_convert_date_raw($input_params['txt_end_date']):'';
	$instructor=isset($input_params['sel_instructor'])?$input_params['sel_instructor']:-1;
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$event=isset($input_params['sel_event'])?$input_params['sel_event']:-1;
	$output_pdf=isset($input_params['output_pdf'])?true:false;
	$output_excel=isset($input_params['output_excel'])?true:false;
	$post_action = isset($input_params['post_action'])?$input_params['post_action']:'';
	$page=isset($input_params['page'])?$input_params['page']:1;

	if ($post_action=="") {
		$output_pdf=true;
		$post_action="screen";
	}
	if (!$product_show && !$event_show) {
		$event_show=true;
		$product_show=true;
	}
	// if the start date is empty,select records past 1 month
	if ($date_begin==""){
		$sql =  "select date_sub('".getServerDate()."', interval 1 month) begin,'".getServerDate()."' as end";
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$date_end = $row["end"];
	}
	/*if ($date_end==""){
		//$sql =  "select date_add('$date_begin', interval -(dayofmonth('$date_begin')-1) day) begin, date_add('$date_begin', interval (30-dayofmonth('$date_begin')) day) end";	
		$sql =  "select date_add('$date_begin', interval 1 month) end";	
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		
		$date_end = $row["end"];	
	}*/
	// prepare the display headers;
	$display_header="";
	
	
	if ($date_end==""){
		$sql =  "select date_add('$date_begin', interval -(dayofmonth('$date_begin')-1) day) begin, date_add('$date_begin', interval (30-dayofmonth('$date_begin')) day) end";	
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$date_end = $row["end"];
	}
	// Create Report params as header string for every page
	$display_header="";
	$display_header.=TEXT_START_DATE  . ":&nbsp;&nbsp;" . $date_begin . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	$display_header.=TEXT_END_DATE  . ":&nbsp;&nbsp;" . $date_end ."\t";
	
	
	$instructors_array = array();
	$events_array = array();
	$locations_array=array();

	// 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)) {
		if ($instructor==-1) {
			$instructor=$row["admin_id"];
		}
		$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";
			$instructor_name=$row["admin_firstname"] . ' ' . $row["admin_lastname"];
		}
	}
	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"]);
	}
	mysql_free_result($sql_result);

	// get all  events
	$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='" . (int)$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)) {
		$events_array[]=array("id"=>$row["events_id"],"text"=>$row["events_name"]);
		if ($row["events_id"]==$event){
			$display_header.=TEXT_EVENT  . ":&nbsp;&nbsp;" . $row["events_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);


	if ($instructor=="-1") $display_header.=TEXT_INSTRUCTOR . ":&nbsp;&nbsp;" . TEXT_ALL_INSTRUCTORS ."\t";
	if ($event=="-1") $display_header.=TEXT_EVENT . ":&nbsp;&nbsp;" . TEXT_ALL_EVENTS  ."\t";

	$found_results=false;
		$where = " ";
		$db_result=array();
		$num_rows=0;
		$cur_row=0;
		
	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."' ";
	}

	// prepare sql for fetching Payment details for Instructor
	$sql="SELECT distinct ec2.events_id,ec2.events_categories_id,ec2d.events_categories_name,s.start_date,op.products_id,ed.events_name,e.instructors_rate_type,e.instructors_rate,e.instructors_base_rate,
					time_format(e.events_start_time,'%H:%i %p') as start_time,time_format(e.events_end_time,'%H:%i %p') as end_time,
					concat(a.admin_firstname,' ',a.admin_lastname) as instructor_name,e.events_instructor,e.events_group_bookings,op.products_quantity,op.orders_id from " .
					TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed," . TABLE_ADMIN . " a, " . 
					TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec2, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ec2d " . 
					"where op.products_id=s.sessions_id and s.events_id=e.events_id and e.events_id=ed.events_id and e.events_id=ec2.events_id " . 
					" and ed.language_id='" . $languages_id ."' and ec2d.language_id='" . $languages_id . "' and ec2.events_categories_id=ec2d.events_categories_id " .
					" and e.events_instructor=a.admin_id and op.products_type='E' and op.events_type='R' " . $where  . " and s.start_date>='$date_begin' and s.start_date<='$date_end' " .
					" group by s.sessions_id,s.events_id order by instructor_name, ed.events_name,s.start_date";
	//query for results

	if ($post_action=="screen"){
		$query_split = new splitPageResultsReport($page, REPORT_MAX_ROWS_PAGE, $sql, $query_split_numrows);
	}
	
	$sql_result=tep_db_query($sql);
	
	if (tep_db_num_rows($sql_result)>0) $found_results=true;
	
	$output_array=array();
	
	if ($found_results){
		$row_cnt=1;
		$prev_category = -1;
		$total_amount = 0;
		$net_amount=0;
		$events_count = 0;
		$students_count = 0;
		$totals_array=array();
		$net_cnt=0;
		$net_students=0;
		$net_pay=0;
		$net_amount=0;

		// Get the possible total amount for the every event sessions, 
		$count_query=tep_db_query("SELECT sessions_id,count(customers_id) as total,sum(final_price) as total_amount from " . TABLE_EVENTS_ATTENDEES . " group by sessions_id");
		while($count_result=tep_db_fetch_array($count_query)){
			$temp_cnt=(int)$count_result["total"];
			$temp_total=(float) $count_result["total_amount"];
			$totals_array[$count_result["sessions_id"]]=array("total"=>$temp_cnt,"total_amount"=>$temp_total);
		}
		mysql_free_result($count_query);
		$count_query=tep_db_query("SELECT sessions_id,count(customers_id) as total,sum(final_price) as total_amount from " . TABLE_EVENTS_ATTENDEES_CONFIRM . " group by sessions_id");
		while($count_result=tep_db_fetch_array($count_query)){
			$temp_cnt=(int)$count_result["total"];
			$temp_total=(float)$count_result["total_amount"];
			$totals_array[$count_result["sessions_id"]]=array("total"=>$temp_cnt,"total_amount"=>$temp_total);
		}
		mysql_free_result($count_query);
		while($row=tep_db_fetch_array($sql_result)) {
			if ($prev_category!=$row["events_categories_id"]){
				// output category heading
				if ($total_amount>0){
					// output total amount
					$output_array[]=array("type"=>"total","total"=>$currencies->format($total_amount));
					$total_amount=0;
				}
				$prev_category=$row["events_categories_id"];
				$output_array[]=array("type"=>"category","category_name"=>$row["events_categories_name"]);
			}

			$rate=0.0;

			$rate_type=$row["instructors_rate_type"];
			$pay_text="";
			$total_students=0;
			$rate=0;
			if (isset($totals_array[$row["products_id"]])){
				$total_students=$totals_array[$row["products_id"]]["total"];
				$amount=$totals_array[$row["products_id"]]["total_amount"];

				// calculate Instructor's rate for the single event
				if ($rate_type=="FL") {// flat rate
					$rate=$row["instructors_base_rate"];
					$pay_text=$currencies->format($rate);
				}
				if ($rate_type=='MU') { // multiple student
					$rate=($row["instructors_rate"]*$total_students)+$row["instructors_base_rate"];
					$pay_text=$currencies->format($row["instructors_rate"]) . "/S + " . $currencies->format($row["instructors_base_rate"]);
				}
				if ($rate_type=='GP'){ // gross percentage
					$rate=$row["instructors_base_rate"]+$amount*$row["instructors_rate"]/100;
					$pay_text=$currencies->format($amount) . " * " . number_format($row["instructors_rate"],2) . "% + " . $currencies->format($row["instructors_base_rate"],2);
				}
			}
			$total_amount+=$rate;
			$output_array[]=array("type"=>"row","no"=>$row_cnt,"events_name"=>$row["events_name"],"time"=>$row["start_time"] . " - " . $row["end_time"],
									"date"=>format_date($row["start_date"]),"students"=>$total_students,"pay_text"=>$pay_text,"rate"=>$currencies->format($rate),"events_group_bookings"=>$row['events_group_bookings'],"products_quantity"=>$row['products_quantity'],"orders_id"=>$row['orders_id']);
			$row_cnt++;
			$net_cnt++;
			$net_amount+=$rate;
			$net_pay+=$rate;
			$net_students+=$total_students;
		}
		if ($total_amount>0){
			$output_array[]=array("type"=>"total","total"=>$currencies->format($total_amount));
			$total_amount=0;
		}
		if ($net_students>0){
			$output_array[]=array("type"=>"net","net_cnt"=>$net_cnt,"net_students"=>$net_students,"content"=>sprintf(TEXT_TOTAL_INSTRUCTOR,$instructor_name),
									"net_pay"=>$currencies->format($net_pay),"net_amount"=>$currencies->format($net_amount));
		}
		mysql_free_result($sql_result);
	}
	$report_filename=sprintf("instructor_payments_%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("instructor_payments_" . $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>
<?php include('includes/date_format_js.php');?>
<script language="JavaScript">
	function doReport(mode) {
		startDate=date_format(document.f.txt_start_date.value,'','',true);
		endDate=date_format(document.f.txt_end_date.value,'','',true);

	if(startDate>endDate) {
		alert("<?php echo REPORT_START_AFTER_END?>");
		return;
	}
		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> 
		<td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr height="10">
				<!--<td class="pageHeading"><?php //echo HEADING_INSTRUCTOR_PAYMENTS;?></td>!-->
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
			</tr>
			<tr >
				<td>
				<?php 
					echo tep_draw_form("f",FILENAME_INSTRUCTOR_PAYMENTS,"action=search&mode=1");
					echo '<input type="hidden" name="post_action" value="screen">';
				?>
				<table border="0" cellspacing="2" cellpadding="2" class="searchArea" width="100%">
					<tr>
						<td><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
							<?php echo tep_draw_separator('pixel_trans.gif',10,10). TEXT_START_DATE;?> <INPUT type="text" name="txt_start_date" size="10" value="<?php echo format_date($date_begin); ?>">
							<a href="javascript:show_calendar('f.txt_start_date',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>&nbsp;&nbsp;
							<?php echo TEXT_END_DATE;?> <INPUT type="text" name="txt_end_date" size="10" value="<?php echo format_date($date_end);?>">
							<a href="javascript:show_calendar('f.txt_end_date',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 class="main" align="left">
						</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 colspan="2">
							<?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 tep_draw_separator('pixel_trans.gif',10,10).TEXT_EVENT . tep_draw_separator('pixel_trans.gif',33,10) . tep_draw_event_select_menu('sel_event',$all_events_array,$event,' id="sel_event"');
							
								echo tep_draw_separator('pixel_trans.gif',10,10).TEXT_INSTRUCTOR . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("sel_instructor",$instructors_array,$instructor);
							?>
							
						</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" valign="middle" align="right" colspan="3">
								<?php echo '<a href="javascript:doReport(3);">' . tep_image_button('button_export_excel.gif', IMG_EXPORT_EXCEL) . '</a>';?>
						</td>
					</tr>
				</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>
		<?php }?>
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="1" cellpadding="1">
			<?php if ($found_results){  ?>
				<tr>
					<td>
					<table width="100%" border="0" cellspacing="1" cellpadding="2">
						<tr class="dataTableHeadingTitleRow">
							<td class="dataTableHeadingTitleContent" width="10" align="left"><?php echo TEXT_ID;?></td>
							<td class="dataTableHeadingTitleContent" align="left"><?php echo TEXT_EVENT_NAME;?></td>
							<td class="dataTableHeadingTitleContent" width="120" align="left"><?php echo TEXT_TIME;?></td>
							<td class="dataTableHeadingTitleContent" width="70" align="left"><?php echo TEXT_DATE;?></td>
							<td class="dataTableHeadingTitleContent" width="60" align="left"><?php echo TEXT_STUDENTS;?></td>
							<td class="dataTableHeadingTitleContent" width="180" align="center"><?php echo TEXT_PAY_TYPE;?></td>
							<td class="dataTableHeadingTitleContent" width="100" align="right"><?php echo TEXT_PAY_AMOUNT;?></td>
							<td class="dataTableHeadingTitleContent" width="100" align="right"><?php echo TEXT_TOTAL_AMOUNT;?></td>
							<td class="dataTableHeadingTitleContent" width="30" align="right">&nbsp;</td>
						</tr>
						<?php 
								for($icnt=0;$icnt<sizeof($output_array);$icnt++){
									$row=$output_array[$icnt];
									if ($row["type"]=="total"){
									?>
										<tr height="20" valign="middle">
											<td class="dataTableContent" colspan="9" align="right">
												<b><?php echo $row["total"]; ?></b>
											</td>
										</tr>
									<?php }	else if ($row["type"]=="net") {?>
										<tr>
											<td colspan=9><?php echo tep_draw_separator();?></td>
										</tr>
										<tr height="20" valign="middle">
											<td class="dataTableContent" colspan="3" align="left">
												<b><?php echo $row["content"]; ?></b>
											</td>
											<td class="dataTableContent" align="right">
												<b><?php echo $row["net_cnt"]; ?></b>
											</td>
											<td class="dataTableContent" align="right">
												<b><?php echo $row["net_students"]; ?></b>
											</td>
											<td class="dataTableContent" colspan="2" align="right">
												<b><?php echo $row["net_pay"]; ?></b>
											</td>
											<td class="dataTableContent" align="right">
												<b><?php echo $row["net_amount"]; ?></b>
											</td>
										</tr>
									<?php }	else if ($row["type"]=="category") {?>
										<tr height="20" valign="middle">
											<td class="dataTableContent" colspan="9" align="left">
												<b><?php echo $row["category_name"]; ?></b>
											</td>
										</tr>
									<?php }  else if ($row["type"]=="row") {
										if($disp_class=='dataTableRow')
										$disp_class='dataTableRowOver';
									  else
										$disp_class='dataTableRow';?>
										<tr class="<?php echo $disp_class;?>">
											<td class="dataTableContent"><?php echo $row["no"]; ?></td>
											<td class="dataTableContent"><?php echo $row["events_name"]; ?>
											<td class="dataTableContent"><?php echo $row["time"]; ?>
											<td class="dataTableContent"><?php echo $row["date"]; ?>
											<td class="dataTableContent" align="right"><?php echo $row["students"]; ?>
											<td class="dataTableContent"><?php echo $row["pay_text"]; ?>
											<td class="dataTableContent" align="right"><?php echo $row["rate"]; ?>
											<td class="dataTableContent" align="right"><?php echo $row["rate"];?>
											<td class="dataTableContent" align="center"><?php 
											$group="";
											if($row['events_group_bookings']=='Y' && $row['products_quantity']>=1)
                 							echo tep_image(DIR_WS_IMAGES . 'multi_booking.gif',TEXT_GROUP_BOOKING); 
											?>
										</tr>
									<?php } 
								}
						?>
						</table>
						</td>
					</tr>
					<?php 
						}
						else{
							echo '<tr><td class="main" align="center">' . TEXT_NO_RECORDS_FOUND . '</td></tr>';
						}
					?>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
					</tr>
			</table>
			</td>
		</tr>
	</table>
	</td>
</tr>
</table>
	<!-- body_eof //-->
	<!-- 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;
		
		// Set initial parameters
		$table=new pdfTable("A4","l");
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		// Add Initial styles for table cols
		$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;

		// calculate width of every column
		$widths[0]=20;
		$widths[2]=95;
		$widths[3]=60;
		$widths[4]=50;
		$widths[5]=150;
		$widths[6]=70;
		$widths[7]=80;
		$widths[8]=80;

		$temp_width=$table->width-($widths[0]+$widths[2]+$widths[3]+$widths[4]+$widths[5]+$widths[6]+$widths[7]+$widths[8]);
		$widths[1]=$temp_width;
		$cols=array();

		if ($found_results){		
		// form the header columns
		$cols[]=array("text"=>TEXT_ID,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_EVENT_NAME,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_TIME,"width"=>$widths[2],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_DATE,"width"=>$widths[3],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_STUDENTS,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_PAY_TYPE,"width"=>$widths[5],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_PAY_AMOUNT,"width"=>$widths[6],"align"=>"R","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_TOTAL_AMOUNT,"width"=>$widths[7],"align"=>"R","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>'',"width"=>$widths[8],"align"=>"R","style"=>"headrow","valign"=>"M");
		
		$table->tableheaders["text"]=$cols;
		unset($cols);
		// output the table rows
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			$cols=array();
			$group="";
			if($row['events_group_bookings']=='Y' && $row['products_quantity']>=1)
			$group=TEXT_GROUP_BOOKING;
			if ($row["type"]=="row"){
				$cols[]=array("text"=>$row["no"],"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row['events_name'],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>strtolower($row['time']),"width"=>$widths[2],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row['date'],"width"=>$widths[3],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["students"],"width"=>$widths[4],"align"=>"R","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["pay_text"],"width"=>$widths[5],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["rate"],"width"=>$widths[6],"align"=>"R","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["rate"],"width"=>$widths[7],"align"=>"R","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$group,"width"=>$widths[8],"align"=>"R","style"=>"row","valign"=>"M");
				$table->OutputRow($cols,15);
			} else if ($row["type"]=="total"){
				$cols[]=array("text"=>$row['total'],"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4]+$widths[5]+$widths[6]+$widths[7]+$widths[8],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,20);
			} else if ($row["type"]=="category") {
				$cols[]=array("text"=>$row["category_name"],"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4]+$widths[5]+$widths[6]+$widths[7]+$widths[8],"align"=>"L","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,20);
			} else if ($row["type"]=="net"){
				$table->DrawLine();
				$cols[]=array("text"=>$row["content"],"width"=>$widths[0]+$widths[1]+$widths[2],"align"=>"L","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row["net_cnt"],"width"=>$widths[3],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row["net_students"],"width"=>$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row["net_pay"],"width"=>$widths[5]+$widths[6],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row["net_amount"],"width"=>$widths[7],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$group,"width"=>$widths[8],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,20);
			}

			unset($cols);
		}
		} else {
			$cols[]=array("text"=>TEXT_NO_RECORDS_FOUND,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
		}
		// output to pdf file
		$table->Render($report_filename.".pdf");
	}

	// function to create excel content
	function generate_excel(){
		global $output_array,$found_results,$login_id,$report_filename;
		
		if ($found_results){
		$result.=TEXT_ID . "," . TEXT_EVENT_NAME . "," . TEXT_TIME . "," .
				TEXT_DATE . "," . TEXT_STUDENTS . "," . 
				 TEXT_PAY_TYPE . "," . TEXT_PAY_AMOUNT . "," . 
				 TEXT_TOTAL_AMOUNT .",". ''. "\n";
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			$group="";
			if($row['events_group_bookings']=='Y' && $row['products_quantity']>=1)
				$group=TEXT_GROUP_BOOKING;
			if ($row["type"]=="row"){
				$result.=$row["no"] . "," . $row['events_name']. "," . strtolower($row['time']) . "," .
						 $row['date'] . "," .  $row['students'] . "," . 
						 $row['pay_text']. "," . $row['rate'] . "," .
						 $row["rate"]. "," . $group . "\n"; 
			} else if ($row["type"]=="total"){
				$result.=",,,,,,," . $row["total"] . "\n";
			}else if ($row["type"]=="category"){
				$result.=$row["category_name"] . "\n";
			}else if ($row["type"]=="net"){
				$result.=$row["content"] . ",,," . $row["net_cnt"] . "," . $row["net_students"] . ",," .
							$row["net_pay"] . "," . $row["net_amount"];
			}
		}
		} else {
			$result.=TEXT_NO_RECORDS_FOUND . "\n";
		}
		tep_write_text_file($report_filename.".csv",$result);
	}


?>
