/**
  * Migrate Customers
  */
 public function actionStep6()
 {
     $step = MigrateSteps::model()->find("sorder = 6");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //get all current customer groups
         $customer_groups = Mage1CustomerGroup::model()->findAll();
         //variables to log
         $migrated_customer_group_ids = array();
         $migrated_customer_ids = array();
         if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) {
             //uncheck foreign key
             Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute();
             $selected_group_ids = Yii::app()->request->getPost('customer_group_ids', array());
             if ($selected_group_ids) {
                 foreach ($selected_group_ids as $group_id) {
                     //customer_group
                     $customer_group1 = Mage1CustomerGroup::model()->findByPk($group_id);
                     $customer_group2 = Mage2CustomerGroup::model()->find("customer_group_id = {$group_id} AND customer_group_code = '{$customer_group1->customer_group_code}'");
                     if (!$customer_group2) {
                         $customer_group2 = new Mage2CustomerGroup();
                         $customer_group2->customer_group_id = $group_id;
                         $customer_group2->customer_group_code = $customer_group1->customer_group_code;
                     }
                     //update tax class_id if have exits
                     $customer_group2->tax_class_id = $customer_group1->tax_class_id;
                     if ($customer_group2->save()) {
                         $migrated_customer_group_ids[] = $customer_group2->customer_group_id;
                         //we will migrate related tax_class here
                         $tax_class1 = Mage1TaxClass::model()->findByPk($customer_group2->tax_class_id);
                         if ($tax_class1) {
                             $tax_class2 = Mage2TaxClass::model()->findByPk($tax_class1->class_id);
                             if (!$tax_class2) {
                                 $tax_class2 = new Mage2TaxClass();
                                 $tax_class2->class_id = $tax_class1->class_id;
                             }
                             $tax_class2->class_type = $tax_class1->class_type;
                             $tax_class2->class_name = $tax_class1->class_name;
                             $tax_class2->save();
                         }
                         //migrate all customers of this customer group
                         //customer_entity
                         //This only for Magento 2 version from 0.74.0 - beta 12
                         //needed_update_attr.
                         $needed_update_attr = array('created_in', 'firstname', 'lastname', 'password_hash', 'rp_token', 'rp_token_created_at');
                         $customers = Mage1CustomerEntity::model()->findAll("group_id = {$group_id}");
                         if ($customers) {
                             foreach ($customers as $customer) {
                                 $customer2 = Mage2CustomerEntity::model()->findByPk($customer->entity_id);
                                 if (!$customer2) {
                                     $customer2 = new Mage2CustomerEntity();
                                     foreach ($customer2->attributes as $key => $value) {
                                         if (isset($customer->{$key})) {
                                             $customer2->{$key} = $customer->{$key};
                                         }
                                     }
                                     $customer2->store_id = MigrateSteps::getMage2StoreId($customer->store_id);
                                     if ($customer2->save()) {
                                         $migrated_customer_ids[] = $customer2->entity_id;
                                         //customer_entity_datetime
                                         $models = Mage1CustomerEntityDatetime::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityDatetime();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_entity_decimal
                                         $models = Mage1CustomerEntityDecimal::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityDecimal();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     $model2->save();
                                                 }
                                             }
                                         }
                                         //customer_entity_int
                                         $models = Mage1CustomerEntityInt::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 //(example: reward_update_notification, reward_warning_notification)
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityInt();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     $model2->save();
                                                 }
                                             }
                                         }
                                         //customer_entity_text
                                         $models = Mage1CustomerEntityText::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityText();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     $model2->save();
                                                 }
                                             }
                                         }
                                         //customer_entity_varchar
                                         $models = Mage1CustomerEntityVarchar::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityVarchar();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_address_entity
                                         $address_entities = Mage1CustomerAddressEntity::model()->findAll("parent_id = {$customer->entity_id}");
                                         if ($address_entities) {
                                             foreach ($address_entities as $address_entity) {
                                                 $address_entity2 = new Mage2CustomerAddressEntity();
                                                 foreach ($address_entity2->attributes as $key => $value) {
                                                     if (isset($address_entity->{$key})) {
                                                         $address_entity2->{$key} = $address_entity->{$key};
                                                     }
                                                 }
                                                 if ($address_entity2->save()) {
                                                     //customer_address_entity_datetime
                                                     $models = Mage1CustomerAddressEntityDatetime::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityDatetime();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 $model2->save();
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_decimal
                                                     $models = Mage1CustomerAddressEntityDecimal::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityDecimal();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 $model2->save();
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_int
                                                     $models = Mage1CustomerAddressEntityInt::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityInt();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 $model2->save();
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_text
                                                     $models = Mage1CustomerAddressEntityText::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityText();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 $model2->save();
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_varchar
                                                     $models = Mage1CustomerAddressEntityVarchar::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityVarchar();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 $model2->save();
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         //end a customer entity address
                                     }
                                     //and save a customer entity
                                 }
                             }
                         }
                     }
                     //end save a customer group
                 }
                 //customer_eav_attribute
                 //customer_eav_attribute_website
                 //customer_form_attribute
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have to select at least one Customer Group to migrate.'));
             }
             //Update step status
             if ($migrated_customer_group_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('customer_group_ids' => $migrated_customer_group_ids, 'customer_ids' => $migrated_customer_ids));
                 if ($step->update()) {
                     //update session
                     Yii::app()->session['migrated_customer_group_ids'] = $migrated_customer_group_ids;
                     Yii::app()->session['migrated_customer_ids'] = $migrated_customer_ids;
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     $message = "Migrated successfully. Total Customer Groups migrated: %s1 and total Customers migrated: %s2.";
                     $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_customer_group_ids), '%s2' => sizeof($migrated_customer_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, 'customer_groups' => $customer_groups));
     } 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']));
     }
 }
 /**
  * Migrate Customers
  */
 public function actionStep6()
 {
     $step = MigrateSteps::model()->find("sorder = 6");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //get all current customer groups
         $customer_groups = Mage1CustomerGroup::model()->findAll();
         //variables to log
         $errors = array();
         $migrated_customer_group_ids = array();
         $migrated_customer_ids = array();
         if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) {
             //uncheck foreign key
             Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute();
             $selected_group_ids = Yii::app()->request->getPost('customer_group_ids', array());
             if ($selected_group_ids) {
                 foreach ($selected_group_ids as $group_id) {
                     //customer_group
                     $customer_group1 = Mage1CustomerGroup::model()->findByPk($group_id);
                     $customer_group2 = Mage2CustomerGroup::model()->find("customer_group_id = {$group_id} AND customer_group_code = '{$customer_group1->customer_group_code}'");
                     if (!$customer_group2) {
                         $customer_group2 = new Mage2CustomerGroup();
                         $customer_group2->customer_group_id = $group_id;
                         $customer_group2->customer_group_code = $customer_group1->customer_group_code;
                     }
                     //update tax class_id if have exits
                     $customer_group2->tax_class_id = $customer_group1->tax_class_id;
                     if ($customer_group2->save()) {
                         $migrated_customer_group_ids[] = $customer_group2->customer_group_id;
                         //we will migrate related tax_class here
                         $tax_class1 = Mage1TaxClass::model()->findByPk($customer_group2->tax_class_id);
                         if ($tax_class1) {
                             $tax_class2 = Mage2TaxClass::model()->findByPk($tax_class1->class_id);
                             if (!$tax_class2) {
                                 $tax_class2 = new Mage2TaxClass();
                                 $tax_class2->class_id = $tax_class1->class_id;
                             }
                             $tax_class2->class_type = $tax_class1->class_type;
                             $tax_class2->class_name = $tax_class1->class_name;
                             $tax_class2->save();
                         }
                         //migrate all customers of this customer group
                         //customer_entity
                         //This only for Magento 2 version from 0.74.0 - beta 12
                         //needed_update_attr.
                         $needed_update_attr = array('created_in', 'firstname', 'middlename', 'lastname', 'password_hash', 'rp_token', 'rp_token_created_at', 'prefix', 'suffix', 'dob', 'default_billing', 'default_shipping', 'taxvat', 'confirmation', 'gender');
                         $customers = Mage1CustomerEntity::model()->findAll("group_id = {$group_id}");
                         if ($customers) {
                             foreach ($customers as $customer) {
                                 $customer2 = Mage2CustomerEntity::model()->findByPk($customer->entity_id);
                                 if (!$customer2) {
                                     $customer2 = new Mage2CustomerEntity();
                                     foreach ($customer2->attributes as $key => $value) {
                                         if (isset($customer->{$key})) {
                                             $customer2->{$key} = $customer->{$key};
                                         }
                                     }
                                     $customer2->store_id = MigrateSteps::getMage2StoreId($customer->store_id);
                                     if ($customer2->save()) {
                                         $migrated_customer_ids[] = $customer2->entity_id;
                                         //customer_entity_datetime
                                         $models = Mage1CustomerEntityDatetime::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityDatetime();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     } else {
                                                         $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_entity_decimal
                                         $models = Mage1CustomerEntityDecimal::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityDecimal();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     } else {
                                                         $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_entity_int
                                         $models = Mage1CustomerEntityInt::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 //(example: reward_update_notification, reward_warning_notification)
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityInt();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     } else {
                                                         $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_entity_text
                                         $models = Mage1CustomerEntityText::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityText();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     } else {
                                                         $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_entity_varchar
                                         $models = Mage1CustomerEntityVarchar::model()->findAll("entity_id = {$customer->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 1);
                                                 // This because some system customer attribute_code was not using in Magento2
                                                 if ($attribute_id2) {
                                                     $model2 = new Mage2CustomerEntityVarchar();
                                                     foreach ($model2->attributes as $key => $value) {
                                                         if (isset($model->{$key})) {
                                                             $model2->{$key} = $model->{$key};
                                                         }
                                                     }
                                                     $model2->attribute_id = $attribute_id2;
                                                     if ($model2->save()) {
                                                         //this only for Magento 2 from version 0.74.0 - beta 12
                                                         $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                         if (in_array($attribute_code1, $needed_update_attr)) {
                                                             if ($customer2->hasAttribute($attribute_code1)) {
                                                                 $customer2->{$attribute_code1} = $model->value;
                                                                 //we have to do this, because the Magento CE 2.0.0 or later was change method to hash password: md5 -> sha256
                                                                 if ($attribute_code1 == 'password_hash') {
                                                                     $customer2->{$attribute_code1} .= ":0";
                                                                     // In Magento2: 0 = HASH_VERSION_MD5
                                                                 }
                                                                 $customer2->update();
                                                             }
                                                         }
                                                     } else {
                                                         $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                     }
                                                 }
                                             }
                                         }
                                         //customer_address_entity
                                         //Some attributes was move from children tables to main table
                                         //So we need to do this
                                         //needed_update_attr.
                                         $needed_update_attr2 = array('country_id', 'firstname', 'lastname', 'middlename', 'street', 'telephone', 'city', 'fax', 'company', 'country_id', 'postcode', 'prefix', 'region', 'region_id', 'suffix', 'vat_id', 'vat_is_valid', 'vat_request_date', 'vat_request_id', 'vat_request_success');
                                         $address_entities = Mage1CustomerAddressEntity::model()->findAll("parent_id = {$customer->entity_id}");
                                         if ($address_entities) {
                                             foreach ($address_entities as $address_entity) {
                                                 $address_entity2 = new Mage2CustomerAddressEntity();
                                                 foreach ($address_entity2->attributes as $key => $value) {
                                                     if (isset($address_entity->{$key})) {
                                                         $address_entity2->{$key} = $address_entity->{$key};
                                                     }
                                                 }
                                                 //some fields is new in Magento2 and required at this table, so we need to do this
                                                 //and we will update correct value for them later
                                                 $address_entity2->country_id = '0';
                                                 $address_entity2->firstname = 'unknown';
                                                 $address_entity2->lastname = 'unknown';
                                                 $address_entity2->street = 'unknown';
                                                 $address_entity2->telephone = 'unknown';
                                                 $address_entity2->city = 'unknown';
                                                 if ($address_entity2->save()) {
                                                     //customer_address_entity_datetime
                                                     $models = Mage1CustomerAddressEntityDatetime::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityDatetime();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 if ($model2->save()) {
                                                                     //some attributes was move to main table, so we need to do this
                                                                     $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                                     if (in_array($attribute_code1, $needed_update_attr2)) {
                                                                         if ($address_entity2->hasAttribute($attribute_code1)) {
                                                                             $address_entity2->{$attribute_code1} = $model->value;
                                                                             $address_entity2->update();
                                                                         }
                                                                     }
                                                                 } else {
                                                                     $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                                 }
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_decimal
                                                     $models = Mage1CustomerAddressEntityDecimal::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityDecimal();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 if ($model2->save()) {
                                                                     //some attributes was move to main table, so we need to do this
                                                                     $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                                     if (in_array($attribute_code1, $needed_update_attr2)) {
                                                                         if ($address_entity2->hasAttribute($attribute_code1)) {
                                                                             $address_entity2->{$attribute_code1} = $model->value;
                                                                             $address_entity2->update();
                                                                         }
                                                                     }
                                                                 } else {
                                                                     $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                                 }
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_int
                                                     $models = Mage1CustomerAddressEntityInt::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityInt();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 if ($model2->save()) {
                                                                     //some attributes was move to main table, so we need to do this
                                                                     $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                                     if (in_array($attribute_code1, $needed_update_attr2)) {
                                                                         if ($address_entity2->hasAttribute($attribute_code1)) {
                                                                             $address_entity2->{$attribute_code1} = $model->value;
                                                                             $address_entity2->update();
                                                                         }
                                                                     }
                                                                 } else {
                                                                     $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                                 }
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_text
                                                     $models = Mage1CustomerAddressEntityText::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityText();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 if ($model2->save()) {
                                                                     //some attributes was move to main table, so we need to do this
                                                                     $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                                     if (in_array($attribute_code1, $needed_update_attr2)) {
                                                                         if ($address_entity2->hasAttribute($attribute_code1)) {
                                                                             $address_entity2->{$attribute_code1} = $model->value;
                                                                             $address_entity2->update();
                                                                         }
                                                                     }
                                                                 } else {
                                                                     $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                                 }
                                                             }
                                                         }
                                                     }
                                                     //customer_address_entity_varchar
                                                     $models = Mage1CustomerAddressEntityVarchar::model()->findAll("entity_id = {$address_entity2->entity_id}");
                                                     if ($models) {
                                                         foreach ($models as $model) {
                                                             $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 2);
                                                             // This because some system customer attribute_code was not using in Magento2
                                                             if ($attribute_id2) {
                                                                 $model2 = new Mage2CustomerAddressEntityVarchar();
                                                                 foreach ($model2->attributes as $key => $value) {
                                                                     if (isset($model->{$key})) {
                                                                         $model2->{$key} = $model->{$key};
                                                                     }
                                                                 }
                                                                 $model2->attribute_id = $attribute_id2;
                                                                 if ($model2->save()) {
                                                                     //some attributes was move to main table, so we need to do this
                                                                     $attribute_code1 = MigrateSteps::getMage1AttributeCode($model->attribute_id);
                                                                     if (in_array($attribute_code1, $needed_update_attr2)) {
                                                                         if ($address_entity2->hasAttribute($attribute_code1)) {
                                                                             $address_entity2->{$attribute_code1} = $model->value;
                                                                             $address_entity2->update();
                                                                         }
                                                                     }
                                                                 } else {
                                                                     $errors[] = get_class($model2) . ": " . MigrateSteps::getStringErrors($model2->getErrors());
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 } else {
                                                     $errors[] = get_class($address_entity2) . ": " . MigrateSteps::getStringErrors($address_entity2->getErrors());
                                                 }
                                             }
                                         }
                                         //end a customer entity address
                                     } else {
                                         $errors[] = get_class($customer2) . ": " . MigrateSteps::getStringErrors($customer2->getErrors());
                                     }
                                     //and save a customer entity
                                 }
                             }
                         }
                     }
                     //end save a customer group
                 }
                 //customer_eav_attribute
                 //customer_eav_attribute_website
                 //customer_form_attribute
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have to select at least one Customer Group to migrate.'));
             }
             //Update step status
             if ($migrated_customer_group_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('customer_group_ids' => $migrated_customer_group_ids, 'customer_ids' => $migrated_customer_ids));
                 if ($step->update()) {
                     //update session
                     Yii::app()->session['migrated_customer_group_ids'] = $migrated_customer_group_ids;
                     Yii::app()->session['migrated_customer_ids'] = $migrated_customer_ids;
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     $message = "Migrated successfully. Total Customer Groups migrated: %s1 and total Customers migrated: %s2.";
                     $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_customer_group_ids), '%s2' => sizeof($migrated_customer_ids)));
                     Yii::app()->user->setFlash('success', $message);
                 }
             }
             //alert errors if exists
             if ($errors) {
                 $strErrors = implode('<br/>', $errors);
                 Yii::app()->user->setFlash('error', $strErrors);
             }
         } 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, 'customer_groups' => $customer_groups));
     } 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']));
     }
 }