1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: class Mage_Weee_Model_Tax extends Mage_Core_Model_Abstract
28: {
29: 30: 31:
32: const DISPLAY_INCL = 0;
33: 34: 35:
36: const DISPLAY_INCL_DESCR = 1;
37: 38: 39:
40: const DISPLAY_EXCL_DESCR_INCL = 2;
41: 42: 43:
44: const DISPLAY_EXCL = 3;
45:
46: protected $_allAttributes = null;
47: protected $_productDiscounts = array();
48:
49: 50: 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: 88: 89: 90: 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: 208: 209: 210:
211: public function updateDiscountPercents()
212: {
213: $this->getResource()->updateDiscountPercents();
214: return $this;
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function updateProductsDiscountPercent($products)
224: {
225: $this->getResource()->updateProductsDiscountPercent($products);
226: return $this;
227: }
228: }
229: