This commit is contained in:
Alex Lushpai 2016-01-29 18:04:48 +03:00
parent 72919a236b
commit b38b23e56b
38 changed files with 2309 additions and 979 deletions

39
Changelog.md Normal file
View File

@ -0,0 +1,39 @@
Changelog
=========
####v0.3.0
* Расширена библиотека клиента
* Добавлена возможность кастомизации моделей заказа через vqmod
* Устранены мелкие баги, проведен рефакторинг кода.
####v0.2.0
Общие изменения
* Код приведен в состоянии совместимости с PHP 5.2
* Убрана необходимость собирать пакет через composer
* Библиотека api-client-php обновлена до последней версии и добавлена в стандартную поставку
* Переименованы методы, пути и значения ключей конфигурации в связи с ребрендингом
* Временая метка последнего запуска получения истории перенесена в таблицу конфигурации БД
Выгрузка каталога (ICML)
* Генерация обновлена в соответствии с последними измениями формата файла выгрузки
* Генерация вынесена в отдельный класс
* Добавлена возможность добавлять подкатегории
* Скорректировано указание активности офера
* Убрана генерация размера офера вследствие кастомизации этого параметра в разных магазинах
####v0.1.1
* Устранена ошибка редактирования, при которой терялась часть данных при получении истории из CRM
* Оптимизирован код получения и обработки истории заказов
* Актуализированы переводы
####v.0.1
* Реализован интерфейс настроек модуля
* Реализована отправка данных о заказе/клиенте в CRM
* Реализована выгрузка каталога (cron only)
* Реализовано получение данных о заказах, сделанных на стороне CRM (cron only)

103
README.md
View File

