Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: snark | itfrogs.ru
- * Date: 2/25/17
- * Time: 6:29 PM
- */
- date_default_timezone_set('Europe/Moscow');
- class shopCscartordersCli extends waCliController
- {
- /**
- * @var shopIconvertPlugin $plugin
- */
- private static $plugin;
- private static function getPlugin()
- {
- if (!isset(self::$plugin)) {
- self::$plugin = wa()->getPlugin('iconvert');
- }
- return self::$plugin;
- }
- /**
- * @var shopConfig $config
- */
- protected $config;
- public function execute()
- {
- $this->config = wa('shop')->getConfig();
- $plugin = self::getPlugin();
- $settings = $plugin->getSettings();
- $old_model = new waModel('cscart');
- $old_orders_count = $old_model->query('SELECT COUNT(order_id) FROM cscart_orders')->fetchField();
- $order_model = new shopOrderModel();
- $order_params_model = new shopOrderParamsModel();
- $order_items_model = new shopOrderItemsModel();
- $shop_sales_model = new shopSalesModel();
- $icm = new shopIconvertPluginContactsModel();
- $iom = new shopIconvertPluginOrdersModel();
- $ipm = new shopIconvertPluginProductsModel();
- $customer_model = new shopCustomerModel();
- $log_model = new shopOrderLogModel();
- $plugin_model = new shopPluginModel();
- $shipping_correct_array = array(
- 6 => 11,
- 11 => 5,
- 16 => 10,
- 20 => 1,
- 21 => 7,
- 22 => 8,
- 23 => 6,
- 24 => 12,
- 26 => 13,
- 27 => 9,
- 28 => 4,
- 30 => 3,
- );
- $payment_correct_array = array(
- 2 => 23,
- 3 => 18,
- 18 => 22,
- 27 => 2,
- 28 => 20,
- 29 => 17,
- 33 => 21,
- 35 => 16,
- 36 => 14,
- 37 => 15,
- 38 => 19,
- );
- $managers = array(
- 10258,
- 1,
- 3096,
- 9834,
- );
- $statuses = array(
- 'O' => 'new',
- 'N' => 'unfinished',
- 'C' => 'completed',
- 'F' => 'processing',
- 'P' => 'paid',
- 'D' => 'shipped',
- 'B' => 'payment-hold',
- 'I' => 'deleted',
- 'Y' => 'collection',
- 'A' => 'on-hold',
- 'E' => 'return',
- 'X' => 'pickup',
- 'W' => 'returning',
- 'G' => 'postmail',
- 'H' => 'lost',
- );
- $offset = 0;
- do {
- $old_orders = $old_model->query('SELECT o.* '
- .'FROM cscart_orders o '
- .'LIMIT ' . $offset. ', 100 ')->fetchAll();
- $offset += 100;
- foreach ($old_orders as $old_order) {
- $old_user_id = $old_order['user_id'];
- if (in_array($old_user_id, $managers)) {
- $old_user_id = 0;
- }
- if (is_numeric($old_user_id) && $old_user_id == 0) {
- $old_user = $old_model->query(
- 'SELECT * FROM cscart_users WHERE email = s:email',
- array(
- 'email' => $old_order['email'],
- )
- )->fetchAssoc();
- if (empty($old_user)) {
- $old_user = $old_model->query(
- 'SELECT * FROM cscart_users WHERE phone = s:phone ',
- array(
- 'phone' => $old_order['phone'],
- )
- )->fetchAssoc();
- }
- if (empty($old_user)) {
- $old_user = $old_model->query(
- 'SELECT * FROM cscart_users WHERE firstname = s:firstname AND lastname = s:lastname ',
- array(
- 'firstname' => $old_order['s_firstname'],
- 'lastname' => $old_order['s_lastname'],
- )
- )->fetchAssoc();
- }
- if (!empty($old_user)) {
- $old_user_id = $old_user['user_id'];
- }
- else {
- $old_user_id = null;
- }
- }
- if ($old_user_id == null) {
- $password = rand(100000, 999999);
- $contact = new waContact();
- $contact->setPassword($password, false);
- $contact->set('firstname', $old_order['firstname']);
- if (empty($old_order['firstname']) && isset($old_order['s_firstname'])) {
- $contact->set('firstname', $old_order['s_firstname']);
- }
- $contact->set('lastname', $old_order['lastname']);
- if (empty($old_order['lastname']) && isset($old_order['s_lastname'])) {
- $contact->set('lastname', $old_order['s_lastname']);
- }
- $contact->set('email', $old_order['email']);
- $contact->set('login', 'user-' . $old_order['order_id']);
- $contact->set('phone', $old_order['phone']);
- $contact->set('address:city.shipping', $old_order['s_city'], true);
- $contact->set('address:street.shipping', $old_order['s_address'], true);
- $contact->set('address:zip.shipping', $old_order['s_zipcode'], true);
- $contact->set('address:phone.shipping', $old_order['s_phone'], true);
- $contact->set('address:country.shipping', 'rus', true);
- $contact->set('address:city.billing', $old_order['b_city'], true);
- $contact->set('address:street.billing', $old_order['b_address'], true);
- $contact->set('address:zip.billing', $old_order['b_zipcode'], true);
- $contact->set('address:phone.billing', $old_order['b_phone'], true);
- $contact->set('address:country.billing', 'rus', true);
- $contact->save();
- $user_link = array(
- 'old_order_id' => $old_order['order_id'],
- 'contact_id' => $contact->getId(),
- 'password' => $password,
- );
- $icm->insert($user_link, 1);
- }
- else {
- $user_link = $icm->getByField('old_contact_id', $old_user_id);
- }
- if (!empty($user_link)) {
- if (!empty($old_user)) {
- $contact = new waContact($user_link['contact_id']);
- $contact->set('email', $old_order['email']);
- $contact->set('phone', $old_order['phone']);
- $contact->save();
- }
- $old_items = $old_model->query('SELECT * FROM cscart_order_details WHERE order_id = i:order_id', array('order_id' => $old_order['order_id']))->fetchAll();
- if (empty($old_items)) {
- continue;
- }
- if ($iom->getByField('old_order_id', $old_order['order_id'])) {
- continue;
- }
- $order = array(
- 'id' => $old_order['order_id'],
- 'contact_id' => $user_link['contact_id'],
- 'create_datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
- 'update_datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
- 'state_id' => $statuses[$old_order['status']],
- 'total' => $old_order['total'],
- 'discount' => $old_order['discount'] + $old_order['subtotal_discount'],
- 'currency' => 'RUB',
- 'shipping' => $old_order['shipping_cost'],
- 'assigned_contact_id' => $user_link['contact_id'],
- 'is_first' => 0,
- 'comment' => $old_order['notes'],
- 'paid_year' => date('Y', $old_order['timestamp']),
- 'paid_quarter' => floor((date('n', $old_order['timestamp']) - 1) / 3) + 1,
- 'paid_month' => date('n', $old_order['timestamp']),
- 'paid_date' => date('Y-m-d', $old_order['timestamp']),
- );
- if ($order['state_id'] == 'paid') {
- $order['paid_date'] = date('Y-m-d H:i:s', $old_order['timestamp']);
- }
- $order['id'] = $order_model->insert($order);
- $order_params = array(
- 'shipping_address.street' => $old_order['s_address'],
- 'shipping_address.city' => $old_order['s_city'],
- 'shipping_address.zip' => $old_order['s_zipcode'],
- 'shipping_address.phone' => $old_order['s_phone'],
- );
- if (!empty($old_order['shipping_ids']) && is_numeric($old_order['shipping_ids']) && isset($shipping_correct_array[$old_order['shipping_ids']])) {
- $order_params['shipping_id'] = $shipping_correct_array[$old_order['shipping_ids']];
- if($shipping = $plugin_model->getById($order_params['shipping_id'])) {
- $order_params['shipping_rate_id'] = 0;
- $order_params['shipping_plugin'] = $shipping['plugin'];
- $order_params['shipping_name'] = $shipping['name'];
- $order_params['shipping_description'] = $shipping['description'];
- }
- }
- if (!empty($old_order['payment_id']) &&is_numeric($old_order['payment_id']) && isset($payment_correct_array[$old_order['payment_id']])) {
- $order_params['payment_id'] = $payment_correct_array[$old_order['payment_id']];
- if($payment = $plugin_model->getById($order_params['payment_id'])) {
- $order_params['payment_name'] = $payment['name'];
- $order_params['payment_plugin'] = $payment['plugin'];
- $order_params['payment_description'] = $payment['description'];
- }
- }
- $order_params_model->set($order['id'], $order_params);
- $customer = $customer_model->getById($user_link['contact_id']);
- if (!empty($customer)) {
- $customer['last_order_id'] = $order['id'];
- $customer['total_spent'] += $order['total'];
- $customer['number_of_orders']++;
- $customer_model->updateById($customer['contact_id'], $customer);
- }
- else{
- $bonus = 0;
- if (isset($user_link['old_contact_id']) && is_numeric($user_link['old_contact_id'])) {
- $rewards = $old_model->query(
- 'SELECT * FROM cscart_reward_point_changes WHERE user_id = i:old_contact_id ',
- array(
- 'old_contact_id' => $user_link['old_contact_id'],
- )
- )->fetchAll();
- foreach ($rewards as $reward) {
- $bonus = $bonus + $reward['amount'];
- }
- }
- $customer = array(
- 'contact_id' => $user_link['contact_id'],
- 'total_spent' => $order['total'],
- 'affiliate_bonus' => $bonus,
- 'number_of_orders' => 1,
- 'last_order_id' => $order['id'],
- );
- $customer_model->createFromContact($user_link['contact_id'], $customer);
- }
- $data = array(
- 'order_id' => $order['id'],
- 'old_order_id' => $old_order['order_id'],
- );
- $iom->insert($data);
- var_dump($old_order['order_id']);
- foreach ($old_items as $old_item) {
- $product_link = $ipm->getByField('old_product_id', $old_item['product_id']);
- $product = new shopProduct($product_link['product_id']);
- if (!empty($product_link)) {
- $item = array(
- 'order_id' => $order['id'],
- 'name' => $product['name'],
- 'product_id' => $product_link['product_id'],
- 'sku_id' => $product->sku_id,
- 'sku_code' => '',
- 'stock_id' => $settings['stock_id'],
- 'type' => 'product',
- 'price' => $old_item['price'],
- 'quantity' => $old_item['amount'],
- //'purchase_price' => $old_item['price_buy'],
- );
- $order_items_model->insert($item);
- }
- }
- if (!empty($old_order['details'])) {
- $log = array(
- 'order_id' => $order['id'],
- 'contact_id' => $user_link['contact_id'],
- 'action_id' => '',
- 'datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
- 'before_state_id' => $statuses[$old_order['status']],
- 'after_state_id' => $statuses[$old_order['status']],
- 'text' => $old_order['details'],
- );
- $log_model->insert($log);
- }
- /*
- * Делаем запись о продаже
- */
- $sale = array(
- 'hash' => 'Заказ #' . $order['id'],
- 'date' => date('Y-m-d', strtotime($order['create_datetime'])),
- 'order_count' => 1,
- 'sales' => $order['total'],
- 'shipping' => $order['shipping'],
- );
- $shop_sales_model->insert($sale);
- }
- else {
- //print ':' . print_r($old_user_id, true) . ':';
- }
- }
- } while ($offset < $old_orders_count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement