archive export

This commit is contained in:
dkorol 2016-08-04 16:44:59 +03:00
parent 82e3a4334b
commit ff980b56ac
6 changed files with 191 additions and 250 deletions

View file

@ -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

View file

@ -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

View file

@ -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
*

View file

@ -0,0 +1,51 @@
<?php
class ModelRetailcrmCustomer extends Model {
public function uploadToCrm($customers) {
$this->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;
}
}

View file

@ -1,174 +1,113 @@
<?php
class ModelRetailcrmOrder extends Model {
public function uploadToCrm($orders) {
$this->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;
}
}

3
system/cron/export.php Normal file
View file

@ -0,0 +1,3 @@
<?php
$cli_action = 'module/retailcrm/export';
require_once('dispatch.php');