<?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_FUNCTIONS . 'events_tree.php');
	define(BOX_WIDTH1,'125');
	// get initial parameters, first check to get previous settings from session if var "return" is present
	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;
	}
	$action=isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action']:'';
	$date=isset($input_params['txtDate'])?tep_convert_date_raw($input_params['txtDate']):getServerDate();
	$view=isset($input_params['sel_view'])? $input_params['sel_view'] : -1;
	$unprinted=isset($input_params['unprinted']) ? true : false; 
	$summary=isset($input_params['summary'])? true : false;
	$pending=isset($input_params['pending']) ? true : false; 
	$instructor=isset($input_params['sel_instructor'])?$input_params['sel_instructor']:-1;
	$event=isset($input_params['sel_event'])?$input_params['sel_event']:-1;
	$instructors_array = array();
	$events_array = array();

	$date_begin=$date;
	$date_end=$date;
	$days= date('t',strtotime($date));
	
	if($view==2) {
		$sql =  "select date_add('$date', interval -(dayofweek('$date')-1) day) begin, date_add('$date', interval (7-dayofweek('$date')) day) end";	
	} else if($view==3) {
		$sql =  "select date_add('$date', interval -(dayofmonth('$date')-1) day) begin, date_add('$date', interval ('$days'-dayofmonth('$date')) day) end";	
	}
	if($view==2 || $view==3) {
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$date_end = $row["end"];	
	}
	// get instructor names			
	$sql_ins = "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_ins);
	$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 events name
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
	$sql_event = "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='" . $languages_id ."'" . $events_option ." order by events_name";
	$sql_result = tep_db_query($sql_event);
	$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);
	$view_array=array(array("id"=>1,"text"=>TEXT_DAY),array("id"=>2,"text"=>TEXT_WEEK),array("id"=>3,"text"=>TEXT_MONTH));
	$found_results=false;
	// build the output if needed
		$where = " and (s.start_date>='" . $date_begin . "' and s.start_date<='" . $date_end . "')";
		if($instructor>0) {
			$where.= "and e.events_instructor='".$instructor."' ";
		} 
		if($event>0) {
			$where.= "and e.events_id='".$event."' ";
		}
		if($unprinted){
			$where.="and op.idcards_printed!='Y'";
		}
		if($pending){
			$where.="and op.idcards_printed='N'";
		}
		
		$sql = "select e.events_group_bookings,op.products_quantity,o.orders_id,o.customers_id,LTRIM(c.customers_firstname) as customers_firstname,LTRIM(c.customers_lastname) as customers_lastname,c.customers_email_address,op.idcards_printed from " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_ORDERS_PRODUCTS . " op," . TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " c, "  . TABLE_EVENTS . " e, ".TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c 
				where e.events_id=s.events_id and op.products_type='E' and op.events_type='R' and s.sessions_id=op.products_id and o.orders_id=op.orders_id and o.orders_id=c2c.orders_id and c2c.customer_id=c.customers_id " . $where . " group by c.customers_id order by customers_lastname,customers_firstname";
		//echo $sql;

		$sql_result=tep_db_query($sql);
		$num_rows=tep_db_num_rows($sql_result);
		if ($num_rows>0) $found_results=true;
		tep_delete_temp_files("id_card");
		tep_delete_temp_files("id_bar");
?>
<!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 doaction(mode) {
		if (mode==1) {
			document.f.action="<?php echo FILENAME_EVENTS_REPORT_IDCARDS . '?action=idcards';?>";
		}
		document.f.submit();
	}
		function print_selected(){
			var element=document.id_card.customer_id;
			var content="";
			if (element[0]){
				for (icnt=0;icnt<element.length;icnt++){
					if (element[icnt].checked){
						content+=element[icnt].value + ",";
					}
				}
			} else {
				if (element.checked){
					content+=element.value + ",";
				}
			}
			if (content!="") {
				content=content.substr(0,content.length-1);
				window.open("<?php echo FILENAME_EVENTS_ID_CARD;?>?cID="+content,"test");
				return;
			}
			alert("<?php echo ERR_SELECT_CUSTOMER;?>");
		}
		function select_all_boxes(){
			var select_all=document.id_card.select_all;
			var customer_id=document.id_card.customer_id;
			if (customer_id[0]){
				for (icnt=0;icnt<customer_id.length;icnt++){
					customer_id[icnt].checked=select_all.checked;
				}
			} else {
				customer_id.checked=select_all.checked;
			}
		}
</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_EVENT_IDCARDS;?></td>!-->	
		</tr>
        <tr >
		  	<TD>
			<?php 	echo tep_draw_form("f",FILENAME_EVENTS_REPORT_IDCARDS);?>
			<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
				<tr>
					
					<td nowrap="true"><?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_DATE . tep_draw_separator('pixel_trans.gif',35,10)  . tep_draw_input_field("txtDate",format_date($date),'maxlength="10" size="10"');?>
						<a href="javascript:show_calendar('f.txtDate',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>
							<?php 
								echo tep_draw_checkbox_field('summary','1',$summary)  . '&nbsp;' . TEXT_SUMMARY .'&nbsp;&nbsp;&nbsp;&nbsp;';
								echo tep_draw_checkbox_field('unprinted',1,$unprinted) . '&nbsp;' . TEXT_UNPRINTED_IDCARDS .'&nbsp;&nbsp;';
								echo tep_draw_checkbox_field('pending',1,$pending) . '&nbsp;' . TEXT_PENDING_IDCARDS .'&nbsp;&nbsp;';
						?>
				    </td>
					<td valign="middle" align="right">
						<?php echo '<a href="javascript:doaction(1);">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_IDCARDS) . '</a>'; ?>
					</td>
				</tr>
				<tr>
					<td nowrap="true">
						<?php 
							echo TEXT_INSTRUCTOR . '&nbsp;' . tep_draw_pull_down_menu("sel_instructor",$instructors_array,$instructor) .'&nbsp;&nbsp;&nbsp;';
							$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('sel_event',$all_events_array,$event,' id="sel_event"');
							echo '&nbsp;&nbsp;' . TEXT_VIEW . '&nbsp;' . tep_draw_pull_down_menu("sel_view",$view_array,$view) . '&nbsp;&nbsp;&nbsp;';
						?>
					</td>
				</tr>
				<tr>
				</tr>
			<!-- report content -->
		</table>
		</form>
		</td>
		</tr>
		<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
		<?php if ($found_results) {
		echo tep_draw_form("id_card",FILENAME_EVENTS_REPORT_IDCARDS);?>
		<tr>
		<td>
	   <table border="0" width="100%" cellspacing="0" cellpadding="0">
	   <?php if (!$summary) { ?>
			<tr class='dataTableHeadingTitleRow'>
				<td class='dataTableHeadingTitleContent' width="30">&nbsp;</td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_LNAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_FNAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_CUSTOMER_ID;?></td>
				<td class='dataTableHeadingTitleContent' width="50"><?php echo TEXT_PRINTED;?></td>
				<td class="dataTableHeadingTitleContent" align="right"><?php echo TEXT_DETAILS;?></td>
			</tr>
			
			<?php } ?>
			
			<?php if ($summary) {?>
			
			<tr class='dataTableHeadingTitleRow'>
					<td class='dataTableHeadingTitleContent'><?php echo TEXT_FNAME;?></td>
					<td class='dataTableHeadingTitleContent' width="230"><?php echo TEXT_CUSTOMER_ID;?></td>
					
			</tr>
			
			
			<?php
			}
			
			
					$i=1;
					mysql_data_seek($sql_result,0);
          			while($row = tep_db_fetch_array($sql_result)) {
					if($disp_class=='dataTableRow')
						  	$disp_class='dataTableRowOver';
						  else
						  	$disp_class='dataTableRow';
					
						 if (!$summary) { 
					?>
                  <tr class='<?php echo $disp_class;?>' height="20">
					   <td class="dataTableContent"><?php echo tep_draw_checkbox_field('customer_id',$row["orders_id"]);?></td>
					   <td class='dataTableContent'><?php echo '<a href="' . FILENAME_CUSTOMERS . '?action=edit&cID=' . $row['customers_id'] . '&return=ric' . '">'. $row['customers_lastname'] . '</a>'?></td>
					   <td class='dataTableContent'><?php echo '<a href="' . FILENAME_CUSTOMERS . '?action=edit&cID=' . $row['customers_id'] . '&return=ric' . '">'. $row['customers_firstname'] . '</a>'?></td>
					   <td class='dataTableContent' align="left"><?php echo $row['customers_id']?></td>
					   <td class="main">
					  <?php if ($row["idcards_printed"]=='Y') {
					  			echo tep_image(DIR_WS_IMAGES . "checked.gif",IMAGE_PRINTED);
					  		} else {
					  			echo tep_image(DIR_WS_IMAGES . "unchecked.gif",IMAGE_NOT_PRINTED);
							}
					  ?>
					  </td>
					  <td class="dataTableContent" align="right"><?php  echo $row['events_group_bookings'] . $row['products_quantity']; if($row['events_group_bookings']=='Y' && $row['products_quantity']>1) echo  '<a href="'.tep_href_link(FILENAME_ORDERS,'oID='.$row['orders_id'].'&action=edit&return=ric').'">'.tep_image(DIR_WS_IMAGES . 'multi_booking.gif',IMAGE_MODIFY_GROUP).'</a>'; else echo '<a href="'.tep_href_link(FILENAME_ORDERS,'oID='.$row['orders_id'].'&action=edit&return=ric').'">' . tep_image(DIR_WS_IMAGES . 'reserve_view.gif',IMAGE_MODIFY_RESERVE) . '</a>';?></td>
                  </tr>
			<?php 
			}
			
			if ($summary) { ?>
			<tr></tr>
			<tr class='<?php echo $disp_class;?>'>
			<td class='dataTableContent' valign="top"><?php echo $row['customers_firstname'] . '&nbsp;'. $row['customers_lastname'];?></td>
			<td class='dataTableContent' valign="top"><?php  echo $row['customers_id'];?>
			</td>
						
			</tr> 
			
			<?php
			}	$i++;						
					}//while
					
			?>
				
				</table>
				</TD>
				</tr>
				<?php if (!$summary) { ?>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>
				<tr><td class="bottomLine">&nbsp;</td></tr>
				<tr>
					<td  class="main"><?php echo tep_draw_checkbox_field('select_all','','','','onClick="javascript:select_all_boxes();"') . '&nbsp;' . SELECT_ALL;?></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>	
				<tr><td colspan="2"><?php echo '<a onClick="javascript:print_selected();" href="#">' . tep_image_button('button_print.gif',IMAGE_BUTTON_PRINT) . '</a>' ;?></td></tr>
				</form>
			<?php }
			
			 }else {
					echo "<tr class='main'><Td  colspan='4' 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'); ?>