00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Tax_Model_Mysql4_Setup extends Mage_Core_Model_Resource_Setup
00033 {
00034 public function convertOldTaxData()
00035 {
00036 $oldRules = $this->_loadTableData('tax_rule');
00037
00038 $oldRateTypes = $this->_loadTableData('tax_rate_type');
00039
00040 $rateById = array();
00041 foreach ($oldRateTypes as $type) {
00042 $rateById[$type['type_id']] = $type['type_name'];
00043 }
00044
00045 $oldRates = $this->_loadOldRates($oldRateTypes);
00046
00047 $oldToNewRateIds = array();
00048
00049 foreach ($oldRates as $rate) {
00050 foreach ($oldRateTypes as $type) {
00051 if (isset($rate["data_{$type['type_id']}"])) {
00052 $rateValue = $rate["data_{$type['type_id']}"];
00053 } else {
00054 continue;
00055 }
00056
00057 $region = Mage::getModel('directory/region')->load($rate['tax_region_id']);
00058 $regionName = $region->getCode() ? $region->getCode() : '*';
00059 $code = "{$rate['tax_country_id']}-{$regionName}-{$rate['tax_postcode']}-{$type['type_name']}";
00060
00061 if ($rateValue > 0) {
00062 $insertData = array(
00063 'tax_country_id'=>$rate['tax_country_id'],
00064 'tax_region_id'=>$rate['tax_region_id'],
00065 'tax_postcode'=>$rate['tax_postcode'],
00066 'code'=>$code,
00067 'rate'=>$rateValue,
00068 );
00069
00070 $newRateModel = Mage::getModel('tax/calculation_rate');
00071
00072 $newRateModel->setData($insertData)->save();
00073 $oldToNewRateIds[$rate['tax_rate_id']] = $newRateModel->getId();
00074 $ratesByType[$type['type_id']][] = $newRateModel->getId();
00075 }
00076 }
00077 }
00078
00079 foreach ($oldRules as $rule) {
00080 if (!isset($ratesByType[$rule['tax_rate_type_id']]) || !count($ratesByType[$rule['tax_rate_type_id']])){
00081 continue;
00082 }
00083
00084 $customerTaxClasses = array($rule['tax_customer_class_id']);
00085 $productTaxClasses = array($rule['tax_product_class_id']);
00086
00087 $ctc = Mage::getModel('tax/class')->load($rule['tax_customer_class_id']);
00088 $ptc = Mage::getModel('tax/class')->load($rule['tax_product_class_id']);
00089 $type = $rateById[$rule['tax_rate_type_id']];
00090
00091 $rates = $ratesByType[$rule['tax_rate_type_id']];
00092 $code = "{$ctc->getClassName()}-{$ptc->getClassName()}-{$type}";
00093
00094 $ruleData = array(
00095 'tax_rate'=>$rates,
00096 'tax_product_class'=>$productTaxClasses,
00097 'tax_customer_class'=>$customerTaxClasses,
00098 'code'=>$code,
00099 'priority'=>1,
00100 'position'=>1
00101 );
00102 Mage::getModel('tax/calculation_rule')->setData($ruleData)->save();
00103 }
00104 }
00105
00106 protected function _loadTableData($table)
00107 {
00108 $table = $this->getTable($table);
00109 $select = $this->_conn->select();
00110 $select->from($table);
00111 return $this->_conn->fetchAll($select);
00112 }
00113
00114 protected function _loadOldRates($oldRateTypes)
00115 {
00116
00117 $table = $this->getTable('tax_rate');
00118 $select = $this->_conn->select();
00119 $select->from(array('main_table'=>$table));
00120 foreach ($oldRateTypes as $type){
00121 $id = $type['type_id'];
00122 $select->joinLeft(
00123 array("data_{$id}"=>$this->getTable('tax_rate_data')),
00124 "data_{$id}.rate_type_id = {$id} AND data_{$id}.tax_rate_id = main_table.tax_rate_id",
00125 array("data_{$id}"=>'rate_value')
00126 );
00127 }
00128 return $this->_conn->fetchAll($select);
00129 }
00130 }