<?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');
	require(DIR_WS_CLASSES . 'currencies.php');
	
	$currencies = new currencies();
	
	$move_elements=(isset($HTTP_POST_VARS["move_elements"])?$HTTP_POST_VARS["move_elements"]:array());
	$session_index=(isset($HTTP_POST_VARS["new_session_index"])?$HTTP_POST_VARS["new_session_index"]:array());
	$new_session_id=(isset($HTTP_POST_VARS["new_session_id"])?$HTTP_POST_VARS["new_session_id"]:array());
    $new_events_id=(isset($HTTP_POST_VARS['new_events_id'])?(int)$HTTP_POST_VARS["new_events_id"]:0);
	if (sizeof($move_elements)<=0 || sizeof($session_index)<=0 || sizeof($new_session_id)<=0 || $new_events_id<=0){
		tep_redirect(tep_href_link(FILENAME_SERVICES_SEARCH_BOOKINGS));
	}
    $process_wait=(isset($HTTP_POST_VARS['process_wait'])?$HTTP_POST_VARS['process_wait']:'N');
	$success_ids=(isset($HTTP_POST_VARS['moved_ids'])?$HTTP_POST_VARS['moved_ids']:array());
	$failure_ids=array();

    $events_query=tep_db_query("SELECT " . tep_event_fields_list() . " from " . TABLE_EVENTS . " e where e.events_id='" . $new_events_id . "'");
    $events_result=tep_db_fetch_array($events_query);


	$check_session=array();
	$new_session_unique=array_unique($new_session_id);
	
	$new_events_name=tep_get_events_name($new_events_id);
	
	$select_ids_string=join(",",$new_session_unique);

	$new_session=array();
	if ($select_ids_string!="") {
		$session_query=tep_db_query("SELECT start_date,sessions_id from " . TABLE_EVENTS_SESSIONS . " where sessions_id in(" . $select_ids_string . ") order by start_date");
		while($session_result=tep_db_fetch_array($session_query)){
			$new_session[$session_result["sessions_id"]]['start_date']=$session_result["start_date"];
			$new_session[$session_result["sessions_id"]]['sessions_id']=$session_result["sessions_id"];
		}
	}
	$select_ids_string=join(",",$move_elements);
	if ($select_ids_string!=""){
		$move_elements_query=tep_db_query("SELECT o.customers_id,op.orders_products_id,op.products_id,events_type from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op where op.orders_products_id in(" . $select_ids_string . ") and op.orders_id=o.orders_id");
	}
	while($move_result=tep_db_fetch_array($move_elements_query)){
		$move_details[$move_result["orders_products_id"]]=array('orders_products_id'=>$move_result["orders_products_id"],	
																'customers_id'=>$move_result["customers_id"],
																'products_id'=>$move_result["products_id"],
																'events_type'=>$move_result["events_type"]);
	}
	$icnt=0;
	$prev_index=-1;
    for ($icnt=0;$icnt<sizeof($move_elements);$icnt++){
		$err_type='U';
		$session_id=(int)$new_session_id[$session_index[$icnt]];
		if ($session_id>0){
			$move_detail=$move_details[$move_elements[$icnt]];
			if (tep_check_single_reservation($session_id,$move_detail["customers_id"])){
				$err_type='A';
			} else {
				if (tep_check_session_free($new_session[$session_id],$events_result,'R',$move_detail["customers_id"])){
					//$events_price=tep_get_events_price($events_result,'R','',$new_session[$new_session_id]);
					$waitlist_type="";
					if ($move_detail["events_type"]=="W") $waitlist_type='P';
					tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . $session_id . "', products_model='" . $new_session[$session_id]['start_date'] . "', products_name='" . tep_db_input($new_events_name) . "', events_type='R', waitlist_type='" . $waitlist_type . "' where orders_products_id='" . (int)$move_detail["orders_products_id"] . "'");
					$err_type='N';
				} else {
					$err_type='M';
					if ($events_result["events_wait_list"]=='Y') {
						$err_type='W';
						$group=tep_find_waitlist_group($events_result["events_id"],$session_id,$move_detail["customers_id"],$events_result);
						if ($group!='E'){
							$err_type='W';
							if ($process_wait=='Y'){
								tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . $session_id . "', products_model='" . $new_session[$session_id]['start_date'] . "', products_name='" . tep_db_input($new_events_name) . "', events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . (int)$move_detail["orders_products_id"] . "'");
								$err_type='N';
							}
						}
					}
				}
			}
		}
		if ($err_type=='N'){
			$success_ids[$move_detail["orders_products_id"]]=1;
		} else {
			$failure_ids[$move_detail["orders_products_id"]]=$err_type;
			if ($prev_index!=$session_index[$icnt]) {
				$prev_index=$session_index[$icnt];
				$check_session[]=$session_id;
			}
		}
		$err_type='U';
     }
	$params=array("tx_fname"=>'',"tx_lname"=>'',"page"=>1,"tx_words"=>'',"tx_code"=>'',
								"tx_from"=>'',"tx_until"=>'',"tx_service"=>'',"tx_resource"=>'',"orders_id"=>'',"service_id"=>'',"tx_pending"=>'');
	$search_get_params=tep_prepare_hidden_params($params,"post","get",array());
	
	if (sizeof($failure_ids)<=0){
		$messageStack->add_session(TEXT_SUCCESS_MOVE_RESERVTIONS);
		tep_redirect(tep_href_link(FILENAME_SEARCH_SEARCH_BOOKINGS,$search_get_params . 'action=search'));
	}
	
	$search_post_params=tep_prepare_hidden_params($params,'post','post',array());

	echo tep_draw_form('post_submit',FILENAME_MOVE_BOOKINGS_CONFIRM) . "\n";
	echo '<input type="hidden" name="new_events_id" value="' . $new_events_id . '">';
	echo $search_post_params  . "\n";
	if (sizeof($success_ids)>0){
		reset($success_ids);
		while(list($id,)=each($success_ids)){
			echo '<input type="hidden" name="moved_ids[' . $id . ']" value="1">'  . "\n";
		}
	}
	if (sizeof($failure_ids)>0){
		reset($failure_ids);
		while(list($id,$value)=each($failure_ids)){
			echo '<input type="hidden" name="unmoved_ids[' . $id . ']" value="' . $value . '">'  . "\n";
		}
	}
	$reserve_ids=(isset($HTTP_POST_VARS["booking_id"])?$HTTP_POST_VARS["booking_id"]:array());
	$type=(isset($HTTP_POST_VARS["items_type"])?$HTTP_POST_VARS["items_type"]:array());
	for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
		echo '<input type="hidden" name="booking_id[]" value="' . $reserve_ids[$icnt] . '">'  . "\n";
	}
	if (sizeof($type)>0){
		reset($type);
		while(list($id,$value)=each($type)){
			echo '<input type="hidden" name="items_type[' . $id . ']" value="' . $value . '">';
		}
	}
	for ($icnt=0;$icnt<sizeof($check_session);$icnt++){
		echo '<input type="hidden" name="new_session[]" value="' . $check_session[$icnt] . '">'  . "\n";
	}
	echo '</form>';
	//return;
	echo '<script language="javascript">document.post_submit.submit()</script>';
?>