<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	 
	
	Freeway eCommerce 
	http://www.openfreeway.org
	Copyright (c) 2007 ZacWare
	
	osConcert, Online Seat Booking 
  	http://www.osconcert.com

  	Copyright (c) 2009-2013 osConcert

	Released under the GNU General Public License
*/


// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	

  require('includes/application_top.php');
  require('tfpdf/tfpdf.php');
  ob_start();
	if (!$FSESSION->is_registered('customer_id')) {
		$navigation->set_snapshot();
		tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
	}
	$dis_time_format="";
	if(defined('TIME_FORMAT')) {
		$dis_time_format=TIME_FORMAT;
	}
	$order_id = $FREQUEST->getvalue('order_id','int',0);
	if(!is_numeric($order_id) || $order_id == 0){
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL'));
	}
	$customer_number_query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id = '". tep_db_input(tep_db_prepare_input($order_id)) . "'");
	$customer_number = tep_db_fetch_array($customer_number_query);

	if (!$FSESSION->get('customer_id')) {
		tep_redirect(tep_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL'));
	}
  
	$payment_info_query = tep_db_query("select payment_info from " . TABLE_ORDERS . " where orders_id = '". tep_db_input(tep_db_prepare_input($order_id)) . "'");
	$payment_info = tep_db_fetch_array($payment_info_query);
	$payment_info = $payment_info['payment_info'];
	
	// get the comments
		$fields_query=tep_db_query("SELECT comments,field_1,field_2,field_3,field_4,other from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id='" . (int)$order_id . "' order by date_added desc limit 1");
		$fields="";
		$fields_result=tep_db_fetch_array($fields_query);
		$comments=$fields_result["comments"];
		$field_1=$fields_result["field_1"];
		$field_2=$fields_result["field_2"];
		$field_3=$fields_result["field_3"];
		$field_4=$fields_result["field_4"];
		$other=$fields_result["other"];
		
		//print_r ($fields_result);
	
	require(DIR_WS_LANGUAGES . $FSESSION->language . '/' . FILENAME_ORDERS_PRINTABLE);
	require(DIR_WS_LANGUAGES . $FSESSION->language . '/modules/payment/bank_transfer.php');
	
	require(DIR_WS_CLASSES . 'order.php');
	$oID=tep_db_prepare_input($FREQUEST->getvalue('order_id'));
	$order = new order($FREQUEST->getvalue('order_id','int'));
  
	$merge_details=array();
	//$merge_details[TEXT_NO]='MPMB'.$oID.'/2013';
	$merge_details[TEXT_NO]=$oID;
	$merge_details[TEXT_OL]='<a href="' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $oID, 'SSL', false) . '">' . TEST_MAIL_IL . '</a>';
	//$merge_details[TEXT_OP]=$order->info['date_purchased'];
        //date_default_timezone_set('Europe/Berlin'); 
        // Set the gloabal LC_TIME constant to german
        //setlocale(LC_ALL, "de_DE", "de_DE.UTF-8", "deu", "deu_deu", "german");
	$merge_details[TEXT_OP]=utf8_encode(strftime('%A, %d. %B %Y', strtotime($order->info['date_purchased'])));
	//strftime(DATE_FORMAT_LONG);
	$merge_details[TEXT_OM]=tep_output_string($order->info['comments']);
	$merge_details[TEXT_FN]=$order->customer['firstname'];
	$merge_details[TEXT_LN]=$order->customer['lastname'];
	$merge_details[TEXT_PM]= $order->info['payment_method'];
	
	//add shipping placeholder
	define("ORDR_SM","Shipping_Method");
	$shipping_method=$order->info['shipping_method'];
	$merge_details[ORDR_SM]=$shipping_method;
	
	$merge_details[LOG_IP]= $order->info['ip_address'];
	$merge_details[TEXT_SM] =STORE_NAME;
	$merge_details[STORE_AD] =str_replace("\n","<br>",STORE_NAME_ADDRESS);
	
	//print_r ($merge_details);

