<?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
	defined('_FEXEC') or die();

  //require('includes/application_top.php');
  //require(DIR_WS_CLASSES . 'currencies.php');
  define('FPDF_FONTPATH','font/');
  require('includes/fpdf.php');
  
  define('PDF_LEFT_MARGIN',20);
  define('PDF_TOP_MARGIN',10);
  define('PDF_ORIENTATION','P');
  define('PDF_PAGE_FORMAT','A4');
	$currencies = new currencies();
	/*$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	if ($action==""){
		$action = (isset($HTTP_GET_VARS['firstaction']) ? $HTTP_GET_VARS['firstaction'] : '');
	}

	$orders_id=(isset($HTTP_GET_VARS['orders_id'])?$HTTP_GET_VARS['orders_id']:'');
	$mode=(isset($HTTP_GET_VARS['mode'])?(int)$HTTP_GET_VARS['mode_id']:0);
	$error=(isset($HTTP_GET_VARS['error'])?$HTTP_GET_VARS['error']:'');
	$view_type=(isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'');
	if ($action=="search")
	{
		if (isset($HTTP_GET_VARS['page']) && ($HTTP_GET_VARS['page'] > 1)) $rows = $HTTP_GET_VARS['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
		$rows=0;
		$search_heading="";
		$search_group='A'; // all users both reserved and waitlist
		$query_field1=$query_field2=$query_field3=$query_field4=$query_field5=$query_field6=$query_field7=$query_field8=$query_field9=$query_field10=$query_field11="";
		if (tep_not_null($HTTP_GET_VARS['tx_fname'])){
			$query_field1=" and c.customers_firstname like '%" . tep_db_input($HTTP_GET_VARS['tx_fname']) . "%'";
			$search_heading.="First Name=" . $HTTP_GET_VARS['tx_fname'] . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_lname'])){
			$query_field2= " and c.customers_lastname like '%" . tep_db_input($HTTP_GET_VARS['tx_lname']) . "%'";
			$search_heading="Last Name=" . $HTTP_GET_VARS['tx_lname'] . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_code'])){
			$query_field3=" and o.customers_postcode='" . tep_db_input($HTTP_GET_VARS['tx_code']) . "'";
			$search_heading.="Post Code=" . $HTTP_GET_VARS['tx_code'] . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_words'])){
			$query_field4=" and (c.customers_firstname like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or c.customers_lastname like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or c.customers_email_address like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%' or e.events_location like '%" . tep_db_input($HTTP_GET_VARS['tx_words']) . "%')";
			$search_heading.="Keywords=" . $HTTP_GET_VARS['tx_words'] . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_from'])){
			$query_field5=" and s.start_date>='" . tep_db_input($HTTP_GET_VARS['tx_from']) . "'";
			$search_heading="Date From=" . format_date($HTTP_GET_VARS['tx_from']) . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_until'])){
			$query_field6=" and s.start_date<='" . tep_db_input($HTTP_GET_VARS['tx_until']) . "'";
			$search_heading.="Date Until=" . format_date($HTTP_GET_VARS['tx_until']) . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_waitlist'])) $search_group='W';
		if (tep_not_null($HTTP_GET_VARS['tx_reserve'])){
			if ($search_group=='W') $search_group='A';
			else $search_group='R';
		}

		if ($search_group=='W' || $search_group=='R') {
			$query_field7=" and op.events_type='" . $search_group . "'";
			$search_heading.="Events Type=" . ($search_group=='R'?'Reserved, ':'Waitlist, ');
		}

		if (tep_not_null($HTTP_GET_VARS['tx_event'])){
			$query_field8=" and e.events_id='" . tep_db_input($HTTP_GET_VARS['tx_event']) . "'";
			$search_heading.="Event Name=" . tep_get_events_name($HTTP_GET_VARS['tx_event']) . ", ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_dated'])){
			$search_heading.="Show Start/End Dates=True, ";
		}
		if (tep_not_null($HTTP_GET_VARS['tx_session'])){
			$date_select=$HTTP_GET_VARS['tx_session'];
			$date_split=split("-",$date_select);
			$format_split=split("-",EVENTS_DATE_FORMAT);
			$date_final=array();
			// convert date to database format
			if (sizeof($date_split)==3 && sizeof($format_split)==3){
				for ($icnt=0;$icnt<sizeof($date_split);$icnt++){
					$date_final[$format_split[$icnt]]=$date_split[$icnt];
				}
				$date_select=$date_final['Y'] . '-' . $date_final['m'] . '-' . $date_final['d'];
			}
			$query_field9=" and s.start_date='" . tep_db_input($date_select) . "'";
			$search_heading.="Session Date=" . $HTTP_GET_VARS['tx_session'] . ", ";
		}
		$query_field10="";
		if (tep_not_null($HTTP_GET_VARS['tx_rtype'])){
			$tx_rtype=$HTTP_GET_VARS['tx_rtype'];
			if ($tx_rtype=='R'){
				$query_field10=" and op.events_type='R' ";
				$search_heading.="Events Type=Reserved, ";
			} else {
				$query_field10=" and op.events_type='W' and op.waitlist_type='" . $tx_rtype . "'";
				$search_heading.="Events Type=Waitlist, ";
			}
		}
		if (tep_not_null($HTTP_GET_VARS['tx_current'])){
	     	$query_field11=" and s.start_date=curdate() ";
			$search_heading.="Only Current, ";
		}
     if ($search_heading!="") $search_heading=substr($search_heading,0,-2);
	 
	$fields=",min(s.start_date) as start_date,count(s.start_date) as total_count,max(s.start_date) as end_date,op.products_tax,sum(op.final_price) as product_price ";
	$query=$query_field1 . $query_field2 . $query_field3 . $query_field4 . $query_field5 . $query_field6 . $query_field7 . $query_field8 . $query_field9 . $query_field10 . $query_field11 . ' group by o.customers_id,e.events_id ';
     	
     $search_query="select o.orders_id,op.orders_products_id,op.products_id,op.waitlist_type,op.events_type,o.customers_id,o.question_value_text,o.question_value_date,o.question_value_number,c.customers_firstname,c.customers_lastname,o.customers_postcode,e.events_location,e.events_renewal_start,e.events_renewal_end,e.events_instructor,ed.events_name,e.events_id,e.priority_question,o.payment_method,o.orders_status " . $fields . " from " . 
                     TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed " . 
		     " where op.products_type='E' and  op.events_type!='' and op.events_type!='C'" .
		     " and  op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ed.events_id=e.events_id and ed.language_id='" . $languages_id . "'" . $query;

     $result_query=tep_db_query($search_query);
     $cnt=tep_db_num_rows($result_query);
     $action="search";
	 // print some search headings
  }*/
 $result_query=tep_db_query($search_query);
 $cnt=tep_db_num_rows($result_query);
 $action="search";

  $font_name="Arial";
  $font_size=9;
 	class PDF extends FPDF{
		function Header()
		{
			global $columns,$cell_height;
			if ($this->PageNo()>1) {
				$this->SetFillColor(201,201,201);
				$this->SetTextColor(255,255,255);
				$this->SetFont($font_name,'',$font_size);
				for($icnt=0;$icnt<sizeof($columns);$icnt++){
					$this->Cell($columns[$icnt]["width"],$cell_height,$columns[$icnt]["name"],0,0,"L",1);
				}
				$this->Ln();
			}
		}
	}
 
	$pdf=new PDF(PDF_ORIENTATION,"pt",PDF_PAGE_FORMAT);
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetFont($font_name,'',$font_size);
	$pdf->SetAutoPageBreak(true,PDF_TOP_MARGIN);
	$pos_x=0;
	$pos_y=0;
	
	//$cell_width=0;
	$cell_height=12;
	$pdf->SetMargins(PDF_LEFT_MARGIN,PDF_TOP_MARGIN);
	$pdf->Cell(0,15,STORE_NAME,0,0,"C");
	$pdf->Ln();
	if ($search_heading!="") {
		$search_heading = str_replace("<br>","\n",$search_heading);		
		$pdf->MultiCell(0,10,TEXT_SEARCH_NAME);
		$pdf->Ln();
		$pdf->MultiCell(0,10,$search_heading);
		$pdf->Ln();
	}
	$pdf->Cell(0,15,TEXT_DATE. date(EVENTS_DATE_FORMAT));
	$pdf->Ln();
	
	$columns=array(array("name"=>TEXT_RESULT_SNO,"width"=>20),
					array("name"=>TEXT_RESULT_FIRSTNAME,"width"=>130),
					array("name"=>TEXT_RESULT_LASTNAME,"width"=>130),
					array("name"=>TEXT_RESULT_EVENT,"width"=>205),
					array("name"=>TEXT_RESULT_STARTDATE,"width"=>40),
					array("name"=>TEXT_RESULT_ENDDATE,"width"=>40));

	$pdf->SetFillColor(201,201,201);
	$pdf->SetTextColor(255,255,255);
	$pdf->SetFont($font_name,'',$font_size);
	for($icnt=0;$icnt<sizeof($columns);$icnt++){
		$pdf->Cell($columns[$icnt]["width"],$cell_height,$columns[$icnt]["name"],0,0,"L",1);
	}
	$pdf->Ln();
	$rows=0;$currow=0;
	$pdf->SetFillColor(240,241,241);
	$pdf->SetTextColor(0,0,0);
	$pdf->SetDrawColor(255,255,255);
	$pdf->SetFont($font_name,'',$font_size);
	for ($icnt=0;$icnt<1;$icnt++){
	tep_db_data_seek($result_query,0);
	$pos_y=PDF_TOP_MARGIN;
	$row_height=$cell_height;
	while($result=tep_db_fetch_array($result_query)) {
			$cell_height=10;
			$pos_x=PDF_LEFT_MARGIN;
			$result['total_count']=(tep_not_null($result['total_count'])?$result['total_count']:1);
			$rows++;
			$currow++;
			if (strlen($rows) < 2) {
				$rows = '0' . $rows;
			}
			
			$payment_method=substr($result["payment_method"],0,2);
	
			
			$paid_text=sprintf("%s",$currencies->format(tep_add_tax($result["product_price"],$result["products_tax"])));
	
			$span=array();
			$max_row=0;
			$span["firstname"]=wrap_content($result["customers_firstname"],$columns[1]['width']);
			if (sizeof($span["firstname"])>$max_row) $max_row=sizeof($span["firstname"]);
			
			$span["lastname"]=wrap_content($result["customers_lastname"],$columns[2]['width']);
			if (sizeof($span["lastname"])>$max_row) $max_row=sizeof($span["lastname"]);
			
			$span["event"]=wrap_content($result["events_name"],$columns[3]['width']);
			if (sizeof($span["event"])>$max_row) $max_row=sizeof($span["event"]);
			
			$pdf->Cell($columns[0]["width"],$cell_height,$rows,1,0,"L",1);
			$pdf->Cell($columns[1]["width"],$cell_height,$span["firstname"][0],1,0,"L",1);
			$pdf->Cell($columns[2]["width"],$cell_height,$span["lastname"][0],1,0,"L",1);
			$pdf->Cell($columns[3]["width"],$cell_height,$span["event"][0],1,0,"L",1);
			$pdf->Cell($columns[4]["width"],$cell_height,format_date($result['start_date'],true),1,0,"L",1);
			$pdf->Cell($columns[5]["width"],$cell_height,format_date($result['end_date'],true),1,0,"L",1);
			$pdf->Ln();
			
			$span_fetch=array("row","firstname","lastname","question","event");
			for ($kcnt=1;$kcnt<$max_row;$kcnt++){
				for ($lcnt=0;$lcnt<7;$lcnt++){
					if (isset($span[$span_fetch[$lcnt]][$kcnt])){
						$pdf->Cell($columns[$lcnt]["width"],$cell_height,$span[$span_fetch[$lcnt]][$kcnt],0,0,"L",1);
					} else {
						$pdf->Cell($columns[$lcnt]["width"],$cell_height,"",0,0,"L",1);
					}
				}
				$pdf->Ln();
			}
		}
	}

	$pdf->output(DIR_FS_CATALOG . "images/reservations.pdf");


	//require(DIR_WS_INCLUDES . 'application_bottom.php');

	function wrap_content($string,$width){
		global $pdf;
		$result=array();
		$pos=-1;
		$temp_string="";
		$char="";
		for ($icnt=0;$icnt<strlen($string);$icnt++){
			$char=substr($string,$icnt,1);
			if ($pdf->GetStringWidth($temp_string . $char)>$width-2){
				$result[]=$temp_string;
				$temp_string="";
			}
			$temp_string.=$char;
		}
		if ($temp_string!="") $result[]=$temp_string;
		return $result;
	}
?>
