<?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 . 'services_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']):date('Y-m-d');
	$view=isset($input_params['sel_view'])? $input_params['sel_view'] : 1;
	$summary=isset($input_params['summary'])? true : false;
	$unprinted=isset($input_params['unprinted']) ? true : false; 
	$resource=isset($input_params['sel_resource'])?$input_params['sel_resource']:-1;
	$service=isset($input_params['sel_service'])?$input_params['sel_service']:-1;
	$resources_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"];	
	}

	
	
	$sql = "select  r.resource_id, rd.resource_name as resource_name from ".TABLE_RESOURCES." r," .TABLE_RESOURCES_DESCRIPTION. " rd where r.resource_id=rd.resource_id order by resource_id";
	$sql_result = tep_db_query($sql);
	$num_rows = tep_db_num_rows($sql_result);
	$resources_array[]=array("id"=>-1,"text"=>TEXT_ALL_RESOURCE);
	while($row=tep_db_fetch_array($sql_result)) {
		$resources_array[]=array("id"=>$row["resource_id"],"text"=>$row["resource_name"]);
	}
	//print_r ($resources_array);
	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 o.date_purchased>='" . $date_begin . "' and o.date_purchased<='" . $date_end . "'";
		if($resource>0) {
			$where.= " and os.resource_id='".$resource."' ";
		} 
		if($service>0) {
			$where.= "and s.service_id='".$service."' ";
		}
		if($unprinted){
			$where.=" and o.ticket_printed!='Y'";	
		}

		// get the ticket details to be shown
		$sql="SELECT os.start_date,s.service_id,o.orders_id,o.customers_id,c.customers_firstname,c.customers_lastname,o.ticket_printed from " . 
					TABLE_ORDERS . " o, " . TABLE_ORDERS_SERVICES . " os," . TABLE_ORDERS_PRODUCTS . " op, " .  TABLE_CUSTOMERS . " c, " . TABLE_SERVICES . " s " .
					" where op.products_type='V' and os.products_id=s.service_id and o.orders_id=op.orders_id and op.products_id=s.service_id and c.customers_id=o.customers_id  " . $where . 
					" order by o.orders_id,op.products_name,s.service_date_available";

		$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_service=0;
			while($order_result=tep_db_fetch_array($sql_result)){
				$services_id=$order_result["service_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_service=0;
					$cnt=1;
				}
				if ($prev_service!=$services_id){
					if (!isset($details[$services_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]["location_name"]=$service_result["location_name"];
						$details[$services_id]["service_name"]=$service_result["service_name"];
						//$details[$services_id]["start_time"]=$service_result["service_start_time"];
						//$details[$services_id]["end_time"]=$service_result["service_end_time"];
					}
					if ($prev_service>0) $row=$row+1;
					$prev_service=$services_id;
					$cnt=1;
				}
				$orders[$row]['service_id']=$services_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"];
				$ticket_numbers[$services_id][$orders_id . "-" . $order_result["customers_id"]]=1;
				$orders[$row]['dates'].=format_date($order_result["start_date"]) .",";
				$cnt=$cnt+1;
				if ($cnt%3==1) {
					$orders[$row]['dates'].='<br>';
				}
				
			}
		}
		tep_delete_temp_files("service_tickets_" . $login_id);
?>
<!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(DIR_WS_INCLUDES."date_format_js.php")?>
<script language="JavaScript">

	function doaction(mode) {
		if (mode==1) {
			document.f.action="<?php echo FILENAME_SERVICES_TICKETS?>";
		}
		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="7">
			<!--<td class="pageHeading"><?php //echo HEADING_SERVICE_TICKETS;?></td>!-->	
		</tr>
        <tr>
		  	<TD>
			<?php 	echo tep_draw_form("f",FILENAME_SERVICES_TICKETS);?>
			<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="true">
						<?php 	echo TEXT_DATE . '&nbsp;' . 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 '&nbsp;&nbsp;' . TEXT_VIEW . '&nbsp;' . tep_draw_pull_down_menu("sel_view",$view_array,$view) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';



								$all_service_array=array();
								$all_service_array = tep_get_service_array_single();
								if(sizeof($all_service_array)<=0) echo 'Services are not Available';
								else echo TEXT_SERVICE . '&nbsp;' . tep_draw_service_select_menu('sel_service',$all_service_array,$service,' id="sel_service"');
						?>

				    </td>
					<td valign="middle" align="right" rowspan="2">

						<?php echo '<a href="javascript:doaction(1);">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_TICKETS) . '</a>'; ?>
					</td>
				</tr>
				<tr>
					<td nowrap="true">
						<?php
							 echo '&nbsp;&nbsp;' . TEXT_RESOURCE . '&nbsp;' . tep_draw_pull_down_menu("sel_resource",$resources_array,$resource) .'&nbsp;'; 	
						    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_TICKETS;
						?>
					</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_SERVICE_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_FIRST_NAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_LAST_NAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_SERVICE_NAME;?></td>
				<td class='dataTableHeadingTitleContent' width="230"><?php echo TEXT_SERVICE_DATE;?></td>
				<td class='dataTableHeadingTitleContent' width="60"><?php echo TEXT_TICKET_ID;?></td>
				<td class='dataTableHeadingTitleContent' width="40"><?php echo TEXT_PRINTED;?></td>
			</tr>
			<tr height="10"></tr>
			<?php
          			for($icnt=0;$icnt<count($orders);$icnt++){
						$order=$orders[$icnt];
						$service=$details[$order["service_id"]];
						if($disp_class=='dataTableRow')
							$disp_class='dataTableRowOver';
						else
							$disp_class='dataTableRow';
			?>
				<tr class='dataTableRow'><td colspan="7"><?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=rstic&type=ser') . '">' . $order['firstname'] . '</a>'; ?></td>
					  <td class='dataTableContent' valign="top"><?php echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS,'cID=' . $order['customers_id'] .'&action=edit&return=rstic&type=ser') . '">' . $order['lastname'] . '</a>'; ?></td>
					  <td class='dataTableContent' valign="top"><?php echo '<a href="' . tep_href_link(FILENAME_ORDERS,'oId=' .$order['orders_id'] . '&return=rstic&page=1&type=ser&action=edit&oID=' . $order['orders_id']) . '">' . $service['service_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>
				</tr>
			<?php 
					} // while
				}  else { //if  summary
			?>
				
				<tr class='dataTableHeadingRow'>
					<td class='dataTableHeadingContent'><?php echo TEXT_SERVICE_NAME;?></td>
					<td class='dataTableHeadingContent' width="230"><?php echo TEXT_TICKET_NUMBERS;?></td>
				</tr>
				<?php reset($ticket_numbers);
					while(list($service,$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[$service]["service_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 class="bottomLine">&nbsp;</td></tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></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'); ?>