<?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_category_title_top($title,$category_id,$show=false,$level=0){
	$image='panel_down.gif';
	$child_show="style='display:none'";
	if ($show) {
		$image='panel_up.gif'; 
		$child_show="";
	}
  	echo '<tr id="tr_id_'.$category_id.'">
			<td valign="top">
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr height="20">
					<td>
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
						<tr>
							<td class="contentTitle1" style="cursor:pointer;cursor:hand;" onClick="javascript:toggle_category_panel(' . $category_id .','. $level . ');"><span id="span_category_'.$category_id.'" style="background:#FFFFFF;padding-right:5px;' . ($level>0?"padding-left:" . ($level*5) ." px":'') . '">' . $title  . '</span></td><td width="15" align="right"><a href="javascript:toggle_category_panel(' . $category_id .','. $level . ');"><img src="images/template/' .$image .'" id="panel_' . $category_id . '_img" border="0"></a></td>
						</tr>
					</table>
					</td>
				</tr>
				<tr height="5">
					<td></td>
				</tr>
				<input type="hidden" id="panel_' . $category_id . '_pgno" value="1">
				<tr><td style="padding-left:' . (($level+1)*15) .'px">
				<div id="panel_' . $category_id .'_content"' . $child_show. '>
			';
  }
  function tep_category_title_bottom(){
  	echo '
				</div>
				</td>
			</tr>
  			</table>
			</td>
		  </tr>';
  }
  
  //Support Packs
  
function tep_send_support_pack_default_email($type,$orders_id){
	global $order;
	$packs_query=tep_db_query("select opsp.subscription_id,spl.license_code,opsp.subscription_name from " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp, " . TABLE_SUPPORT_PACKS_LICENSE . " spl where spl.support_packs_id=opsp.support_packs_id and opsp.orders_id='" . $orders_id . "'");
	if(tep_db_num_rows($packs_query)<=0) return;
	
	//define('DEBUGGING_MODE_VIEW',true);
	$details=array();
	$details['type']=$type;
	$details['table']=TABLE_EMAIL_MESSAGES;
	tep_get_template($details);      	  // get template using type and table   
	
	$prev_id='';
	while ($packs_result=tep_db_fetch_array($packs_query)){
		$license_code.=sprintf('%05d',$packs_result['license_code']) .',';
		if($prev_id!=$packs_result['subscription_id']) {
			$packs_list.=$packs_result['subscription_name']. ",";
			$prev_id=$packs_result['subscription_id'];
		}	
	}
	if($license_code!='') $license_code=substr($license_code,0,-1);
	if($packs_list!='') $packs_list=substr($packs_list,0,-1);
	
	$merge_details=array(CUST_CF=>$order->customer['firstname'],
						CUST_CL=>$order->customer['lastname'],
						CUST_CM=>$order->customer['company'],
						TEXT_SM=>STORE_NAME,
						TEXT_SN=>STORE_OWNER,
						TEXT_SE=>STORE_OWNER_EMAIL_ADDRESS,
						TEXT_AL=>STORE_OWNER_EMAIL_ADDRESS,
						CUST_CT=>$order->customer['street_address'],
						CUST_CP=>$order->customer['postcode'],
						CUST_CC=>$order->customer['city'],
						CUST_CS=>$order->customer['suburb'],
						CUST_CE=>$order->customer['state'],
						CUST_CU=>$order->customer['country']['title'],
						CUST_CO=>$order->customer['telephone'],
						CUST_CA=>'<a href="mailto:' . $order->customer['email_address'] . '"><u>' . $order->customer['email_address'] .'</u></a>',
						TEXT_LC=>$license_code,
						TEXT_SPL=>$packs_list
					);		
	
	tep_replace_template($details,$merge_details); // replace template content with merge details
	tep_strip_html($details); // strip tags;

    //send mails to given addresses
		$details['to_name']=$order->customer['firstname'] . ' ' . $order->customer['lastname'];
		$details['to_email']=$order->customer['email_address'];
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		tep_send_email($details,true); // send email
}	
  
function tep_show_support_packs($product_id) {
	global $languages_id;
	$pack_query=tep_db_query("select sud.subscription_name,su.support_minutes,psp.no_users from  " . TABLE_PRODUCTS . " p, " .TABLE_SUBSCRIPTIONS_DESCRIPTION  . " sud, " . TABLE_SUBSCRIPTIONS ." su, " . TABLE_PRODUCTS_TO_SUPPORT_PACKS . " psp where sud.subscription_id=su.subscription_id and sud.subscription_id=psp.subscription_id and psp.product_id=p.products_id and sud.language_id='" . $languages_id . "' and p.products_id='" . $product_id . "'");
		if(tep_db_num_rows($pack_query)>0) { 
			$result='<table cellpadding="0" cellspacing="0" border="0" width="100%">';
			$result.='<tr><td class="dataTableContent"><b>Support Packs: </b></td></tr>';
			while($pack_result=tep_db_fetch_array($pack_query)){
				 $result.='<tr><td class="dataTableContent"><b>' . $pack_result['support_minutes'] . ' minutes ' . $pack_result['subscription_name'] . ' for ' . $pack_result['no_users'].' users</b></td></tr>';
			}
			$result.='</table>';
		}
		return $result;
	}
	
function tep_get_plain_products_price($products_price){
     global $customer_id;
	  if (!tep_session_is_registered('customer_id')) {
		 $customer_discount = 0;
		 if (defined("GUEST_DISCOUNT")) $customer_discount=(int)GUEST_DISCOUNT;
		 if ($customer_discount >= 0) {
			$products_price = $products_price + $products_price * abs($customer_discount) / 100;
		 } else {
			$products_price = $products_price - $products_price * abs($customer_discount) / 100;
		 }
		 return $products_price;
		 
	  } elseif (tep_session_is_registered('customer_id')) {
		 $query = tep_db_query("select g.customers_groups_discount from " . TABLE_CUSTOMERS_GROUPS . " g inner join  " . TABLE_CUSTOMERS  . " c on g.customers_groups_id = c.customers_groups_id and c.customers_id = '" . $customer_id . "'");
		 $query_result = tep_db_fetch_array($query);
		 $customers_groups_discount = $query_result['customers_groups_discount'];
		 $query = tep_db_query("select customers_discount from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
		 $query_result = tep_db_fetch_array($query);
		 $customer_discount = $query_result['customers_discount'];
		 $customer_discount = $customer_discount + $customers_groups_discount;

		 if ($customer_discount >= 0) {
			$products_price = $products_price + $products_price * abs($customer_discount) / 100;
		 } else {
			$products_price = $products_price - $products_price * abs($customer_discount) / 100;
		 }
		return $products_price;
	  }
}

  function tep_get_products_spl_price($product_id,$sale_id=0,$cart_item_id='') {
  	global $customer_id,$cart;
    $product_query = tep_db_query("select products_price, products_model,products_price_break from " . TABLE_PRODUCTS . " where products_id = '" .(int)$product_id . "'");
    if (tep_db_num_rows($product_query)) {
      $product = tep_db_fetch_array($product_query);
	  $product_price = $product['products_price'];
	  if ($product["products_price_break"]=='Y') return false;
    } else {
	  return false;
    }
	$sDate=getServerDate();

	if (!isset($customer_id)) $customer_id = 0;
    $customer_groups_id = tep_get_customers_groups_id($customer_id);

	$specials_query = tep_db_query("select specials_new_products_price,customers_id,customers_groups_id from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status = '1'");
	

    if (tep_db_num_rows($specials_query)) {
      $special = tep_db_fetch_array($specials_query);
	  	if ($customer_id>0 && $customer_id==$special["customers_id"]){
		  $special_price = $special['specials_new_products_price'];
		} else if ($customer_groups_id>0 && $customer_groups_id==$special["customers_groups_id"]){
		  $special_price = $special['specials_new_products_price'];
		} else if ($special["customers_id"]<=0 && $special["customers_groups_id"]<=0){
		  $special_price = $special['specials_new_products_price'];
		} else {
			$special_price=false;
		}
    } else {
	  $special_price = false;
    }
	
    if(substr($product['products_model'], 0, 4) == 'GIFT') {    //Never apply a salededuction to Ian Wilson's Giftvouchers
      return $special_price;
    }

    $product_to_categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$product_id . "'");
    $product_to_categories = tep_db_fetch_array($product_to_categories_query);
    $category = $product_to_categories['categories_id'];

	$option_where='';
	if ($sale_id>0) $option_where=" sale_id=" . $sale_id . " and ";
	else $option_where=" sale_discount_type='S' and ";
//echo "select sale_specials_condition, sale_deduction_value, sale_deduction_type, sale_id from " . TABLE_SALEMAKER_SALES . " where " . $option_where . " ((sale_categories_all='' and sale_products_selected='') or sale_categories_all like '%," . $category . ",%' or sale_products_selected like '%," . (int)$product_id .",%') and sale_status = '1' and (sale_date_start <= '" . $sDate. "' or sale_date_start = '0000-00-00') and (sale_date_end >= '" . $sDate . "' or sale_date_end = '0000-00-00') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')";
    $sale_query = tep_db_query("select sale_specials_condition, sale_deduction_value, sale_deduction_type, sale_discount_type,sale_id from " . TABLE_SALEMAKER_SALES . " where " . $option_where . " ((sale_categories_all='' and sale_products_selected='') or sale_categories_all like '%," . $category . ",%' or sale_products_selected like '%," . (int)$product_id .",%') and sale_status = '1' and (sale_date_start <= '" . $sDate. "' or sale_date_start = '0000-00-00') and (sale_date_end >= '" . $sDate . "' or sale_date_end = '0000-00-00') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')");
    if (tep_db_num_rows($sale_query)) {
      $sale = tep_db_fetch_array($sale_query);
    } else {
	  return $special_price;
    }
    if (!$special_price) {
      $tmp_special_price = $product_price;
    } else {
      $tmp_special_price = $special_price;
    }
	if($cart_item_id!='' && $sale['sale_discount_type']=='S')
		$cart->contents[$cart_item_id]['salemaker_id']=$sale['sale_id'];
		
    switch ($sale['sale_deduction_type']) {
      case 0:
        $sale_product_price = $product_price - $sale['sale_deduction_value'];
        $sale_special_price = $tmp_special_price - $sale['sale_deduction_value'];
        break;
      case 1:
        $sale_product_price = $product_price - (($product_price * $sale['sale_deduction_value']) / 100);
        $sale_special_price = $tmp_special_price - (($tmp_special_price * $sale['sale_deduction_value']) / 100);

        break;
      case 2:
        $sale_product_price = $sale['sale_deduction_value'];
        $sale_special_price = $sale['sale_deduction_value'];
        break;
      default:
        return $special_price;
    }

    if ($sale_product_price < 0) {
      $sale_product_price = 0;
    }

    if ($sale_special_price < 0) {
      $sale_special_price = 0;
    }

    if (!$special_price) {
      return number_format($sale_product_price, 4, '.', '');
	} else {
      switch($sale['sale_specials_condition']){
        case 0:
          return number_format($sale_product_price, 4, '.', '');
          break;
        case 1:
          return number_format($special_price, 4, '.', '');
          break;
        case 2:
          return number_format($sale_special_price, 4, '.', '');
          break;
        default:
          return number_format($special_price, 4, '.', '');
      }
    }
  }
  
  
  function tep_get_salemaker_price($special_price,$product_price,$sale){
    if (!$special_price) {
      $tmp_special_price = $product_price;
    } else {
      $tmp_special_price = $special_price;
    }

    switch ($sale['sale_deduction_type']) {
      case 0:
        $sale_product_price = $product_price - $sale['sale_deduction_value'];
        $sale_special_price = $tmp_special_price - $sale['sale_deduction_value'];
        break;
      case 1:
        $sale_product_price = $product_price - (($product_price * $sale['sale_deduction_value']) / 100);
        $sale_special_price = $tmp_special_price - (($tmp_special_price * $sale['sale_deduction_value']) / 100);
        break;
      case 2:
        $sale_product_price = $sale['sale_deduction_value'];
        $sale_special_price = $sale['sale_deduction_value'];
        break;
      default:
        return $special_price;
    }

    if ($sale_product_price < 0) {
      $sale_product_price = 0;
    }

    if ($sale_special_price < 0) {
      $sale_special_price = 0;
    }

    if (!$special_price) {
      return number_format($sale_product_price, 4, '.', '');
	} else {
      switch($sale['sale_specials_condition']){
        case 0:
          return number_format($sale_product_price, 4, '.', '');
          break;
        case 1:
          return number_format($special_price, 4, '.', '');
          break;
        case 2:
          return number_format($sale_special_price, 4, '.', '');
          break;
        default:
          return number_format($special_price, 4, '.', '');
      }
    }
  }
  function tep_get_products_special_price_only($products_id){
  	global $customer_id;
  	if (!isset($customer_id)) $customer_id = 0;
	
    $customer_groups_id = tep_get_customers_groups_id($customer_id);
	$specials_query = tep_db_query("select specials_new_products_price,customers_id,customers_groups_id from " . TABLE_SPECIALS . " where products_id = '" . (int)$products_id . "' and status = '1'");

		if (tep_db_num_rows($specials_query)) {
		  $special = tep_db_fetch_array($specials_query);
			if ($customer_id>0 && $customer_id==$special["customers_id"]){
			  $special_price = $special['specials_new_products_price'];
			} else if ($customer_groups_id>0 && $customer_groups_id==$special["customers_groups_id"]){
			  $special_price = $special['specials_new_products_price'];
			} else if ($special["customers_id"]<=0 && $special["customers_groups_id"]<=0){
			  $special_price = $special['specials_new_products_price'];
			} else {
				$special_price=false;
			}
		} else {
		  $special_price = false;
		}
		return $special_price;
	}
 function get_discount_details($orders_id,$product_id){
 global $currencies;
  	$return='';   
    $product_query = tep_db_query("select discount_type,discount_text,products_price,final_price,products_tax,products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id='" . $orders_id . "' and products_id='" . $product_id . "'" );
	
	$product_result=tep_db_fetch_array($product_query);
	if($product_result['discount_type']=='C' || $product_result['discount_type']=='S') {
		$product_to_categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $product_id . "'");
		$product_to_categories = tep_db_fetch_array($product_to_categories_query);
		$category = $product_to_categories['categories_id'];
		$fp=$product_result["final_price"];
		$tax=$product_result["products_tax"];
		$qty=$product_result["products_quantity"];
		//$tax_rate=tep_get_tax_rate($products_result['products_price'],$tax);
		$price=$product_result['products_price'];
		$product_amount=tep_add_tax(($price * $product_result['products_quantity']),$tax);
		$final_amount=tep_add_tax(($fp*$product_result['products_quantity']),$tax);
		$total=($product_amount-$final_amount);
		
			$return .=TEXT_DISCOUNT_APPLIED . '&nbsp;&nbsp;'. $product_result['discount_text']  . '<br>';
			$return .=TEXT_ORIGINAL_AMOUNT . ' &nbsp;&nbsp; '. $currencies->format($product_amount) . '<br>';
			$return .=TEXT_DISCOUNT . ' &nbsp;&nbsp; '. $currencies->format($total);
	}	
	return $return;
 	
 }

	
?>