@ -1,16 +1,99 @@
Opencart module
==============
===============
Opencart module for interaction with [IntaroCRM](http://www.intarocrm.com) through [REST API](http://docs.intarocrm.ru/rest-api/).
Модуль интеграции CMS Openacrt c [RetailCRM](http://retailcrm.ru)
Module allows:
#### Модуль позволяет:
* Send to IntaroCRM new orders
* Configure relations between dictionaries of IntaroCRM and Opencart (statuses, payments, delivery types and etc)
* Generate [ICML](http://docs.intarocrm.ru/index.php?n=Пользователи.ФорматICML) (IntaroCRM Markup Language) for catalog loading by IntaroCRM
* Экспортировать в CRM данные о заказах и клиентах и получать обратно изменения по этим данным
* Синхронизировать справочники (способы доставки и оплаты, статусы заказов и т.п.)
* Выгружать каталог товаров в формате [ICML](http://retailcrm.ru/docs/Разработчики/ФорматICML) (IntaroCRM Markup Language)
#### Documentation
#### Установка
* [Install](doc/Install.md)
* [Changelog](doc/Changelog.md)
* [TODO](doc/TODO.md)
Установите модуль скопировав необходимые файлы в корень сайта
```
unzip master.zip
cp -r opencart-module/* /path/to/site/root
```
#### Активируйте модуль
В списке модулей нажмите "Установить"
#### Настройте параметры интеграции
На странице настроек модуля укажите URL Вашей CRM и ключ авторизации, после сохранения этих данных укажите соответствия справочников типов доставок, оплат и статусов заказа.
#### Выгрузка новых заказов в CRM (для версии opencart 1.5.x.x, для версии 2.0 и старше не требуется)
##### VQmod
Скопируйте xml-файл модифицирующий работу моделей _admin/model/sale/order.php_ и _catalog/model/checkout/order.php_ в _/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 следующую запись:
```
*/5 * * * * /usr/bin/php /path/to/opencart/system/cron/retailcrm/history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1
```
#### Настройка экспорта каталога
Для периодической выгрузки каталога добавьте в cron следующую запись:
```
* */4 * * * /usr/bin/php /path/to/opencart/system/cron/export.php >> /path/to/opencart/system/logs/cronjob_export.log 2>&1
```
В настройках CRM установите путь к файлу выгрузки
```
http://myopencartsite.ru/download/retailcrm.xml
```

View File

@ -0,0 +1,188 @@
<?php
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
class ControllerModuleRetailcrm extends Controller
{
private $error = array();
protected $log, $statuses, $payments, $deliveryTypes, $retailcrm;
public $children, $template;
public function install()
{
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('retailcrm', array('retailcrm_status' => 1));
}
public function uninstall()
{
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('retailcrm', array('retailcrm_status' => 0));
}
public function index()
{
$this->load->model('setting/setting');
$this->load->model('setting/extension');
$this->load->model('retailcrm/references');
$this->load->language('module/retailcrm');
$this->document->setTitle($this->language->get('heading_title'));
$this->document->addStyle('/admin/view/stylesheet/retailcrm.css');
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
$this->model_setting_setting->editSetting('retailcrm', $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->redirect($this->url->link('module/retailcrm', 'token=' . $this->session->data['token'], 'SSL'));
}
$text_strings = array(
'heading_title',
'text_enabled',
'text_disabled',
'button_save',
'button_cancel',
'text_notice',
'retailcrm_url',
'retailcrm_apikey',
'retailcrm_base_settings',
'retailcrm_dict_settings',
'retailcrm_dict_delivery',
'retailcrm_dict_status',
'retailcrm_dict_payment',
);
foreach ($text_strings as $text) {
$this->data[$text] = $this->language->get($text);
}
$this->data['retailcrm_errors'] = array();
$this->data['saved_settings'] = $this->model_setting_setting->getSetting('retailcrm');
$url = $this->data['saved_settings']['retailcrm_url'];
$key = $this->data['saved_settings']['retailcrm_apikey'];
if (!empty($url) && !empty($key)) {
$this->retailcrm = new RetailcrmProxy($url, $key, DIR_SYSTEM . 'logs/retailcrm.log');
$this->data['delivery'] = $this->model_retailcrm_references->getDeliveryTypes();
$this->data['statuses'] = $this->model_retailcrm_references->getOrderStatuses();
$this->data['payments'] = $this->model_retailcrm_references->getPaymentTypes();
}
$config_data = array(
'retailcrm_status'
);
foreach ($config_data as $conf) {
if (isset($this->request->post[$conf])) {
$this->data[$conf] = $this->request->post[$conf];
} else {
$this->data[$conf] = $this->config->get($conf);
}
}
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link(
'common/home',
'token=' . $this->session->data['token'], 'SSL'
),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link(
'extension/module',
'token=' . $this->session->data['token'], 'SSL'
),
'separator' => ' :: '
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link(
'module/retailcrm',
'token=' . $this->session->data['token'], 'SSL'
),
'separator' => ' :: '
);
$this->data['action'] = $this->url->link(
'module/retailcrm',
'token=' . $this->session->data['token'], 'SSL'
);
$this->data['cancel'] = $this->url->link(
'extension/module',
'token=' . $this->session->data['token'], 'SSL'
);
$this->data['modules'] = array();
if (isset($this->request->post['retailcrm_module'])) {
$this->data['modules'] = $this->request->post['retailcrm_module'];
} elseif ($this->config->get('retailcrm_module')) {
$this->data['modules'] = $this->config->get('retailcrm_module');
}
$this->load->model('design/layout');
$this->data['layouts'] = $this->model_design_layout->getLayouts();
$this->template = 'module/retailcrm.tpl';
$this->children = array(
'common/header',
'common/footer',
);
$this->response->setOutput($this->render());
}
public function history()
{
if (file_exists(DIR_APPLICATION . 'model/retailcrm/custom/history.php')) {
$this->load->model('retailcrm/custom/history');
$this->model_retailcrm_custom_history->request();
} else {
$this->load->model('retailcrm/history');
$this->model_retailcrm_history->request();
}
}
public function icml()
{
if (file_exists(DIR_APPLICATION . 'model/retailcrm/custom/icml.php')) {
$this->load->model('retailcrm/custom/icml');
$this->model_retailcrm_custom_icml->generateICML();
} else {
$this->load->model('retailcrm/icml');
$this->model_retailcrm_icml->generateICML();
}
}
private function validate()
{
if (!$this->user->hasPermission('modify', 'module/retailcrm')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if (!$this->error) {
return TRUE;
} else {
return FALSE;
}
}
}

View File

@ -1,25 +0,0 @@
<?php
// Heading Goes here:
$_['heading_title'] = 'IntaroCRM';
// Text
$_['text_module'] = 'Modules';
$_['text_success'] = 'Setting saved';
$_['text_notice'] = 'Warning! Timezone in CRM & your shop must be equal, you must setup it here:';
$_['intarocrm_base_settings'] = 'Connection settings';
$_['intarocrm_dict_settings'] = 'Dictionary settings';
$_['intarocrm_url'] = 'IntaroCRM URL';
$_['intarocrm_apikey'] = 'Api key';
$_['intarocrm_dict_delivery'] = 'Shipment methods';
$_['intarocrm_dict_status'] = 'Order statuses';
$_['intarocrm_dict_payment'] = 'Payment methods';
$_['column_total'] = 'Total';
$_['product_summ'] = 'Amount';
// Errors
$_['error_permission'] = 'Warning! You do not have permission to modify module';
?>

View File

@ -0,0 +1,30 @@
<?php
// Heading Goes here:
$_['heading_title'] = 'RetailCRM';
// Text
$_['text_module'] = 'Modules';
$_['text_success'] = 'Setting saved';
$_['text_notice'] = 'Warning! Timezone in CRM & your shop must be equal, you must setup it here:';
$_['retailcrm_base_settings'] = 'Connection settings';
$_['retailcrm_dict_settings'] = 'Dictionary settings';
$_['retailcrm_url'] = 'RetailCRM URL';
$_['retailcrm_apikey'] = 'RetailCRM API Key';
$_['retailcrm_dict_delivery'] = 'Shipment methods';
$_['retailcrm_dict_status'] = 'Order statuses';
$_['retailcrm_dict_payment'] = 'Payment methods';
$_['column_total'] = 'Total';
$_['product_summ'] = 'Amount';
$_['article'] = 'SKU';
$_['color'] = 'Color';
$_['weight'] = 'Weight';
$_['size'] = 'Size';
// Errors
$_['error_permission'] = 'Warning! You do not have permission to modify module';

View File

@ -1,25 +1,30 @@
<?php
// Heading Goes here:
$_['heading_title'] = 'IntaroCRM';
$_['heading_title'] = 'RetailCRM';
// Text
$_['text_module'] = 'Модули';
$_['text_success'] = 'Настройки успешно сохранены';
$_['text_notice'] = 'Внимание! Часовой пояс в CRM должен совпадать с часовым поясом в магазине, настроки часового пояса CRM можно задать по адресу:';
$_['intarocrm_base_settings'] = 'Настройки соединения';
$_['intarocrm_dict_settings'] = 'Настройки соответствия справочников';
$_['retailcrm_base_settings'] = 'Настройки соединения';
$_['retailcrm_dict_settings'] = 'Настройки соответствия справочников';
$_['intarocrm_url'] = 'Адрес IntaroCRM';
$_['intarocrm_apikey'] = 'Api ключ';
$_['retailcrm_url'] = 'Адрес RetailCRM';
$_['retailcrm_apikey'] = 'Api ключ RetailCRM';
$_['intarocrm_dict_delivery'] = 'Способы доставки';
$_['intarocrm_dict_status'] = 'Статусы';
$_['intarocrm_dict_payment'] = 'Способы оплаты';
$_['retailcrm_dict_delivery'] = 'Способы доставки';
$_['retailcrm_dict_status'] = 'Статусы';
$_['retailcrm_dict_payment'] = 'Способы оплаты';
$_['column_total'] = 'Итого';
$_['product_summ'] = 'Сумма';
$_['article'] = 'Артикул';
$_['color'] = 'Цвет';
$_['weight'] = 'Вес';
$_['size'] = 'Размер';
// Errors
$_['error_permission'] = 'У вас недостаточно прав на изменение настроек модуля';
?>

View File

@ -1,19 +0,0 @@
<?php
class ModelIntarocrmOrder extends Model {
public function send($order, $order_id)
{
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('intarocrm');
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
if(isset($settings['intarocrm_url']) && $settings['intarocrm_url'] != '' && isset($settings['intarocrm_apikey']) && $settings['intarocrm_apikey'] != '') {
include_once DIR_SYSTEM . 'library/intarocrm/apihelper.php';
$order['order_id'] = $order_id;
$crm = new ApiHelper($settings);
$crm->processOrder($order);
}
}
}
?>

View File

@ -1,197 +0,0 @@
<?php
class ModelIntarocrmTools extends Model {
protected $dd, $eCategories, $eOffers;
public function getOpercartDeliveryMethods()
{
$deliveryMethods = array();
$files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('shipping/' . $extension);
if ($this->config->get($extension . '_status')) {
$deliveryMethods[$extension.'.'.$extension] = strip_tags($this->language->get('heading_title'));
}
}
}
return $deliveryMethods;
}
public function getOpercartOrderStatuses()
{
$this->load->model('localisation/order_status');
return $this->model_localisation_order_status->getOrderStatuses(array());
}
public function getOpercartPaymentTypes()
{
$paymentTypes = array();
$files = glob(DIR_APPLICATION . 'controller/payment/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('payment/' . $extension);
if ($this->config->get($extension . '_status')) {
$paymentTypes[$extension] = strip_tags($this->language->get('heading_title'));
}
}
}
return $paymentTypes;
}
public function generateICML()
{
$string = '<?xml version="1.0" encoding="UTF-8"?>
<yml_catalog date="'.date('Y-m-d H:i:s').'">
<shop>
<name>'.$this->config->get('config_name').'</name>
<categories/>
<offers/>
</shop>
</yml_catalog>
';
$xml = new SimpleXMLElement($string, LIBXML_NOENT |LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE);
$this->dd = new DOMDocument();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd->getElementsByTagName('offers')->item(0);
$this->addCategories();
$this->addOffers();
$this->dd->saveXML();
$downloadPath = DIR_SYSTEM . '/../download/';
if (!file_exists($downloadPath)) {
mkdir($downloadPath, 0755);
}
$this->dd->save($downloadPath . 'intarocrm.xml');
}
private function addCategories()
{
$this->load->model('catalog/category');
foreach ($this->model_catalog_category->getCategories(array()) as $category) {
$e = $this->eCategories->appendChild($this->dd->createElement('category', $category['name']));
$e->setAttribute('id',$category['category_id']);
}
}
private function addOffers()
{
$this->load->model('catalog/product');
$this->load->model('catalog/manufacturer');
$this->load->model('tool/image');
$offerManufacturers = array();
$manufacturers = $this->model_catalog_manufacturer->getManufacturers(array());
foreach ($manufacturers as $manufacturer) {
$offerManufacturers[$manufacturer['manufacturer_id']] = $manufacturer['name'];
}
foreach ($this->model_catalog_product->getProducts(array()) as $offer) {
$e = $this->eOffers->appendChild($this->dd->createElement('offer'));
$e->setAttribute('id', $offer['product_id']);
$e->setAttribute('productId', $offer['product_id']);
$e->setAttribute('quantity', $offer['quantity']);
$e->setAttribute('available', $offer['status'] ? 'true' : 'false');
/*
* DIRTY HACK, NEED TO REFACTOR
*/
$sql = "SELECT * FROM `" .
DB_PREFIX .
"product_to_category` WHERE `product_id` = " .$offer['product_id']. ";"
;
$result = $this->db->query($sql);
foreach ($result->rows as $row) {
$e->appendChild($this->dd->createElement('categoryId', $row['category_id']));
}
$e->appendChild($this->dd->createElement('name'))->appendChild($this->dd->createTextNode($offer['name']));
$e->appendChild($this->dd->createElement('productName'))
->appendChild($this->dd->createTextNode($offer['name']));
$e->appendChild($this->dd->createElement('price', $offer['price']));
if ($offer['manufacturer_id'] != 0) {
$e->appendChild($this->dd->createElement('vendor'))
->appendChild($this->dd->createTextNode($offerManufacturers[$offer['manufacturer_id']]));
}
if ($offer['image']) {
$e->appendChild(
$this->dd->createElement(
'picture',
$this->model_tool_image->resize(
$offer['image'],
$this->config->get('config_image_product_width'),
$this->config->get('config_image_product_height')
)
)
);
}
$this->url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
$e->appendChild($this->dd->createElement('url'))->appendChild(
$this->dd->createTextNode(
$this->url->link('product/product&product_id=' . $offer['product_id'])
)
);
if ($offer['sku'] != '') {
$sku = $this->dd->createElement('param');
$sku->setAttribute('name', 'article');
$sku->appendChild($this->dd->createTextNode($offer['sku']));
$e->appendChild($sku);
}
if ($offer['weight'] != '') {
$weight = $this->dd->createElement('param');
$weight->setAttribute('name', 'weight');
$weightValue = (isset($offer['weight_class']))
? round($offer['weight'], 3) . ' ' . $offer['weight_class']
: round($offer['weight'], 3)
;
$weight->appendChild($this->dd->createTextNode($weightValue));
$e->appendChild($weight);
}
if ($offer['length'] != '' && $offer['width'] != '' && $offer['height'] != '') {
$size = $this->dd->createElement('param');
$size->setAttribute('name', 'size');
$size->appendChild(
$this->dd->createTextNode(
round($offer['length'], 2) .'x'.
round($offer['width'], 2) .'x'.
round($offer['height'], 2)
)
);
$e->appendChild($size);
}
}
}
}

View File

View File

@ -1,280 +1,52 @@
<?php
require_once __DIR__ . '/../../../system/library/intarocrm/vendor/autoload.php';
class ControllerModuleIntarocrm extends Controller {
private $error = array();
protected $log, $statuses, $payments, $deliveryTypes;
public function install() {
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>1));
}
public function uninstall() {
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('intarocrm', array('intarocrm_status'=>0));
}
public function index() {
$this->log = new Monolog\Logger('opencart-module');
$this->log->pushHandler(
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->load->model('setting/setting');
$this->load->model('setting/extension');
$this->load->model('intarocrm/tools');
$this->load->language('module/intarocrm');
$this->document->setTitle($this->language->get('heading_title'));
$this->document->addStyle('/admin/view/stylesheet/intarocrm.css');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('intarocrm', $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
$text_strings = array(
'heading_title',
'text_enabled',
'text_disabled',
'button_save',
'button_cancel',
'text_notice',
'intarocrm_url',
'intarocrm_apikey',
'intarocrm_base_settings',
'intarocrm_dict_settings',
'intarocrm_dict_delivery',
'intarocrm_dict_status',
'intarocrm_dict_payment',
);
foreach ($text_strings as $text) {
$this->data[$text] = $this->language->get($text);
}
$this->data['intarocrm_errors'] = array();
$this->data['saved_settings'] = $this->model_setting_setting->getSetting('intarocrm');
if ($this->data['saved_settings']['intarocrm_url'] != '' &&
$this->data['saved_settings']['intarocrm_apikey'] != ''
) {
$this->intarocrm = new \IntaroCrm\RestApi(
$this->data['saved_settings']['intarocrm_url'],
$this->data['saved_settings']['intarocrm_apikey']
);
/*
* Delivery
*/
try {
$this->deliveryTypes = $this->intarocrm->deliveryTypesList();
}
catch (IntaroCrm\Exception\ApiException $e)
class ModelRetailcrmHistory extends Model
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::deliveryTypesList::Api:' . $e->getMessage()
);
}
catch (IntaroCrm\Exception\CurlException $e)
public function request()
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' . $this->config->get('store_name') .
'] RestApi::deliveryTypesList::Curl:' . $e->getMessage()
);
}
$this->data['delivery'] = array(
'opencart' => $this->model_intarocrm_tools->getOpercartDeliveryMethods(),
'intarocrm' => $this->deliveryTypes
);
/*
* Statuses
*/
try {
$this->statuses = $this->intarocrm->orderStatusesList();
}
catch (IntaroCrm\Exception\ApiException $e)
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::orderStatusesList::Api:' . $e->getMessage()
);
}
catch (IntaroCrm\Exception\CurlException $e)
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::orderStatusesList::Curl:' . $e->getMessage()
);
}
$this->data['statuses'] = array(
'opencart' => $this->model_intarocrm_tools->getOpercartOrderStatuses(),
'intarocrm' => $this->statuses
);
/*
* Payment
*/
try {
$this->payments = $this->intarocrm->paymentTypesList();
}
catch (IntaroCrm\Exception\ApiException $e)
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::paymentTypesList::Api:' . $e->getMessage()
);
}
catch (IntaroCrm\Exception\CurlException $e)
{
$this->data['intarocrm_error'][] = $e->getMessage();
$this->log->addError(
'[' .
$this->config->get('store_name') .
'] RestApi::paymentTypesList::Curl:' . $e->getMessage()
);
}
$this->data['payments'] = array(
'opencart' => $this->model_intarocrm_tools->getOpercartPaymentTypes(),
'intarocrm' => $this->payments
);
}
$config_data = array(
'intarocrm_status'
);
foreach ($config_data as $conf) {
if (isset($this->request->post[$conf])) {
$this->data[$conf] = $this->request->post[$conf];
} else {
$this->data[$conf] = $this->config->get($conf);
}
}
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => ' :: '
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => ' :: '
);
$this->data['action'] = $this->url->link('module/intarocrm', 'token=' . $this->session->data['token'], 'SSL');
$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
$this->data['modules'] = array();
if (isset($this->request->post['intarocrm_module'])) {
$this->data['modules'] = $this->request->post['intarocrm_module'];
} elseif ($this->config->get('intarocrm_module')) {
$this->data['modules'] = $this->config->get('intarocrm_module');
}
$this->load->model('design/layout');
$this->data['layouts'] = $this->model_design_layout->getLayouts();
$this->template = 'module/intarocrm.tpl';
$this->children = array(
'common/header',
'common/footer',
);
$this->response->setOutput($this->render());
}
public function order_history()
{
$this->log = new Monolog\Logger('opencart-module');
$this->log->pushHandler(
new Monolog\Handler\StreamHandler(DIR_LOGS . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->load->model('setting/setting');
$this->load->model('setting/store');
$this->load->model('sale/order');
$this->load->model('sale/customer');
$this->load->model('intarocrm/tools');
$this->load->model('retailcrm/tools');
$this->load->model('catalog/product');
$this->load->model('localisation/zone');
$this->load->language('module/intarocrm');
$this->load->language('module/retailcrm');
$settings = $this->model_setting_setting->getSetting('intarocrm');
$settings = $this->model_setting_setting->getSetting('retailcrm');
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
if (isset($settings['intarocrm_url']) &&
$settings['intarocrm_url'] != '' &&
isset($settings['intarocrm_apikey']) &&
$settings['intarocrm_apikey'] != ''
) {
include_once __DIR__ . '/../../../system/library/intarocrm/apihelper.php';
$crm = new ApiHelper($settings);
$orders = $crm->orderHistory();
if (!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
$crm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log'
);
$orders = $crm->ordersHistory();
$ordersIdsFix = array();
$customersIdsFix = array();
$subtotalSettings = $this->model_setting_setting->getSetting('sub_total');
$totalSettings = $this->model_setting_setting->getSetting('total');
$shippingSettings = $this->model_setting_setting->getSetting('shipping');
$delivery = array_flip($settings['intarocrm_delivery']);
$payment = array_flip($settings['intarocrm_payment']);
$status = array_flip($settings['intarocrm_status']);
$delivery = array_flip($settings['retailcrm_delivery']);
$payment = array_flip($settings['retailcrm_payment']);
$status = array_flip($settings['retailcrm_status']);
$ocPayment = $this->model_intarocrm_tools->getOpercartPaymentTypes();
$ocDelivery = $this->model_intarocrm_tools->getOpercartDeliveryMethods();
$ocPayment = $this->model_retailcrm_tools->getOpercartPaymentTypes();
$ocDelivery = $this->model_retailcrm_tools->getOpercartDeliveryMethods();
$zones = $this->model_localisation_zone->getZones();
foreach ($orders as $order) {
if (!isset($order['deleted']) || !$order['deleted']) {
if (empty($order['deleted'])) {
$data = array();
$customer_id = (isset($order['customer']['externalId']) && $order['customer']['externalId'] != 0)
$customer_id = (!empty($order['customer']['externalId']))
? $order['customer']['externalId']
: ''
;
@ -521,39 +293,15 @@ class ControllerModuleIntarocrm extends Controller {
}
if (!empty($customersIdsFix)) {
$crm->customerFixExternalIds($customersIdsFix);
$crm->customersFixExternalIds($customersIdsFix);
}
if (!empty($ordersIdsFix)) {
$crm->orderFixExternalIds($ordersIdsFix);
$crm->ordersFixExternalIds($ordersIdsFix);
}
} else {
$this->log->addNotice(
'['.
$this->config->get('store_name').
'] RestApi::orderHistory: you need to configure Intarocrm module first.'
);
$this->log->addNotice('You need to configure retailcrm module first.');
}
}
public function export_icml()
{
$this->load->model('intarocrm/tools');
$this->model_intarocrm_tools->generateICML();
}
private function validate() {
if (!$this->user->hasPermission('modify', 'module/intarocrm')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if (!$this->error) {
return TRUE;
} else {
return FALSE;
}
}
}

View File

@ -0,0 +1,215 @@
<?php
class ModelRetailcrmIcml extends Model
{
protected $shop;
protected $file;
protected $properties;
protected $params;
protected $dd;
protected $eCategories;
protected $eOffers;
public function generateICML()
{
$this->load->language('module/retailcrm');
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('catalog/manufacturer');
$string = '<?xml version="1.0" encoding="UTF-8"?>
<yml_catalog date="'.date('Y-m-d H:i:s').'">
<shop>
<name>'.$this->config->get('config_name').'</name>
<categories/>
<offers/>
</shop>
</yml_catalog>
';
$xml = new SimpleXMLElement(
$string,
LIBXML_NOENT |LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE
);
$this->dd = new DOMDocument();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd
->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd
->getElementsByTagName('offers')->item(0);
$this->addCategories();
$this->addOffers();
$this->dd->saveXML();
$downloadPath = DIR_SYSTEM . '/../download/';
if (!file_exists($downloadPath)) {
mkdir($downloadPath, 0755);
}
$this->dd->save($downloadPath . 'retailcrm.xml');
}
/**
*
*/
private function addCategories()
{
$categories = $this->model_catalog_category->getCategories(array());
foreach($categories as $category) {
$e = $this->eCategories->appendChild(
$this->dd->createElement(
'category', $category['name']
)
);
$e->setAttribute('id', $category['category_id']);
if ($category['parent_id'] > 0) {
$e->setAttribute('parentId', $category['parent_id']);
}
}
}
private function addOffers()
{
$offerManufacturers = array();
$manufacturers = $this->model_catalog_manufacturer
->getManufacturers(array());
foreach ($manufacturers as $manufacturer) {
$offerManufacturers[
$manufacturer['manufacturer_id']
] = $manufacturer['name'];
}
$products = $this->model_catalog_product->getProducts(array());
foreach ($products as $offer) {
$e = $this->eOffers->appendChild($this->dd->createElement('offer'));
$e->setAttribute('id', $offer['product_id']);
$e->setAttribute('productId', $offer['product_id']);
$e->setAttribute('quantity', $offer['quantity']);
/**
* Offer activity
*/
$activity = $offer['status'] == 1 ? 'Y' : 'N';
$e->appendChild(
$this->dd->createElement('productActivity')
)->appendChild(
$this->dd->createTextNode($activity)
);
/**
* Offer categories
*/
$categories = $this->model_catalog_product
->getProductCategories($offer['product_id']);
if (!empty($categories)) {
foreach ($categories as $category) {
$e->appendChild($this->dd->createElement('category'))
->appendChild(
$this->dd->createTextNode($category)
);
}
}
/**
* Name & price
*/
$e->appendChild($this->dd->createElement('name'))
->appendChild($this->dd->createTextNode($offer['name']));
$e->appendChild($this->dd->createElement('productName'))
->appendChild($this->dd->createTextNode($offer['name']));
$e->appendChild($this->dd->createElement('price'))
->appendChild($this->dd->createTextNode($offer['price']));
/**
* Vendor
*/
if ($offer['manufacturer_id'] != 0) {
$e->appendChild($this->dd->createElement('vendor'))
->appendChild(
$this->dd->createTextNode(
$offerManufacturers[$offer['manufacturer_id']]
)
);
}
/**
* Image
*/
if ($offer['image']) {
$image = $this->generateImage($offer['image']);
$e->appendChild($this->dd->createElement('picture'))
->appendChild($this->dd->createTextNode($image));
}
/**
* Url
*/
$this->url = new Url(
HTTP_CATALOG,
$this->config->get('config_secure')
? HTTP_CATALOG
: HTTPS_CATALOG
);
$e->appendChild($this->dd->createElement('url'))
->appendChild(
$this->dd->createTextNode(
$this->url->link(
'product/product&product_id=' . $offer['product_id']
)
)
);
if ($offer['sku']) {
$sku = $this->dd->createElement('param');
$sku->setAttribute('code', 'article');
$sku->setAttribute('name', $this->language->get('article'));
$sku->appendChild($this->dd->createTextNode($offer['sku']));
$e->appendChild($sku);
}
if ($offer['weight'] != '') {
$weight = $this->dd->createElement('param');
$weight->setAttribute('color', 'weight');
$weight->setAttribute('name', $this->language->get('weight'));
$weightValue = (isset($offer['weight_class']))
? round($offer['weight'], 3) . ' ' . $offer['weight_class']
: round($offer['weight'], 3)
;
$weight->appendChild($this->dd->createTextNode($weightValue));
$e->appendChild($weight);
}
}
}
/**
* @param $image
* @return mixed
*/
private function generateImage($image)
{
$this->load->model('tool/image');
return $this->model_tool_image->resize(
$image,
$this->config->get('config_image_product_width'),
$this->config->get('config_image_product_height')
);
}
}

View File

@ -0,0 +1,174 @@
<?php
class ModelRetailcrmOrder extends Model {
public function sendToCrm($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'];
$customers = $this->retailcrm->customersList(
array(
'name' => $order_data['telephone'],
'email' => $order_data['email']
),
1,
100
);
foreach($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id'];
}
unset($customers);
$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'];
}
}
$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']
))
)
);
$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['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'];
}
}
$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']
))
)
);
$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['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
}
$this->retailcrm->ordersEdit($order);
}
}
}

