<?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 );
	
  include('includes/application_top.php');
  include('includes/classes/order.php');


// if the customer is not logged on, redirect them to the login page
	if (!tep_session_is_registered('customer_id')) {
		tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  	}
  	$orders_id=isset($HTTP_GET_VARS["oID"])?(int)$HTTP_GET_VARS["oID"]:0;
	$events_id=isset($HTTP_GET_VARS["events_id"])?(int)$HTTP_GET_VARS["events_id"]:0;
	
	if ($orders_id<=0 || $events_id<=0) tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));

	$currencies=new currencies();
	// create the order
	$orders_query=tep_db_query("SELECT * from " . TABLE_ORDERS . " where orders_id ='" . tep_db_input($orders_id) . "'");
	$orders_result=tep_db_fetch_array($orders_query);
	unset($orders_result["orders_id"]);
	tep_db_perform(TABLE_ORDERS,$orders_result);
	$new_order_id=tep_db_insert_id();
	
	// get the products
	$products_query=tep_db_query("SELECT op.orders_products_id,op.orders_id,op.products_id,op.products_model,op.products_name,op.products_price,op.final_price, " .
							 " op.products_tax,op.products_quantity,op.products_type,op.events_type,op.waitlist_type,op.events_fees,op.waitlist_orders_id from ". TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id='" . tep_db_input($orders_id) . "' and s.events_id='" . tep_db_input($events_id) . "' and op.products_id=s.sessions_id and op.products_type='E' and op.events_type='R' and op.waitlist_type='P'");
	$details=tep_get_orders_reminder_details($orders_id,$events_id,'E',true);

	$calculate_amount=0; // actual calculated amount
	$prepared_amount=$details['product_total']-($details['order_total']+$details['confirm_paid']); //final prepared amount to be paid

	if ($prepared_amount>0) tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));
	
	$updated_amount=$details['confirm_total']-$details['confirm_paid'];  // remaining waitlist amount
	if ($updated_amount>=$details['order_total']) $updated_amount=0;
	else $updated_amount=$details['order_total']-$updated_amount;

	while($products_result=tep_db_fetch_array($products_query)){
		$calculate_amount+=$products_result['events_fees'];

		if ($calculate_amount<=$updated_amount){
			$new_price=0;
		} else if ($prepared_amount>$products_result['events_fees']) {
			$new_price=$products_result['events_fees'];
			$prepared_amount-=$products_result['events_fees'];
		} else {
			$new_price=$prepared_amount;
		}
			$update_id=$products_result['orders_products_id'];
			unset($products_result['orders_products_id']);
			$products_result['products_price']=$new_price;
			$products_result['final_price']=$products_result['products_price'];
			$products_result['events_type']='R';
			$products_result['waitlist_type']='';
			$products_result['waitlist_orders_id']=$orders_id;
			$products_result['orders_id']=$new_order_id;

		tep_db_perform(TABLE_ORDERS_PRODUCTS, $products_result);
		tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='C',waitlist_type='' where orders_products_id='" . $update_id . "'");
	}
	update_totals($new_order_id);

	//$messageStack->add_session('account',SUCCESS_REMAINDER_COMPLETE,'success');
	
	tep_redirect(tep_href_link(FILENAME_ACCOUNT,'', 'SSL'));

require(DIR_WS_INCLUDES . 'application_bottom.php');

function update_totals($order_id)
{
	global $currencies;
	$order = new order($order_id);
	
	$RunningSubTotal = 0;
	$RunningTax = 0;
	
	for ($i=0; $i<sizeof($order->products); $i++)
	{
		$RunningSubTotal += ($order->products[$i]['qty'] * $order->products[$i]['final_price']);
		$RunningTax += (($order->products[$i]['tax'] / 100) * ($order->products[$i]['qty'] * $order->products[$i]['final_price']));			
	}
	$sql_array=array(	'orders_id'=>$order_id,
						'text'=>$currencies->format($RunningSubTotal),
						'value'=>$RunningSubTotal,
						'class'=>'ot_subtotal',
						'title'=>'Sub Total',
						'sort_order'=>1);
	tep_db_perform(TABLE_ORDERS_TOTAL,$sql_array);

	$sql_array=array(	'orders_id'=>$order_id,
						'text'=>$currencies->format($RunningTax),
						'value'=>$RunningTax,
						'class'=>'ot_tax',
						'title'=>'Tax',
						'sort_order'=>2);
	tep_db_perform(TABLE_ORDERS_TOTAL,$sql_array);

	$sql_array=array(	'orders_id'=>$order_id,
						'text'=>$currencies->format(tep_get_rounded_amount($RunningTax+$RunningSubTotal), true, $order->info['currency'], $order->info['currency_value']),
						'value'=>tep_get_rounded_amount($RunningTax+$RunningSubTotal),
						'class'=>'ot_total',
						'title'=>'Total',
						'sort_order'=>3);
	tep_db_perform(TABLE_ORDERS_TOTAL,$sql_array);
}
?>