<?php
/*
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/



// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	
  require('includes/application_top.php');
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SERVICES_LIST);
  require(DIR_WS_CLASSES . "object_info.php");
     $category_id=isset($HTTP_POST_VARS['category_id'])?(int)$HTTP_POST_VARS['category_id']:'0';
     $search_start_date=tep_convert_format_date(isset($HTTP_POST_VARS['start_date'])?tep_db_prepare_input($HTTP_POST_VARS['start_date']):date('Y-m-d'),'Y-m-d');
	 $search_start_time=isset($HTTP_POST_VARS['start_time'])?tep_db_prepare_input($HTTP_POST_VARS['start_time']):'';
	 $search_end_date=tep_convert_format_date(isset($HTTP_POST_VARS['end_date'])?tep_db_prepare_input($HTTP_POST_VARS['end_date']):date('Y-m-d'),'Y-m-d');
	 $search_end_time=isset($HTTP_POST_VARS['end_time'])?tep_db_prepare_input($HTTP_POST_VARS['end_time']):'';
	 $s_time=split(":",$search_start_time);
	 $e_time=split(":",$search_end_time);
	 $s_hour=$s_time[0];
	 $s_minute=$s_time[1];
	 $s_session=$s_time[2];
	 $e_hour=$e_time[0];
	 $e_minute=$e_time[1];
	 $e_session=$e_time[2];
	 $service_list_array=array();
	 //$service_search=tep_db_query("select se.service_id,sd.service_name,se.service_costs,se.service_start_time,se.service_end_time,se.quantity,se.service_tax_class_id,se.booking_type,se.max_bookings from ".TABLE_SERVICES." se LEFT JOIN ".TABLE_SERVICES_DESCRIPTION." sd on se.service_id=sd.service_id  Left Join ".TABLE_SERVICES_TO_SERVICES_CATEGORIES." seTsc on seTsc.service_id=se.service_id where seTsc.services_categories_id='".$category_id."' and se.service_status='1' and ((se.booking_type='D' and ((se.service_start_time>='".$search_start_time."' and se.service_end_time>='".$search_end_time."') or (se.service_start_time>='".$search_start_time."' and se.service_end_time<='".$search_end_time."') or (se.service_start_time<='".$search_start_time."' and se.service_end_time>='".$search_end_time."') or (se.service_start_time<='".$search_start_time."' and se.service_end_time<='".$search_end_time."')))or (se.booking_type='H' and (se.service_start_time<='".$search_start_time."' and  se.service_end_time>='".
	$service_search=tep_db_query("select se.service_id,sd.service_name,se.service_costs,se.quantity,se.service_tax_class_id,se.booking_type,se.max_bookings from ".TABLE_SERVICES." se LEFT JOIN ".TABLE_SERVICES_DESCRIPTION." sd on se.service_id=sd.service_id  Left Join ".TABLE_SERVICES_TO_SERVICES_CATEGORIES." seTsc on seTsc.service_id=se.service_id where seTsc.services_categories_id='".tep_db_input($category_id)."' and se.service_status='1' and se.booking_type='D'".
	"  group by se.service_id");
	 while($search_result=tep_db_fetch_array($service_search)){
	      $quantity=tep_db_query("SELECT if(sum(op.products_quantity)>=se.quantity,'not_found','found') as service_status from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " .TABLE_ORDERS_SERVICES . " os, " .  TABLE_SERVICES . " se where os.products_id='".tep_db_input($search_result['service_id'])."' and op.products_type='V' and op.products_id=se.service_id and o.orders_id=op.orders_id and op.products_id=os.products_id  and o.orders_id=os.orders_id and (o.orders_status>1 or date_add(o.date_purchased,interval se.pending_period day)>'now()') and ((os.start_date>='".$search_start_date."' and os.start_date<='".$search_end_date."') or (os.end_date>='".$search_start_date."' and os.end_date<='".$search_end_date."')) group by op.products_id");
		  $result_quantity=tep_db_fetch_array($quantity);
		  if($result_quantity['service_status']!='not_found'){
		    $service_list_array[]=array('service_id'=>$search_result['service_id'],'service_name'=>$search_result['service_name'],'service_tax'=>$search_result['service_tax_class_id'],'service_costs'=>$search_result['service_costs'],'service_quantity'=>$search_result['quantity'],'booking_type'=>$search_result['booking_type'],'max_bookings'=>$search_result['max_bookings']);
	      }
	  }
   $action=isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'';
   if($action=='do_service'){
     tep_redirect(tep_href_link(FILENAME_SERVICES_INFO,'service_id='.(int)$HTTP_POST_VARS['service_ids'].'&option_id='.tep_db_prepare_input($HTTP_POST_VARS['option_id']).'&category_id='.$category_id));
   }
   $category_name=tep_service_categories_name($category_id,'name');
   $content = CONTENT_SERVICES_LIST;
   $javascript = 'popup_window.js';

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>