<?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/functions/message_functions.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'])?$input_params['txt_start_date']:'';
	$date_end=isset($input_params['txt_end_date'])?$input_params['txt_end_date']:'';
	$provider=isset($input_params['sel_provider'])?$input_params['sel_provider']:-1;
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$service=isset($input_params['sel_service'])?$input_params['sel_service']:-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 the start date is empty,select records past 1 month
	if ($date_begin==""){
		$sql =  "select date_sub(curdate(), interval 1 month) begin,curdate() as end";
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$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";

	$providers_array = array();
	$services_array = array();
	$locations_array=array();

	// get provider names			
	 $sql = "select  provider_id, provider_firstname, provider_lastname  from ".TABLE_SERVICES_PROVIDERS." sep order by provider_firstname,provider_lastname";
	$sql_result = tep_db_query($sql);
	$num_rows = tep_db_num_rows($sql_result);
	$providers_array[]=array("id"=>-1,"text"=>TEXT_ALL_PROVIDERS);
	while($row=tep_db_fetch_array($sql_result)) {
		if ($provider==-1) {
			$provider=$row["provider_id"];
		}
		$providers_array[]=array("id"=>$row["provider_id"],"text"=>$row["provider_firstname"]." ".$row["provider_lastname"]);
		if ($row["provider_id"]==$provider){
			$display_header.=TEXT_PROVIDER  . ":&nbsp;&nbsp;" . $row["provider_firstname"]." ".$row["provider_lastname"] . "\t";
			$provider_name=$row["provider_firstname"] . ' ' . $row["provider_lastname"];
		}
	}
	mysql_free_result($sql_result);
		
	// get location names
	$sql = "select  distinct se.service_location as service_location,l.service_location_name as location_name from ".TABLE_SERVICES ." se, services_locations l where l.service_location_id=se.service_location  ";
	//"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["service_location"],"text"=>$row["location_name"]);
	}
	mysql_free_result($sql_result);

	// get all  services
	$sql = "select se.service_id service_id, service_name  service_name from services se, services_description sed where se.service_id = sed.service_id and sed.language_id='" . (int)$languages_id . "'" . SERVICES_CONDITION_STATUS . "order by service_name";
	$sql_result = tep_db_query($sql);
	$services_array[]=array("id"=>-1,"text"=>TEXT_ALL_SERVICES);
	while($row=tep_db_fetch_array($sql_result)) {
		$services_array[]=array("id"=>$row["service_id"],"text"=>$row["service_name"]);
		if ($row["service_id"]==$service){
			$display_header.=TEXT_SERVICE  . ":&nbsp;&nbsp;" . $row["service_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);

	if ($provider=="-1") $display_header.=TEXT_PROVIDER . ":&nbsp;&nbsp;" . TEXT_ALL_PROVIDERS ."\t";
	if ($service=="-1") $display_header.=TEXT_SERVICE . ":&nbsp;&nbsp;" . TEXT_ALL_SERVICES  ."\t";

	$found_results=false;
		$where = " ";
		$db_result=array();
		$num_rows=0;
		$cur_row=0;
		
	if($provider!="-1") {
		$where.= "and se.provider_id='".$provider."' ";
	} 
	if($location!="-1") {
		$where.= "and se.service_location='".$location."' ";
	}
	if($service!="-1") {
		$where.= "and se.service_id='".$service ."' ";
	}

	// prepare sql for fetching Payment details for Instructor
	$sql="SELECT distinct se2c.service_id,se2c.services_categories_id,secd.services_categories_name,op.products_id,sed.service_name,se.provider_rate_type,se.provider_rate,se.provider_base_rate,
					time_format(se.service_start_time,'%H:%i %p') as start_time,time_format(se.service_end_time,'%H:%i %p') as end_time,
					concat(a.admin_firstname,' ',a.admin_lastname) as instructor_name,se.provider_id from " .
					TABLE_ORDERS_PRODUCTS . " op, " .   TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed," . TABLE_ADMIN . " a, " . 
					TABLE_SERVICES_TO_SERVICES_CATEGORIES . " se2c, " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd " . 
					"where op.products_id=se.service_id and se.service_id=sed.service_id and se.service_id=se2c.service_id " . 
					" and sed.language_id='" . $languages_id ."' and secd.language_id='" . $languages_id . "' and se2c.services_categories_id=secd.services_categories_id " .
					" and se.provider_id=a.admin_id and op.products_type='V' " . $where .
					" group by se.service_id order by instructor_name, sed.service_name";
	//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;
		$services_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 service, 
		$count_query=tep_db_query("SELECT os.start_date,os.products_id,count(os.customers_id) as total,sum(op.final_price) as total_amount from " . TABLE_ORDERS_SERVICES . " os," . TABLE_ORDERS_PRODUCTS . " op group by os.products_id");
		while($count_result=tep_db_fetch_array($count_query)){
		 $temp_cnt=(int)$count_result["total"];
		 $temp_total=(float) $count_result["total_amount"];
		 $temp_date=format_date($count_result['start_date']);
			$totals_array[$count_result["products_id"]]=array("total"=>$temp_cnt,"total_amount"=>$temp_total,"start_date"=>$temp_date);
		}
		mysql_free_result($count_query);
		
		while($row=tep_db_fetch_array($sql_result)) {
			if ($prev_category!=$row["services_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["services_categories_id"];
				$output_array[]=array("type"=>"category","category_name"=>$row["services_categories_name"]);
			}

			$rate=0.0;

			$rate_type=$row["provider_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"];
                $service_date=$totals_array[$row["products_id"]]["start_date"];
				// calculate Instructor's rate for the single event
				if ($rate_type=="FL") {// flat rate
					$rate=$row["provider_base_rate"];
					$pay_text=$currencies->format($rate);
				}
				if ($rate_type=='MU') { // multiple student
					$rate=($row["provider_rate"]*$total_students)+$row["provider_base_rate"];
					$pay_text=$currencies->format($row["provider_rate"]) . "/S + " . $currencies->format($row["provider_base_rate"]);
				}
				if ($rate_type=='GP'){ // gross percentage
					$rate=$row["provider_base_rate"]+$amount*$row["provider_rate"]/100;
					$pay_text=$currencies->format($amount) . " * " . number_format($row["provider_rate"],2) . "% + " . $currencies->format($row["provider_base_rate"],2);
				}
			}
			$total_amount+=$rate;
			$output_array[]=array(	"type"=>"row","no"=>$row_cnt,"service_name"=>$row["service_name"],"time"=>$row["start_time"] . " - " . $row["end_time"],
									"date"=>format_date($service_date),"students"=>$total_students,"pay_text"=>$pay_text,"rate"=>$currencies->format($rate));
			$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_PROVIDER,$provider_name),
									"net_pay"=>$currencies->format($net_pay),"net_amount"=>$currencies->format($net_amount));
		}
		mysql_free_result($sql_result);
	}
	// output to pdf
	if ($post_action=="pdf"){
		generate_pdf();
		tep_redirect(DIR_WS_CATALOG . "images/services_provider_payments.pdf");
		return;
	}

	// output to excel
	if ($post_action=="excel"){
		generate_excel();
		tep_redirect(DIR_WS_CATALOG . "images/services_provider_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("<?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>
				<td class="pageHeading"><?php echo HEADING_PROVIDER_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_SERVICES_PROVIDER_PAYMENTS,"action=search&mode=1");
					echo '<input type="hidden" name="post_action" value="screen">';
				?>
				<table border="0" cellspacing="2" cellpadding="2" class="formArea" width="100%">
					<tr>
						<td nowrap class="main">
							<table width="100%" border="0" cellspacing="0" cellpadding="2">
						     <tr>
							 <td class="main" width="5%" align="left"><?php echo TEXT_START_DATE;?></td>
							 <td class="main" width="8%">
							 &nbsp;<INPUT type="text" name="txt_start_date" size="10" value="<?php echo $date_begin; ?>">
							<a href="javascript:show_calendar('f.txt_start_date');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>&nbsp;&nbsp;
							</td>
							<td class="main" align="left" width="5%"><?php echo TEXT_END_DATE;?></td>
							<td class="main" align="left" width="17%">
							&nbsp;<INPUT type="text" name="txt_end_date" size="10" value="<?php echo $date_end;?>">
							<a href="javascript:show_calendar('f.txt_end_date');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>
							 </td>
							</tr>
						  </table>
						</td>
						<td class="main" align="left" colspan="2"></td>
						<td valign="middle" align="right" colspan="2">
							<?php echo '<a href="javascript:doReport(1);">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_DETAILS) . '</a>'; ?>
						</td>
					</tr>
					<tr>
						<td class="main" colspan="2">
						<table width="100%" border="0" cellspacing="0" cellpadding="2">
						 <tr>
						  <td class="main" align="left" width="5%"><?php echo TEXT_PROVIDER ;?></td>
						  <td class="main" align="left" width="8%"><?php echo tep_draw_pull_down_menu("sel_provider",$providers_array,$provider);?></td>
						  <td class="main" align="left"	 width="5%"><?php echo TEXT_SERVICE;?></td>
						  <td class="main" align="left" width="17%"><?php echo tep_draw_pull_down_menu("sel_service",$services_array,$service);?></td>
						</td>
						</tr>
						</table>
						</td>
						<td class="main" valign="middle" align="right" colspan="5">
								<?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="6">
								<?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><?php echo tep_draw_separator('pixel_trans.gif', '1', '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">
			<?php if ($found_results){  ?>
				<tr>
					<td>
					<table width="100%" border="0" cellspacing="1" cellpadding="2">
						<tr class="dataTableHeadingRow">
							<td class="dataTableHeadingContent" width="10"><?php echo TEXT_ID;?></td>
							<td class="dataTableHeadingContent"><?php echo TEXT_SERVICE_NAME;?></td>
							<td class="dataTableHeadingContent" width="120"><?php echo TEXT_TIME;?></td>
							<td class="dataTableHeadingContent" width="70"><?php echo TEXT_DATE;?></td>
							<td class="dataTableHeadingContent" width="60"><?php echo TEXT_STUDENTS;?></td>
							<td class="dataTableHeadingContent" width="180"><?php echo TEXT_PAY_TYPE;?></td>
							<td class="dataTableHeadingContent" width="100"><?php echo TEXT_PAY_AMOUNT;?></td>
							<td class="dataTableHeadingContent" width="100"><?php echo TEXT_TOTAL_AMOUNT;?></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") {?>
										<tr class="dataTableRow">
											<td class="dataTableContent"><?php echo $row["no"]; ?></td>
											<td class="dataTableContent"><?php echo $row["service_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"]; ?>
										</tr>
									<?php } 
								}
						?>
						</table>
						</td>
					</tr>
					<?php } ?>
					<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;
		// 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;

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

		$cols=array();
		
		// form the header columns
		$cols[]=array("text"=>TEXT_ID,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_SERVICE_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");
		
		$table->tableheaders["text"]=$cols;
		unset($cols);
		if ($found_results){		
		// output the table rows
		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['service_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");
				$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],"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],"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");
				$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 to pdf file
		$table->Render("services_provider_payments.pdf");
	}

	// function to create excel content
	function generate_excel(){
		global $output_array,$found_results;
		$result.=TEXT_ID . "," . TEXT_SERVICE_NAME . "," . TEXT_TIME . "," .
				TEXT_DATE . "," . TEXT_STUDENTS . "," . 
				 TEXT_PAY_TYPE . "," . TEXT_PAY_AMOUNT . "," . 
				 TEXT_TOTAL_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['service_name']. "," . strtolower($row['time']) . "," .
						 $row['date'] . "," .  $row['students'] . "," . 
						 $row['pay_text']. "," . $row['rate'] . "," .
						 $row["rate"] . "\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.=REPORT_NO_RESULTS . "\n";
		}
		tep_write_text_file("services_provider_payments.csv",$result);
	}


?>
