<?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
*/
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 

  function tep_get_subscription_name($subscription_id, $language_id = 0) {
    global $languages_id;

    if ($language_id == 0) $language_id = $languages_id;
    $subscription_query = tep_db_query("select subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . (int)$subscription_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription = tep_db_fetch_array($subscription_query);

    return $subscription['subscription_name'];
  }
  
// get subscription description by passing subscription_id
 function tep_get_subscription_description($subscription_id, $language_id=0) {
	global $languages_id;

	if ($language_id == 0) $language_id = $languages_id;
    $subscription_query = tep_db_query("select subscription_description from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . (int)$subscription_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription = tep_db_fetch_array($subscription_query);

    return $subscription['subscription_description'];
  }
  
   // get subscription url by passing subscription_id
 function tep_get_subscription_url($subscription_id, $language_id=0) {
	global $languages_id;
	
	if ($language_id == 0) $language_id = $languages_id;
    $subscription_query = tep_db_query("select subscription_url from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . (int)$subscription_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription = tep_db_fetch_array($subscription_query);

    return $subscription['subscription_url'];
 }
 
// get subscription group name by passing group_id and language_id
  function tep_get_subscription_group_name($subscription_group_id, $language_id) {
    $subscription_group_query = tep_db_query("select subscription_group_name from " . TABLE_SUBSCRIPTIONS_GROUP . " where subscription_group_id = '" . (int)$subscription_group_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription_group = tep_db_fetch_array($subscription_group_query);

    return $subscription_group['subscription_group_name'];
  }
  
   // set subscription status by passing subscription_group_id
  function tep_set_subscription_group_status($subscription_group_id, $status) {
    if ($status == '1') {
      return tep_db_query("update " . TABLE_SUBSCRIPTIONS_GROUP . " set subscription_group_status = 1, last_modified = now() where subscription_group_id = '" . (int)$subscription_group_id . "'");
    } elseif ($status == '0') {
      return tep_db_query("update " . TABLE_SUBSCRIPTIONS_GROUP . " set subscription_group_status = 0, last_modified = now() where subscription_group_id = '" . (int)$subscription_group_id . "'");
    } else {
      return -1;
    }
  }
  
	// get subscription category name by passing category_id and language_id
  function tep_get_subscription_category_name($subscription_category_id, $language_id) {
    $subscription_category_query = tep_db_query("select subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " where subscription_categories_id = '" . (int)$subscription_category_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription_category = tep_db_fetch_array($subscription_category_query);
    return $subscription_category['subscription_categories_name'];
  }
  
	// get subscription category name by passing category_id and language_id
  function tep_get_subscription_category_title($subscription_category_id, $language_id) {
    $subscription_category_query = tep_db_query("select subscription_categories_heading_title from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " where subscription_categories_id = '" . (int)$subscription_category_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription_category = tep_db_fetch_array($subscription_category_query);

    return $subscription_category['subscription_categories_heading_title'];
  }
  
	// get subscription category name by passing category_id and language_id
  function tep_get_subscription_category_description($subscription_category_id, $language_id) {
    $subscription_category_query = tep_db_query("select subscription_categories_description from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " where subscription_categories_id = '" . (int)$subscription_category_id . "' and language_id = '" . (int)$language_id . "'");
    $subscription_category = tep_db_fetch_array($subscription_category_query);

    return $subscription_category['subscription_categories_description'];
  }
  
  function tep_remove_subscription_group($subscription_group_id) {
		$subscription_group_id=(int)$subscription_group_id;

		tep_db_query("delete from " . TABLE_SUBSCRIPTIONS_GROUP . " where subscription_group_id = '" . (int)$subscription_group_id . "'");
	
	}
	
  // get subscription_path url parameter by passing current subscription_category_id  
  function tep_get_subscription_path($current_subscription_category_id = '') {
    global $sucPath_array;

    if ($current_subscription_category_id == '') {
      $sucPath_new = implode('_', $sucPath_array);
    } else {
      if (sizeof($sucPath_array) == 0) {
        $sucPath_new = $current_subscription_category_id;
      } else {
        $sucPath_new = '';
        $last_subscription_category_query = tep_db_query("select parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$sucPath_array[(sizeof($sucPath_array)-1)] . "'");
        $last_subscription_category = tep_db_fetch_array($last_subscription_category_query);

        $current_subscription_category_query = tep_db_query("select parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$current_subscription_category_id . "'");
        $current_subscription_category = tep_db_fetch_array($current_subscription_category_query);

        if ($last_subscription_category['parent_id'] == $current_subscription_category['parent_id']) {
          for ($i = 0, $n = sizeof($sucPath_array) - 1; $i < $n; $i++) {
            $sucPath_new .= '_' . $sucPath_array[$i];
          }
        } else {
          for ($i = 0, $n = sizeof($sucPath_array); $i < $n; $i++) {
            $sucPath_new .= '_' . $sucPath_array[$i];
          }
        }

        $sucPath_new .= '_' . $current_subscription_category_id;

        if (substr($sucPath_new, 0, 1) == '_') {
          $sucPath_new = substr($sucPath_new, 1);
        }
      }
    }

    return 'sucPath=' . $sucPath_new;
  } 
  
  // form the generated subscription category tree
	function tep_get_subscription_category_tree($parent_id = '0', $spacing = '', $exclude = '', $subscription_category_tree_array = '', $include_itself = false) {
		global $languages_id;
		
		if (!is_array($subscription_category_tree_array)) $subscription_category_tree_array = array();
		if ( (sizeof($subscription_category_tree_array) < 1) && ($exclude != '0') ) $subscription_category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP);
		if ($include_itself) {
			$subscription_category_query = tep_db_query("select sucd.subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where sucd.language_id = '" . (int)$languages_id . "' and sucd.subscription_categories_id = '" . (int)$parent_id . "'");
			$subscription_category = tep_db_fetch_array($subscription_category_query);
			$subscription_category_tree_array[] = array('id' => $parent_id, 'text' => $subscription_category['subscription_categories_name']);
		}
		$subscription_categories_query = tep_db_query("select suc.subscription_categories_id, sucd.subscription_categories_name, suc.parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = sucd.subscription_categories_id and sucd.language_id = '" . (int)$languages_id . "' and suc.parent_id = '" . (int)$parent_id . "' order by suc.sort_order, sucd.subscription_categories_name");
		while ($subscription_categories = tep_db_fetch_array($subscription_categories_query)) {
			if ($exclude != $subscription_categories['subscription_categories_id']) {
				$subscription_category_tree_array[] = array('id' => $subscription_categories['subscription_categories_id'], 'text' => $spacing . $subscription_categories['subscription_categories_name']);
			}
			$subscription_category_tree_array = tep_get_subscription_category_tree($subscription_categories['subscription_categories_id'], $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $subscription_category_tree_array);
		}
		return $subscription_category_tree_array;
	}
	
	  // get the subscription category path by passing current directory
	  function tep_get_generated_subscription_category_path_ids($id, $from = 'subscription_category') {
		$calculated_subscription_category_path_string = '';
		$calculated_subscription_category_path = tep_generate_subscription_category_path($id, $from);
		for ($i=0, $n=sizeof($calculated_subscription_category_path); $i<$n; $i++) {
		  for ($j=0, $k=sizeof($calculated_subscription_category_path[$i]); $j<$k; $j++) {
			$calculated_subscription_category_path_string .= $calculated_subscription_category_path[$i][$j]['id'] . '_';
		  }
		  $calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -1) . '<br>';
		}
		$calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -4);
	
		if (strlen($calculated_subscription_category_path_string) < 1) $calculated_subscription_category_path_string = TEXT_TOP;
	
		return $calculated_subscription_category_path_string;
	  }
	  
	  // find total no of child subscription in a selected category
	  function tep_childs_in_subscription_category_count($subscription_categories_id) {
		$subscription_categories_count = 0;
	
		$subscription_categories_query = tep_db_query("select subscription_categories_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " where parent_id = '" . (int)$subscription_categories_id . "'");
		while ($subscription_categories = tep_db_fetch_array($subscription_categories_query)) {
		  $subscription_categories_count++;
		  $subscription_categories_count += tep_childs_in_subscription_category_count($subscription_categories['subscription_categories_id']);
		}
	
		return $subscription_categories_count;
	  }

	 // get total subscription count by navigating nested category
	  function tep_subscription_in_subscription_category_count($subscription_categories_id, $include_deactivated = false) {
		$subscription_count = 0;
	
		if ($include_deactivated) {
		  $subscription_query = tep_db_query("select count(*) as total from " . TABLE_SUBSCRIPTIONS . " su " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " su2c where su.subscription_id = su2c.subscription_id and su2c.subscription_categories_id = '" . (int)$subscription_categories_id . "'");
		} else {
		  $subscription_query = tep_db_query("select count(*) as total from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " su2c where su.subscription_id = su2c.subscription_id and su.subscription_status>=1 and su2c.subscription_categories_id = '" . (int)$subscription_categories_id . "'");
		}
	
		$subscription = tep_db_fetch_array($subscription_query);
	
		$subscription_count += $subscription['total'];
	
		$childs_query = tep_db_query("select subscription_categories_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " where parent_id = '" . (int)$subscription_categories_id . "'");
		if (tep_db_num_rows($childs_query)) {
		  while ($childs = tep_db_fetch_array($childs_query)) {
			$subscription_count += tep_subscription_in_subscription_category_count($childs['subscription_categories_id'], $include_deactivated);
		  }
		}
	
		return $subscription_count;
	  }
	  
	 // generate subscription category path from the current category
	  function tep_output_generated_subscription_category_path($id, $from = 'subscription_category') {
		$calculated_subscription_category_path_string = '';
		$calculated_subscription_category_path = tep_generate_subscription_category_path($id, $from);
		for ($i=0, $n=sizeof($calculated_subscription_category_path); $i<$n; $i++) {
		  for ($j=0, $k=sizeof($calculated_subscription_category_path[$i]); $j<$k; $j++) {
			$calculated_subscription_category_path_string .= $calculated_subscription_category_path[$i][$j]['text'] . '&nbsp;&gt;&nbsp;';
		  }
		  $calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -16) . '<br>';
		}
		$calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -4);
	
		if (strlen($calculated_subscription_category_path_string) < 1) $calculated_subscription_category_path_string = TEXT_TOP;
	
		return $calculated_subscription_category_path_string;
	  }

	  // get subscription category path
	  function tep_generate_subscription_category_path($id, $from = 'subscription_category', $subscription_categories_array = '', $index = 0) {
		global $languages_id;
	
		if (!is_array($subscription_categories_array)) $subscription_categories_array = array();
	
		if ($from == 'subscription') {
		  $subscription_categories_query = tep_db_query("select subscription_categories_id from " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " where subscription_id = '" . (int)$id . "'");
		  while ($subscription_categories = tep_db_fetch_array($subscription_categories_query)) {
			if ($subscription_categories['subscription_categories_id'] == '0') {
			  $subscription_categories_array[$index][] = array('id' => '0', 'text' => TEXT_TOP);
			} else {
			  $subscription_category_query = tep_db_query("select sucd.subscription_categories_name, suc.parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = '" . (int)$subscription_categories['subscription_categories_id'] . "' and suc.subscription_categories_id = sucd.subscription_categories_id and sucd.language_id = '" . (int)$languages_id . "'");
			  $subscription_category = tep_db_fetch_array($subscription_category_query);
			  $subscription_categories_array[$index][] = array('id' => $subscription_categories['subscription_categories_id'], 'text' => $subscription_category['subscription_categories_name']);
			  if ( (tep_not_null($subscription_category['parent_id'])) && ($subscription_category['parent_id'] != '0') ) $subscription_categories_array = tep_generate_subscription_category_path($subscription_category['parent_id'], 'subscription_category', $subscription_categories_array, $index);
			  $subscription_categories_array[$index] = array_reverse($subscription_categories_array[$index]);
			}
			$index++;
		  }
		} elseif ($from == 'subscription_category') {
		  $subscription_category_query = tep_db_query("select sucd.subscription_categories_name, suc.parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = '" . (int)$id . "' and suc.subscription_categories_id = sucd.subscription_categories_id and sucd.language_id = '" . (int)$languages_id . "'");
		  $subscription_category = tep_db_fetch_array($subscription_category_query);
		  $subscription_categories_array[$index][] = array('id' => $id, 'text' => $subscription_category['subscription_categories_name']);
		  if ( (tep_not_null($subscription_category['parent_id'])) && ($subscription_category['parent_id'] != '0') ) $subscription_categories_array = tep_generate_subscription_category_path($subscription_category['parent_id'], 'subscription_category', $subscription_categories_array, $index++);
		}
		return $subscription_categories_array;
	  }

// remove subscription from database
	function tep_remove_subscription($subscription_id) {
		// fetch image name 
		$subscription_image_query = tep_db_query("select subscription_image from " . TABLE_SUBSCRIPTIONS . " where subscription_id = '" . (int)$subscription_id . "'");
		$subscription_image = tep_db_fetch_array($subscription_image_query);
	
		$duplicate_image_query = tep_db_query("select count(*) as total from " . TABLE_SUBSCRIPTIONS . " where subscription_image = '" . tep_db_input($subscription_image['subscription_image']) . "'");
		$duplicate_image = tep_db_fetch_array($duplicate_image_query);
	
		// delete if there is only one reference for this image
		if ($duplicate_image['total'] < 2) {
		  if (file_exists(DIR_FS_CATALOG_IMAGES . $subscription_image['subscription_image'])) {
			@unlink(DIR_FS_CATALOG_IMAGES . $subscription_image['subscription_image']);
		  }
		}
		$subscription_id=(int)$subscription_id;
		
		// delete from subscription,subscription,subscription_to_subscription table
		tep_db_query("delete from " . TABLE_SUBSCRIPTIONS . " where subscription_id = '" . $subscription_id . "'");
		tep_db_query("delete from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . $subscription_id . "'");
		tep_db_query("delete from " . TABLE_SUBSCRIPTION_MESSAGES . " where subscription_id = '" . $subscription_id . "'");
	}	
	
  // remove the selected subscription category
  function tep_remove_subscription_category($subscription_category_id) {
    $category_image_query = tep_db_query("select subscription_categories_image from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$subscription_category_id . "'");
    $category_image = tep_db_fetch_array($category_image_query);

    $duplicate_image_query = tep_db_query("select count(*) as total from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_image = '" . tep_db_input($subscription_category_image['subscription_categories_image']) . "'");
    $duplicate_image = tep_db_fetch_array($duplicate_image_query);

    if ($duplicate_image['total'] < 2) {
      if (file_exists(DIR_FS_CATALOG_IMAGES . $category_image['subscription_categories_image'])) {
        @unlink(DIR_FS_CATALOG_IMAGES . $category_image['subscription_categories_image']);
      }
    }

    tep_db_query("delete from " . TABLE_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$subscription_category_id . "'");
    tep_db_query("delete from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " where subscription_categories_id = '" . (int)$subscription_category_id . "'");
    tep_db_query("delete from " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " where subscription_categories_id = '" . (int)$subscription_category_id . "'");

  }

  // generate subscription category path to the current category
  function tep_output_rev_generated_subscription_category_path($id, $from = 'subscription_category') {
    $calculated_subscription_category_path_string = '';
    $calculated_subscription_category_path = tep_generate_subscription_category_path($id, $from);
    for ($i=0, $n=sizeof($calculated_subscription_category_path); $i<$n; $i++) {
      for ($j=0, $k=sizeof($calculated_subscription_category_path[$i]); $j<$k; $j++) {
        $calculated_subscription_category_path_string = $calculated_subscription_category_path[$i][$j]['text'] . '&nbsp;&gt;&nbsp;' . $calculated_subscription_category_path_string;
      }
      $calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -16) . '<br>';
    }
    $calculated_subscription_category_path_string = substr($calculated_subscription_category_path_string, 0, -4);

    if (strlen($calculated_subscription_category_path_string) < 1) $calculated_subscription_category_path_string = '';

    return $calculated_subscription_category_path_string;
  }
  
  /// get the value for subcription group name
	function tep_get_group_name($subscription_group_id) {
		$subscription_group_query = tep_db_query("select subscription_group_name from " . TABLE_SUBSCRIPTIONS_GROUP . " where subscription_group_id = '" . (int)$subscription_group_id . "'");
		$subscription_group = tep_db_fetch_array($subscription_group_query);
		return $subscription_group['subscription_group_name'];
  }
  
	// get the subscription display tree
  function tep_get_subscription_display_tree($parent_id = '0', $spacing = '', $exclude = '', $subscription_category_tree_array = '', $include_itself = false) {
    global $languages_id;
    
    if (!is_array($subscription_category_tree_array)) $subscription_category_tree_array = array();
    if ( (sizeof($subscription_category_tree_array) < 1) && ($exclude != '0') ) $subscription_category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP);

    if ($include_itself) {
      $subscription_category_query = tep_db_query("select sud.subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sud where sud.language_id = '" . (int)$languages_id . "' and sud.subscription_categories_id = '" . (int)$parent_id . "'");
	  $subscription_category = tep_db_fetch_array($subscription_category_query);
      $subscription_category_tree_array[] = array('id' => $parent_id, 'text' => $subscription_category['subscription_categories_name']);
    }

    $subscription_categories_query = tep_db_query("select su.subscription_categories_id, sud.subscription_categories_name, su.parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " su, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sud where su.subscription_categories_id = sud.subscription_categories_id and sud.language_id = '" . (int)$languages_id . "' and su.parent_id = '" . (int)$parent_id . "' order by su.sort_order, sud.subscription_categories_name");
	while ($subscription_categories = tep_db_fetch_array($subscription_categories_query)) {
      if ($exclude != $subscription_categories['subscription_categories_id']) $subscription_category_tree_array[] = array('id' => $subscription_categories['subscription_categories_id'], 'text' => $spacing . "--" .  $subscription_categories['subscription_categories_name'] . "--");
      $subscription_category_tree_array = tep_get_subscription_display_tree($subscription_categories['subscription_categories_id'], $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $subscription_category_tree_array);
    }
    $subscription_query=tep_db_query("select sud.subscription_name,sud.subscription_id from " . TABLE_SUBSCRIPTIONS." su," . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud, " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " suTsuc where su.subscription_status>=1 and su.subscription_id=sud.subscription_id and suTsuc.subscription_categories_id='" . (int)$parent_id . "' and sud.subscription_id=suTsuc.subscription_id and sud.language_id='" . (int)$languages_id . "'");
	while ($subscription=tep_db_fetch_array($subscription_query))
    {
        $subscription_category_tree_array[] = array('id' => $subscription['subscription_id'], 'text' => $spacing . $subscription['subscription_name'] . '(subscription)');
    }
    return $subscription_category_tree_array;
  }


// send email to customer regarding the order
function tep_send_subscription_order_email($order_id){
	global $currencies,$referenceID;

	if ((int)EMAIL_ACTIVATE!=1) return;

	if ((int)$order_id<=0)	return;
	$url_home=(ENABLE_SSL == 'true'?HTTPS_SERVER . DIR_WS_CATALOG:HTTP_SERVER . DIR_WS_CATALOG);
	// fetch order details
	$order_query=tep_db_query("select c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_dob,c.customers_telephone,c.customers_fax,c.customers_second_telephone,c.customers_second_email_address,
					  o.customers_street_address,c.customers_email_address,o.customers_suburb,o.customers_postcode,o.customers_city,o.orders_id,o.date_purchased,o.billing_name,o.billing_street_address,o.payment_method,
					  o.customers_state,o.customers_country from " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c where o.orders_id='" . (int)$order_id . "' and o.customers_id=c.customers_id");

	if (tep_db_num_rows($order_query)<=0) return;    
	$order_result=tep_db_fetch_array($order_query);

	//fetch subscription added to order
	$subscription_query=tep_db_query("SELECT op.final_price,op.products_id,op.products_tax,op.products_name,su.subscription_period,su.subscription_costs," .
									" date_add(o.date_purchased,interval su.subscription_period-su.subscription_renewal_end day) as renewal_end_date,su.subscription_tax_class_id,su.subscription_type,os.start_date,os.finish_date from " . 
									TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_ORDERS_SUBSCRIPTIONS ." os " .
							 		" where o.orders_id='" . $order_id ."' and o.orders_id=op.orders_id and op.products_type='S' and op.products_id=su.subscription_id and o.orders_id = os.orders_id");
	// get mail template details
	$details_r=array();
	$details_r['type']='RCF';
	$details_r['table']=TABLE_SUBSCRIPTION_MESSAGES;

	
	while($subscription_result=tep_db_fetch_array($subscription_query))
	{
		$details_r['subscription_id']=$subscription_result['products_id'];
		tep_get_template($details_r);
		
	//for direct deposit module
	if($GLOBALS['payment']=='ausbank')
		$direct_deposit=sprintf(TEXT_DIRECT_DEPOSIT,$referenceID );
	else 
		$direct_deposit='';	
		
	if($subscription_result['subscription_type']=='C'){
		$subscription_type=TEXT_CASUAL;
	}else if($subscription_result['subscription_type']=='S'){
		$subscription_type=TEXT_SEASON;
	}else if($subscription_result['subscription_type']=='D'){
		$subscription_type=TEXT_DEGITAL;
	}else if($subscription_result['subscription_type']=='T'){
		$subscription_type=TEXT_TANGIBILE;
	}	
		// form the merge fields
		$replace_array=array(	TEXT_FN=>$order_result['customers_firstname'],
								TEXT_LN=>$order_result['customers_lastname'],
								TEXT_DF=>format_date($order_result['customers_dob']),
								TEXT_EM=>'<a href="mailto:' . $order_result['customers_email_address'] . '"><u>' . $order_result['customers_email_address'] .'</u></a>',
								TEXT_TN=>$order_result['customers_telephone'],
								TEXT_FX=>$order_result['customers_fax'],
								TEXT_SA=>$order_result['customers_street_address'],
								TEXT_SU=>$order_result['customers_suburb'],
								TEXT_PC=>$order_result['customers_postcode'],
								TEXT_CT=>$order_result['customers_city'],
								TEXT_ST=>$order_result['customers_state'],
								TEXT_CY=>$order_result['customers_country'],
								TEXT_RE=>$subscription_result['renewal_end_date'],
								TEXT_SUB_NA=>$subscription_result['products_name'],
								TEXT_SUB_CO=>$currencies->format(tep_add_tax($subscription_result['subscription_costs'],tep_get_tax_rate($subscription_result['subscription_tax_class_id']))),
								TEXT_SUB_PE=>$subscription_result["subscription_period"],
								TEXT_IV=>'<a href="' . tep_catalog_href_link(FILENAME_SUBSCRIPTION_INVITE_FRIEND,'R=1&path=ACC&suID=' . $subscription_result['products_id'],'NONSSL',false) . '">' . TEST_MAIL_SUB_IV . '</a>',
								TEXT_UN=>'<a href="' . tep_catalog_href_link(FILENAME_SUBSCRIPTION_UNSUBSCRIBE,'R=1&cID=' . $order_result['customers_id'],'NONSSL',false) . '">' . TEST_MAIL_SUB_UN . '</a>',
								TEXT_SUB_RL=>'<a href="' . tep_catalog_href_link(FILENAME_ACCOUNT,'R=1','NONSSL',false) . '">' . TEST_MAIL_SUB_RL . '</a>',
								TEXT_IL=>'<a href="' . tep_catalog_href_link(FILENAME_ACCOUNT_HISTORY_INFO,'R=1&order_id=' . $order_id,'NONSSL',false) . '">' . TEST_MAIL_SUB_IL . '</a>',
								TEXT_SL=>'<a href="../index.php" style="font-family:Arial, Helvetica, sans-serif;font-size:13px;color:#3966B1;font-weight:bold;text-decoration:none;">' . STORE_OWNER . '</a>',
								TEXT_SP=>'<img src="' . $url_home . 'admin/images/freeway.png" title="' . STORE_NAME . '">',
								TEXT_ON=>$order_result['orders_id'],
								TEXT_OC=>$order_result['orders_comments'],
								TEXT_OD=>format_date($order_result['date_purchased']),
								TEXT_BT=>$order_result['billing_name'],
								TEXT_BA=>$order_result['billing_street_address'],
								TEXT_PY=>$order_result['payment_method'],
								TEXT_DD=>$direct_deposit,
								TEXT_TX=>$currencies->format(tep_calculate_tax($subscription_result['final_price'],$subscription_result['products_tax'])),
								TEXT_SB=>$currencies->format($subscription_result['final_price']),
								TEXT_TL=>$currencies->format(tep_add_tax($subscription_result['final_price'],$subscription_result['products_tax'])),
								TEXT_SUB_TY=>$subscription_type,
								TEXT_SUB_SD=>$subscription_result['start_date'],
								TEXT_SUB_FD=>$subscription_result['finish_date']
								);
							

	$sql_array=array(	'orders_id'=>$order_id,
						'subscription_id'=>$subscription_result['products_id'],
						'customers_id'=>$order_result["customers_id"],
						'message_type'=>'RCF',
						'message_mode'=>'E',
						'email_address'=>$order_result["customers_email_address"],
						'send_date'=>'now()'
					);
		// replace template
		if ($details_r['html_text']!='') {
			tep_replace_template($details_r,$replace_array);
			tep_strip_html($details_r);
			$details_r['to_name']=$order_result['customers_firstname'] . ' ' . $order_result['customers_lastname'];
			$details_r['to_email']=$order_result['customers_email_address'];
			$details_r['from_name']=STORE_OWNER;
			$details_r['from_email']=STORE_OWNER_EMAIL_ADDRESS;
			tep_send_email($details_r);
			
			if (SEND_EXTRA_ORDER_EMAILS_TO != ''){
			$details_r['to_email']=SEND_EXTRA_ORDER_EMAILS_TO;
			tep_send_email($details_r);
			}
			tep_db_perform(TABLE_SUBSCRIPTION_MESSAGES_HISTORY,$sql_array);
		}
	}				
}  

// send subscription test email 
function tep_send_subscription_test_email($mail_type,$subscription_id){
	global $currencies;
	
	if ($mail_type=='' || (int)$subscription_id<=0) return;

	$mail_data_query=tep_db_query("SELECT * from " . TABLE_SUBSCRIPTION_MESSAGES . " where message_type='" . $mail_type . "' and subscription_id='" . (int)$subscription_id . "'");

	if (tep_db_num_rows($mail_data_query)<=0) return;

	$mail_data_result=tep_db_fetch_array($mail_data_query);
     
	$mes_text=tep_replace_test_mail_content($mail_data_result['message_text']);

	$text = strip_tags($mes_text,'<br>');
	$text = str_replace('<br>',chr(13) .chr(10),$text);
	$text = str_replace('<BR>',chr(13) .chr(10),$text);
  
	$message = new email(array('X-Mailer: FreeWay'));


     if ($mail_data_result['message_format'] != 'T') {
	 	$message->add_html($mes_text, $text);
     } else {
	 	$message->add_text($text);
     }
     $message->build_message();
	 $message->send(TEST_MAIL_FN . ' ' . TEST_MAIL_LN , TEST_MAIL_EM, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, $mail_data_result['message_subject']);
}

// send event test sms email
function tep_send_subscription_test_sms_email($mail_type,$subscription_id)
{
  if ($mail_type=='' || (int)$subscription_id<=0)
     return;

   $mail_data_query=tep_db_query("SELECT message_text from " . TABLE_SUBSCRIPTION_SMS_MESSAGES . " where message_type='" . $mail_type . "' and subscription_id='" . (int)$subscription_id . "'");
     if (tep_db_num_rows($mail_data_query)<=0) return;
   
   $mail_data_result=tep_db_fetch_array($mail_data_query);

   $mes_text=tep_replace_test_mail_content($mail_data_result['message_text']);

     $text = strip_tags($mes_text,'<br>');
     
     $text = str_replace('<br>',chr(13) .chr(10),$text);
     $text = str_replace('<BR>',chr(13) .chr(10),$text); 
	 
	  	 if(defined('DEBUGGING_MODE_VIEW') && (DEBUGGING_MODE_VIEW=='1' || DEBUGGING_MODE_VIEW=='true')){
		 	echos(nl2br($mes_text));
		 }
  		else
     		send_sms(SMS_TEST_PHONE,SMS_FROM_NAME,$text,SMS_TEST_COUNTRY);
}
//subscription discount start
function subscription_discount($subscription_id,$date,$fees)
{
  $discount_amount=0;
  $discount_query=tep_db_query("SELECT discount_value,discount_type from " . TABLE_SUBSCRIPTIONS_DISCOUNTS . " where subscription_id='" . $subscription_id . "' and '" . $date . "'>=discount_start_date and '" . $date . "'<=discount_end_date order by discount_start_date");
  if (tep_db_num_rows($discount_query)){
    $discount_result=tep_db_fetch_array($discount_query);
	// P=>Percentage A=>Amount
    if ($discount_result['discount_type']=='P'){
       $discount_amount=$fees*$discount_result['discount_value']/100;
    } else {
       $discount_amount=$discount_result['discount_value'];
    }
  }
  return $discount_amount;   
}

//end

?>
