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_Weee_Block_Element_Weee_Tax
  • Mage_Weee_Helper_Data
  • Mage_Weee_Model_Attribute_Backend_Weee_Tax
  • Mage_Weee_Model_Config_Source_Display
  • Mage_Weee_Model_Mysql4_Attribute_Backend_Weee_Tax
  • Mage_Weee_Model_Mysql4_Setup
  • Mage_Weee_Model_Mysql4_Tax
  • Mage_Weee_Model_Observer
  • Mage_Weee_Model_Resource_Attribute_Backend_Weee_Tax
  • Mage_Weee_Model_Resource_Setup
  • Mage_Weee_Model_Resource_Tax
  • Mage_Weee_Model_Tax
  • Mage_Weee_Model_Total_Creditmemo_Weee
  • Mage_Weee_Model_Total_Invoice_Weee
  • Mage_Weee_Model_Total_Quote_Nominal_Weee
  • Mage_Weee_Model_Total_Quote_Weee
  • 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_Weee
 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: class Mage_Weee_Model_Tax extends Mage_Core_Model_Abstract
 28: {
 29:     /**
 30:      * Including FPT only
 31:      */
 32:     const DISPLAY_INCL              = 0;
 33:     /**
 34:      * Including FPT and FPT description
 35:      */
 36:     const DISPLAY_INCL_DESCR        = 1;
 37:     /**
 38:      * Excluding FPT, FPT description, final price
 39:      */
 40:     const DISPLAY_EXCL_DESCR_INCL   = 2;
 41:     /**
 42:      * Excluding FPT
 43:      */
 44:     const DISPLAY_EXCL              = 3;
 45: 
 46:     protected $_allAttributes = null;
 47:     protected $_productDiscounts = array();
 48: 
 49:     /**
 50:      * Initialize resource
 51:      */
 52:     protected function _construct()
 53:     {
 54:         $this->_init('weee/tax', 'weee/tax');
 55:     }
 56: 
 57: 
 58:     public function getWeeeAmount(
 59:         $product,
 60:         $shipping = null,
 61:         $billing = null,
 62:         $website = null,
 63:         $calculateTax = false,
 64:         $ignoreDiscount = false)
 65:     {
 66:         $amount = 0;
 67:         $attributes = $this->getProductWeeeAttributes(
 68:             $product,
 69:             $shipping,
 70:             $billing,
 71:             $website,
 72:             $calculateTax,
 73:             $ignoreDiscount
 74:         );
 75:         foreach ($attributes as $attribute) {
 76:             $amount += $attribute->getAmount();
 77:         }
 78:         return $amount;
 79:     }
 80: 
 81:     public function getWeeeAttributeCodes($forceEnabled = false)
 82:     {
 83:         return $this->getWeeeTaxAttributeCodes($forceEnabled);
 84:     }
 85: 
 86:     /**
 87:      * Retrieve Wee tax attribute codes
 88:      *
 89:      * @param bool $forceEnabled
 90:      * @return array
 91:      */
 92:     public function getWeeeTaxAttributeCodes($forceEnabled = false)
 93:     {
 94:         if (!$forceEnabled && !Mage::helper('weee')->isEnabled()) {
 95:             return array();
 96:         }
 97: 
 98:         if (is_null($this->_allAttributes)) {
 99:             $this->_allAttributes = Mage::getModel('eav/entity_attribute')->getAttributeCodesByFrontendType('weee');
100:         }
101:         return $this->_allAttributes;
102:     }
103: 
104:     public function getProductWeeeAttributes(
105:         $product,
106:         $shipping = null,
107:         $billing = null,
108:         $website = null,
109:         $calculateTax = null,
110:         $ignoreDiscount = false)
111:     {
112:         $result = array();
113:         $allWeee = $this->getWeeeTaxAttributeCodes();
114:         if (!$allWeee) {
115:             return $result;
116:         }
117: 
118:         $websiteId = Mage::app()->getWebsite($website)->getId();
119:         $store = Mage::app()->getWebsite($website)->getDefaultGroup()->getDefaultStore();
120:         $customer = null;
121: 
122:         if ($shipping) {
123:             $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
124:             $customer = $shipping->getQuote()->getCustomer();
125:         } else {
126:             $customerTaxClass = null;
127:         }
128: 
129:         $calculator = Mage::getModel('tax/calculation');
130:         if ($customer) {
131:             $calculator->setCustomer($customer);
132:         }
133:         $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
134:         $defaultRateRequest = $calculator->getRateRequest(false, false, false, $store);
135:         $discountPercent = 0;
136: 
137:         if (!$ignoreDiscount && Mage::helper('weee')->isDiscounted($store)) {
138:             $discountPercent = $this->_getDiscountPercentForProduct($product);
139:         }
140: 
141:         $productAttributes = $product->getTypeInstance(true)->getSetAttributes($product);
142:         foreach ($productAttributes as $code => $attribute) {
143:             if (in_array($code, $allWeee)) {
144:                 $attributeSelect = $this->getResource()->getReadConnection()->select();
145:                 $attributeSelect
146:                     ->from($this->getResource()->getTable('weee/tax'), 'value')
147:                     ->where('attribute_id = ?', (int)$attribute->getId())
148:                     ->where('website_id IN(?)', array($websiteId, 0))
149:                     ->where('country = ?', $rateRequest->getCountryId())
150:                     ->where('state IN(?)', array($rateRequest->getRegionId(), '*'))
151:                     ->where('entity_id = ?', (int)$product->getId())
152:                     ->limit(1);
153: 
154:                 $order = array('state ' . Varien_Db_Select::SQL_DESC, 'website_id ' . Varien_Db_Select::SQL_DESC);
155:                 $attributeSelect->order($order);
156:                 $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
157: 
158:                 if ($value) {
159:                     if ($discountPercent) {
160:                         $value = Mage::app()->getStore()->roundPrice($value - ($value * $discountPercent / 100));
161:                     }
162: 
163:                     $taxAmount = 0;
164:                     $amount    = $value;
165:                     if ($calculateTax && Mage::helper('weee')->isTaxable($store)) {
166:                         $defaultPercent = Mage::getModel('tax/calculation')
167:                             ->getRate($defaultRateRequest
168:                             ->setProductClassId($product->getTaxClassId()));
169:                         $currentPercent = $product->getTaxPercent();
170:                         if (Mage::helper('tax')->priceIncludesTax($store)) {
171:                             $taxAmount = Mage::app()->getStore()->roundPrice($value/(100+$defaultPercent)*$currentPercent);
172:                         } else {
173:                             $taxAmount = Mage::app()->getStore()->roundPrice($value*$defaultPercent/100);
174:                         }
175:                     }
176: 
177:                     $one = new Varien_Object();
178:                     $one->setName(Mage::helper('catalog')->__($attribute->getFrontend()->getLabel()))
179:                         ->setAmount($amount)
180:                         ->setTaxAmount($taxAmount)
181:                         ->setCode($attribute->getAttributeCode());
182: 
183:                     $result[] = $one;
184:                 }
185:             }
186:         }
187:         return $result;
188:     }
189: 
190:     protected function _getDiscountPercentForProduct($product)
191:     {
192:         $website = Mage::app()->getStore()->getWebsiteId();
193:         $group = Mage::getSingleton('customer/session')->getCustomerGroupId();
194:         $key = implode('-', array($website, $group, $product->getId()));
195:         if (!isset($this->_productDiscounts[$key])) {
196:             $this->_productDiscounts[$key] = (int) $this->getResource()
197:                 ->getProductDiscountPercent($product->getId(), $website, $group);
198:         }
199:         if ($value = $this->_productDiscounts[$key]) {
200:             return 100-min(100, max(0, $value));
201:         } else {
202:             return 0;
203:         }
204:     }
205: 
206:     /**
207:      * Update discounts for FPT amounts of all products
208:      *
209:      * @return Mage_Weee_Model_Tax
210:      */
211:     public function updateDiscountPercents()
212:     {
213:         $this->getResource()->updateDiscountPercents();
214:         return $this;
215:     }
216: 
217:     /**
218:      * Update discounts for FPT amounts base on products condiotion
219:      *
220:      * @param  mixed $products
221:      * @return Mage_Weee_Model_Tax
222:      */
223:     public function updateProductsDiscountPercent($products)
224:     {
225:         $this->getResource()->updateProductsDiscountPercent($products);
226:         return $this;
227:     }
228: }
229: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0