?>
<!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_NEW . ' - ' . TITLE_PRINT_ORDER . ' #' . $order_id; ?></title>
<base href="<?php echo (getenv('HTTPS') == 'on' ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="print.css">
</head>
<body marginwidth="10" marginheight="10" topmargin="10" bottommargin="10" leftmargin="10" rightmargin="10">


<!-- body_text //-->
<table width="100%" align="center" cellpadding="2">
	<tr> 
		<td style="vertical-align: text-top;" align="left" class="main">
		<script language="JavaScript">
			if (window.print) {
			document.write('<a href="javascript:;" onClick="javascript:window.print()" onMouseOut=document.imprim.src="<?php echo (DIR_WS_IMAGES . 'printimage.gif'); ?>" onMouseOver=document.imprim.src="<?php echo (DIR_WS_IMAGES . 'printimage_over.gif'); ?>"><img src="<?php echo (DIR_WS_IMAGES . 'printimage.gif'); ?>" width="43" height="28" align="absbottom" name="imprim">' + '<?php echo IMAGE_BUTTON_PRINT; ?></a></center>');
			}
			else document.write ('<h2><?php echo IMAGE_BUTTON_PRINT; ?></h2>')
			</script>
		</td>
		<td align="right" valign="bottom" class="main"><p align="right" class="main"><a href="javascript:window.close();"><?php echo '[' . IMAGE_BUTTON_CLOSE . ']';?></a></p></td>
	</tr>
  <tr>
  	<td colspan="2">
		<?php echo tep_draw_separator('pixel_trans.gif',1,10);?>
	</td>
  </tr>
  <tr>
  	<td colspan="2" style="vertical-align: text-top;" class="main">
  <?php
		$products_ordered='<table cellspacing=0 cellpadding=0 width="100%">';
		$products_ordered.='<tr><td><table border=0 cellspacing=0 cellpadding=0 width="100%">';
		$products_ordered.="<tr height='20'>";
		//include headings
		/*$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold' width='50'>" . TABLE_HEADING_QTY;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>" . TABLE_HEADING_PRODUCTS;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>SKU";
		$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" align="right" width="100">' . TABLE_HEADING_TOTAL_INCLUDING_TAX;*/
		$product_type="";
		//print_r($order->products);
		$products_name="";
		$products = "";
		$products_names_p="";
		$pre_qty="";
		$pre_name="";
		$qty_p = "";
		$pre_sign="";
		$first_event = true;
		$first_event_id = 0;
		$jj=0;
		$attendees_name="";

		for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
			$products_names_p="";
			$qty_p = "";
			$sign_p="";
			$model="";
			$products_ordered_attributes="";
			if (isset($order->products[$i]['attributes']) && (sizeof($order->products[$i]['attributes']) > 0)) {
				for ($j = 0, $k = sizeof($order->products[$i]['attributes']); $j < $k; $j++) {
					if($order->products[$i]['attributes'][$j]['option'] && $order->products[$i]['attributes'][$j]['value'])
						$products_ordered_attributes .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;- " .  $order->products[$i]['attributes'][$j]['option'] . ': &nbsp;' . $order->products[$i]['attributes'][$j]['value'];
					if ($order->products[$i]['attributes'][$j]['price'] != '0') 
						$products_ordered_attributes .= ' (' . $order->products[$i]['attributes'][$j]['prefix'] . $currencies->format($order->products[$i]['attributes'][$j]['price'] * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']) . ')';
				}
			}	 
		    $products_ordered_attributes1="";
			for($jcnt=0;$jcnt<sizeof($order->products[$i]['resources']);$jcnt++){
			   if(count($order->products[$i]['resources'])>0){
				$disp_date=$order->products[$i]['resources'][$jcnt]['end_date'];
				$disp_time=$order->products[$i]['resources'][$jcnt]['end_time'];
				
				if($disp_date && $disp_time){
					$date_time=strtotime($disp_date.' '.$disp_time)+60;
					$disp_date=date('Y-m-d',$date_time); 
					$disp_time=date('h:i A',$date_time); 
				}
				$disp_stime=date('h:i A',strtotime($order->products[$i]['resources'][$jcnt]['start_time']));
				if($dis_time_format!="") {
					if($dis_time_format=='24') {
						$disp_time=date('H:i',$date_time); 
						$disp_stime=date('H:i',strtotime($order->products[$i]['resources'][$jcnt]['start_time']));
					}	
				}
					if($order->products[$i]['resources'][$jcnt]['start_date'])
						$products_ordered_attributes1 .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;- " . TEXT_START_DATE . ': &nbsp;&nbsp;' .  format_date($order->products[$i]['resources'][$jcnt]['start_date']) . '&nbsp;&nbsp;' . $disp_stime;
					if($order->products[$i]['resources'][$jcnt]['end_date'])
						$products_ordered_attributes1 .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;- " . TEXT_END_DATE . ': &nbsp;&nbsp;&nbsp;' .  format_date($disp_date) . '&nbsp;&nbsp;' . $disp_time;
					if($order->products[$i]['resources'][$jcnt]['resource_name'])
						$products_ordered_attributes1 .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;- " . TEXT_RESOURCE . ': &nbsp;&nbsp;' . $order->products[$i]['resources'][$jcnt]['resource_name'];
			  }
			}
			
			if($order->products[$i]['element_type']=="E"){
			 $model="";
				if($order->products[$i]['model'])
				{
				  if($order->products[$i]['model']!=""){
					 $model.=$order->products[$i]['model'] . ",";
				  }
				 
				}
			}	
			if(($pre_name!=$order->products[$i]['name']) && ($order->products[$i]['element_type']=="E")){
					$products_names_p = $order->products[$i]['name'] . "<br>" . tep_draw_separator('pixel_trans.gif','20','10');
					$pre_name=$order->products[$i]['name'];	
					$qty_p=$order->products[$i]['qty'];
					$pre_qty=$order->products[$i]['qty']; 
					$sign_p="X";
					$pre_sign="X";
			 }else if($order->products[$i]['element_type']=="V"){
			  $products_names_p = $order->products[$i]['name'];
			  $qty_p=$order->products[$i]['qty'];
			  $sign_p="X";
				  if($order->products[$i]['model'])
					$model= " , " . $order->products[$i]['model'];
			  }else if($order->products[$i]['element_type']=="S"){ 
			  $products_names_p = $order->products[$i]['name'];
			  $qty_p=$order->products[$i]['qty'];  
			  $sign_p="X"; 
				  //if($order->products[$i]['model'])
					//$model= " , " . $order->products[$i]['model'];
			  }else if($order->products[$i]['element_type']=="P"){ 
			  $products_names_p = $order->products[$i]['name'];
			  $comments = $order->products[$i]['comments'];
			  $qty_p=$order->products[$i]['qty'];
			  $sign_p="X";
				  if($order->products[$i]['model'])
					$model= " " . $order->products[$i]['model'];
			  }
			
			$rax_total=tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']) * $order->products[$i]['qty'];
			$sku = $order->products[$i]['sku'];
			$event_id = 0;
			if($order->products[$i]['element_type']=='E'){
				$event_id = $order->products[$i]['event_id'];
				if($first_event){ 
					$first_event_id = $order->products[$i]['event_id'];
					$first_event = false;
				}
			} 
			$products[] = array( "qty" => $qty_p,
								 "sign"  => $sign_p,
								 "id" => $order->products[$i]['id'],
								 "products_type" => $order->products[$i]['element_type'],
								 "p_name"	 => $products_names_p,
								 "event_id" => $event_id,
								 "p_attributes1" => $products_ordered_attributes1, 
								 "p_attributes" => $products_ordered_attributes,
								 "model" => $model,
								 "sku" => $sku,
								 "row_total"=>$rax_total
							   ); 
	     }
		$session_dates = "";
		$event_id = $first_event_id;
		$date = array(); 
		
		$events_row_total = array();
		for ($i = 0, $n = sizeof($products); $i < $n; $i++) { 
			if(($products[$i]['products_type']=="E")){
				if($event_id!=$products[$i]['event_id']){
					$event_id=$products[$i]['event_id'];
					$session_dates = "";
				}
				$session_dates .= $products[$i]['model'];
				$date[$products[$i]['event_id']] = substr($session_dates,0,-1); 
				$events_row_total[$products[$i]['event_id']] += $products[$i]['row_total'];	
			} 
		} 
		$date_keys = array_keys($date);
		$date_keys_length = count($date_keys);
		for($i=0;$i<$date_keys_length;$i++){
			$final_date='';
			$var_date = $date[$date_keys[$i]];
			$date_split = preg_split('/,/',$var_date);
			$date_split_count = count($date_split);
			
			for($j=0;$j<$date_split_count;$j++){
				$final_date .= format_date($date_split[$j]) .',';
				$m=$j+1;
				//if($m%4==0)	$final_date = substr($final_date,0,-1). ',' . '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			   if($m%4==0)	$final_date = substr($final_date,0,-1) . '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$date[$date_keys[$i]] = substr($final_date,0,-1);
		}
		$event_id = 0;$flagp=0;$flage=0;$flagv=0;$flags=0;
		for ($i = 0, $n = sizeof($products); $i < $n; $i++) {
	   		 if($products[$i]['products_type']=="P"){
				$row_total= $products[$i]['row_total'];			
				$date=$products[$i]['model'];
			}else if($products[$i]['products_type']=="S"){
			    $row_total= $products[$i]['row_total'];
				   if($products[$i]['model']!="")
				    $date=$products[$i]['model'];	
			}else if($products[$i]['products_type']=="V"){
			    $row_total= $products[$i]['row_total'];
				   if($products[$i]['model']!="")
				   $date=$products[$i]['model'];
			}
			if($event_id!=$products[$i]['event_id'] || $products[$i]['products_type']!='E'){
				if($products[$i]['products_type']=='E'){
					$row_total = $events_row_total[$products[$i]['event_id']];
					$attendees_name="";
					if( ($products[$i]['qty'])>1 ){
						
						$attendee_query=tep_db_query("select distinct customers_firstname,customers_lastname from ".TABLE_CUSTOMERS ." c, ".TABLE_CUSTOMERS_TO_CUSTOMERS ." ctc where ctc.customer_id=c.customers_id and ctc.orders_id='".tep_db_input($FREQUEST->getvalue('order_id','int'))."'");
						while($attendee_result=tep_db_fetch_array($attendee_query)){
							$attendees_name.=$attendee_result['customers_firstname'].' '.$attendee_result['customers_lastname'].',';
						}
				   }
				} 
			$model = "";
			if(($products[$i]['products_type']=="S")){
			   if($products[$i]['model']!="")
			     $model = $products[$i]['model'];
				 $attendees_name="";
		    }if(($products[$i]['products_type']=="P")){
			     if($products[$i]['model']!="")
			       $model = $products[$i]['model'];
				   $attendees_name="";	 
			}
		    if( ($products[$i]['products_type']=="E") && ($products[$i]['qty'])>1 ) {
						if ($attendees_name!=""){
								//$start_name=strpos($attendees_name,',',1);
								//$attendees_name=substr($attendees_name,$start_name+1,-1);
								$attendees_name='<br>' .'<small>' . '<b>' . TEXT_ATTENDEES . '</b>' . '</small>' . '<br>'.tep_draw_separator('pixel_trans.gif',35,1) .'-'.substr($attendees_name,0,-1);
						}
			}
			$discount='';
			/*
			$discount=(isset($order->products[$i]['discount_whole_text']) && trim($order->products[$i]['discount_whole_text'])!='')?'<br>' . $order->products[$i]['discount_whole_text']:'';
			$event_id = $products[$i]['event_id'];

			$products_ordered.='<tr height="20">';
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" valign="top" align="left">' . "&nbsp;" . $products[$i]['qty'] . "&nbsp;&nbsp;" . $products[$i]['sign'] .'&nbsp;</td>';
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" valign="top" align="left">' . $products[$i]['p_name'] .  $products[$i]['p_attributes1'] . $products[$i]['p_attributes'] . $date[$event_id]  . $model . $attendees_name . $discount . "</td>";
			$products_ordered.='<td class="dataTableContent" valign="top" width="20">' . $products[$i]['sku'] . '</td>';
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" align="right" valign="top">' . $currencies->format($row_total, true, $order->info['currency'], $order->info['currency_value']) . '</td>';
			$products_ordered.='</tr>';
		  }
	  } */
			
			if(($products[$i]['products_type']=='P')){
				$discount=(isset($order->products[$i]['discount_whole_text']) && $order->products[$i]['discount_whole_text']!='')?'<br>' . $order->products[$i]['discount_whole_text']:'';
			}
			
			$event_id = $products[$i]['event_id'];
			$products_ordered.='<tr height="20"  style="background:#ECE9D8";>';
			if(($products[$i]['products_type']=='P')&&($flagp==0)){
				$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold' width='50'>" . TABLE_HEADING_QTY;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>" . TABLE_HEADING_PRODUCTS."</td>";
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'></td>";
		$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" align="right" width="100">' . TABLE_HEADING_TOTAL_INCLUDING_TAX."</td></tr>";
		     $flagp=1;
			}
			if(($products[$i]['products_type']=='E')&&($flage==0)){
				$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold' width='50'>" . TABLE_HEADING_QTY;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>" . TABLE_HEADING_EVENTS."</td>";
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'> </td>";
		$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" align="right" width="100">' . TABLE_HEADING_TOTAL_INCLUDING_TAX."</td></tr>";
		     $flage=1;
			}
			if(($products[$i]['products_type']=='V')&&($flagv==0)){
				$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold' width='50'>" . TABLE_HEADING_QTY;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>" . TABLE_HEADING_SERVICES."</td>";
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'></td>";
		$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" align="right" width="100">' . TABLE_HEADING_TOTAL_INCLUDING_TAX."</td></tr>";
		     $flagv=1;
			}
			if(($products[$i]['products_type']=='S')&&($flags==0)){
				$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold' width='50'>" . TABLE_HEADING_QTY;
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'>" . TABLE_HEADING_SUBSCRIPTIONS."</td>";
		$products_ordered.="<td style='font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold'></td>";
		$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" align="right" width="100">' . TABLE_HEADING_TOTAL_INCLUDING_TAX."</td></tr>";
		     $flags=1;
			}
			$products_ordered.='<tr height="20" >';
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px; vertical-align: text-top;" align="left">' . "&nbsp;" . $products[$i]['qty'] . "&nbsp;&nbsp;" . $products[$i]['sign'] .'&nbsp;</td>';
			if(($products[$i]['products_type']!='P')){
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" style="vertical-align: text-top;" align="left">' . $products[$i]['p_name'] .  $products[$i]['p_attributes1'] . $products[$i]['p_attributes'] . $date[$event_id]  . $model . $attendees_name .   "</td>";}
			else {
			$heading_name = $order->products[$i]['categories_name'];
			//$categories_description = $order->products[$i]['categories_description'];
			//$categories_heading_title = $order->products[$i]['categories_heading_title'];
			$heading_venue = $order->products[$i]['concert_venue'];
			$heading_date = $order->products[$i]['concert_date'];
			$heading_time = $order->products[$i]['concert_time'];
			// products listed here
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px; vertical-align: text-top;" align="left">' . $products[$i]['p_name'] .  $products[$i]['p_attributes1'] . $products[$i]['p_attributes']. ' ['. $model . ']  ' . $discount .  ' ' .$heading_name . '   ' . $heading_venue . '  ' . $heading_date . '  ' . $heading_time . "</td>";
			}
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" valign="top" width="20"></td>';//' . $products[$i]['sku'] . '
			$products_ordered.='<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" align="right" valign="top">' . mb_convert_encoding($currencies->format($row_total, true, $order->info['currency'], $order->info['currency_value']), "HTML-ENTITIES", 'UTF-8') . '</td>';
			$products_ordered.='</tr>';
			
		  }
	  } 
			
						
	  for ($i = 0, $n = sizeof($products); $i < $n; $i++) {  
			if ($products_type!="M"){
				if ($products_type=='')
					$products_type=$order->products[$i]['products_type'];
				else if ($products_type!=$order->products[$i]['products_type'])
					$products_type="M";
			}
			if ($order->products[$i]['products_type']=='E') $session_ids.=$order->products[$i]['id'] . ",";
	   }
	$products_ordered .='</table></td></tr><tr height="10"><td></td></tr><tr><td><table width="100%" cellspacing=0><tr><td><table border=0 cellspacing=0 align="right" width="100%">';


	$pending_period=0;
	$fetch_id=0;

	if ($session_ids!=""){
		$session_ids=substr($session_ids,0,-1);
		$find_query=tep_db_query("SELECT e.events_pending_period as pending_period,e.events_id from " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e where sessions_id='" . tep_db_input($session_ids) . "' and s.events_id=e.events_id order by events_pending_period limit 1");
		$find_result=tep_db_fetch_array($find_query);
		$pending_period=(int)$find_result["pending_period"];
		$fetch_id=(int)$find_result["events_id"];
	}

	$order_totals_str="";
	$titles=array('ot_subtotal'=>INVOICE_SUBTOTAL,'ot_shipping'=>INVOICE_SHIPPING,'ot_total'=>INVOICE_TOTAL);
	
	for ($i = 0, $n = sizeof($order->totals); $i < $n; $i++) {
		if ($order->totals[$i]['class']!="ot_total"){
			$order_totals_str .= '<tr height="20"><td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" align="right">' . (isset($titles[$order->totals[$i]['class']])?$titles[$order->totals[$i]['class']]:strip_tags($order->totals[$i]['title'])) . '<td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;" width="80" align="right">' . mb_convert_encoding(str_replace("€","â‚¬",strip_tags($order->totals[$i]['text'])), "HTML-ENTITIES", 'UTF-8') . "</td><td width='2'></td></tr>"; 
		} else {
			$order_totals_str .= '<tr height="20" style="background:#FF7300;"><td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FFFFFF;" align="right"><b>' . (isset($titles[$order->totals[$i]['class']])?$titles[$order->totals[$i]['class']]:strip_tags($order->totals[$i]['title'])) . '</b><td style="font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FFFFFF;" align="right"><b>' . mb_convert_encoding(str_replace("€","â‚¬", strip_tags($order->totals[$i]['text'])), "HTML-ENTITIES", 'UTF-8') . "</b></td><td width='2'></td></tr>"; 
		}
		//$order_totals_str.="<tr height='3'><td></tr>";
	}
	$products_ordered.=$order_totals_str .'</table></td></tr></table></td></tr></table>';

	//here
    $merge_details[TEXT_PO]=$products_ordered;
	
	// get the shipping and billing address
	$merge_details[ORDR_BA]=tep_address_format($order->billing['format_id'], $order->billing, 1, '', '<br>');
		
	if(($order->delivery['format_id'])>0)
		$merge_details[ORDR_SA]=tep_address_format($order->delivery['format_id'], $order->delivery, 1, '', '<br>');
	else 	
		$merge_details[ORDR_SA]=tep_address_format($order->customer['format_id'], $order->customer, 1, '', '<br>');
	
	$payment_detail="";
	$note="";
	
	$merge_details[PAYMT_DT]=$payment_detail;
	
	 if (EXTRA_FIELDS == 'yes'){
	 
	//add extra fields (may 2012)
	$extra_fields_str = '<span class="smallText"><b><i>' . NEW_FIELDS_HEADING . '</i></b>' . NOTE . '</span><table width="80%" cellpadding="5"><tr><td class="main" width="15%">' .FIELD_1. '&nbsp;</td><td class="main">' .$field_1. '</td></tr><tr><td class="main">' .FIELD_2. '&nbsp;</td><td class="main">' .$field_2. '</td></tr><tr><td class="main">' .FIELD_3. '&nbsp;</td><td class="main">' .$field_3. '</td></tr><tr><td class="main">' .FIELD_4. '&nbsp;</td><td class="main">' .$field_4. '</td></tr><tr><td class="main">' .FIELD_5. '&nbsp;</td><td class="main">' .$other. '</td></tr></table>';
	$merge_details[TEXT_XF]=$extra_fields_str;
	}
	//eof
	
	$merge_details[PAYMT_DT]='';
	$merge_details["Phone"]=$order->customer["telephone"];
	$merge_details["Email_Address"]=$order->customer["email_address"];
	
	//Bank Transfer Payment - bank_transfer.php
	//Bank Deposit Message as place holder %%Bank_Deposit_Message%%
	define("ORDR_BDM","Bank_Deposit_Message");
	if($order->info['payment_method']=='Bank Transfer Payment'){
	
	$merge_details[ORDR_BDM]=MODULE_BANK_TRANSFER_INFO;
	}else{
	$merge_details[ORDR_BDM]="";
	}

    $merge_details["Store_Logo"]=tep_image(DIR_WS_TEMPLATES . DEFAULT_TEMPLATE . '/images/' .COMPANY_LOGO, STORE_NAME,'','');
	$mail_data_query=tep_db_query("SELECT * from " . TABLE_EMAIL_MESSAGES . " where message_type='MIV'");

	if (tep_db_num_rows($mail_data_query)>0){
		
		$mail_data_result=tep_db_fetch_array($mail_data_query);
		
		//get message content
		$mes_content=$mail_data_result['message_text'];
	   //replace the merge details with the fields in content
	   while (list($key, $value) = each($merge_details))
		  $mes_content=str_replace("%%" . $key  . "%%",$value,$mes_content);
		  
	   //$mes_content=str_replace("\n","<br>",$mes_content);
	  // echo $mes_content;
	  // echo '<span class="smallText"><b><i>' . NEW_FIELDS_HEADING . '</i></b>(agents note)</span>';
	}else {
		echo TEXT_NO_CONTENT;
	}
	if ($note!=""){
		echo '<span class="smallText"><b><i>' . TEXT_NOTE . '</i></b>' . $note . '</span>';
	}
?>	
  </tr>
</table>
<!-- body_text_eof //-->
</body>
</html>
<?php 
ob_clean();
 $pdfcontentnew = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <body>';
           $pdfcontentnew .= $mes_content;
           
           $pdfcontentnew .= "</body>";
           $pdfcontentnew .= "</html>";
           $mes_content_1 = stripslashes($pdfcontentnew);
           $filename = "order_".$_REQUEST['oID']."-".date('Y-M-d his').".pdf";
	    $tfpdf=new tfPDF('win-1252','A4','','',0,0,0,0,10,0);//A4 page in portrait for landscape add -L.
            //$tfpdf->SetHeader('|Voucher|');
            //$tfpdf->setFooter('{PAGENO}');// Giving page number to your footer.
            $tfpdf->useOnlyCoreFonts = true;    // false is default
            $tfpdf->showImageErrors = true;
            $tfpdf->SetDisplayMode('fullpage');			

            $tfpdf->WriteHTML(utf8_encode($mes_content_1));
            //$tfpdf->Debug();
            $content = $tfpdf->Output('', 'S');
            //$storepdf = $tfpdf->Output($filepath.$filename,'F');
            $content = chunk_split(base64_encode($content));
            $op_pdf = $tfpdf->Output($filename,'D');			

            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment;filename="'.$filename.'"');
            header('Cache-Control: max-age=0');
            exit(0);
?>
<?php //require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>