2014-08-15 13:35:22 +04:00
|
|
|
<?php
|
|
|
|
|
2016-01-29 17:59:53 +03:00
|
|
|
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* Class ControllerModule
|
2016-03-10 02:07:25 +03:00
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion3
|
2016-02-09 04:22:06 +03:00
|
|
|
*/
|
2017-03-22 12:12:54 +03:00
|
|
|
class ControllerExtensionModuleRetailcrm extends Controller
|
2015-07-23 13:14:23 +03:00
|
|
|
{
|
2016-02-09 04:22:06 +03:00
|
|
|
private $_error = array();
|
2015-07-23 00:27:20 +03:00
|
|
|
protected $log, $statuses, $payments, $deliveryTypes, $retailcrm;
|
|
|
|
public $children, $template;
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* Install method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-07-23 13:14:23 +03:00
|
|
|
public function install()
|
|
|
|
{
|
2014-08-15 13:35:22 +04:00
|
|
|
$this->load->model('setting/setting');
|
2016-02-09 04:22:06 +03:00
|
|
|
$this->model_setting_setting
|
|
|
|
->editSetting('retailcrm', array('retailcrm_status' => 1));
|
2016-03-10 02:07:25 +03:00
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->load->model('extension/event');
|
|
|
|
|
|
|
|
$this->model_extension_event
|
|
|
|
->addEvent(
|
|
|
|
'retailcrm',
|
2017-03-22 12:12:54 +03:00
|
|
|
'catalog/model/checkout/order/addOrder/after',
|
|
|
|
'extension/module/retailcrm/order_create'
|
2016-07-13 17:42:44 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->model_extension_event
|
|
|
|
->addEvent(
|
|
|
|
'retailcrm',
|
2017-03-22 12:12:54 +03:00
|
|
|
'catalog/model/checkout/order/addOrderHistory/after',
|
|
|
|
'extension/module/retailcrm/order_edit'
|
2016-07-13 17:42:44 +03:00
|
|
|
);
|
2016-08-31 17:28:11 +03:00
|
|
|
|
|
|
|
$this->model_extension_event
|
|
|
|
->addEvent(
|
|
|
|
'retailcrm',
|
2017-03-22 12:12:54 +03:00
|
|
|
'catalog/model/account/customer/addCustomer/after',
|
|
|
|
'extension/module/retailcrm/customer_create'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->model_extension_event
|
|
|
|
->addEvent(
|
|
|
|
'retailcrm',
|
|
|
|
'catalog/model/checkout/order/editOrder/after',
|
|
|
|
'extension/module/retailcrm/order_edit'
|
2016-08-31 17:28:11 +03:00
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* Uninstall method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-07-23 13:14:23 +03:00
|
|
|
public function uninstall()
|
|
|
|
{
|
2014-08-15 13:35:22 +04:00
|
|
|
$this->load->model('setting/setting');
|
2016-02-09 04:22:06 +03:00
|
|
|
$this->model_setting_setting
|
|
|
|
->editSetting('retailcrm', array('retailcrm_status' => 0));
|
2016-03-10 02:07:25 +03:00
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->load->model('extension/event');
|
|
|
|
$this->model_extension_event->deleteEvent('retailcrm');
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* Setup page
|
2016-03-10 02:07:25 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2016-02-09 04:22:06 +03:00
|
|
|
*/
|
2015-07-23 13:14:23 +03:00
|
|
|
public function index()
|
|
|
|
{
|
2014-08-15 13:35:22 +04:00
|
|
|
|
|
|
|
$this->load->model('setting/setting');
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->load->model('extension/module');
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/references');
|
|
|
|
$this->load->language('extension/module/retailcrm');
|
2014-08-15 13:35:22 +04:00
|
|
|
$this->document->setTitle($this->language->get('heading_title'));
|
2015-06-26 18:07:36 +03:00
|
|
|
$this->document->addStyle('/admin/view/stylesheet/retailcrm.css');
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-01-29 17:59:53 +03:00
|
|
|
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
|
2017-03-22 12:12:54 +03:00
|
|
|
if (parse_url($this->request->post['retailcrm_url'])){
|
|
|
|
$crm_url = parse_url($this->request->post['retailcrm_url'], PHP_URL_HOST);
|
|
|
|
$this->request->post['retailcrm_url'] = 'https://'.$crm_url;
|
|
|
|
}
|
2016-03-10 02:07:25 +03:00
|
|
|
$this->model_setting_setting->editSetting(
|
|
|
|
'retailcrm',
|
|
|
|
$this->request->post
|
|
|
|
);
|
|
|
|
|
2014-08-15 13:35:22 +04:00
|
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
2016-03-10 02:07:25 +03:00
|
|
|
$redirect = $this->url->link(
|
2017-03-22 12:12:54 +03:00
|
|
|
'extension/module/retailcrm', 'token=' . $this->session->data['token'],
|
2016-03-10 02:07:25 +03:00
|
|
|
'SSL'
|
|
|
|
);
|
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->response->redirect($redirect);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$text_strings = array(
|
|
|
|
'heading_title',
|
|
|
|
'text_enabled',
|
|
|
|
'text_disabled',
|
|
|
|
'button_save',
|
|
|
|
'button_cancel',
|
|
|
|
'text_notice',
|
2016-03-10 02:07:25 +03:00
|
|
|
'retailcrm_title',
|
2015-06-26 18:07:36 +03:00
|
|
|
'retailcrm_url',
|
|
|
|
'retailcrm_apikey',
|
|
|
|
'retailcrm_base_settings',
|
|
|
|
'retailcrm_dict_settings',
|
|
|
|
'retailcrm_dict_delivery',
|
|
|
|
'retailcrm_dict_status',
|
|
|
|
'retailcrm_dict_payment',
|
2014-08-15 13:35:22 +04:00
|
|
|
);
|
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->load->model('extension/extension');
|
|
|
|
$_data = &$data;
|
2016-03-10 02:07:25 +03:00
|
|
|
|
2014-08-15 13:35:22 +04:00
|
|
|
foreach ($text_strings as $text) {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data[$text] = $this->language->get($text);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['retailcrm_errors'] = array();
|
|
|
|
$_data['saved_settings'] = $this->model_setting_setting
|
|
|
|
->getSetting('retailcrm');
|
2016-01-29 17:59:53 +03:00
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$url = isset($_data['saved_settings']['retailcrm_url'])
|
|
|
|
? $_data['saved_settings']['retailcrm_url']
|
2016-02-09 04:22:06 +03:00
|
|
|
: null;
|
2016-03-10 02:07:25 +03:00
|
|
|
$key = isset($_data['saved_settings']['retailcrm_apikey'])
|
|
|
|
? $_data['saved_settings']['retailcrm_apikey']
|
2016-02-09 04:22:06 +03:00
|
|
|
: null;
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-01-29 17:59:53 +03:00
|
|
|
if (!empty($url) && !empty($key)) {
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$this->retailcrm = new RetailcrmProxy(
|
|
|
|
$url,
|
|
|
|
$key,
|
2017-03-22 12:12:54 +03:00
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log'
|
2016-03-10 02:07:25 +03:00
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
$_data['delivery'] = $this->model_extension_retailcrm_references
|
2016-03-10 02:07:25 +03:00
|
|
|
->getDeliveryTypes();
|
2017-03-22 12:12:54 +03:00
|
|
|
$_data['statuses'] = $this->model_extension_retailcrm_references
|
2016-03-10 02:07:25 +03:00
|
|
|
->getOrderStatuses();
|
2017-03-22 12:12:54 +03:00
|
|
|
$_data['payments'] = $this->model_extension_retailcrm_references
|
2016-03-10 02:07:25 +03:00
|
|
|
->getPaymentTypes();
|
2014-08-15 13:35:22 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$config_data = array(
|
2015-06-26 18:07:36 +03:00
|
|
|
'retailcrm_status'
|
2014-08-15 13:35:22 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($config_data as $conf) {
|
|
|
|
if (isset($this->request->post[$conf])) {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data[$conf] = $this->request->post[$conf];
|
2014-08-15 13:35:22 +04:00
|
|
|
} else {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data[$conf] = $this->config->get($conf);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->error['warning'])) {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['error_warning'] = $this->error['warning'];
|
2014-08-15 13:35:22 +04:00
|
|
|
} else {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['error_warning'] = '';
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['breadcrumbs'] = array();
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['breadcrumbs'][] = array(
|
2014-08-15 13:35:22 +04:00
|
|
|
'text' => $this->language->get('text_home'),
|
2015-07-23 13:14:23 +03:00
|
|
|
'href' => $this->url->link(
|
|
|
|
'common/home',
|
|
|
|
'token=' . $this->session->data['token'], 'SSL'
|
|
|
|
),
|
2014-08-15 13:35:22 +04:00
|
|
|
'separator' => false
|
|
|
|
);
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['breadcrumbs'][] = array(
|
2014-08-15 13:35:22 +04:00
|
|
|
'text' => $this->language->get('text_module'),
|
2015-07-23 13:14:23 +03:00
|
|
|
'href' => $this->url->link(
|
2017-03-22 12:12:54 +03:00
|
|
|
'extension/extension/module',
|
2015-07-23 13:14:23 +03:00
|
|
|
'token=' . $this->session->data['token'], 'SSL'
|
|
|
|
),
|
2014-08-15 13:35:22 +04:00
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('retailcrm_title'),
|
2015-07-23 13:14:23 +03:00
|
|
|
'href' => $this->url->link(
|
2017-03-22 12:12:54 +03:00
|
|
|
'extension/module/retailcrm',
|
2015-07-23 13:14:23 +03:00
|
|
|
'token=' . $this->session->data['token'], 'SSL'
|
|
|
|
),
|
2014-08-15 13:35:22 +04:00
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['action'] = $this->url->link(
|
2017-03-22 12:12:54 +03:00
|
|
|
'extension/module/retailcrm',
|
2015-07-23 13:14:23 +03:00
|
|
|
'token=' . $this->session->data['token'], 'SSL'
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['cancel'] = $this->url->link(
|
2017-03-22 12:12:54 +03:00
|
|
|
'extension/extension',
|
2015-07-23 13:14:23 +03:00
|
|
|
'token=' . $this->session->data['token'], 'SSL'
|
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['modules'] = array();
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2015-06-26 18:07:36 +03:00
|
|
|
if (isset($this->request->post['retailcrm_module'])) {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['modules'] = $this->request->post['retailcrm_module'];
|
2015-06-26 18:07:36 +03:00
|
|
|
} elseif ($this->config->get('retailcrm_module')) {
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['modules'] = $this->config->get('retailcrm_module');
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('design/layout');
|
2016-03-10 02:07:25 +03:00
|
|
|
$_data['layouts'] = $this->model_design_layout->getLayouts();
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$_data['header'] = $this->load->controller('common/header');
|
|
|
|
$_data['column_left'] = $this->load->controller('common/column_left');
|
|
|
|
$_data['footer'] = $this->load->controller('common/footer');
|
2014-08-15 13:35:22 +04:00
|
|
|
|
2016-07-13 17:42:44 +03:00
|
|
|
$this->response->setOutput(
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->view('extension/module/retailcrm.tpl', $_data)
|
2016-07-13 17:42:44 +03:00
|
|
|
);
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* History method
|
2016-03-10 02:07:25 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2016-02-09 04:22:06 +03:00
|
|
|
*/
|
2015-06-26 18:07:36 +03:00
|
|
|
public function history()
|
2014-08-18 18:14:04 +04:00
|
|
|
{
|
2017-03-22 12:12:54 +03:00
|
|
|
if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/history.php')) {
|
|
|
|
$this->load->model('extension/retailcrm/custom/history');
|
|
|
|
$this->model_extension_retailcrm_custom_history->request();
|
2015-07-23 13:14:23 +03:00
|
|
|
} else {
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/history');
|
|
|
|
$this->model_extension_retailcrm_history->request();
|
2015-07-23 13:14:23 +03:00
|
|
|
}
|
2014-08-18 18:14:04 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* ICML generation
|
2016-03-10 02:07:25 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2016-02-09 04:22:06 +03:00
|
|
|
*/
|
2015-06-26 18:07:36 +03:00
|
|
|
public function icml()
|
2014-08-27 18:06:19 +04:00
|
|
|
{
|
2017-03-22 12:12:54 +03:00
|
|
|
if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/icml.php')) {
|
|
|
|
$this->load->model('extension/retailcrm/custom/icml');
|
|
|
|
$this->model_extension_retailcrm_custom_icml->generateICML();
|
2015-07-23 13:14:23 +03:00
|
|
|
} else {
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/icml');
|
|
|
|
$this->model_extension_retailcrm_icml->generateICML();
|
2015-07-23 13:14:23 +03:00
|
|
|
}
|
2014-08-19 07:56:17 +04:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:07:25 +03:00
|
|
|
/**
|
|
|
|
* Create order on event
|
|
|
|
*
|
|
|
|
* @param int $order_id order identificator
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function order_create($order_id)
|
|
|
|
{
|
|
|
|
$this->load->model('checkout/order');
|
|
|
|
$this->load->model('account/order');
|
|
|
|
|
|
|
|
$data = $this->model_checkout_order->getOrder($order_id);
|
|
|
|
$data['products'] = $this->model_account_order->getOrderProducts($order_id);
|
|
|
|
|
|
|
|
if (!isset($data['fromApi'])) {
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$status = $this->model_setting_setting->getSetting('retailcrm');
|
|
|
|
$data['order_status'] = $status['retailcrm_status'][$data['order_status_id']];
|
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/order');
|
|
|
|
$this->model_extension_retailcrm_order->sendToCrm($data, $data['order_id']);
|
2016-03-10 02:07:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:28:11 +03:00
|
|
|
/**
|
|
|
|
* Export orders
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function export() {
|
2017-03-22 12:12:54 +03:00
|
|
|
|
|
|
|
$this->load->model('customer/customer');
|
|
|
|
$customers = $this->model_customer_customer->getCustomers();
|
2016-08-31 17:28:11 +03:00
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/customer');
|
|
|
|
$this->model_extension_retailcrm_customer->uploadToCrm($customers);
|
2016-08-31 17:28:11 +03:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2017-03-22 12:12:54 +03:00
|
|
|
$this->load->model('extension/retailcrm/order');
|
|
|
|
$this->model_extension_retailcrm_order->uploadToCrm($fullOrders);
|
2016-08-31 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
/**
|
|
|
|
* Validate
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-23 13:14:23 +03:00
|
|
|
private function validate()
|
|
|
|
{
|
2017-03-22 12:12:54 +03:00
|
|
|
if (!$this->user->hasPermission('modify', 'extension/module/retailcrm')) {
|
2016-02-09 04:22:06 +03:00
|
|
|
$this->_error['warning'] = $this->language->get('error_permission');
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 04:22:06 +03:00
|
|
|
if (!$this->_error) {
|
2016-03-10 02:07:25 +03:00
|
|
|
return true;
|
2014-08-15 13:35:22 +04:00
|
|
|
} else {
|
2016-03-10 02:07:25 +03:00
|
|
|
return false;
|
2014-08-15 13:35:22 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-03 13:03:25 +04:00
|
|
|
}
|