<?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']):'';
	$instructor=isset($HTTP_POST_VARS['sel_instructor'])?$HTTP_POST_VARS['sel_instructor']:-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('".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"];
	}
	// 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";
	
	
	$instructors_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)) {
		$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 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 all  events
	$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);
	$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 ($category=="-1") $display_header.=TEXT_EVENT_CATEGORY . ":&nbsp;&nbsp;" . TEXT_ALL_EVENT_CATEGORIES  ."\t";

	$found_results=false;
	$where = " ";
	$db_result=array();
	$num_rows=0;
	$cur_row=0;
	
	$cat_where='';
	if($instructor!="-1") {
		$where.= "and e.events_instructor='".$instructor."' ";
	} 
	if($location!="-1") {
		$where.= "and e.events_location='".$location."' ";
	}
	if ($category!="-1"){
		$cat_where=" and ecd.events_categories_id='" . $category . "' ";
	}

	$output_array=array();
	// fetch the category details
	$category_query=tep_db_query("SELECT ecd.events_categories_name,ecd.events_categories_id from " . TABLE_EVENTS_CATEGORIES . " ec, " .
									TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.events_categories_id=ecd.events_categories_id and ecd.language_id='" . $languages_id ."' " . $cat_where . " order by events_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)){
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
		//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.events_fees) as subtotal,sum(op.events_fees-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_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec2, " . TABLE_ORDERS . " o " .
					"where o.orders_id=op.orders_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ec2.events_categories_id='" . $category_result["events_categories_id"] . "'" . 
					" and e.events_id=ec2.events_id and op.products_type='E' and op.events_type='R' " . $where  . $events_option ." and s.start_date>='$date_begin' and s.start_date<='$date_end' and lower(o.payment_method)='cheque/money order'" .
					" group by ec2.events_categories_id union " .
					"SELECT 2 as id,sum(op.events_fees) as subtotal,sum(op.events_fees-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_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec2, " . TABLE_ORDERS . " o " .
							"where o.orders_id=op.orders_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ec2.events_categories_id='" . $category_result["events_categories_id"] . "'" .
							" and e.events_id=ec2.events_id and op.products_type='E' and op.events_type='R' " . $where  . $events_option . " and s.start_date>='$date_begin' and s.start_date<='$date_end' and lower(o.payment_method)='cash'" .
							" group by ec2.events_categories_id union " .
					"SELECT 3 as id,sum(op.events_fees) as subtotal,sum(op.events_fees-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_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec2, " . TABLE_ORDERS . " o " .
							"where o.orders_id=op.orders_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ec2.events_categories_id='" . $category_result["events_categories_id"] . "'" .
							" and e.events_id=ec2.events_id and op.products_type='E' and op.events_type='R' " . $where  . $events_option . " and s.start_date>='$date_begin' and s.start_date<='$date_end' and lower(o.payment_method)='credit card'" .
							" group by ec2.events_categories_id";
		//query for results
		
		
		$sql_result=tep_db_query($sql);
		$net_subtotal=0;
		$net_tax=0;
		$net_total=0;
		$net_discount=0;
		$cash=0;
		$credit=0;
		$cheque=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 {
				$cheque=$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["events_categories_name"],
								"cash"=>tep_get_rounded_amount($cash),"credit"=>tep_get_rounded_amount($credit),
								"cheque"=>tep_get_rounded_amount($cheque),"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;
		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),
							"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("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>
<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_SALES_CATEGORY);
					echo '<input type="hidden" name="post_action" value="screen">';
				?>
				<table border="0" cellspacing="2" cellpadding="2" class="searchArea" width="100%">
					<tr>
						<td nowrap><?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;?> <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 align="left">
							<?php 
								echo TEXT_INSTRUCTOR . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("sel_instructor",$instructors_array,$instructor);
							?>
						</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>
							<?php 
								echo TEXT_EVENT_CATEGORY . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("sel_category",$categories_array,$category);
							?>
						</td>
						<td valign="middle" align="right" colspan="2">
								<?php echo '<a href="javascript:doReport(2);">' . tep_image_button('button_export_pdf.gif', IMG_EXPORT_PDF) . '</a>';?>
						</td>		
					</tr>
					<tr>
						<td 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><?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["discount"]); ?></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>
										</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_DISCOUNTS;?></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>
										</tr>
										<tr>
											<td class="dataTableContent"></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($row["subtotal"]); ?></td>
											<td class="dataTableContent" align="right"><b><?php echo $currencies->format($row["discount"]); ?></b></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>
										</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,$login_id,$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"]=3;

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


		


		$temp_width=$table->width-($widths[1]+$widths[2]+$widths[3]+$widths[4]+$widths[5]+$widths[6]+$widths[7]);
		$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['discount']),"width"=>$widths[2],"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");
				$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_DISCOUNTS,"width"=>$widths[2],"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");
				$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['discount']),"width"=>$widths[2],"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");

				$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,$login_id,$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']) . "\n";
			} else {
				$result.=$row["content"] . "," . TEXT_SUBTOTAL . "," . TEXT_DISCOUNTS . "," .TEXT_TAX . "," .
						 TEXT_TOTAL . "," .  TEXT_CREDIT . "," . 
						 TEXT_CASH. "," . TEXT_CHEQUE . "\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']) . "\n";
			}
		}
		tep_write_text_file($report_filename.".csv",$result);
	}


?>
