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_Attribute_Backend_Weee_Tax extends Mage_Catalog_Model_Product_Attribute_Backend_Price
28: {
29: public static function getBackendModelName()
30: {
31: return 'weee/attribute_backend_weee_tax';
32: }
33: /**
34: * Retrieve resource model
35: *
36: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Backend_Weee
37: */
38: protected function _getResource()
39: {
40: return Mage::getResourceSingleton(self::getBackendModelName());
41: }
42:
43: /**
44: * Validate data
45: *
46: * @param Mage_Catalog_Model_Product $object
47: * @return this
48: */
49: public function validate($object)
50: {
51: $taxes = $object->getData($this->getAttribute()->getName());
52: if (empty($taxes)) {
53: return $this;
54: }
55: $dup = array();
56:
57: foreach ($taxes as $tax) {
58: if (!empty($tax['delete'])) {
59: continue;
60: }
61:
62: $state = isset($tax['state']) ? $tax['state'] : '*';
63: $key1 = implode('-', array($tax['website_id'], $tax['country'], $state));
64:
65: if (!empty($dup[$key1])) {
66: Mage::throwException(
67: Mage::helper('catalog')->__('Duplicate website, country and state tax found.')
68: );
69: }
70: $dup[$key1] = 1;
71: }
72: return $this;
73: }
74:
75: /**
76: * Assign WEEE taxes to product data
77: *
78: * @param Mage_Catalog_Model_Product $object
79: * @return Mage_Catalog_Model_Product_Attribute_Backend_Weee
80: */
81: public function afterLoad($object)
82: {
83: $data = $this->_getResource()->loadProductData($object, $this->getAttribute());
84:
85: foreach ($data as $i=>$row) {
86: if ($data[$i]['website_id'] == 0) {
87: $rate = Mage::app()->getStore()->getBaseCurrency()->getRate(Mage::app()->getBaseCurrencyCode());
88: if ($rate) {
89: $data[$i]['website_value'] = $data[$i]['value']/$rate;
90: } else {
91: unset($data[$i]);
92: }
93: } else {
94: $data[$i]['website_value'] = $data[$i]['value'];
95: }
96:
97: }
98: $object->setData($this->getAttribute()->getName(), $data);
99: return $this;
100: }
101:
102: public function afterSave($object)
103: {
104: $orig = $object->getOrigData($this->getAttribute()->getName());
105: $current = $object->getData($this->getAttribute()->getName());
106: if ($orig == $current) {
107: return $this;
108: }
109:
110: $this->_getResource()->deleteProductData($object, $this->getAttribute());
111: $taxes = $object->getData($this->getAttribute()->getName());
112:
113: if (!is_array($taxes)) {
114: return $this;
115: }
116:
117: foreach ($taxes as $tax) {
118: if (empty($tax['price']) || empty($tax['country']) || !empty($tax['delete'])) {
119: continue;
120: }
121:
122: if (isset($tax['state']) && $tax['state']) {
123: $state = $tax['state'];
124: } else {
125: $state = '*';
126: }
127:
128: $data = array();
129: $data['website_id'] = $tax['website_id'];
130: $data['country'] = $tax['country'];
131: $data['state'] = $state;
132: $data['value'] = $tax['price'];
133: $data['attribute_id'] = $this->getAttribute()->getId();
134:
135: $this->_getResource()->insertProductData($object, $data);
136: }
137:
138: return $this;
139: }
140:
141: public function afterDelete($object)
142: {
143: $this->_getResource()->deleteProductData($object, $this->getAttribute());
144: return $this;
145: }
146:
147: public function getTable()
148: {
149: return $this->_getResource()->getTable('weee/tax');
150: }
151: }
152:
153: