<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/


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

  require('includes/application_top.php');
	if ((isset($HTTP_GET_VARS['joomla_include_yes']) && $HTTP_GET_VARS['joomla_include_yes']=='') && !tep_session_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 = isset($HTTP_GET_VARS['order_id'])?(int)$HTTP_GET_VARS['order_id']: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 ((isset($HTTP_GET_VARS['joomla_include_yes']) && $HTTP_GET_VARS['joomla_include_yes']=='') && $customer_number['customers_id'] != $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'];
	
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ORDERS_PRINTABLE);
	
	require(DIR_WS_CLASSES . 'order.php');
	$oID=tep_db_prepare_input($HTTP_GET_VARS['order_id']);
	$order = new order((int)$HTTP_GET_VARS['order_id']);
  
	$merge_details=array();
	$merge_details[TEXT_NO]=tep_db_prepare_input($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'];
	//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'];
	$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 . ' - ' . TITLE_PRINT_ORDER . ' #' . tep_output_string($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 border="0" width="100%" align="center" cellpadding="2" cellspacing="0">
	<tr> 
		<td valign="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" border="0" 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 '[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" class="main">
	<?php
		$products_ordered='<table border="0" 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, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
					$products_ordered_attributes .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $order->products[$i]['attributes'][$j]['option'] . ':' . $order->products[$i]['attributes'][$j]['value'];
					if($order->products[$i]['attributes'][$j]['price'])
					$products_ordered_attributes .= " - " . $currencies->format($order->products[$i]['attributes'][$j]['price']);
				}
			}
			
		    $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'];
			  $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 = 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;
		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='".(int)$HTTP_GET_VARS['order_id']."'");
						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>';
		  }
	  } 
						
	  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 cellpadding="0" border="0"><tr><td><table border=0 cellspacing=0 align="right" cellpadding="0" 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">' . strip_tags($order->totals[$i]['text']) . "</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>' . strip_tags($order->totals[$i]['text']) . "</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>';

	
    $merge_details[TEXT_PO]=$products_ordered;

	// get the shipping and billing address
	// get the shipping and billing address
	$merge_details[ORDR_BA]=tep_address_format($order->customer['format_id'], $order->customer, 1, '', '<br>');
	$merge_details[ORDR_SA]=tep_address_format($order->delivery['format_id'], $order->delivery, 1, '', '<br>');
	
	$payment_detail="";
	$note="";
	//$store_address=str_replace("\n",', ',STORE_NAME_ADDRESS);

/*	if ($products_type=='P' && $order->info['status_id']==3) {// product and delivered
		$payment_detail=PAYMT_PRODUCT_C;
	} else if ($products_type=='P' && $order->info['status_id']==2) {// product and processed
		$payment_detail=PAYMT_PRODUCT_R;
	} else if ($products_type=='P'&& $order->info['status_id']==1) {// product and pending
		$payment_detail=sprintf(PAYMT_PRODUCT_P,STORE_NAME,STORE_NAME_ADDRESS);
	} else if ($products_type=='E' && $order->info['status_id']==3) {// event and delivered
		$payment_detail=PAYMT_EVENT_C;
	} else if ($products_type=='E' && $order->info['status_id']==2) { // event and processing
		$payment_detail=sprintf(PAYMT_EVENT_P,STORE_NAME,STORE_NAME_ADDRESS,$pending_period);
		$note=PAYMT_EVENT_NOTE_P;
	} else if ($products_type=='M' && $order->info['status_id']==1) { // event and pending
		$payment_detail=sprintf(PAYMT_MIXED_P,STORE_NAME,STORE_NAME_ADDRESS,$pending_period);
		$note=PAYMT_EVENT_NOTE_P;
	}else if ($products_type=='S' && $order->info['status_id']==3) {// subscription and delivered
		$payment_detail=PAYMT_PRODUCT_C;
	} else if ($products_type=='S' && $order->info['status_id']==2) {// subscription and processed
		$payment_detail=PAYMT_PRODUCT_R;
	} else if ($products_type=='S'&& $order->info['status_id']==1) {// subscription and pending
		$payment_detail=sprintf(PAYMT_PRODUCT_P,STORE_NAME,STORE_NAME_ADDRESS);
	}*/
	$merge_details[PAYMT_DT]=$payment_detail;
	$merge_details[PAYMT_DT]='';
	$merge_details["Phone"]=$order->customer["telephone"];
	$merge_details["Email_Address"]=$order->customer["email_address"];
	//$merge_details["Store_Logo"]=tep_image(DIR_WS_IMAGES . "template_logo.png",STORE_NAME);
   // $merge_details["Store_Logo"]=tep_image(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/images/' .COMPANY_LOGO, STORE_NAME);
    $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;
	}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 require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>