<?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('includes/classes/currencies.php');
  require('includes/classes/pdfTable.php');
  require(DIR_WS_CLASSES . 'split_page_results_report.php');
  
  define(BOX_WIDTH1,'125');

	// get initial report parameters, check for "return" value if present get from 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"])){
			if ($input_params["post_action"]!="change_status"){
				tep_set_session_value("rep_params",$HTTP_POST_VARS);
				$GLOBALS["rep_params"]["post_action"]="screen";				
			}
		} else {
			tep_set_session_value("rep_params",array());
		}
	}
	
	$start_date=isset($input_params['txt_start_date'])?tep_convert_date_raw($input_params['txt_start_date']):'';
	$end_date=isset($input_params['txt_end_date'])?tep_convert_date_raw($input_params['txt_end_date']):'';
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$category = isset($input_params['sel_category'])?$input_params['sel_category']:-1;
	$summary_show = isset($input_params['summary_show'])?true:false;
	$waitlist_show = isset($input_params['waitlist_show'])?true:false;
	$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 start date is empty set the start date to past 1 month
if($start_date=="") {
	$res = tep_db_query("SELECT curdate() - INTERVAL 1 MONTH");
	$row = mysql_fetch_array($res);
	$start_date = $row[0]; 
}

if($end_date=="") {
	$res = tep_db_query("SELECT curdate()");		
	$row = mysql_fetch_array($res);
	$end_date = $row[0]; 
}
	// Header to be placed in pdf output
	$display_header=TEXT_START_DATE .  ":&nbsp;&nbsp;" . $start_date . "&nbsp;&nbsp;&nbsp;&nbsp;";
	$display_header.=TEXT_END_DATE . ":&nbsp;&nbsp;" . $end_date . "\t";
	if ($location=="-1") $display_header.=TEXT_LOCATION . ":&nbsp;&nbsp;" . TEXT_ALL_LOCATIONS  ."\t";
	if ($category=="-1") $display_header.=TEXT_EVENT_CATEGORY . ":&nbsp;&nbsp;" . TEXT_ALL_EVENT_CATEGORIES  ."\t";
	
	
	// 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);
	
	// get event category names
	$sql = "select * from events_categories_description where language_id='" . $languages_id . "' order by events_categories_name";
	$sql_result = tep_db_query($sql);
	$categories_array[]=array("id"=>-1,"text"=>TEXT_ALL_EVENT_CATEGORIES);
	while($row=tep_db_fetch_array($sql_result)) {
		$categories_array[]=array("id"=>$row["events_categories_id"],"text"=>$row["events_categories_name"]);
		if ($row["events_categories_id"]==$category){
			$display_header.=TEXT_EVENT_CATEGORY  . ":&nbsp;&nbsp;" . $row["events_categories_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);
	
	// get event names
	$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 ."' order by events_name";
	$sql_result = tep_db_query($sql);
	while($row=tep_db_fetch_array($sql_result)) {
		$events_array[$row["events_id"]]=$row["events_name"];
	}
	mysql_free_result($sql_result);
	
	// get the mysql results;
	$sql="SELECT orders_status_id,orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id='" . $languages_id . "'";
	$sql_result=tep_db_query($sql);
	while($row=tep_db_fetch_array($sql_result)){
		$order_status_array[$row["orders_status_id"]]=$row["orders_status_name"];
	}
	
	$where = " ";
	if($location!="-1") {
		$where.= "and e.events_location='".$location."' ";
	}
	if($category!="-1") {
		$where.= "and ec.events_categories_id='".$category."' ";
	}
	if (!$waitlist_show){
		$where.= "and op.events_type='R'";
	}
	
	// form the report result query
	$sql=	"select distinct ec.events_id,ed.events_name,o.orders_id,op.events_type,op.waitlist_type,o.customers_name,count(*) as no_sessions,min(s.start_date) as start_date,orders_status," .
			" sum(op.final_price) as amount_paid,sum(op.events_fees) as amount_total, max(s.start_date) as end_date,op.products_tax,o.customers_id from " . 
			TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec, " .
			TABLE_EVENTS_DESCRIPTION . " ed " .
			"where op.orders_id=o.orders_id and op.products_id=s.sessions_id and s.events_id=e.events_id and e.events_id=ec.events_id and e.events_id=ed.events_id " .
			" and op.products_type='E' and op.events_type ='R' and op.waitlist_type='P' " . $where . " and date_format(o.date_purchased,'%Y-%m-%d')>='$start_date' and ed.language_id='" . $languages_id . "'" .
			" and date_format(o.date_purchased,'%Y-%m-%d')<='$end_date'  group by o.orders_id order by ed.events_name,o.customers_name";
	if ($post_action=="screen"){
		// restrict report page rows
		$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 there is some records
	if ($found_results){
		$i=1;
		$prev_event=0;
		$net_total=0;
		$net_outs=0;
		while($row = mysql_fetch_assoc($sql_result)) {
			// output event heading
			if ($prev_event!=$row["events_id"]){
				// output total amount for current event
				if ($net_total>0){
					$output_array[]=array("type"=>"total","net_total"=>$currencies->format($net_total),"net_outs"=>$currencies->format($net_outs));
					$net_total=0;
					$net_outs=0;
				}
				$output_array[]=array( "type"=>"event","content"=>$row["events_name"]);
				$prev_event=$row["events_id"];
			}
			$products_tax=$row["products_tax"];
			$total=(float)$row["amount_total"];
			$paid=(float)$row["amount_paid"];
			
			// get 	the total amount for confirmed order
			$confirm_order_query=tep_db_query("SELECT sum(op.final_price) as confirm_total from " . TABLE_ORDERS_PRODUCTS  . " op, " .  TABLE_EVENTS_SESSIONS  . " s where op.waitlist_orders_id='" . $row["orders_id"] . "' and s.events_id='" . $row["events_id"] . "' and op.products_id=s.sessions_id and op.products_type='E' and op.events_type='R'");
			$confirm_result=tep_db_fetch_array($confirm_order_query);
			
			$confirm_amount=(float)$confirm_result["confirm_total"];
			
			// calculate the outstanding amount for confirmed reservations
			$outstanding=tep_get_rounded_amount(tep_add_tax($total-($paid+$confirm_amount),$products_tax));
			$current_total=tep_get_rounded_amount(tep_add_tax($total,$products_tax));
			if (!$summary_show){
			$output_array[]=array(	"type"=>"row","no"=>$i,"customers_name"=>$row["customers_name"],"customers_email_address"=>$row["customers_email_address"],
									"no_sessions"=>$row["no_sessions"],"amount_total"=>$currencies->format($current_total),"outstanding"=>$currencies->format($outstanding),
									"start_date"=>format_date($row["start_date"]),"end_date"=>format_date($row["end_date"]),'orders_id'=>$row["orders_id"],'status'=>$order_status_array[$row["orders_status"]]
									);
			}
			$net_total+=$current_total;
			$net_outs+=$outstanding;
			$i++;
		}
		if ($net_total>0){
			$output_array[]=array("type"=>"total","net_total"=>$currencies->format(tep_get_rounded_amount($net_total)),"net_outs"=>$currencies->format(tep_get_rounded_amount($net_outs)));
			$net_total=0;
			$net_outs=0;
		}
		mysql_free_result($sql_result);
	}
	// output to pdf
	if ($post_action=="pdf"){
		generate_pdf();
		tep_redirect(DIR_WS_CATALOG . "images/pending_payments.pdf");
		return;
	}
	// output to excel
	if ($post_action=="excel"){
		generate_excel();
		tep_redirect(DIR_WS_CATALOG . "images/pending_payments.csv");
		return;
	}
?>

<!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) {
		strStart = document.f.txt_start_date.value.split("-");
	startDate = new Date(strStart[0], strStart[1], strStart[2]);

		strEnd = document.f.txt_end_date.value.split("-");
	endDate = new Date(strEnd[0], strEnd[1], strEnd[2]);

	if(startDate.getTime()>endDate.getTime()) {
		alert("Start date must be before end date.");
		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> 
	<!-- body_text //-->	  
	<td width=100% align=left valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr>
			<td class="pageHeading"><?php echo HEADING_PENDING_PAYMENTS;?></td>
		</tr>
		<tr>
			<TD>
			<?php echo tep_draw_form("f",FILENAME_PENDING_PAYMENTS);?>
			<input type="hidden" name="post_action" value="screen">
			<table cellspacing="3" cellpadding="2" class="formArea" width="100%" border="0">
				<tr class="main">
					<td class="main" 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 TEXT_START_DATE . '&nbsp;' . tep_draw_input_field("txt_start_date",format_date($start_date),'maxlength="10" size="10"');?>
						<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>
					</TD>
					<td class="main" nowrap="true">
						<?php echo TEXT_END_DATE . '&nbsp;' . tep_draw_input_field("txt_end_date",format_Date($end_date),'maxlength="10" size="10"');?>
						<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>
						<?php echo '&nbsp;&nbsp;' . TEXT_LOCATION . '&nbsp;' . tep_draw_pull_down_menu("sel_location",$locations_array,$location);?>
					</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 class="main">
					<td class="main" nowrap="true">
						<?php echo TEXT_EVENT_CATEGORY . '&nbsp;' . tep_draw_pull_down_menu("sel_category",$categories_array,$category);?>
					</td>
					<td class="main" nowrap="true">
						<?php 	echo TEXT_SHOW . '&nbsp;&nbsp;' . tep_draw_checkbox_field("summary_show",1,$summary_show). TEXT_SUMMARY;
								echo '&nbsp;&nbsp;&nbsp;' . tep_draw_checkbox_field("waitlist_show",1,$waitlist_show) . TEXT_WAITLIST_ORDERS;
						?>
					</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 height="10"></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">
				<tr class='dataTableHeadingRow'>
				  <td class='dataTableHeadingContent' width="30"><?php echo TEXT_ID;?></td>
				  <TD class='dataTableHeadingContent'><?php echo TEXT_CLIENT;?></TD>
				  <TD class='dataTableHeadingContent' width="40"><?php echo TEXT_EMAIL;?></TD>
				  <td class='dataTableHeadingContent' width="50"><?php echo TEXT_SESSIONS;?></td>
				  <TD class='dataTableHeadingContent' width="60"><?php echo TEXT_ORDER_NO;?></TD>
				  <TD class='dataTableHeadingContent' width="80"><?php echo TEXT_STATUS;?></TD>
				  <TD class='dataTableHeadingContent' width="80"><?php echo TEXT_START_DATE;?></TD>
				  <TD class='dataTableHeadingContent' width="80"><?php echo TEXT_END_DATE;?></TD>
				  <TD class='dataTableHeadingContent' width="80"><?php echo TEXT_AMOUNT;?></TD>
				  <TD class='dataTableHeadingContent' width="90"><?php echo TEXT_OUTSTAND_AMOUNT;?></TD>
				</tr>
				<?php
					for ($icnt=0;$icnt<sizeof($output_array);$icnt++) {
						$row=&$output_array[$icnt];
						// output default row
						if ($row["type"]=="row"){
					?>
					  <TR class='dataTableRow' height="20" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="javascript:location.href='<?php echo tep_href_link(FILENAME_ORDERS,'oID=' . $row["orders_id"] . '&action=edit&return=pp');?>';">
						  <TD class='dataTableContent' style="cursor:hand"><?php echo $row["no"];?></td>
						  <TD class='dataTableContent' style="cursor:hand"><?php echo $row['customers_name']?></TD>
						  <TD class='dataTableContent' style="cursor:hand"><a href="mailto:<?php echo $row["customers_email_adress"];?>"><img src="images/icons/mail.gif" border="0"></a></TD>
						  <TD class='dataTableContent' style="cursor:hand"><?php echo $row['no_sessions']?></TD>
						  <TD class='dataTableContent' style="cursor:hand"><?php echo $row['orders_id'];?></TD>
						  <TD class='dataTableContent' style="cursor:hand"><?php echo $row['status'];?></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>
						  <TD class='dataTableContent' style="cursor:hand" align="right"><?php echo $row['amount_total']; ?></TD>
						  <TD class='dataTableContent' style="cursor:hand" align="right"><?php echo $row['outstanding']; ?></TD>
					  </TR>
				<?php
						} else if ($row["type"]=="total"){ 
						// output total
						?>
						<tr height="20">
							<td colspan="8" class="dataTableContent" align="right"><b><?php echo TEXT_TOTAL;?></b></td>
							<td class="dataTableContent" align="right"><b><?php echo $row["net_total"];?></b></td>
							<td class="dataTableContent" align="right"><b><?php echo $row["net_outs"];?></b></td>
						</tr>
					<?php
						} else if ($row["type"]=="event") {
						// output event name
						?>
						<tr height="20">
							<td colspan="10" class="main"><b><?php echo $row["content"];?></b></td>
						</tr>
					<?php
					   $i++;
					   }
				  }
				?>
				</table>
				</td>
			<tr>
		</table>
		</td>
	</tr>
</table>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 

	// function to generate pdf content
	function generate_pdf(){
		global $output_array,$display_header,$found_results;
		//create pdf table class
		$table=new pdfTable("A4","l");
		
		//set margins
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		
		// create styles for output content
		$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");

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

		// genreate coloumn widths
		$widths[0]=20;
		$widths[2]=60;
		$widths[3]=80;
		$widths[4]=50;
		$widths[5]=70;
		$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();
		
		// generate table header columns
		$cols[]=array("text"=>TEXT_ID,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_CLIENT,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_SESSIONS,"width"=>$widths[2],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_ORDER_NO,"width"=>$widths[3],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_STATUS,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_START_DATE,"width"=>$widths[5],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_END_DATE,"width"=>$widths[6],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_AMOUNT,"width"=>$widths[7],"align"=>"C","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_OUTSTAND_AMOUNT,"width"=>$widths[8],"align"=>"R","style"=>"headrow","valign"=>"M");
		
		$table->tableheaders["text"]=$cols;
		unset($cols);
		if ($found_results){
		// output table content columns
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			$cols=array();
			if ($row["type"]=="row"){
				$cols[]=array("text"=>$row["no"],"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row['customers_name'],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row['no_sessions'],"width"=>$widths[2],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row['orders_id'],"width"=>$widths[3],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["status"],"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");
				$cols[]=array("text"=>$row["amount_total"],"width"=>$widths[7],"align"=>"R","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$row["outstanding"],"width"=>$widths[8],"align"=>"R","style"=>"row","valign"=>"M");
				$table->OutputRow($cols,15);
			} else if ($row["type"]=="total"){
				$cols[]=array("text"=>TEXT_TOTAL,"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4]+$widths[5]+$widths[6],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row['net_total'],"width"=>$widths[7],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$row['net_outs'],"width"=>$widths[8],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,20);
			} else if ($row["type"]=="event") {
				$cols[]=array("text"=>$row["content"],"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);
			}

			unset($cols);
		}
		} else {
			$cols[]=array("text"=>REPORT_NO_RESULTS,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
		}
		// output pdf file
		$table->Render("pending_payments.pdf");
	}
	
	//function to get excel format csv datas
	function generate_excel(){
		global $output_array,$found_results;
		$result.=TEXT_ID . "," . TEXT_CLIENT . "," . TEXT_SESSIONS . "," .
				TEXT_ORDER_NO . "," . TEXT_STATUS . "," . 
				 TEXT_START_DATE . "," . TEXT_END_DATE . "," . 
				 TEXT_AMOUNT . "," .  TEXT_OUTSTAND_AMOUNT .  "\n";
		if ($found_results){
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			if ($row["type"]=="row"){
				$result.=$row["no"] . "," . $row['customers_name']. "," . $row['no_sessions'] . "," .
						 $row['orders_id'] . "," .  $row['status'] . "," . 
						 $row['start_date']. "," . $row['end_date'] . "," .
						 $row["amount_total"] . "," . $row["outstanding"] . "\n";
			} else if ($row["type"]=="total"){
				$result.=",,,,,,," . $row["net_total"] . "," . $row["net_outs"] . "\n";
			}else if ($row["type"]=="event"){
				$result.=$row["content"] . "\n";
			}
					
		}
		} else {
			$result.=REPORT_NO_RESULTS . "\n";
		}
		tep_write_text_file("pending_payments.csv",$result);
	}


?>
