PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Weee/Model/Attribute/Backend/Weee/Tax.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 147 lines | 88 code | 18 blank | 41 comment | 17 complexity | 8afa1d6a03df04e398a814f3ac3c349c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Weee_Model_Attribute_Backend_Weee_Tax extends Mage_Catalog_Model_Product_Attribute_Backend_Price
  27. {
  28. /**
  29. * Retrieve resource model
  30. *
  31. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Backend_Weee
  32. */
  33. protected function _getResource()
  34. {
  35. return Mage::getResourceSingleton('weee/attribute_backend_weee_tax');
  36. }
  37. /**
  38. * Validate data
  39. *
  40. * @param Mage_Catalog_Model_Product $object
  41. * @return this
  42. */
  43. public function validate($object)
  44. {
  45. $taxes = $object->getData($this->getAttribute()->getName());
  46. if (empty($taxes)) {
  47. return $this;
  48. }
  49. $dup = array();
  50. foreach ($taxes as $tax) {
  51. if (!empty($tax['delete'])) {
  52. continue;
  53. }
  54. $state = isset($tax['state']) ? $tax['state'] : '*';
  55. $key1 = implode('-', array($tax['website_id'], $tax['country'], $state));
  56. if (!empty($dup[$key1])) {
  57. Mage::throwException(
  58. Mage::helper('catalog')->__('Duplicate website, country and state tax found.')
  59. );
  60. }
  61. $dup[$key1] = 1;
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Assign WEEE taxes to product data
  67. *
  68. * @param Mage_Catalog_Model_Product $object
  69. * @return Mage_Catalog_Model_Product_Attribute_Backend_Weee
  70. */
  71. public function afterLoad($object)
  72. {
  73. $data = $this->_getResource()->loadProductData($object, $this->getAttribute());
  74. foreach ($data as $i=>$row) {
  75. if ($data[$i]['website_id'] == 0) {
  76. $rate = Mage::app()->getStore()->getBaseCurrency()->getRate(Mage::app()->getBaseCurrencyCode());
  77. if ($rate) {
  78. $data[$i]['website_value'] = $data[$i]['value']/$rate;
  79. } else {
  80. unset($data[$i]);
  81. }
  82. } else {
  83. $data[$i]['website_value'] = $data[$i]['value'];
  84. }
  85. }
  86. $object->setData($this->getAttribute()->getName(), $data);
  87. return $this;
  88. }
  89. public function afterSave($object)
  90. {
  91. $orig = $object->getOrigData($this->getAttribute()->getName());
  92. $current = $object->getData($this->getAttribute()->getName());
  93. if ($orig == $current) {
  94. return $this;
  95. }
  96. $this->_getResource()->deleteProductData($object, $this->getAttribute());
  97. $taxes = $object->getData($this->getAttribute()->getName());
  98. if (!is_array($taxes)) {
  99. return $this;
  100. }
  101. foreach ($taxes as $tax) {
  102. if (empty($tax['price']) || empty($tax['country']) || !empty($tax['delete'])) {
  103. continue;
  104. }
  105. if (isset($tax['state']) && $tax['state']) {
  106. $state = $tax['state'];
  107. } else {
  108. $state = '*';
  109. }
  110. $data = array();
  111. $data['website_id'] = $tax['website_id'];
  112. $data['country'] = $tax['country'];
  113. $data['state'] = $state;
  114. $data['value'] = $tax['price'];
  115. $data['attribute_id'] = $this->getAttribute()->getId();
  116. $this->_getResource()->insertProductData($object, $data);
  117. }
  118. return $this;
  119. }
  120. public function afterDelete($object)
  121. {
  122. $this->_getResource()->deleteProductData($object, $this->getAttribute());
  123. return $this;
  124. }
  125. public function getTable()
  126. {
  127. return $this->_getResource()->getTable('weee/tax');
  128. }
  129. }