View File

@ -0,0 +1,138 @@
<?php
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
class ModelRetailcrmReferences extends Model
{
protected $retailcrm;
public function getDeliveryTypes()
{
return array(
'opencart' => $this->getOpercartDeliveryTypes(),
'retailcrm' => $this->getApiDeliveryTypes()
);
}
public function getOrderStatuses()
{
return array(
'opencart' => $this->getOpercartOrderStatuses(),
'retailcrm' => $this->getApiOrderStatuses()
);
}
public function getPaymentTypes()
{
return array(
'opencart' => $this->getOpercartPaymentTypes(),
'retailcrm' => $this->getApiPaymentTypes()
);
}
protected function getOpercartDeliveryTypes()
{
$deliveryMethods = array();
$files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('shipping/' . $extension);
if ($this->config->get($extension . '_status')) {
$deliveryMethods[$extension.'.'.$extension] = strip_tags(
$this->language->get('heading_title')
);
}
}
}
return $deliveryMethods;
}
protected function getOpercartOrderStatuses()
{
$this->load->model('localisation/order_status');
return $this->model_localisation_order_status
->getOrderStatuses(array());
}
protected function getOpercartPaymentTypes()
{
$paymentTypes = array();
$files = glob(DIR_APPLICATION . 'controller/payment/*.php');
if ($files) {
foreach ($files as $file) {
$extension = basename($file, '.php');
$this->load->language('payment/' . $extension);
if ($this->config->get($extension . '_status')) {
$paymentTypes[$extension] = strip_tags(
$this->language->get('heading_title')
);
}
}
}
return $paymentTypes;
}
protected function getApiDeliveryTypes()
{
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
$this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log'
);
$response = $this->retailcrm->deliveryTypesList();
return ($response === false) ? array() : $response->deliveryTypes;
}
}
protected function getApiOrderStatuses()
{
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
$this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log'
);
$response = $this->retailcrm->statusesList();
return ($response === false) ? array() : $response->statuses;
}
}
protected function getApiPaymentTypes()
{
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm');
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
$this->retailcrm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log'
);
$response = $this->retailcrm->paymentTypesList();
return ($response === false) ? array() : $response->paymentTypes;
}
}
}

