<?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');
	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='TICS'");
	
	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);

	
	// 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 os.start_date,os.end_date,os.start_time,os.end_time,os.products_id as service_id,o.orders_id,o.customers_id,c.customers_firstname,c.customers_lastname from " . 
				TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_SERVICES . " os, " . TABLE_CUSTOMERS . " c " .
				" where o.orders_id in(" . $order_ids . ") and op.products_type='V' and op.orders_products_status>1 and op.orders_products_status<5 and o.orders_id=op.orders_id and o.orders_id=os.orders_id and op.products_id=os.products_id and o.customers_id=c.customers_id " .
				" order by o.orders_id,op.products_name,os.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_service=0;
	
	while($order_result=tep_db_fetch_array($order_query)){
		$services_id=$order_result["service_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_service=0;
		}

			if (!isset($details[$services_id])){
				/*$service_query=tep_db_query("SELECT l.location_name,sd.service_name,time_format(s.service_start_time,'%h:%i %p') as services_start_time,time_format(s.service_end_time,'%h:%i %p') as services_end_time from " . 
											TABLE_SERVICES . " s left join " . TABLE_SERVICES_DESCRIPTION . " sd on s.service_id=sd.service_id left join " . 
											TABLE_EVENTS_LOCATIONS . " l on s.service_location=l.location_id where s.service_id='" . $services_id . "' and sd.language_id='" . $languages_id . "'"
										);*/
										
										
				$service_query=tep_db_query("SELECT l.location_name,sd.service_name from " . 
											TABLE_SERVICES . " s left join " . TABLE_SERVICES_DESCRIPTION . " sd on s.service_id=sd.service_id left join " . 
											TABLE_EVENTS_LOCATIONS . " l on s.service_location=l.location_id where s.service_id='" . $services_id . "' and sd.language_id='" . $languages_id . "'"
										);
				$service_result=tep_db_fetch_array($service_query);
				$details[$services_id][TEXT_SEL]=$service_result["location_name"];
				$details[$services_id][TEXT_SEN]=$service_result["service_name"];
				$details[$services_id][TEXT_SI]=date('h:i A',strtotime($order_result["start_time"]));
				$details[$services_id][TEXT_EI]=date('h:i A',strtotime($order_result["end_time"]));
		$tickets[$row]['serE_time'][]=date("h:i A",$service_result["services_end_time"]);
			}
			$cnt=1;
			if ($prev_service>0) $row=$row+1;
			$prev_service=$services_id;
		if ($cnt==1) {
			$tickets[$row]["service_id"]=$services_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"];
			
		}
		$tickets[$row]['s_dates'][]=format_date($order_result["start_date"]);
		$tickets[$row]['e_dates'][]=format_date($order_result["end_date"]);
		$s_time=split(":",$order_result["start_time"]);
		$e_time=split(":",$order_result["end_time"]);
		$tickets[$row]['s_time'][].=date("h:i A",mktime($s_time[0],$s_time[1],$s_time[2],0,0)) .",";
		$tickets[$row]['e_time'][].=date("h:i A",mktime($e_time[0],$e_time[1],$e_time[2],0,0)) .",";
		$max_date[$orders_id][$services_id]["date"]=format_date($order_result["start_date"]);
		if (!isset($max_date[$orders_id][$services_id]["cnt"])) $max_date[$services_id]["cnt"]=0;
		$max_date[$orders_id][$services_id]["cnt"]=$max_date[$services_id]["cnt"]+1;	
		 $cnt=$cnt+1;
		 if ($cnt>9){
		  $row=$row+1;
		  $cnt=1;
		 }
	}

	if ($found_results){
		require("barcode/image.php");
		$bar_width=15;
		$count=0;
		// print the ticket details
		//print_r($tickets);
	 for($tic=0;$tic<sizeof($tickets);$tic++){
			$ticket=$tickets[$tic];
			$service=$details[$ticket["service_id"]];
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',10);
			$pos_top=5;
			$pos_left=5;
			
			$service[TEXT_FN]=$ticket["customers_firstname"];
			$service[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;
				$info_bar[1]=$info_bar[1]/$unit;
				$bar_width=$info_bar[0];
			}

			// adjust for the bar image			
			if ($template["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,($pos_left>5?$pos_left:0),0);
				} else  {
					$pdf->Image($tInfo->shop_logo_image,$pos_width-$info_shop[0]-2,0);
				}
			}
			$pdf->SetFont('Arial','B',10);
			//service details
			$content=$tInfo->service_details_content;
			reset($service);
			while(list($key1,$value1)=each($service)){
				$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->service_details_position=="L"){
				$temp_left=$pos_left;
			} else {
				$temp_left=$pos_width-$temp_width-15;
			}
			
			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);
			// service 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["s_dates"]);$icnt++){
			    $start_d_t='Start Date:'. '  ' . $ticket["s_dates"][$icnt] . ' - ' . $ticket["s_time"][$icnt];
				$end_d_t='End Date:'. '  ' . $ticket["e_dates"][$icnt] . ' - ' . $ticket["e_time"][$icnt];
				$pdf->Text($temp_left,$temp_top,$start_d_t);
				$temp_left+=$part_width+$pdf->GetStringWidth($start_d_t);
				$temp_top+=6;
				$temp_left=$pos_left;
				$pdf->Text($temp_left,$temp_top,$end_d_t);
				$temp_left+=$part_width+$pdf->GetStringWidth($end_d_t);
				$cnt=$cnt+1;
				if ($cnt%3==0){
					$temp_top+=6;
					$temp_left=$pos_left;
				}
			
			}
			$pdf->SetFont('Arial','B',8);
			$pos_top+=20;
			
			if (!isset($cond_splt)){
				$cond_height=0;
				$cond_splt=split("\n",$tInfo->service_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->service_condition_position=="L"){
				$cond_left=$temp_left+2;
			} else {
				$cond_left=$temp_right-$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');
	}
	$report_filename=sprintf("service_tickets_%s_%s",$login_id,time());
	$pdf->output(DIR_FS_CATALOG . "images/".$report_filename.".pdf");
	tep_db_query("update  " . TABLE_ORDERS . "  set ticket_printed='Y' where orders_id in(" . $order_ids . ")");
	tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".pdf");
	function &getDefaultTemplate(){
		$template_result=array(	"template_width"=>10.5,
								"template_height"=>6.3,
								"template_content"=>"shop_logo_image{}" . DIR_FS_CATALOG . "images/shop_logo.png{}shop_logo_position{}L{}service_details_content{}%%First_Name%%-%%Last_Name%% \n%%Service_Name%% \n%%Service_Location%% \n%%Start_Time%% \n%%End_Time%%{}service_details_position{}R{}oscomm_logo_image{}" . DIR_FS_CATALOG . "/images/osCommRes_logo.png{}oscomm_logo_position{}R{}service_condition_content{}Card is valid for one until last class date.\nNo refunds for unused classes.\nMake-up any missed classes \nbefore last class date. \nRenew card ph12345678.\nPlease Bring this card for each class. \nFull Conditions www.yogbaby.com.au{}service_condition_position{}L{}bar_image_position{}R"
								);
		return $template_result;
	}
?>