/**
  * Migrate Tax Data
  */
 public function actionStep9()
 {
     $step = MigrateSteps::model()->find("sorder = 9");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //declare objects to migrate
         $objects = array('tax' => Yii::t('frontend', 'Tax Rules, Tax Zones and Rates'), 'catalog_rule' => Yii::t('frontend', 'Catalog Rules'));
         //variables to log
         $migrated_object_ids = array();
         $migrated_tax_rule_ids = $migrated_tax_rate_ids = $migrated_catalog_rule_ids = array();
         if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) {
             //un-check foreign key
             Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute();
             $selected_objects = Yii::app()->request->getPost('selected_objects', array());
             if ($selected_objects) {
                 //get migrated data from first step in session
                 $migrated_website_ids = isset(Yii::app()->session['migrated_website_ids']) ? Yii::app()->session['migrated_website_ids'] : array();
                 $str_website_ids = implode(',', $migrated_website_ids);
                 $migrated_store_ids = isset(Yii::app()->session['migrated_store_ids']) ? Yii::app()->session['migrated_store_ids'] : array();
                 $str_store_ids = implode(',', $migrated_store_ids);
                 $migrated_customer_group_ids = isset(Yii::app()->session['migrated_customer_group_ids']) ? Yii::app()->session['migrated_customer_group_ids'] : array();
                 $str_customer_group_ids = implode(',', $migrated_customer_group_ids);
                 if (in_array('tax', $selected_objects)) {
                     //tax_calculation_rate
                     //Reset
                     Mage2TaxCalculationRate::model()->deleteAll();
                     //start migrate
                     $models = Mage1TaxCalculationRate::model()->findAll();
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxCalculationRate();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             if ($model2->save()) {
                                 $migrated_tax_rate_ids[] = $model2->tax_calculation_rate_id;
                             }
                         }
                     }
                     //tax_calculation_rate_title
                     $models = Mage1TaxCalculationRateTitle::model()->findAll("store_id IN ({$str_store_ids})");
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxCalculationRateTitle();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     //tax_calculation_rule
                     $models = Mage1TaxCalculationRule::model()->findAll();
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxCalculationRule();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             //this only for Magento 1.6.x
                             if (MigrateSteps::getMG1Version() == 'mage16x') {
                                 $model2->calculate_subtotal = 0;
                             }
                             if ($model2->save()) {
                                 $migrated_tax_rule_ids[] = $model2->tax_calculation_rule_id;
                             }
                         }
                     }
                     //tax_calculation
                     $models = Mage1TaxCalculation::model()->findAll();
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxCalculation();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             if ($model2->save()) {
                                 $migrated_tax_ids[] = $model2->tax_calculation_id;
                             }
                         }
                     }
                     //tax_order_aggregated_created
                     $models = Mage1TaxOrderAggregatedCreated::model()->findAll("store_id IN ({$str_store_ids})");
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxOrderAggregatedCreated();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     //tax_order_aggregated_updated
                     $models = Mage1TaxOrderAggregatedUpdated::model()->findAll("store_id IN ({$str_store_ids})");
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2TaxOrderAggregatedUpdated();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     $migrated_object_ids[] = 'tax';
                 }
                 //end migrate tax
                 if (in_array('catalog_rule', $selected_objects)) {
                     //catalogrule
                     $models = Mage1Catalogrule::model()->findAll();
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2Catalogrule();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             //update conditions, because model name was changed in Magento2
                             $model2->conditions_serialized = MigrateSteps::replaceCatalogRuleModels($model2->conditions_serialized);
                             $model2->actions_serialized = MigrateSteps::replaceCatalogRuleModels($model2->actions_serialized);
                             if ($model2->save()) {
                                 $migrated_catalog_rule_ids[] = $model2->rule_id;
                                 //this only for Magento 1.6.x
                                 if (MigrateSteps::getMG1Version() == 'mage16x') {
                                     if ($model->customer_group_ids) {
                                         $customer_group_ids = explode(',', $model->customer_group_ids);
                                         if ($customer_group_ids) {
                                             foreach ($customer_group_ids as $id) {
                                                 $row = new Mage2CatalogruleCustomerGroup();
                                                 $row->rule_id = $model2->rule_id;
                                                 $row->customer_group_id = $id;
                                                 $row->save();
                                             }
                                         }
                                     }
                                     if ($model->website_ids) {
                                         $website_ids = explode(',', $model->website_ids);
                                         if ($website_ids) {
                                             foreach ($website_ids as $id) {
                                                 $row = new Mage2CatalogruleWebsite();
                                                 $row->rule_id = $model2->rule_id;
                                                 $row->website_id = $id;
                                                 $row->save();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     //catalogrule_group_website
                     $conditions = array();
                     if ($str_website_ids) {
                         $conditions[] = "website_id IN ({$str_website_ids})";
                     }
                     if ($str_customer_group_ids) {
                         $conditions[] = "customer_group_id IN ({$str_customer_group_ids})";
                     }
                     $conditions = implode(" AND ", $conditions);
                     $models = Mage1CatalogruleGroupWebsite::model()->findAll($conditions);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2CatalogruleGroupWebsite();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->save();
                         }
                     }
                     //catalogrule_product
                     $conditions = array();
                     if ($str_website_ids) {
                         $conditions[] = "website_id IN ({$str_website_ids})";
                     }
                     if ($str_customer_group_ids) {
                         $conditions[] = "customer_group_id IN ({$str_customer_group_ids})";
                     }
                     $conditions = implode(" AND ", $conditions);
                     $models = Mage1CatalogruleProduct::model()->findAll($conditions);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2CatalogruleProduct();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->save();
                         }
                     }
                     //we only migrate bellow tables for Magento >= 1.7.x
                     if (MigrateSteps::getMG1Version() != 'mage16x') {
                         //catalogrule_customer_group
                         $condition = '';
                         if ($str_customer_group_ids) {
                             $condition = "customer_group_id IN ({$str_customer_group_ids})";
                         }
                         $models = Mage1CatalogruleCustomerGroup::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogruleCustomerGroup();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 $model2->save();
                             }
                         }
                         //catalogrule_website
                         $conditions = array();
                         if ($str_website_ids) {
                             $conditions[] = "website_id IN ({$str_website_ids})";
                         }
                         $conditions = implode(" AND ", $conditions);
                         $models = Mage1CatalogruleWebsite::model()->findAll($conditions);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogruleWebsite();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 $model2->save();
                             }
                         }
                     }
                     //catalogrule_product_price
                     //this table will auto generate by indexer
                     /*
                                             $conditions = array();
                                             if ($str_website_ids){
                                                 $conditions[] = "website_id IN ({$str_website_ids})";
                                             }
                                             if ($str_customer_group_ids){
                                                 $conditions[] = "customer_group_id IN ({$str_customer_group_ids})";
                                             }
                                             $conditions = implode(" AND ", $conditions);
                                             $models = Mage1CatalogruleProductPrice::model()->findAll($conditions);
                                             if ($models){
                                                 foreach ($models as $model){
                                                     $model2 = new Mage2CatalogruleProductPrice();
                                                     foreach ($model2->attributes as $key => $value){
                                                         if (isset($model->$key)){
                                                             $model2->$key = $model->$key;
                                                         }
                                                     }
                                                     $model2->save();
                                                 }
                                             }*/
                     $migrated_object_ids[] = 'catalog_rule';
                 }
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have not selected any Object.'));
             }
             //Update step status
             if ($migrated_object_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('other_object_ids' => $migrated_object_ids, 'tax_ids' => $migrated_tax_ids, 'catalog_rule_ids' => $migrated_catalog_rule_ids));
                 if ($step->update()) {
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     //update session
                     Yii::app()->session['migrated_other_object_ids'] = $migrated_object_ids;
                     $message = Yii::t('frontend', 'Migrated successfully.');
                     $message .= "<br/>" . Yii::t('frontend', "Total Tax Rules migrated: %s1.", array('%s1' => sizeof($migrated_tax_rule_ids)));
                     $message .= "<br/>" . Yii::t('frontend', "Total Tax Rates migrated: %s1.", array('%s1' => sizeof($migrated_tax_rate_ids)));
                     $message .= "<br/>" . Yii::t('frontend', "Total Catalog Rules migrated: %s2.", array('%s2' => sizeof($migrated_catalog_rule_ids)));
                     Yii::app()->user->setFlash('success', $message);
                 }
             }
         } else {
             if ($step->status == MigrateSteps::STATUS_DONE) {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', "This step was finished. If you want to update data of this step, the first you have to click to 'Reset' button."));
             }
         }
         $this->render("step{$step->sorder}", array('step' => $step, 'objects' => $objects));
     } else {
         Yii::app()->user->setFlash('note', Yii::t('frontend', "The first you need to finish the %s.", array("%s" => ucfirst($result['back_step']))));
         $this->redirect(array($result['back_step']));
     }
 }