View File

@ -1,2 +0,0 @@
.intarocrm_unit {margin-bottom: 10px;}
.intarocrm_unit input {width: 30%;}

View File

@ -0,0 +1,2 @@
.retailcrm_unit {margin-bottom: 10px;}
.retailcrm_unit input {width: 30%;}

View File

@ -9,10 +9,10 @@
<?php if ($error_warning) : ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php endif; ?>
<?php if (isset($saved_settings['intarocrm_url'])): ?>
<?php if (isset($saved_settings['retailcrm_url'])): ?>
<div class="success">
<?php echo $text_notice; ?>
<a href="<?php echo $saved_settings['intarocrm_url']; ?>/admin/settings#t-main"><?php echo $saved_settings['intarocrm_url']; ?>/admin/settings#t-main</a>
<a href="<?php echo $saved_settings['retailcrm_url']; ?>/admin/settings#t-main"><?php echo $saved_settings['retailcrm_url']; ?>/admin/settings#t-main</a>
</div>
<?php endif; ?>
@ -23,67 +23,67 @@
</div>
<div class="content">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
<input type="hidden" name="intarocrm_status" value="1">
<input type="hidden" name="retailcrm_status" value="1">
<h3><?php echo $intarocrm_base_settings; ?></h3>
<div class="intarocrm_unit">
<label for="intarocrm_url"><?php echo $intarocrm_url; ?></label><br>
<input id="intarocrm_url" type="text" name="intarocrm_url" value="<?php if (isset($saved_settings['intarocrm_url'])): echo $saved_settings['intarocrm_url']; endif; ?>">
<h3><?php echo $retailcrm_base_settings; ?></h3>
<div class="retailcrm_unit">
<label for="retailcrm_url"><?php echo $retailcrm_url; ?></label><br>
<input id="retailcrm_url" type="text" name="retailcrm_url" value="<?php if (isset($saved_settings['retailcrm_url'])): echo $saved_settings['retailcrm_url']; endif; ?>">
</div>
<div class="intarocrm_unit">
<label for="intarocrm_apikey"><?php echo $intarocrm_apikey; ?></label><br>
<input id="intarocrm_apikey" type="text" name="intarocrm_apikey" value="<?php if (isset($saved_settings['intarocrm_apikey'])): echo $saved_settings['intarocrm_apikey']; endif;?>">
<div class="retailcrm_unit">
<label for="retailcrm_apikey"><?php echo $retailcrm_apikey; ?></label><br>
<input id="retailcrm_apikey" type="text" name="retailcrm_apikey" value="<?php if (isset($saved_settings['retailcrm_apikey'])): echo $saved_settings['retailcrm_apikey']; endif;?>">
</div>
<?php if (isset($saved_settings['intarocrm_apikey']) && $saved_settings['intarocrm_apikey'] != '' && isset($saved_settings['intarocrm_url']) && $saved_settings['intarocrm_url'] != ''): ?>
<?php if (isset($saved_settings['retailcrm_apikey']) && $saved_settings['retailcrm_apikey'] != '' && isset($saved_settings['retailcrm_url']) && $saved_settings['retailcrm_url'] != ''): ?>
<?php if (!empty($intarocrm_errors)) : ?>
<?php foreach($intarocrm_errors as $intarocrm_error): ?>
<div class="warning"><?php echo $intarocrm_error ?></div>
<?php if (!empty($retailcrm_errors)) : ?>
<?php foreach($retailcrm_errors as $retailcrm_error): ?>
<div class="warning"><?php echo $retailcrm_error ?></div>
<?php endforeach; ?>
<?php else: ?>
<h3><?php echo $intarocrm_dict_settings; ?></h3>
<h3><?php echo $retailcrm_dict_settings; ?></h3>
<h4><?php echo $intarocrm_dict_delivery; ?></h4>
<h4><?php echo $retailcrm_dict_delivery; ?></h4>
<?php foreach ($delivery['opencart'] as $key => $value): ?>
<div class="intarocrm_unit">
<select id="intarocrm_delivery_<?php echo $key; ?>" name="intarocrm_delivery[<?php echo $key; ?>]" >
<?php foreach ($delivery['intarocrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['intarocrm_delivery'][$key]) && $v['code'] == $saved_settings['intarocrm_delivery'][$key]):?>selected="selected"<?php endif;?>>
<div class="retailcrm_unit">
<select id="retailcrm_delivery_<?php echo $key; ?>" name="retailcrm_delivery[<?php echo $key; ?>]" >
<?php foreach ($delivery['retailcrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['retailcrm_delivery'][$key]) && $v['code'] == $saved_settings['retailcrm_delivery'][$key]):?>selected="selected"<?php endif;?>>
<?php echo $v['name'];?>
</option>
<?php endforeach; ?>
</select>
<label for="intarocrm_delivery_<?php echo $key; ?>"><?php echo $value; ?></label>
<label for="retailcrm_delivery_<?php echo $key; ?>"><?php echo $value; ?></label>
</div>
<?php endforeach; ?>
<h4><?php echo $intarocrm_dict_status; ?></h4>
<h4><?php echo $retailcrm_dict_status; ?></h4>
<?php foreach ($statuses['opencart'] as $status): ?>
<?php $uid = $status['order_status_id']?>
<div class="intarocrm_unit">
<select id="intarocrm_status_<?php echo $uid; ?>" name="intarocrm_status[<?php echo $uid; ?>]" >
<?php foreach ($statuses['intarocrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['intarocrm_status'][$uid]) && $v['code'] == $saved_settings['intarocrm_status'][$uid]):?>selected="selected"<?php endif;?>>
<div class="retailcrm_unit">
<select id="retailcrm_status_<?php echo $uid; ?>" name="retailcrm_status[<?php echo $uid; ?>]" >
<?php foreach ($statuses['retailcrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['retailcrm_status'][$uid]) && $v['code'] == $saved_settings['retailcrm_status'][$uid]):?>selected="selected"<?php endif;?>>
<?php echo $v['name'];?>
</option>
<?php endforeach; ?>
</select>
<label for="intarocrm_status_<?php echo $status['order_status_id']; ?>"><?php echo $status['name']; ?></label>
<label for="retailcrm_status_<?php echo $status['order_status_id']; ?>"><?php echo $status['name']; ?></label>
</div>
<?php endforeach; ?>
<h4><?php echo $intarocrm_dict_payment; ?></h4>
<h4><?php echo $retailcrm_dict_payment; ?></h4>
<?php foreach ($payments['opencart'] as $key => $value): ?>
<div class="intarocrm_unit">
<select id="intarocrm_payment_<?php echo $key; ?>" name="intarocrm_payment[<?php echo $key; ?>]" >
<?php foreach ($payments['intarocrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['intarocrm_payment'][$key]) && $v['code'] == $saved_settings['intarocrm_payment'][$key]):?>selected="selected"<?php endif;?>>
<div class="retailcrm_unit">
<select id="retailcrm_payment_<?php echo $key; ?>" name="retailcrm_payment[<?php echo $key; ?>]" >
<?php foreach ($payments['retailcrm'] as $k => $v): ?>
<option value="<?php echo $v['code'];?>" <?php if(isset($saved_settings['retailcrm_payment'][$key]) && $v['code'] == $saved_settings['retailcrm_payment'][$key]):?>selected="selected"<?php endif;?>>
<?php echo $v['name'];?>
</option>
<?php endforeach; ?>
</select>
<label for="intarocrm_payment_<?php echo $key; ?>"><?php echo $value; ?></label>
<label for="retailcrm_payment_<?php echo $key; ?>"><?php echo $value; ?></label>
</div>
<?php endforeach; ?>
@ -93,7 +93,6 @@
</form>
</div>
</div>
<?php //var_dump($saved_settings);?>
</div>

View File

@ -1,19 +0,0 @@
<?php
class ModelIntarocrmOrder extends Model {
public function send($order, $order_id)
{
$this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('intarocrm');
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
if(isset($settings['intarocrm_url']) && $settings['intarocrm_url'] != '' && isset($settings['intarocrm_apikey']) && $settings['intarocrm_apikey'] != '') {
include_once DIR_SYSTEM . 'library/intarocrm/apihelper.php';
$order['order_id'] = $order_id;
$crm = new ApiHelper($settings);
$crm->processOrder($order);
}
}
}
?>

View File

@ -0,0 +1,97 @@
<?php
class ModelRetailcrmOrder extends Model {
public function sendToCrm($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'];
$customers = $this->retailcrm->customersList(
array(
'name' => $order_data['telephone'],
'email' => $order_data['email']
),
1,
100
);
foreach($customers['customers'] as $customer) {
$order['customer']['id'] = $customer['id'];
}
unset($customers);
$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'];
}
}
$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']
))
)
);
$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['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
}
$this->retailcrm->ordersCreate($order);
}
}
}

View File

@ -1,3 +0,0 @@
<?php
$cli_action = 'module/intarocrm/export_icml';
require_once('cli_dispatch.php');

View File

@ -1,3 +0,0 @@
<?php
$cli_action = 'module/intarocrm/order_history';
require_once('cli_dispatch.php');

View File

@ -1,17 +0,0 @@
Changelog
=========
### v.0.1.1
* Устранена ошибка редактирования, при которой терялась часть данных при получении истории из CRM
* Оптимизирован код получения и обработки истории заказов
* Актуализированы переводы
### v.0.1
* Реализован интерфейс настроек модуля
* Реализована отправка данных о заказе/клиенте в CRM
* Реализована выгрузка каталога (cron only)
* Реализовано получение данных о заказах, сделанных на стороне CRM (cron only)

View File

@ -1,72 +0,0 @@
Installation
============
### Clone module.
```
git clone git@github.com:/retailcrm/opencart-module.git
```
### Install Rest API Client.
```
cd opencart-module/system/library/intarocrm
./composer.phar install
```
### Install module
```
cp -r opencart-module/* /path/to/opecart/instance
```
### Activate via Admin interface.
Go to Modules -> Intstall module. Before running exchange you must configure module.
### Export
Setup cron job for periodically catalog export
```
* */12 * * * /usr/bin/php /path/to/opencart/cli/cli_export.php >> /path/to/opencart/system/logs/cronjob_export.log 2>&1
```
Into your CRM settings set path to exported file
```
/download/intarocrm.xml
```
### Exchange setup
#### Export new order from shop to CRM
```
$this->load->model('intarocrm/order');
$this->model_intarocrm_order->send($data, $order_id);
```
Add this lines into:
* /catalog/model/checkout/order.php script, into addOrder method before return statement
```
if (!isset($data['fromApi'])) {
$this->load->model('setting/setting');
$status = $this->model_setting_setting->getSetting('intarocrm');
$data['order_status'] = $status['intarocrm_status'][$data['order_status_id']];
$this->load->model('intarocrm/order');
$this->model_intarocrm_order->send($data, $order_id);
}
```
Add this lines into:
* /admin/model/sale/order.php script, into addOrder & editOrder methods at the end of these methods
#### Export new order from CRM to shop
Setup cron job for exchange between CRM & your shop
```
*/5 * * * * /usr/bin/php /path/to/opencart/cli/cli_history.php >> /path/to/opencart/system/logs/cronjob_history.log 2>&1
```

View File

@ -1,6 +0,0 @@
TODO
====
* Export old customers & orders
* New customers export
* Make sources PSR-2 compatible

View File

@ -1,12 +1,5 @@
<?php
// CLI must be called by cli php
if (php_sapi_name() != 'cli') {
$log->write("ERROR: cli $cli_action call attempted by non-cli.");
http_response_code(400);
exit;
}
// Ensure $cli_action is set
if (!isset($cli_action)) {
echo 'ERROR: $cli_action must be set in calling script.';
@ -19,7 +12,7 @@ if (!isset($cli_action)) {
define('VERSION', '1.5.6');
// Configuration (note we're using the admin config)
require_once(__DIR__ . '/../admin/config.php');
require_once(realpath(dirname(__FILE__)) . '/../../admin/config.php');
// Configuration check
if (!defined('DIR_APPLICATION')) {
@ -138,4 +131,3 @@ $controller->dispatch($action, new Action('error/not_found'));
// Output
$response->output();
?>

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

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

3
system/cron/history.php Normal file
View File

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

View File

@ -1,2 +0,0 @@
/vendor
composer.lock

View File

@ -1,231 +0,0 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';
class ApiHelper
{
private $dir, $fileDate;
protected $intaroApi, $log, $settings;
public function __construct($settings) {
$this->dir = __DIR__ . '/../../logs/';
$this->fileDate = $this->dir . 'intarocrm_history.log';
$this->settings = $settings;
$this->domain = $settings['domain'];
$this->log = new Monolog\Logger('intarocrm');
$this->log->pushHandler(
new Monolog\Handler\StreamHandler($this->dir . 'intarocrm_module.log', Monolog\Logger::INFO)
);
$this->intaroApi = new IntaroCrm\RestApi(
$settings['intarocrm_url'],
$settings['intarocrm_apikey']
);
}
public function processOrder($data) {
$order = array();
$customer = array();
$customers = array();
$payment_code = $data['payment_code'];
$delivery_code = $data['shipping_code'];
$settings = $this->settings;
try {
$customers = $this->intaroApi->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data));
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage());
}
if(count($customers) > 0 && isset($customers[0]['externalId'])) {
$order['customerId'] = $customers[0]['externalId'];
} else {
$order['customerId'] = ($data['customer_id'] != '') ? $data['customer_id'] : (int) substr((microtime(true) * 10000) . mt_rand(1, 1000), 10, -1);
$customer['externalId'] = $order['customerId'];
$customer['firstName'] = $data['firstname'];
$customer['lastName'] = $data['lastname'];
$customer['email'] = $data['email'];
$customer['phones'] = array(array('number' => $data['telephone']));
$customer['address']['country'] = $data['payment_country_id'];
$customer['address']['region'] = $data['payment_zone_id'];
$customer['address']['text'] = implode(', ', array(
$data['payment_postcode'],
$data['payment_country'],
$data['payment_city'],
$data['payment_address_1'],
$data['payment_address_2']
));
try {
$this->customer = $this->intaroApi->customerEdit($customer);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->customer = $e->getMessage();
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
} catch (IntaroCrm\Exception\CurlException $e) {
$this->customer = $e->getMessage();
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
}
}
unset($customer);
unset($customers);
$order['externalId'] = $data['order_id'];
$order['firstName'] = $data['firstname'];
$order['lastName'] = $data['lastname'];
$order['email'] = $data['email'];
$order['phone'] = $data['telephone'];
$order['customerComment'] = $data['comment'];
$deliveryCost = 0;
$orderTotals = isset($data['totals']) ? $data['totals'] : $data['order_total'] ;
foreach ($orderTotals as $totals) {
if ($totals['code'] == 'shipping') {
$deliveryCost = $totals['value'];
}
}
$order['createdAt'] = date('Y-m-d H:i:s');
$order['paymentType'] = $settings['intarocrm_payment'][$payment_code];
$country = (isset($data['shipping_country'])) ? $data['shipping_country'] : '' ;
$order['delivery'] = array(
'code' => $settings['intarocrm_delivery'][$delivery_code],
'cost' => $deliveryCost,
'address' => array(
'index' => $data['shipping_postcode'],
'city' => $data['shipping_city'],
'country' => $data['shipping_country_id'],
'region' => $data['shipping_zone_id'],
'text' => implode(', ', array(
$data['shipping_postcode'],
$country,
$data['shipping_city'],
$data['shipping_address_1'],
$data['shipping_address_2']
))
)
);
$orderProducts = isset($data['products']) ? $data['products'] : $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($data['order_status_id'])) {
$order['status'] = $data['order_status'];
}
try {
$this->intaroApi->orderEdit($order);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
}
}
public function orderHistory() {
$orders = array();
try {
$orders = $this->intaroApi->orderHistory($this->getDate());
$this->saveDate($this->intaroApi->getGeneratedAt()->format('Y-m-d H:i:s'));
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders));
return false;
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage());
return false;
}
return $orders;
}
public function orderFixExternalIds($data)
{
try {
return $this->intaroApi->orderFixExternalIds($data);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data));
return false;
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
return false;
}
}
public function customerFixExternalIds($data)
{
try {
return $this->intaroApi->customerFixExternalIds($data);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . json_encode($data));
return false;
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds::Curl:' . $e->getMessage());
return false;
}
}
public function getOrder($order_id)
{
try {
return $this->intaroApi->orderGet($order_id);
} catch (IntaroCrm\Exception\ApiException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($order));
return false;
} catch (IntaroCrm\Exception\CurlException $e) {
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
return false;
}
}
private function saveDate($date) {
file_put_contents($this->fileDate, $date, LOCK_EX);
}
private function getDate() {
if (file_exists($this->fileDate)) {
$result = file_get_contents($this->fileDate);
} else {
$result = date('Y-m-d H:i:s', strtotime('-2 days', strtotime(date('Y-m-d H:i:s'))));
}
return $result;
}
}

