<?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;
	$unprinted=isset($input_params['unprinted']) ? true : false; 
	$resource=isset($input_params['sel_resource'])?$input_params['sel_resource']:'';
	$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"];	
	}
	
	// get resource names			
	$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"=>'',"text"=>TEXT_ALL_RESOURCE);
	//$resources_array[]=array('id'=>-1,'text'=>TEXT_NONE);
	while($row=tep_db_fetch_array($sql_result)) {
		$resources_array[]=array("id"=>$row["resource_id"],"text"=>$row["resource_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 (os.start_date>='" . $date_begin . "' and os.end_date<='" . $date_end . "')";
		if($resource=="-1"){
				$where.=" and os.resource_id='0' ";
				}
				
		if($resource>0) {
			$where.= "and os.resource_id='".$resource."' and os.resource_id!='0' ";
		} else if($resource==""){
			$where.= "and  os.resource_id>0 ";
		} 
		
		if($service>0) {
			$where.= "and se.service_id='".$service."' ";
		}
		if($unprinted){
			$where.="and c.idcards_printed!='Y'";
		}
		$sql = "select o.customers_id,sd.service_name,c.customers_firstname,c.customers_lastname,c.customers_email_address,c.idcards_printed from " . TABLE_ORDERS_PRODUCTS . " op," . TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " c, "  . TABLE_SERVICES . " se ," . TABLE_ORDERS_SERVICES . " os, " . TABLE_RESOURCES . " r, " . TABLE_SERVICES_DESCRIPTION . " sd
				where op.products_type='V' and se.service_id=sd.service_id  and os.products_id=se.service_id and se.service_id=op.products_id and o.orders_id=op.orders_id and o.customers_id=c.customers_id  " . $where . " group by o.orders_id,os.products_id order by o.customers_name";
		//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("service_id_card_" . $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_IDCARTS . '?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_SERVICES_ID_CARTS;?>?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="5">
			<!--<td class="pageHeading"><?php //echo HEADING_SERVICE_IDCARDS;?></td>!-->	
		</tr>
        <tr class="cell_bg_report_header">
		  	<TD>
			<?php 	echo tep_draw_form("f",FILENAME_SERVICES_IDCARTS);?>
			<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
				<tr>
					<td align="right"><?php echo TEXT_DATE;?></td>
					<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 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>
					</td>	
					<td align="right"><?php echo TEXT_VIEW;?></td>
					<td><?php echo tep_draw_pull_down_menu("sel_view",$view_array,$view) . '&nbsp;&nbsp;&nbsp;';?></td>
					<?php
							$all_service_array=array();
							$all_service_array = tep_get_service_array_single();
							if(sizeof($all_service_array)<=0) echo '<td colspan="2" class="main">Services are not Available</td>';
							else { ?> 
					<td align="right"><?php echo TEXT_SERVICE;?>		</td>
					<td><?php echo tep_draw_service_select_menu('sel_service',$all_service_array,$service,' id="sel_service"');	?>
				    </td>
				    	<?php }?>	
					<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 align="right"><?php echo TEXT_RESOURCE;?></td>
				<td >
				<?php echo  tep_draw_pull_down_menu("sel_resource",$resources_array,$resource) ;?></td>
				<td align="right"><?php echo TEXT_UNPRINTED_IDCARDS;?></td>
				<td><?php echo tep_draw_checkbox_field('unprinted',1,$unprinted); ?>
				</td>
				<td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif','1','3');?></td>
				</tr>
				<tr><td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','1','3');?></td></tr>
			<!-- report content -->
		</table>
		</form>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif');?></td>
		</tr>
		<?php if ($found_results) {
		echo tep_draw_form("id_card",FILENAME_SERVICES_IDCARTS);?>
		<tr>
		<td>
	   <table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr class='dataTableHeadingTitleRow'>
				<td class='dataTableHeadingTitleContent' width="30">&nbsp;</td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_FNAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_LNAME;?></td>
				<td class='dataTableHeadingTitleContent'><?php echo TEXT_CUSTOMER_ID;?></td>
				<td class='dataTableHeadingTitleContent' width="50"><?php echo TEXT_PRINTED;?></td>
			</tr>
			<tr height="10"></tr>
			<?php
					$i=1;
					mysql_data_seek($sql_result,0);
          			while($row = tep_db_fetch_array($sql_result)) {
					?>
                  <tr class='dataTableRow' height="20">
					  <td class="dataTableContent"><?php echo tep_draw_checkbox_field('customer_id',$row["customers_id"]);?></td>
					  <td class='dataTableContent'><?php echo '<a href="' . FILENAME_CUSTOMERS . '?action=edit&cID=' . $row['customers_id'] . '&return=rsid&type=ser' . '">'. $row['customers_firstname'] . '</a>'   ?></td>
					  <td class='dataTableContent'><?php echo '<a href="' . FILENAME_CUSTOMERS . '?action=edit&cID=' . $row['customers_id'] . '&return=rsid&type=ser' . '">'. $row['customers_lastname'] . '</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>
                  </tr>
			<?php 
						$i++;						
					}
			?>
				</table>
				</TD>
				</tr>
				<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;' . 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'); ?>