<?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('includes/classes/pdfTable.php');
	
	$currencies=new currencies();
	$date_begin=isset($HTTP_POST_VARS['txt_start_date'])?tep_convert_date_raw($HTTP_POST_VARS['txt_start_date']):'';
	$date_end=isset($HTTP_POST_VARS['txt_end_date'])?tep_convert_date_raw($HTTP_POST_VARS['txt_end_date']):'';
	$resource=isset($HTTP_POST_VARS['sel_resource'])?$HTTP_POST_VARS['sel_resource']:-1;
	$location=isset($HTTP_POST_VARS['sel_location'])?$HTTP_POST_VARS['sel_location']:-1;
	$category=isset($HTTP_POST_VARS['sel_category'])?$HTTP_POST_VARS['sel_category']:-1;
	$output_pdf=isset($HTTP_POST_VARS['output_pdf'])?true:false;
	$output_excel=isset($HTTP_POST_VARS['output_excel'])?true:false;
	$post_action = isset($HTTP_POST_VARS['post_action'])?$HTTP_POST_VARS['post_action']:'';

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

	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 to be printed in the pdf page;
	$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"];
	}
	$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";
	
	
	$resources_array = array();
	$locations_array=array();

	
	
	// get resource names			
	$sql = "select r.resource_id resource_id, rd.resource_name  from " . TABLE_RESOURCES . " r, " . TABLE_RESOURCES_DESCRIPTION . " rd where r.resource_id=rd.resource_id order by resource_name";
	$sql_result = tep_db_query($sql);
	$num_rows = tep_db_num_rows($sql_result);
	$resources_array[]=array("id"=>-1,"text"=>TEXT_ALL_RESOURCE);
	while($row=tep_db_fetch_array($sql_result)) {
		$resources_array[]=array("id"=>$row["resource_id"],"text"=>$row["resource_name"]);
	}
	mysql_free_result($sql_result);

	// get service category names
	$sql = "select * from services_categories_description where language_id='" . $languages_id . "' order by services_categories_name";
	$sql_result = tep_db_query($sql);
	$categories_array[]=array("id"=>-1,"text"=>TEXT_ALL_SERVICE_CATEGORIES);
	while($row=tep_db_fetch_array($sql_result)) {
		$categories_array[]=array("id"=>$row["services_categories_id"],"text"=>$row["services_categories_name"]);
		if ($row["services_categories_id"]==$category){
			$display_header.=TEXT_SERVICE_CATEGORY  . ":&nbsp;&nbsp;" . $row["services_categories_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);
	
	if ($resource=="-1") $display_header.=TEXT_RESOURCE . ":&nbsp;&nbsp;" . TEXT_ALL_RESOURCE ."\t";
	if ($category=="-1") $display_header.=TEXT_SERVICE_CATEGORY . ":&nbsp;&nbsp;" . TEXT_ALL_SERVICE_CATEGORIES  ."\t";

	$found_results=false;
	$where = " ";
	$db_result=array();
	$num_rows=0;
	$cur_row=0;
	
	$cat_where='';
	if($resource!="-1") {
		$where.= "and os.resource_id='".$resource."' ";
	} 
	if($location!="-1") {
		$where.= "and se.service_location='".$location."' ";
	}
	if ($category!="-1"){
		$cat_where=" and secd.services_categories_id='" . $category . "' ";
	}

	$output_array=array();
	// fetch the category details
	
	//echo "SELECT secd.services_categories_name,secd.services_categories_id from " . TABLE_SERVICES_CATEGORIES . " sec, " .
	//								TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd where sec.services_categories_id=secd.services_categories_id and secd.language_id='" . $languages_id ."' " . $cat_where . " order by services_categories_name ";
	$category_query=tep_db_query("SELECT secd.services_categories_name,secd.services_categories_id from " . TABLE_SERVICES_CATEGORIES . " sec, " .
									TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd where sec.services_categories_id=secd.services_categories_id and secd.language_id='" . $languages_id ."' " . $cat_where . " order by services_categories_name ");

										
	$grand_tax=0;
	$grand_subtotal=0;
	$grand_total=0;
	$grand_cash=0;
	$grand_credit=0;
	$grand_cheque=0;
	$grand_discount=0;
	while ($category_result=tep_db_fetch_array($category_query)){
		//to get total amount for the current categoies, making indivitual fetch for every payment and join the details
			$sql=	"SELECT 1 as id,sum(op.products_price) as subtotal,sum(op.products_price-op.final_price) as discount,sum(op.final_price*op.products_tax/100) as tax,sum(op.final_price+(op.final_price*op.products_tax/100)) as total from " .
							TABLE_ORDERS_PRODUCTS . " op, " .  TABLE_SERVICES . " se, " . TABLE_SERVICES_TO_SERVICES_CATEGORIES . " se2c, " . TABLE_ORDERS . " o, " . TABLE_ORDERS_SERVICES ." os ".
							"where o.orders_id=op.orders_id and se2c.services_categories_id='" . $category_result["services_categories_id"] . "'" . 
							" and se.service_id=se2c.service_id and o.orders_id=os.orders_id and se.service_id and os.products_id and op.products_type='V' " . $where  . " and  lower(o.payment_method)='cheque/money order'" .
							" group by se2c.services_categories_id union " .
					"SELECT 2 as id,sum(op.products_price) as subtotal,sum(op.products_price-op.final_price) as discount,sum(op.final_price*op.products_tax/100) as tax,sum(op.final_price+(op.final_price*op.products_tax/100)) as total from " .
							TABLE_ORDERS_PRODUCTS . " op, " .   TABLE_SERVICES . " se, " . TABLE_SERVICES_TO_SERVICES_CATEGORIES . " se2c, " . TABLE_ORDERS . " o, " . TABLE_ORDERS_SERVICES ." os ".
							"where o.orders_id=op.orders_id and se2c.services_categories_id='" . $category_result["services_categories_id"] . "'" .
							" and se.service_id=se2c.service_id and o.orders_id=os.orders_id and se.service_id and os.products_id  and op.products_type='V' " . $where  . " and lower(o.payment_method)='cash'" .
							" group by se2c.services_categories_id union " .
					"SELECT 3 as id,sum(op.products_price) as subtotal,sum(op.products_price-op.final_price) as discount,sum(op.final_price*op.products_tax/100) as tax,sum(op.final_price+(op.final_price*op.products_tax/100)) as total from " .
							TABLE_ORDERS_PRODUCTS . " op, " .  TABLE_SERVICES . " se, " . TABLE_SERVICES_TO_SERVICES_CATEGORIES . " se2c, " . TABLE_ORDERS . " o, " . TABLE_ORDERS_SERVICES ." os ".
							"where o.orders_id=op.orders_id and se2c.services_categories_id='" . $category_result["services_categories_id"] . "'" .
							" and se.service_id=se2c.service_id and o.orders_id=os.orders_id and se.service_id and os.products_id  and op.products_type='V' " . $where  . " and lower(o.payment_method)='credit card'" .
							" group by se2c.services_categories_id";
		//query for results
		
		//echo $sql;
		
		$sql_result=tep_db_query($sql);
		$net_subtotal=0;
		$net_tax=0;
		$net_total=0;
		$net_discount=0;
		$cash=0;
		$credit=0;
		$cheque=0;
		$bank_transfer=0;
		while($row=tep_db_fetch_array($sql_result)){
			if ($row["id"]==3){
				$credit=$row["total"];
			} else if ($row["id"]==2){
				$cash=$row["total"];
			} else if($row["id"]==1) {
				$cheque=$row["total"];
			}else {
			   $bank_transfer=$row["total"];
			}
			$net_subtotal+=$row["subtotal"];
			$net_tax+=$row["tax"];
			$net_discount+=$row["discount"];
			$net_total+=$row["total"];
		}
		
				
		$output_array[]=array(	"type"=>"row","content"=>$category_result["services_categories_name"],
								"cash"=>tep_get_rounded_amount($cash),"credit"=>tep_get_rounded_amount($credit),
								"cheque"=>tep_get_rounded_amount($cheque),"bank_transfer"=>tep_get_rounded_amount($bank_transfer),"subtotal"=>tep_get_rounded_amount($net_subtotal),
								"discount"=>tep_get_rounded_amount($net_discount),
								"total"=>tep_get_rounded_amount($net_total),"tax"=>tep_get_rounded_amount($net_tax)
							);
		$grand_tax+=$net_tax;
		$grand_total+=$net_total;
		$grand_subtotal+=$net_subtotal;
		$grand_discount+=$net_discount;
		$grand_cash+=$cash;
		$grand_credit+=$credit;
		$grand_cheque+=$cheque;
		$grand_banktransfer+=$bank_transfer;
		mysql_free_result($sql_result);	
	}
	$output_array[]=array(	"type"=>"total","content"=>TEXT_TOTALS,
							"cash"=>tep_get_rounded_amount($grand_cash),"credit"=>tep_get_rounded_amount($grand_credit),
							"cheque"=>tep_get_rounded_amount($grand_cheque),
							"bank_transfer"=>tep_get_rounded_amount($grand_banktransfer),
							"subtotal"=>tep_get_rounded_amount($grand_subtotal),"total"=>tep_get_rounded_amount($grand_total),
							"discount"=>tep_get_rounded_amount($grand_discount),
							"tax"=>tep_get_rounded_amount($grand_tax)
						);
	$found_results=true;
	$report_filename=sprintf("services_sales_category_%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;
	}
?>
<!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(DIR_WS_INCLUDES."date_format_js.php")?>

<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_SALES_BY_CATEGORY;?></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_SALESBY_CATEGORY);
					echo '<input type="hidden" name="post_action" value="screen">';
				?>
				<table border="0" cellspacing="2" cellpadding="2" width="100%">
					<tr>
					    <td class="main" colspan="2">
						   <table width="100%" border="0" cellspacing="0" cellpadding="2" class="searchArea">
						     <tr>
							 <td width="6%" align="left"><?php echo TEXT_START_DATE . "&nbsp;&nbsp;" . tep_draw_input_field('txt_start_date',format_date($date_begin),' size="10"');?></td>
							 <td width="8%"><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
							<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;
							</td>
							<td class="main" align="left" width="5%"><?php echo TEXT_END_DATE . "&nbsp;&nbsp;" . tep_draw_input_field('txt_end_date',format_date($date_end),' size="10"');?></td>
							<td class="main" align="left" width="17%">
							<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>
							</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="6%"><?php echo TEXT_SERVICE_CATEGORY;?></td>
						  <td class="main" align="left" width="7%"><?php echo tep_draw_pull_down_menu("sel_category",$categories_array,$category);?></td>
						  <td class="main" align="left" width="5%"><?php echo '&nbsp;' .  TEXT_RESOURCE;?></td>
						  <td class="main" align="left" width="16%"><?php echo  tep_draw_pull_down_menu("sel_resource",$resources_array,$resource);?></td>
						</td>
						</tr>
						</table>
						</td>
						<td class="main" valign="middle" align="right" colspan="4">
								<?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>
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="1" cellpadding="1">
			<?php if ($found_results){  ?>
				<tr>
					<td>
					<table width="100%" border="0" cellspacing="0" cellpadding="2">
						<?php 
								for($icnt=0;$icnt<sizeof($output_array);$icnt++){
									$row=$output_array[$icnt];
									if ($row["type"]=="total") { 
									?>
										<tr>
											<td class="dataTableContent"><b><?php echo $row["content"];?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["subtotal"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["tax"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["total"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["credit"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["cash"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["cheque"]); ?></b></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["bank_transfer"]); ?></b></td>
										</tr>
										<tr>
											<td colspan="8"><?php echo tep_draw_separator();?></td>
										</tr>
										<tr>
											<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
										</tr>
									<?php } else { ?>
										<tr class="dataTableHeadingRow">
											<td class="dataTableHeadingContent"><?php echo $row["content"];?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_SUBTOTAL;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_TAX;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_TOTAL;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_CREDIT;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_CASH;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_CHEQUE;?></td>
											<td class="dataTableHeadingContent" width="100" align="right"><?php echo TEXT_BANKTRANSFER;?></td>
										</tr>
										<tr>
											<td class="dataTableContent"></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["subtotal"]); ?></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["tax"]); ?></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["total"]); ?></b></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["credit"]); ?></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["cash"]); ?></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["cheque"]); ?></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["ban_transfer"]); ?></td>
										</tr>
										<tr>
											<td colspan="8"><?php echo tep_draw_separator();?></td>
										</tr>
										<tr>
											<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
										</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 to pdf content
	function generate_pdf(){
		global $output_array,$display_header,$currencies,$report_filename;
		
		// set initital 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 styles for table
		$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"]=4;

		// define column widths
		$widths[1]=70;
		$widths[2]=70;
		$widths[3]=70;
		$widths[4]=70;
		$widths[5]=70;
		$widths[6]=70;
		$widths[7]=70;
		$widths[8]=70;

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

		$cols=array();
		// output the content
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			$cols=array();
			if ($row["type"]=="total"){
				$cols[]=array("text"=>$row["content"],"width"=>$widths[0],"align"=>"L","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['subtotal']),"width"=>$widths[1],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['tax']),"width"=>$widths[3],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['total']),"width"=>$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['credit']),"width"=>$widths[5],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['cash']),"width"=>$widths[6],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['cheque']),"width"=>$widths[7],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['bank_transfer']),"width"=>$widths[8],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,15);
			} else {
				$cols[]=array("text"=>$row["content"],"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_SUBTOTAL,"width"=>$widths[1],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_TAX,"width"=>$widths[3],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_TOTAL,"width"=>$widths[4],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_CREDIT,"width"=>$widths[5],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_CASH,"width"=>$widths[6],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_CHEQUE,"width"=>$widths[7],"align"=>"R","style"=>"headrow","valign"=>"M");
				$cols[]=array("text"=>TEXT_BANKTRANSFER,"width"=>$widths[8],"align"=>"R","style"=>"headrow","valign"=>"M");
				$table->OutputRow($cols,20);
				unset($cols);
				$cols[]=array("text"=>"","width"=>$widths[0],"align"=>"L","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['subtotal']),"width"=>$widths[1],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['tax']),"width"=>$widths[3],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['total']),"width"=>$widths[4],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['credit']),"width"=>$widths[5],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['cash']),"width"=>$widths[6],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['cheque']),"width"=>$widths[7],"align"=>"R","style"=>"subrow","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($row['bank_transfer']),"width"=>$widths[8],"align"=>"R","style"=>"subrow","valign"=>"M");

				$table->OutputRow($cols,20);
				$table->DrawLine();
				$table->DrawGap(10);
			}

			unset($cols);
		}
		// create the pdf file
		$table->Render($report_filename.".pdf");
	}
	function generate_excel(){
		global $output_array,$currencies,$report_filename;
		for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			$row=&$output_array[$icnt];
			if ($row["type"]=="total"){
				$result.=$row["content"] . "," . $currencies->format($row['subtotal']) . "," . 
						 //$currencies->format($row['discount']) . "," . $currencies->format($row['tax']) . "," .
						 $currencies->format($row['total']) . "," .  $currencies->format($row['credit']) . "," . 
						 $currencies->format($row['cash']). "," . $currencies->format($row['cheque']) . "," . $currencies->format($row['bank_transfer']) . "\n";
			} else {
				$result.=$row["content"] . "," . TEXT_SUBTOTAL . "," . TEXT_TAX . "," .
						 TEXT_TOTAL . "," .  TEXT_CREDIT . "," . 
						 TEXT_CASH. "," . TEXT_CHEQUE . "," . TEXT_BANKTRANSFER . "\n";

				$result.="," . $currencies->format($row['subtotal']) . "," . 
						 //$currencies->format($row['discount']) . "," . $currencies->format($row['tax']) . "," .
						 $currencies->format($row['total']) . "," .  $currencies->format($row['credit']) . "," . 
						 $currencies->format($row['cash']). "," . $currencies->format($row['cheque']) . "," . $currencies->format($row['bank_transfer']) . "\n";
			}
		}
		tep_write_text_file($report_filename.".csv",$result);
	}


?>
