2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ControllerModule
|
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
2017-07-11 17:39:48 +03:00
|
|
|
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
|
2017-03-22 11:52:11 +03:00
|
|
|
*/
|
|
|
|
class ControllerExtensionModuleRetailcrm extends Controller
|
|
|
|
{
|
|
|
|
private $_error = array();
|
2018-01-23 16:34:20 +03:00
|
|
|
protected $log, $statuses, $payments, $deliveryTypes, $retailcrmApiClient;
|
2017-03-22 11:52:11 +03:00
|
|
|
public $children, $template;
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
public function __construct($registry)
|
|
|
|
{
|
|
|
|
parent::__construct($registry);
|
|
|
|
$this->load->library('retailcrm/retailcrm');
|
|
|
|
$this->moduleTitle = $this->retailcrm->getModuleTitle();
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
/**
|
|
|
|
* Install method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function install()
|
|
|
|
{
|
|
|
|
$this->load->model('setting/setting');
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->model_setting_setting
|
2018-01-23 16:34:20 +03:00
|
|
|
->editSetting($this->moduleTitle, array(
|
|
|
|
$this->moduleTitle . '_status' => 1,
|
|
|
|
$this->moduleTitle . '_country' => array($this->config->get('config_country_id'))
|
2017-03-27 17:07:54 +03:00
|
|
|
)
|
|
|
|
);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->addEvents();
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstall method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function uninstall()
|
2018-01-23 16:34:20 +03:00
|
|
|
{
|
2017-05-29 15:48:56 +03:00
|
|
|
$this->uninstall_collector();
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->model_setting_setting
|
2018-01-23 16:34:20 +03:00
|
|
|
->editSetting($this->moduleTitle, array($this->moduleTitle . '_status' => 0));
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->model_setting_setting->deleteSetting('retailcrm_history');
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->deleteEvents();
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
2017-05-29 15:48:56 +03:00
|
|
|
/**
|
|
|
|
* Install Demon Collector method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function install_collector()
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
|
|
|
$collector = $this->getCollectorTitle();
|
|
|
|
$this->loadModels();
|
2017-05-29 15:48:56 +03:00
|
|
|
$this->load->model('setting/setting');
|
2017-09-21 10:09:26 +03:00
|
|
|
$this->{'model_' . $this->modelExtension}->install('analytics', 'daemon_collector');
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->model_setting_setting->editSetting($collector, array($collector . '_status' => 1));
|
2017-05-29 15:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstall Demon Collector method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function uninstall_collector()
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
|
|
|
$collector = $this->getCollectorTitle();
|
|
|
|
$this->loadModels();
|
2017-05-29 15:48:56 +03:00
|
|
|
$this->load->model('setting/setting');
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->model_setting_setting->editSetting($collector, array($collector . '_status' => 0));
|
2017-09-21 10:09:26 +03:00
|
|
|
$this->{'model_' . $this->modelExtension}->uninstall('analytics', 'daemon_collector');
|
2017-05-29 15:48:56 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
/**
|
|
|
|
* Setup page
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function index()
|
2017-05-29 15:48:56 +03:00
|
|
|
{
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->loadModels();
|
2017-03-27 16:09:27 +03:00
|
|
|
$this->load->model('localisation/country');
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->model('extension/retailcrm/references');
|
|
|
|
$this->load->language('extension/module/retailcrm');
|
|
|
|
$this->document->setTitle($this->language->get('heading_title'));
|
|
|
|
$this->document->addStyle('/admin/view/stylesheet/retailcrm.css');
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$tokenTitle = $this->retailcrm->getTokenTitle();
|
2017-07-11 17:39:48 +03:00
|
|
|
$collector = $this->getCollectorTitle();
|
2017-09-15 15:47:38 +03:00
|
|
|
$history_setting = $this->model_setting_setting->getSetting('retailcrm_history');
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($this->checkEvents() === false) {
|
|
|
|
$this->deleteEvents();
|
|
|
|
$this->addEvents();
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
$analytics = $this->{'model_' . $this->modelExtension}->getInstalled('analytics');
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($this->request->post[$this->moduleTitle . '_collector_active'] == 1 &&
|
2017-07-11 17:39:48 +03:00
|
|
|
!in_array($collector, $analytics)) {
|
2017-05-29 15:48:56 +03:00
|
|
|
$this->install_collector();
|
2018-01-23 16:34:20 +03:00
|
|
|
} elseif ($this->request->post[$this->moduleTitle . '_collector_active'] == 0 &&
|
2017-07-11 17:39:48 +03:00
|
|
|
in_array($collector, $analytics)) {
|
2017-05-29 15:48:56 +03:00
|
|
|
$this->uninstall_collector();
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (parse_url($this->request->post[$this->moduleTitle . '_url'])) {
|
|
|
|
$crm_url = parse_url($this->request->post[$this->moduleTitle . '_url'], PHP_URL_HOST);
|
|
|
|
$this->request->post[$this->moduleTitle . '_url'] = 'https://'.$crm_url;
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (isset($this->request->post[$this->moduleTitle . '_custom_field_active']) &&
|
|
|
|
$this->request->post[$this->moduleTitle . '_custom_field_active'] == 0
|
2017-11-17 16:32:18 +03:00
|
|
|
) {
|
2018-01-23 16:34:20 +03:00
|
|
|
unset($this->request->post[$this->moduleTitle . '_custom_field']);
|
2017-09-21 10:09:26 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->model_setting_setting->editSetting(
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->moduleTitle,
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->request->post
|
|
|
|
);
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($this->request->post[$this->moduleTitle . '_apiversion'] != 'v3') {
|
2017-09-15 15:47:38 +03:00
|
|
|
if (!isset($history_setting['retailcrm_history_orders']) && !isset($history_setting['retailcrm_history_customers'])) {
|
2018-01-23 16:34:20 +03:00
|
|
|
$api = $this->retailcrm->getApiClient(
|
|
|
|
$this->request->post[$this->moduleTitle . '_url'],
|
|
|
|
$this->request->post[$this->moduleTitle . '_apikey'],
|
|
|
|
$this->request->post[$this->moduleTitle . '_apiversion']
|
2017-09-15 15:47:38 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$ordersHistory = $api->ordersHistory();
|
|
|
|
|
|
|
|
if ($ordersHistory->isSuccessful()) {
|
|
|
|
$ordersHistory = $api->ordersHistory(array(), $ordersHistory['pagination']['totalPageCount']);
|
|
|
|
|
|
|
|
if ($ordersHistory->isSuccessful()) {
|
2017-11-17 16:32:18 +03:00
|
|
|
$ordersHistoryArr = $ordersHistory['history'];
|
|
|
|
$lastChangeOrders = end($ordersHistoryArr);
|
2017-09-15 15:47:38 +03:00
|
|
|
$sinceIdOrders = $lastChangeOrders['id'];
|
|
|
|
$generatedAt = $ordersHistory['generatedAt'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$customersHistory = $api->customersHistory();
|
|
|
|
|
|
|
|
if ($customersHistory->isSuccessful()) {
|
|
|
|
$customersHistory = $api->customersHistory(array(), $customersHistory['pagination']['totalPageCount']);
|
|
|
|
|
|
|
|
if ($customersHistory->isSuccessful()) {
|
2017-11-17 16:32:18 +03:00
|
|
|
$customersHistoryArr = $customersHistory['history'];
|
|
|
|
$lastChangeCustomers = end($customersHistoryArr);
|
2017-09-15 15:47:38 +03:00
|
|
|
$sinceIdCustomers = $lastChangeCustomers['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->model_setting_setting->editSetting(
|
|
|
|
'retailcrm_history',
|
|
|
|
array(
|
|
|
|
'retailcrm_history_orders' => $sinceIdOrders,
|
|
|
|
'retailcrm_history_customers' => $sinceIdCustomers,
|
|
|
|
'retailcrm_history_datetime' => $generatedAt
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->session->data['success'] = $this->language->get('text_success');
|
|
|
|
$redirect = $this->url->link(
|
2017-07-11 17:39:48 +03:00
|
|
|
'extension/module/retailcrm', $tokenTitle . '=' . $this->session->data[$tokenTitle],
|
2017-03-22 11:52:11 +03:00
|
|
|
'SSL'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->response->redirect($redirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
$text_strings = array(
|
|
|
|
'heading_title',
|
|
|
|
'text_enabled',
|
|
|
|
'text_disabled',
|
|
|
|
'button_save',
|
|
|
|
'button_cancel',
|
|
|
|
'text_notice',
|
|
|
|
'retailcrm_title',
|
2017-07-11 17:39:48 +03:00
|
|
|
'retailcrm_apiversion',
|
2017-03-22 11:52:11 +03:00
|
|
|
'retailcrm_url',
|
|
|
|
'retailcrm_apikey',
|
|
|
|
'retailcrm_base_settings',
|
|
|
|
'retailcrm_dict_settings',
|
|
|
|
'retailcrm_dict_delivery',
|
|
|
|
'retailcrm_dict_status',
|
|
|
|
'retailcrm_dict_payment',
|
2017-04-04 16:53:04 +03:00
|
|
|
'retailcrm_countries_settings',
|
|
|
|
'text_success_export',
|
|
|
|
'text_success_export_order',
|
|
|
|
'text_button_export',
|
|
|
|
'text_button_export_order',
|
|
|
|
'text_button_catalog',
|
2017-04-04 16:56:39 +03:00
|
|
|
'text_success_catalog',
|
2017-04-06 16:40:06 +03:00
|
|
|
'retailcrm_upload_order',
|
|
|
|
'text_error_order',
|
2017-05-29 15:48:56 +03:00
|
|
|
'text_error_order_id',
|
|
|
|
'daemon_collector',
|
|
|
|
'general_tab_text',
|
|
|
|
'references_tab_text',
|
|
|
|
'collector_tab_text',
|
2017-11-17 16:32:18 +03:00
|
|
|
'logs_tab_text',
|
2017-05-29 15:48:56 +03:00
|
|
|
'text_yes',
|
|
|
|
'text_no',
|
2017-05-31 13:08:07 +03:00
|
|
|
'collector_site_key',
|
|
|
|
'text_collector_activity',
|
|
|
|
'text_collector_form_capture',
|
|
|
|
'text_collector_period',
|
|
|
|
'text_label_promo',
|
|
|
|
'text_label_send',
|
|
|
|
'collector_custom_text',
|
2017-09-15 15:47:38 +03:00
|
|
|
'text_require',
|
|
|
|
'custom_fields_tab_text',
|
|
|
|
'text_error_custom_field',
|
|
|
|
'text_error_cf_opencart',
|
|
|
|
'text_error_cf_retailcrm',
|
2017-09-21 10:09:26 +03:00
|
|
|
'retailcrm_dict_custom_fields',
|
|
|
|
'text_payment',
|
|
|
|
'text_shipping',
|
|
|
|
'retailcrm_dict_default',
|
|
|
|
'text_custom_field_activity',
|
|
|
|
'text_orders_custom_fields',
|
2017-11-17 16:32:18 +03:00
|
|
|
'text_customers_custom_fields',
|
2018-01-23 16:34:20 +03:00
|
|
|
'text_confirm_log',
|
|
|
|
'text_error_delivery',
|
|
|
|
'retailcrm_missing_status'
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$_data = &$data;
|
|
|
|
|
|
|
|
foreach ($text_strings as $text) {
|
|
|
|
$_data[$text] = $this->language->get($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_data['retailcrm_errors'] = array();
|
|
|
|
$_data['saved_settings'] = $this->model_setting_setting
|
2018-01-23 16:34:20 +03:00
|
|
|
->getSetting($this->moduleTitle);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$url = isset($_data['saved_settings'][$this->moduleTitle . '_url'])
|
|
|
|
? $_data['saved_settings'][$this->moduleTitle . '_url']
|
2017-03-22 11:52:11 +03:00
|
|
|
: null;
|
2018-01-23 16:34:20 +03:00
|
|
|
$key = isset($_data['saved_settings'][$this->moduleTitle . '_apikey'])
|
|
|
|
? $_data['saved_settings'][$this->moduleTitle . '_apikey']
|
2017-07-11 17:39:48 +03:00
|
|
|
: null;
|
2018-01-23 16:34:20 +03:00
|
|
|
$apiVersion = isset($_data['saved_settings'][$this->moduleTitle . '_apiversion'])
|
|
|
|
? $_data['saved_settings'][$this->moduleTitle . '_apiversion']
|
2017-03-22 11:52:11 +03:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!empty($url) && !empty($key)) {
|
|
|
|
|
|
|
|
$_data['delivery'] = $this->model_extension_retailcrm_references
|
|
|
|
->getDeliveryTypes();
|
|
|
|
$_data['statuses'] = $this->model_extension_retailcrm_references
|
|
|
|
->getOrderStatuses();
|
|
|
|
$_data['payments'] = $this->model_extension_retailcrm_references
|
|
|
|
->getPaymentTypes();
|
2017-09-15 15:47:38 +03:00
|
|
|
|
|
|
|
if ($apiVersion == 'v5') {
|
|
|
|
$_data['customFields'] = $this->model_extension_retailcrm_references
|
|
|
|
->getCustomFields();
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$config_data = array(
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->moduleTitle . '_status'
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($config_data as $conf) {
|
|
|
|
if (isset($this->request->post[$conf])) {
|
|
|
|
$_data[$conf] = $this->request->post[$conf];
|
|
|
|
} else {
|
|
|
|
$_data[$conf] = $this->config->get($conf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:08:07 +03:00
|
|
|
if (isset($this->_error['warning'])) {
|
|
|
|
$_data['error_warning'] = $this->_error['warning'];
|
|
|
|
} else {
|
|
|
|
$_data['error_warning'] = '';
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$_data['breadcrumbs'] = array();
|
|
|
|
|
|
|
|
$_data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('text_home'),
|
|
|
|
'href' => $this->url->link(
|
|
|
|
'common/home',
|
2017-07-11 17:39:48 +03:00
|
|
|
$tokenTitle . '=' . $this->session->data[$tokenTitle], 'SSL'
|
2017-03-22 11:52:11 +03:00
|
|
|
),
|
|
|
|
'separator' => false
|
|
|
|
);
|
|
|
|
|
|
|
|
$_data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('text_module'),
|
|
|
|
'href' => $this->url->link(
|
|
|
|
'extension/extension/module',
|
2017-07-11 17:39:48 +03:00
|
|
|
$tokenTitle . '=' . $this->session->data[$tokenTitle], 'SSL'
|
2017-03-22 11:52:11 +03:00
|
|
|
),
|
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
|
|
|
$_data['breadcrumbs'][] = array(
|
|
|
|
'text' => $this->language->get('retailcrm_title'),
|
|
|
|
'href' => $this->url->link(
|
|
|
|
'extension/module/retailcrm',
|
2017-07-11 17:39:48 +03:00
|
|
|
$tokenTitle . '=' . $this->session->data[$tokenTitle], 'SSL'
|
2017-03-22 11:52:11 +03:00
|
|
|
),
|
|
|
|
'separator' => ' :: '
|
|
|
|
);
|
|
|
|
|
|
|
|
$_data['action'] = $this->url->link(
|
|
|
|
'extension/module/retailcrm',
|
2017-07-11 17:39:48 +03:00
|
|
|
$tokenTitle . '=' . $this->session->data[$tokenTitle], 'SSL'
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$_data['cancel'] = $this->url->link(
|
2017-07-11 17:39:48 +03:00
|
|
|
version_compare(VERSION, '3.0', '<') ? 'extension/extension' : 'marketplace/extension',
|
|
|
|
$tokenTitle . '=' . $this->session->data[$tokenTitle], 'SSL'
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$_data['modules'] = array();
|
|
|
|
|
|
|
|
if (isset($this->request->post['retailcrm_module'])) {
|
|
|
|
$_data['modules'] = $this->request->post['retailcrm_module'];
|
|
|
|
} elseif ($this->config->get('retailcrm_module')) {
|
|
|
|
$_data['modules'] = $this->config->get('retailcrm_module');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('design/layout');
|
|
|
|
$_data['layouts'] = $this->model_design_layout->getLayouts();
|
|
|
|
|
|
|
|
$_data['header'] = $this->load->controller('common/header');
|
|
|
|
$_data['column_left'] = $this->load->controller('common/column_left');
|
|
|
|
$_data['footer'] = $this->load->controller('common/footer');
|
2017-03-27 16:09:27 +03:00
|
|
|
$_data['countries'] = $this->model_localisation_country->getCountries();
|
2017-04-04 16:45:39 +03:00
|
|
|
$_data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
|
2017-07-11 17:39:48 +03:00
|
|
|
$_data[$tokenTitle] = $this->request->get[$tokenTitle];
|
2017-04-04 16:53:04 +03:00
|
|
|
|
2017-05-29 15:48:56 +03:00
|
|
|
if(file_exists(DIR_SYSTEM . '/cron/export_done')) {
|
2017-04-07 11:58:04 +03:00
|
|
|
$_data['export_file'] = false;
|
|
|
|
} else {
|
|
|
|
$_data['export_file'] = true;
|
|
|
|
}
|
2017-11-17 16:32:18 +03:00
|
|
|
|
2017-05-31 13:08:07 +03:00
|
|
|
$collectorFields = array(
|
|
|
|
'name' => $this->language->get('field_name'),
|
|
|
|
'email' => $this->language->get('field_email'),
|
|
|
|
'phone' => $this->language->get('field_phone')
|
|
|
|
);
|
|
|
|
|
|
|
|
$_data['collectorFields'] = $collectorFields;
|
2017-07-11 17:39:48 +03:00
|
|
|
$_data['api_versions'] = array('v3', 'v4', 'v5');
|
2017-07-21 10:27:08 +03:00
|
|
|
$_data['default_apiversion'] = 'v4';
|
2017-11-17 16:32:18 +03:00
|
|
|
|
|
|
|
$retailcrmLog = file_exists(DIR_SYSTEM . 'storage/logs/retailcrm.log') ? DIR_SYSTEM . 'storage/logs/retailcrm.log' : false;
|
|
|
|
$ocApiLog = file_exists(DIR_SYSTEM . 'storage/logs/opencartapi.log') ? DIR_SYSTEM . 'storage/logs/opencartapi.log' : false;
|
|
|
|
|
|
|
|
if ($this->checkLogFile($retailcrmLog) !== false) {
|
|
|
|
$_data['logs']['retailcrm_log'] = $this->checkLogFile($retailcrmLog);
|
|
|
|
} else {
|
|
|
|
$_data['logs']['retailcrm_error'] = $this->language->get('text_error_log');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->checkLogFile($ocApiLog) !== false) {
|
|
|
|
$_data['logs']['oc_api_log'] = $this->checkLogFile($ocApiLog);
|
|
|
|
} else {
|
|
|
|
$_data['logs']['oc_error'] = $this->language->get('text_error_log');
|
|
|
|
}
|
|
|
|
|
|
|
|
$_data['clear_retailcrm'] = $this->url->link('extension/module/retailcrm/clear_retailcrm', $tokenTitle . '=' . $this->session->data[$tokenTitle], true);
|
|
|
|
$_data['clear_opencart'] = $this->url->link('extension/module/retailcrm/clear_opencart', $tokenTitle . '=' . $this->session->data[$tokenTitle], true);
|
|
|
|
$_data['button_clear'] = $this->language->get('button_clear');
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->response->setOutput(
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->load->view('extension/module/retailcrm', $_data)
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
2017-11-17 16:32:18 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function history()
|
2018-01-23 16:34:20 +03:00
|
|
|
{
|
2017-07-11 17:39:48 +03:00
|
|
|
$this->load->model('setting/setting');
|
2018-01-23 16:34:20 +03:00
|
|
|
$settings = $this->model_setting_setting->getSetting($this->moduleTitle);
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($settings[$this->moduleTitle . '_apiversion'] == 'v3') {
|
2017-07-11 17:39:48 +03:00
|
|
|
if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/history/v3.php')) {
|
|
|
|
$this->load->model('extension/retailcrm/custom/history/v3');
|
|
|
|
$this->model_extension_retailcrm_custom_history_v3->request();
|
|
|
|
} else {
|
|
|
|
$this->load->model('extension/retailcrm/history/v3');
|
|
|
|
$this->model_extension_retailcrm_history_v3->request();
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
} else {
|
2017-07-11 17:39:48 +03:00
|
|
|
if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/history/v4-5.php')) {
|
|
|
|
$this->load->model('extension/retailcrm/custom/history/v4-5');
|
|
|
|
$this->model_extension_retailcrm_custom_history_v4_5->request();
|
|
|
|
} else {
|
|
|
|
$this->load->model('extension/retailcrm/history/v4_5');
|
|
|
|
$this->model_extension_retailcrm_history_v4_5->request();
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ICML generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function icml()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
} else {
|
|
|
|
$this->load->model('extension/retailcrm/icml');
|
|
|
|
$this->model_extension_retailcrm_icml->generateICML();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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']];
|
|
|
|
|
|
|
|
$this->load->model('extension/retailcrm/order');
|
|
|
|
$this->model_extension_retailcrm_order->sendToCrm($data, $data['order_id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:29:28 +03:00
|
|
|
/**
|
|
|
|
* Update customer on event
|
|
|
|
*
|
|
|
|
* @param int $customer_id customer identificator
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function customer_edit($route, $customer)
|
|
|
|
{
|
|
|
|
$this->load->model('localisation/country');
|
|
|
|
$this->load->model('localisation/zone');
|
|
|
|
$this->load->model('customer/customer');
|
|
|
|
|
|
|
|
$customerId = $customer[0];
|
|
|
|
$customer = $customer[1];
|
|
|
|
$addresses = $customer['address'];
|
|
|
|
unset($customer);
|
|
|
|
|
|
|
|
$customer = $this->model_customer_customer->getCustomer($customerId);
|
|
|
|
|
|
|
|
foreach ($addresses as $address) {
|
|
|
|
$country = $this->model_localisation_country->getCountry($address['country_id']);
|
|
|
|
$zone = $this->model_localisation_zone->getZone($address['zone_id']);
|
|
|
|
|
|
|
|
$customer['address'] = array(
|
|
|
|
'address_1' => $address['address_1'],
|
|
|
|
'address_2' => $address['address_2'],
|
|
|
|
'city' => $address['city'],
|
|
|
|
'postcode' => $address['postcode'],
|
|
|
|
'iso_code_2' => $country['iso_code_2'],
|
|
|
|
'zone' => $zone['name']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('extension/retailcrm/customer');
|
|
|
|
$this->model_extension_retailcrm_customer->changeInCrm($customer);
|
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-04-04 16:45:39 +03:00
|
|
|
/**
|
|
|
|
* Export single order
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function exportOrder()
|
2017-09-15 15:47:38 +03:00
|
|
|
{
|
2017-04-04 16:45:39 +03:00
|
|
|
$order_id = isset($this->request->get['order_id']) ? $this->request->get['order_id'] : '';
|
|
|
|
$this->load->model('sale/order');
|
|
|
|
|
|
|
|
$data = $this->model_sale_order->getOrder($order_id);
|
|
|
|
$data['products'] = $this->model_sale_order->getOrderProducts($order_id);
|
|
|
|
$data['totals'] = $this->model_sale_order->getOrderTotals($order_id);
|
|
|
|
|
|
|
|
if (!isset($data['fromApi'])) {
|
|
|
|
$this->load->model('setting/setting');
|
2018-01-23 16:34:20 +03:00
|
|
|
$status = $this->model_setting_setting->getSetting($this->moduleTitle);
|
|
|
|
$data['order_status'] = $status[$this->moduleTitle . '_status'][$data['order_status_id']];
|
2017-04-04 16:45:39 +03:00
|
|
|
|
|
|
|
$this->load->model('extension/retailcrm/order');
|
2017-10-30 16:49:49 +03:00
|
|
|
$response = $this->model_extension_retailcrm_order->uploadOrder($data);
|
2017-04-04 16:45:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 16:49:49 +03:00
|
|
|
if (!$response->isSuccessful()) {
|
|
|
|
if (isset($response['errors'])) {
|
|
|
|
$error = implode("\n", $response['errors']);
|
|
|
|
} else {
|
|
|
|
$error = $response->getErrorMsg();
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->response->setOutput(
|
|
|
|
json_encode(
|
|
|
|
array(
|
|
|
|
'status_code' => $response->getStatusCode(),
|
|
|
|
'error_msg' => $error
|
|
|
|
)
|
2017-10-30 16:49:49 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->response->setOutPut(
|
|
|
|
json_encode(
|
|
|
|
array(
|
|
|
|
'status_code' => $response->getStatusCode()
|
|
|
|
)
|
2017-10-30 16:49:49 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-04-04 16:45:39 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
/**
|
|
|
|
* Export orders
|
|
|
|
*
|
2017-11-17 16:32:18 +03:00
|
|
|
* @return void
|
2017-03-22 11:52:11 +03:00
|
|
|
*/
|
|
|
|
public function export() {
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->load->model('customer/customer');
|
|
|
|
$customers = $this->model_customer_customer->getCustomers();
|
|
|
|
|
|
|
|
$this->load->model('extension/retailcrm/customer');
|
|
|
|
$this->model_extension_retailcrm_customer->uploadToCrm($customers);
|
|
|
|
|
|
|
|
$this->load->model('sale/order');
|
|
|
|
$orders = $this->model_sale_order->getOrders();
|
|
|
|
|
|
|
|
$fullOrders = array();
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
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']);
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
foreach($fullOrder['products'] as $key=>$product) {
|
|
|
|
$fullOrder['products'][$key]['option'] = $this->model_sale_order->getOrderOptions($product['order_id'], $product['order_product_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fullOrders[] = $fullOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('extension/retailcrm/order');
|
|
|
|
$this->model_extension_retailcrm_order->uploadToCrm($fullOrders);
|
2018-01-23 16:34:20 +03:00
|
|
|
fopen(DIR_SYSTEM . '/cron/export_done', "x");
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function validate()
|
|
|
|
{
|
2017-09-15 15:47:38 +03:00
|
|
|
$versionsMap = array(
|
|
|
|
'v3' => '3.0',
|
|
|
|
'v4' => '4.0',
|
|
|
|
'v5' => '5.0'
|
|
|
|
);
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (!empty($this->request->post[$this->moduleTitle . '_url']) && !empty($this->request->post[$this->moduleTitle . '_apikey'])) {
|
|
|
|
$apiClient = $this->retailcrm->getApiClient(
|
|
|
|
$this->request->post[$this->moduleTitle . '_url'],
|
|
|
|
$this->request->post[$this->moduleTitle . '_apikey']
|
2017-07-21 10:27:08 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$response = $apiClient->apiVersions();
|
2017-07-21 10:27:08 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if ($response && $response->isSuccessful()) {
|
2018-01-23 16:34:20 +03:00
|
|
|
if (!in_array($versionsMap[$this->request->post[$this->moduleTitle . '_apiversion']], $response['versions'])) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->_error['warning'] = $this->language->get('text_error_api');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->_error['warning'] = $this->language->get('text_error_save');
|
2017-07-21 10:27:08 +03:00
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
if (!$this->user->hasPermission('modify', 'extension/module/retailcrm')) {
|
|
|
|
$this->_error['warning'] = $this->language->get('error_permission');
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (isset($this->request->post[$this->moduleTitle . '_collector']['custom']) &&
|
|
|
|
$this->request->post[$this->moduleTitle . '_collector']['custom_form'] == 1) {
|
|
|
|
$customField = $this->request->post[$this->moduleTitle . '_collector']['custom'];
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-05-31 13:08:07 +03:00
|
|
|
if (empty($customField['name']) && empty($customField['email']) && empty($customField['phone'])) {
|
|
|
|
$this->_error['fields'] = $this->language->get('text_error_collector_fields');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
if (!$this->_error) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
/**
|
|
|
|
* Clear retailcrm log file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clear_retailcrm()
|
|
|
|
{
|
|
|
|
$tokenTitle = $this->getTokenTitle();
|
|
|
|
|
|
|
|
if ($this->user->hasPermission('modify', 'extension/module/retailcrm')) {
|
|
|
|
$file = DIR_LOGS . 'retailcrm.log';
|
|
|
|
|
|
|
|
$handle = fopen($file, 'w+');
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->redirect($this->url->link('extension/module/retailcrm', $tokenTitle . '=' . $this->session->data[$tokenTitle], true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear opencart API log file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clear_opencart()
|
|
|
|
{
|
|
|
|
$tokenTitle = $this->getTokenTitle();
|
|
|
|
|
|
|
|
if ($this->user->hasPermission('modify', 'extension/module/retailcrm')) {
|
|
|
|
$file = DIR_LOGS . 'opencartapi.log';
|
|
|
|
|
|
|
|
$handle = fopen($file, 'w+');
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->redirect($this->url->link('extension/module/retailcrm', $tokenTitle . '=' . $this->session->data[$tokenTitle], true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for load modelds
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
private function loadModels()
|
|
|
|
{
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
|
|
|
$this->load->model('extension/event');
|
|
|
|
$this->load->model('extension/extension');
|
|
|
|
// $this->load->model('extension/module');
|
|
|
|
|
|
|
|
$this->modelEvent = 'extension_event';
|
|
|
|
$this->modelExtension = 'extension_extension';
|
|
|
|
// $this->modelModule = 'extension_module';
|
|
|
|
} else {
|
|
|
|
$this->load->model('setting/event');
|
|
|
|
$this->load->model('setting/extension');
|
|
|
|
// $this->load->model('setting/module');
|
|
|
|
|
|
|
|
$this->modelEvent = 'setting_event';
|
|
|
|
$this->modelExtension = 'setting_extension';
|
|
|
|
// $this->modelModule = 'setting_module';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
/**
|
2018-01-23 16:34:20 +03:00
|
|
|
* Get collector module name
|
2017-11-17 16:32:18 +03:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-23 16:34:20 +03:00
|
|
|
private function getCollectorTitle()
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
2018-01-23 16:34:20 +03:00
|
|
|
$title = 'daemon_collector';
|
2017-07-11 17:39:48 +03:00
|
|
|
} else {
|
2018-01-23 16:34:20 +03:00
|
|
|
$title = 'analytics_daemon_collector';
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
return $title;
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
/**
|
2018-01-23 16:34:20 +03:00
|
|
|
* Check file size
|
2017-11-17 16:32:18 +03:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-23 16:34:20 +03:00
|
|
|
private function checkLogFile($file)
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
$logs = '';
|
|
|
|
|
|
|
|
if ($file === false) {
|
|
|
|
return $logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filesize($file) < 2097152) {
|
|
|
|
$logs .= file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
|
2017-07-11 17:39:48 +03:00
|
|
|
} else {
|
2018-01-23 16:34:20 +03:00
|
|
|
return false;
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
return $logs;
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 16:32:18 +03:00
|
|
|
/**
|
2018-01-23 16:34:20 +03:00
|
|
|
* Add events to db
|
2017-11-17 16:32:18 +03:00
|
|
|
*
|
2018-01-23 16:34:20 +03:00
|
|
|
* @return void
|
2017-11-17 16:32:18 +03:00
|
|
|
*/
|
2018-01-23 16:34:20 +03:00
|
|
|
private function addEvents()
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->loadModels();
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'catalog/model/checkout/order/addOrder/after',
|
|
|
|
'extension/module/retailcrm/order_create'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'catalog/model/checkout/order/addOrderHistory/after',
|
|
|
|
'extension/module/retailcrm/order_edit'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'catalog/model/account/customer/addCustomer/after',
|
|
|
|
'extension/module/retailcrm/customer_create'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'catalog/model/account/customer/editCustomer/after',
|
|
|
|
'extension/module/retailcrm/customer_edit'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'catalog/model/account/address/editAddress/after',
|
|
|
|
'extension/module/retailcrm/customer_edit'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->{'model_' . $this->modelEvent}
|
|
|
|
->addEvent(
|
|
|
|
$this->moduleTitle,
|
|
|
|
'admin/model/customer/customer/editCustomer/after',
|
|
|
|
'extension/module/retailcrm/customer_edit'
|
|
|
|
);
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-11-17 16:32:18 +03:00
|
|
|
|
|
|
|
/**
|
2018-01-23 16:34:20 +03:00
|
|
|
* Check events in db
|
2017-11-17 16:32:18 +03:00
|
|
|
*
|
2018-01-23 16:34:20 +03:00
|
|
|
* @return boolean
|
2017-11-17 16:32:18 +03:00
|
|
|
*/
|
2018-01-23 16:34:20 +03:00
|
|
|
private function checkEvents()
|
2017-11-17 16:32:18 +03:00
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
$events = $this->{'model_' . $this->modelEvent}->getEvent(
|
|
|
|
'retailcrm',
|
|
|
|
'catalog/model/checkout/order/addOrder/after',
|
|
|
|
'extension/module/retailcrm/order_create'
|
|
|
|
);
|
2017-11-17 16:32:18 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (!empty($events)) {
|
|
|
|
return true;
|
2017-11-17 16:32:18 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete events from db
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function deleteEvents()
|
|
|
|
{
|
|
|
|
$this->loadModels();
|
|
|
|
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
|
|
|
$this->{'model_' . $this->modelEvent}->deleteEvent($this->moduleTitle);
|
2017-11-17 16:32:18 +03:00
|
|
|
} else {
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->{'model_' . $this->modelEvent}->deleteEventByCode($this->moduleTitle);
|
2017-11-17 16:32:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|