00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 class Mage_Weee_Model_Tax extends Mage_Core_Model_Abstract
00022 {
00023 protected $_allAttributes = null;
00024 protected $_productDiscounts = array();
00025
00026 protected function _construct()
00027 {
00028 $this->_init('weee/tax', 'weee/tax');
00029 }
00030
00031 public function getWeeeAmount($product, $shipping = null, $billing = null, $website = null, $calculateTax = false, $ignoreDiscount = false)
00032 {
00033 $amount = 0;
00034 $attributes = $this->getProductWeeeAttributes($product, $shipping, $billing, $website, $calculateTax, $ignoreDiscount);
00035 foreach ($attributes as $attribute) {
00036 $amount += $attribute->getAmount();
00037 }
00038 return $amount;
00039 }
00040
00041 public function getWeeeAttributeCodes($forceEnabled = false)
00042 {
00043 return $this->getWeeeTaxAttributeCodes($forceEnabled);
00044 }
00045
00046 public function getWeeeTaxAttributeCodes($forceEnabled = false)
00047 {
00048 if (!$forceEnabled && !Mage::helper('weee')->isEnabled()) {
00049 return array();
00050 }
00051
00052 if (is_null($this->_allAttributes)) {
00053 $this->_allAttributes = Mage::getModel('eav/entity_attribute')->getAttributeCodesByFrontendType('weee');
00054 }
00055 return $this->_allAttributes;
00056 }
00057
00058 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null, $ignoreDiscount = false)
00059 {
00060 $result = array();
00061 $allWeee = $this->getWeeeTaxAttributeCodes();
00062 if (!$allWeee) {
00063 return $result;
00064 }
00065
00066 $websiteId = Mage::app()->getWebsite($website)->getId();
00067 $store = Mage::app()->getWebsite($website)->getDefaultGroup()->getDefaultStore();
00068
00069 if ($shipping) {
00070 $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
00071 } else {
00072 $customerTaxClass = null;
00073 }
00074
00075 $rateRequest = Mage::getModel('tax/calculation')->getRateRequest($shipping, $billing, $customerTaxClass, $store);
00076 $defaultRateRequest = Mage::getModel('tax/calculation')->getRateRequest(false, false, false, $store);
00077
00078 $discountPercent = 0;
00079 if (!$ignoreDiscount && Mage::helper('weee')->isDiscounted($store)) {
00080 $discountPercent = $this->_getDiscountPercentForProduct($product);
00081 }
00082
00083 $productAttributes = $product->getTypeInstance(true)->getSetAttributes($product);
00084 foreach ($productAttributes as $code=>$attribute) {
00085 if (in_array($code, $allWeee)) {
00086 $attributeId = $attribute->getId();
00087
00088 $attributeSelect = $this->getResource()->getReadConnection()->select();
00089 $attributeSelect->from($this->getResource()->getTable('weee/tax'), 'value');
00090
00091 $on = array();
00092 $on[] = "attribute_id = '{$attributeId}'";
00093 $on[] = "(website_id in ('{$websiteId}', 0))";
00094
00095 $country = $rateRequest->getCountryId();
00096 $on[] = "(country = '{$country}')";
00097
00098 $region = $rateRequest->getRegionId();
00099 $on[] = "(state in ('{$region}', '*'))";
00100
00101 foreach ($on as $one) {
00102 $attributeSelect->where($one);
00103 }
00104 $attributeSelect->where('entity_id = ?', $product->getId());
00105 $attributeSelect->limit(1);
00106
00107 $order = array('state DESC', 'website_id DESC');
00108
00109 $attributeSelect->order($order);
00110 $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
00111 if ($value) {
00112 if ($discountPercent) {
00113 $value = Mage::app()->getStore()->roundPrice($value-($value*$discountPercent/100));
00114 }
00115
00116 $taxAmount = $amount = 0;
00117 $amount = $value;
00118
00119 if ($calculateTax && Mage::helper('weee')->isTaxable($store)) {
00120 $defaultPercent = Mage::getModel('tax/calculation')->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
00121 $currentPercent = $product->getTaxPercent();
00122
00123 $taxAmount = Mage::app()->getStore()->roundPrice($value/(100+$defaultPercent)*$currentPercent);
00124 $amount = $value - $taxAmount;
00125 }
00126
00127 $one = new Varien_Object();
00128 $one->setName(Mage::helper('catalog')->__($attribute->getFrontend()->getLabel()))
00129 ->setAmount($amount)
00130 ->setTaxAmount($taxAmount)
00131 ->setCode($attribute->getAttributeCode());
00132
00133 $result[] = $one;
00134 }
00135 }
00136 }
00137 return $result;
00138 }
00139
00140 protected function _getDiscountPercentForProduct($product)
00141 {
00142 $website = Mage::app()->getStore()->getWebsiteId();
00143 $group = Mage::getSingleton('customer/session')->getCustomerGroupId();
00144 $key = implode('-', array($website, $group, $product->getId()));
00145 if (!isset($this->_productDiscounts[$key])) {
00146 $this->_productDiscounts[$key] = (int) $this->getResource()->getProductDiscountPercent($product->getId(), $website, $group);
00147 }
00148 if ($value = $this->_productDiscounts[$key]) {
00149 return 100-min(100, max(0, $value));
00150 } else {
00151 return 0;
00152 }
00153 }
00154
00155 public function updateDiscountPercents()
00156 {
00157 $this->getResource()->updateDiscountPercents();
00158 return $this;
00159 }
00160 }