From ff980b56ac55adb83530db192d2c3e22fb5afdb8 Mon Sep 17 00:00:00 2001 From: dkorol Date: Thu, 4 Aug 2016 16:44:59 +0300 Subject: [PATCH] archive export --- README.md | 57 +------ README.ru.md | 57 +------ admin/controller/module/retailcrm.php | 38 +++++ admin/model/retailcrm/customer.php | 51 ++++++ admin/model/retailcrm/order.php | 235 ++++++++++---------------- system/cron/export.php | 3 + 6 files changed, 191 insertions(+), 250 deletions(-) create mode 100644 admin/model/retailcrm/customer.php create mode 100644 system/cron/export.php diff --git a/README.md b/README.md index 78b7f5d..efa3e3e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Opencart module =============== -Module allows integrate CMS Opencart with [retailCRM](http://retailcrm.pro) +Module allows integrate CMS Opencart 2.x with [retailCRM](http://retailcrm.pro) #### Features: @@ -23,56 +23,6 @@ cp -r opencart-module/* /path/to/site/root * Fill you api url & api key * Specify directories matching - -#### Export orders to retailCRM (for opencart 1.5.x, for version 2.x this step is unnecessary) - -##### VQmod - -Copy _retailcrm_create_order.xml_ into _/path/to/site/root/vqmod/xml_. - -For VQmod cache renewal you may need to delete files _/path/to/site/root/vqmod/vqcache/vq2-admin_model_sale_order.php_ & _/path/to/site/root/vqmod/vqcache/vq2-catalog_model_checkout_order.php_ - -##### Manual setup - -In the file: - -``` -/catalog/model/checkout/order.php -``` - -add theese lines into addOrder method right before return statement: - -```php -$this->load->model('retailcrm/order'); -$this->model_retailcrm_order->sendToCrm($data, $order_id); -``` - -In the file: - -``` -/admin/model/sale/order.php -``` - -add theese lines into addOrder & editOrder methods right before return statement: - -```php -if (!isset($data['fromApi'])) { - $this->load->model('setting/setting'); - $status = $this->model_setting_setting->getSetting('retailcrm'); - - if (!empty($data['order_status_id'])) { - $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; - } - - $this->load->model('retailcrm/order'); - if (isset ($order_query)) { - $this->model_retailcrm_order->changeInCrm($data, $order_id); - } else { - $this->model_retailcrm_order->sendToCrm($data, $order_id); - } -} -``` - #### Getting changes in orders Add to cron: @@ -94,3 +44,8 @@ Your export file will be available by following url ``` http://youropencartsite.com/retailcrm.xml ``` + +#### Export existing orders and customers + +You want to run this command onecly: +/usr/bin/php /path/to/opencart/system/cron/export.php diff --git a/README.ru.md b/README.ru.md index 44a8723..44bc3ef 100644 --- a/README.ru.md +++ b/README.ru.md @@ -1,7 +1,7 @@ Opencart module =============== -Модуль интеграции CMS Openacart c [RetailCRM](http://retailcrm.ru) +Модуль интеграции CMS Openacart 2.x c [RetailCRM](http://retailcrm.ru) #### Модуль позволяет: @@ -26,56 +26,6 @@ cp -r opencart-module/* /path/to/site/root На странице настроек модуля укажите URL Вашей CRM и ключ авторизации, после сохранения этих данных укажите соответствия справочников типов доставок, оплат и статусов заказа. - -#### Выгрузка новых заказов в CRM (для версии opencart 1.5.x.x, для версии 2.0 и старше не требуется) - -##### VQmod - -Скопируйте файл _retailcrm_create_order.xml_ в _/path/to/site/root/vqmod/xml_. - -Для обновления кеша VQmod может потрбоваться удалить файлы _/path/to/site/root/vqmod/vqcache/vq2-admin_model_sale_order.php_ и _/path/to/site/root/vqmod/vqcache/vq2-catalog_model_checkout_order.php_ - -##### Ручная установка - -В файле: - -``` -/catalog/model/checkout/order.php -``` - -Добавьте следующие строки в метод addOrder непосредственно перед языковой конструкцией return: - -```php -$this->load->model('retailcrm/order'); -$this->model_retailcrm_order->sendToCrm($data, $order_id); -``` - -В файле: - -``` -/admin/model/sale/order.php -``` - -Добавьте следующие строки в методы addOrder и editOrder в самом конце каждого метода: - -```php -if (!isset($data['fromApi'])) { - $this->load->model('setting/setting'); - $status = $this->model_setting_setting->getSetting('retailcrm'); - - if (!empty($data['order_status_id'])) { - $data['order_status'] = $status['retailcrm_status'][$data['order_status_id']]; - } - - $this->load->model('retailcrm/order'); - if (isset ($order_query)) { - $this->model_retailcrm_order->changeInCrm($data, $order_id); - } else { - $this->model_retailcrm_order->sendToCrm($data, $order_id); - } -} -``` - #### Получение измений из CRM Для получения изменений и новых данных добавьте в cron следующую запись: @@ -97,3 +47,8 @@ if (!isset($data['fromApi'])) { ``` http://youropencartsite.com/retailcrm.xml ``` + +#### Выгрузка существующих заказов и покупателей + +Запустите команду единожды: +/usr/bin/php /path/to/opencart/system/cron/export.php diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 3df6c4b..5d3aba9 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -276,6 +276,44 @@ class ControllerModuleRetailcrm extends Controller } } + /** + * Export orders + * + * + */ + public function export() { + if(version_compare(VERSION, '2.1', '<')) { + $this->load->model('sale/customer'); + $customers = $this->model_sale_customer->getCustomers(); + } else { + $this->load->model('customer/customer'); + $customers = $this->model_customer_customer->getCustomers(); + } + + $this->load->model('retailcrm/customer'); + $this->model_retailcrm_customer->uploadToCrm($customers); + + $this->load->model('sale/order'); + $orders = $this->model_sale_order->getOrders(); + + $fullOrders = array(); + foreach($orders as $order) { + $fullOrder = $this->model_sale_order->getOrder($order['order_id']); + + $fullOrder['order_total'] = $this->model_sale_order->getOrderTotals($order['order_id']); + + $fullOrder['products'] = $this->model_sale_order->getOrderProducts($order['order_id']); + foreach($fullOrder['products'] as $key=>$product) { + $fullOrder['products'][$key]['option'] = $this->model_sale_order->getOrderOptions($product['order_id'], $product['order_product_id']); + } + + $fullOrders[] = $fullOrder; + } + + $this->load->model('retailcrm/order'); + $this->model_retailcrm_order->uploadToCrm($fullOrders); + } + /** * Validate * diff --git a/admin/model/retailcrm/customer.php b/admin/model/retailcrm/customer.php new file mode 100644 index 0000000..a18c6c8 --- /dev/null +++ b/admin/model/retailcrm/customer.php @@ -0,0 +1,51 @@ +load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(empty($customers)) + return false; + if(empty($settings['retailcrm_url']) || empty($settings['retailcrm_apikey'])) + return false; + + require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + + $this->retailcrmApi = new RetailcrmProxy( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'logs/retailcrm.log' + ); + + $customersToCrm = array(); + + foreach($customers as $customer) { + $customersToCrm[] = $this->process($customer); + } + + $chunkedCustomers = array_chunk($customersToCrm, 50); + + foreach($chunkedCustomers as $customersPart) { + $this->retailcrmApi->customersUpload($customersPart); + } + } + + private function process($customer) { + $customerToCrm = array( + 'externalId' => $customer['customer_id'], + 'firstName' => $customer['firstname'], + 'lastName' => $customer['lastname'], + 'email' => $customer['email'], + 'phones' => array( + array( + 'number' => $customer['telephone'] + ) + ), + 'createdAt' => $customer['date_added'] + ); + + return $customerToCrm; + } +} diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 8f40478..b0c169c 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -1,174 +1,113 @@ load->model('catalog/product'); - public function sendToCrm($order_data, $order_id) - { $this->load->model('setting/setting'); - $settings = $this->model_setting_setting->getSetting('retailcrm'); + $this->settings = $this->model_setting_setting->getSetting('retailcrm'); - if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + $ordersToCrm = array(); - $this->retailcrm = new RetailcrmProxy( - $settings['retailcrm_url'], - $settings['retailcrm_apikey'], - DIR_SYSTEM . 'logs/retailcrm.log' - ); + foreach($orders as $order) { + $ordersToCrm[] = $this->process($order); + } - $order = array(); + $chunkedOrders = array_chunk($ordersToCrm, 50); - $customers = $this->retailcrm->customersList( - array( - 'name' => $order_data['telephone'], - 'email' => $order_data['email'] - ), - 1, - 100 - ); + foreach($chunkedOrders as $ordersPart) { + $this->retailcrmApi->ordersUpload($ordersPart); + } + } - foreach($customers['customers'] as $customer) { - $order['customer']['id'] = $customer['id']; + private function process($order_data) { + $order = array(); + + $payment_code = $order_data['payment_code']; + $delivery_code = $order_data['shipping_code']; + + $order['externalId'] = $order_data['order_id']; + $order['firstName'] = $order_data['firstname']; + $order['lastName'] = $order_data['lastname']; + $order['email'] = $order_data['email']; + $order['phone'] = $order_data['telephone']; + $order['customerComment'] = $order_data['comment']; + + $order['customer']['externalId'] = $order_data['customer_id']; + + $deliveryCost = 0; + $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; + + foreach ($orderTotals as $totals) { + if ($totals['code'] == 'shipping') { + $deliveryCost = $totals['value']; } + } - unset($customers); + $order['createdAt'] = $order_data['date_added']; + $order['paymentType'] = $this->settings['retailcrm_payment'][$payment_code]; - $order['externalId'] = $order_id; - $order['firstName'] = $order_data['firstname']; - $order['lastName'] = $order_data['lastname']; - $order['email'] = $order_data['email']; - $order['phone'] = $order_data['telephone']; - $order['customerComment'] = $order_data['comment']; + $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - $deliveryCost = 0; - $altTotals = isset($order_data['order_total']) ? $order_data['order_total'] : ""; - $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $altTotals ; + $order['delivery'] = array( + 'code' => !empty($delivery_code) ? $this->settings['retailcrm_delivery'][$delivery_code] : '', + 'cost' => $deliveryCost, + 'address' => array( + 'index' => $order_data['shipping_postcode'], + 'city' => $order_data['shipping_city'], + 'country' => $order_data['shipping_country_id'], + 'region' => $order_data['shipping_zone_id'], + 'text' => implode(', ', array( + $order_data['shipping_postcode'], + $country, + $order_data['shipping_city'], + $order_data['shipping_address_1'], + $order_data['shipping_address_2'] + )) + ) + ); - if (!empty($orderTotals)) { - foreach ($orderTotals as $totals) { - if ($totals['code'] == 'shipping') { - $deliveryCost = $totals['value']; + $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); + + foreach ($orderProducts as $product) { + $offerId = ''; + + if(!empty($product['option'])) { + $options = array(); + + $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); + + foreach($product['option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + foreach($productOptions as $productOption) { + if($productOption['product_option_id'] = $option['product_option_id']) { + foreach($productOption['product_option_value'] as $productOptionValue) { + if($productOptionValue['product_option_value_id'] == $option['product_option_value_id']) { + $options[$option['product_option_id']] = $productOptionValue['option_value_id']; + } + } + } } } - } - $order['createdAt'] = date('Y-m-d H:i:s'); + ksort($options); - $payment_code = $order_data['payment_code']; - $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; - - $delivery_code = $order_data['shipping_code']; - $order['delivery'] = array( - 'code' => $settings['retailcrm_delivery'][$delivery_code], - 'cost' => $deliveryCost, - 'address' => array( - 'index' => $order_data['shipping_postcode'], - 'city' => $order_data['shipping_city'], - 'countryIso' => $order_data['shipping_iso_code_2'], - 'region' => $order_data['shipping_zone'], - 'text' => implode(', ', array( - $order_data['shipping_postcode'], - (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '', - $order_data['shipping_city'], - $order_data['shipping_address_1'], - $order_data['shipping_address_2'] - )) - ) - ); - - $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; - - foreach ($orderProducts as $product) { - $order['items'][] = array( - 'productId' => $product['product_id'], - 'productName' => $product['name'], - 'initialPrice' => $product['price'], - 'quantity' => $product['quantity'], - ); - } - - if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { - $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; - } - - $this->retailcrm->ordersCreate($order); - } - } - - public function changeInCrm($order_data, $order_id) - { - $this->load->model('setting/setting'); - $settings = $this->model_setting_setting->getSetting('retailcrm'); - - if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; - - $this->retailcrm = new RetailcrmProxy( - $settings['retailcrm_url'], - $settings['retailcrm_apikey'], - DIR_SYSTEM . 'logs/retailcrm.log' - ); - - $order = array(); - - $payment_code = $order_data['payment_code']; - $delivery_code = $order_data['shipping_code']; - - $order['externalId'] = $order_id; - $order['firstName'] = $order_data['firstname']; - $order['lastName'] = $order_data['lastname']; - $order['email'] = $order_data['email']; - $order['phone'] = $order_data['telephone']; - $order['customerComment'] = $order_data['comment']; - - $deliveryCost = 0; - $orderTotals = isset($order_data['totals']) ? $order_data['totals'] : $order_data['order_total'] ; - - foreach ($orderTotals as $totals) { - if ($totals['code'] == 'shipping') { - $deliveryCost = $totals['value']; + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; } + $offerId = implode('_', $offerId); } - $order['createdAt'] = date('Y-m-d H:i:s'); - $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; - - $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - - $order['delivery'] = array( - 'code' => $settings['retailcrm_delivery'][$delivery_code], - 'cost' => $deliveryCost, - 'address' => array( - 'index' => $order_data['shipping_postcode'], - 'city' => $order_data['shipping_city'], - 'country' => $order_data['shipping_country_id'], - 'region' => $order_data['shipping_zone_id'], - 'text' => implode(', ', array( - $order_data['shipping_postcode'], - $country, - $order_data['shipping_city'], - $order_data['shipping_address_1'], - $order_data['shipping_address_2'] - )) - ) + $order['items'][] = array( + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], + 'productName' => $product['name'], + 'initialPrice' => $product['price'], + 'quantity' => $product['quantity'], ); - - $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; - - foreach ($orderProducts as $product) { - $order['items'][] = array( - 'productId' => $product['product_id'], - 'productName' => $product['name'], - 'initialPrice' => $product['price'], - 'quantity' => $product['quantity'], - ); - } - - if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { - $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; - } - - $this->retailcrm->ordersEdit($order); } + + return $order; } } diff --git a/system/cron/export.php b/system/cron/export.php new file mode 100644 index 0000000..8da82f4 --- /dev/null +++ b/system/cron/export.php @@ -0,0 +1,3 @@ +