<?php
	/*
	  osCommRes, Services Online
	  http://www.oscommres.com
	  Copyright (c) 2005 osCommRes
	  Released under the GNU General Public License
	*/
	// Set flag that this is a parent file
	defined('_FEXEC') or die();
	$error="";
	$where="";
	$reserved_quantity="";
	$navigation=isset($HTTP_POST_VARS['navigation'])?tep_db_prepare_input($HTTP_POST_VARS['navigation']):'';
	$step=isset($HTTP_POST_VARS['step'])?(int)$HTTP_POST_VARS['step']:2;
	$mode=isset($HTTP_GET_VARS['mode'])?tep_db_prepare_input($HTTP_GET_VARS['mode']):'';
	$id=isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:0;
	$start_date=isset($HTTP_POST_VARS['start_date'])?tep_db_prepare_input($HTTP_POST_VARS['start_date']):'';
	$start_time=isset($HTTP_POST_VARS['start_time'])?tep_db_prepare_input($HTTP_POST_VARS['start_time']):'';
	$start_times=isset($HTTP_POST_VARS['start_times'])?tep_db_prepare_input($HTTP_POST_VARS['start_times']):'';
	$end_times=isset($HTTP_POST_VARS['end_times'])?tep_db_prepare_input($HTTP_POST_VARS['end_times']):'';
	$splt_date=split(" ",$end_times);
	$end_date=$splt_date[0];
	$end_time=$splt_date[1];
	$resource_id=isset($HTTP_POST_VARS['resource_ids'])?(int)$HTTP_POST_VARS['resource_ids']:"0";
	$resource_costs=isset($HTTP_POST_VARS['resource_costs'])?tep_db_prepare_input($HTTP_POST_VARS['resource_costs']):0;
	$include_resource=isset($HTTP_POST_VARS['include_resource'])?tep_db_prepare_input($HTTP_POST_VARS['include_resource']):'N';
	$sel_quantity=isset($HTTP_POST_VARS['ser_quantity'])?(int)$HTTP_POST_VARS['ser_quantity']:1;
	$quantity=$sel_quantity;
	if($start_time=='24:00') $start_time="12:00";
	$service_id=isset($HTTP_POST_VARS['service_name'])?(int)$HTTP_POST_VARS['service_name']:0;

	if($service_id>0) $service_info=get_services_info((int)$service_id,(int)$languages_id);
	if($service_info['booking_type']=='D'){
		$splt_stime=split(" ",$start_times);
		$start_date=$splt_stime[0];
		$start_time=$splt_stime[1];
	}
	$attribute_array=array();
	// get service details  
	$booking_type=$service_info['booking_type'];
	$include_resource_price=$service_info['include_resource_price'];
	$service_quantity=(($service_info['include_resource']=='Y')?1:$service_info['quantity']);
	if(!tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time) && $service_info['include_resource']=='N')
		$error=tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time,'error');
	$resource_query="";
	if($service_info['include_resource']=='Y' || $error) {
		$reserved_resource_id="";
		if($service_id){ 
		//get resource details
		    $start_time_split=split(":",$start_time);
			$start_time=sprintf("%02d:%02d",$start_time_split[0],$start_time_split[1]);
	   //get resource details	
			 $res_num_rows=0;
			 if(!$reserved_resource_id) $reserved_resource_id=0;
			 $resource_query=tep_db_query("select red.resource_name,red.resource_description,re.resource_id,re.resource_costs,re.resource_tax_class_id from " . TABLE_RESOURCES . " re, " . TABLE_RESOURCES_DESCRIPTION . " red," .  TABLE_SERVICES_TO_SERVICES_RESOURCES . " seTre where seTre.service_id='" . $service_id . "' and re.resource_id not in(".$reserved_resource_id.")  and red.language_id='".(int)$languages_id."' and seTre.service_resource_id=re.resource_id and re.resource_id=red.resource_id order by red.resource_name");			 
			 $res_num_rows=tep_db_num_rows($resource_query);
			 if(!$res_num_rows)$error=ERR_TIME_ALREADY_RESERVED;
			}
			$quantity=1;
		}
		$products_id=get_details('resource_id','orders_edit_services',"start_date='".$start_date."' and end_date='".$end_date."' and start_time='".$start_time."' and end_time='".$end_time."' and products_id='".(int)$service_id."' and resource_id='".(int)$resource_id."'",$list=true);
		if($products_id && $mode!='edit'){
		while(list($key,$value)=each($products_id)){
		  if($products_id[$key] && $service_info['include_resource']=='Y')
			$mode="";
		  }
		}
	if ($step>4){ 
		if($service_id) $service_info=get_services_info((int)$service_id,(int)$languages_id);
		$sInfo=new objectInfo($service_info);
		$AddedOptionsPrice = 0;
		if(isset($HTTP_POST_VARS['option_ids'])){
			$option_array=split(",",tep_db_prepare_input($HTTP_POST_VARS['option_ids'])); 
			while(list($key,$value)=each($option_array)){
				$attribute_split=split("-",$value);
				if($attribute_split[0])		$attribute_array[].=$attribute_split[0] .'#'.$attribute_split[1];
			}
		}
		
		if(isset($attribute_array)){
		 for($at=0;$at<=sizeof($attribute_array);$at++){
				$splt=split("#",$attribute_array[$at]);
				$attrib_id=$splt[0];
				$option_id=$splt[1];		
				$result = tep_db_query("SELECT * FROM " . TABLE_SERVICES_ATTRIBUTES . " sa LEFT JOIN " . TABLE_SERVICES_OPTIONS . " so ON so.services_options_id=sa.options_id LEFT JOIN " . TABLE_SERVICES_OPTIONS_VALUES . " sov ON sov.services_options_values_id=sa.options_values_id WHERE sa.service_id='" .(int)$service_id. "' and so.services_options_id= '" .(int)$attrib_id . "' and sov.services_options_values_id= '" . (int)$option_id . "' and sov.language_id='" . (int)$languages_id . "' and so.language_id='" . (int)$languages_id . "'");
				$row = tep_db_fetch_array($result);
					if($row){
						extract($row, EXTR_PREFIX_ALL, "opt");
						$AddedOptionsPrice += $opt_options_values_price;
						$option_value_details[$opt_services_options_id][$opt_services_options_values_id] = array ("options_values_price" => $opt_options_values_price);
						$services_options_names[$opt_services_options_id] = $opt_services_options_name;
						$services_options_values_names[$opt_services_options_values_id] = $opt_services_options_values_name;
		  			}
		  }
		}
		$order_detail=new order($oID);
		$CountryID = tep_get_country_id($order_detail->billing["country"]);
		$ZoneID = tep_get_zone_id($CountryID, $order_detail->billing["state"]);
		$product_tax = tep_get_tax_rate($sInfo->service_tax_class_id, $CountryID, $ZoneID);
		$product_type='V';
		$product_price=$sInfo->service_costs;
		if($resource_id && $sInfo->include_resource_price=='Y') $product_price=$resource_costs;
		// get loading price
		$loading_price_dates=get_loading_array((int)$service_id);
		$sInfo=new objectInfo($service_info);
		// find the final price by calculating the total time
		$loading_dates=array('loading_price_dates'=>$loading_price_dates,'start_date'=>$start_date,'end_date'=>$end_date,'start_time'=>$start_time,'end_time'=>$end_time);
		
		if($resource_id && $sInfo->include_resource_price=='Y'){
			$product_tax=tep_get_tax_rate(tep_get_resource('resource_tax_class_id',tep_db_prepare_input($HTTP_POST_VARS['resource_ids']),(int)$languages_id), $CountryID, $ZoneID);
			$final_price=tep_calc_service_cost($service_info,$start_date . " " . $start_time,$end_date . " " . $end_time,$AddedOptionsPrice,$loading_dates);
		}else{
			$final_price=tep_calc_service_cost($service_info,$start_date . " " . $start_time,$end_date . " " . $end_time,$AddedOptionsPrice,$loading_dates);			
		}
		//get resource name	 
		$resource_name="";
	 	if($resource_id)	$resource_name=tep_get_resource('resource_name',(int)$resource_id,(int)$languages_id);
	 	tep_session_register('services');
		$services=new services();
		$services->start_date=$start_date;
		$services->start_time=$start_time;
		$services->end_date=$end_date;
		$services->end_time=$end_time;
		$services->service_id=$service_id;
		$services->resource_id=$resource_id;
		$services->attributes=$attribute_array;
		/*if(is_array($attribute_array) && $error==""){
			for($at=0;$at<sizeof($attribute_array);$at++){
				$splt=split("#",$attribute_array[$at]);
				$attrib_id=$splt[0];
				$option_id=$splt[1];
				$new_product_id=$id;
				if($mode!='edit' || $id=='0'){	
					$insert_query=tep_db_query("select max(orders_products_id)  as orders_products_id from ".TABLE_ORDERS_EDIT_PRODUCTS." where orders_id='".(int)$oID."' and products_id='".(int)$service_id."'");
					$insert_id=tep_db_fetch_array($insert_query);
					$new_product_id=$insert_id['orders_products_id'];
				}
			$Query = "insert into " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " set
				orders_id = '".(int)$oID."',
				orders_products_id = '".(int)$new_product_id."',
				products_options = '" . $services_options_names[$attrib_id] . "',
				products_options_values = '" . $services_options_values_names[$option_id] . "',
				options_values_price = '" .  $option_value_details[$attrib_id][$option_id]["options_values_price"] . "',
				price_prefix = '+';";
			  tep_db_query($Query);
			}
		}*/
		if($error=='') tep_redirect(tep_href_link(FILENAME_SHOPPING_CART,'service_action=buy_service&service_id=' . $services->service_id));		
	}
	echo "new_service||";			
?>
	<table border="0" width="100%" cellspacing="2" cellpadding="2">
		<tr>
    	<td  width="100%" valign="top">
			<?php echo tep_draw_form("select_resource",FILENAME_ADDSERVICES_NEW,$page_params);?>
			<table border="0" width="100%" cellspacing="0" cellpadding="0"> 
		 		<tr>
					<td><table border="0" width="100%" cellspacing=0 cellpadding=0 ?>
						 <tr >
							<td class="main"><B><?php echo SERVICE_ADDING_TITLE; ?></b> </td>
							<?php	print '<td id="show_service_save_icon_up" width=10% nowrap style="display:none" colspan=3 class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:do_page_fetch(\'service_submit\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif','Save') .'</a>&nbsp;
										<a href="javascript:do_page_fetch(\'ajx_close_service_new\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif','Close') . '</a>
									   </td>';?>
						  </tr>
						</table> 
					</td>
				</tr>
				<tr>
					<td>
						<table border="0" width=100% cellspacing="0" cellpadding="1"> 
							<tr  valign="middle">
								<td class="dataTableContent" align=left height="20"><b><?php echo SERVICE_STEP1; ?></b></td>
							</tr>
							<tr >
								<td>
								<table border="0" cellpadding="2" cellspacing="0">
									<tr>
										<td class="dataTableContent" width="100" valign="top"><?php echo TEXT_SERVICE_NAME;?></td>
										<td><?php echo tep_draw_separator('pixel_trans.gif', '20', ''); ?></td>
										<td class="dataTableContent" align=left valign="middle" width="550">
											<?php  $service_array=tep_get_service_time_info_tree($customer_id);
												   customServiceListBox('service_name',$service_array,$service_id,"100%","100px");
											 ?> 
										</td>
										<td><?php echo tep_draw_separator('pixel_trans.gif', '1', ''); ?></td>
											<td class="main" align="center" valign="bottom"><?php echo '<a href="javascript:do_page_fetch(\'new_service_step2\')">' . tep_image_button("button_select.gif",IMAGE_SELECT_SERVICE) . '</a>';?></td>	
									</tr>
								</table>	
								</td>	
							</tr>
						</table>
				</td>
			</tr>
			<tr><td colspan='3' id='new_service_step2'></td></tr>
			<tr><td colspan='3' id='new_service_step3'></td></tr>
			<?php
			print "<tr><td colspan='3' id='service_submit'></td></tr>";
			print '<tr ><td id="show_service_save_icon_down" style="display:none" colspan=3 class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:do_page_fetch(\'service_submit\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif','Save') .'</a>&nbsp;
			<a href="javascript:do_page_fetch(\'ajx_close_service_new\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif','Close') . '</a>
		   </td></tr>';
			?>
		</table>
		</form>
	</td></tr></table>	
<?php
function customServiceListBox($name,&$list,$selected,$width,$height){
	$selIndex=0;
	
	echo '<div id="' . $name . '_box" style="border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
	echo '<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
	for ($icnt=0;$icnt<sizeof($list);$icnt++){
		if ($list[$icnt]['mode']=='V') {
			if ($selected==0) $selected=$list[$icnt]['id'];
			echo  '<tr class="attributes-even"><td nowrap bgcolor="' . ($selected==$list[$icnt]['id']?"#D8EDFB":"#FFFFFF") . '" valign="middle" width="100%" id="' . $name . $list[$icnt]['id'] . '" onClick="javascript:selectServiceItem(\'' . $name . '\',\'' . $list[$icnt]['id'] .  '\',' . $icnt .')" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
			if ($selected==$list[$icnt]['id']) $selIndex=$icnt;
		} else {
			echo  '<tr class="attributes-even"><td nowrap bgcolor="#FFFFFF" valign="middle" width="100%" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
		}
	}
	echo '</table></div>' . "\n";
	echo '<input type="hidden" name="' . $name . '" value="' . $selected . '">';
	echo '<input type="hidden" name="' . $name . '_index" value="' . $selIndex . '">';
}


function customListBox($name,&$list,$selected,$width,$height){
	$selIndex=0;
	
	echo '<div id="' . $name . '_box" style="border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
	echo '<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
	for ($icnt=0;$icnt<sizeof($list);$icnt++){
		 	echo  '<tr class="attributes-even" onClick="javascript:sel_services(\'' . $name . '\',\'' . $list[$icnt]['id'] .  '\',' . $icnt .')"><td nowrap bgcolor="' . ($selected==$list[$icnt]['id']?"#D8EDFB":"#FFFFFF") . '" valign="middle" width="100%" id="' . $name . $list[$icnt]['id'] . '" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
			if ($selected==$list[$icnt]['id']) $selIndex=$icnt;
	}
	echo '</table></div>' . "\n";
	echo '<input type="hidden" name="' . $name . '" value="' . $selected . '">';
	echo '<input type="hidden" name="' . $name . '_index" value="' . $selIndex . '">';
}

  function found_service(){
	global $service_id,$start_date,$end_date,$start_time,$end_time,$service,$languages_id,$resource_id;
	$error="";
	if(!tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time) && $service['include_resource']=='N'){
		$error=ERR_TIME_ALREADY_RESERVED;
    }
	if($service['include_resource']=='Y' || $error) {
			//echo $start_date.'-'.$start_time.'-'.$end_date.'-'.$end_time;
			$qty=get_orders_products_quantity($service_id,$start_date,$end_date,$start_time,$end_time,$booking_type,$resource_id);
			if($qty>0) $error=ERR_TIME_ALREADY_RESERVED;
	}	
	return $error;	
  }
?>