<?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(DIR_WS_CLASSES . 'split_page_results_report.php');
  require(DIR_WS_FUNCTIONS . 'services_tree.php');
  require('includes/classes/pdfTable.php');
  define(BOX_WIDTH1,'125');
  // get initial parameters, first check session for previous settings
	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());
		}
	}


	$date_begin=isset($input_params['txt_start_date'])?tep_convert_date_raw($input_params['txt_start_date']):'';
	$date_end=isset($input_params['txt_end_date'])?tep_convert_date_raw($input_params['txt_end_date']):'';
	$resource=isset($input_params['sel_resource'])?$input_params['sel_resource']:-1;
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$customer=isset($input_params['sel_customer'])?$input_params['sel_customer']:"-1";
	$service=isset($input_params['sel_service'])?$input_params['sel_service']:-1;
	$summary_show=isset($input_params['chk_summary'])?true:false;
	$post_action = isset($input_params['post_action'])?$input_params['post_action']:'';
	$page = isset($input_params['page'])?$input_params['page']:1;

	$currencies=new currencies();
	
	// change the current status of selected orders
	if ($post_action=="change_status"){
		$stat_ids=$input_params["chk_status"];
		$new_status=$input_params["new_status"];
		$order_ids="";
		for ($icnt=0;$icnt<sizeof($stat_ids);$icnt++){
			$order_ids.=$stat_ids[$icnt] . ",";
		}
		if ($order_ids!=""){
			$order_ids=substr($order_ids,0,-1);
			tep_db_query("UPDATE " . TABLE_ORDERS . " set last_modified='" . getServerDate(true) . "',orders_status='" . $new_status . "' where orders_id in(" . $order_ids . ")");
			tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set orders_products_status= '" . $new_status . "' where orders_id = '" . (int)$order_ids . "'"); 
			tep_redirect(FILENAME_SERVICES_PENDING_ORDERS);
			$HTTP_POST_VARS['post_action']="screen";
			/*echo tep_draw_form("frmSearch",FILENAME_SERVICES_PENDING_ORDERS);
			echo '<input type="hidden" name="post_action" value="screen">';
			echo tep_get_report_params();
			echo '<script language="javascript">' .
				'document.frmSearch.submit();' .
				'</script></form>';
			return; */ 
		}
	}

	if ($post_action==""){
		$post_action="screen";
	}
	if ($date_begin==""){
		$date_begin=date("Y-m-d");
	}
	if ($date_end==""){
		//$sql =  "select date_add('$date_begin', interval -(dayofmonth('$date_begin')-1) day) begin, date_add('$date_begin', interval (30-dayofmonth('$date_begin')) day) end";	
		$sql =  "select date_add('$date_begin', interval 1 month) end";	
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		
		$date_end = $row["end"];	
	}

	$customers_array = array();
	$locations_array=array();
	$order_status_array=array();
	$status_array=array();
	

	// get provider names			
$sql = "select  r.resource_id, rd.resource_name as resource_name from ".TABLE_RESOURCES." r," .TABLE_RESOURCES_DESCRIPTION. " rd where r.resource_id=rd.resource_id  and rd.language_id='".(int)$languages_id."' order by resource_id";
	$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 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 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  ";
	$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 call centre orders
	$sql="SELECT distinct c.customers_id,concat(c.customers_firstname,' ',c.customers_lastname) as customers_name from " . TABLE_CUSTOMERS . " c, " . TABLE_ORDERS . 
			" o where c.customers_id=o.customers_id order by customers_name";
	
	$sql_result=tep_db_query($sql);
	$customers_array[]=array("id"=>"-1","text"=>TEXT_ALL_CUSTOMERS);
	while($row=tep_db_fetch_array($sql_result)){
		$customers_array[]=array("id"=>$row["customers_id"],"text"=>$row["customers_name"]);
	}
	mysql_free_result($sql_result);
$status_array=tep_get_orders_status();
	// 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"];
		//$status_array[]=array('id'=>$row["orders_status_id"],'text'=>$row["orders_status_name"]);
	}
	$found_results=false;
		$where = " ";
		$db_result=array();
		$num_rows=0;
		$cur_row=0;
		$rows_each=REPORT_MAX_ROWS_PAGE;
		//if ($event_show && $product_show){
	//		$rows_each=$rows_each/2;
	//	}
			if($resource!="-1") {
				$where.= "and os.resource_id='".$resource."' ";
			} 
			if($location!="-1") {
				$where.= "and se.service_location='".$location."' ";
			}
			if($service!="-1") {
				$where.= "and se.service_id='".$service."' ";
			}
			if($customer!="-1") {
				$where.= "and o.customers_id='".tep_db_prepare_input($customer)."' ";
			}
			// create the query to fetch unpaid service details
			$sql = "select o.orders_status,o.orders_id, o.customers_name customers_name,o.customers_id,
			o.customers_email_address customers_email_address, sed.service_name as item_name,se.service_id as item_id,
			sum(op.final_price) as final_price, op.products_tax from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_SERVICES . " se, ". TABLE_ORDERS_SERVICES . " os," . TABLE_SERVICES_DESCRIPTION . 
			" sed, " . TABLE_ORDERS . " o where op.products_type='V' and os.products_id=se.service_id and os.products_id = op.products_id and os.orders_id=o.orders_id and op.orders_id=os.orders_id and op.orders_id=o.orders_id and sed.language_id='" . $languages_id . "'
			and sed.service_id=se.service_id and o.orders_status='1' and date_format(o.date_purchased,'%Y-%m-%d')>='$date_begin' and date_format(o.date_purchased,'%Y-%m-%d')<='$date_end'".$where." group by o.orders_id,se.service_id order by item_name,o.customers_name";
			
			//echo $sql;
			if ($post_action=="screen"){
				$query_split1 = new splitPageResultsReport($page, $rows_each, $sql, $query_split_numrows1);
			}
					
			$db_result[$cur_row]=array("display"=>TEXT_SERVICES , "result"=>tep_db_query($sql));
			$num_rows=tep_db_num_rows($db_result[$cur_row]["result"]);
			$cur_row++;

		if ($num_rows>0) {
			$found_results=true;
			if ($post_action=="screen"){
				if ($query_split_numrows1>=$query_split_numrows2){
					$query_split=&$query_split1;
				} else {
					$query_split=&$query_split2;
				}
				$query_split_numrows=$query_split_numrows1+$query_split_numrows2;
			}
		}
		
	//output to pdf
	$report_filename=sprintf("services_pending_%s_%s",$login_id,time());
	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 showOrderDetails(id){
		location.href="orders.php?action=edit&oID="+id;
	}
	function doReport(mode) {
	  startDate=date_format(document.f.txt_start_date.value,'','y-m-d',true);	
	  endDate=date_format(document.f.txt_end_date.value,'','y-m-d',true);
	  if(startDate>endDate) {
			alert("<?php echo REPORT_START_AFTER_END?>");
			return;
     }
	
	 if (mode==3){
					document.f.post_action.value="excel"
					document.f.target="_blank";
				} else if (mode==2){
					document.f.post_action.value="pdf";
					document.f.target="_blank";
				} else {
					document.f.post_action.value="screen";
					document.f.target="_self";
				}
				
		
		document.f.submit();
	}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
	<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->	  
	<td width=100% align=left valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr height="5">
			<!--<td class="pageHeading"><?php //echo HEADING_UNPAID_PAYMENTS;?></td>!-->
		</tr>
        <tr >
		  	<TD>
			<?php 	echo tep_draw_form("f",FILENAME_SERVICES_PENDING_ORDERS	);
					echo '<input type="hidden" name="post_action" value="screen">';
			?>
				<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
					<tr>
					<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
						<td align="left" nowrap="true"><?php echo TEXT_FROM;?> &nbsp;<?php echo tep_draw_input_field("txt_start_date",format_date($date_begin),'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>
							<?php echo tep_draw_separator('pixel_trans.gif','15','10') . TEXT_TO;?>&nbsp;<?php echo tep_draw_input_field("txt_end_date",format_date($date_end),'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>
						</td>	
						<td nowrap="true">
						<?php echo TEXT_LOCATION . '&nbsp;' . tep_draw_pull_down_menu("sel_location",$locations_array,$location);?>&nbsp;<?php echo tep_draw_checkbox_field("chk_summary",1,$summary_show) . TEXT_SUMMARY_ONLY;?>
						</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 nowrap="true" width="10%" align="left"><?php echo TEXT_CUSTOMER;?>&nbsp;<?php echo tep_draw_pull_down_menu("sel_customer",$customers_array,$customer);?></td>
						<td nowrap="true">
						<?php 
							$all_service_array=array();
							$all_service_array = tep_get_service_array_single();
							if(sizeof($all_service_array)<=0) echo 'Services are not Available';
							else echo TEXT_SERVICES . '&nbsp;' . tep_draw_service_select_menu('sel_service',$all_service_array,$service,' id="sel_service"');
						?>
							<?php 	echo TEXT_RESOURCE . '&nbsp;' . tep_draw_pull_down_menu("sel_resource",$resources_array,$resource);?>	
						</td>
						<td 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 valign="middle" align="right" colspan="2">
						</td>
						<td valign="middle" align="right">
								<?php echo '<a href="javascript:doReport(3);">' . tep_image_button('button_export_excel.gif', IMG_EXPORT_EXCEL) . '</a>';?>
						</td>
					</tr>
					<tr><td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','1','3');?></td></tr>
					<!-- report content -->
				</table>
		</form>
		</td>
	</tr>
	<script language="JavaScript">
		function validateForm(frm){
			var err="";
			var element=frm.elements["chk_status[]"];
			
			if (element.length){
			
				for (icnt=0;icnt<element.length;icnt++){
					if (element[icnt].checked) break;
				}
				if (icnt>=element.length){
					err="<?php echo addslashes(stripslashes(ERR_SELECT_ORDER));?>";
				}
			} else {
				if (!element.checked){
					err="<?php echo addslashes(stripslashes(ERR_SELECT_ORDER));?>";
				}
			}
			if (err!="") {
				alert(err);
				return false;
			}
			return true;
		}
		function selectAll(){
			var element=document.change_status.elements["chk_status[]"];
			var status=document.change_status.chk_select.checked;
			if (element.length){
				for (icnt=0;icnt<element.length;icnt++){
					element[icnt].checked=status;
				}
			} else {
				element.checked=status;			
			}
		}
	</script>
	<?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) {
					echo tep_draw_form("change_status",FILENAME_SERVICES_PENDING_ORDERS,'','post','onSubmit="javascript:return validateForm(this);"');
					echo tep_get_report_params();
					echo '<input type="hidden" name="post_action" value="change_status">';
					for ($icnt=0;$icnt<sizeof($db_result);$icnt++) {
						$sql_result=&$db_result[$icnt]["result"];
			?>
				<tr>
					<td>
					<table border="0" cellpadding="2" cellspacing="0" class="searchArea" width="100%">
						<tr>
							<td class="TableHeading"><?php echo $db_result[$icnt]["display"]?></td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,5);?></td>
				</tr>
				<tr>
					<td>
					<table border="0" cellpadding="2" cellspacing="0" width="100%">
							<tr class='dataTableHeadingTitleRow'>
								<td class='dataTableHeadingTitleContent' width="30">&nbsp;</td>
								<td class='dataTableHeadingTitleContent' width="30"><?php echo TEXT_ID;?></td>
								<td class='dataTableHeadingTitleContent'><?php echo TEXT_CLIENT;?></td>
								<td class='dataTableHeadingTitleContent' width="50"><?php echo TEXT_EMAIL;?></td>
								<td class='dataTableHeadingTitleContent' width="100"><?php echo TEXT_STATUS;?></td>
								<td class='dataTableHeadingTitleContent' width="100" align="right"><?php echo TEXT_AMOUNT;?></td>
							</tr>

			<?php 
					$i=1;
					$prev_customer="";
					$prev_item=0;
					$total=0;
					$grand_total=0;
          			while($row = tep_db_fetch_array($sql_result)) {
						$customer=$row["customers_id"];
						$item=$row["item_id"];
					?>
					<?php if ($prev_item!=$item) { // output the item name
								if ($total>0) { // output the total amount
						?>
							<tr>
								<td class="dataTableContent" colspan="5" align="right">
									<b><?php echo TEXT_TOTAL;?></b>
								</td>
								<td align="right" class='dataTableContent'>
									<?php echo $currencies->format($total);
										$total=0;
									?>
								</td>
							</tr>
							<tr>
								<td colspan="6"><?php echo tep_draw_separator();?></td>
							</tr>
						<?php } // total >0 ?>
							<tr>
								<td class="dataTableContent" colspan="6"><b>
									<?php
										echo $row["item_name"];
										$prev_item=$item;
									?>
									</b>
								</td>
							</tr>
					<?php 
					} // prev item 
						$amount=tep_add_tax($row['final_price'],$row["products_tax"]);
						$amount=tep_get_rounded_amount($amount);
						if (!$summary_show) {
					?>
						  <tr class='dataTableRow' height="20">
						  	  <td class='dataTableContent'><?php echo tep_draw_checkbox_field('chk_status[]',$row["orders_id"]);?></td>
							  <span onClick="javascript:location.href='<?php echo tep_href_link(FILENAME_ORDERS,'oID=' . $row["orders_id"] . '&action=edit&return=rspo&type=ser');?>';">
								  <td class='dataTableContent' style="cursor:default;cursor:hand"><?php echo $i?></td>
								  <td class='dataTableContent' style="cursor:default;cursor:hand"><?php echo $row['customers_name']?></td>
								  <td class='dataTableContent' align="center"><a href="mailto:<?php echo $row['customers_email_address']?>"><img src="images/icons/mail.gif" border="0"></a></td>
								  <td class='dataTableContent' style="cursor:default;cursor:hand"><?php echo $order_status_array[$row['orders_status']];?></td>
								  <td class='dataTableContent' style="cursor:default;cursor:hand" align="right"><?php echo $currencies->format($amount);?></td>
							  </span>
						  </tr>
			<?php 
						} // summary show
						$i++;
						$total+=$amount;
						$grand_total+=$amount;
						}
						if ($total>0) {
						?>
							<tr>
								<td class="dataTableContent" style="cursor:hand" colspan="5" align="right">
									<b><?php echo TEXT_TOTAL;?></b>
								</td>
								<td style="cursor:hand" align="right" class='dataTableContent'>
									<?php echo $currencies->format($total);
										$total=0;
									?>
								</td>
							</tr>
							<tr>
								<td colspan="6"><?php echo tep_draw_separator();?></td>
							</tr>
						<?php } // total >0 
						if ($grand_total>0) {
						?>
							<tr>
								<td class="dataTableContent" style="cursor:hand" colspan="5" align="right">
									<b><?php echo TEXT_GRAND_TOTAL;?></b>
								</td>
								<td style="cursor:hand" align="right" class='dataTableContent'>
									<?php echo $currencies->format($grand_total);
									?>
								</td>
							</tr>
						<?php } // grand total >0 ?>
				</table>
				</td>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif',1,5);?></td>
			</tr>
			<?php
					}
				if (!$summary_show) {
			?>
					<tr><td class="bottomLine">&nbsp;</td></tr>
					<tr>
						<td class="main" valign="top">
							<?php  echo '<input type="checkbox" name="chk_select" value="1" onClick="javascript:selectAll();">&nbsp;' . TEXT_SELECT_ALL . '&nbsp;&nbsp;'; ?>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif',1,5);?></td>
					</tr>
					<tr>
						<td class="main" valign="middle">
						<?php 
								echo tep_image_submit('button_change_status.gif',IMAGE_CHANGE_STATUS,'align=absmiddle') . '&nbsp;&nbsp;';
								echo tep_draw_pull_down_menu('new_status',$status_array);
						?>
						</td>
					</tr>
				<?php } ?>
				</form>	
			<?php
				}
				else{
						echo '<tr><td class="main" align="center">' . TEXT_NO_RECORDS_FOUND . '</td></tr>';
				}
			?>
	</table>
	</TD>
	</tr>
