From bf5a5b9e15f107e895f417f333697d26b4fde2c9 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Tue, 23 Jan 2018 16:34:20 +0300 Subject: [PATCH] Transferring order number, added library retailcrm, check events in db, transferring order status for missing order (#65) --- .../controller/extension/module/retailcrm.php | 314 +++++++++--------- .../en-gb/extension/module/retailcrm.php | 3 +- .../ru-ru/extension/module/retailcrm.php | 5 +- admin/model/extension/retailcrm/customer.php | 94 +++--- admin/model/extension/retailcrm/order.php | 144 +++++--- .../model/extension/retailcrm/references.php | 73 ++-- .../template/extension/module/retailcrm.tpl | 51 ++- .../template/extension/module/retailcrm.twig | 13 +- catalog/controller/api/retailcrm.php | 14 +- .../extension/analytics/daemon_collector.php | 14 +- .../controller/extension/module/retailcrm.php | 20 +- .../model/extension/retailcrm/customer.php | 92 ++--- catalog/model/extension/retailcrm/order.php | 146 ++++---- .../library/retailcrm/OpencartApiClient.php | 6 + .../retailcrm/RetailcrmHistoryHelper.php | 16 +- .../library/retailcrm/RetailcrmHttpClient.php | 1 - system/library/retailcrm/retailcrm.php | 92 +++++ 17 files changed, 630 insertions(+), 468 deletions(-) create mode 100644 system/library/retailcrm/retailcrm.php diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index 6c9d75f..7620dd4 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -1,7 +1,5 @@ load->library('retailcrm/retailcrm'); + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + } + /** * Install method * @@ -24,59 +29,16 @@ class ControllerExtensionModuleRetailcrm extends Controller */ public function install() { - $moduleTitle = $this->getModuleTitle(); $this->load->model('setting/setting'); $this->model_setting_setting - ->editSetting($moduleTitle, array( - $moduleTitle . '_status' => 1, - $moduleTitle . '_country' => array($this->config->get('config_country_id')) + ->editSetting($this->moduleTitle, array( + $this->moduleTitle . '_status' => 1, + $this->moduleTitle . '_country' => array($this->config->get('config_country_id')) ) ); - - $this->loadModels(); - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'catalog/model/checkout/order/addOrder/after', - 'extension/module/retailcrm/order_create' - ); - - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'catalog/model/checkout/order/addOrderHistory/after', - 'extension/module/retailcrm/order_edit' - ); - - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'catalog/model/account/customer/addCustomer/after', - 'extension/module/retailcrm/customer_create' - ); - - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'catalog/model/account/customer/editCustomer/after', - 'extension/module/retailcrm/customer_edit' - ); - - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'catalog/model/account/address/editAddress/after', - 'extension/module/retailcrm/customer_edit' - ); - - $this->{'model_' . $this->modelEvent} - ->addEvent( - $moduleTitle, - 'admin/model/customer/customer/editCustomer/after', - 'extension/module/retailcrm/customer_edit' - ); + $this->addEvents(); } /** @@ -85,20 +47,13 @@ class ControllerExtensionModuleRetailcrm extends Controller * @return void */ public function uninstall() - { - $moduleTitle = $this->getModuleTitle(); + { $this->uninstall_collector(); $this->load->model('setting/setting'); $this->model_setting_setting - ->editSetting($moduleTitle, array($moduleTitle . '_status' => 0)); + ->editSetting($this->moduleTitle, array($this->moduleTitle . '_status' => 0)); $this->model_setting_setting->deleteSetting('retailcrm_history'); - $this->loadModels(); - - if (version_compare(VERSION, '3.0', '<')) { - $this->{'model_' . $this->modelEvent}->deleteEvent($moduleTitle); - } else { - $this->{'model_' . $this->modelEvent}->deleteEventByCode($moduleTitle); - } + $this->deleteEvents(); } /** @@ -109,7 +64,6 @@ class ControllerExtensionModuleRetailcrm extends Controller public function install_collector() { $collector = $this->getCollectorTitle(); - $moduleTitle = $this->getModuleTitle(); $this->loadModels(); $this->load->model('setting/setting'); $this->{'model_' . $this->modelExtension}->install('analytics', 'daemon_collector'); @@ -145,45 +99,48 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->document->setTitle($this->language->get('heading_title')); $this->document->addStyle('/admin/view/stylesheet/retailcrm.css'); - $tokenTitle = $this->getTokenTitle(); - $moduleTitle = $this->getModuleTitle(); + $tokenTitle = $this->retailcrm->getTokenTitle(); $collector = $this->getCollectorTitle(); $history_setting = $this->model_setting_setting->getSetting('retailcrm_history'); if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) { + if ($this->checkEvents() === false) { + $this->deleteEvents(); + $this->addEvents(); + } + $analytics = $this->{'model_' . $this->modelExtension}->getInstalled('analytics'); - if ($this->request->post[$moduleTitle . '_collector_active'] == 1 && + if ($this->request->post[$this->moduleTitle . '_collector_active'] == 1 && !in_array($collector, $analytics)) { $this->install_collector(); - } elseif ($this->request->post[$moduleTitle . '_collector_active'] == 0 && + } elseif ($this->request->post[$this->moduleTitle . '_collector_active'] == 0 && in_array($collector, $analytics)) { $this->uninstall_collector(); } - if (parse_url($this->request->post[$moduleTitle . '_url'])) { - $crm_url = parse_url($this->request->post[$moduleTitle . '_url'], PHP_URL_HOST); - $this->request->post[$moduleTitle . '_url'] = 'https://'.$crm_url; + 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; } - if (isset($this->request->post[$moduleTitle . '_custom_field_active']) && - $this->request->post[$moduleTitle . '_custom_field_active'] == 0 + if (isset($this->request->post[$this->moduleTitle . '_custom_field_active']) && + $this->request->post[$this->moduleTitle . '_custom_field_active'] == 0 ) { - unset($this->request->post[$moduleTitle . '_custom_field']); + unset($this->request->post[$this->moduleTitle . '_custom_field']); } $this->model_setting_setting->editSetting( - $moduleTitle, + $this->moduleTitle, $this->request->post ); - if ($this->request->post[$moduleTitle . '_apiversion'] != 'v3') { + if ($this->request->post[$this->moduleTitle . '_apiversion'] != 'v3') { if (!isset($history_setting['retailcrm_history_orders']) && !isset($history_setting['retailcrm_history_customers'])) { - $api = new RetailcrmProxy( - $this->request->post[$moduleTitle . '_url'], - $this->request->post[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $this->request->post[$moduleTitle . '_apiversion'] + $api = $this->retailcrm->getApiClient( + $this->request->post[$this->moduleTitle . '_url'], + $this->request->post[$this->moduleTitle . '_apikey'], + $this->request->post[$this->moduleTitle . '_apiversion'] ); $ordersHistory = $api->ordersHistory(); @@ -283,7 +240,9 @@ class ControllerExtensionModuleRetailcrm extends Controller 'text_custom_field_activity', 'text_orders_custom_fields', 'text_customers_custom_fields', - 'text_confirm_log' + 'text_confirm_log', + 'text_error_delivery', + 'retailcrm_missing_status' ); $_data = &$data; @@ -294,27 +253,20 @@ class ControllerExtensionModuleRetailcrm extends Controller $_data['retailcrm_errors'] = array(); $_data['saved_settings'] = $this->model_setting_setting - ->getSetting($moduleTitle); + ->getSetting($this->moduleTitle); - $url = isset($_data['saved_settings'][$moduleTitle . '_url']) - ? $_data['saved_settings'][$moduleTitle . '_url'] + $url = isset($_data['saved_settings'][$this->moduleTitle . '_url']) + ? $_data['saved_settings'][$this->moduleTitle . '_url'] : null; - $key = isset($_data['saved_settings'][$moduleTitle . '_apikey']) - ? $_data['saved_settings'][$moduleTitle . '_apikey'] + $key = isset($_data['saved_settings'][$this->moduleTitle . '_apikey']) + ? $_data['saved_settings'][$this->moduleTitle . '_apikey'] : null; - $apiVersion = isset($_data['saved_settings'][$moduleTitle . '_apiversion']) - ? $_data['saved_settings'][$moduleTitle . '_apiversion'] + $apiVersion = isset($_data['saved_settings'][$this->moduleTitle . '_apiversion']) + ? $_data['saved_settings'][$this->moduleTitle . '_apiversion'] : null; if (!empty($url) && !empty($key)) { - $this->retailcrm = new RetailcrmProxy( - $url, - $key, - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $apiVersion - ); - $_data['delivery'] = $this->model_extension_retailcrm_references ->getDeliveryTypes(); $_data['statuses'] = $this->model_extension_retailcrm_references @@ -329,7 +281,7 @@ class ControllerExtensionModuleRetailcrm extends Controller } $config_data = array( - $moduleTitle . '_status' + $this->moduleTitle . '_status' ); foreach ($config_data as $conf) { @@ -450,12 +402,11 @@ class ControllerExtensionModuleRetailcrm extends Controller * @return void */ public function history() - { - $moduleTitle = $this->getModuleTitle(); + { $this->load->model('setting/setting'); - $settings = $this->model_setting_setting->getSetting($moduleTitle); + $settings = $this->model_setting_setting->getSetting($this->moduleTitle); - if ($settings[$moduleTitle . '_apiversion'] == 'v3') { + if ($settings[$this->moduleTitle . '_apiversion'] == 'v3') { 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(); @@ -562,7 +513,6 @@ class ControllerExtensionModuleRetailcrm extends Controller { $order_id = isset($this->request->get['order_id']) ? $this->request->get['order_id'] : ''; $this->load->model('sale/order'); - $moduleTitle = $this->getModuleTitle(); $data = $this->model_sale_order->getOrder($order_id); $data['products'] = $this->model_sale_order->getOrderProducts($order_id); @@ -570,8 +520,8 @@ class ControllerExtensionModuleRetailcrm extends Controller if (!isset($data['fromApi'])) { $this->load->model('setting/setting'); - $status = $this->model_setting_setting->getSetting($moduleTitle); - $data['order_status'] = $status[$moduleTitle . '_status'][$data['order_status_id']]; + $status = $this->model_setting_setting->getSetting($this->moduleTitle); + $data['order_status'] = $status[$this->moduleTitle . '_status'][$data['order_status_id']]; $this->load->model('extension/retailcrm/order'); $response = $this->model_extension_retailcrm_order->uploadOrder($data); @@ -584,16 +534,20 @@ class ControllerExtensionModuleRetailcrm extends Controller $error = $response->getErrorMsg(); } - echo json_encode( - array( - 'status_code' => $response->getStatusCode(), - 'error_msg' => $error + $this->response->setOutput( + json_encode( + array( + 'status_code' => $response->getStatusCode(), + 'error_msg' => $error + ) ) ); } else { - echo json_encode( - array( - 'status_code' => $response->getStatusCode() + $this->response->setOutPut( + json_encode( + array( + 'status_code' => $response->getStatusCode() + ) ) ); } @@ -632,7 +586,7 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->load->model('extension/retailcrm/order'); $this->model_extension_retailcrm_order->uploadToCrm($fullOrders); - $file = fopen(DIR_SYSTEM . '/cron/export_done', "x"); + fopen(DIR_SYSTEM . '/cron/export_done', "x"); } /** @@ -642,27 +596,23 @@ class ControllerExtensionModuleRetailcrm extends Controller */ private function validate() { - $moduleTitle = $this->getModuleTitle(); - $versionsMap = array( 'v3' => '3.0', 'v4' => '4.0', 'v5' => '5.0' ); - 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' + 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'] ); } - $response = $this->retailcrm->apiVersions(); + $response = $apiClient->apiVersions(); if ($response && $response->isSuccessful()) { - if (!in_array($versionsMap[$this->request->post[$moduleTitle . '_apiversion']], $response['versions'])) { + if (!in_array($versionsMap[$this->request->post[$this->moduleTitle . '_apiversion']], $response['versions'])) { $this->_error['warning'] = $this->language->get('text_error_api'); } } else { @@ -673,9 +623,9 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->_error['warning'] = $this->language->get('error_permission'); } - if (isset($this->request->post[$moduleTitle . '_collector']['custom']) && - $this->request->post[$moduleTitle . '_collector']['custom_form'] == 1) { - $customField = $this->request->post[$moduleTitle . '_collector']['custom']; + 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']; if (empty($customField['name']) && empty($customField['email']) && empty($customField['phone'])) { $this->_error['fields'] = $this->language->get('text_error_collector_fields'); @@ -755,38 +705,6 @@ class ControllerExtensionModuleRetailcrm extends Controller } } - /** - * Get token param name - * - * @return string - */ - private function getTokenTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $token = 'token'; - } else { - $token = 'user_token'; - } - - return $token; - } - - /** - * Get module name - * - * @return string - */ - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } - /** * Get collector module name * @@ -824,4 +742,92 @@ class ControllerExtensionModuleRetailcrm extends Controller return $logs; } + + /** + * Add events to db + * + * @return void + */ + private function addEvents() + { + $this->loadModels(); + + $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' + ); + } + + /** + * Check events in db + * + * @return boolean + */ + private function checkEvents() + { + $events = $this->{'model_' . $this->modelEvent}->getEvent( + 'retailcrm', + 'catalog/model/checkout/order/addOrder/after', + 'extension/module/retailcrm/order_create' + ); + + if (!empty($events)) { + return true; + } + + 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); + } else { + $this->{'model_' . $this->modelEvent}->deleteEventByCode($this->moduleTitle); + } + } } diff --git a/admin/language/en-gb/extension/module/retailcrm.php b/admin/language/en-gb/extension/module/retailcrm.php index dbfa65d..1542a1c 100644 --- a/admin/language/en-gb/extension/module/retailcrm.php +++ b/admin/language/en-gb/extension/module/retailcrm.php @@ -54,6 +54,7 @@ $_['text_error_cf_opencart'] = 'None custom fields in Opencart'; $_['text_error_cf_retailcrm'] = 'None custom fields in RetailCRM'; $_['text_error_save'] = 'Error saving settings'; $_['text_error_log'] = 'Log size more than 2MB'; +$_['text_error_delivery'] = 'Not found delivery methods'; $_['text_confirm_log'] = 'Are you sure you want to clear the log?'; $_['retailcrm_dict_delivery'] = 'Shipment methods'; @@ -61,6 +62,7 @@ $_['retailcrm_dict_status'] = 'Order statuses'; $_['retailcrm_dict_payment'] = 'Payment methods'; $_['retailcrm_dict_custom_fields'] = 'Configure custom fields'; $_['retailcrm_dict_default'] = 'Default'; +$_['retailcrm_missing_status'] = 'Missing orders status'; $_['column_total'] = 'Total'; $_['product_summ'] = 'Amount'; @@ -74,4 +76,3 @@ $_['text_yes'] = 'Yes'; $_['text_no'] = 'No'; // Errors $_['error_permission'] = 'Warning! You do not have permission to modify module'; - diff --git a/admin/language/ru-ru/extension/module/retailcrm.php b/admin/language/ru-ru/extension/module/retailcrm.php index ec86848..7311b02 100644 --- a/admin/language/ru-ru/extension/module/retailcrm.php +++ b/admin/language/ru-ru/extension/module/retailcrm.php @@ -54,13 +54,15 @@ $_['text_error_cf_opencart'] = 'Отсутствуют пользовате $_['text_error_cf_retailcrm'] = 'Отсутствуют пользовательские поля в RetailCRM'; $_['text_error_save'] = 'Ошибка сохранения настроек'; $_['text_error_log'] = 'Размер лога более 2MB'; +$_['text_error_delivery'] = 'Не найдены типы доставки'; $_['text_confirm_log'] = 'Вы уверены, что хотите очистить лог?'; $_['retailcrm_dict_delivery'] = 'Способы доставки'; $_['retailcrm_dict_status'] = 'Статусы'; $_['retailcrm_dict_payment'] = 'Способы оплаты'; $_['retailcrm_dict_custom_fields'] = 'Настройка пользовательских полей'; -$_['retailcrm_dict_default'] = 'По умалчанию'; +$_['retailcrm_dict_default'] = 'По умолчанию'; +$_['retailcrm_missing_status'] = 'Статус пропавших заказов'; $_['column_total'] = 'Итого'; $_['product_summ'] = 'Сумма'; @@ -74,4 +76,3 @@ $_['text_yes'] = 'Да'; $_['text_no'] = 'Нет'; // Errors $_['error_permission'] = 'У вас недостаточно прав на изменение настроек модуля'; - diff --git a/admin/model/extension/retailcrm/customer.php b/admin/model/extension/retailcrm/customer.php index ab1b6e1..35b09f2 100644 --- a/admin/model/extension/retailcrm/customer.php +++ b/admin/model/extension/retailcrm/customer.php @@ -1,13 +1,33 @@ load->model('setting/setting'); + $this->load->library('retailcrm/retailcrm'); + + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); + $this->retailcrmApiClient = $this->retailcrm->getApiClient(); + } + + /** + * Upload customers + * + * @param array $customers + * + * @return void + */ public function uploadToCrm($customers) { - $this->initApi(); - - if(empty($customers)) - return false; + if ($this->retailcrmApiClient === false || empty($customers)) { + return; + } $customersToCrm = array(); @@ -18,25 +38,37 @@ class ModelExtensionRetailcrmCustomer extends Model { $chunkedCustomers = array_chunk($customersToCrm, 50); foreach($chunkedCustomers as $customersPart) { - $this->retailcrmApi->customersUpload($customersPart); + $this->retailcrmApiClient->customersUpload($customersPart); } } + /** + * Edit customer + * + * @param array $customer + * + * @return void + */ public function changeInCrm($customer) { - $this->initApi(); - - if(empty($customer)) { - return false; + if ($this->retailcrmApiClient === false || empty($customer)) { + return; } - + $customerToCrm = $this->process($customer); - $this->retailcrmApi->customersEdit($customerToCrm); + $this->retailcrmApiClient->customersEdit($customerToCrm); } - private function process($customer) { - $moduleTitle = $this->getModuleTitle(); + /** + * Process customer + * + * @param array $customer + * + * @return array $customerToCrm + */ + private function process($customer) + { $customerToCrm = array( 'externalId' => $customer['customer_id'], @@ -61,12 +93,12 @@ class ModelExtensionRetailcrmCustomer extends Model { ); } - if (isset($this->settings[$moduleTitle . '_custom_field']) && $customer['custom_field']) { + if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $customer['custom_field']) { $customFields = json_decode($customer['custom_field']); foreach ($customFields as $key => $value) { - if (isset($this->settings[$moduleTitle . '_custom_field']['c_' . $key])) { - $customFieldsToCrm[$this->settings[$moduleTitle . '_custom_field']['c_' . $key]] = $value; + if (isset($this->settings[$this->moduleTitle . '_custom_field']['c_' . $key])) { + $customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['c_' . $key]] = $value; } } @@ -77,34 +109,4 @@ class ModelExtensionRetailcrmCustomer extends Model { return $customerToCrm; } - - protected function initApi() - { - $moduleTitle = $this->getModuleTitle(); - $this->load->model('setting/setting'); - $this->settings = $this->model_setting_setting->getSetting($moduleTitle); - - if(empty($this->settings[$moduleTitle . '_url']) || empty($this->settings[$moduleTitle . '_apikey'])) - return false; - - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; - - $this->retailcrmApi = new RetailcrmProxy( - $this->settings[$moduleTitle . '_url'], - $this->settings[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $this->settings[$moduleTitle . '_apiversion'] - ); - } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/admin/model/extension/retailcrm/order.php b/admin/model/extension/retailcrm/order.php index 4e674bb..38e85fb 100644 --- a/admin/model/extension/retailcrm/order.php +++ b/admin/model/extension/retailcrm/order.php @@ -1,10 +1,33 @@ initApi(); + public function __construct($registry) + { + parent::__construct($registry); + $this->load->model('setting/setting'); + $this->load->library('retailcrm/retailcrm'); + + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); + $this->retailcrmApiClient = $this->retailcrm->getApiClient(); + } + + /** + * Upload orders to CRM + * + * @param array $orders + * + * @return void + */ + public function uploadToCrm($orders) + { + if ($this->retailcrmApiClient === false) { + return; + } $ordersToCrm = array(); @@ -15,18 +38,28 @@ class ModelExtensionRetailcrmOrder extends Model { $chunkedOrders = array_chunk($ordersToCrm, 50); foreach($chunkedOrders as $ordersPart) { - $this->retailcrm->ordersUpload($ordersPart); + $this->retailcrmApiClient->ordersUpload($ordersPart); } } + /** + * Send one order by id + * + * @param array $order_data + * + * @return object $result + */ public function uploadOrder($order_data) { - if(isset($this->request->post['fromApi'])) return; + if ($this->retailcrmApiClient === false) { + return; + } - $moduleTitle = $this->getModuleTitle(); - $this->initApi(); - - $customers = $this->retailcrm->customersList( + if(isset($this->request->post['fromApi'])) { + return; + } + + $customers = $this->retailcrmApiClient->customersList( array( 'name' => $order_data['telephone'], 'email' => $order_data['email'] @@ -36,8 +69,8 @@ class ModelExtensionRetailcrmOrder extends Model { ); $order = $this->process($order_data); - - if($customers) { + + if ($customers) { foreach ($customers['customers'] as $customer) { $order['customer']['id'] = $customer['id']; } @@ -45,7 +78,7 @@ class ModelExtensionRetailcrmOrder extends Model { unset($customers); - $result = $this->retailcrm->ordersCreate($order); + $result = $this->retailcrmApiClient->ordersCreate($order); if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $result->isSuccessful()) { $this->createPayment($order_data, $order_data['order_id']); @@ -54,14 +87,32 @@ class ModelExtensionRetailcrmOrder extends Model { return $result; } + /** + * Process order + * + * @param array $order_data + * + * @return array $order + */ private function process($order_data) { $order = array(); $this->load->model('catalog/product'); - $this->moduleTitle = $this->getModuleTitle(); $payment_code = $order_data['payment_code']; - $delivery_code = $order_data['shipping_code']; + if (!empty($order_data['payment_code']) && isset($this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']])) { + $payment_code = $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]; + } else { + $payment_code = ''; + } + + if (!empty($order_data['shipping_code']) && isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) { + $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']]; + } else { + $delivery_code = ''; + } + + $order['number'] = $order_data['order_id']; $order['externalId'] = $order_data['order_id']; $order['firstName'] = $order_data['firstname']; $order['lastName'] = $order_data['lastname']; @@ -88,14 +139,14 @@ class ModelExtensionRetailcrmOrder extends Model { $order['createdAt'] = $order_data['date_added']; if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { - $order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code]; + $order['paymentType'] = $payment_code; } $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; $order['delivery'] = array( - 'code' => !empty($delivery_code) ? $this->settings[$this->moduleTitle . '_delivery'][$delivery_code] : '', + 'code' => $delivery_code, 'cost' => $deliveryCost, 'address' => array( 'index' => $order_data['shipping_postcode'], @@ -123,7 +174,7 @@ class ModelExtensionRetailcrmOrder extends Model { $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); - foreach($product['option'] as $option) { + foreach ($product['option'] as $option) { if ($option['type'] == 'checkbox') { $properties[] = array( 'code' => $option['product_option_value_id'], @@ -179,6 +230,17 @@ class ModelExtensionRetailcrmOrder extends Model { $order['status'] = $this->settings[$this->moduleTitle . '_status'][$order_data['order_status_id']]; } + if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5') { + if ($payment_code) { + $payment = array( + 'externalId' => $order_data['order_id'], + 'type' => $payment_code + ); + + $order['payments'][] = $payment; + } + } + if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $order_data['custom_field']) { $customFields = json_decode($order_data['custom_field']); @@ -196,14 +258,22 @@ class ModelExtensionRetailcrmOrder extends Model { return $order; } + /** + * Create payment + * + * @param array $order + * @param int $order_id + * + * @return void + */ protected function createPayment($order, $order_id) - { - $this->moduleTitle = $this->getModuleTitle(); - + { $payment_code = $order['payment_code']; foreach ($order['totals'] as $total) { - if ($total['code'] == 'total') $amount = $total['value']; + if ($total['code'] == 'total') { + $amount = $total['value']; + } } $payment = array( @@ -216,36 +286,6 @@ class ModelExtensionRetailcrmOrder extends Model { 'externalId' => $order_id ); - $this->retailcrm->ordersPaymentCreate($payment); - } - - private function initApi() - { - $moduleTitle = $this->getModuleTitle(); - $this->load->model('setting/setting'); - $this->settings = $this->model_setting_setting->getSetting($moduleTitle); - - if(!empty($this->settings[$moduleTitle . '_url']) && !empty($this->settings[$moduleTitle . '_apikey'])) { - - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; - - $this->retailcrm = new RetailcrmProxy( - $this->settings[$moduleTitle . '_url'], - $this->settings[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $this->settings[$moduleTitle . '_apiversion'] - ); - } - } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; + $this->retailcrmApiClient->ordersPaymentCreate($payment); } } diff --git a/admin/model/extension/retailcrm/references.php b/admin/model/extension/retailcrm/references.php index 8f9f832..9bf781d 100644 --- a/admin/model/extension/retailcrm/references.php +++ b/admin/model/extension/retailcrm/references.php @@ -1,16 +1,26 @@ load->model('setting/setting'); + $this->load->library('retailcrm/retailcrm'); + + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); + $this->retailcrmApiClient = $this->retailcrm->getApiClient(); + } + + public function getOpercartDeliveryTypes() { - $this->load->model('user/api'); - $this->opencartApiClient = new OpencartApiClient($this->registry); + $this->opencartApiClient = $this->retailcrm->getOcApiClient($this->registry); return $this->opencartApiClient->request('retailcrm/getDeliveryTypes', array(), array()); } @@ -93,38 +103,30 @@ class ModelExtensionRetailcrmReferences extends Model } public function getApiDeliveryTypes() - { - $this->initApi(); - - $response = $this->retailcrm->deliveryTypesList(); + { + $response = $this->retailcrmApiClient->deliveryTypesList(); return (!$response->isSuccessful()) ? array() : $response->deliveryTypes; } public function getApiOrderStatuses() { - $this->initApi(); - - $response = $this->retailcrm->statusesList(); + $response = $this->retailcrmApiClient->statusesList(); return (!$response->isSuccessful()) ? array() : $response->statuses; } public function getApiPaymentTypes() - { - $this->initApi(); - - $response = $this->retailcrm->paymentTypesList(); + { + $response = $this->retailcrmApiClient->paymentTypesList(); return (!$response->isSuccessful()) ? array() : $response->paymentTypes; } public function getApiCustomFields() { - $this->initApi(); - - $customers = $this->retailcrm->customFieldsList(array('entity' => 'customer')); - $orders = $this->retailcrm->customFieldsList(array('entity' => 'order')); + $customers = $this->retailcrmApiClient->customFieldsList(array('entity' => 'customer')); + $orders = $this->retailcrmApiClient->customFieldsList(array('entity' => 'order')); $customFieldsCustomers = (!$customers->isSuccessful()) ? array() : $customers->customFields; $customFieldsOrders = (!$orders->isSuccessful()) ? array() : $orders->customFields; @@ -135,31 +137,4 @@ class ModelExtensionRetailcrmReferences extends Model return array('customers' => $customFieldsCustomers, 'orders' => $customFieldsOrders); } - - protected function initApi() - { - $moduleTitle = $this->getModuleTitle(); - $this->load->model('setting/setting'); - $settings = $this->model_setting_setting->getSetting($moduleTitle); - - if(!empty($settings[$moduleTitle . '_url']) && !empty($settings[$moduleTitle . '_apikey'])) { - $this->retailcrm = new RetailcrmProxy( - $settings[$moduleTitle . '_url'], - $settings[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $settings[$moduleTitle . '_apiversion'] - ); - } - } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/admin/view/template/extension/module/retailcrm.tpl b/admin/view/template/extension/module/retailcrm.tpl index 651c828..f20f78f 100644 --- a/admin/view/template/extension/module/retailcrm.tpl +++ b/admin/view/template/extension/module/retailcrm.tpl @@ -104,24 +104,30 @@

- - -
- - $val): ?> -
- - -
- - + + +
+ + $val): ?> +
+ + +
+ + + +
+ + +
+

