<?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');
	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'];
	$merge_details[TEXT_OP]=date(DATE_FORMAT,strtotime($order->info['date_purchased']));
	//$merge_details[TEXT_OP]=utf8_encode(strftime('%A %B %d, %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'];
	$merge_details[ORDR_OM].=(($comments)?TEXT_ORDER_COMMENTS .' '. $comments:'');
	
	//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">
<link rel="stylesheet" type="text/css" media="all" href="<?php echo DIR_WS_TEMPLATES.TEMPLATE_NAME.'/css/bootstrap.css';?>" />
<link rel="stylesheet" type="text/css" media="all" href="<?php echo DIR_WS_TEMPLATES.TEMPLATE_NAME.'/css/bootstrap.min.css';?>" />
<link rel="stylesheet" type="text/css" media="all" href="<?php //echo DIR_WS_TEMPLATES.TEMPLATE_NAME.'/css/themes/'.TEMPLATE_COLOR.'.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="bottom" align="left" class="main" width="30%">
		<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 valign="bottom" align="center" class="main" colspan="3" width="40%">
        <!--<a href="customers_invoice_pdf.php?oID=<?php //echo $_REQUEST['oID']; ?>" target="_blank"><?php //echo 'Export To PDF';?></a>-->
		<a href="pdforder.php?order_id=<?php echo $_REQUEST['order_id']; ?>" target="_blank">
		<?php //echo tep_template_image_button_basic('pdf_invoice.gif', IMAGE_PDF_INVOICE) . '</a>'; ?>
		</td>
		<td align="right" valign="bottom" class="main" width="30%"><p align="right" class="main"><a href="javascript:window.close();"><?php echo '[Close]';?></a></p></td>
	</tr>
</table>
	</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="";
			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']=="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'];
			$event_id = 0;

			$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,
								 "model" => $model,
								 "row_total"=>$rax_total
							   ); 
	     }
		$date = array(); 
		
		$events_row_total = array();
		for ($i = 0, $n = sizeof($products); $i < $n; $i++) { 
 
		} 
		$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;&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'];
			}

			$model = "";

			if(($products[$i]['products_type']=="P")){
			     if($products[$i]['model']!="")
			       $model = $products[$i]['model']; 
			}

			$discount='';

			
			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;
			}


			$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>';
			
			$heading_name = $order->products[$i]['categories_name'];
			$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'] . $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_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";
			}

	   }
	$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;

	$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">' . str_replace("€","â‚¬",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>' . str_replace("€","â‚¬", 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>';

	//here
    $merge_details[TEXT_PO]=$products_ordered;
	
	// get the shipping and billing address
	$merge_details[ORDR_BA]=tep_address_format($order->customer['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]="";
	}
	$customers_language=$order->customer['language'];
	include(DIR_WS_LANGUAGES . $customers_language . '/templates.php');
	
	$merge_details[TEXT_INV_ON]=TEXT_MAIL_ORDER_NUMBER;//Order Number
	$merge_details[TEXT_INV_DEAR]=TEXT_DEAR;// Dear
	$merge_details[TEXT_INV_THANKS_PURCHASE]=TEXT_THANKS_PURCHASE;//Thanks for...
	$merge_details[TEXT_INV_DD]=TEXT_DELIVERY_DETAILS;//Delivery Details
	$merge_details[TEXT_INV_ADDRESS]=TEXT_ADDRESS;
	$merge_details[TEXT_INV_TELEPHONE]=TEXT_TELEPHONE;
	$merge_details[TEXT_INV_EMAIL]=TEXT_EMAIL;
	$merge_details[TEXT_INV_PD]=TEXT_PAYMENT_DETAILS;
	$merge_details[TEXT_INV_PM]=TEXT_PAYMENT_METHOD;
	$merge_details[TEXT_INV_PRODUCTS]=TEXT_TICKETS;
	$merge_details[TEXT_INV_WITH_THANKS]=TEXT_WITH_THANKS;
	$merge_details[TEXT_INV_THANKS]=TEXT_THANKS_FOR;
	//$merge_details['Text_Regards']=TEXT_REGARDS;
    $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 require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>