00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class Mage_Tax_Helper_Data extends Mage_Core_Helper_Abstract
00031 {
00032 const PRICE_CONVERSION_PLUS = 1;
00033 const PRICE_CONVERSION_MINUS = 2;
00034
00035
00036
00037 protected $_displayTaxColumn;
00038 protected $_taxData;
00039 protected $_priceIncludesTax;
00040 protected $_shippingPriceIncludesTax;
00041 protected $_applyTaxAfterDiscount;
00042 protected $_priceDisplayType;
00043 protected $_shippingPriceDisplayType;
00044
00045 public function getProductPrice($product, $format=null)
00046 {
00047 try {
00048 $value = $product->getPrice();
00049 $value = Mage::app()->getStore()->convertPrice($value, $format);
00050 }
00051 catch (Exception $e){
00052 $value = $e->getMessage();
00053 }
00054 return $value;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 public function priceIncludesTax($store=null)
00064 {
00065 $storeId = Mage::app()->getStore($store)->getId();
00066 if (!isset($this->_priceIncludesTax[$storeId])) {
00067 $this->_priceIncludesTax[$storeId] = (int)Mage::getStoreConfig(
00068 Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
00069 $store
00070 );
00071 }
00072 return $this->_priceIncludesTax[$storeId];
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 public function applyTaxAfterDiscount($store=null)
00082 {
00083 $storeId = Mage::app()->getStore($store)->getId();
00084 if (!isset($this->_applyTaxAfterDiscount[$storeId])) {
00085 $this->_applyTaxAfterDiscount[$storeId] = (int)Mage::getStoreConfig(
00086 Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT,
00087 $store
00088 );
00089 }
00090 return $this->_applyTaxAfterDiscount[$storeId];
00091 }
00092
00093
00094
00095
00096
00097
00098 public function getIncExcText($flag, $store=null)
00099 {
00100 if ($flag) {
00101 $s = $this->__('Incl. Tax');
00102 } else {
00103 $s = $this->__('Excl. Tax');
00104 }
00105 return $s;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 public function getPriceDisplayType($store = null)
00118 {
00119 $storeId = Mage::app()->getStore($store)->getId();
00120 if (!isset($this->_priceDisplayType[$storeId])) {
00121 $this->_priceDisplayType[$storeId] = (int)Mage::getStoreConfig(
00122 Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
00123 $store
00124 );
00125 }
00126 return $this->_priceDisplayType[$storeId];
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 public function needPriceConversion($store = null)
00137 {
00138 $res = false;
00139 if ($this->priceIncludesTax($store)) {
00140 switch ($this->getPriceDisplayType($store)) {
00141 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
00142 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
00143 return self::PRICE_CONVERSION_MINUS;
00144
00145 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
00146 $res = false;
00147 }
00148 } else {
00149 switch ($this->getPriceDisplayType($store)) {
00150 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
00151 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
00152 return self::PRICE_CONVERSION_PLUS;
00153
00154 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
00155 $res = false;
00156 }
00157 }
00158
00159 if ($res === false) {
00160 $res = $this->displayTaxColumn($store);
00161 }
00162 return $res;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 public function displayFullSummary($store = null)
00172 {
00173 return ((int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DISPLAY_FULL_SUMMARY, $store) == 1);
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 public function displayZeroTax($store = null)
00183 {
00184 return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DISPLAY_ZERO_TAX, $store);
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 public function displayCartPriceInclTax($store = null)
00194 {
00195 return $this->displayTaxColumn($store) == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 public function displayCartPriceExclTax($store = null)
00205 {
00206 return $this->displayTaxColumn($store) == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215 public function displayCartBothPrices($store = null)
00216 {
00217 return $this->displayTaxColumn($store) == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function displayTaxColumn($store = null)
00227 {
00228 if (is_null($this->_displayTaxColumn)) {
00229 $this->_displayTaxColumn = (int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DISPLAY_TAX_COLUMN, $store);
00230 }
00231 return $this->_displayTaxColumn;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240 public function getPriceFormat($store = null)
00241 {
00242 return Zend_Json::encode(Mage::app()->getLocale()->getJsPriceFormat());
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 public function getTaxRatesByProductClass()
00255 {
00256 $result = array();
00257 $calc = Mage::getSingleton('tax/calculation');
00258 $rates = $calc->getRatesForAllProductTaxClasses($calc->getRateRequest());
00259
00260 foreach ($rates as $class=>$rate) {
00261 $result["value_{$class}"] = $rate;
00262 }
00263
00264 return Zend_Json::encode($result);
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 public function getPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null)
00281 {
00282 $store = Mage::app()->getStore($store);
00283 if (!$this->needPriceConversion($store)) {
00284 return $store->roundPrice($price);
00285 }
00286 if (is_null($priceIncludesTax)) {
00287 $priceIncludesTax = $this->priceIncludesTax($store);
00288 }
00289
00290 $percent = $product->getTaxPercent();
00291 $includingPercent = null;
00292
00293 $taxClassId = $product->getTaxClassId();
00294 if (is_null($percent)) {
00295 if ($taxClassId) {
00296 $request = Mage::getSingleton('tax/calculation')->getRateRequest($shippingAddress, $billingAddress, $ctc, $store);
00297 $percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId));
00298 }
00299 }
00300 if ($taxClassId && $priceIncludesTax) {
00301 $request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false, $store);
00302 $includingPercent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId));
00303 }
00304
00305 if ($percent === false || is_null($percent)) {
00306 if ($priceIncludesTax && !$includingPercent) {
00307 return $price;
00308 }
00309 }
00310
00311 $product->setTaxPercent($percent);
00312
00313 if (!is_null($includingTax)) {
00314 if ($priceIncludesTax) {
00315 if ($includingTax) {
00316 $price = $this->_calculatePrice($price, $includingPercent, false);
00317 $price = $this->_calculatePrice($price, $percent, true);
00318 } else {
00319 $price = $this->_calculatePrice($price, $includingPercent, false);
00320 }
00321 } else {
00322 if ($includingTax) {
00323 $price = $this->_calculatePrice($price, $percent, true);
00324 }
00325 }
00326 } else {
00327 if ($priceIncludesTax) {
00328 switch ($this->getPriceDisplayType($store)) {
00329 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
00330 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
00331 $price = $this->_calculatePrice($price, $includingPercent, false);
00332 break;
00333
00334 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
00335 $price = $this->_calculatePrice($price, $includingPercent, false);
00336 $price = $this->_calculatePrice($price, $percent, true);
00337 break;
00338 }
00339 } else {
00340 switch ($this->getPriceDisplayType($store)) {
00341 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
00342 $price = $this->_calculatePrice($price, $percent, true);
00343 break;
00344
00345 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
00346 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
00347 break;
00348 }
00349 }
00350 }
00351
00352 return $store->roundPrice($price);
00353 }
00354
00355 public function displayPriceIncludingTax()
00356 {
00357 return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX;
00358 }
00359
00360 public function displayPriceExcludingTax()
00361 {
00362 return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
00363 }
00364
00365 public function displayBothPrices()
00366 {
00367 return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
00368 }
00369
00370 protected function _calculatePrice($price, $percent, $type)
00371 {
00372 $store = Mage::app()->getStore();
00373 if ($type) {
00374 return $price * (1+($percent/100));
00375 } else {
00376 return $price - ($price/(100+$percent)*$percent);
00377 }
00378 }
00379
00380 public function getIncExcTaxLabel($flag)
00381 {
00382 $text = $this->getIncExcText($flag);
00383 return $text ? ' <span class="tax-flag">('.$text.')</span>' : '';
00384 }
00385
00386 public function shippingPriceIncludesTax($store = null)
00387 {
00388 $storeId = Mage::app()->getStore($store)->getId();
00389 if (!isset($this->_shippingPriceIncludesTax[$storeId])) {
00390 $this->_shippingPriceIncludesTax[$storeId] =
00391 (int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX, $store);
00392 }
00393 return $this->_shippingPriceIncludesTax[$storeId];
00394 }
00395
00396 public function getShippingPriceDisplayType($store = null)
00397 {
00398
00399 $storeId = Mage::app()->getStore($store)->getId();
00400 if (!isset($this->_shippingPriceDisplayType[$storeId])) {
00401 $this->_shippingPriceDisplayType[$storeId] =
00402 (int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DISPLAY_SHIPPING, $store);
00403 }
00404 return $this->_shippingPriceDisplayType[$storeId];
00405 }
00406
00407 public function displayShippingPriceIncludingTax()
00408 {
00409 return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX;
00410 }
00411
00412 public function displayShippingPriceExcludingTax()
00413 {
00414 return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
00415 }
00416
00417 public function displayShippingBothPrices()
00418 {
00419 return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
00420 }
00421
00422 public function getShippingTaxClass($store)
00423 {
00424 return (int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $store);
00425 }
00426
00427 public function getShippingPrice($price, $includingTax = null, $shippingAddress = null, $ctc = null, $store = null){
00428 $pseudoProduct = new Varien_Object();
00429 $pseudoProduct->setTaxClassId($this->getShippingTaxClass($store));
00430
00431 $billingAddress = false;
00432 if ($shippingAddress && $shippingAddress->getQuote() && $shippingAddress->getQuote()->getBillingAddress()) {
00433 $billingAddress = $shippingAddress->getQuote()->getBillingAddress();
00434 }
00435
00436 return $this->getPrice($pseudoProduct, $price, $includingTax, $shippingAddress, $billingAddress, $ctc, $store, $this->shippingPriceIncludesTax($store));
00437 }
00438
00439 public function getPriceTaxSql($priceField, $taxClassField)
00440 {
00441 if (!$this->priceIncludesTax() && $this->displayPriceExcludingTax()) {
00442 return '';
00443 }
00444
00445 $request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
00446 $defaultTaxes = Mage::getSingleton('tax/calculation')->getRatesForAllProductTaxClasses($request);
00447
00448 $request = Mage::getSingleton('tax/calculation')->getRateRequest();
00449 $currentTaxes = Mage::getSingleton('tax/calculation')->getRatesForAllProductTaxClasses($request);
00450
00451 $defaultTaxString = $currentTaxString = '';
00452
00453 $rateToVariable = array(
00454 'defaultTaxString'=>'defaultTaxes',
00455 'currentTaxString'=>'currentTaxes',
00456 );
00457 foreach ($rateToVariable as $rateVariable=>$rateArray) {
00458 if ($$rateArray && is_array($$rateArray)) {
00459 $$rateVariable = '';
00460 foreach ($$rateArray as $classId=>$rate) {
00461 if ($rate) {
00462 $$rateVariable .= "WHEN '{$classId}' THEN '".($rate/100)."'";
00463 }
00464 }
00465 if ($$rateVariable) {
00466 $$rateVariable = "CASE {$taxClassField} {$$rateVariable} ELSE 0 END";
00467 }
00468 }
00469 }
00470
00471 $result = '';
00472
00473 if ($this->priceIncludesTax()) {
00474 if ($defaultTaxString) {
00475 $result = "-({$priceField}/(1+({$defaultTaxString}))*{$defaultTaxString})";
00476 }
00477 if (!$this->displayPriceExcludingTax() && $currentTaxString) {
00478 $result .= "+(({$priceField}{$result})*{$currentTaxString})";
00479 }
00480 } else {
00481 if ($this->displayPriceIncludingTax()) {
00482 if ($currentTaxString) {
00483 $result .= "+({$priceField}*{$currentTaxString})";
00484 }
00485 }
00486 }
00487 return $result;
00488 }
00489
00490 public function joinTaxClass($select, $storeId, $priceTable='main_table')
00491 {
00492 $taxClassAttribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'tax_class_id');
00493 $select->joinLeft(
00494 array('tax_class_d'=>$taxClassAttribute->getBackend()->getTable()),
00495 "tax_class_d.entity_id = {$priceTable}.entity_id AND tax_class_d.attribute_id = '{$taxClassAttribute->getId()}'
00496 AND tax_class_d.store_id = 0",
00497 array());
00498 $select->joinLeft(
00499 array('tax_class_c'=>$taxClassAttribute->getBackend()->getTable()),
00500 "tax_class_c.entity_id = {$priceTable}.entity_id AND tax_class_c.attribute_id = '{$taxClassAttribute->getId()}'
00501 AND tax_class_c.store_id = '{$storeId}'",
00502 array());
00503 }
00504
00505 public function discountTax($store=null)
00506 {
00507 return ((int)Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DISCOUNT_TAX, $store) == 1);
00508 }
00509
00510 public function getTaxBasedOn($store = null)
00511 {
00512 return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store);
00513 }
00514
00515 public function applyTaxOnCustomPrice($store = null) {
00516 return ((int) Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store) == 0);
00517 }
00518
00519 public function applyTaxOnOriginalPrice($store = null) {
00520 return ((int) Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store) == 1);
00521 }
00522 }