View File

@ -1,28 +0,0 @@
{
"name": "retailcrm/opencart-module",
"description": "Opencart integration for IntaroCRM",
"type": "library",
"keywords": ["api", "Intaro CRM", "rest"],
"homepage": "http://www.retailcrm.ru/",
"authors": [
{
"name": "Alex Lushpai",
"email": "lushpai@intaro.ru",
"role": "Developer"
}
],
"support": {
"email": "support@intarocrm.ru"
},
"require": {
"php": ">=5.3",
"retailcrm/api-client-php": "1.3.*",
"symfony/console": "2.6.*",
"monolog/monolog": "1.12.*"
},
"autoload": {
"psr-0": {
"": "/"
}
}
}

View File

@ -0,0 +1,5 @@
<?php
class CurlException extends RuntimeException
{
}

View File

@ -0,0 +1,5 @@
<?php
class InvalidJsonException extends DomainException
{
}

View File

@ -0,0 +1,811 @@
<?php
/**
* retailCRM API client class
*/
class RetailcrmApiClient
{
const VERSION = 'v3';
protected $client;
/**
* Site code
*/
protected $siteCode;
/**
* Client creating
*
* @param string $url
* @param string $apiKey
* @param string $site
* @return mixed
*/
public function __construct($url, $apiKey, $site = null)
{
if ('/' != substr($url, strlen($url) - 1, 1)) {
$url .= '/';
}
$url = $url . 'api/' . self::VERSION;
$this->client = new RetailcrmHttpClient($url, array('apiKey' => $apiKey));
$this->siteCode = $site;
}
/**
* Create a order
*
* @param array $order
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function ordersCreate(array $order, $site = null)
{
if (!sizeof($order)) {
throw new InvalidArgumentException('Parameter `order` must contains a data');
}
return $this->client->makeRequest("/orders/create", RetailcrmHttpClient::METHOD_POST, $this->fillSite($site, array(
'order' => json_encode($order)
)));
}
/**
* Edit a order
*
* @param array $order
* @param string $by
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function ordersEdit(array $order, $by = 'externalId', $site = null)
{
if (!sizeof($order)) {
throw new InvalidArgumentException('Parameter `order` must contains a data');
}
$this->checkIdParameter($by);
if (!isset($order[$by])) {
throw new InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by));
}
return $this->client->makeRequest(
"/orders/" . $order[$by] . "/edit",
RetailcrmHttpClient::METHOD_POST,
$this->fillSite($site, array(
'order' => json_encode($order),
'by' => $by,
))
);
}
/**
* Upload array of the orders
*
* @param array $orders
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function ordersUpload(array $orders, $site = null)
{
if (!sizeof($orders)) {
throw new InvalidArgumentException('Parameter `orders` must contains array of the orders');
}
return $this->client->makeRequest("/orders/upload", RetailcrmHttpClient::METHOD_POST, $this->fillSite($site, array(
'orders' => json_encode($orders),
)));
}
/**
* Get order by id or externalId
*
* @param string $id
* @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function ordersGet($id, $by = 'externalId', $site = null)
{
$this->checkIdParameter($by);
return $this->client->makeRequest("/orders/$id", RetailcrmHttpClient::METHOD_GET, $this->fillSite($site, array(
'by' => $by
)));
}
/**
* Returns a orders history
*
* @param DateTime $startDate (default: null)
* @param DateTime $endDate (default: null)
* @param int $limit (default: 100)
* @param int $offset (default: 0)
* @param bool $skipMyChanges (default: true)
* @return RetailcrmApiResponse
*/
public function ordersHistory(
DateTime $startDate = null,
DateTime $endDate = null,
$limit = 100,
$offset = 0,
$skipMyChanges = true
) {
$parameters = array();
if ($startDate) {
$parameters['startDate'] = $startDate->format('Y-m-d H:i:s');
}
if ($endDate) {
$parameters['endDate'] = $endDate->format('Y-m-d H:i:s');
}
if ($limit) {
$parameters['limit'] = (int) $limit;
}
if ($offset) {
$parameters['offset'] = (int) $offset;
}
if ($skipMyChanges) {
$parameters['skipMyChanges'] = (bool) $skipMyChanges;
}
return $this->client->makeRequest('/orders/history', RetailcrmHttpClient::METHOD_GET, $parameters);
}
/**
* Returns filtered orders list
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @return RetailcrmApiResponse
*/
public function ordersList(array $filter = array(), $page = null, $limit = null)
{
$parameters = array();
if (sizeof($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest('/orders', RetailcrmHttpClient::METHOD_GET, $parameters);
}
/**
* Returns statuses of the orders
*
* @param array $ids (default: array())
* @param array $externalIds (default: array())
* @return RetailcrmApiResponse
*/
public function ordersStatuses(array $ids = array(), array $externalIds = array())
{
$parameters = array();
if (sizeof($ids)) {
$parameters['ids'] = $ids;
}
if (sizeof($externalIds)) {
$parameters['externalIds'] = $externalIds;
}
return $this->client->makeRequest('/orders/statuses', RetailcrmHttpClient::METHOD_GET, $parameters);
}
/**
* Save order IDs' (id and externalId) association in the CRM
*
* @param array $ids
* @return RetailcrmApiResponse
*/
public function ordersFixExternalIds(array $ids)
{
if (!sizeof($ids)) {
throw new InvalidArgumentException('Method parameter must contains at least one IDs pair');
}
return $this->client->makeRequest("/orders/fix-external-ids", RetailcrmHttpClient::METHOD_POST, array(
'orders' => json_encode($ids),
));
}
/**
* Get orders assembly history
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @return RetailcrmApiResponse
*/
public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null)
{
$parameters = array();
if (sizeof($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest('/orders/packs/history', RetailcrmHttpClient::METHOD_GET, $parameters);
}
/**
* Create a customer
*
* @param array $customer
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function customersCreate(array $customer, $site = null)
{
if (!sizeof($customer)) {
throw new InvalidArgumentException('Parameter `customer` must contains a data');
}
return $this->client->makeRequest("/customers/create", RetailcrmHttpClient::METHOD_POST, $this->fillSite($site, array(
'customer' => json_encode($customer)
)));
}
/**
* Edit a customer
*
* @param array $customer
* @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function customersEdit(array $customer, $by = 'externalId', $site = null)
{
if (!sizeof($customer)) {
throw new InvalidArgumentException('Parameter `customer` must contains a data');
}
$this->checkIdParameter($by);
if (!isset($customer[$by])) {
throw new InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by));
}
return $this->client->makeRequest(
"/customers/" . $customer[$by] . "/edit",
RetailcrmHttpClient::METHOD_POST,
$this->fillSite(
$site,
array(
'customer' => json_encode($customer),
'by' => $by
)
)
);
}
/**
* Upload array of the customers
*
* @param array $customers
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function customersUpload(array $customers, $site = null)
{
if (!sizeof($customers)) {
throw new InvalidArgumentException('Parameter `customers` must contains array of the customers');
}
return $this->client->makeRequest("/customers/upload", RetailcrmHttpClient::METHOD_POST, $this->fillSite($site, array(
'customers' => json_encode($customers),
)));
}
/**
* Get customer by id or externalId
*
* @param string $id
* @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function customersGet($id, $by = 'externalId', $site = null)
{
$this->checkIdParameter($by);
return $this->client->makeRequest("/customers/$id", RetailcrmHttpClient::METHOD_GET, $this->fillSite($site, array(
'by' => $by
)));
}
/**
* Returns filtered customers list
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @return RetailcrmApiResponse
*/
public function customersList(array $filter = array(), $page = null, $limit = null)
{
$parameters = array();
if (sizeof($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest('/customers', RetailcrmHttpClient::METHOD_GET, $parameters);
}
/**
* Save customer IDs' (id and externalId) association in the CRM
*
* @param array $ids
* @return RetailcrmApiResponse
*/
public function customersFixExternalIds(array $ids)
{
if (!sizeof($ids)) {
throw new InvalidArgumentException('Method parameter must contains at least one IDs pair');
}
return $this->client->makeRequest("/customers/fix-external-ids", RetailcrmHttpClient::METHOD_POST, array(
'customers' => json_encode($ids),
));
}
/**
* Get purchace prices & stock balance
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null)
{
$parameters = array();
if (sizeof($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest('/store/inventories', RetailcrmHttpClient::METHOD_GET, $this->fillSite($site, $parameters));
}
/**
* Upload store inventories
*
* @param array $offers
* @param string $site (default: null)
* @return RetailcrmApiResponse
*/
public function storeInventoriesUpload(array $offers, $site = null)
{
if (!sizeof($offers)) {
throw new InvalidArgumentException('Parameter `offers` must contains array of the customers');
}
return $this->client->makeRequest(
"/store/inventories/upload",
RetailcrmHttpClient::METHOD_POST,
$this->fillSite($site, array('offers' => json_encode($offers)))
);
}
/**
* Returns deliveryServices list
*
* @return RetailcrmApiResponse
*/
public function deliveryServicesList()
{
return $this->client->makeRequest('/reference/delivery-services', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns deliveryTypes list
*
* @return RetailcrmApiResponse
*/
public function deliveryTypesList()
{
return $this->client->makeRequest('/reference/delivery-types', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns orderMethods list
*
* @return RetailcrmApiResponse
*/
public function orderMethodsList()
{
return $this->client->makeRequest('/reference/order-methods', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns orderTypes list
*
* @return RetailcrmApiResponse
*/
public function orderTypesList()
{
return $this->client->makeRequest('/reference/order-types', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns paymentStatuses list
*
* @return RetailcrmApiResponse
*/
public function paymentStatusesList()
{
return $this->client->makeRequest('/reference/payment-statuses', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns paymentTypes list
*
* @return RetailcrmApiResponse
*/
public function paymentTypesList()
{
return $this->client->makeRequest('/reference/payment-types', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns productStatuses list
*
* @return RetailcrmApiResponse
*/
public function productStatusesList()
{
return $this->client->makeRequest('/reference/product-statuses', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns statusGroups list
*
* @return RetailcrmApiResponse
*/
public function statusGroupsList()
{
return $this->client->makeRequest('/reference/status-groups', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns statuses list
*
* @return RetailcrmApiResponse
*/
public function statusesList()
{
return $this->client->makeRequest('/reference/statuses', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns sites list
*
* @return RetailcrmApiResponse
*/
public function sitesList()
{
return $this->client->makeRequest('/reference/sites', RetailcrmHttpClient::METHOD_GET);
}
/**
* Returns stores list
*
* @return RetailcrmApiResponse
*/
public function storesList()
{
return $this->client->makeRequest('/reference/stores', RetailcrmHttpClient::METHOD_GET);
}
/**
* Edit deliveryService
*
* @param array $data delivery service data
* @return RetailcrmApiResponse
*/
public function deliveryServicesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/delivery-services/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'deliveryService' => json_encode($data)
)
);
}
/**
* Edit deliveryType
*
* @param array $data delivery type data
* @return RetailcrmApiResponse
*/
public function deliveryTypesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/delivery-types/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'deliveryType' => json_encode($data)
)
);
}
/**
* Edit orderMethod
*
* @param array $data order method data
* @return RetailcrmApiResponse
*/
public function orderMethodsEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/order-methods/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'orderMethod' => json_encode($data)
)
);
}
/**
* Edit orderType
*
* @param array $data order type data
* @return RetailcrmApiResponse
*/
public function orderTypesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/order-types/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'orderType' => json_encode($data)
)
);
}
/**
* Edit paymentStatus
*
* @param array $data payment status data
* @return RetailcrmApiResponse
*/
public function paymentStatusesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/payment-statuses/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'paymentStatus' => json_encode($data)
)
);
}
/**
* Edit paymentType
*
* @param array $data payment type data
* @return RetailcrmApiResponse
*/
public function paymentTypesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/payment-types/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'paymentType' => json_encode($data)
)
);
}
/**
* Edit productStatus
*
* @param array $data product status data
* @return RetailcrmApiResponse
*/
public function productStatusesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/product-statuses/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'productStatus' => json_encode($data)
)
);
}
/**
* Edit order status
*
* @param array $data status data
* @return RetailcrmApiResponse
*/
public function statusesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/statuses/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'status' => json_encode($data)
)
);
}
/**
* Edit site
*
* @param array $data site data
* @return RetailcrmApiResponse
*/
public function sitesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
return $this->client->makeRequest(
'/reference/sites/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'site' => json_encode($data)
)
);
}
/**
* Edit store
*
* @param array $data site data
* @return RetailcrmApiResponse
*/
public function storesEdit(array $data)
{
if (!isset($data['code'])) {
throw new InvalidArgumentException('Data must contain "code" parameter.');
}
if (!isset($data['name'])) {
throw new InvalidArgumentException('Data must contain "name" parameter.');
}
return $this->client->makeRequest(
'/reference/stores/' . $data['code'] . '/edit',
RetailcrmHttpClient::METHOD_POST,
array(
'store' => json_encode($data)
)
);
}
/**
* Update CRM basic statistic
*
* @return RetailcrmApiResponse
*/
public function statisticUpdate()
{
return $this->client->makeRequest('/statistic/update', RetailcrmHttpClient::METHOD_GET);
}
/**
* Return current site
*
* @return string
*/
public function getSite()
{
return $this->siteCode;
}
/**
* Set site
*
* @param string $site
* @return void
*/
public function setSite($site)
{
$this->siteCode = $site;
}
/**
* Check ID parameter
*
* @param string $by
* @return bool
*/
protected function checkIdParameter($by)
{
$allowedForBy = array('externalId', 'id');
if (!in_array($by, $allowedForBy)) {
throw new InvalidArgumentException(sprintf(
'Value "%s" for parameter "by" is not valid. Allowed values are %s.',
$by,
implode(', ', $allowedForBy)
));
}
return true;
}
/**
* Fill params by site value
*
* @param string $site
* @param array $params
* @return array
*/
protected function fillSite($site, array $params)
{
if ($site) {
$params['site'] = $site;
} elseif ($this->siteCode) {
$params['site'] = $this->siteCode;
}
return $params;
}
}

