<?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');

	$subscription_id=(isset($HTTP_GET_VARS["subscription_id"])?(int)$HTTP_GET_VARS["subscription_id"]:0);
	
	if ($subscription_id<=0){
		tep_redirect(tep_href_link(FILENAME_DEFAULT));
	}
	if (!tep_session_is_registered('customer_id')) {
		$navigation->set_snapshot();
		tep_redirect(tep_href_link(FILENAME_LOGIN, 'trial=1', 'SSL'));
	}

	$customer_address_query = tep_db_query("select c.customers_firstname, c.customers_lastname, c.customers_telephone, c.customers_email_address,c.customers_second_email_address,c.customers_second_telephone,c.customers_fax,ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, z.zone_name, co.countries_id, co.countries_name, co.countries_iso_code_2, co.countries_iso_code_3, co.address_format_id, ab.entry_state from " . TABLE_CUSTOMERS . " c, " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) left join " . TABLE_COUNTRIES . " co on (ab.entry_country_id = co.countries_id) where c.customers_id = '" . (int)$customer_id . "' and ab.customers_id = '" . (int)$customer_id . "' and c.customers_default_address_id = ab.address_book_id");
	$customer = tep_db_fetch_array($customer_address_query);

	$currency_query=tep_db_query("SELECT value from " . TABLE_CURRENCIES . " where code='" . DEFAULT_CURRENCY . "'");
	if (tep_db_num_rows($currency_query)>0){
		$currency_result=tep_db_fetch_array($currency_query);
		$currency = DEFAULT_CURRENCY;
		$currency_value = $currency_result["value"];
	} else {
		$currency="USD";
		$currency_value=0.77060002;
	}
	
	$customer_format_id=$customer['customers_address_format_id'];
	if($customer_format_id=='') $customer_format_id=0;
	$sql_data_array = array(
							'customers_id' => $customer_id,
							'customers_name' => $customer['customers_firstname'] . ' ' . $customer['customers_lastname'],
							'customers_company' => $customer['entry_company'],
							'customers_street_address' => $customer['entry_street_address'],
							'customers_suburb' => $customer['entry_suburb'],
							'customers_city' => $customer['entry_city'],
							'customers_postcode' => $customer['entry_postcode'], 
							'customers_state' => ((tep_not_null($customer['entry_state'])) ? $customer['entry_state'] : $customer['zone_name']),
							'customers_country' => $customer['countries_name'], 
							'customers_telephone' => $customer['customers_telephone'], 
							'customers_email_address' => $customer['customers_email_address'],
							'customers_second_telephone'=>$customer['customers_second_telephone'],
							'customers_second_email_address'=>$customer['customers_second_email_address'],
							'customers_fax'=>$customer['customers_fax'],
							'customers_address_format_id' => $customer_format_id, 
							'delivery_name' => $customer['customers_firstname'] . ' ' . $customer['customers_lastname'],
							'delivery_company' => $customer['entry_company'],
							'delivery_street_address' => $customer['entry_street_address'],
							'delivery_suburb' => $customer['entry_suburb'],
							'delivery_city' => $customer['entry_city'], 
							'delivery_postcode' => $customer['entry_postcode'], 
							'delivery_state' => ((tep_not_null($customer['entry_state'])) ? $customer['entry_state'] : $customer['zone_name']), 
							'delivery_country' => $customer['countries_name'], 
							'delivery_address_format_id' => $customer_format_id, 
							'billing_name' => $customer['customers_firstname'] . ' ' . $customer['customers_lastname'], 
							'billing_company' => $customer['entry_company'],
							'billing_street_address' => $customer['entry_street_address'],
							'billing_suburb' => $customer['entry_suburb'],
							'billing_city' => $customer['entry_city'],  
							'billing_postcode' => $customer['entry_postcode'], 
							'billing_state' => ((tep_not_null($customer['entry_state'])) ? $customer['entry_state'] : $customer['zone_name']), 
							'billing_country' => $customer['countries_name'], 
							'billing_address_format_id' => $customer_format_id,
							'payment_method' => 'free trial',
							'payment_info' => 'free trial',
							'date_purchased' => date('Y-m-d H:i:s',getServerDate(false)), 
							'last_modified' => date('Y-m-d H:i:s',getServerDate(false)),
							'orders_status' => 3, 
							'currency' => $currency, 
							'currency_value' => $currency_value
							);

	// insert order detail							
  	tep_db_perform(TABLE_ORDERS, $sql_data_array);
	$insert_id = tep_db_insert_id();
	
	
	// insert order total
	$total_class=array(	'ot_total'=>array('title'=>'Total:','sort_order'=>4));
	
	reset($total_class);
	while(list($key,)=each($total_class)){
		$sql_data_array = array('orders_id' => $insert_id,
								'title' => $total_class[$key]['title'],
								'text' => "0.0",
								'value' => "0.00", 
								'class' => $key, 
								'sort_order' => $total_class[$key]['sort_order']);
		
		tep_db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
	} 

	// insert product detail
		$sql_data_array = array('orders_id'=>$insert_id,
								'products_id'=>tep_db_input($subscription_id),
								'products_name'=>tep_get_subscription_name(tep_db_input($subscription_id)),
								'products_model'=>'',
								'products_quantity'=>1,
								'products_price'=>0.0,
								'final_price'=>0.0,
								'products_tax'=>0,
								'products_quantity'=>0,
								'products_type'=>'S',
								'events_type'=>'L',
								'waitlist_type'=>'',
								'events_fees'=>0.0,
								'waitlist_orders_id'=>0
								);
		
		tep_db_perform(TABLE_ORDERS_PRODUCTS,$sql_data_array);

		tep_redirect(tep_href_link(FILENAME_SUBSCRIPTION_TRIAL_SUCCESS,tep_get_all_get_params(array('trial'))));
  	// get the customer details
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
