<?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('includes/classes/currencies.php');
  require('includes/classes/pdfTable.php');
  require(DIR_WS_CLASSES . 'split_page_results_report.php');
  define(BOX_WIDTH1,'125');
  
  // get initial parameters, try to load from the session
	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=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;
	$format=isset($input_params['sel_format'])?$input_params['sel_format']:1;
	$location=isset($input_params['sel_location'])?$input_params['sel_location']:-1;
	$services=isset($input_params['sel_service'])?$input_params['sel_service']:-1;
	$post_action = isset($input_params['post_action'])?$input_params['post_action']:"";
	$output_pdf=isset($input_params['chk_output_pdf'])?true:false;
	$output_excel=isset($input_params['chk_output_excel'])?true:false;
	$page = isset($input_params['page'])?$input_params['page']:1;

if ($post_action==""){
	$show_pending=true;
	$show_waiting=true;
	$output_pdf=true;
		$post_action="screen";
}
$search_header="";
$date_begin = $date;
$date_end = $date;	
$days= date('t',strtotime($date));
	if($view==1){
		$date_begin=isset($input_params['txtStartDate'])?tep_convert_date_raw($input_params['txtStartDate']):date('Y-m-d');
		$date_end=isset($input_params['txtEndDate'])?tep_convert_date_raw($input_params['txtEndDate']):date('Y-m-d');
	}
	else if($view==2) {
		$sql =  "select date_add('$date', interval -(dayofweek('$date')-2) day) begin, date_add('$date', interval (7-dayofweek('$date')+1) 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"];	
	}
	$date_sql=tep_db_query("select to_days('".$date_end."') - to_days('".$date_begin."') as date_diff");
		$row_date = mysql_fetch_assoc($date_sql);
		$date_diff = $row_date["date_diff"];
	
	// create header text for pdf page
	$display_header=TEXT_DAY .  ":&nbsp;&nbsp;" . $date . "&nbsp;&nbsp;&nbsp;&nbsp;";
	if ($view==1) {
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_DAY . "\t";
	} else if ($view==2){
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_WEEK . "\t";
	} else {
		$display_header.=TEXT_VIEW .  ":&nbsp;&nbsp;" . TEXT_MONTH . "\t";
	}
	//if ($instructor=="-1" && $login_groups_type!=TEXT_INSTRUCTOR_ENTRY) $display_header.=TEXT_INSTRUCTOR . ":&nbsp;&nbsp;" . TEXT_ALL_INSTRUCTORS ."\t";
	if ($location=="-1") $display_header.=TEXT_LOCATION . ":&nbsp;&nbsp;" . TEXT_ALL_LOCATIONS  ."\t";
	if ($services=="-1") $display_header.=TEXT_SERVICES . ":&nbsp;&nbsp;" . TEXT_ALL_SERVICES  ."\t";
	
	//$instructors_array = array();
	$services_array = array();
	$locations_array=array();

	$view_array=array(array("id"=>1,"text"=>TEXT_DAY),array("id"=>2,"text"=>TEXT_WEEK),array("id"=>3,"text"=>TEXT_MONTH));	
	$format_array=array(array("id"=>1,"text"=>TEXT_TEXT_FORMAT),array("id"=>2,"text"=>TEXT_CALENDAR_FORMAT));	
	
	// get location names
	 $sql = "select distinct se.service_location as service_location ,l.service_location_name as location_name from ". TABLE_SERVICES . " se," . TABLE_SERVICES_LOCATIONS ." l where l.service_location_id=se.service_location order by l.service_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["service_location"],"text"=>$row["location_name"]);
		if ($row["service_location"]==$location){
			$display_header.=TEXT_LOCATION  . ":&nbsp;&nbsp;" . $row["location_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);
	
	// Services List
	$services_option="";
//	//if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
	$sql="select se.service_id, sed.service_name from services se, services_description sed where se.service_id=sed.service_id and sed.language_id='". $languages_id . "' ";
	$sql_result = tep_db_query($sql);
	$services_array[]=array("id"=>-1,"text"=>TEXT_ALL_SERVICES);
	while($row=tep_db_fetch_array($sql_result)) {
		$services_array[]=array("id"=>$row["service_id"],"text"=>$row["service_name"]);
		if ($row["service_id"]==$services){
			$display_header.=TEXT_SERVICES  . ":&nbsp;&nbsp;" . $row["service_name"] . "\t";
		}
	}
	mysql_free_result($sql_result);
	

	$found_results=false;
	// build the output if needed

	$where = " ";
	/*if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
		$instructor=$login_id;
	}
	if($instructor!="-1") {
		$where.= "and e.events_instructor='".$instructor."' ";
	}*/ 
	if($location!="-1") {
		$where.= "and se.service_location='".$location."' ";
	}
	if($services!="-1") {
		$where.= "and se.service_id='".$services."' ";
	}
	$tem_date_begin=$date_begin;
	$tem_date_end=$date_end;
	// create query to fetch reservation details
	$sql = "select se.service_id, o.orders_id, c.customers_id, c.customers_firstname, c.customers_lastname,".
	"se.service_location, sed.service_name, sum(op.final_price) as product_price,time_format(o.start_time,'%h:%i:%s %p') as start_time, time_format(o.end_time,'%h:%i:%s %p') as end_time,  o.start_date,o.end_date, ".
	"od.customers_id from " . TABLE_ORDERS_PRODUCTS . " op," . TABLE_ORDERS_SERVICES . " o," . TABLE_CUSTOMERS . " c," . 
	 TABLE_SERVICES_DESCRIPTION . " sed, " . TABLE_SERVICES . " se  ,".TABLE_ORDERS ." od "  .
	" where op.orders_id=o.orders_id and od.orders_id=op.orders_id and o.products_id=op.products_id and se.service_id=o.products_id ".
	" and products_type='V' and sed.service_id=se.service_id and (('$date_begin'>=start_date and '$date_begin'<=end_date) or ('$date_end'>=start_date and '$date_end'<=end_date) or ('$date_begin'<=start_date and '$date_end'>=end_date)) ".$where .
	" group by o.orders_id order by c.customers_firstname,c.customers_lastname,o.start_time";
	 
	 
	if ($post_action=="screen"){
	// split the report
		$query_split = new splitPageResultsReport($page, REPORT_MAX_ROWS_PAGE, $sql, $query_split_numrows);
	}

	$sql_result=tep_db_query($sql);

	$num_rows=tep_db_num_rows($sql_result);
	if ($num_rows>0) $found_results=true;

	$currencies=new currencies();
	
	if ($found_results){
		mysql_data_seek($sql_result,0);
		if($format==1){		
		while($row = mysql_fetch_assoc($sql_result)) {
			$serviceId =  $row["service_id"]; 
			// fetch customer answer details
			$output_array[$row["start_date"]][]=array("customers_firstname"=>$row["customers_firstname"],"customers_id"=>$row["customers_id"],
									"customers_lastname"=>$row["customers_lastname"],
									"service_name"=>$row["service_name"],"start_time"=>$row["start_time"],
									"end_time"=>$row["end_time"]
									);
			} // for while
		}// for text format
		else if($format==2){ 
		set_time_limit(5);
		$output_time_array=array();
			while($row = mysql_fetch_assoc($sql_result)) {
			$serviceId =  $row["service_id"]; 
			// fetch customer answer details
			if(!in_array($row["start_time"],$output_time_array))
			$output_time_array[]=$row["start_time"]; // create time array
			$output_array[$row["start_time"]][]=array("service_id" => $row["service_id"],"customers_firstname"=>$row["customers_firstname"],"customers_id"=>$row["customers_id"],
									"customers_lastname"=>$row["customers_lastname"],
									"service_name"=>$row["service_name"],"start_time"=>$row["start_time"],
									"end_time"=>$row["end_time"],"start_date" => $row["start_date"]
									);
			} // for while
			
		
		mysql_free_result($sql_result);
		sort($output_time_array);  // sorting time array
		$morning_time_array=array();
		$evening_time_array=array();
				for($ar=0;$ar<sizeof($output_time_array);$ar++){
					$sr=substr($output_time_array[$ar],9);
					$tr=substr($output_time_array[$ar],0,2);
							
							if($sr=="AM")
								$morning_time_array[]=$output_time_array[$ar];
							else if($sr=="PM")
								//if($tr=="12")
								//	$morning_time_array[]=$output_time_array[$ar];
								//else	
								$evening_time_array[]=$output_time_array[$ar]	;
							}
							
			$time_array=array_merge($morning_time_array,$evening_time_array);
				
	}
}
	$report_filename=sprintf("services_bookings_%s_%s",$login_id,time());
	// output to pdf
	if ($post_action=="pdf"){
		generate_pdf();
		tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".pdf");
		return;
	}
	// output to excel
	if ($post_action=="excel"){
		generate_excel();
		tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".csv");
		return;
	}
?>
<!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 doReport(mode) {
		startDate=date_format(document.f.txtStartDate.value,'','Y-m-d',true);
		endDate=date_format(document.f.txtEndDate.value,'','Y-m-d',true);	
		if(startDate.getTime()>endDate.getTime()) {
			alert("<?php echo REPORT_START_AFTER_END?>");
			return;
		}
		if (mode==3){
			document.f.post_action.value="excel"
			document.f.target="_blank";
		} else if (mode==2){
			document.f.post_action.value="pdf";
			document.f.target="_blank";
		} else {
			document.f.post_action.value="screen";
			document.f.target="_self";
		}
	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>
					<TD>
						<?php echo tep_draw_form("f",FILENAME_SERVICES_BOOKINGS);?>
						<input type="hidden" name="post_action" value="screen"/>
						<table border="0" cellspacing="3" cellpadding="2" class="searchArea" width="100%">
							<tr>
								<td><?php  echo  TEXT_FORMAT . '&nbsp;';?></td>
								<td align="left"><?php  echo tep_draw_pull_down_menu("sel_format",$format_array,$format);?></td>
								<?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_START_DAY . '&nbsp;';?></TD>
								<td align="left"><?php echo tep_draw_input_field("txtStartDate",isset($date_begin)?format_date($date_begin):date('Y-m-d'),'maxlength="10" size="10"');?>
								<a href="javascript:show_calendar('f.txtStartDate',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="true"><?php  echo TEXT_END_DAY . '&nbsp;';?></td>
								<td align="left"><?php echo tep_draw_input_field("txtEndDate",isset($date_end)?format_date($date_end):date('Y-m-d'),'maxlength="10" size="10"');?>
									<a href="javascript:show_calendar('f.txtEndDate',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 valign="middle" align="right"><?php echo '<a href="javascript:doReport(1);">' . tep_image_button('button_report_search.gif', IMAGE_SEARCH_DETAILS) . '</a>'; ?></td>
							</tr>
							<tr>
								<td nowrap="true"><?php echo TEXT_LOCATION . '&nbsp;';?></td>
								<td align="left"><?php  echo tep_draw_pull_down_menu("sel_location",$locations_array,$location);;?></td>
								<td nowrap="true"><?php echo TEXT_SERVICES . '&nbsp;';?></td>
								<td align="left"><?php echo tep_draw_pull_down_menu("sel_service",$services_array,$service);;?></td>
								<td nowrap><?php echo TEXT_VIEW . '&nbsp;';?></td>
								<td align="left"><?php echo tep_draw_pull_down_menu("sel_view",$view_array,$view);?></td>
								<td valign="middle" align="right"><?php echo '<a href="javascript:doReport(2);">' . tep_image_button('button_export_pdf.gif', IMG_EXPORT_PDF) . '</a>';?></td>
							</tr>
							<tr>
								<td valign="middle" align="right" colspan="6">
								</td>
								<td valign="middle" align="right">
										<?php echo '<a href="javascript:doReport(3);">' . tep_image_button('button_export_excel.gif', IMG_EXPORT_EXCEL) . '</a>';?>
								</td>
							</tr>
							<!-- report content -->
						</table>
						</form>
					</td>
				</tr>
				<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>
				<?php }?>
				
				<?php 
					if ($found_results) { // if search found
					if ($format==1){ // for result of text format?> 
				<tr>
					<td>
						<table border="0" width="100%" cellspacing="1" cellpadding="1">
							<tr class='dataTableHeadingTitleRow'>
								<td class='dataTableHeadingTitleContent' width="20"><?php echo TEXT_ID;?></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_SERVICES;?></td>
								<td class='dataTableHeadingTitleContent' width="60"><?php echo TEXT_START;?></td>
								<td class='dataTableHeadingTitleContent' width="60"><?php echo TEXT_END;?></td>
							</tr>
							<tr height="10"></tr>
						<?php 
							$i=1;
								while ($date_begin<=$date_end){
									if(is_array($output_array[$date_begin])){?>
										<tr><td colspan="7" bgcolor="#f7f7f7"  class="dataTableContent" height="20"><?php echo '<font color="red">'. format_date($date_begin).'</font>';?></td></tr>
										<?php for($icnt=0;$icnt<sizeof($output_array[$date_begin]);$icnt++){
												$row=$output_array[$date_begin][$icnt];
										?>
												  <tr class='dataTableRow' height="20" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="javascript:location.href='<?php echo tep_href_link(FILENAME_CUSTOMERS,'cID=' . $row["customers_id"] . '&action=edit&return=rsb&type=ser');?>';">
													<td class='dataTableContent' style="cursor:hand"><?php echo $icnt+1;?></td>
													<td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_firstname']; ?> </td>
													<td class='dataTableContent' style="cursor:hand"><?php echo $row['customers_lastname'];?> </td>
													<td class='dataTableContent' style="cursor:hand"><?php echo $row['service_name']?></td>
													<td class='dataTableContent' style="cursor:hand"><?php echo $row['start_time'];?></td>
													<td class='dataTableContent' style="cursor:hand"><?php echo $row['end_time'];?></td>
												  </tr>
										<?php } // end of for
										} // end of if
									 $date_begin=date('Y-m-d',tep_dateadd(strtotime($date_begin),"day",1));
									} // end of while
					   ?>
						</table>
					</TD>
				</tr>
			<?php } // end of if ?>
			<?php if($format==2 && ($view==2 || $view==3  )){ // week & month view of calendar format ?> 
				<tr>
				<td>
				<table border="0" width="100%" cellspacing="1" cellpadding="1">
					<tr class='dataTableHeadingRow'>
						<td align="center" class="dataTableHeadingContent">Time </td>
								<?php 
									// display the week name header
									for($jcnt=1;$jcnt<=7;$jcnt++){
										echo '<td class="dataTableHeadingContent"'  . '" align="center" >' . constant("TEXT_DAY" . $jcnt) . '</td>';
									}
								?>
					</tr>
					<?php 
					
					$dcnt=1;
					while($date_begin<=$date_end){
						if ($dcnt==1 || $dcnt%7==1) echo '<tr class="dataTableRow"  bgcolor="red" style="height:auto" ><td width="12%">&nbsp;</td>';
							if($date_begin==$tem_date_begin){ //  the date starts intermediate day of the week 
								$st_cnt=date("w",strtotime($date_begin))-1;
								if($st_cnt>0){
									for($stcnt=0;$stcnt<$st_cnt;$stcnt++){
									echo '<td>&nbsp;</td>';
									$dcnt=$dcnt+1;
									} // end of for
								 } //end of if
									$flag=2;
							 } // end of if
					 ?>
						
						<Td style="font-size:9px" valign="top" width="12%" ><span ><?php echo date("d",strtotime($date_begin)); ?></span>
									</Td>
									
									
					<?php 	
					
						$dcnt=$dcnt+1;
						if($date_begin==$date_end){ // the date ends  intermediate day of the week
							$cols_cnt=date("w",strtotime($date_begin));
							if($cols_cnt>0){
								for($cocnt=$cols_cnt;$cocnt<=6;$cocnt++){
									echo '<td>&nbsp;</td>';
									$dcnt=$dcnt+1;
									} // end of for
								$flag=1;
							} // end of if
						} // end of if
						if ($dcnt % 7==1){
							echo '</tr>';
							 $ddcnt=1;
							 for($fcnt=0;$fcnt<sizeof($time_array);$fcnt++){
								if ($ddcnt==1 || $ddcnt%7==1)  echo ' <tr height="5" bgcolor="f9f9f9">';
					  ?>	
									  <td style="font-size:11px"  bgcolor="#f0f1f1">
					 <?php echo $time_array[$fcnt]; ?></td>
					 <?php 
									$tacnt=$time_array[$fcnt];
									if($sdate_begin=='') $sdate_begin=$tem_date_begin;
										if($flag==1)
											$sdate_begin=date('Y-m-d',tep_datesub(strtotime($date_begin),"day",$cols_cnt-1));
										else	
											$sdate_begin=date('Y-m-d',tep_datesub(strtotime($date_begin),"day",6));
										
									while($sdate_begin<=$date_begin){
										if(sizeof($output_array[$tacnt]>0)){
											echo '<td class="dataTableContent">';
											for($tcnt=0;$tcnt<sizeof($output_array[$tacnt]);$tcnt++){
												if($output_array[$tacnt][$tcnt]["start_date"]==$sdate_begin)
													{
													echo '<a href='.FILENAME_CUSTOMERS.'?cID='.$output_array[$tacnt][$tcnt]["customers_id"].'&action=edit&return=rsb&type=ser>'.$output_array[$tacnt][$tcnt]["customers_firstname"].' '.$output_array[$tacnt][$tcnt]["customers_lastname"].'</a>';
														if($services=="-1") 
															echo "(".$output_array[$tacnt][$tcnt]["service_name"].")";
														echo '<br>'	;
													} // end of if
												
												} //end of for
												echo '</td>';
											} //if
											else "<td>&nbsp;</td>";
												 $sdate_begin=date('Y-m-d',tep_dateadd(strtotime($sdate_begin),"day",1)); 
										} // end of while
									} //for
									$ddcnt=$ddcnt+1;
									if ($ddcnt % 7==1) echo '</tr>';
								} // if
								
							 $date_begin=date('Y-m-d',tep_dateadd(strtotime($date_begin),"day",1)); 

							} //while
						?>
								
					</table>
				</td>
			</tr>
			<?php 	} 
					// day view of calendar format
					else if($format==2 && $view==1){ ?>
			<tr>
				<td>
					<table border="0" width="100%" cellspacing="1" cellpadding="1">
						<?php	
							$rowcnt=0;
							$zcnt=0;
							 for($xcnt=0;$xcnt<sizeof($time_array);$xcnt++){
								if($xcnt==0 || ($xcnt %7  ==0)){
									echo '<tr class="dataTableHeadingRow" height="2%"><td align="center" nowrap width="10%" class="dataTableContent">Date</td>';
								}	
						?>
						<?php 
							// display the time  header 										
								echo '<td  width="12%"  class="dataTableContent"'  . '" align="center" >' .$time_array[$xcnt]. '</td>';
								$ycnt=$xcnt+1;
								if($xcnt==sizeof($time_array)-1){ // the time ends intermediate row
									for($qcnt=0;$qcnt<(7-($xcnt %7)-1);$qcnt++){
										echo '<td bgcolor="#ffffff">&nbsp;</td>';
										$ycnt=$ycnt+1;
										$flag=1;
										}	 // end of for							
								 } // end of if
										
								if($ycnt %7 ==0){
									 echo '</tr>';
									 $date_begin=$tem_date_begin;
									 while($date_begin<=$date_end){
											echo '<tr height="5" bgcolor="f9f9f9"><td class="dataTableContent"  bgcolor="#f0f1f1">'.$date_begin.'</td>';
											for($pcnt=$zcnt;$pcnt<=$xcnt;$pcnt++){
												if(sizeof($output_array[$time_array[$pcnt]])>0){
													$mcnt=0;
													echo '<td class="dataTableContent">';
													for($qcnt=0;$qcnt<sizeof($output_array[$time_array[$pcnt]]);$qcnt++){
														if($output_array[$time_array[$pcnt]][$qcnt]['start_date']==$date_begin){
														echo '<a href='.FILENAME_CUSTOMERS.'?cID='.$output_array[$time_array[$pcnt]][$qcnt]['customers_id'].'&action=edit&return=rsb&type=ser>'.$output_array[$time_array[$pcnt]][$qcnt]['customers_firstname'].' '.$output_array[$time_array[$pcnt]][$qcnt]['customers_lastname'].'</a>';
															if($services=="-1") 
																echo "(".$output_array[$time_array[$pcnt]][$qcnt]["service_name"].")";
														echo '<br>';
														} // end of if
															//echo $output_array[$time_array[$pcnt]][$qcnt]['customers_firstname'].' '.$output_array[$time_array[$pcnt]][$qcnt]['customers_lastname'].'<br>';
														
													} // end of for
														echo '</td>'	;
												} // end of if
												else
													echo '<td>&nbsp;</td>';	
											 } // end of for
											
											 echo '</tr>';
											 $date_begin=date('Y-m-d',tep_dateadd(strtotime($date_begin),"day",1)); 
						
									} //while
									$zcnt=$xcnt+1;
							  } // end of if
							
						?>
					<?php }  // end of for
						} // end of if
					} // end of if
					?>
					</table>
				</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'); 

	// function to create pdf content
	function generate_pdf(){
		global $output_array,$display_header,$found_results,$format,$date_begin,$date_end,$report_filename;
		//create inital settings
		$table=new pdfTable("A4","l");
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		
		// Add styles for tablular data
		$table->AddStyle("heading","color:#727272;bgcolor:#ffffff;font:Arial;size:18;style:B");
		$table->AddStyle("user","color:#000000;bgcolor:#f1f9fe;font:Arial;size:14;style:B;border-color:#7b9ebd");
		$table->AddStyle("subhead","color:#000000;bgcolor:#7b9ebd;font:Arial;size:10;style:B");
		$table->AddStyle("headrow","color:#000000;bgcolor:#C9C9C9;font:Arial;size:10;style:B");
		$table->AddStyle("row","color:#000000;bgcolor:#F0F1F1;font:Arial;size:10");
		$table->AddStyle("query","color:#000000;bgcolor:#FFFFFF;font:Arial;size:11");
		$table->AddStyle("subrow","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10");
		$table->AddStyle("subrowhead","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10;style:B");

		$table->headers["text"]=$display_header;
		$table->headers["style"]="query";
		$table->headers["height"]=12;
		$table->headers["width"]="100%";
		$table->headers["cols"]=5;

		//define column width details
		$widths[0]=40;
		//$widths[3]=95;
		$widths[4]=70;
		$widths[5]=70;
		/*$widths[6]=50;
		$widths[7]=90;
		$widths[8]=50;
		$widths[9]=15;*/


		$temp_width=$table->width-($widths[0]+$widths[4]+$widths[5]);
		$widths[1]=$temp_width/3;
		$widths[2]=$temp_width/3;
		$widths[3]=$temp_width/3;
		$cols=array();
		
		// define table header columns
		$cols[]=array("text"=>TEXT_ID,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_FIRST_NAME,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>TEXT_LAST_NAME,"width"=>$widths[2],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Service,"width"=>$widths[3],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>Start,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>End,"width"=>$widths[5],"align"=>"L","style"=>"headrow","valign"=>"M");
		//$cols[]=array("text"=>TEXT_END,"width"=>$widths[6],"align"=>"L","style"=>"headrow","valign"=>"M");
		//$cols[]=array("text"=>TEXT_PAID,"width"=>$widths[7],"align"=>"C","style"=>"headrow","valign"=>"M");
	//	$cols[]=array("text"=>TEXT_STAT,"width"=>$widths[8],"align"=>"R","style"=>"headrow","valign"=>"M");
	//	$cols[]=array("text"=>TEXT_RENEWAL,"width"=>$widths[9],"align"=>"L","style"=>"headrow","valign"=>"M");
		
		$table->tableheaders["text"]=$cols;
		unset($cols);
		
		//set_time_limit(5);
		if ($found_results){
		
		if ($format==1){
		$i=1;
		while ($date_begin<=$date_end){
		
		if(is_array($output_array[$date_begin])){
		
		$cols=array();
		$cols[]=array("text"=>$date_begin,"width"=>$widths[0]+$widths[1]+$widths[2]+$widths[3]+$widths[4],"align"=>"L","style"=>"subrowhead","valign"=>"M");
			//$cols[]=array("text"=>$date_begin,"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
 			//$date_begin
			$table->OutputRow($cols,15);
				unset($cols);
		
		for($icnt=0;$icnt<sizeof($output_array[$date_begin]);$icnt++){
		$row=$output_array[$date_begin][$icnt];
		// output content details
		//for($icnt=0;$icnt<sizeof($output_array);$icnt++){
			//$row=&$output_array[$icnt];
			$cols=array();
			$cols[]=array("text"=>$i,"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['customers_firstname'],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['customers_lastname'],"width"=>$widths[2],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row['service_name'],"width"=>$widths[3],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["start_time"],"width"=>$widths[4],"align"=>"L","style"=>"row","valign"=>"M");
			$cols[]=array("text"=>$row["end_time"],"width"=>$widths[5],"align"=>"L","style"=>"row","valign"=>"M");
		
			$table->OutputRow($cols,15);
			unset($cols);
			$i++;
		}
		}
		
		 $date_begin=date('Y-m-d',tep_dateadd(strtotime($date_begin),"day",1));
		}
		
		} else {
			$cols[]=array("text"=>REPORT_NO_RESULTS,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
		}
		}
		// create pdf file
		$table->Render("$report_filename.pdf");
	}
	// function to create excel data
	function generate_excel(){
		global $output_array,$display_header,$found_results,$format,$date_begin,$date_end,$report_filename;
		
		$result.=TEXT_ID . "," . TEXT_FIRST_NAME . "," . TEXT_LAST_NAME . "," .
				 TEXT_SERVICES . "," . TEXT_START . "," . 
				 TEXT_END .  "\n";
		if ($found_results){
		
		if ($format==1){
		$i=1;
		while ($date_begin<=$date_end){
		
		if(is_array($output_array[$date_begin])){
		
		//dete 
		$result.=$date_begin;
		
	for($icnt=0;$icnt<sizeof($output_array[$date_begin]);$icnt++){
		$row=$output_array[$date_begin][$icnt];
			$result.="\n".$row["no"] . "," . $row['customers_firstname']. "," . $row['customers_lastname'] . "," .
					 $row['service_name']. "," . $row['start_time'] . "," .
				 	 $row['end_time'];
					
		}
		} 
		 $date_begin=date('Y-m-d',tep_dateadd(strtotime($date_begin),"day",1));
		}
		}
		}
		else {
		$result.=REPORT_NO_RESULTS . "\n";
		}
		tep_write_text_file("$report_filename.csv",$result);
	}


?>