Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: snark | itfrogs.ru
  5. * Date: 2/25/17
  6. * Time: 6:29 PM
  7. */
  8. date_default_timezone_set('Europe/Moscow');
  9. class shopCscartordersCli extends waCliController
  10. {
  11. /**
  12. * @var shopIconvertPlugin $plugin
  13. */
  14. private static $plugin;
  15. private static function getPlugin()
  16. {
  17. if (!isset(self::$plugin)) {
  18. self::$plugin = wa()->getPlugin('iconvert');
  19. }
  20. return self::$plugin;
  21. }
  22.  
  23. /**
  24. * @var shopConfig $config
  25. */
  26. protected $config;
  27.  
  28.  
  29. public function execute()
  30. {
  31. $this->config = wa('shop')->getConfig();
  32. $plugin = self::getPlugin();
  33. $settings = $plugin->getSettings();
  34.  
  35. $old_model = new waModel('cscart');
  36. $old_orders_count = $old_model->query('SELECT COUNT(order_id) FROM cscart_orders')->fetchField();
  37.  
  38. $order_model = new shopOrderModel();
  39. $order_params_model = new shopOrderParamsModel();
  40. $order_items_model = new shopOrderItemsModel();
  41. $shop_sales_model = new shopSalesModel();
  42. $icm = new shopIconvertPluginContactsModel();
  43. $iom = new shopIconvertPluginOrdersModel();
  44. $ipm = new shopIconvertPluginProductsModel();
  45. $customer_model = new shopCustomerModel();
  46. $log_model = new shopOrderLogModel();
  47. $plugin_model = new shopPluginModel();
  48.  
  49. $shipping_correct_array = array(
  50. 6 => 11,
  51. 11 => 5,
  52. 16 => 10,
  53. 20 => 1,
  54. 21 => 7,
  55. 22 => 8,
  56. 23 => 6,
  57. 24 => 12,
  58. 26 => 13,
  59. 27 => 9,
  60. 28 => 4,
  61. 30 => 3,
  62. );
  63.  
  64. $payment_correct_array = array(
  65. 2 => 23,
  66. 3 => 18,
  67. 18 => 22,
  68. 27 => 2,
  69. 28 => 20,
  70. 29 => 17,
  71. 33 => 21,
  72. 35 => 16,
  73. 36 => 14,
  74. 37 => 15,
  75. 38 => 19,
  76. );
  77.  
  78.  
  79. $managers = array(
  80. 10258,
  81. 1,
  82. 3096,
  83. 9834,
  84. );
  85.  
  86. $statuses = array(
  87. 'O' => 'new',
  88. 'N' => 'unfinished',
  89. 'C' => 'completed',
  90. 'F' => 'processing',
  91. 'P' => 'paid',
  92. 'D' => 'shipped',
  93. 'B' => 'payment-hold',
  94. 'I' => 'deleted',
  95. 'Y' => 'collection',
  96. 'A' => 'on-hold',
  97. 'E' => 'return',
  98. 'X' => 'pickup',
  99. 'W' => 'returning',
  100. 'G' => 'postmail',
  101. 'H' => 'lost',
  102. );
  103.  
  104. $offset = 0;
  105. do {
  106. $old_orders = $old_model->query('SELECT o.* '
  107. .'FROM cscart_orders o '
  108. .'LIMIT ' . $offset. ', 100 ')->fetchAll();
  109. $offset += 100;
  110.  
  111. foreach ($old_orders as $old_order) {
  112. $old_user_id = $old_order['user_id'];
  113. if (in_array($old_user_id, $managers)) {
  114. $old_user_id = 0;
  115. }
  116.  
  117. if (is_numeric($old_user_id) && $old_user_id == 0) {
  118. $old_user = $old_model->query(
  119. 'SELECT * FROM cscart_users WHERE email = s:email',
  120. array(
  121. 'email' => $old_order['email'],
  122. )
  123. )->fetchAssoc();
  124.  
  125. if (empty($old_user)) {
  126. $old_user = $old_model->query(
  127. 'SELECT * FROM cscart_users WHERE phone = s:phone ',
  128. array(
  129. 'phone' => $old_order['phone'],
  130. )
  131. )->fetchAssoc();
  132. }
  133.  
  134. if (empty($old_user)) {
  135. $old_user = $old_model->query(
  136. 'SELECT * FROM cscart_users WHERE firstname = s:firstname AND lastname = s:lastname ',
  137. array(
  138. 'firstname' => $old_order['s_firstname'],
  139. 'lastname' => $old_order['s_lastname'],
  140. )
  141. )->fetchAssoc();
  142. }
  143.  
  144. if (!empty($old_user)) {
  145. $old_user_id = $old_user['user_id'];
  146. }
  147. else {
  148. $old_user_id = null;
  149. }
  150. }
  151.  
  152. if ($old_user_id == null) {
  153. $password = rand(100000, 999999);
  154. $contact = new waContact();
  155. $contact->setPassword($password, false);
  156.  
  157. $contact->set('firstname', $old_order['firstname']);
  158. if (empty($old_order['firstname']) && isset($old_order['s_firstname'])) {
  159. $contact->set('firstname', $old_order['s_firstname']);
  160. }
  161.  
  162. $contact->set('lastname', $old_order['lastname']);
  163. if (empty($old_order['lastname']) && isset($old_order['s_lastname'])) {
  164. $contact->set('lastname', $old_order['s_lastname']);
  165. }
  166.  
  167. $contact->set('email', $old_order['email']);
  168. $contact->set('login', 'user-' . $old_order['order_id']);
  169.  
  170. $contact->set('phone', $old_order['phone']);
  171.  
  172. $contact->set('address:city.shipping', $old_order['s_city'], true);
  173. $contact->set('address:street.shipping', $old_order['s_address'], true);
  174. $contact->set('address:zip.shipping', $old_order['s_zipcode'], true);
  175. $contact->set('address:phone.shipping', $old_order['s_phone'], true);
  176. $contact->set('address:country.shipping', 'rus', true);
  177.  
  178. $contact->set('address:city.billing', $old_order['b_city'], true);
  179. $contact->set('address:street.billing', $old_order['b_address'], true);
  180. $contact->set('address:zip.billing', $old_order['b_zipcode'], true);
  181. $contact->set('address:phone.billing', $old_order['b_phone'], true);
  182. $contact->set('address:country.billing', 'rus', true);
  183.  
  184. $contact->save();
  185.  
  186. $user_link = array(
  187. 'old_order_id' => $old_order['order_id'],
  188. 'contact_id' => $contact->getId(),
  189. 'password' => $password,
  190. );
  191.  
  192. $icm->insert($user_link, 1);
  193. }
  194. else {
  195. $user_link = $icm->getByField('old_contact_id', $old_user_id);
  196. }
  197.  
  198. if (!empty($user_link)) {
  199. if (!empty($old_user)) {
  200. $contact = new waContact($user_link['contact_id']);
  201. $contact->set('email', $old_order['email']);
  202. $contact->set('phone', $old_order['phone']);
  203. $contact->save();
  204. }
  205.  
  206. $old_items = $old_model->query('SELECT * FROM cscart_order_details WHERE order_id = i:order_id', array('order_id' => $old_order['order_id']))->fetchAll();
  207. if (empty($old_items)) {
  208. continue;
  209. }
  210. if ($iom->getByField('old_order_id', $old_order['order_id'])) {
  211. continue;
  212. }
  213.  
  214. $order = array(
  215. 'id' => $old_order['order_id'],
  216. 'contact_id' => $user_link['contact_id'],
  217. 'create_datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
  218. 'update_datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
  219. 'state_id' => $statuses[$old_order['status']],
  220. 'total' => $old_order['total'],
  221. 'discount' => $old_order['discount'] + $old_order['subtotal_discount'],
  222. 'currency' => 'RUB',
  223. 'shipping' => $old_order['shipping_cost'],
  224. 'assigned_contact_id' => $user_link['contact_id'],
  225. 'is_first' => 0,
  226. 'comment' => $old_order['notes'],
  227. 'paid_year' => date('Y', $old_order['timestamp']),
  228. 'paid_quarter' => floor((date('n', $old_order['timestamp']) - 1) / 3) + 1,
  229. 'paid_month' => date('n', $old_order['timestamp']),
  230. 'paid_date' => date('Y-m-d', $old_order['timestamp']),
  231. );
  232.  
  233. if ($order['state_id'] == 'paid') {
  234. $order['paid_date'] = date('Y-m-d H:i:s', $old_order['timestamp']);
  235. }
  236. $order['id'] = $order_model->insert($order);
  237.  
  238. $order_params = array(
  239. 'shipping_address.street' => $old_order['s_address'],
  240. 'shipping_address.city' => $old_order['s_city'],
  241. 'shipping_address.zip' => $old_order['s_zipcode'],
  242. 'shipping_address.phone' => $old_order['s_phone'],
  243. );
  244.  
  245. if (!empty($old_order['shipping_ids']) && is_numeric($old_order['shipping_ids']) && isset($shipping_correct_array[$old_order['shipping_ids']])) {
  246. $order_params['shipping_id'] = $shipping_correct_array[$old_order['shipping_ids']];
  247. if($shipping = $plugin_model->getById($order_params['shipping_id'])) {
  248. $order_params['shipping_rate_id'] = 0;
  249. $order_params['shipping_plugin'] = $shipping['plugin'];
  250. $order_params['shipping_name'] = $shipping['name'];
  251. $order_params['shipping_description'] = $shipping['description'];
  252. }
  253. }
  254.  
  255. if (!empty($old_order['payment_id']) &&is_numeric($old_order['payment_id']) && isset($payment_correct_array[$old_order['payment_id']])) {
  256. $order_params['payment_id'] = $payment_correct_array[$old_order['payment_id']];
  257. if($payment = $plugin_model->getById($order_params['payment_id'])) {
  258. $order_params['payment_name'] = $payment['name'];
  259. $order_params['payment_plugin'] = $payment['plugin'];
  260. $order_params['payment_description'] = $payment['description'];
  261. }
  262. }
  263.  
  264. $order_params_model->set($order['id'], $order_params);
  265.  
  266. $customer = $customer_model->getById($user_link['contact_id']);
  267. if (!empty($customer)) {
  268. $customer['last_order_id'] = $order['id'];
  269. $customer['total_spent'] += $order['total'];
  270. $customer['number_of_orders']++;
  271. $customer_model->updateById($customer['contact_id'], $customer);
  272. }
  273. else{
  274. $bonus = 0;
  275. if (isset($user_link['old_contact_id']) && is_numeric($user_link['old_contact_id'])) {
  276. $rewards = $old_model->query(
  277. 'SELECT * FROM cscart_reward_point_changes WHERE user_id = i:old_contact_id ',
  278. array(
  279. 'old_contact_id' => $user_link['old_contact_id'],
  280. )
  281. )->fetchAll();
  282.  
  283. foreach ($rewards as $reward) {
  284. $bonus = $bonus + $reward['amount'];
  285. }
  286. }
  287.  
  288. $customer = array(
  289. 'contact_id' => $user_link['contact_id'],
  290. 'total_spent' => $order['total'],
  291. 'affiliate_bonus' => $bonus,
  292. 'number_of_orders' => 1,
  293. 'last_order_id' => $order['id'],
  294. );
  295. $customer_model->createFromContact($user_link['contact_id'], $customer);
  296. }
  297.  
  298. $data = array(
  299. 'order_id' => $order['id'],
  300. 'old_order_id' => $old_order['order_id'],
  301. );
  302. $iom->insert($data);
  303. var_dump($old_order['order_id']);
  304.  
  305. foreach ($old_items as $old_item) {
  306. $product_link = $ipm->getByField('old_product_id', $old_item['product_id']);
  307. $product = new shopProduct($product_link['product_id']);
  308. if (!empty($product_link)) {
  309. $item = array(
  310. 'order_id' => $order['id'],
  311. 'name' => $product['name'],
  312. 'product_id' => $product_link['product_id'],
  313. 'sku_id' => $product->sku_id,
  314. 'sku_code' => '',
  315. 'stock_id' => $settings['stock_id'],
  316. 'type' => 'product',
  317. 'price' => $old_item['price'],
  318. 'quantity' => $old_item['amount'],
  319. //'purchase_price' => $old_item['price_buy'],
  320. );
  321. $order_items_model->insert($item);
  322. }
  323. }
  324.  
  325. if (!empty($old_order['details'])) {
  326. $log = array(
  327. 'order_id' => $order['id'],
  328. 'contact_id' => $user_link['contact_id'],
  329. 'action_id' => '',
  330. 'datetime' => date('Y-m-d H:i:s', $old_order['timestamp']),
  331. 'before_state_id' => $statuses[$old_order['status']],
  332. 'after_state_id' => $statuses[$old_order['status']],
  333. 'text' => $old_order['details'],
  334. );
  335. $log_model->insert($log);
  336. }
  337.  
  338. /*
  339. * Делаем запись о продаже
  340. */
  341.  
  342. $sale = array(
  343. 'hash' => 'Заказ #' . $order['id'],
  344. 'date' => date('Y-m-d', strtotime($order['create_datetime'])),
  345. 'order_count' => 1,
  346. 'sales' => $order['total'],
  347. 'shipping' => $order['shipping'],
  348. );
  349. $shop_sales_model->insert($sale);
  350.  
  351. }
  352. else {
  353. //print ':' . print_r($old_user_id, true) . ':';
  354. }
  355. }
  356. } while ($offset < $old_orders_count);
  357. }
  358.  
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement