<?php
/*
#####################################
#  ShopOS: Shopping Cart Software.
#  Copyright (c) 2008-2010
#  http://www.shopos.ru
#  http://www.shoposs.com
#  Ver. 1.0.2
#####################################
*/

class osPrice {
	var $currencies;

	function osPrice($currency, $cGroup) {

		$this->currencies = array ();
		$this->cStatus = array ();
		$this->actualGroup = $cGroup;
		$this->actualCurr = $currency;
		$this->TAX = array ();
		$this->SHIPPING = array();
		$this->showFrom_Attributes = true;

        global $default_cache;
		
		if (!isset($default_cache['currencies']))
	    {
		    		
		    $currencies_query = "SELECT * FROM ".TABLE_CURRENCIES;
		    $currencies_query = osDBquery($currencies_query);
		    while ($currencies = os_db_fetch_array($currencies_query, true)) 
			{
			     $this->currencies[$currencies['code']] = array 
				 (
			        'title' => $currencies['title'], 
			        'symbol_left' => $currencies['symbol_left'], 
			        'symbol_right' => $currencies['symbol_right'], 
			        'decimal_point' => $currencies['decimal_point'], 
			        'thousands_point' => $currencies['thousands_point'], 
			        'decimal_places' => $currencies['decimal_places'], 
			        'value' => $currencies['value']
			     );
		    }
		}
		else
		{
		   if (!empty($default_cache['currencies']))
		   {
			foreach ($default_cache['currencies'] as $code_value => $_value) 
			{
			   $this->currencies[$code_value] = $_value;
			}
		    }
		}
		
		 //cache get_customers_status
		$customers_status_value = get_customers_status($this->actualGroup);
		
		$this->cStatus = array (
		
		'customers_status_id' => $this->actualGroup, 
		'customers_status_name' => $customers_status_value['customers_status_name'], 
		'customers_status_image' => $customers_status_value['customers_status_image'], 
		'customers_status_public' => $customers_status_value['customers_status_public'], 
		'customers_status_discount' => $customers_status_value['customers_status_discount'], 
		'customers_status_ot_discount_flag' => $customers_status_value['customers_status_ot_discount_flag'], 
		'customers_status_ot_discount' => $customers_status_value['customers_status_ot_discount'], 
		'customers_status_graduated_prices' => $customers_status_value['customers_status_graduated_prices'], 
		'customers_status_show_price' => $customers_status_value['customers_status_show_price'], 
		'customers_status_show_price_tax' => $customers_status_value['customers_status_show_price_tax'], 
		'customers_status_add_tax_ot' => $customers_status_value['customers_status_add_tax_ot'], 
		'customers_status_payment_unallowed' => $customers_status_value['customers_status_payment_unallowed'], 
		'customers_status_shipping_unallowed' => $customers_status_value['customers_status_shipping_unallowed'], 
		'customers_status_discount_attributes' => $customers_status_value['customers_status_discount_attributes'], 
		'customers_fsk18' => $customers_status_value['customers_fsk18'], 
		'customers_fsk18_display' => $customers_status_value['customers_fsk18_display']
		
		);
        
		if (!isset($default_cache['tax_class_id']))
		{
		$zones_query = osDBquery("SELECT tax_class_id as class FROM ".TABLE_TAX_CLASS);
		while ($zones_data = os_db_fetch_array($zones_query,true)) 
		{
			if (isset($_SESSION['billto']) && isset($_SESSION['sendto'])) 
			{
			    $tax_address_query = os_db_query("select ab.entry_country_id, ab.entry_zone_id from " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) where ab.customers_id = '" . $_SESSION['customer_id'] . "' and ab.address_book_id = '" . ($this->content_type == 'virtual' ? $_SESSION['billto'] : $_SESSION['sendto']) . "'");
      		    $tax_address = os_db_fetch_array($tax_address_query);
			    $this->TAX[$zones_data['class']]=os_get_tax_rate($zones_data['class'],$tax_address['entry_country_id'], $tax_address['entry_zone_id']);				
			} 
			else 
			{
			    $this->TAX[$zones_data['class']]=os_get_tax_rate($zones_data['class']);		
			}
		}
		}
		
		else
		{
		   if (isset($default_cache['tax_class_id']) && !empty($default_cache['tax_class_id']))
		   {
		   foreach ($default_cache['tax_class_id'] as $_val => $__val)
		   {
		      $zones_data['class'] = $_val;
			  
		      if (isset($_SESSION['billto']) && isset($_SESSION['sendto'])) 
			  {
			    $tax_address_query = os_db_query("select ab.entry_country_id, ab.entry_zone_id from " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) where ab.customers_id = '" . $_SESSION['customer_id'] . "' and ab.address_book_id = '" . ($this->content_type == 'virtual' ? $_SESSION['billto'] : $_SESSION['sendto']) . "'");
      		    $tax_address = os_db_fetch_array($tax_address_query);
			    $this->TAX[$zones_data['class']]=os_get_tax_rate($zones_data['class'],$tax_address['entry_country_id'], $tax_address['entry_zone_id']);				
			  } 
			  else 
			  {
			    $this->TAX[$zones_data['class']]=os_get_tax_rate($zones_data['class']);		
			  }
		   }
		   }
		}
		
				
	}

