Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Tax_Block_Adminhtml_Frontend_Region_Updater
  • Mage_Tax_Block_Checkout_Discount
  • Mage_Tax_Block_Checkout_Grandtotal
  • Mage_Tax_Block_Checkout_Shipping
  • Mage_Tax_Block_Checkout_Subtotal
  • Mage_Tax_Block_Checkout_Tax
  • Mage_Tax_Block_Sales_Order_Tax
  • Mage_Tax_Helper_Data
  • Mage_Tax_Model_Calculation
  • Mage_Tax_Model_Calculation_Rate
  • Mage_Tax_Model_Calculation_Rate_Title
  • Mage_Tax_Model_Calculation_Rule
  • Mage_Tax_Model_Class
  • Mage_Tax_Model_Class_Source_Customer
  • Mage_Tax_Model_Class_Source_Product
  • Mage_Tax_Model_Config
  • Mage_Tax_Model_Config_Price_Include
  • Mage_Tax_Model_Mysql4_Calculation
  • Mage_Tax_Model_Mysql4_Calculation_Collection
  • Mage_Tax_Model_Mysql4_Calculation_Rate
  • Mage_Tax_Model_Mysql4_Calculation_Rate_Collection
  • Mage_Tax_Model_Mysql4_Calculation_Rate_Title
  • Mage_Tax_Model_Mysql4_Calculation_Rate_Title_Collection
  • Mage_Tax_Model_Mysql4_Calculation_Rule
  • Mage_Tax_Model_Mysql4_Calculation_Rule_Collection
  • Mage_Tax_Model_Mysql4_Class
  • Mage_Tax_Model_Mysql4_Class_Collection
  • Mage_Tax_Model_Mysql4_Report_Collection
  • Mage_Tax_Model_Mysql4_Report_Tax
  • Mage_Tax_Model_Mysql4_Report_Updatedat_Collection
  • Mage_Tax_Model_Mysql4_Sales_Order_Tax
  • Mage_Tax_Model_Mysql4_Sales_Order_Tax_Collection
  • Mage_Tax_Model_Mysql4_Setup
  • Mage_Tax_Model_Observer
  • Mage_Tax_Model_Resource_Calculation
  • Mage_Tax_Model_Resource_Calculation_Collection
  • Mage_Tax_Model_Resource_Calculation_Rate
  • Mage_Tax_Model_Resource_Calculation_Rate_Collection
  • Mage_Tax_Model_Resource_Calculation_Rate_Title
  • Mage_Tax_Model_Resource_Calculation_Rate_Title_Collection
  • Mage_Tax_Model_Resource_Calculation_Rule
  • Mage_Tax_Model_Resource_Calculation_Rule_Collection
  • Mage_Tax_Model_Resource_Class
  • Mage_Tax_Model_Resource_Class_Collection
  • Mage_Tax_Model_Resource_Report_Collection
  • Mage_Tax_Model_Resource_Report_Tax
  • Mage_Tax_Model_Resource_Report_Tax_Createdat
  • Mage_Tax_Model_Resource_Report_Tax_Updatedat
  • Mage_Tax_Model_Resource_Report_Updatedat_Collection
  • Mage_Tax_Model_Resource_Sales_Order_Tax
  • Mage_Tax_Model_Resource_Sales_Order_Tax_Collection
  • Mage_Tax_Model_Resource_Sales_Order_Tax_Item
  • Mage_Tax_Model_Resource_Sales_Order_Tax_Item_Collection
  • Mage_Tax_Model_Resource_Setup
  • Mage_Tax_Model_Sales_Order_Tax
  • Mage_Tax_Model_Sales_Order_Tax_Item
  • Mage_Tax_Model_Sales_Pdf_Grandtotal
  • Mage_Tax_Model_Sales_Pdf_Shipping
  • Mage_Tax_Model_Sales_Pdf_Subtotal
  • Mage_Tax_Model_Sales_Pdf_Tax
  • Mage_Tax_Model_Sales_Total_Quote_Discount
  • Mage_Tax_Model_Sales_Total_Quote_Nominal_Subtotal
  • Mage_Tax_Model_Sales_Total_Quote_Nominal_Tax
  • Mage_Tax_Model_Sales_Total_Quote_Shipping
  • Mage_Tax_Model_Sales_Total_Quote_Subtotal
  • Mage_Tax_Model_Sales_Total_Quote_Tax
  • Mage_Tax_Model_System_Config_Source_Algorithm
  • Mage_Tax_Model_System_Config_Source_Apply
  • Mage_Tax_Model_System_Config_Source_PriceType
  • Mage_Tax_Model_System_Config_Source_Tax_Country
  • Mage_Tax_Model_System_Config_Source_Tax_Display_Type
  • Mage_Tax_Model_System_Config_Source_Tax_Region

