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_Observer extends Mage_Core_Model_Abstract
28: {
29: 30: 31: 32: 33: 34:
35: public function setWeeeRendererInForm(Varien_Event_Observer $observer)
36: {
37:
38:
39: $form = $observer->getEvent()->getForm();
40:
41:
42: $attributes = Mage::getSingleton('weee/tax')->getWeeeAttributeCodes(true);
43: foreach ($attributes as $code) {
44: if ($weeeTax = $form->getElement($code)) {
45: $weeeTax->setRenderer(
46: Mage::app()->getLayout()->createBlock('weee/renderer_weee_tax')
47: );
48: }
49: }
50:
51: return $this;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function updateExcludedFieldList(Varien_Event_Observer $observer)
61: {
62:
63:
64: $block = $observer->getEvent()->getObject();
65: $list = $block->getFormExcludedFieldList();
66: $attributes = Mage::getSingleton('weee/tax')->getWeeeAttributeCodes(true);
67: $list = array_merge($list, array_values($attributes));
68:
69: $block->setFormExcludedFieldList($list);
70:
71: return $this;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function prepareCatalogIndexSelect(Varien_Event_Observer $observer)
81: {
82: $storeId = (int)$observer->getEvent()->getStoreId();
83: if (!Mage::helper('weee')->isEnabled($storeId)) {
84: return $this;
85: }
86:
87: switch(Mage::helper('weee')->getListPriceDisplayType()) {
88: case Mage_Weee_Model_Tax::DISPLAY_EXCL_DESCR_INCL:
89: case Mage_Weee_Model_Tax::DISPLAY_EXCL:
90: return $this;
91: }
92:
93:
94: $select = $observer->getEvent()->getSelect();
95: $table = $observer->getEvent()->getTable();
96:
97: $websiteId = (int)Mage::app()->getStore($storeId)->getWebsiteId();
98: $customerGroupId = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();
99:
100: $response = $observer->getEvent()->getResponseObject();
101: $additionalCalculations = $response->getAdditionalCalculations();
102:
103: $attributes = Mage::getSingleton('weee/tax')->getWeeeAttributeCodes();
104:
105: if ($attributes && Mage::helper('weee')->isDiscounted()) {
106: $joinConditions = array(
107: "discount_percent.entity_id = {$table}.entity_id",
108: $select->getAdapter()->quoteInto('discount_percent.website_id = ?', $websiteId),
109: $select->getAdapter()->quoteInto('discount_percent.customer_group_id = ?', $customerGroupId)
110: );
111: $tableWeeDiscount = Mage::getSingleton('weee/tax')->getResource()->getTable('weee/discount');
112: $select->joinLeft(
113: array('discount_percent' => $tableWeeDiscount),
114: implode(' AND ', $joinConditions),
115: array()
116: );
117: }
118: $checkDiscountField = $select->getAdapter()->getCheckSql(
119: 'discount_percent.value IS NULL', 0, 'discount_percent.value');
120: foreach ($attributes as $attribute) {
121: $fieldAlias = sprintf('weee_%s_table.value', $attribute);
122: $checkAdditionalCalculation = $select->getAdapter()->getCheckSql("{$fieldAlias} IS NULL", 0, $fieldAlias);
123: if (Mage::helper('weee')->isDiscounted()) {
124: $additionalCalculations[] = sprintf('+(%s*(1-(%s/100)))', $checkAdditionalCalculation, $checkDiscountField);
125: } else {
126: $additionalCalculations[] = "+($checkAdditionalCalculation)";
127: }
128: }
129: $response->setAdditionalCalculations($additionalCalculations);
130:
131:
132: $rateRequest = Mage::getSingleton('tax/calculation')->getRateRequest();
133:
134: $attributes = Mage::getSingleton('weee/tax')->getWeeeTaxAttributeCodes();
135: foreach ($attributes as $attribute) {
136: $attributeId = (int)Mage::getSingleton('eav/entity_attribute')
137: ->getIdByCode(Mage_Catalog_Model_Product::ENTITY, $attribute);
138: $tableAlias = sprintf('weee_%s_table', $attribute);
139: $quotedTableAlias = $select->getAdapter()->quoteTableAs($tableAlias, null);
140: $attributeSelect = $this->_getSelect();
141: $attributeSelect
142: ->from(array($tableAlias => Mage::getSingleton('weee/tax')->getResource()->getTable('weee/tax')))
143: ->where("{$quotedTableAlias}.attribute_id = ?", $attributeId)
144: ->where("{$quotedTableAlias}.website_id IN(?)", array($websiteId, 0))
145: ->where("{$quotedTableAlias}.country = ?", $rateRequest->getCountryId())
146: ->where("{$quotedTableAlias}.state IN(?)", array($rateRequest->getRegionId(), '*'));
147:
148: $order = array(
149: sprintf('%s.state %s', $tableAlias, Varien_Db_Select::SQL_DESC),
150: sprintf('%s.website_id %s', $tableAlias, Varien_Db_Select::SQL_DESC)
151: );
152: $attributeSelect->order($order);
153:
154: $joinCondition = sprintf('%s.entity_id = %s.entity_id', $table, $quotedTableAlias);
155: $select->joinLeft(
156: array($tableAlias => $attributeSelect),
157: $joinCondition,
158: array()
159: );
160: }
161: return $this;
162: }
163:
164: 165: 166: 167: 168:
169: protected function _getSelect()
170: {
171: return Mage::getSingleton('weee/tax')->getResource()->getReadConnection()->select();
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function addWeeeTaxAttributeType(Varien_Event_Observer $observer)
181: {
182:
183:
184: $response = $observer->getEvent()->getResponse();
185: $types = $response->getTypes();
186: $types[] = array(
187: 'value' => 'weee',
188: 'label' => Mage::helper('weee')->__('Fixed Product Tax'),
189: 'hide_fields' => array(
190: 'is_unique',
191: 'is_required',
192: 'frontend_class',
193: 'is_configurable',
194:
195: '_scope',
196: '_default_value',
197: '_front_fieldset',
198: ),
199: 'disabled_types' => array(
200: Mage_Catalog_Model_Product_Type::TYPE_GROUPED,
201: )
202: );
203:
204: $response->setTypes($types);
205:
206: return $this;
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function assignBackendModelToAttribute(Varien_Event_Observer $observer)
216: {
217: $backendModel = Mage_Weee_Model_Attribute_Backend_Weee_Tax::getBackendModelName();
218:
219: $object = $observer->getEvent()->getAttribute();
220: if ($object->getFrontendInput() == 'weee') {
221: $object->setBackendModel($backendModel);
222: if (!$object->getApplyTo()) {
223: $applyTo = array();
224: foreach (Mage_Catalog_Model_Product_Type::getOptions() as $option) {
225: if ($option['value'] == Mage_Catalog_Model_Product_Type::TYPE_GROUPED) {
226: continue;
227: }
228: $applyTo[] = $option['value'];
229: }
230: $object->setApplyTo($applyTo);
231: }
232: }
233:
234: return $this;
235: }
236:
237: 238: 239: 240: 241:
242: public function updateElementTypes(Varien_Event_Observer $observer)
243: {
244: $response = $observer->getEvent()->getResponse();
245: $types = $response->getTypes();
246: $types['weee'] = Mage::getConfig()->getBlockClassName('weee/element_weee_tax');
247: $response->setTypes($types);
248: return $this;
249: }
250:
251: 252: 253: 254: 255: 256:
257: public function updateDiscountPercents(Varien_Event_Observer $observer)
258: {
259: if (!Mage::helper('weee')->isEnabled()) {
260: return $this;
261: }
262:
263: $productCondition = $observer->getEvent()->getProductCondition();
264: if ($productCondition) {
265: $eventProduct = $productCondition;
266: } else {
267: $eventProduct = $observer->getEvent()->getProduct();
268: }
269: Mage::getModel('weee/tax')->updateProductsDiscountPercent($eventProduct);
270:
271: return $this;
272: }
273:
274: 275: 276: 277: 278: 279:
280: public function updateCofigurableProductOptions(Varien_Event_Observer $observer)
281: {
282:
283: $weeeHelper = Mage::helper('weee');
284: if (!$weeeHelper->isEnabled()) {
285: return $this;
286: }
287:
288: $response = $observer->getEvent()->getResponseObject();
289: $options = $response->getAdditionalOptions();
290:
291: $_product = Mage::registry('current_product');
292: if (!$_product) {
293: return $this;
294: }
295:
296: $amount = $weeeHelper->getAmount($_product);
297: $origAmount = $weeeHelper->getOriginalAmount($_product);
298: $attributes = $weeeHelper->getProductWeeeAttributes($_product, null, null, null, $weeeHelper->isTaxable());
299: $amountInclTaxes = $weeeHelper->getAmountInclTaxes($attributes);
300: $taxes = $amountInclTaxes - $amount;
301:
302: $options['oldPlusDisposition'] = $origAmount;
303: $options['plusDisposition'] = $amount;
304: $options['plusDispositionTax'] = ($taxes < 0) ? 0 : $taxes;
305:
306:
307: if (!$weeeHelper->typeOfDisplay($_product, array(0, 1, 4))) {
308: $options['exclDisposition'] = true;
309: }
310:
311: $response->setAdditionalOptions($options);
312:
313: return $this;
314: }
315:
316: 317: 318: 319: 320: 321:
322: public function updateBundleProductOptions(Varien_Event_Observer $observer)
323: {
324:
325: $weeeHelper = Mage::helper('weee');
326: if (!$weeeHelper->isEnabled()) {
327: return $this;
328: }
329:
330: $response = $observer->getEvent()->getResponseObject();
331: $selection = $observer->getEvent()->getSelection();
332: $options = $response->getAdditionalOptions();
333:
334: $_product = Mage::registry('current_product');
335:
336: $typeDynamic = Mage_Bundle_Block_Adminhtml_Catalog_Product_Edit_Tab_Attributes_Extend::DYNAMIC;
337: if (!$_product || $_product->getPriceType() != $typeDynamic) {
338: return $this;
339: }
340:
341: $amount = $weeeHelper->getAmount($selection);
342: $attributes = $weeeHelper->getProductWeeeAttributes($_product, null, null, null, $weeeHelper->isTaxable());
343: $amountInclTaxes = $weeeHelper->getAmountInclTaxes($attributes);
344: $taxes = $amountInclTaxes - $amount;
345: $options['plusDisposition'] = $amount;
346: $options['plusDispositionTax'] = ($taxes < 0) ? 0 : $taxes;
347:
348: if (!$weeeHelper->typeOfDisplay($_product, array(0, 1, 4))) {
349: $options['exclDisposition'] = true;
350: }
351:
352: $response->setAdditionalOptions($options);
353:
354: return $this;
355: }
356: }
357:
358: