/**
  * Migrate Data from:
  * Sales Orders, Sales Quote, Sales Payments, Sales Invoices, Sales Shipments
  */
 public function actionStep7()
 {
     $step = MigrateSteps::model()->find("sorder = 7");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //declare objects to migrate
         $sales_objects = array('order' => Yii::t('frontend', 'Sales Orders'), 'quote' => Yii::t('frontend', 'Sales Quote'), 'payment' => Yii::t('frontend', 'Sales Payments'), 'invoice' => Yii::t('frontend', 'Sales Invoices'), 'shipment' => Yii::t('frontend', 'Sales Shipments'), 'credit' => Yii::t('frontend', 'Sales Credit Memo'), 'bestseller' => Yii::t('frontend', 'Sales Bestsellers'), 'rule_coupon' => Yii::t('frontend', 'Sales Rules & Coupons'));
         //variables to log
         $migrated_sales_object_ids = array();
         $migrated_order_ids = $migrated_quote_ids = $migrated_payment_ids = $migrated_invoice_ids = $migrated_shipment_ids = $migrated_credit_ids = array();
         $migrated_order_statuses = $migrated_sales_rule_ids = $migrated_sales_coupon_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_objects = Yii::app()->request->getPost('selected_objects', array());
             $selected_objects[] = 'bestseller';
             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_ids = isset(Yii::app()->session['migrated_customer_ids']) ? Yii::app()->session['migrated_customer_ids'] : array();
                 $str_customer_ids = implode(',', $migrated_customer_ids);
                 $migrated_product_ids = isset(Yii::app()->session['migrated_product_ids']) ? Yii::app()->session['migrated_product_ids'] : array();
                 $str_product_ids = implode(',', $migrated_product_ids);
                 if (in_array('order', $selected_objects)) {
                     //sales_order_status
                     $models = Mage1SalesOrderStatus::model()->findAll();
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = Mage2SalesOrderStatus::model()->find("status = '{$model->status}'");
                             if (!$model2) {
                                 $model2 = new Mage2SalesOrderStatus();
                                 $model2->status = $model->status;
                             }
                             $model2->label = $model->label;
                             if ($model2->save()) {
                                 $migrated_order_statuses[] = $model->status;
                                 //sales_order_status_label
                                 $condition = "status = '{$model->status}'";
                                 if ($str_store_ids) {
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1SalesOrderStatusLabel::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderStatusLabel();
                                         $model2->attributes = $model->attributes;
                                         $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                         $model2->save();
                                     }
                                 }
                                 //sales_order_status_state
                                 $condition = "status = '{$model->status}'";
                                 $models = Mage1SalesOrderStatusState::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = Mage2SalesOrderStatusState::model()->find("status = '{$model->status}' AND state = '{$model->state}'");
                                         if (!$model2) {
                                             $model2 = new Mage2SalesOrderStatusState();
                                             $model2->status = $model->status;
                                             $model2->state = $model->state;
                                             //this field not exists in Magento1
                                             $model2->visible_on_front = 0;
                                         }
                                         $model2->is_default = $model->is_default;
                                         $model2->save();
                                     }
                                 }
                             }
                         }
                     }
                     //sales_order
                     $condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL )";
                     if ($str_customer_ids) {
                         $condition .= " AND ( customer_id IN ({$str_customer_ids}) OR customer_id IS NULL )";
                     }
                     $sales_orders = Mage1SalesOrder::model()->findAll($condition);
                     if ($sales_orders) {
                         foreach ($sales_orders as $sales_order) {
                             $sales_order2 = new Mage2SalesOrder();
                             foreach ($sales_order2->attributes as $key => $value) {
                                 if (isset($sales_order->{$key})) {
                                     $sales_order2->{$key} = $sales_order->{$key};
                                 }
                             }
                             //we have changed store_id in magento2
                             if ($sales_order2->store_id) {
                                 $sales_order2->store_id = MigrateSteps::getMage2StoreId($sales_order2->store_id);
                             }
                             if ($sales_order2->save()) {
                                 $migrated_order_ids[] = $sales_order->entity_id;
                                 //sales_order_address
                                 $models = Mage1SalesOrderAddress::model()->findAll("parent_id = {$sales_order->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderAddress();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         $model2->save();
                                     }
                                 }
                                 //sales_order_grid
                                 $models = Mage1SalesOrderGrid::model()->findAll("entity_id = {$sales_order->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderGrid();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         //we have changed store_id in magento2
                                         $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                         $model2->save();
                                     }
                                 }
                                 //sales_order_item
                                 $models = Mage1SalesOrderItem::model()->findAll("order_id = {$sales_order->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderItem();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                         $model2->save();
                                     }
                                 }
                                 //sales_order_status_history
                                 $models = Mage1SalesOrderStatusHistory::model()->findAll("parent_id = {$sales_order->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderStatusHistory();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         $model2->save();
                                     }
                                 }
                                 //sales_order_tax
                                 $models = Mage1SalesOrderTax::model()->findAll("order_id = {$sales_order->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesOrderTax();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         if ($model2->save()) {
                                             //sales_order_tax_item
                                             $items = Mage1SalesOrderTaxItem::model()->findAll("tax_id = {$model->tax_id}");
                                             if ($items) {
                                                 foreach ($items as $item) {
                                                     $item2 = new Mage2SalesOrderTaxItem();
                                                     foreach ($item2->attributes as $key => $value) {
                                                         if (isset($item->{$key})) {
                                                             $item2->{$key} = $item->{$key};
                                                         }
                                                     }
                                                     //bellow fields was not exists in Magento1 -> note
                                                     $item2->amount = 0;
                                                     $item2->base_amount = 0;
                                                     $item2->real_amount = 0;
                                                     $item2->real_base_amount = 0;
                                                     $item2->associated_item_id = null;
                                                     $item2->taxable_item_type = '';
                                                     $item2->save();
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             //end save a sales order
                         }
                     }
                     //sales_order_aggregated_created
                     $condition = "store_id IN ({$str_store_ids}) OR store_id is NULL";
                     $models = Mage1SalesOrderAggregatedCreated::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesOrderAggregatedCreated();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     //sales_order_aggregated_updated
                     $condition = "store_id IN ({$str_store_ids}) OR store_id is NULL";
                     $models = Mage1SalesOrderAggregatedUpdated::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesOrderAggregatedUpdated();
                             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_sales_object_ids[] = 'order';
                 }
                 //end migrate orders
                 //Sales Quote
                 if (in_array('quote', $selected_objects)) {
                     //quote
                     //$condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL ) AND ( customer_id IN ({$str_customer_ids}) OR customer_id IS NULL )";
                     $condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL )";
                     $quotes = Mage1SalesQuote::model()->findAll($condition);
                     if ($quotes) {
                         foreach ($quotes as $quote) {
                             $quote2 = new Mage2SalesQuote();
                             foreach ($quote2->attributes as $key => $value) {
                                 if (isset($quote->{$key})) {
                                     $quote2->{$key} = $quote->{$key};
                                 }
                             }
                             $quote2->store_id = MigrateSteps::getMage2StoreId($quote->store_id);
                             if ($quote2->save()) {
                                 $migrated_quote_ids[] = $quote->entity_id;
                                 //quote_item
                                 $models = Mage1SalesQuoteItem::model()->findAll("quote_id = {$quote->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesQuoteItem();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                         if ($model2->save()) {
                                             //quote_item_option
                                             $item_options = Mage1SalesQuoteItemOption::model()->findAll("item_id = {$model->item_id}");
                                             if ($item_options) {
                                                 foreach ($item_options as $item_option) {
                                                     $item_option2 = new Mage2SalesQuoteItemOption();
                                                     foreach ($item_option2->attributes as $key => $value) {
                                                         if (isset($item_option->{$key})) {
                                                             $item_option2->{$key} = $item_option->{$key};
                                                         }
                                                     }
                                                     $item_option2->save();
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 //quote_payment
                                 $models = Mage1SalesQuotePayment::model()->findAll("quote_id = {$quote->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesQuotePayment();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         $model2->save();
                                     }
                                 }
                                 //quote_address
                                 $models = Mage1SalesQuoteAddress::model()->findAll("quote_id = {$quote->entity_id}");
                                 if ($models) {
                                     foreach ($models as $model) {
                                         $model2 = new Mage2SalesQuoteAddress();
                                         foreach ($model2->attributes as $key => $value) {
                                             if (isset($model->{$key})) {
                                                 $model2->{$key} = $model->{$key};
                                             }
                                         }
                                         if ($model2->save()) {
                                             //quote_address_item
                                             $address_items = Mage1SalesQuoteAddressItem::model()->findAll("quote_address_id = {$model->address_id}");
                                             if ($address_items) {
                                                 foreach ($address_items as $address_item) {
                                                     $address_item2 = new Mage2SalesQuoteAddressItem();
                                                     foreach ($address_item2->attributes as $key => $value) {
                                                         if (isset($address_item->{$key})) {
                                                             $address_item2->{$key} = $address_item->{$key};
                                                         }
                                                     }
                                                     $address_item2->save();
                                                 }
                                             }
                                             //quote_shipping_rate
                                             $shipping_rates = Mage1SalesQuoteShippingRate::model()->findAll("address_id = {$model->address_id}");
                                             if ($shipping_rates) {
                                                 foreach ($shipping_rates as $shipping_rate) {
                                                     $shipping_rate2 = new Mage2SalesQuoteShippingRate();
                                                     foreach ($shipping_rate2->attributes as $key => $value) {
                                                         if (isset($shipping_rate->{$key})) {
                                                             $shipping_rate2->{$key} = $shipping_rate->{$key};
                                                         }
                                                     }
                                                     $shipping_rate2->save();
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     $migrated_sales_object_ids[] = 'quote';
                 }
                 //end sales quote
                 if (in_array('payment', $selected_objects)) {
                     if ($migrated_order_ids) {
                         $str_order_ids = implode(',', $migrated_order_ids);
                         $condition = "parent_id IN ({$str_order_ids})";
                         //sales_order_payment
                         $sales_payments = Mage1SalesOrderPayment::model()->findAll($condition);
                         if ($sales_payments) {
                             foreach ($sales_payments as $sales_payment) {
                                 $sales_payment2 = new Mage2SalesOrderPayment();
                                 foreach ($sales_payment2->attributes as $key => $value) {
                                     if (isset($sales_payment->{$key})) {
                                         $sales_payment2->{$key} = $sales_payment->{$key};
                                     }
                                 }
                                 //because the this field name was changed in Magento 2
                                 $sales_payment2->cc_last_4 = isset($sales_payment->cc_last4) ? $sales_payment->cc_last4 : null;
                                 if ($sales_payment2->save()) {
                                     //sales_payment_transaction
                                     $models = Mage1SalesPaymentTransaction::model()->findAll("payment_id = {$sales_payment->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesPaymentTransaction();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     //sales_refunded_aggregated
                     $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                     $models = Mage1SalesRefundedAggregated::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesRefundedAggregated();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     //sales_refunded_aggregated_order
                     $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                     $models = Mage1SalesRefundedAggregatedOrder::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesRefundedAggregatedOrder();
                             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_sales_object_ids[] = 'payment';
                 }
                 //end sales payment
                 if (in_array('invoice', $selected_objects)) {
                     if ($migrated_order_ids) {
                         $condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL )";
                         $str_order_ids = implode(',', $migrated_order_ids);
                         $condition .= " AND order_id IN ({$str_order_ids})";
                         //sales_invoice
                         $sales_invoices = Mage1SalesInvoice::model()->findAll($condition);
                         if ($sales_invoices) {
                             foreach ($sales_invoices as $sales_invoice) {
                                 $sales_invoice2 = new Mage2SalesInvoice();
                                 foreach ($sales_invoice2->attributes as $key => $value) {
                                     if (isset($sales_invoice->{$key})) {
                                         $sales_invoice2->{$key} = $sales_invoice->{$key};
                                     }
                                 }
                                 $sales_invoice2->store_id = MigrateSteps::getMage2StoreId($sales_invoice->store_id);
                                 if ($sales_invoice2->save()) {
                                     $migrated_invoice_ids[] = $sales_invoice->entity_id;
                                     //sales_invoice_grid
                                     $condition = "entity_id = {$sales_invoice->entity_id}";
                                     $models = Mage1SalesInvoiceGrid::model()->findAll($condition);
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesInvoiceGrid();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                             //this field was not exists in Magento1
                                             $model2->updated_at = null;
                                             $model2->save();
                                         }
                                     }
                                     //sales_invoice_item
                                     $condition = "parent_id = {$sales_invoice->entity_id}";
                                     $models = Mage1SalesInvoiceItem::model()->findAll($condition);
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesInvoiceItem();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             //this field was not exists in Magento1
                                             $model2->tax_ratio = null;
                                             $model2->save();
                                         }
                                     }
                                     //sales_invoice_comment
                                     $condition = "parent_id = {$sales_invoice->entity_id}";
                                     $models = Mage1SalesInvoiceComment::model()->findAll($condition);
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesInvoiceComment();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     //sales_invoiced_aggregated
                     $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                     $models = Mage1SalesInvoicedAggregated::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesInvoicedAggregated();
                             foreach ($model2->attributes as $key => $value) {
                                 if (isset($model->{$key})) {
                                     $model2->{$key} = $model->{$key};
                                 }
                             }
                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                             $model2->save();
                         }
                     }
                     //sales_invoiced_aggregated_order
                     $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                     $models = Mage1SalesInvoicedAggregatedOrder::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2SalesInvoicedAggregatedOrder();
                             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_sales_object_ids[] = 'invoice';
                 }
                 //end sales invoice migration
                 //Sales shipments migration
                 if (in_array('shipment', $selected_objects)) {
                     if ($migrated_order_ids) {
                         $condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL )";
                         $str_order_ids = implode(',', $migrated_order_ids);
                         $condition .= " AND order_id IN ({$str_order_ids})";
                         //sales_shipment
                         $sales_shipments = Mage1SalesShipment::model()->findAll($condition);
                         if ($sales_shipments) {
                             foreach ($sales_shipments as $sales_shipment) {
                                 $sales_shipment2 = new Mage2SalesShipment();
                                 foreach ($sales_shipment2->attributes as $key => $value) {
                                     if (isset($sales_shipment->{$key})) {
                                         $sales_shipment2->{$key} = $sales_shipment->{$key};
                                     }
                                 }
                                 $sales_shipment2->store_id = MigrateSteps::getMage2StoreId($sales_shipment->store_id);
                                 if ($sales_shipment2->save()) {
                                     $migrated_shipment_ids[] = $sales_shipment->entity_id;
                                     //sales_shipment_grid
                                     $models = Mage1SalesShipmentGrid::model()->findAll("entity_id = {$sales_shipment->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesShipmentGrid();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                             //this field was not exists in Magento 1
                                             $model2->updated_at = null;
                                             $model2->save();
                                         }
                                     }
                                     //sales_shipment_item
                                     $models = Mage1SalesShipmentItem::model()->findAll("parent_id = {$sales_shipment->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesShipmentItem();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                     //sales_shipment_track
                                     $models = Mage1SalesShipmentTrack::model()->findAll("parent_id = {$sales_shipment->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesShipmentTrack();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                     //sales_shipment_comment
                                     $models = Mage1SalesShipmentComment::model()->findAll("parent_id = {$sales_shipment->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesShipmentComment();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                             }
                         }
                         //sales_shipping_aggregated
                         $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                         $models = Mage1SalesShippingAggregated::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2SalesShippingAggregated();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                 $model2->save();
                             }
                         }
                         //sales_shipping_aggregated_order
                         $condition = "store_id IN ({$str_store_ids}) OR store_id IS NULL";
                         $models = Mage1SalesShippingAggregatedOrder::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2SalesShippingAggregatedOrder();
                                 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_sales_object_ids[] = 'shipment';
                 }
                 //end sales shipment migration
                 //Sales credit memo migration
                 if (in_array('credit', $selected_objects)) {
                     if ($migrated_order_ids) {
                         $condition = "( store_id IN ({$str_store_ids}) OR store_id IS NULL )";
                         $str_order_ids = implode(',', $migrated_order_ids);
                         $condition .= " AND order_id IN ({$str_order_ids})";
                         //sales_creditmemo
                         $sales_credits = Mage1SalesCreditmemo::model()->findAll($condition);
                         if ($sales_credits) {
                             foreach ($sales_credits as $sales_credit) {
                                 $sales_credit2 = new Mage2SalesCreditmemo();
                                 foreach ($sales_credit2->attributes as $key => $value) {
                                     if (isset($sales_credit->{$key})) {
                                         $sales_credit2->{$key} = $sales_credit->{$key};
                                     }
                                 }
                                 $sales_credit2->store_id = MigrateSteps::getMage2StoreId($sales_credit->store_id);
                                 if ($sales_credit2->save()) {
                                     //this for log
                                     $migrated_credit_ids[] = $sales_credit->store_id;
                                     //sales_creditmemo_grid
                                     $models = Mage1SalesCreditmemoGrid::model()->findAll("entity_id = {$sales_credit->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesCreditmemoGrid();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                             //this field was not exists in Magento 1
                                             $model2->updated_at = null;
                                             $model2->save();
                                         }
                                     }
                                     //sales_creditmemo_item
                                     $models = Mage1SalesCreditmemoItem::model()->findAll("parent_id = {$sales_credit->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesCreditmemoItem();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             //this field was not exists in Magento1
                                             $model2->tax_ratio = null;
                                             $model2->save();
                                         }
                                     }
                                     //sales_creditmemo_comment
                                     $models = Mage1SalesCreditmemoComment::model()->findAll("parent_id = {$sales_credit->entity_id}");
                                     if ($models) {
                                         foreach ($models as $model) {
                                             $model2 = new Mage2SalesCreditmemoComment();
                                             foreach ($model2->attributes as $key => $value) {
                                                 if (isset($model->{$key})) {
                                                     $model2->{$key} = $model->{$key};
                                                 }
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     $migrated_sales_object_ids[] = 'credit';
                 }
                 //End Sales credit memo migration
                 //sales bestsellers
                 if (in_array('bestseller', $selected_objects)) {
                     if ($migrated_store_ids && $migrated_product_ids) {
                         $condition = "store_id IN ({$str_store_ids}) AND product_id IN ({$str_product_ids})";
                         //sales_bestsellers_aggregated_daily
                         $models = Mage1SalesBestsellersDaily::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2SalesBestsellersDaily();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                 $model2->save();
                             }
                         }
                         //sales_bestsellers_aggregated_monthly
                         $models = Mage1SalesBestsellersMonthly::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2SalesBestsellersMonthly();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 $model2->store_id = MigrateSteps::getMage2StoreId($model->store_id);
                                 $model2->save();
                             }
                         }
                         //sales_bestsellers_aggregated_yearly
                         $models = Mage1SalesBestsellersYearly::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2SalesBestsellersYearly();
                                 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_sales_object_ids[] = 'bestseller';
                     }
                 }
                 //end sales bestsellers
                 //sales rules & coupons
                 if (in_array('rule_coupon', $selected_objects)) {
                     if ($migrated_store_ids && $migrated_product_ids) {
                         //salesrule
                         $models = Mage1Salesrule::model()->findAll();
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2Salesrule();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 //replace model names because it was changed in magento 2
                                 $model2->conditions_serialized = MigrateSteps::replaceSalesRuleModels($model2->conditions_serialized);
                                 $model2->actions_serialized = MigrateSteps::replaceSalesRuleModels($model2->actions_serialized);
                                 if ($model2->save()) {
                                     $migrated_sales_rule_ids[] = $model2->rule_id;
                                     //salesrule_coupon
                                     $coupons = Mage1SalesruleCoupon::model()->findAll("rule_id = {$model2->rule_id}");
                                     if ($coupons) {
                                         foreach ($coupons as $coupon) {
                                             $coupon2 = new Mage2SalesruleCoupon();
                                             foreach ($coupon2->attributes as $key => $value) {
                                                 if (isset($coupon->{$key})) {
                                                     $coupon2->{$key} = $coupon->{$key};
                                                 }
                                             }
                                             if ($coupon2->save()) {
                                                 $migrated_sales_coupon_ids[] = $coupon2->coupon_id;
                                                 //salesrule_coupon_usage
                                                 $coupon_usages = Mage1SalesruleCouponUsage::model()->findAll("coupon_id = {$coupon2->coupon_id}");
                                                 if ($coupon_usages) {
                                                     foreach ($coupon_usages as $coupon_usage) {
                                                         $coupon_usage2 = new Mage2SalesruleCouponUsage();
                                                         foreach ($coupon_usage2->attributes as $key => $value) {
                                                             if (isset($coupon_usage->{$key})) {
                                                                 $coupon_usage2->{$key} = $coupon_usage->{$key};
                                                             }
                                                         }
                                                         $coupon_usage2->save();
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                     //salesrule_label
                                     $labels = Mage1SalesruleLabel::model()->findAll("rule_id = {$model2->rule_id}");
                                     if ($labels) {
                                         foreach ($labels as $label) {
                                             $label2 = new Mage2SalesruleLabel();
                                             foreach ($label2->attributes as $key => $value) {
                                                 if (isset($label->{$key})) {
                                                     $label2->{$key} = $label->{$key};
                                                 }
                                             }
                                             $label2->store_id = MigrateSteps::getMage2StoreId($label2->store_id);
                                             $label2->save();
                                         }
                                     }
                                     //salesrule_product_attribute
                                     $condition = "rule_id = {$model2->rule_id}";
                                     if ($migrated_website_ids) {
                                         $condition .= " AND website_id IN ({$str_website_ids})";
                                     }
                                     $product_attributes = Mage1SalesruleProductAttribute::model()->findAll($condition);
                                     if ($product_attributes) {
                                         foreach ($product_attributes as $product_attribute) {
                                             $product_attribute2 = new Mage2SalesruleProductAttribute();
                                             foreach ($product_attribute2->attributes as $key => $value) {
                                                 if (isset($product_attribute->{$key})) {
                                                     $product_attribute2->{$key} = $product_attribute->{$key};
                                                 }
                                             }
                                             $product_attribute2->attribute_id = MigrateSteps::getMage2AttributeId($product_attribute2->attribute_id);
                                             $product_attribute2->save();
                                         }
                                     }
                                     //salesrule_website
                                     $r_websites = Mage1SalesruleWebsite::model()->findAll($condition);
                                     if ($r_websites) {
                                         foreach ($r_websites as $r_website) {
                                             $r_website2 = new Mage2SalesruleWebsite();
                                             foreach ($r_website2->attributes as $key => $value) {
                                                 if (isset($r_website->{$key})) {
                                                     $r_website2->{$key} = $r_website->{$key};
                                                 }
                                             }
                                             $r_website2->save();
                                         }
                                     }
                                     //salesrule_customer
                                     $r_customers = Mage1SalesruleCustomer::model()->findAll("rule_id = {$model2->rule_id}");
                                     if ($r_customers) {
                                         foreach ($r_customers as $r_customer) {
                                             $r_customer2 = new Mage2SalesruleCustomer();
                                             foreach ($r_customer2->attributes as $key => $value) {
                                                 if (isset($r_customer->{$key})) {
                                                     $r_customer2->{$key} = $r_customer->{$key};
                                                 }
                                             }
                                             $r_customer2->save();
                                         }
                                     }
                                     //salesrule_customer_group
                                     $r_customer_groups = Mage1SalesruleCustomerGroup::model()->findAll("rule_id = {$model2->rule_id}");
                                     if ($r_customer_groups) {
                                         foreach ($r_customer_groups as $r_customer_group) {
                                             $r_customer_group2 = new Mage2SalesruleCustomerGroup();
                                             foreach ($r_customer_group2->attributes as $key => $value) {
                                                 if (isset($r_customer_group->{$key})) {
                                                     $r_customer_group2->{$key} = $r_customer_group->{$key};
                                                 }
                                             }
                                             $r_customer_group->save();
                                         }
                                     }
                                 }
                             }
                         }
                         $migrated_sales_object_ids[] = 'rule_coupon';
                     }
                 }
                 //end sales rules & coupons
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have not selected any Object.'));
             }
             //Update step status
             if ($migrated_sales_object_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('sales_object_ids' => $migrated_sales_object_ids, 'sales_order_ids' => $migrated_order_ids, 'sales_quote_ids' => $migrated_quote_ids, 'sales_invoice_ids' => $migrated_invoice_ids, 'sales_shipment_ids' => $migrated_shipment_ids, 'sales_credit_ids' => $migrated_credit_ids));
                 if ($step->update()) {
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     //update session
                     Yii::app()->session['migrated_sales_object_ids'] = $migrated_sales_object_ids;
                     Yii::app()->session['migrated_sales_order_ids'] = $migrated_order_ids;
                     $message = '<ul>';
                     $message .= '<li>' . Yii::t('frontend', 'Migrated successfully.') . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Orders migrated: %s1.", array('%s1' => sizeof($migrated_order_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Orders Statuses migrated: %s2.", array('%s2' => sizeof($migrated_order_statuses))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Quote migrated: %s3.", array('%s3' => sizeof($migrated_quote_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Invoices migrated: %s4.", array('%s4' => sizeof($migrated_invoice_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Shipments migrated: %s5.", array('%s5' => sizeof($migrated_shipment_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Credit Memo migrated: %s6.", array('%s6' => sizeof($migrated_credit_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Rules migrated: %s7.", array('%s7' => sizeof($migrated_sales_rule_ids))) . '</li>';
                     $message .= '<li>' . Yii::t('frontend', "Total Sales Coupons migrated: %s8.", array('%s8' => sizeof($migrated_sales_coupon_ids))) . '</li>';
                     $message .= '</ul>';
                     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, 'sale_objects' => $sales_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']));
     }
 }