Exceptions

  • Mage_Tax_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Tax
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * Catalog data helper
 29:  */
 30: class Mage_Tax_Helper_Data extends Mage_Core_Helper_Abstract
 31: {
 32:     const PRICE_CONVERSION_PLUS = 1;
 33:     const PRICE_CONVERSION_MINUS = 2;
 34: 
 35:     /**
 36:      * Tax configuration object
 37:      *
 38:      * @var Mage_Tax_Model_Config
 39:      */
 40:     protected $_config      = null;
 41:     protected $_calculator  = null;
 42:     protected $_displayTaxColumn;
 43:     protected $_taxData;
 44:     protected $_priceIncludesTax;
 45:     protected $_shippingPriceIncludesTax;
 46:     protected $_applyTaxAfterDiscount;
 47:     protected $_priceDisplayType;
 48:     protected $_shippingPriceDisplayType;
 49: 
 50:     /**
 51:      * Postcode cut to this length when creating search templates
 52:      *
 53:      * @var integer
 54:      */
 55:     protected $_postCodeSubStringLength = 10;
 56: 
 57:     public function  __construct()
 58:     {
 59:         $this->_config = Mage::getSingleton('tax/config');
 60:     }
 61: 
 62:     /**
 63:      * Return max postcode length to create search templates
 64:      *
 65:      * @return integer  $len
 66:      */
 67:     public function getPostCodeSubStringLength()
 68:     {
 69:         $len = (int)$this->_postCodeSubStringLength;
 70:         if ($len <= 0) {
 71:             $len = 10;
 72:         }
 73:         return $len;
 74:     }
 75: 
 76:     /**
 77:      * Get tax configuration object
 78:      *
 79:      * @return Mage_Tax_Model_Config
 80:      */
 81:     public function getConfig()
 82:     {
 83:         return $this->_config;
 84:     }
 85: 
 86:     /**
 87:      * Get tax calculation object
 88:      *
 89:      * @return  Mage_Tac_Model_Calculation
 90:      */
 91:     public function getCalculator()
 92:     {
 93:         if ($this->_calculator === null) {
 94:             $this->_calculator = Mage::getSingleton('tax/calculation');
 95:         }
 96:         return $this->_calculator;
 97:     }
 98: 
 99:     /**
100:      * Get product price including store convertion rate
101:      *
102:      * @param   Mage_Catalog_Model_Product $product
103:      * @param   null|string $format
104:      * @return  float|string
105:      */
106:     public function getProductPrice($product, $format=null)
107:     {
108:         try {
109:             $value = $product->getPrice();
110:             $value = Mage::app()->getStore()->convertPrice($value, $format);
111:         } catch (Exception $e){
112:             $value = $e->getMessage();
113:         }
114:         return $value;
115:     }
116: 
117:     /**
118:      * Check if product prices inputed include tax
119:      *
120:      * @param   mix $store
121:      * @return  bool
122:      */
123:     public function priceIncludesTax($store=null)
124:     {
125:         return $this->_config->priceIncludesTax($store) || $this->_config->getNeedUseShippingExcludeTax();
126:     }
127: 
128:     /**
129:      * Check what taxes should be applied after discount
130:      *
131:      * @param   mixed $store
132:      * @return  bool
133:      */
134:     public function applyTaxAfterDiscount($store=null)
135:     {
136:         return $this->_config->applyTaxAfterDiscount($store);
137:     }
138: 
139:     /**
140:      * Output
141:      *
142:      * @param boolean $includes
143:      */
144:     public function getIncExcText($flag, $store=null)
145:     {
146:         if ($flag) {
147:             $s = $this->__('Incl. Tax');
148:         } else {
149:             $s = $this->__('Excl. Tax');
150:         }
151:         return $s;
152:     }
153: 
154:     /**
155:      * Get product price display type
156:      *  1 - Excluding tax
157:      *  2 - Including tax
158:      *  3 - Both
159:      *
160:      * @param   mixed $store
161:      * @return  int
162:      */
163:     public function getPriceDisplayType($store = null)
164:     {
165:         return $this->_config->getPriceDisplayType($store);
166:     }
167: 
168:     /**
169:      * Check if necessary do product price conversion
170:      * If it necessary will be returned conversion type (minus or plus)
171:      *
172:      * @param   mixed $store
173:      * @return  false | int
174:      */
175:     public function needPriceConversion($store = null)
176:     {
177:         $res = false;
178:         if ($this->priceIncludesTax($store)) {
179:             switch ($this->getPriceDisplayType($store)) {
180:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
181:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
182:                     return self::PRICE_CONVERSION_MINUS;
183:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
184:                     $res = true;
185:             }
186:         } else {
187:             switch ($this->getPriceDisplayType($store)) {
188:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
189:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
190:                     return self::PRICE_CONVERSION_PLUS;
191:                 case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
192:                     $res = false;
193:             }
194:         }
195: 
196:         if ($res === false) {
197:             $res = $this->displayTaxColumn($store);
198:         }
199:         return $res;
200:     }
201: 
202:     /**
203:      * Check if need display full tax summary information in totals block
204:      *
205:      * @param   mixed $store
206:      * @return  bool
207:      */
208:     public function displayFullSummary($store = null)
209:     {
210:         return $this->_config->displayCartFullSummary($store);
211:     }
212: 
213:     /**
214:      * Check if need display zero tax in subtotal
215:      *
216:      * @param   mixed $store
217:      * @return  bool
218:      */
219:     public function displayZeroTax($store = null)
220:     {
221:         return $this->_config->displayCartZeroTax($store);
222:     }
223: 
224:     /**
225:      * Check if need display cart prices included tax
226:      *
227:      * @param   mixed $store
228:      * @return  bool
229:      */
230:     public function displayCartPriceInclTax($store = null)
231:     {
232:         return $this->_config->displayCartPricesInclTax($store);
233:     }
234: 
235:     /**
236:      * Check if need display cart prices excluding price
237:      *
238:      * @param   mixed $store
239:      * @return  bool
240:      */
241:     public function displayCartPriceExclTax($store = null)
242:     {
243:         return $this->_config->displayCartPricesExclTax($store);
244:     }
245: 
246:     /**
247:      * Check if need display cart prices excluding and including tax
248:      *
249:      * @param   mixed $store
250:      * @return  bool
251:      */
252:     public function displayCartBothPrices($store = null)
253:     {
254:         return $this->_config->displayCartPricesBoth($store);
255:     }
256: 
257:     /**
258:      * Check if need display order prices included tax
259:      *
260:      * @param   mixed $store
261:      * @return  bool
262:      */
263:     public function displaySalesPriceInclTax($store = null)
264:     {
265:         return $this->_config->displaySalesPricesInclTax($store);
266:     }
267: 
268:     /**
269:      * Check if need display order prices excluding price
270:      *
271:      * @param   mixed $store
272:      * @return  bool
273:      */
274:     public function displaySalesPriceExclTax($store = null)
275:     {
276:         return $this->_config->displaySalesPricesExclTax($store);
277:     }
278: 
279:     /**
280:      * Check if need display order prices excluding and including tax
281:      *
282:      * @param   mixed $store
283:      * @return  bool
284:      */
285:     public function displaySalesBothPrices($store = null)
286:     {
287:         return $this->_config->displaySalesPricesBoth($store);
288:     }
289: 
290: 
291:     /**
292:      * Check if we need display price include and exclude tax for order/invoice subtotal
293:      *
294:      * @param mixed $store
295:      * @return bool
296:      */
297:     public function displaySalesSubtotalBoth($store = null)
298:     {
299:         return $this->_config->displaySalesSubtotalBoth($store);
300:     }
301: 
302:     /**
303:      * Check if we need display price include tax for order/invoice subtotal
304:      *
305:      * @param mixed $store
306:      * @return bool
307:      */
308:     public function displaySalesSubtotalInclTax($store = null)
309:     {
310:         return $this->_config->displaySalesSubtotalInclTax($store);
311:     }
312: 
313:     /**
314:      * Check if we need display price exclude tax for order/invoice subtotal
315:      *
316:      * @param mixed $store
317:      * @return bool
318:      */
319:     public function displaySalesSubtotalExclTax($store = null)
320:     {
321:         return $this->_config->displaySalesSubtotalExclTax($store);
322:     }
323: 
324:     /**
325:      * Check if need display tax column in for shopping cart/order items
326:      *
327:      * @param   mixed $store
328:      * @return  bool
329:      */
330:     public function displayTaxColumn($store = null)
331:     {
332:         return $this->_config->displayCartPricesBoth();
333:     }
334: 
335:     /**
336:      * Get prices javascript format json
337:      *
338:      * @param   mixed $store
339:      * @return  string
340:      */
341:     public function getPriceFormat($store = null)
342:     {
343:         Mage::app()->getLocale()->emulate($store);
344:         $priceFormat = Mage::app()->getLocale()->getJsPriceFormat();
345:         Mage::app()->getLocale()->revert();
346:         if ($store) {
347:             $priceFormat['pattern'] = Mage::app()->getStore($store)->getCurrentCurrency()->getOutputFormat();
348:         }
349:         return Mage::helper('core')->jsonEncode($priceFormat);
350:     }
351: 
352:     /**
353:      * Get all tax rates JSON for all product tax classes
354:      *
355:      * array(
356:      *      value_{$productTaxVlassId} => $rate
357:      * )
358:      * @deprecated after 1.4 - please use getAllRatesByProductClass
359:      * @return string
360:      */
361:     public function getTaxRatesByProductClass()
362:     {
363:         return $this->_getAllRatesByProductClass();
364:     }
365: 
366:     /**
367:      * Get all tax rates JSON for all product tax classes of specific store
368:      *
369:      * array(
370:      *      value_{$productTaxVlassId} => $rate
371:      * )
372:      * @return string
373:      */
374:     public function getAllRatesByProductClass($store=null)
375:     {
376:         return $this->_getAllRatesByProductClass($store);
377:     }
378: 
379: 
380:     /**
381:      * Get all tax rates JSON for all product tax classes of specific store
382:      *
383:      * array(
384:      *      value_{$productTaxVlassId} => $rate
385:      * )
386:      * @return string
387:      */
388:     protected function _getAllRatesByProductClass($store=null)
389:     {
390:         $result = array();
391:         $calc = Mage::getSingleton('tax/calculation');
392:         $rates = $calc->getRatesForAllProductTaxClasses($calc->getRateOriginRequest($store));
393: 
394:         foreach ($rates as $class=>$rate) {
395:             $result["value_{$class}"] = $rate;
396:         }
397: 
398:         return Mage::helper('core')->jsonEncode($result);
399:     }
400: 
401:     /**
402:      * Get product price with all tax settings processing
403:      *
404:      * @param   Mage_Catalog_Model_Product $product
405:      * @param   float $price inputed product price
406:      * @param   bool $includingTax return price include tax flag
407:      * @param   null|Mage_Customer_Model_Address $shippingAddress
408:      * @param   null|Mage_Customer_Model_Address $billingAddress
409:      * @param   null|int $ctc customer tax class
410:      * @param   mixed $store
411:      * @param   bool $priceIncludesTax flag what price parameter contain tax
412:      * @return  float
413:      */
414:     public function getPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null,
415:         $ctc = null, $store = null, $priceIncludesTax = null
416:     ) {
417:         if (!$price) {
418:             return $price;
419:         }
420:         $store = Mage::app()->getStore($store);
421:         if (!$this->needPriceConversion($store)) {
422:             return $store->roundPrice($price);
423:         }
424:         if (is_null($priceIncludesTax)) {
425:             $priceIncludesTax = $this->priceIncludesTax($store);
426:         }
427: 
428:         $percent = $product->getTaxPercent();
429:         $includingPercent = null;
430: 
431:         $taxClassId = $product->getTaxClassId();
432:         if (is_null($percent)) {
433:             if ($taxClassId) {
434:                 $request = Mage::getSingleton('tax/calculation')
435:                     ->getRateRequest($shippingAddress, $billingAddress, $ctc, $store);
436:                 $percent = Mage::getSingleton('tax/calculation')
437:                     ->getRate($request->setProductClassId($taxClassId));
438:             }
439:         }
440:         if ($taxClassId && $priceIncludesTax) {
441:             $request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false, $store);
442:             $includingPercent = Mage::getSingleton('tax/calculation')
443:                 ->getRate($request->setProductClassId($taxClassId));
444:         }
445: 
446:         if ($percent === false || is_null($percent)) {
447:             if ($priceIncludesTax && !$includingPercent) {
448:                 return $price;
449:             }
450:         }
451: 
452:         $product->setTaxPercent($percent);
453: 
454:         if (!is_null($includingTax)) {
455:             if ($priceIncludesTax) {
456:                 if ($includingTax) {
457:                     /**
458:                      * Recalculate price include tax in case of different rates
459:                      */
460:                     if ($includingPercent != $percent) {
461:                         $price = $this->_calculatePrice($price, $includingPercent, false);
462:                         /**
463:                          * Using regular rounding. Ex:
464:                          * price incl tax   = 52.76
465:                          * store tax rate   = 19.6%
466:                          * customer tax rate= 19%
467:                          *
468:                          * price excl tax = 52.76 / 1.196 = 44.11371237 ~ 44.11
469:                          * tax = 44.11371237 * 0.19 = 8.381605351 ~ 8.38
470:                          * price incl tax = 52.49531773 ~ 52.50 != 52.49
471:                          *
472:                          * that why we need round prices excluding tax before applying tax
473:                          * this calculation is used for showing prices on catalog pages
474:                          */
475:                         if ($percent != 0) {
476:                             $price = $this->getCalculator()->round($price);
477:                             $price = $this->_calculatePrice($price, $percent, true);
478:                         }
479:                     }
480:                 } else {
481:                     $price = $this->_calculatePrice($price, $includingPercent, false);
482:                 }
483:             } else {
484:                 if ($includingTax) {
485:                     $price = $this->_calculatePrice($price, $percent, true);
486:                 }
487:             }
488:         } else {
489:             if ($priceIncludesTax) {
490:                 switch ($this->getPriceDisplayType($store)) {
491:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
492:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
493:                         $price = $this->_calculatePrice($price, $includingPercent, false);
494:                         break;
495: 
496:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
497:                         $price = $this->_calculatePrice($price, $includingPercent, false);
498:                         $price = $this->_calculatePrice($price, $percent, true);
499:                         break;
500:                 }
501:             } else {
502:                 switch ($this->getPriceDisplayType($store)) {
503:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
504:                         $price = $this->_calculatePrice($price, $percent, true);
505:                         break;
506: 
507:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
508:                     case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
509:                         break;
510:                 }
511:             }
512:         }
513:         return $store->roundPrice($price);
514:     }
515: 
516:     /**
517:      * Check if we have display in catalog prices including tax
518:      *
519:      * @return bool
520:      */
521:     public function displayPriceIncludingTax()
522:     {
523:         return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX;
524:     }
525: 
526:     /**
527:      * Check if we have display in catalog prices excluding tax
528:      *
529:      * @return bool
530:      */
531:     public function displayPriceExcludingTax()
532:     {
533:         return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
534:     }
535: 
536:     /**
537:      * Check if we have display in catalog prices including and excluding tax
538:      *
539:      * @return bool
540:      */
541:     public function displayBothPrices()
542:     {
543:         return $this->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
544:     }
545: 
546:     /**
547:      * Calculate price imcluding/excluding tax base on tax rate percent
548:      *
549:      * @param   float $price
550:      * @param   float $percent
551:      * @param   bool $type true - for calculate price including tax and false if price excluding tax
552:      * @return  float
553:      */
554:     protected function _calculatePrice($price, $percent, $type)
555:     {
556:         $calculator = Mage::getSingleton('tax/calculation');
557:         if ($type) {
558:             $taxAmount = $calculator->calcTaxAmount($price, $percent, false, false);
559:             return $price + $taxAmount;
560:         } else {
561:             $taxAmount = $calculator->calcTaxAmount($price, $percent, true, false);
562:             return $price - $taxAmount;
563:         }
564:     }
565: 
566:     public function getIncExcTaxLabel($flag)
567:     {
568:         $text = $this->getIncExcText($flag);
569:         return $text ? ' <span class="tax-flag">('.$text.')</span>' : '';
570:     }
571: 
572:     public function shippingPriceIncludesTax($store = null)
573:     {
574:         return $this->_config->shippingPriceIncludesTax($store);
575:     }
576: 
577:     public function getShippingPriceDisplayType($store = null)
578:     {
579:         return $this->_config->getShippingPriceDisplayType($store);
580:     }
581: 
582:     public function displayShippingPriceIncludingTax()
583:     {
584:         return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX;
585:     }
586: 
587:     public function displayShippingPriceExcludingTax()
588:     {
589:         return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
590:     }
591: 
592:     public function displayShippingBothPrices()
593:     {
594:         return $this->getShippingPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
595:     }
596: 
597:     public function getShippingTaxClass($store)
598:     {
599:         return $this->_config->getShippingTaxClass($store);
600:     }
601: 
602:     /**
603:      * Get shipping price
604:      *
605:      * @return float
606:      */
607:     public function getShippingPrice($price, $includingTax = null, $shippingAddress = null, $ctc = null, $store = null)
608:     {
609:         $pseudoProduct = new Varien_Object();
610:         $pseudoProduct->setTaxClassId($this->getShippingTaxClass($store));
611: 
612:         $billingAddress = false;
613:         if ($shippingAddress && $shippingAddress->getQuote() && $shippingAddress->getQuote()->getBillingAddress()) {
614:             $billingAddress = $shippingAddress->getQuote()->getBillingAddress();
615:         }
616: 
617:         $price = $this->getPrice(
618:             $pseudoProduct,
619:             $price,
620:             $includingTax,
621:             $shippingAddress,
622:             $billingAddress,
623:             $ctc,
624:             $store,
625:             $this->shippingPriceIncludesTax($store)
626:         );
627:         return $price;
628:     }
629: 
630:     public function getPriceTaxSql($priceField, $taxClassField)
631:     {
632:         if (!$this->priceIncludesTax() && $this->displayPriceExcludingTax()) {
633:             return '';
634:         }
635: 
636:         $request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
637:         $defaultTaxes = Mage::getSingleton('tax/calculation')->getRatesForAllProductTaxClasses($request);
638: 
639:         $request = Mage::getSingleton('tax/calculation')->getRateRequest();
640:         $currentTaxes = Mage::getSingleton('tax/calculation')->getRatesForAllProductTaxClasses($request);
641: 
642:         $defaultTaxString = $currentTaxString = '';
643: 
644:         $rateToVariable = array(
645:                             'defaultTaxString'=>'defaultTaxes',
646:                             'currentTaxString'=>'currentTaxes',
647:                             );
648:         foreach ($rateToVariable as $rateVariable=>$rateArray) {
649:             if ($$rateArray && is_array($$rateArray)) {
650:                 $$rateVariable = '';
651:                 foreach ($$rateArray as $classId=>$rate) {
652:                     if ($rate) {
653:                         $$rateVariable .= sprintf("WHEN %d THEN %12.4f ", $classId, $rate/100);
654:                     }
655:                 }
656:                 if ($$rateVariable) {
657:                     $$rateVariable = "CASE {$taxClassField} {$$rateVariable} ELSE 0 END";
658:                 }
659:             }
660:         }
661: 
662:         $result = '';
663: 
664:         if ($this->priceIncludesTax()) {
665:             if ($defaultTaxString) {
666:                 $result  = "-({$priceField}/(1+({$defaultTaxString}))*{$defaultTaxString})";
667:             }
668:             if (!$this->displayPriceExcludingTax() && $currentTaxString) {
669:                 $result .= "+(({$priceField}{$result})*{$currentTaxString})";
670:             }
671:         } else {
672:             if ($this->displayPriceIncludingTax()) {
673:                 if ($currentTaxString) {
674:                     $result .= "+({$priceField}*{$currentTaxString})";
675:                 }
676:             }
677:         }
678:         return $result;
679:     }
680: 
681:     /**
682:      * Join tax class
683:      * @param Varien_Db_Select $select
684:      * @param int $storeId
685:      * @param string $priceTable
686:      * @return Mage_Tax_Helper_Data
687:      */
688:     public function joinTaxClass($select, $storeId, $priceTable = 'main_table')
689:     {
690:         $taxClassAttribute = Mage::getModel('eav/entity_attribute')
691:             ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'tax_class_id');
692:         $joinConditionD = implode(' AND ',array(
693:             "tax_class_d.entity_id = {$priceTable}.entity_id",
694:             $select->getAdapter()->quoteInto('tax_class_d.attribute_id = ?', (int)$taxClassAttribute->getId()),
695:             'tax_class_d.store_id = 0'
696:         ));
697:         $joinConditionC = implode(' AND ',array(
698:             "tax_class_c.entity_id = {$priceTable}.entity_id",
699:             $select->getAdapter()->quoteInto('tax_class_c.attribute_id = ?', (int)$taxClassAttribute->getId()),
700:             $select->getAdapter()->quoteInto('tax_class_c.store_id = ?', (int)$storeId)
701:         ));
702:         $select
703:             ->joinLeft(
704:                 array('tax_class_d' => $taxClassAttribute->getBackend()->getTable()),
705:                 $joinConditionD,
706:                 array())
707:             ->joinLeft(
708:                 array('tax_class_c' => $taxClassAttribute->getBackend()->getTable()),
709:                 $joinConditionC,
710:                 array());
711: 
712:         return $this;
713:     }
714: 
715:     /**
716:      * Get configuration setting "Apply Discount On Prices Including Tax" value
717:      *
718:      * @param   null|int $store
719:      * @return  0|1
720:      */
721:     public function discountTax($store=null)
722:     {
723:         return $this->_config->discountTax($store);
724:     }
725: 
726:     /**
727:      * Get value of "Apply Tax On" custom/original price configuration settings
728:      *
729:      * @param $store
730:      * @return 0|1
731:      */
732:     public function getTaxBasedOn($store = null)
733:     {
734:         return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store);
735:     }
736: 
737:     /**
738:      * Check if tax can be applied to custom price
739:      *
740:      * @param $store
741:      * @return bool
742:      */
743:     public function applyTaxOnCustomPrice($store = null)
744:     {
745:         return ((int) Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_ON, $store) == 0);
746:     }
747: 
748:     /**
749:      * Check if tax should be applied just to original price
750:      *
751:      * @param $store
752:      * @return bool
753:      */
754:     public function applyTaxOnOriginalPrice($store = null)
755:     {
756:         return ((int) Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_ON, $store) == 1);
757:     }
758: 
759:     /**
760:      * Get taxes/discounts calculation sequence.
761:      * This sequence depends on "Catalog price include tax", "Apply Tax After Discount"
762:      * and "Apply Discount On Prices Including Tax" configuration options.
763:      *
764:      * @param   null|int|string|Mage_Core_Model_Store $store
765:      * @return  string
766:      */
767:     public function getCalculationSequence($store=null)
768:     {
769:         return $this->_config->getCalculationSequence($store);
770:     }
771: 
772:     /**
773:      * Get tax caclulation algorithm code
774:      *
775:      * @param   null|int $store
776:      * @return  string
777:      */
778:     public function getCalculationAgorithm($store=null)
779:     {
780:         return $this->_config->getAlgorithm($store);
781:     }
782: 
783:     /**
784:      * Get calculated taxes for each tax class
785:      *
786:      * This method returns array with format:
787:      * array(
788:      *  $index => array(
789:      *      'tax_amount'        => $taxAmount,
790:      *      'base_tax_amount'   => $baseTaxAmount,
791:      *      'hidden_tax_amount' => $hiddenTaxAmount
792:      *      'title'             => $title
793:      *      'percent'           => $percent
794:      *  )
795:      * )
796:      *
797:      * @param Mage_Sales_Model_Order $source
798:      * @return array
799:      */
800:     public function getCalculatedTaxes($source)
801:     {
802:         if (Mage::registry('current_invoice')) {
803:             $current = Mage::registry('current_invoice');
804:         } elseif (Mage::registry('current_creditmemo')) {
805:             $current = Mage::registry('current_creditmemo');
806:         } else {
807:             $current = $source;
808:         }
809: 
810:         $taxClassAmount = array();
811:         if ($current && $source) {
812:             foreach($current->getItemsCollection() as $item) {
813:                 $taxCollection = Mage::getResourceModel('tax/sales_order_tax_item')
814:                     ->getTaxItemsByItemId(
815:                         $item->getOrderItemId() ? $item->getOrderItemId() : $item->getItemId()
816:                     );
817: 
818:                 foreach ($taxCollection as $tax) {
819:                     $taxClassId = $tax['tax_id'];
820:                     $percent    = $tax['tax_percent'];
821: 
822:                     $price     = $item->getRowTotal();
823:                     $basePrice = $item->getBaseRowTotal();
824:                     if ($this->applyTaxAfterDiscount($item->getStoreId())) {
825:                         $price     = $price - $item->getDiscountAmount() + $item->getHiddenTaxAmount();
826:                         $basePrice = $basePrice - $item->getBaseDiscountAmount() + $item->getBaseHiddenTaxAmount();
827:                     }
828: 
829:                     if (isset($taxClassAmount[$taxClassId])) {
830:                         $taxClassAmount[$taxClassId]['tax_amount']      += $price * $percent / 100;
831:                         $taxClassAmount[$taxClassId]['base_tax_amount'] += $basePrice * $percent / 100;
832:                     } else {
833:                         $taxClassAmount[$taxClassId]['tax_amount']      = $price * $percent / 100;
834:                         $taxClassAmount[$taxClassId]['base_tax_amount'] = $basePrice * $percent / 100;
835:                         $taxClassAmount[$taxClassId]['title']           = $tax['title'];
836:                         $taxClassAmount[$taxClassId]['percent']         = $tax['percent'];
837:                     }
838:                 }
839:             }
840: 
841:             foreach ($taxClassAmount as $key=>$tax) {
842:                  if ($tax['tax_amount'] == 0 && $tax['base_tax_amount'] == 0) {
843:                      unset($taxClassAmount[$key]);
844:                  }
845:             }
846: 
847:             $taxClassAmount = array_values($taxClassAmount);
848:         }
849: 
850:         return $taxClassAmount;
851:     }
852: 
853:     /**
854:      * Get calculated Shipping & Handling Tax
855:      *
856:      * This method returns array with format:
857:      * array(
858:      *  $index => array(
859:      *      'tax_amount'        => $taxAmount,
860:      *      'base_tax_amount'   => $baseTaxAmount,
861:      *      'hidden_tax_amount' => $hiddenTaxAmount
862:      *      'title'             => $title
863:      *      'percent'           => $percent
864:      *  )
865:      * )
866:      *
867:      * @param Mage_Sales_Model_Order $source
868:      * @return array
869:      */
870:     public function getShippingTax($source)
871:     {
872:         if (Mage::registry('current_invoice')) {
873:             $current = Mage::registry('current_invoice');
874:         } elseif (Mage::registry('current_creditmemo')) {
875:             $current = Mage::registry('current_creditmemo');
876:         } else {
877:             $current = $source;
878:         }
879: 
880:         $taxClassAmount = array();
881:         if ($current && $source) {
882:             if ($current->getShippingTaxAmount() != 0 && $current->getBaseShippingTaxAmount() != 0) {
883:                 $taxClassAmount[0]['tax_amount']        = $current->getShippingTaxAmount();
884:                 $taxClassAmount[0]['base_tax_amount']   = $current->getBaseShippingTaxAmount();
885:                 if ($current->getShippingHiddenTaxAmount() > 0) {
886:                     $taxClassAmount[0]['hidden_tax_amount'] = $current->getShippingHiddenTaxAmount();
887:                 }
888:                 $taxClassAmount[0]['title']             = $this->__('Shipping & Handling Tax');
889:                 $taxClassAmount[0]['percent']           = NULL;
890:             }
891:         }
892: 
893:         return $taxClassAmount;
894:     }
895: }
896: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0