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_Tax
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: /**
28: * Tax Rate Model
29: *
30: * @method Mage_Tax_Model_Resource_Calculation_Rate _getResource()
31: * @method Mage_Tax_Model_Resource_Calculation_Rate getResource()
32: * @method string getTaxCountryId()
33: * @method Mage_Tax_Model_Calculation_Rate setTaxCountryId(string $value)
34: * @method int getTaxRegionId()
35: * @method Mage_Tax_Model_Calculation_Rate setTaxRegionId(int $value)
36: * @method string getTaxPostcode()
37: * @method Mage_Tax_Model_Calculation_Rate setTaxPostcode(string $value)
38: * @method string getCode()
39: * @method Mage_Tax_Model_Calculation_Rate setCode(string $value)
40: * @method float getRate()
41: * @method Mage_Tax_Model_Calculation_Rate setRate(float $value)
42: * @method int getZipIsRange()
43: * @method Mage_Tax_Model_Calculation_Rate setZipIsRange(int $value)
44: * @method int getZipFrom()
45: * @method Mage_Tax_Model_Calculation_Rate setZipFrom(int $value)
46: * @method int getZipTo()
47: * @method Mage_Tax_Model_Calculation_Rate setZipTo(int $value)
48: *
49: * @category Mage
50: * @package Mage_Tax
51: * @author Magento Core Team <core@magentocommerce.com>
52: */
53: class Mage_Tax_Model_Calculation_Rate extends Mage_Core_Model_Abstract
54: {
55: protected $_titles = null;
56: protected $_titleModel = null;
57:
58: /**
59: * Varien model constructor
60: */
61: protected function _construct()
62: {
63: $this->_init('tax/calculation_rate');
64: }
65:
66: /**
67: * Prepare location settings and tax postcode before save rate
68: *
69: * @return Mage_Tax_Model_Calculation_Rate
70: */
71: protected function _beforeSave()
72: {
73: if ($this->getZipIsRange()) {
74: $zipFrom = substr($this->getZipFrom(), 0, 9);
75: $zipTo = substr($this->getZipTo(), 0, 9);
76: $this->setTaxPostcode($zipFrom . '-' . $zipTo);
77: } else {
78: $taxPostCode = $this->getTaxPostcode();
79:
80: // postcode must be not longer than 10 symbols
81: if (strlen($taxPostCode) > 10) {
82: $taxPostCode = substr($taxPostCode, 0, 10);
83: }
84:
85: $this->setTaxPostcode($taxPostCode)
86: ->setZipIsRange(null)
87: ->setZipFrom(null)
88: ->setZipTo(null);
89: }
90:
91: parent::_beforeSave();
92: $country = $this->getTaxCountryId();
93: $region = $this->getTaxRegionId();
94: $regionModel = Mage::getModel('directory/region');
95: $regionModel->load($region);
96: if ($regionModel->getCountryId() != $country) {
97: $this->setTaxRegionId('*');
98: }
99: return $this;
100: }
101:
102: /**
103: * Save rate titles
104: *
105: * @return Mage_Tax_Model_Calculation_Rate
106: */
107: protected function _afterSave()
108: {
109: $this->saveTitles();
110: Mage::dispatchEvent('tax_settings_change_after');
111: return parent::_afterSave();
112: }
113:
114: /**
115: * Processing object before delete data
116: *
117: * @return Mage_Core_Model_Abstract
118: * @throws Mage_Core_Exception
119: */
120: protected function _beforeDelete()
121: {
122: if ($this->_isInRule()) {
123: Mage::throwException(Mage::helper('tax')->__('Tax rate cannot be removed. It exists in tax rule'));
124: }
125: return parent::_beforeDelete();
126: }
127:
128: /**
129: * After rate delete
130: * redeclared for dispatch tax_settings_change_after event
131: *
132: * @return Mage_Tax_Model_Calculation_Rate
133: */
134: protected function _afterDelete()
135: {
136: Mage::dispatchEvent('tax_settings_change_after');
137: return parent::_afterDelete();
138: }
139:
140: public function saveTitles($titles = null)
141: {
142: if (is_null($titles)) {
143: $titles = $this->getTitle();
144: }
145:
146: $this->getTitleModel()->deleteByRateId($this->getId());
147: if (is_array($titles) && $titles) {
148: foreach ($titles as $store=>$title) {
149: if ($title !== '') {
150: $this->getTitleModel()
151: ->setId(null)
152: ->setTaxCalculationRateId($this->getId())
153: ->setStoreId((int) $store)
154: ->setValue($title)
155: ->save();
156: }
157: }
158: }
159: }
160:
161: public function getTitleModel()
162: {
163: if (is_null($this->_titleModel)) {
164: $this->_titleModel = Mage::getModel('tax/calculation_rate_title');
165: }
166: return $this->_titleModel;
167: }
168:
169: public function getTitles()
170: {
171: if (is_null($this->_titles)) {
172: $this->_titles = $this->getTitleModel()->getCollection()->loadByRateId($this->getId());
173: }
174: return $this->_titles;
175: }
176:
177: public function deleteAllRates()
178: {
179: $this->_getResource()->deleteAllRates();
180: Mage::dispatchEvent('tax_settings_change_after');
181: return $this;
182: }
183:
184: /**
185: * Load rate model by code
186: *
187: * @param string $code
188: * @return Mage_Tax_Model_Calculation_Rate
189: */
190: public function loadByCode($code)
191: {
192: $this->load($code, 'code');
193: return $this;
194: }
195:
196:
197: /**
198: * Check if rate exists in tax rule
199: *
200: * @return array
201: */
202: protected function _isInRule()
203: {
204: return $this->getResource()->isInRule($this->getId());
205: }
206: }
207: