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

	$orders_id=(isset($HTTP_POST_VARS['orders_id'])?(int)$HTTP_POST_VARS['orders_id']:0);
	$orders_items=(isset($HTTP_POST_VARS['orders'])?$HTTP_POST_VARS['orders']:'');
	if ($orders_id<=0 || sizeof($orders_items)<=0)
		tep_redirect(tep_href_link(FILENAME_PENDING_WAITLIST_PAYMENTS));
	
	$currencies=new currencies();
	$server_date = getServerDate(true);
	$orders_query=tep_db_query("SELECT * from " . TABLE_ORDERS . " where orders_id ='" . (int)$orders_id . "'");
	$orders_result=tep_db_fetch_array($orders_query);
	unset($orders_result["orders_id"]);
	
	// create the order
	tep_db_perform(TABLE_ORDERS,$orders_result);
	$new_order_id=tep_db_insert_id();
	$edit_order=false;
	for ($icnt=0;$icnt<sizeof($orders_items);$icnt++){
		$products_query=tep_db_query("SELECT orders_products_id,orders_id,products_id,products_name,products_model,products_price,final_price,products_tax,products_quantity,products_type,events_type,waitlist_type,events_fees,waitlist_orders_id,products_sku from " . TABLE_ORDERS_PRODUCTS . " where orders_products_id='" . (int)$orders_items[$icnt]['id'] . "'");
		if (tep_db_num_rows($products_query)>0){
			$products_result=tep_db_fetch_array($products_query);
			unset($products_result['orders_products_id']);
			$products_result['products_price']=(float)$orders_items[$icnt]['amount'];
			$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;
			if ($orders_items[$icnt]['type']=='P'){ // if paid amount
				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='" . (int)$orders_items[$icnt]['id'] . "'");
			} else { // if unpaid
				$edit_order=true;
				$products_result['date_added']=$server_date;
				tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$products_result);
			}
		}
	}
	update_totals($new_order_id);
	if ($edit_order){	
		if (!tep_session_is_registered('edit_order')) tep_session_register("edit_order");
		$edit_order=$new_order_id;
		tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS, 'oID=' . $new_order_id . '&step=2&selected_box=customers', 'SSL'));
	}
	tep_redirect(tep_href_link(FILENAME_ORDERS, 'oID=' . $new_order_id . '&selected_box=customers&action=edit', 'SSL'));

  require(DIR_WS_INCLUDES . 'application_bottom.php');
  
  
function sbs_get_zone_name($country_id, $zone_id) {
    $zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'");
    if (tep_db_num_rows($zone_query)) {
      $zone = tep_db_fetch_array($zone_query);
      return $zone['zone_name'];
    } else {
      return $default_zone;
    }
  }
  
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);
}
?>