</table>
</td>
</tr>
</table>
	<!-- 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 $sql_result,$display_header,$db_result,$found_results,$currencies,$summary_show,$order_status_array,$report_filename;
		// set initial parameters
		$table=new pdfTable("A4","l");
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		// Add 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"]=16;
		$table->headers["width"]="100%";
		$table->headers["cols"]=4;
		
		// define column width
		$widths[0]=40;
		$widths[2]=80;
		$widths[3]=80;
		$widths[4]=80;
		$widths[1]=$table->width-($widths[0]+$widths[2]+$widths[3]+$widths[4]);

		// output header details
		//unset($cols);
		$i=1;
		$prev_customer="";
		$prev_item=0;
		$total=0;
		$grand_total=0;
			
		if ($found_results){
		$cols=array();
		$cols[]=array("text"=>NO,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Client,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Email,"width"=>$widths[2],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Status,"width"=>$widths[3],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Amount,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		$table->tableheaders["text"]=$cols;
		for ($icnt=0;$icnt<sizeof($db_result);$icnt++) {
			$sql_result=&$db_result[$icnt]["result"];
			mysql_data_seek($sql_result,0);
		//output content
		while($row = tep_db_fetch_array($sql_result)) {
		   $customer=$row["customers_id"];
		   $item=$row["item_id"];
			if ($prev_item!=$item) { // output the item name
				if ($subtotal>0){  // output the total amount
					$cols=array();
				
				$cols[]=array("text"=>$currencies->format($subtotal),"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($subtotal),"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,15);
				unset($cols);
				$subtotal=0;
				}
				$cols=array();
				$cols[]=array("text"=>$row["item_name"],"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4],"align"=>"L","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,15);
				unset($cols);
				$prev_item=$item;
					} // prev item 		
		
			$amount=tep_add_tax($row['final_price'],$row["products_tax"]);
			$amount=tep_get_rounded_amount($amount);
			//$order_status_array[$row["orders_status_id"]]=$row["orders_status_name"];
			
			if (!$summary_show) { 
				$cols[]=array("text"=>$i,"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"=>$email,"width"=>$widths[2],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$order_status_array[$row['orders_status']],"width"=>$widths[3],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($amount),"width"=>$widths[4],"align"=>"R","style"=>"row","valign"=>"M");
				$table->OutputRow($cols,15);
				unset($cols);
			}
			$i++;	
			$subtotal+=$amount;
			$sub_grand_total+=$amount;	
			$total+=$amount;
			$grand_total+=$amount;					
			}
		 }
			if($total>0){
				$cols=array();
				$cols[]=array("text"=>TEXT_TOTAL,"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($subtotal),"width"=>$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,15);
				unset($cols);
			}
			if($grand_total>0)	{
				$cols=array();
				$cols[]=array("text"=>TEXT_GRAND_TOTAL,"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$cols[]=array("text"=>$currencies->format($grand_total),"width"=>$widths[4],"align"=>"R","style"=>"subrowhead","valign"=>"M");
				$table->OutputRow($cols,15);
				unset($cols);
			}
		}else {
			$cols[]=array("text"=>TEXT_NO_RECORDS_FOUND,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
		}
		$table->Render($report_filename.".pdf");
		// create pdf page
	}
	
	// function to create excel content
	function generate_excel(){
		global $sql_result,$display_header,$db_result,$found_results,$currencies,$summary_show,$order_status_array,$report_filename;
		$sql_result="";
		if ($found_results){
		$sql_result.=No . "," . Client. "," . Email . "," . Status . "," . Amount . "\n";
		
		$i=1;
		$prev_customer="";
					$prev_item=0;
					$total=0;
					$grand_total=0;
		
		for ($icnt=0;$icnt<sizeof($db_result);$icnt++) {
			$sql_result=&$db_result[$icnt]["result"];
						

		
		mysql_data_seek($sql_result,0);
		//$prev_services_id=0;
		while($row = tep_db_fetch_array($sql_result)) {
			
		$customer=$row["customers_id"];
		$item=$row["item_id"];

		if ($prev_item!=$item) { // output the item name
			if ($subtotal>0) { // output the total amount
				$result.=sprintf(",,,%s,%s\n\n",TEXT_TOTAL,$subtotal);
				$subtotal=0;
				$sub_grand_total=0;
				}
				$result.=$row["item_name"] . "\n";
				$prev_item=$item;
		} // prev item 		
			$amount=tep_add_tax($row['final_price'],$row["products_tax"]);
			$amount=tep_get_rounded_amount($amount);
			$result.=sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n",$i,$row['customers_name'],$email,$status,$currencies->format($amount));
			$i++;	
			$subtotal+=$amount;
			$sub_grand_total+=$amount;	
			$total+=$amount;
			$grand_total+=$amount;					
		}

		
		}
		if($total>0){
			$result.=sprintf(",,,%s,%s\n\n",TEXT_TOTAL,$currencies->format($subtotal));
		}	
		
		if($grand_total>0)
		{
			$result.=sprintf(",,,%s,%s\n\n",TEXT_GRAND_TOTAL,$currencies->format($grand_total));
		}
		
		} else {
			$result.=TEXT_NO_RECORDS_FOUND . "\n";
		}
		tep_write_text_file($report_filename.".csv",$result);
	}



?>