@@ -176,6 +182,17 @@ +

+
+ +
diff --git a/admin/view/template/extension/module/retailcrm.twig b/admin/view/template/extension/module/retailcrm.twig index 64454af..0d44af4 100644 --- a/admin/view/template/extension/module/retailcrm.twig +++ b/admin/view/template/extension/module/retailcrm.twig @@ -176,7 +176,18 @@ {% endfor %} - + + +

{{ retailcrm_missing_status }}

+
+
{% endif %} diff --git a/catalog/controller/api/retailcrm.php b/catalog/controller/api/retailcrm.php index 2308cd8..cfa69c3 100644 --- a/catalog/controller/api/retailcrm.php +++ b/catalog/controller/api/retailcrm.php @@ -5,7 +5,8 @@ class ControllerApiRetailcrm extends Controller { $this->load->model('localisation/country'); $this->load->model('setting/setting'); - $moduleTitle = $this->getModuleTitle(); + $this->load->library('retailcrm/retailcrm'); + $moduleTitle = $this->retailcrm->getModuleTitle(); $countries = $this->model_setting_setting->getSetting($moduleTitle)[$moduleTitle . '_country']; $deliveryTypes = array(); @@ -96,15 +97,4 @@ class ControllerApiRetailcrm extends Controller $this->modelExtension = 'setting_extension'; } } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/catalog/controller/extension/analytics/daemon_collector.php b/catalog/controller/extension/analytics/daemon_collector.php index bbfb5a9..170cfd9 100644 --- a/catalog/controller/extension/analytics/daemon_collector.php +++ b/catalog/controller/extension/analytics/daemon_collector.php @@ -2,7 +2,8 @@ class ControllerExtensionAnalyticsDaemonCollector extends Controller { public function index() { $this->load->model('setting/setting'); - $moduleTitle = $this->getModuleTitle(); + $this->load->library('retailcrm/retailcrm'); + $moduleTitle = $this->retailcrm->getModuleTitle(); $settings = $this->model_setting_setting->getSetting($moduleTitle); $setting = $settings[$moduleTitle . '_collector']; @@ -87,15 +88,4 @@ class ControllerExtensionAnalyticsDaemonCollector extends Controller { return html_entity_decode($js, ENT_QUOTES, 'UTF-8'); } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/catalog/controller/extension/module/retailcrm.php b/catalog/controller/extension/module/retailcrm.php index 1e56d51..b726187 100644 --- a/catalog/controller/extension/module/retailcrm.php +++ b/catalog/controller/extension/module/retailcrm.php @@ -19,13 +19,13 @@ class ControllerExtensionModuleRetailcrm extends Controller * @return void */ public function order_create($parameter1, $parameter2 = null, $parameter3 = null) - { - $moduleTitle = $this->getModuleTitle(); + { $this->load->model('checkout/order'); $this->load->model('account/order'); + $this->load->library('retailcrm/retailcrm'); + $moduleTitle = $this->retailcrm->getModuleTitle(); $order_id = $parameter3; - $data = $this->model_checkout_order->getOrder($order_id); $data['totals'] = $this->model_account_order->getOrderTotals($order_id); @@ -62,12 +62,13 @@ class ControllerExtensionModuleRetailcrm extends Controller * @return void */ public function order_edit($parameter1, $parameter2 = null) { - $moduleTitle = $this->getModuleTitle(); $order_id = $parameter2[0]; $this->load->model('checkout/order'); $this->load->model('account/order'); + $this->load->library('retailcrm/retailcrm'); + $moduleTitle = $this->retailcrm->getModuleTitle(); $data = $this->model_checkout_order->getOrder($order_id); if($data['order_status_id'] == 0) return; @@ -162,15 +163,4 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->model_extension_retailcrm_customer->changeInCrm($customer); } } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/catalog/model/extension/retailcrm/customer.php b/catalog/model/extension/retailcrm/customer.php index 1676907..82a5002 100644 --- a/catalog/model/extension/retailcrm/customer.php +++ b/catalog/model/extension/retailcrm/customer.php @@ -1,33 +1,65 @@ load->model('setting/setting'); + $this->load->library('retailcrm/retailcrm'); + + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); + $this->retailcrmApiClient = $this->retailcrm->getApiClient(); + } + + /** + * Create customer + * + * @param array $customer + * + * @return void + */ public function sendToCrm($customer) { - $this->initApi(); - - if(empty($customer)) + if(empty($customer) || $this->retailcrmApiClient === false) { return false; - + } + $customerToCrm = $this->process($customer); - $this->retailcrmApi->customersCreate($customerToCrm); + $this->retailcrmApiClient->customersCreate($customerToCrm); } + /** + * Edit customer + * + * @param array $customer + * + * @return void + */ public function changeInCrm($customer) { - $this->initApi(); - - if(empty($customer)) + if(empty($customer) || $this->retailcrmApiClient === false) { return false; + } $customerToCrm = $this->process($customer); - $this->retailcrmApi->customersEdit($customerToCrm); + $this->retailcrmApiClient->customersEdit($customerToCrm); } - + + /** + * Process customer + * + * @param array $customer + * + * @return array $customerToCrm + */ private function process($customer) { - $moduleTitle = $this->getModuleTitle(); - $customerToCrm = array( 'externalId' => $customer['customer_id'], 'firstName' => $customer['firstname'], @@ -51,12 +83,12 @@ class ModelExtensionRetailcrmCustomer extends Model { ); } - if (isset($this->settings[$moduleTitle . '_custom_field']) && $customer['custom_field']) { + if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $customer['custom_field']) { $customFields = json_decode($customer['custom_field']); foreach ($customFields as $key => $value) { - if (isset($this->settings[$moduleTitle . '_custom_field']['c_' . $key])) { - $customFieldsToCrm[$this->settings[$moduleTitle . '_custom_field']['c_' . $key]] = $value; + if (isset($this->settings[$this->moduleTitle . '_custom_field']['c_' . $key])) { + $customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['c_' . $key]] = $value; } } @@ -67,34 +99,4 @@ class ModelExtensionRetailcrmCustomer extends Model { return $customerToCrm; } - - protected function initApi() - { - $this->load->model('setting/setting'); - $moduleTitle = $this->getModuleTitle(); - $this->settings = $this->model_setting_setting->getSetting($moduleTitle); - - if(empty($this->settings[$moduleTitle . '_url']) || empty($this->settings[$moduleTitle . '_apikey'])) - return false; - - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; - - $this->retailcrmApi = new RetailcrmProxy( - $this->settings[$moduleTitle . '_url'], - $this->settings[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $this->settings[$moduleTitle . '_apiversion'] - ); - } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } } diff --git a/catalog/model/extension/retailcrm/order.php b/catalog/model/extension/retailcrm/order.php index d08c003..bee0aaa 100644 --- a/catalog/model/extension/retailcrm/order.php +++ b/catalog/model/extension/retailcrm/order.php @@ -1,16 +1,38 @@ load->model('setting/setting'); + $this->load->library('retailcrm/retailcrm'); + + $this->moduleTitle = $this->retailcrm->getModuleTitle(); + $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); + $this->retailcrmApiClient = $this->retailcrm->getApiClient(); + } + + /** + * Create order in CRM + * + * @param array $order_data + * @param int $order_id + * + * @return void + */ public function sendToCrm($order_data, $order_id) { - if(isset($this->request->post['fromApi'])) return; - - $this->initApi(); + if(isset($this->request->post['fromApi']) || $this->retailcrmApiClient === false) { + return; + } $order = $this->processOrder($order_data, $order_id); - $customers = $this->retailcrm->customersList( + $customers = $this->retailcrmApiClient->customersList( array( 'name' => $order_data['telephone'], 'email' => $order_data['email'] @@ -27,22 +49,30 @@ class ModelExtensionRetailcrmOrder extends Model { unset($customers); - $response = $this->retailcrm->ordersCreate($order); + $response = $this->retailcrmApiClient->ordersCreate($order); if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { $this->createPayment($order_data, $order_id); } } + /** + * Edit order in CRM + * + * @param array $order_data + * @param int $order_id + * + * @return void + */ public function changeInCrm($order_data, $order_id) { - if(isset($this->request->post['fromApi'])) return; - - $this->initApi(); + if(isset($this->request->post['fromApi']) || $this->retailcrmApiClient === false) { + return; + } $order = $this->processOrder($order_data, $order_id); - $customers = $this->retailcrm->customersList( + $customers = $this->retailcrmApiClient->customersList( array( 'name' => $order_data['telephone'], 'email' => $order_data['email'] @@ -59,10 +89,10 @@ class ModelExtensionRetailcrmOrder extends Model { unset($customers); - $response = $this->retailcrm->ordersEdit($order); + $response = $this->retailcrmApiClient->ordersEdit($order); if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { - $response_order = $this->retailcrm->ordersGet($order_id); + $response_order = $this->retailcrmApiClient->ordersGet($order_id); if ($response_order->isSuccessful()) { $order_info = $response_order['order']; } @@ -74,7 +104,7 @@ class ModelExtensionRetailcrmOrder extends Model { } if (isset($payment) && $payment['type'] != $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]) { - $response = $this->retailcrm->ordersPaymentDelete($payment['id']); + $response = $this->retailcrmApiClient->ordersPaymentDelete($payment['id']); if ($response->isSuccessful()) { $this->createPayment($order_data, $order_id); @@ -85,14 +115,33 @@ class ModelExtensionRetailcrmOrder extends Model { } } + /** + * Process order + * + * @param array $order_data + * @param int $order_id + * + * @return array $order + */ protected function processOrder($order_data, $order_id) - { - $this->moduleTitle = $this->getModuleTitle(); + { $this->load->model('setting/setting'); $this->load->model('catalog/product'); $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); - $payment_code = $order_data['payment_code']; + if (!empty($order_data['payment_code']) && isset($this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']])) { + $payment_code = $this->settings[$this->moduleTitle . '_payment'][$order_data['payment_code']]; + } else { + $payment_code = ''; + } + + if (!empty($order_data['shipping_code']) && isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) { + $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']]; + } else { + $delivery_code = ''; + } + + $order['number'] = $order_data['order_id']; $order['externalId'] = $order_id; $order['firstName'] = $order_data['firstname']; $order['lastName'] = $order_data['lastname']; @@ -119,7 +168,7 @@ class ModelExtensionRetailcrmOrder extends Model { $order['createdAt'] = $order_data['date_added']; if ($this->settings[$this->moduleTitle . '_apiversion'] != 'v5') { - $order['paymentType'] = $this->settings[$this->moduleTitle . '_payment'][$payment_code]; + $order['paymentType'] = $payment_code; if (isset($couponTotal)) $order['discount'] = $couponTotal; } else { if (isset($couponTotal)) $order['discountManualAmount'] = $couponTotal; @@ -127,14 +176,8 @@ class ModelExtensionRetailcrmOrder extends Model { $country = (isset($order_data['shipping_country'])) ? $order_data['shipping_country'] : '' ; - 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) ? $this->settings[$this->moduleTitle . '_delivery'][$delivery_code] : '', + 'code' => $delivery_code, 'address' => array( 'index' => $order_data['shipping_postcode'], 'city' => $order_data['shipping_city'], @@ -219,6 +262,8 @@ class ModelExtensionRetailcrmOrder extends Model { if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { $order['status'] = $this->settings[$this->moduleTitle . '_status'][$order_data['order_status_id']]; + } elseif (isset($order_data['order_status_id']) && $order_data['order_status_id'] == 0) { + $order['status'] = $this->settings[$this->moduleTitle . '_missing_status']; } if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $order_data['custom_field']) { @@ -239,12 +284,22 @@ class ModelExtensionRetailcrmOrder extends Model { return $order; } + /** + * Create payment + * + * @param array $order + * @param int $order_id + * + * @return void + */ protected function createPayment($order, $order_id) { $payment_code = $order['payment_code']; foreach ($order['totals'] as $total) { - if ($total['code'] == 'total') $amount = $total['value']; + if ($total['code'] == 'total') { + $amount = $total['value']; + } } $payment = array( @@ -257,9 +312,17 @@ class ModelExtensionRetailcrmOrder extends Model { 'externalId' => $order_id ); - $this->retailcrm->ordersPaymentCreate($payment); + $this->retailcrmApiClient->ordersPaymentCreate($payment); } + /** + * Edit payment + * + * @param array $order + * @param int $order_id + * + * @return void + */ protected function editPayment($order, $order_id) { $payment_code = $order['payment_code']; @@ -276,37 +339,6 @@ class ModelExtensionRetailcrmOrder extends Model { 'amount' => $amount ); - $this->retailcrm->ordersPaymentEdit($payment); + $this->retailcrmApiClient->ordersPaymentEdit($payment); } - - private function initApi() - { - $moduleTitle = $this->getModuleTitle(); - $this->load->model('setting/setting'); - $settings = $this->model_setting_setting->getSetting($moduleTitle); - - if(!empty($settings[$moduleTitle . '_url']) && !empty($settings[$moduleTitle . '_apikey'])) { - - require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; - - $this->retailcrm = new RetailcrmProxy( - $settings[$moduleTitle . '_url'], - $settings[$moduleTitle . '_apikey'], - DIR_SYSTEM . 'storage/logs/retailcrm.log', - $settings[$moduleTitle . '_apiversion'] - ); - } - } - - private function getModuleTitle() - { - if (version_compare(VERSION, '3.0', '<')) { - $title = 'retailcrm'; - } else { - $title = 'module_retailcrm'; - } - - return $title; - } - } diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index 662f639..5eb3f46 100644 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -89,6 +89,10 @@ class OpencartApiClient { curl_close($curl); + if (!$json && json_last_error() !== JSON_ERROR_NONE) { + return false; + } + if (isset($json['error'])) { if (is_array($json['error'])) { foreach ($json['error'] as $error) { @@ -103,6 +107,8 @@ class OpencartApiClient { } private function auth() { + $this->load->model('user/api'); + $apiUsers = $this->model_user_api->getApis(); $api = array(); diff --git a/system/library/retailcrm/RetailcrmHistoryHelper.php b/system/library/retailcrm/RetailcrmHistoryHelper.php index 5247020..f762ebb 100644 --- a/system/library/retailcrm/RetailcrmHistoryHelper.php +++ b/system/library/retailcrm/RetailcrmHistoryHelper.php @@ -41,13 +41,21 @@ class RetailcrmHistoryHelper { $orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item']; } - if(empty($change['oldValue']) && $change['field'] == 'order_product') { + if (isset($change['oldValue']) + && empty($change['oldValue']) + && $change['field'] == 'order_product' + ) { $orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = true; } - if(empty($change['newValue']) && $change['field'] == 'order_product') { + + if (isset($change['newValue']) + && empty($change['newValue']) + && $change['field'] == 'order_product' + ) { $orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true; } - if(!isset($orders[$change['order']['id']]['items'][$change['item']['id']]['create']) && $fields['item'][$change['field']]) { + + if (!isset($orders[$change['order']['id']]['items'][$change['item']['id']]['create']) && $fields['item'][$change['field']]) { $orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue']; } } else { @@ -63,7 +71,7 @@ class RetailcrmHistoryHelper { $orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']); } elseif(strripos($change['field'], 'custom_') !== false) { $orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']); - } elseif($fields['order'][$change['field']]) { + } elseif(isset($fields['order'][$change['field']]) && $fields['order'][$change['field']]) { $orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']); } diff --git a/system/library/retailcrm/RetailcrmHttpClient.php b/system/library/retailcrm/RetailcrmHttpClient.php index 6ec0e5c..7f4d4e0 100644 --- a/system/library/retailcrm/RetailcrmHttpClient.php +++ b/system/library/retailcrm/RetailcrmHttpClient.php @@ -81,7 +81,6 @@ class RetailcrmHttpClient $curlHandler = curl_init(); curl_setopt($curlHandler, CURLOPT_URL, $url); curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false); diff --git a/system/library/retailcrm/retailcrm.php b/system/library/retailcrm/retailcrm.php new file mode 100644 index 0000000..1740251 --- /dev/null +++ b/system/library/retailcrm/retailcrm.php @@ -0,0 +1,92 @@ +registry = $registry; + } + public function __get($name) { + return $this->registry->get($name); + } + + /** + * Get api client object + * + * @param string $apiUrl (default = null) + * @param string $apiKey (default = null) + * @param string $apiVersion (default = null) + * + * @return mixed object | boolean + */ + public function getApiClient($apiUrl = null, $apiKey = null, $apiVersion = null) + { + $this->load->model('setting/setting'); + + $setting = $this->model_setting_setting->getSetting($this->getModuleTitle()); + + if ($apiUrl === null && $apiKey === null) { + $apiUrl = $setting[$this->getModuleTitle() . '_url']; + $apiKey = $setting[$this->getModuleTitle() . '_apikey']; + $apiVersion = $setting[$this->getModuleTitle() . '_apiversion']; + } + + if ($apiUrl && $apiKey) { + $this->apiClient = new \RetailcrmProxy($apiUrl, $apiKey, DIR_LOGS . 'retailcrm.log', $apiVersion); + + return $this->apiClient; + } + + return false; + } + + /** + * Get opencart api client + * + * @param object $registry + * + * @return \OpencartApiClient + */ + public function getOcApiClient($registry) + { + return new \OpencartApiClient($registry); + } + + /** + * Get module title for this version + * + * @return string $title + */ + public function getModuleTitle() + { + if (version_compare(VERSION, '3.0', '<')) { + $title = 'retailcrm'; + } else { + $title = 'module_retailcrm'; + } + + return $title; + } + + /** + * Get token param name + * + * @return string $token + */ + public function getTokenTitle() + { + if (version_compare(VERSION, '3.0', '<')) { + $token = 'token'; + } else { + $token = 'user_token'; + } + + return $token; + } +}