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:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Tax_Model_Resource_Setup extends Mage_Sales_Model_Resource_Setup
36: {
37: 38: 39: 40:
41: public function convertOldTaxData()
42: {
43: $oldRules = $this->_loadTableData('tax_rule');
44:
45: $oldRateTypes = $this->_loadTableData('tax_rate_type');
46:
47: $rateById = array();
48: foreach ($oldRateTypes as $type) {
49: $rateById[$type['type_id']] = $type['type_name'];
50: }
51:
52: $oldRates = $this->_loadOldRates($oldRateTypes);
53:
54: $oldToNewRateIds = array();
55:
56: foreach ($oldRates as $rate) {
57: foreach ($oldRateTypes as $type) {
58: $rateIndex = sprintf('data_%s', $type['type_id']);
59: if (isset($rate[$rateIndex])) {
60: $rateValue = $rate[$rateIndex];
61: } else {
62: continue;
63: }
64:
65: $region = Mage::getModel('directory/region')->load($rate['tax_region_id']);
66: $regionName = $region->getCode() ? $region->getCode() : '*';
67: $code = "{$rate['tax_country_id']}-{$regionName}-{$rate['tax_postcode']}-{$type['type_name']}";
68:
69: if ($rateValue > 0) {
70: $insertData = array(
71: 'tax_country_id' => $rate['tax_country_id'],
72: 'tax_region_id' => $rate['tax_region_id'],
73: 'tax_postcode' => $rate['tax_postcode'],
74: 'code' => $code,
75: 'rate' => $rateValue,
76: );
77:
78: $newRateModel = Mage::getModel('tax/calculation_rate');
79:
80: $newRateModel->setData($insertData)->save();
81: $oldToNewRateIds[$rate['tax_rate_id']] = $newRateModel->getId();
82: $ratesByType[$type['type_id']][] = $newRateModel->getId();
83: }
84: }
85: }
86:
87: foreach ($oldRules as $rule) {
88: if (!isset($ratesByType[$rule['tax_rate_type_id']]) || !count($ratesByType[$rule['tax_rate_type_id']])) {
89: continue;
90: }
91:
92: $customerTaxClasses = array($rule['tax_customer_class_id']);
93: $productTaxClasses = array($rule['tax_product_class_id']);
94:
95: $ctc = Mage::getModel('tax/class')->load($rule['tax_customer_class_id']);
96: $ptc = Mage::getModel('tax/class')->load($rule['tax_product_class_id']);
97: $type = $rateById[$rule['tax_rate_type_id']];
98:
99: $rates = $ratesByType[$rule['tax_rate_type_id']];
100: $code = "{$ctc->getClassName()}-{$ptc->getClassName()}-{$type}";
101:
102: $ruleData = array(
103: 'tax_rate' => $rates,
104: 'tax_product_class' => $productTaxClasses,
105: 'tax_customer_class' => $customerTaxClasses,
106: 'code' => $code,
107: 'priority' => 1,
108: 'position' => 1
109: );
110: Mage::getModel('tax/calculation_rule')->setData($ruleData)->save();
111: }
112:
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121:
122: protected function _loadTableData($table)
123: {
124: $table = $this->getTable($table);
125: $select = $this->_conn->select();
126: $select->from($table);
127: return $this->_conn->fetchAll($select);
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: protected function _loadOldRates($oldRateTypes)
138: {
139: $table = $this->getTable('tax_rate');
140: $select = $this->_conn->select()
141: ->from(array('main_table'=>$table));
142: foreach ($oldRateTypes as $type){
143: $id = $type['type_id'];
144: $select->joinLeft(
145: array("data_{$id}"=>$this->getTable('tax_rate_data')),
146: "data_{$id}.rate_type_id = {$id} AND data_{$id}.tax_rate_id = main_table.tax_rate_id",
147: array("data_{$id}"=>'rate_value')
148: );
149: }
150: return $this->_conn->fetchAll($select);
151: }
152: }
153: