<?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 the session for previous values
  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());
				}
			}

	$action=isset($HTTP_POST_VARS['post_action']) ? $HTTP_POST_VARS['post_action']:'';
	$date_begin=(isset($HTTP_POST_VARS["txt_date_begin"])?tep_convert_date_raw($HTTP_POST_VARS["txt_date_begin"]):'');
	$date_end=(isset($HTTP_POST_VARS["txt_date_end"])?tep_convert_date_raw($HTTP_POST_VARS["txt_date_end"]):getServerDate());
	$view=isset($input_params['sel_view'])? $input_params['sel_view'] : 1;
	$summary=isset($input_params['summary'])? true : false;
	$unprinted=isset($input_params['unprinted']) ? 'Y' : ''; 
	$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();
	
	if ($action=="" && $unprinted==""){
		$pdf_output=true;
		$action="screen";
		$unprinted='Y';
	}
	if ($date_begin==""){
		$date_begin=getServerDate();
		$sql =  "select date_add('$date_begin', interval -1 month) start_date";	
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["start_date"];
		
	}
	// 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_CONDITION_STATUS . $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);
	$found_results=false;
	
	// build the output if needed
	$where = " and o.date_purchased>='" . $date_begin . "' and o.date_purchased<='" . $date_end . "'";
	
		if($instructor>0) {
			$where.= " and e.events_instructor='".$instructor."' ";
		} 
		if($event>0) {
			$where.= "and e.events_id='".$event."' ";
		}
		if($unprinted){
			$where.=" and o.ticket_printed!='Y'";	
		}
		if($pending){
		$where.=" and o.ticket_printed='N'";
		}
		// get the ticket details to be shown
		$sql="SELECT distinct s.start_date,s.events_id,o.orders_id,c.customers_id,LTRIM(c.customers_firstname) as customers_firstname,LTRIM(c.customers_lastname) as customers_lastname,o.ticket_printed,e.events_group_bookings,op.products_quantity from " . 
					TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_CUSTOMERS . " c, " . TABLE_EVENTS . " e " .
					" where op.products_type='E' and op.events_type='R' and o.orders_id=op.orders_id and op.products_id=s.sessions_id and c.customers_id=o.customers_id and s.events_id=e.events_id " . $where . 
					" order by c.customers_id,o.orders_id,op.products_name,s.start_date,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;
		
		if ($found_results){
			$details=array();
			$orders=array();
			$cnt=1;
			$row=0;
			$prev_order=0;
			$prev_event=0;
			
			$session_dates = "";
			$pre_cus_id="";
			$date = array(); 
			
			while($order_result=tep_db_fetch_array($sql_result)){
				$events_id=$order_result["events_id"];
				$orders_id=$order_result["orders_id"];
				if ($prev_order!=$orders_id){
					if ($prev_order>0) $row=$row+1;
					$prev_order=$order_result["orders_id"];
					$prev_event=0;
					$cnt=1;
				}
				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]["location_name"]=$event_result["location_name"];
						$details[$events_id]["events_name"]=$event_result["events_name"];
						$details[$events_id]["start_time"]=$event_result["events_start_time"];
						$details[$events_id]["end_time"]=$event_result["events_end_time"];
					}
					if ($prev_event>0) $row=$row+1;
					$prev_event=$events_id;
					$cnt=1;
				}
				$orders[$row]['events_id']=$events_id;
				$orders[$row]['firstname']=$order_result["customers_firstname"];
				$orders[$row]['lastname']=$order_result["customers_lastname"];
				$orders[$row]['customers_id']=$order_result["customers_id"];
				$orders[$row]['orders_id']=$order_result["orders_id"];
				$orders[$row]['ticket_printed']=$order_result["ticket_printed"];
				$orders[$row]['events_group_bookings']=$order_result["events_group_bookings"];
				$orders[$row]['products_quantity']=$order_result["products_quantity"];
				$ticket_numbers[$events_id][$orders_id . "-" . $order_result["customers_id"]]=1;
				
				if($pre_cus_id!=$order_result["customers_id"]){
				   $pre_cus_id=$order_result["customers_id"];
				   $session_dates=$order_result["start_date"].',';	
				   $date[$order_result['customers_id']] =$session_dates; 
				  }else{
				   $session_dates.=$order_result["start_date"]. ",";
				   $date[$order_result['customers_id']] =$session_dates; 	
				}
				//$orders[$row]['dates']=split(",",$date[$order_result['customers_id']]);
				//	$orders[$row]['dates'].=format_date($date);
				
				$orders[$row]['dates'].=format_date($order_result["start_date"]) .",";
				$cnt=$cnt+1;
				if ($cnt%3==1) {
					$orders[$row]['dates'].='<br>';
				}
			}
		} 
		tep_delete_temp_files("events_tickets_" . $login_id);
		tep_delete_temp_files("ticket_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>
<?php include('includes/date_format_js.php');?>
<script language="JavaScript">
		function doSubmit() {
		startDate=date_format(document.f.txt_date_begin.value,'','',true);
		endDate=date_format(document.f.txt_date_end.value,'','',true);	
		if(startDate>endDate) {
			alert("<?php echo REPORT_START_AFTER_END?>");
			return;
		}
		document.f.post_action.value="screen";
		document.f.submit();
		}	
</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_TICKETS;?></td>!-->	
		</tr>
        <tr >
		  	<TD>
			<?php 	echo tep_draw_form("f",FILENAME_EVENT_TICKET);
				echo '<input type="hidden" name="post_action" value="">';
			?>
			<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
				<tr>
					<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
    	  <td nowrap><?php echo TEXT_START_DATE . "&nbsp;&nbsp;" . tep_draw_input_field('txt_date_begin',format_date($date_begin),' size="10"');?>
     	<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>
		   &nbsp;&nbsp;
	  <?php echo TEXT_END_DATE . "&nbsp;&nbsp;" . tep_draw_input_field('txt_date_end',format_date($date_end),' size="10"');?>
     	<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>
		   <?php echo tep_draw_checkbox_field('summary','1',$summary)  . '&nbsp;' . TEXT_SUMMARY .'&nbsp;&nbsp;&nbsp;&nbsp;';
								echo tep_draw_checkbox_field('unprinted',1,($unprinted=='Y')?true:false)  . '&nbsp;' . TEXT_UNPRINTED_TICKETS .'&nbsp;&nbsp;&nbsp;&nbsp;';
								echo tep_draw_checkbox_field('pending','1',$pending)  . '&nbsp;' . TEXT_PENDING_TICKETS;?>
				    </td>
					<td valign="middle" align="right" rowspan="2">
						<?php echo '<a href="javascript:doSubmit();">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_TICKETS) . '</a>'; ?>
					</td>
				</tr>
				<tr>
					<td nowrap="true">
						<?php 	echo TEXT_INSTRUCTOR . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("sel_instructor",$instructors_array,$instructor) .'&nbsp;&nbsp;&nbsp;&nbsp;';	
						?>
						<?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('sel_event',$all_events_array,$event,' id="sel_event"');
						?>
					</td>
				</tr>
				<tr>
				</tr>
			<!-- report content -->
		</table>
		</form>
		</td>
		</tr>
		<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
		<?php if ($found_results) {?>
		<script language="JavaScript">
			function print_selected(){
				var element=document.ticket.order_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_TICKET;?>?oID="+content,"test");
					return;	
				}
				alert("<?php echo ERR_SELECT_TICKET;?>");
			}
			function select_all_boxes(){
				var select_all=document.ticket.select_all;
				var order_id=document.ticket.order_id;
				if (order_id[0]){
					for (icnt=0;icnt<order_id.length;icnt++){
						order_id[icnt].checked=select_all.checked;
					}
				} else {
					order_id.checked=select_all.checked;
				}
			}
		</script>
		<form name="ticket">
		<tr>
		<td>
	   <table border="0" width="100%" cellspacing="0" cellpadding="0">
	   		<?php if (!$summary) { ?>
			<tr class='dataTableHeadingTitleRow'>
				<td class='dataTableHeadingTitleContent' width="20">&nbsp;</td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_LAST_NAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_FIRST_NAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_EVENT_NAME;?></td>
				<td class='dataTableHeadingTitleContent' width="230"><?php echo TEXT_EVENT_DATE;?></td>
				<td class='dataTableHeadingTitleContent' width="60"><?php echo TEXT_TICKET_ID;?></td>
				<td class='dataTableHeadingTitleContent' width="40"><?php echo TEXT_PRINTED;?></td>
				<td class='dataTableHeadingTitleContent' width="40">&nbsp;</td>
			</tr>
			<tr height="10"></tr>
			<?php 
          			for($icnt=0;$icnt<count($orders);$icnt++){
						$order=$orders[$icnt];
						$event=$details[$order["events_id"]];
						 if($disp_class=='dataTableRow')
						 	$disp_class='dataTableRowOver';
						  else
						  	$disp_class='dataTableRow';
			?>
				<tr class='dataTableRow'><td colspan="8"><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td></tr>
                  <tr class='<?php echo $disp_class;?>'>
				  	  <td class='dataTableContent' valign="top"><?php echo tep_draw_checkbox_field('order_id',$order['orders_id']);?></td>
					  <td class='dataTableContent' valign="top"><?php echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS,'cID=' . $order['customers_id'] .'&action=edit&return=ret') . '">' . $order['lastname'] . '</a>'; ?></td>
					  <td class='dataTableContent' valign="top"><?php echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS,'cID=' . $order['customers_id'] . '&action=edit&return=ret') . '">' . $order['firstname'] . '</a>'; ?></td>
					  <td class='dataTableContent' valign="top"><?php echo '<a href="' . tep_href_link(FILENAME_ORDERS,'oId=' .$order['orders_id'] . '&return=ret&page=1&action=edit&oID=' . $order['orders_id']) . '">' . $event['events_name'] . '</a>'; ?></td>
					  <td class='dataTableContent' valign="top"><?php echo $order["dates"];?></td>
					  <td class='dataTableContent' valign="top"><?php echo $order["orders_id"] . "-" . $order["customers_id"];?></td>
					  <td class='dataTableContent' align="center" valign="top">
					  <?php if ($order['ticket_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="center" valign="top"><?php
							if($order['events_group_bookings']=='Y' && $order['products_quantity']>1)  echo '<a href="'.tep_href_link(FILENAME_ORDERS,'oID='.$order['orders_id'].'&action=edit&return=ret').'">'.tep_image(DIR_WS_IMAGES . 'multi_booking.gif',TEXT_GROUP_BOOKING).'</a>';?>
					  </td>
				</tr>
			<?php 
					} // while
				}  else { //if  summary
			?>
				
				<tr class='dataTableHeadingTitleRow'>
					<td class='dataTableHeadingTitleContent'><?php echo TEXT_EVENT_NAME;?></td>
					<td class='dataTableHeadingTitleContent' width="230"><?php echo TEXT_TICKET_NUMBERS;?></td>
				</tr>
				<?php reset($ticket_numbers);
					while(list($event,$value)=each($ticket_numbers)){
						reset($value);
						if($disp_class=='dataTableRow')
						 	$disp_class='dataTableRowOver';
						  else
						  	$disp_class='dataTableRow';
				?>
				<tr class='dataTableRow'><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td></tr>
					<tr class='<?php echo $disp_class;?>'>
						<td class='dataTableContent' valign="top"><?php echo $details[$event]["events_name"];?></td>
						<td class='dataTableContent' valign="top">
						<?php while(list($ticket_no,)=each($value)){ 
									echo $ticket_no . ", ";
							  } // while ticket_no ?>
						</td>
					</tr>
				<?php
					} // while event	
				} // if summary
				?>
				 </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;' . TEXT_SELECT_ALL;?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td></tr>
			<tr>
				<td class="main"><?php echo '<a onClick="javascript:print_selected();" href="#">' .tep_image_button('button_print.gif',IMAGE_PRINT);?></td>
			</tr>
	 		</form>
	<?php } 
		}	else {
					echo "<tr class='main'><Td  colspan='7' 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'); ?>
