<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
	define('FPDF_FONTPATH','font/');
	require('includes/fpdf.php');
	tep_set_time_limit(100);
	$order_id=isset($HTTP_GET_VARS["oID"])?$HTTP_GET_VARS["oID"]:0;
	$found_results=false;
	$unit=72/25.4;
	$order_ids='0';
	if ($order_id!=""){
		$order_id_splt=split(",",$order_id);
		$order_id_splt=array_unique($order_id_splt);
		$order_ids="'" .join("','",$order_id_splt) . "'";
	}
		
	// get the template details if not found use default template
	$template_query=tep_db_query("SELECT template_width,template_height,template_content from " . TABLE_GENERAL_TEMPLATES . " where template_type='TIC'");
	
	if (tep_db_num_rows($template_query)>0) {
		$template_result=tep_db_fetch_array($template_query);
	} else {
		$template_result=getDefaultTemplate();
	}

	$template_splt=split("{}",$template_result["template_content"]);
	for ($icnt=0;$icnt<count($template_splt);$icnt=$icnt+2){
		$key=$template_splt[$icnt];
		$template[$key]=$template_splt[$icnt+1];
	}

	$ticket_width=$template_result["template_width"]*10;
	$ticket_height=$template_result["template_height"]*10;
	
	$tInfo=new objectInfo($template);
	$tInfo->shop_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->shop_logo_image;
	$tInfo->oscomm_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->oscomm_logo_image;

	// set page setup default width and height
	$pdf=new FPDF("P","mm",array($ticket_width,$ticket_height));

	$pos_left=5;
	$pos_top=5;
	$pos_width=$ticket_width;
	$pos_height=$ticket_height;
	// create the content and positions to be drawn 
	$content=array();
	
	// get the ticket details to be shown
	$order_sql="SELECT distinct c.customers_id,s.start_date,s.events_id,o.orders_id,c.customers_firstname,c.customers_lastname from " . 
				TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_CUSTOMERS . " c, " . TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c ".
				" where o.customers_id=c2c.group_customer_id and c2c.customer_id=c.customers_id and c2c.orders_id=o.orders_id and o.orders_id in(" . $order_ids . ") and op.products_type='E' and op.events_type='R' and o.orders_id=op.orders_id and op.products_id=s.sessions_id" .
				" order by c.customers_id,o.orders_id,op.products_name,s.start_date";
	
	$order_query=tep_db_query($order_sql);
		
	if (!(tep_db_num_rows($order_query))){ 
	
	$order_sql="SELECT s.start_date,s.events_id,o.orders_id,o.customers_id,c.customers_firstname,c.customers_lastname from " . 
				TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_CUSTOMERS . " c " .
				" where o.orders_id in(" . $order_ids . ") and op.products_type='E' and op.events_type='R' and o.orders_id=op.orders_id and op.products_id=s.sessions_id and o.customers_id=c.customers_id " .
				" order by o.orders_id,op.products_name,s.start_date";

	$order_query=tep_db_query($order_sql);
	
	}if (tep_db_num_rows($order_query)) $found_results=true;
	
	// get info of image
	if (file_exists($tInfo->shop_logo_image)){
		$info_shop=getimagesize($tInfo->shop_logo_image);
		$info_shop[0]=($info_shop[0]/$unit)*74/100;
		$info_shop[1]=($info_shop[1]/$unit)*74/100;
	}
	if (file_exists($tInfo->oscomm_logo_image)){
		$info_oscomm=getimagesize($tInfo->oscomm_logo_image);
		$info_oscomm[0]=($info_oscomm[0]/$unit)*74/100;
		$info_oscomm[1]=($info_oscomm[1]/$unit)*74/100;
	}
	$details=array();
	$tickets=array();
	$max_date=array();
	$cnt=1;
	$row=0;
	$prev_order=0;
	$prev_event=0;
	$session_dates = "";
	$pre_cus_id=0;
	$date = array();
	
	while($order_result=tep_db_fetch_array($order_query)){
		$events_id=$order_result["events_id"];
		$orders_id=$order_result["orders_id"];
		if ($prev_order!=$orders_id){
			$cnt=1;
			if ($prev_order>0) $row=$row+1;
			$prev_order=$orders_id;
			$prev_event=0;
		}
		if($pre_cus_id!=$order_result["customers_id"]){
		}
		if ($prev_event!=$events_id){
			if (!isset($details[$events_id])){
				$event_query=tep_db_query("SELECT l.location_name,ed.events_name,time_format(e.events_start_time,'%h:%i %p') as events_start_time,time_format(e.events_end_time,'%h:%i %p') as events_end_time from " . 
											TABLE_EVENTS . " e left join " . TABLE_EVENTS_DESCRIPTION . " ed on e.events_id=ed.events_id left join " . 
											TABLE_EVENTS_LOCATIONS . " l on e.events_location=l.location_id where e.events_id='" . $events_id . "' and ed.language_id='" . $languages_id . "'"
										);
				$event_result=tep_db_fetch_array($event_query);
				$details[$events_id][TEXT_EL]=$event_result["location_name"];
				$details[$events_id][TEXT_EN]=$event_result["events_name"];
				$details[$events_id][TEXT_SI]=$event_result["events_start_time"];
				$details[$events_id][TEXT_EI]=$event_result["events_end_time"];
			}
			$cnt=1;
			if ($prev_event>0) $row=$row+1;
			$prev_event=$events_id;
		}
		if($pre_cus_id!=$order_result["customers_id"]){
		   $cnt = 1;
		   $row++;
		   $pre_cus_id=$order_result["customers_id"];
		   $tickets[$row]['dates'][]=format_date($order_result["start_date"]);
		}else{
		   $tickets[$row]['dates'][]=format_date($order_result["start_date"]);
	   }
		if ($cnt==1) {
			$tickets[$row]["events_id"]=$events_id;
			$tickets[$row]["customers_id"]=$order_result["customers_id"];
			$tickets[$row]["orders_id"]=$orders_id;
			$tickets[$row]["customers_firstname"]=$order_result["customers_firstname"];
			$tickets[$row]["customers_lastname"]=$order_result["customers_lastname"];
		}	
		$cnt=$cnt+1;
		if ($cnt>9){
			$row=$row+1;
			$cnt=1;
		}		
	}

	if ($found_results){

		require("barcode/image.php");
		$bar_width=15;
		
		// print the ticket details
		$tickets = array_values($tickets);
		for ($tcnt=0;$tcnt<count($tickets);$tcnt++){
			$ticket=$tickets[$tcnt];
			$event=$details[$ticket["events_id"]];
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',10);
			$pos_top=5;
			$pos_left=5;
			$event[TEXT_FN]=$ticket["customers_firstname"];
			$event[TEXT_LN]=$ticket["customers_lastname"];
			
			// create bar image
			$bar_text=$ticket["orders_id"] . "-" . $ticket["customers_id"];
			$bar_filename=DIR_FS_CATALOG_IMAGES . "ticket_bar_". $bar_text . ".png";
			if (!file_exists($bar_filename)){
				create_bar_image($bar_text,$bar_filename);
			}
			// get info
			if (file_exists($bar_filename)){
				$info_bar=getimagesize($bar_filename);
				$info_bar[0]=$info_bar[0]/$unit*(74/100);
				$info_bar[1]=$info_bar[1]/$unit*(74/100);
				$bar_width=$info_bar[0];
			}

			// adjust for the bar image			
			if ($tInfo->bar_image_position=="L"){
				$pos_left=$bar_width+2;
			} else {
				$pos_width=$ticket_width-$bar_width-2;
			}
			$content_width=$ticket_width-$bar_width-2;
			//bar image
			if (isset($info_bar)){
				if ($tInfo->bar_image_position=="L"){
					$pdf->Image($bar_filename,2,2);
				} else {
					$pdf->Image($bar_filename,$ticket_width-$bar_width-2,2);
				}
			}

			// shop logo
			if (isset($info_shop)){
				if ($tInfo->shop_logo_position=="L"){
					$pdf->Image($tInfo->shop_logo_image,($tInfo->bar_image_position=="L"?$pos_left:0),0);
				} else  {
					$pdf->Image($tInfo->shop_logo_image,($tInfo->bar_image_position=="L"?$pos_width-$info_shop[0]:$pos_width-$info_shop[0]-2),0);
				}
			}
			$pdf->SetFont('Arial','B',10);
			//event details
			$content=$tInfo->event_details_content;
			reset($event);
			while(list($key1,$value1)=each($event)){
				$content=eregi_replace("%%" . $key1 . "%%",$value1,$content);
			}
	
			$splt_line=split("\n",$content);
			$temp_width=0;
			for ($icnt=0;$icnt<count($splt_line);$icnt++){
				$str_width=$pdf->GetStringWidth($splt_line[$icnt]);
				if ($str_width>$temp_width)	$temp_width=$str_width;
			}
	
			if ($tInfo->event_details_position=="L"){
				$temp_left=$pos_left;
			} else {
				$temp_left=$pos_width-2-$temp_width;
			}
			
			for ($icnt=0;$icnt<count($splt_line);$icnt++){
				if ($icnt>=1) $pdf->SetFont('Arial','B',8);
				$pdf->Text($temp_left,$pos_top,$splt_line[$icnt]);
				$pos_top+=3;
			}
			$pos_top+=8;
	
	
			$pdf->SetFont('Arial','B',10);
			// session dates
			$temp_top=$pos_top;
			$temp_width=$content_width-10;
			$temp_left=$pos_left;
			$part_width=($temp_width-($pdf->GetStringWidth("00-00-0000")*3))/2;
			$cnt=0;
			for ($icnt=0;$icnt<count($ticket["dates"]);$icnt++){
				$pdf->Text($temp_left,$temp_top,$ticket["dates"][$icnt]);
				$temp_left+=$part_width+$pdf->GetStringWidth($ticket["dates"][$icnt]);
				$cnt=$cnt+1;
				if ($cnt%3==0){
					$temp_top+=6;
					$temp_left=$pos_left;
				}
			}
			$pdf->SetFont('Arial','B',8);
			
			$pos_top+=10;
			// Expiry Date
			if ($max_date[$ticket["events_id"]]["cnt"]>1){
				$temp_str="Expiry Date: " . $max_date[$ticket["events_id"]]["date"];
				$pdf->Text($pos_width-$pdf->GetStringWidth($temp_str)-2,$pos_top,$temp_str);
			}
			if (!isset($cond_splt)){
				$cond_height=0;
				$cond_splt=split("\n",$tInfo->event_condition_content);
				$cond_height=count($cond_splt)*3;
				$cond_width=0;
				$temp_width=0;
				for ($icnt=0;$icnt<count($cond_splt);$icnt++){
					$temp_width=$pdf->GetStringWidth($cond_splt[$icnt]);
					if ($temp_width>$cond_width) $cond_width=$temp_width;
				}
			}
			
			// print osCommRes
			$temp_left=0;
			$temp_right=$ticket_width-$info_oscomm[0];
			if (isset($info_oscomm)){
				if ($tInfo->oscomm_logo_position=="L"){
					$pdf->Image($tInfo->oscomm_logo_image,$temp_left,$ticket_height-$info_oscomm[1]);
					$temp_left+=$info_oscomm[0];
				} else  {
					$pdf->Image($tInfo->oscomm_logo_image,$temp_right,$ticket_height-$info_oscomm[1]);
				}
			}
			
			
			// conditions
			$temp_top=$ticket_height-$cond_height;
			if ($tInfo->event_condition_position=="L"){
				$cond_left=$temp_left+2;
			} else {
				$cond_left=$ticket_width-$cond_width;
			}
			for ($icnt=0;$icnt<count($cond_splt);$icnt++){
				$pdf->Text($cond_left,$temp_top,$cond_splt[$icnt]);
				$temp_top+=3;
			}
		}
	} else {
		$pdf->AddPage();
		$pdf->SetFont('Arial','B',10);
		$pdf->Text(5,5,'No Details Found');
	}
	$events_tickets=sprintf("events_tickets_%s_%s",$login_id,time());
	$pdf->output(DIR_FS_CATALOG . "images/".$events_tickets.".pdf");
	tep_db_query("update  " . TABLE_ORDERS . "  set ticket_printed='Y' where orders_id in(" . $order_ids . ")");
	tep_redirect(DIR_WS_CATALOG . "images/".$events_tickets.".pdf");
	function &getDefaultTemplate(){
		$template_result=array(	"template_width"=>9,
								"template_height"=>5.5,
								"template_content"=>"shop_logo_image{}oscommres_micro_logo.png{}shop_logo_position{}L{}event_details_content{}%%First_Name%% %%Last_Name%% %%Event_Name%% %%Events_Location%% %%Start_Time%% %%End_Time%%{}event_details_position{}R{}oscomm_logo_image{}oscommres_micro_logo.png{}oscomm_logo_position{}R{}event_condition_content{}Card is valid for one until last class date. No refunds for unused classes. Make-up any missed classes before last class date. Renew card ph 12345678, oscommres.com{}event_condition_position{}L{}bar_image_position{}R"
								);
		return $template_result;
	}
?>