View File

@ -0,0 +1,122 @@
<?php
/**
* Response from retailCRM API
*/
class RetailcrmApiResponse implements ArrayAccess
{
// HTTP response status code
protected $statusCode;
// response assoc array
protected $response;
public function __construct($statusCode, $responseBody = null)
{
$this->statusCode = (int) $statusCode;
if (!empty($responseBody)) {
$response = json_decode($responseBody, true);
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
throw new InvalidJsonException(
"Invalid JSON in the API response body. Error code #$error",
$error
);
}
$this->response = $response;
}
}
/**
* Return HTTP response status code
*
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* HTTP request was successful
*
* @return bool
*/
public function isSuccessful()
{
return $this->statusCode < 400;
}
/**
* Allow to access for the property throw class method
*
* @param string $name
* @return mixed
*/
public function __call($name, $arguments)
{
// convert getSomeProperty to someProperty
$propertyName = strtolower(substr($name, 3, 1)) . substr($name, 4);
if (!isset($this->response[$propertyName])) {
throw new InvalidArgumentException("Method \"$name\" not found");
}
return $this->response[$propertyName];
}
/**
* Allow to access for the property throw object property
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if (!isset($this->response[$name])) {
throw new InvalidArgumentException("Property \"$name\" not found");
}
return $this->response[$name];
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
throw new BadMethodCallException('This activity not allowed');
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
throw new BadMethodCallException('This call not allowed');
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->response[$offset]);
}
/**
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
if (!isset($this->response[$offset])) {
throw new InvalidArgumentException("Property \"$offset\" not found");
}
return $this->response[$offset];
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* HTTP client
*/
class RetailcrmHttpClient
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
protected $url;
protected $defaultParameters;
protected $retry;
public function __construct($url, array $defaultParameters = array())
{
if (false === stripos($url, 'https://')) {
throw new InvalidArgumentException('API schema requires HTTPS protocol');
}
$this->url = $url;
$this->defaultParameters = $defaultParameters;
$this->retry = 0;
}
/**
* Make HTTP request
*
* @param string $path
* @param string $method (default: 'GET')
* @param array $parameters (default: array())
* @param int $timeout
* @param bool $verify
* @param bool $debug
* @return RetailcrmApiResponse
*/
public function makeRequest(
$path,
$method,
array $parameters = array(),
$timeout = 30,
$verify = false,
$debug = false
) {
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
if (!in_array($method, $allowedMethods)) {
throw new InvalidArgumentException(sprintf(
'Method "%s" is not valid. Allowed methods are %s',
$method,
implode(', ', $allowedMethods)
));
}
$parameters = array_merge($this->defaultParameters, $parameters);
$url = $this->url . $path;
if (self::METHOD_GET === $method && sizeof($parameters)) {
$url .= '?' . http_build_query($parameters, '', '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify);
if (!$debug) {
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT_MS, (int) $timeout + ($this->retry * 2000));
}
if (self::METHOD_POST === $method) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
}
$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) {
$errno = null;
$error = null;
$this->retry += 1;
$this->makeRequest(
$path,
$method,
$parameters,
$timeout,
$verify,
$debug
);
}
if ($errno) {
throw new CurlException($error, $errno);
}
return new RetailcrmApiResponse($statusCode, $responseBody);
}
public function getRetry()
{
return $this->retry;
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Class RequestProxy
* @package RetailCrm\Component
*/
class RetailcrmProxy
{
private $api;
private $log;
public function __construct($url, $key, $log)
{
$this->api = new RetailcrmApiClient($url, $key);
$this->log = $log;
}
public function __call($method, $arguments)
{
try {
$response = call_user_func_array(array($this->api, $method), $arguments);
if (!$response->isSuccessful()) {
error_log("[$method] " . $response->getErrorMsg() . "\n", 3, $this->log);
if (isset($response['errors'])) {
$error = implode("\n", $response['errors']);
error_log($error . "\n", 3, $this->log);
}
$response = false;
}
return $response;
} catch (CurlException $e) {
error_log("[$method] " . $e->getMessage() . "\n", 3, $this->log);
return false;
} catch (InvalidJsonException $e) {
error_log("[$method] " . $e->getMessage() . "\n", 3, $this->log);
return false;
}
}
}

View File

@ -0,0 +1,99 @@
<?php
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Rob Dunham
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Simple Recursive Autoloader
*
* A simple autoloader that loads class files recursively starting in the directory
* where this class resides. Additional options can be provided to control the naming
* convention of the class files.
*
* @package Autoloader
* @license http://opensource.org/licenses/MIT MIT License
* @author Rob Dunham <contact@robunham.info>
* @author Alex Lushpai <lushpai@gmail.com>
*/
class RetailcrmAutoloader
{
/**
* File extension as a string. Defaults to ".php".
*/
protected static $fileExt = '.php';
/**
* The top level directory where recursion will begin.
*
*/
protected static $pathTop;
/**
* Autoload function for registration with spl_autoload_register
*
* Looks recursively through project directory and loads class files based on
* filename match.
*
* @param string $className
*/
public static function loader($className)
{
$directory = new RecursiveDirectoryIterator(self::$pathTop);
$fileIterator = new RecursiveIteratorIterator($directory);
$filename = $className . self::$fileExt;
foreach ($fileIterator as $file) {
if (strtolower($file->getFilename()) === strtolower($filename) && $file->isReadable()) {
include_once $file->getPathname();
}
}
}
/**
* Sets the $fileExt property
*
* @param string $fileExt The file extension used for class files. Default is "php".
*/
public static function setFileExt($fileExt)
{
self::$fileExt = $fileExt;
}
/**
* Sets the $path property
*
* @param string $path The path representing the top level where recursion should
* begin. Defaults to the current directory.
*/
public static function setPath($path)
{
self::$pathTop = $path;
}
}
RetailcrmAutoloader::setPath(realpath(dirname(__FILE__)));
RetailcrmAutoloader::setFileExt('.php');
spl_autoload_register('RetailcrmAutoloader::loader');

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Send order to RetailCRM when it created</id>
<version>1.5.x</version>
<vqmver required="true">2.3.x</vqmver>
<author>retailcrm.ru</author>
<file path="catalog/model/checkout/" name="order.php">
<operation error="skip">
<search position="before" ><![CDATA[return $order_id]]></search>
<add><![CDATA[
$this->load->model('retailcrm/order');
$this->model_retailcrm_order->sendToCrm($data, $order_id);
]]></add>
</operation>
</file>
<file path="admin/model/sale/" name="order.php">
<operation error="skip">
<search position="after" ><![CDATA[$this->db->query("UPDATE `" . DB_PREFIX . "order` SET total = '" . (float)$total . "', affiliate_id = '" . (int)$affiliate_id . "', commission = '" . (float)$commission . "' WHERE order_id = '" . (int)$order_id . "'");]]></search>
<add><![CDATA[
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);
}
}
]]></add>
</operation>
</file>
</modification>