	function GetPrice($pID, $format = true, $qty, $tax_class, $pPrice, $vpeStatus = 0, $cedit_id = 0) 
	{

	if ($this->cStatus['customers_status_show_price'] == '0')
			return $this->ShowNote($vpeStatus, $vpeStatus);

		if ($cedit_id != 0) 
		{
			$cinfo = os_customer_infos($cedit_id);
			$products_tax = os_get_tax_rate($tax_class, $cinfo['country_id'], $cinfo['zone_id']);
		} 
		else 
		{
		    if (isset( $this->TAX[$tax_class])) $products_tax = $this->TAX[$tax_class]; else $products_tax = 0;
		}

		if ($this->cStatus['customers_status_show_price_tax'] == '0')
			$products_tax = '';

		if ($pPrice == 0) $pPrice = $this->getPprice($pID);
		
		$pPrice = $this->AddTax($pPrice, $products_tax);

		if ($sPrice = $this->CheckSpecial($pID)) 
		{
		   return $this->FormatSpecial($pID, $this->AddTax($sPrice, $products_tax), $pPrice, $format, $vpeStatus);
		}

		if ($this->cStatus['customers_status_graduated_prices'] == '1') 
		{
			
			if ($sPrice = $this->GetGraduatedPrice($pID, $qty)) return $this->FormatSpecialGraduated($pID, $this->AddTax($sPrice, $products_tax), $pPrice, $format, $vpeStatus, $pID);
		} 
		else 
		{
			if ($sPrice = $this->GetGroupPrice($pID, 1)) return $this->FormatSpecialGraduated($pID, $this->AddTax($sPrice, $products_tax), $pPrice, $format, $vpeStatus, $pID);
		}

		if ($discount = $this->CheckDiscount($pID))
			return $this->FormatSpecialDiscount($pID, $discount, $pPrice, $format, $vpeStatus);

		return $this->Format($pPrice, $format, 0, false, $vpeStatus, $pID);

	}

	function getPprice($pID) {
		$pQuery = "SELECT products_price FROM ".TABLE_PRODUCTS." WHERE products_id='".$pID."'";
		$pQuery = osDBquery($pQuery);
		$pData = os_db_fetch_array($pQuery, true);
		return $pData['products_price'];


	}

	function AddTax($price, $tax) {
		$price = $price + $price / 100 * $tax;
		$price = $this->CalculateCurr($price);
		return round($price, $this->currencies[$this->actualCurr]['decimal_places']);
	}

	function CheckDiscount($pID) {

		if ($this->cStatus['customers_status_discount'] != '0.00') {

			$discount_query = "SELECT products_discount_allowed FROM ".TABLE_PRODUCTS." WHERE products_id = '".$pID."'";
			$discount_query = osDBquery($discount_query);
			$dData = os_db_fetch_array($discount_query, true);

			$discount = $dData['products_discount_allowed'];
			if ($this->cStatus['customers_status_discount'] < $discount)
				$discount = $this->cStatus['customers_status_discount'];
			if ($discount == '0.00')
				return false;
			return $discount;

		}
		return false;
	}

	function GetGraduatedPrice($pID, $qty) 
	{
	    global $_graduated_price;
	    global $_graduated_personal_offer;
		global $_products_array;
		
		$pID = (int) $pID;
		
		if (empty($pID)) return;
		
		if (empty($this->actualGroup)) return;
		
		
        $__products_array = $_products_array;
		
	    if (GRADUATED_ASSIGN == 'true') if (os_get_qty($pID) > $qty) $qty = os_get_qty($pID);
		
		if (!isset($_graduated_price[$this->actualGroup][$pID][$qty]))
		{
			$sql = '';
			if (!empty($__products_array))
			{
                foreach ($__products_array as $val => $_val)
                {
		           if (empty($sql))  $sql .= $val; else $sql .= ','.$val;
				   $vals[] = $val;            
                }
				
				if (!in_array($pID, $vals))
				{
				     if (empty($sql))  $sql .= $pID; else $sql .= ','.$pID;
					 $vals[] = $pID;
				}
				
				$sql = 'products_id in ('.$sql.')';
			}
			else
			{
			    $sql = 'products_id in ('.$pID.')';
			}

			$product_query = osDBquery("SELECT products_id, max(quantity) as qty FROM ".TABLE_PERSONAL_OFFERS_BY.$this->actualGroup." WHERE ".$sql ." and quantity<='".$qty."' GROUP BY products_id");
				 
	         if (os_db_num_rows($product_query,true)) 
	         {
		           while ($products = os_db_fetch_array($product_query,true))  
                   {
	                    $_graduated_price[$this->actualGroup][$products['products_id']][$qty] = array('qty' => $products['qty']);
                   } 
	         }
		     else
		     {
		          $_graduated_price[$this->actualGroup][$pID][$qty] = array('qty' => null);
		     }
			 
			 if (!empty($vals))
			 {
			    foreach ($vals as $val)
                {
				    if (!isset($_graduated_price[$this->actualGroup][$val][$qty]))
					{
		                $_graduated_price[$this->actualGroup][$val][$qty] = array('qty' => null); 
                    }					   
                }
			 }
		}
		
		$graduated_price_data = $_graduated_price[$this->actualGroup][$pID][$qty];

		if (!empty($graduated_price_data['qty'])) 
		{
		    if (!isset($_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']]))
			{
			   $graduated_price_query = "SELECT personal_offer FROM ".TABLE_PERSONAL_OFFERS_BY.$this->actualGroup." WHERE products_id='".$pID."' AND quantity='".$graduated_price_data['qty']."'";
			   $graduated_price_query = osDBquery($graduated_price_query);
			   $_graduated_price_data = os_db_fetch_array($graduated_price_query, true);
			   $_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']] = $_graduated_price_data;
            }
			else
			{
			   $_graduated_price_data = $_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']];
			}
			
			$sPrice = $_graduated_price_data['personal_offer'];
			
			if ($sPrice != 0.00) return $sPrice;
		} 
		else 
		{
			return;
		}

		
	}

	function GetGroupPrice($pID, $qty) 
	{
	    global $_graduated_price;
	    global $_graduated_personal_offer;
		global $_products_array;
		
        $pID = (int) $pID;

        $__products_array = $_products_array;

		if (!isset($_graduated_price[$this->actualGroup][$pID][$qty]))
		{
			$sql = '';
			if (!empty($__products_array))
			{
                foreach ($__products_array as $val => $_val)
                {
		           if (empty($sql))  $sql .= $val; else $sql .= ','.$val;
				   $vals[] = $val;            
                }
				
				if (!in_array($pID, $vals))
				{
				     if (empty($sql))  $sql .= $pID; else $sql .= ','.$pID;
					 $vals[] = $pID;
				}
				
				$sql = 'products_id in ('.$sql.')';
			}
			else
			{
			    $sql = 'products_id in ('.$pID.')';
			}

			$product_query = osDBquery("SELECT products_id, max(quantity) as qty FROM ".TABLE_PERSONAL_OFFERS_BY.$this->actualGroup." WHERE ".$sql ." and quantity<='".$qty."' GROUP BY products_id");
				 
	         if (os_db_num_rows($product_query,true)) 
	         {
		           while ($products = os_db_fetch_array($product_query,true))  
                   {
	                    $_graduated_price[$this->actualGroup][$products['products_id']][$qty] = array('qty' => $products['qty']);
                   } 
	         }
		     else
		     {
		          $_graduated_price[$this->actualGroup][$pID][$qty] = array('qty' => null);
		     }
			 
			 if (!empty($vals))
			 {
			    foreach ($vals as $val)
                {
				    if (!isset($_graduated_price[$this->actualGroup][$val][$qty]))
					{
		                $_graduated_price[$this->actualGroup][$val][$qty] = array('qty' => null); 
                    }					   
                }
			 }
		}
		
		$graduated_price_data = $_graduated_price[$this->actualGroup][$pID][$qty];

		if (!empty($graduated_price_data['qty'])) 
		{
		    if (!isset($_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']]))
			{
			   $graduated_price_query = "SELECT personal_offer FROM ".TABLE_PERSONAL_OFFERS_BY.$this->actualGroup." WHERE products_id='".$pID."' AND quantity='".$graduated_price_data['qty']."'";
			   $graduated_price_query = osDBquery($graduated_price_query);
			   $_graduated_price_data = os_db_fetch_array($graduated_price_query, true);
			   $_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']] = $_graduated_price_data;
            }
			else
			{
			   $_graduated_price_data = $_graduated_personal_offer[$this->actualGroup][$pID][$graduated_price_data['qty']];
			}
			
			$sPrice = $_graduated_price_data['personal_offer'];
			
			if ($sPrice != 0.00) return $sPrice;
		} 
		else 
		{
			return;
		}

	}

	function GetOptionPrice($pID, $option, $value) {
		$attribute_price_query = "select pd.products_discount_allowed,pd.products_tax_class_id, p.options_values_price, p.price_prefix, p.options_values_weight, p.weight_prefix from ".TABLE_PRODUCTS_ATTRIBUTES." p, ".TABLE_PRODUCTS." pd where p.products_id = '".$pID."' and p.options_id = '".$option."' and pd.products_id = p.products_id and p.options_values_id = '".$value."'";
		$attribute_price_query = osDBquery($attribute_price_query);
		$attribute_price_data = os_db_fetch_array($attribute_price_query, true);
		$dicount = 0;
		if ($this->cStatus['customers_status_discount_attributes'] == 1 && $this->cStatus['customers_status_discount'] != 0.00) {
			$discount = $this->cStatus['customers_status_discount'];
			if ($attribute_price_data['products_discount_allowed'] < $this->cStatus['customers_status_discount'])
				$discount = $attribute_price_data['products_discount_allowed'];
		}
		$price = $this->Format($attribute_price_data['options_values_price'], false, $attribute_price_data['products_tax_class_id'],true);
		if ($attribute_price_data['weight_prefix'] != '+')
			$attribute_price_data['options_values_weight'] *= -1;
		if ($attribute_price_data['price_prefix'] == '+') {
			$price = $price - $price / 100 * $discount;
		} else {
			$price *= -1;
		}
		return array ('weight' => $attribute_price_data['options_values_weight'], 'price' => $price);
	}

	function ShowNote($vpeStatus, $vpeStatus = 0) {
		if ($vpeStatus == 1)
			return array ('formated' => NOT_ALLOWED_TO_SEE_PRICES, 'plain' => 0);
		return NOT_ALLOWED_TO_SEE_PRICES;
	}

	function CheckSpecial($pID) 
	{
		return get_checkspecial($pID);
		/*$product_query = "select specials_new_products_price from ".TABLE_SPECIALS." where products_id = '".$pID."' and status=1";
		$product_query = osDBquery($product_query);
		$product = os_db_fetch_array($product_query, true);

		return $product['specials_new_products_price'];*/
	}

	function CalculateCurr($price) {
		return $this->currencies[$this->actualCurr]['value'] * $price;
	}

	function calcTax($price, $tax) {
		return $price * $tax / 100;
	}

	function RemoveCurr($price) {

		if (DEFAULT_CURRENCY != $this->actualCurr) {
			return $price * (1 / $this->currencies[$this->actualCurr]['value']);
		} else {
			return $price;
		}

	}

	function RemoveTax($price, $tax) {
		$price = ($price / (($tax +100) / 100));
		return $price;
	}

	function GetTax($price, $tax) {
		$tax = $price - $this->RemoveTax($price, $tax);
		return $tax;
	}
	
	function RemoveDC($price,$dc) {
	
		$price = $price - ($price/100*$dc);
		
		return $price;	
	}
	
	function GetDC($price,$dc) {
		
		$dc = $price/100*$dc;
	
		return $dc;	
	}

	function checkAttributes($pID) {
		if (!$this->showFrom_Attributes) return;
		if ($pID == 0) return;
		
		//$products_attributes_query = "select count(*) as total from ".TABLE_PRODUCTS_OPTIONS." popt, ".TABLE_PRODUCTS_ATTRIBUTES." patrib where patrib.products_id='".$pID."' and patrib.options_id = popt.products_options_id and popt.language_id = '".(int) $_SESSION['languages_id']."'";
		//$products_attributes = osDBquery($products_attributes_query);
		//$products_attributes = os_db_fetch_array($products_attributes, true);
		
		$products_attributes = get_check_attributes ($pID);
		if (isset($products_attributes['total']) && $products_attributes['total'] > 0) return ' '.strtolower(FROM).' ';
	}

	function CalculateCurrEx($price, $curr) {
		return $price * ($this->currencies[$curr]['value'] / $this->currencies[$this->actualCurr]['value']);
	}


	function Format($price, $format, $tax_class = 0, $curr = false, $vpeStatus = 0, $pID = 0) {

		if ($curr)
			$price = $this->CalculateCurr($price);

		if ($tax_class != 0) {
			$products_tax = $this->TAX[$tax_class];
			if ($this->cStatus['customers_status_show_price_tax'] == '0')
				$products_tax = '';
			$price = $this->AddTax($price, $products_tax);
		}

		if ($format) {
			$Pprice = number_format($price, $this->currencies[$this->actualCurr]['decimal_places'], $this->currencies[$this->actualCurr]['decimal_point'], $this->currencies[$this->actualCurr]['thousands_point']);
			$Pprice = $this->checkAttributes($pID).$this->currencies[$this->actualCurr]['symbol_left'].' '.$Pprice.' '.$this->currencies[$this->actualCurr]['symbol_right'];
			if ($vpeStatus == 0) {
				return $Pprice;
			} else {
				return array ('formated' => $Pprice, 'plain' => $price);
			}
		} else {

			return round($price, $this->currencies[$this->actualCurr]['decimal_places']);

		}

	}

	function FormatSpecialDiscount($pID, $discount, $pPrice, $format, $vpeStatus = 0) {
		$sPrice = $pPrice - ($pPrice / 100) * $discount;
		if ($format) {
			$price = '<span class="productOldPrice">'.INSTEAD.$this->Format($pPrice, $format).'</span><br />'.ONLY.$this->checkAttributes($pID).$this->Format($sPrice, $format).'<br />'.YOU_SAVE.$discount.'%';
			if ($vpeStatus == 0) {
				return $price;
			} else {
				return array ('formated' => $price, 'plain' => $sPrice);
			}
		} else {
			return round($sPrice, $this->currencies[$this->actualCurr]['decimal_places']);
		}
	}

	function FormatSpecial($pID, $sPrice, $pPrice, $format, $vpeStatus = 0) {
		if ($format) {
			$price = '<span class="productOldPrice">'.INSTEAD.$this->Format($pPrice, $format).'</span><br />'.ONLY.$this->checkAttributes($pID).$this->Format($sPrice, $format);
			if ($vpeStatus == 0) {
				return $price;
			} else {
				return array ('formated' => $price, 'plain' => $sPrice);
			}
		} else {
			return round($sPrice, $this->currencies[$this->actualCurr]['decimal_places']);
		}
	}

	function FormatSpecialGraduated($pID, $sPrice, $pPrice, $format, $vpeStatus = 0, $pID) {
		if ($pPrice == 0)
			return $this->Format($sPrice, $format, 0, false, $vpeStatus);
		if ($discount = $this->CheckDiscount($pID))
			$sPrice -= $sPrice / 100 * $discount;
		if ($format) {
			if ($sPrice != $pPrice) {
				$price = '<span class="productOldPrice">'.MSRP.$this->Format($pPrice, $format).'</span><br />'.YOUR_PRICE.$this->checkAttributes($pID).$this->Format($sPrice, $format);
			} else {
				$price = FROM.$this->Format($sPrice, $format);
			}
			if ($vpeStatus == 0) {
				return $price;
			} else {
				return array ('formated' => $price, 'plain' => $sPrice);
			}
		} else {
			return round($sPrice, $this->currencies[$this->actualCurr]['decimal_places']);
		}
	}

	function get_decimal_places($code) {
		return $this->currencies[$this->actualCurr]['decimal_places'];
	}

}
?>