diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index 610d636..121a2ee 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -272,12 +272,6 @@ class ControllerExtensionModuleRetailcrm extends Controller $_data['error_warning'] = ''; } - if (isset($this->_error['fields'])) { - $_data['error_warning'] = $this->_error['fields']; - } else { - $_data['error_warning'] = ''; - } - $_data['breadcrumbs'] = array(); $_data['breadcrumbs'][] = array( @@ -349,7 +343,7 @@ class ControllerExtensionModuleRetailcrm extends Controller $_data['collectorFields'] = $collectorFields; $_data['api_versions'] = array('v3', 'v4', 'v5'); - $_data['default_apiversion'] = 'v5'; + $_data['default_apiversion'] = 'v4'; $this->response->setOutput( $this->load->view('extension/module/retailcrm', $_data) @@ -540,6 +534,22 @@ class ControllerExtensionModuleRetailcrm extends Controller { $moduleTitle = $this->getModuleTitle(); + if (!empty($this->request->post[$moduleTitle . '_url']) && !empty($this->request->post[$moduleTitle . '_apikey'])) { + + $this->retailcrm = new RetailcrmProxy( + $this->request->post[$moduleTitle . '_url'], + $this->request->post[$moduleTitle . '_apikey'], + DIR_SYSTEM . 'storage/logs/retailcrm.log', + $this->request->post[$moduleTitle . '_apiversion'] + ); + } + + $response = $this->retailcrm->statisticUpdate(); + + if (!$response) { + $this->_error['warning'] = $this->language->get('text_error_api'); + } + if (!$this->user->hasPermission('modify', 'extension/module/retailcrm')) { $this->_error['warning'] = $this->language->get('error_permission'); } diff --git a/admin/language/en-gb/extension/module/retailcrm.php b/admin/language/en-gb/extension/module/retailcrm.php index 67eeac7..1d59139 100644 --- a/admin/language/en-gb/extension/module/retailcrm.php +++ b/admin/language/en-gb/extension/module/retailcrm.php @@ -41,6 +41,7 @@ $_['field_email'] = 'Email'; $_['field_phone'] = 'Phone'; $_['text_require'] = 'Require'; $_['text_error_collector_fields']= 'Fill in the field names Daemon Collector'; +$_['text_error_api'] = 'The selected version of the API is unavailable'; $_['retailcrm_dict_delivery'] = 'Shipment methods'; $_['retailcrm_dict_status'] = 'Order statuses'; diff --git a/admin/language/ru-ru/extension/module/retailcrm.php b/admin/language/ru-ru/extension/module/retailcrm.php index ef726e7..1e6c044 100644 --- a/admin/language/ru-ru/extension/module/retailcrm.php +++ b/admin/language/ru-ru/extension/module/retailcrm.php @@ -41,6 +41,7 @@ $_['field_email'] = 'Email'; $_['field_phone'] = 'Телефон'; $_['text_require'] = 'Обязательно для заполнения'; $_['text_error_collector_fields']= 'Заполните названия полей формы Демон Collector'; +$_['text_error_api'] = 'Недоступна выбранная версия API'; $_['retailcrm_dict_delivery'] = 'Способы доставки'; $_['retailcrm_dict_status'] = 'Статусы'; diff --git a/admin/model/extension/retailcrm/history/v4_5.php b/admin/model/extension/retailcrm/history/v4_5.php index 7b936b1..6c56b07 100644 --- a/admin/model/extension/retailcrm/history/v4_5.php +++ b/admin/model/extension/retailcrm/history/v4_5.php @@ -60,9 +60,10 @@ class ModelExtensionRetailcrmHistoryV45 extends Model $generatedAt = $packsOrders['generatedAt']; - $this->subtotalSettings = $this->model_setting_setting->getSetting('sub_total'); - $this->totalSettings = $this->model_setting_setting->getSetting('total'); - $this->shippingSettings = $this->model_setting_setting->getSetting('shipping'); + $this->totalTitle = $this->totalTitles(); + $this->subtotalSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'sub_total'); + $this->totalSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'total'); + $this->shippingSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'shipping'); $this->delivery = array_flip($settings[$moduleTitle . '_delivery']); $this->payment = array_flip($settings[$moduleTitle . '_payment']); @@ -141,7 +142,7 @@ class ModelExtensionRetailcrmHistoryV45 extends Model foreach ($orders as $order) { $store = $this->config->get('config_store_id'); - if ($order['payments']) { + if (isset($order['payments'])) { foreach ($order['payments'] as $orderPayment) { if (isset($orderPayment['externalId'])) { $payment = $orderPayment; @@ -151,6 +152,8 @@ class ModelExtensionRetailcrmHistoryV45 extends Model if (!isset($payment) && count($order['payments']) == 1) { $payment = end($order['payments']); } + } elseif (isset($order['paymentType'])) { + $payment = $order['paymentType']; } $data = array(); @@ -296,7 +299,7 @@ class ModelExtensionRetailcrmHistoryV45 extends Model 'title' => $this->ocDelivery[$data['shipping_code']], 'value' => $deliveryCost, 'text' => $deliveryCost, - 'sort_order' => $this->shippingSettings['shipping_sort_order'] + 'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order'] ), array( 'order_total_id' => '', @@ -304,7 +307,7 @@ class ModelExtensionRetailcrmHistoryV45 extends Model 'title' => $this->language->get('column_total'), 'value' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, 'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, - 'sort_order' => $this->totalSettings['total_sort_order'] + 'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order'] ) ); @@ -329,8 +332,10 @@ class ModelExtensionRetailcrmHistoryV45 extends Model foreach ($orders as $order) { $store = $this->config->get('config_store_id'); - if ($order['payments']) { + if (isset($order['payments'])) { $payment = end($order['payments']); + } elseif (isset($order['paymentType'])) { + $payment = $order['paymentType']; } $customer_id = (!empty($order['customer']['externalId'])) @@ -489,7 +494,7 @@ class ModelExtensionRetailcrmHistoryV45 extends Model 'title' => $this->ocDelivery[$data['shipping_code']], 'value' => $deliveryCost, 'text' => $deliveryCost, - 'sort_order' => $this->shippingSettings['shipping_sort_order'] + 'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order'] ), array( 'order_total_id' => '', @@ -497,7 +502,7 @@ class ModelExtensionRetailcrmHistoryV45 extends Model 'title' => $this->language->get('column_total'), 'value' => !empty($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, 'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost, - 'sort_order' => $this->totalSettings['total_sort_order'] + 'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order'] ) ); @@ -549,4 +554,15 @@ class ModelExtensionRetailcrmHistoryV45 extends Model return $title; } + + private function totalTitles() + { + if (version_compare(VERSION, '3.0', '<')) { + $title = ''; + } else { + $title = 'total_'; + } + + return $title; + } } diff --git a/admin/model/extension/retailcrm/order.php b/admin/model/extension/retailcrm/order.php index f6e0e3f..a097a04 100644 --- a/admin/model/extension/retailcrm/order.php +++ b/admin/model/extension/retailcrm/order.php @@ -47,7 +47,7 @@ class ModelExtensionRetailcrmOrder extends Model { $result = $this->retailcrm->ordersCreate($order); - if ($this->settings[$moduleTitle . '_apiversion'] == 'v5' && $result->isSuccessful()) { + if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $result->isSuccessful()) { $this->createPayment($order_data, $order_data['order_id']); } @@ -57,7 +57,7 @@ class ModelExtensionRetailcrmOrder extends Model { private function process($order_data) { $order = array(); - $moduleTitle = $this->getModuleTitle(); + $this->moduleTitle = $this->getModuleTitle(); $payment_code = $order_data['payment_code']; $delivery_code = $order_data['shipping_code']; @@ -84,14 +84,14 @@ class ModelExtensionRetailcrmOrder extends Model { $order['createdAt'] = $order_data['date_added']; - if ($this->settings[$moduleTitle . '_apiversion'] != 'v5') { - $order['paymentType'] = $this->settings[$moduleTitle . '_payment'][$payment_code]; + if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { + $order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code]; } $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; $order['delivery'] = array( - 'code' => !empty($delivery_code) ? $this->settings[$moduleTitle . '_delivery'][$delivery_code] : '', + 'code' => !empty($delivery_code) ? $this->settings[$this->moduleTitle . '_delivery'][$delivery_code] : '', 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], @@ -149,7 +149,7 @@ class ModelExtensionRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } - if ($this->settings[$moduleTitle . '_apiversion'] != 'v3') { + if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v3') { $item = array( 'offer' => array( 'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'] @@ -176,8 +176,7 @@ class ModelExtensionRetailcrmOrder extends Model { protected function createPayment($order, $order_id) { - $moduleTitle = $this->getModuleTitle(); - $settings = $this->model_setting_setting->getSetting($moduleTitle); + $this->moduleTitle = $this->getModuleTitle(); if (version_compare(VERSION, '3.0', '<')) { $settingPaid = $this->model_setting_setting->getSetting($order['payment_code']); @@ -193,7 +192,7 @@ class ModelExtensionRetailcrmOrder extends Model { $payment = array( 'externalId' => $order_id, - 'type' => $settings[$moduleTitle . '_payment'][$payment_code], + 'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code], 'amount' => $amount ); diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index 45cd15a..f3623bf 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -8,7 +8,7 @@ class ModelExtensionRetailcrmOrder extends Model { $this->initApi(); - $order = array(); + $order = $this->processOrder($order_data, $order_id); $customers = $this->retailcrm->customersList( array( @@ -27,10 +27,9 @@ class ModelExtensionRetailcrmOrder extends Model { unset($customers); - $order = $this->processOrder($order_data, $order_id); $response = $this->retailcrm->ordersCreate($order); - if ($settings[$moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { + if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { $this->createPayment($order_data, $order_id); } } @@ -41,23 +40,38 @@ class ModelExtensionRetailcrmOrder extends Model { $this->initApi(); - $order = array(); - $order = $this->processOrder($order_data, $order_id); + $customers = $this->retailcrm->customersList( + array( + 'name' => $order_data['telephone'], + 'email' => $order_data['email'] + ), + 1, + 100 + ); + + if($customers) { + foreach ($customers['customers'] as $customer) { + $order['customer']['id'] = $customer['id']; + } + } + + unset($customers); + $response = $this->retailcrm->ordersEdit($order); - if ($settings[$moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { + if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { $this->editPayment($order_data, $order_id); } } protected function processOrder($order_data, $order_id) { - $moduleTitle = $this->getModuleTitle(); + $this->moduleTitle = $this->getModuleTitle(); $this->load->model('setting/setting'); $this->load->model('catalog/product'); - $settings = $this->model_setting_setting->getSetting($moduleTitle); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); if (version_compare(VERSION, '3.0', '<')) { $settingPaid = $this->model_setting_setting->getSetting($order_data['payment_code']); @@ -92,20 +106,20 @@ class ModelExtensionRetailcrmOrder extends Model { if (isset($couponTotal)) $order['discount'] = $couponTotal; $order['createdAt'] = $order_data['date_added']; - if ($settings[$moduleTitle . '_apiversion'] != 'v5') { - $order['paymentType'] = $settings[$moduleTitle . '_payment'][$payment_code]; + if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { + $order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code]; } $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - if(isset($settings[$moduleTitle . '_delivery'][$order_data['shipping_code']])) { + if(isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) { $delivery_code = $order_data['shipping_code']; } else { $delivery_code = stristr($order_data['shipping_code'], '.', TRUE); } $order['delivery'] = array( - 'code' => !empty($delivery_code) ? $settings[$moduleTitle . '_delivery'][$delivery_code] : '', + 'code' => !empty($delivery_code) ? $this->settings[$this->moduleTitle . '_delivery'][$delivery_code] : '', 'address' => array( 'index' => $order_data['shipping_postcode'], 'city' => $order_data['shipping_city'], @@ -166,7 +180,7 @@ class ModelExtensionRetailcrmOrder extends Model { $offerId = implode('_', $offerId); } - if ($settings[$moduleTitle . '_apiversion'] != 'v3') { + if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v3') { $item = array( 'offer' => array( 'externalId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'] @@ -189,10 +203,10 @@ class ModelExtensionRetailcrmOrder extends Model { $order['items'][] = $item; if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { - $order['status'] = $settings[$moduleTitle . '_status'][$order_data['order_status_id']]; + $order['status'] = $this->settings[$this->moduleTitle . '_status'][$order_data['order_status_id']]; } - if ($settings[$moduleTitle . '_apiversion'] != 'v5') { + if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { if (version_compare(VERSION, '3.0', '<')) { if ($order_data['order_status_id'] == $settingPaid[$order_data['payment_code'] . '_order_status_id']) { $order['paymentStatus'] = 'paid'; @@ -209,10 +223,7 @@ class ModelExtensionRetailcrmOrder extends Model { } protected function createPayment($order, $order_id) - { - $moduleTitle = $this->getModuleTitle(); - $settings = $this->model_setting_setting->getSetting($moduleTitle); - + { if (version_compare(VERSION, '3.0', '<')) { $settingPaid = $this->model_setting_setting->getSetting($order['payment_code']); } else { @@ -227,7 +238,7 @@ class ModelExtensionRetailcrmOrder extends Model { $payment = array( 'externalId' => $order_id, - 'type' => $settings[$moduleTitle . '_payment'][$payment_code], + 'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code], 'amount' => $amount ); @@ -250,9 +261,6 @@ class ModelExtensionRetailcrmOrder extends Model { protected function editPayment($order, $order_id) { - $moduleTitle = $this->getModuleTitle(); - $settings = $this->model_setting_setting->getSetting($moduleTitle); - if (version_compare(VERSION, '3.0', '<')) { $settingPaid = $this->model_setting_setting->getSetting($order['payment_code']); } else { @@ -267,7 +275,7 @@ class ModelExtensionRetailcrmOrder extends Model { $payment = array( 'externalId' => $order_id, - 'type' => $settings[$moduleTitle . '_payment'][$payment_code], + 'type' => $this->settings[$this->moduleTitle . '_payment'][$payment_code], 'amount' => $amount ); diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index 13f9b91..9e8ac65 100644 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -120,12 +120,11 @@ class OpencartApiClient { $apiAnswer = $this->request('login', array(), $api); $this->apiToken = $apiAnswer['token']; } else { - $this->apiToken = $this->session->getID(); - $apiAnswer = $this->request('login', array(), $api); + $this->apiToken = $this->apiLogin(); } - - return $apiAnswer; + if (isset($apiAnswer)) + return $apiAnswer; } public function editOrder($order_id, $data) { @@ -309,4 +308,23 @@ class OpencartApiClient { return curl_exec($curl); } + + private function apiLogin() { + $this->load->model('user/api'); + $registry = new Registry(); + $config = new Config(); + $config->load('default'); + + $api_info = $this->model_user_api->getApi($this->config->get('config_api_id')); + $session = new Session($this->config->get('session_engine'), $this->registry); + $session->start(); + + $this->model_user_api->deleteApiSessionBySessonId($session->getId()); + $this->model_user_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']); + + $session->data['api_id'] = $api_info['api_id']; + $api_token = $session->getId(); + + return $api_token; + } }