<?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');
  tep_get_last_access_file();
  require(DIR_WS_CLASSES . 'split_page_results_report.php');
  require(DIR_WS_FUNCTIONS . 'events_tree.php');
  define(BOX_WIDTH1,'125');
	// get initial parameters, check the session for previous settings
	if (isset($HTTP_GET_VARS["return"]) && isset($HTTP_SESSION_VARS["rep_params"])){
		$input_params=&$HTTP_SESSION_VARS["rep_params"];
	} else {
		$input_params=&$HTTP_POST_VARS;
		if (isset($input_params["post_action"])){
			tep_set_session_value("rep_params",$HTTP_POST_VARS);
			$GLOBALS["rep_params"]["post_action"]="screen";
		} else tep_set_session_value("rep_params",array());
	}
	$date_begin = (isset($input_params["txt_date_begin"])?tep_convert_date_raw($input_params["txt_date_begin"]):getServerDate());
	$date_end = (isset($input_params["txt_date_end"])?tep_convert_date_raw($input_params["txt_date_end"]):'');
	$instructor=(isset($input_params["cbo_instructor"])?$input_params["cbo_instructor"]:-1);
	$location=(isset($input_params["cbo_location"])?$input_params["cbo_location"]:-1);
	$event=(isset($input_params["cbo_event"])?$input_params["cbo_event"]:-1);
	$post_action=(isset($input_params["post_action"])?$input_params["post_action"]:'');
	$page=(isset($input_params["page"])?$input_params["page"]:1);
	
	if ($date_end==""){
		$res = tep_db_query("SELECT date_add('" . $date_begin . "',INTERVAL 7 day)");	
		$row = mysql_fetch_array($res);
		$date_end = $row[0];
	}
	// get instructor names			
	$sql = "select a.admin_id admin_id, admin_firstname, admin_lastname  from events_instructors ei, admin a where ei.admin_id=a.admin_id order by admin_firstname,admin_lastname";
	$sql_result = tep_db_query($sql);
	$num_rows = tep_db_num_rows($sql_result);
	$instructors_array[]=array("id"=>-1,"text"=>TEXT_ALL_INSTRUCTORS);
	while($row=tep_db_fetch_array($sql_result)) {
		$instructors_array[]=array("id"=>$row["admin_id"],"text"=>$row["admin_firstname"]." ".$row["admin_lastname"]);
	}
	mysql_free_result($sql_result);
		
	// get location names
	$sql = "select distinct location_id, location_name from events,events_locations where location_id=events_location order by location_name";
	$sql_result = tep_db_query($sql);
	$locations_array[]=array("id"=>-1,"text"=>TEXT_ALL_LOCATIONS);
	while($row=tep_db_fetch_array($sql_result)) {
		$locations_array[]=array("id"=>$row["location_id"],"text"=>$row["location_name"]);
	}
	mysql_free_result($sql_result);
	// Events List
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
	$sql = "select e.events_id events_id, Events_name  events_name from events e, events_description ed where e.events_id = ed.events_id and ed.language_id='" . (int)$languages_id . "' " . EVENTS_CONDITION_STATUS . $events_option ." order by events_name";
	$sql_result = tep_db_query($sql);
	$events_array[]=array("id"=>-1,"text"=>TEXT_ALL_EVENTS);
	while($row=tep_db_fetch_array($sql_result)) {
		$events_array[]=array("id"=>$row["events_id"],"text"=>$row["events_name"]);
	}
	mysql_free_result($sql_result);
	
	$found_results=false;
		$where = " ";
		$where_status = " ";
		if($instructor!="-1") {
			$where.= "and e.events_instructor='".$instructor."' ";
		} 
		if($location!="-1") {
			$where.= "and e.events_location='".$location."' ";
		}
		if($event!="-1") {
			$where.= "and e.events_id='".$event."' ";
		}

		// create the query for fetching customer datas
		$sql = "select distinct c.customers_id, LTRIM(c.customers_firstname) as customers_firstname,LTRIM(c.customers_lastname) as customers_lastname,o.customers_suburb,
		o.customers_email_address customers_email_address, c.customers_telephone customers_telephone,
		c.customers_second_telephone,c.customers_fax, events_type, waitlist_type, orders_status,
		c.customers_dob customers_dob from " . TABLE_EVENTS_SESSIONS . " es, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS . " e, " .
		TABLE_CUSTOMERS . " c where sessions_id = op.products_id and op.orders_id=o.orders_id 
		and products_type='E' and es.events_id=e.events_id and o.customers_id=c.customers_id and
		date_format(DATE_ADD(customers_dob, INTERVAL extract(YEAR from curdate())-extract(YEAR from customers_dob) YEAR),'%Y-%m-%d') between '$date_begin' and '$date_end' " .
		$where. " group by c.customers_id order by customers_lastname,customers_firstname";

		$sql_temp=$sql;
		// split the report
		$query_split = new splitPageResultsReport($page, REPORT_MAX_ROWS_PAGE, $sql_temp, $query_split_numrows);
		
		$sql_result = tep_db_query($sql);
		
		if (tep_db_num_rows($sql_result)>0) $found_results=true;
?>
<!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>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="JavaScript" src="includes/date-picker.js"></script>
<script language="JavaScript">
	function doSubmit() {
		document.f.submit();
	}
	function showCustomerDetails(id){
		location.href="<?php echo tep_href_link(FILENAME_CUSTOMERS);?>"+"?action=edit&cID="+id+"&return=bl";
	}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
	<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
		<!-- body_text //-->	  
		<td width=100% align=left valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<tr height="10">
				<!-- <td class="pageHeading"><?php //echo HEADING_CUSTOMER_BIRTHDAYS;?></td>	!-->
			</tr>
			<?php echo tep_draw_form("f",FILENAME_BIRTHDAY_LIST); ?>
			<input type="hidden" name="post_action" value="1"/>
			<!-- form -->
			<tr >
				<td>
				<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
					<tr>
						
						<td nowrap><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
						<?php echo TEXT_START_DATE . '&nbsp;' . tep_draw_input_field("txt_date_begin",format_date($date_begin),"size=10",false,'text',false);?>
							
							<a href="javascript:show_calendar('f.txt_date_begin',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>
						</td>
						
						<td nowrap>
						<?php echo TEXT_END_DATE . '&nbsp;' . tep_draw_input_field("txt_date_end",format_date($date_end),"size=10",false,'text',false);?>
							
							<a href="javascript:show_calendar('f.txt_date_end',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>
							</td>
						<td rowspan="2" valign="middle" align="right">
						   <a href="javascript:doSubmit()"><?php echo tep_image_submit('button_search_customers.gif', IMAGE_SEARCH_CUSTOMERS); ?></a>
						</td>
						<?php 
							//$all_events_array=array();
						//	$all_events_array = tep_get_events_array_single();
						//	if(sizeof($all_events_array)<=0) echo '<td colspan="2">Events are not Available</td>';
						//	else{ ?>
						<td align="right"><?php //echo TEXT_EVENT;?></td>
						<td nowrap="true"><?php //echo tep_draw_event_select_menu('cbo_event',$all_events_array,$event,' id="cbo_event"');?></td>
						<?php //}?>	
						<td colspan="2"><?php //echo tep_draw_separator("pixel_trans.gif","1","5");?></td>
					</tr>
					<tr>
						<td nowrap="true">
							<?php echo  TEXT_LOCATION . tep_draw_separator('pixel_trans.gif',15,10)  . tep_draw_pull_down_menu("cbo_location",$locations_array,$location);?>
						</td>
						
						<td nowrap="true">
						<?php 
							$all_events_array=array();
							$all_events_array = tep_get_events_array_single();
							if(sizeof($all_events_array)<=0) echo 'Events are not Available';
							else echo TEXT_EVENT . '&nbsp;' . tep_draw_event_select_menu('cbo_event',$all_events_array,$event,' id="cbo_event"');
						?>
						<?php echo '&nbsp;&nbsp;&nbsp;&nbsp;' . TEXT_INSTRUCTOR . '&nbsp;' . tep_draw_pull_down_menu("cbo_instructor",$instructors_array,$instructor);?>
						</td>
						



					</tr>
					<!-- report content -->
				</table>
				</td>
			</tr>
			</form>
			<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
<?php 
				if ($found_results){
				?>
					<tr>
						<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
								<td class="smallText" align="left">
									<?php echo $query_split->display_links($query_split_numrows, REPORT_MAX_ROWS_PAGE, REPORT_MAX_LINKS_PAGE, $page,tep_get_report_params()); ?>&nbsp;
								</td>
							</tr>
						</table>
						</td>
					</tr>
					<tr>
						<td>
						<table border="0" width="100%" cellspacing="1" cellpadding="1">
							<tr class='dataTableHeadingTitleRow'>
								<td class='dataTableHeadingTitleContent' width="30">#</td>
								<td class='dataTableHeadingTitleContent'><?php echo TEXT_LAST_NAME;?></td>
								<td class='dataTableHeadingTitleContent'><?php echo TEXT_FIRST_NAME;?></td>
								<td class='dataTableHeadingTitleContent'>&nbsp;</td>
								<td class='dataTableHeadingTitleContent'><?php echo TEXT_SUBURB;?></td>
								<td class='dataTableHeadingTitleContent' width="130"><?php echo TEXT_PHONE;?></td>
								<td class='dataTableHeadingTitleContent' width="130"><?php echo TEXT_SECOND_PHONE;?></td>
								<td class='dataTableHeadingTitleContent' width="130"><?php echo TEXT_MOBILE;?></td>
								<td class='dataTableHeadingTitleContent' width="80"><?php echo TEXT_BIRTH_DATE;?></td>
							</tr>
							<tr height="10"></tr>

				<?php
					$i=1;
          			while($row = mysql_fetch_assoc($sql_result)) {
					?>
					  <tr class='dataTableRow' height="20" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="javascript:showCustomerDetails('<?php echo $row["customers_id"];?>');">
						  <td class='dataTableContent' style="cursor:hand"><?php echo $i?></td>
							<td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_lastname']?></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_firstname']?></td>
						   <td class='dataTableContent' style="cursor:hand" align="center"><a href="mailto:<?php echo $row['customers_email_address']?>"><img src="images/icons/mail.gif" border="0"></a></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_suburb']?></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_telephone']?></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_second_telephone']?></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_fax']?></td>
						  <td class='dataTableContent' style="cursor:hand"><?php echo format_date($row['customers_dob'])?></td>
					  </tr>
      			<?php 
						$i++;						
					} // for $icnt
					?>
				</table>
				</TD>
			</tr>
			<?php	} // if $found_results
					else{
						echo '<tr><td class="main" align="center">' . TEXT_NO_RECORDS_FOUND . '</td></tr>';
					}
			?>
	</table>
	</td>
</tr>
</table>	
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

