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_Attribute_Backend_Weee_Tax extends Mage_Catalog_Model_Product_Attribute_Backend_Price
00022 {
00023
00024
00025
00026
00027
00028 protected function _getResource()
00029 {
00030 return Mage::getResourceSingleton('weee/attribute_backend_weee_tax');
00031 }
00032
00033
00034
00035
00036
00037
00038
00039 public function validate($object)
00040 {
00041 $taxes = $object->getData($this->getAttribute()->getName());
00042 if (empty($taxes)) {
00043 return $this;
00044 }
00045 $dup = array();
00046
00047 foreach ($taxes as $tax) {
00048 if (!empty($tax['delete'])) {
00049 continue;
00050 }
00051
00052 $state = isset($tax['state']) ? $tax['state'] : '*';
00053 $key1 = implode('-', array($tax['website_id'], $tax['country'], $state));
00054
00055 if (!empty($dup[$key1])) {
00056 Mage::throwException(
00057 Mage::helper('catalog')->__('Duplicate website, country and state tax found.')
00058 );
00059 }
00060 $dup[$key1] = 1;
00061 }
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public function afterLoad($object)
00072 {
00073 $data = $this->_getResource()->loadProductData($object, $this->getAttribute());
00074
00075 foreach ($data as $i=>$row) {
00076 if ($data[$i]['website_id'] == 0) {
00077 $rate = Mage::app()->getStore()->getBaseCurrency()->getRate(Mage::app()->getBaseCurrencyCode());
00078 if ($rate) {
00079 $data[$i]['website_value'] = $data[$i]['value']/$rate;
00080 } else {
00081 unset($data[$i]);
00082 }
00083 } else {
00084 $data[$i]['website_value'] = $data[$i]['value'];
00085 }
00086
00087 }
00088 $object->setData($this->getAttribute()->getName(), $data);
00089 return $this;
00090 }
00091
00092 public function afterSave($object)
00093 {
00094 $orig = $object->getOrigData($this->getAttribute()->getName());
00095 $current = $object->getData($this->getAttribute()->getName());
00096 if ($orig == $current) {
00097 return $this;
00098 }
00099
00100 $this->_getResource()->deleteProductData($object, $this->getAttribute());
00101 $taxes = $object->getData($this->getAttribute()->getName());
00102
00103 if (!is_array($taxes)) {
00104 return $this;
00105 }
00106
00107 foreach ($taxes as $tax) {
00108 if (empty($tax['price']) || empty($tax['country']) || !empty($tax['delete'])) {
00109 continue;
00110 }
00111
00112 if (isset($tax['state']) && $tax['state']) {
00113 $state = $tax['state'];
00114 } else {
00115 $state = '*';
00116 }
00117
00118 $data = array();
00119 $data['website_id'] = $tax['website_id'];
00120 $data['country'] = $tax['country'];
00121 $data['state'] = $state;
00122 $data['value'] = $tax['price'];
00123 $data['attribute_id'] = $this->getAttribute()->getId();
00124
00125 $this->_getResource()->insertProductData($object, $data);
00126 }
00127
00128 return $this;
00129 }
00130
00131 public function afterDelete($object)
00132 {
00133 $this->_getResource()->deleteProductData($object, $this->getAttribute());
00134 return $this;
00135 }
00136
00137 public function getTable()
00138 {
00139 return $this->_getResource()->getTable('weee/tax');
00140 }
00141 }