From db00b14872f48da89cd43f76ce78a1075c7cf7ad Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 22 Sep 2017 11:24:35 +0300 Subject: [PATCH] fix getting shipping methods --- admin/controller/module/retailcrm.php | 13 ++- admin/language/english/module/retailcrm.php | 1 + admin/language/russian/module/retailcrm.php | 1 + admin/model/retailcrm/customer.php | 4 +- admin/model/retailcrm/history.php | 4 +- admin/model/retailcrm/order.php | 10 +-- admin/model/retailcrm/references.php | 37 +++------ admin/view/template/module/retailcrm.tpl | 40 ++++++--- catalog/controller/api/retailcrm.php | 81 +++++++++++++++++++ catalog/model/retailcrm/customer.php | 4 +- catalog/model/retailcrm/order.php | 4 +- .../library/retailcrm/OpencartApiClient.php | 6 ++ 12 files changed, 152 insertions(+), 53 deletions(-) create mode 100644 catalog/controller/api/retailcrm.php diff --git a/admin/controller/module/retailcrm.php b/admin/controller/module/retailcrm.php index 2576b1f..178e2fc 100644 --- a/admin/controller/module/retailcrm.php +++ b/admin/controller/module/retailcrm.php @@ -26,7 +26,10 @@ class ControllerModuleRetailcrm extends Controller { $this->load->model('setting/setting'); $this->model_setting_setting - ->editSetting('retailcrm', array('retailcrm_status' => 1)); + ->editSetting('retailcrm', array( + 'retailcrm_status' => 1, + 'retailcrm_country' => array($this->config->get('config_country_id')) + )); $this->load->model('extension/event'); @@ -78,6 +81,7 @@ class ControllerModuleRetailcrm extends Controller $this->load->model('setting/setting'); $this->load->model('extension/module'); $this->load->model('retailcrm/references'); + $this->load->model('localisation/country'); $this->load->language('module/retailcrm'); $this->document->setTitle($this->language->get('heading_title')); $this->document->addStyle('/admin/view/stylesheet/retailcrm.css'); @@ -112,6 +116,7 @@ class ControllerModuleRetailcrm extends Controller 'retailcrm_dict_delivery', 'retailcrm_dict_status', 'retailcrm_dict_payment', + 'retailcrm_countries_settings' ); $this->load->model('extension/extension'); @@ -216,7 +221,7 @@ class ControllerModuleRetailcrm extends Controller $this->load->model('design/layout'); $_data['layouts'] = $this->model_design_layout->getLayouts(); - + $_data['countries'] = $this->model_localisation_country->getCountries(); $_data['header'] = $this->load->controller('common/header'); $_data['column_left'] = $this->load->controller('common/column_left'); $_data['footer'] = $this->load->controller('common/footer'); @@ -342,9 +347,9 @@ class ControllerModuleRetailcrm extends Controller private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/admin/language/english/module/retailcrm.php b/admin/language/english/module/retailcrm.php index f1baabb..b3e8e7e 100644 --- a/admin/language/english/module/retailcrm.php +++ b/admin/language/english/module/retailcrm.php @@ -10,6 +10,7 @@ $_['text_success'] = 'Setting saved'; $_['text_notice'] = 'Warning! Timezone in CRM & your shop must be equal, you must setup it here:'; $_['retailcrm_base_settings'] = 'Connection settings'; $_['retailcrm_dict_settings'] = 'Dictionary settings'; +$_['retailcrm_countries_settings'] = 'Trading zones setting'; $_['retailcrm_url'] = 'RetailCRM URL'; $_['retailcrm_apikey'] = 'RetailCRM API Key'; diff --git a/admin/language/russian/module/retailcrm.php b/admin/language/russian/module/retailcrm.php index b2062ff..892924d 100644 --- a/admin/language/russian/module/retailcrm.php +++ b/admin/language/russian/module/retailcrm.php @@ -10,6 +10,7 @@ $_['text_success'] = 'Настройки успешно сохра $_['text_notice'] = 'Внимание! Часовой пояс в CRM должен совпадать с часовым поясом в магазине, настроки часового пояса CRM можно задать по адресу:'; $_['retailcrm_base_settings'] = 'Настройки соединения'; $_['retailcrm_dict_settings'] = 'Настройки соответствия справочников'; +$_['retailcrm_countries_settings'] = 'Настройка торговых зон'; $_['retailcrm_url'] = 'Адрес RetailCRM'; $_['retailcrm_apikey'] = 'Api ключ RetailCRM'; diff --git a/admin/model/retailcrm/customer.php b/admin/model/retailcrm/customer.php index 77c347a..5f0036f 100644 --- a/admin/model/retailcrm/customer.php +++ b/admin/model/retailcrm/customer.php @@ -52,9 +52,9 @@ class ModelRetailcrmCustomer extends Model { private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 1ac2e42..dcb7801 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -471,9 +471,9 @@ class ModelRetailcrmHistory extends Model private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 89843af..91c6d7b 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -9,14 +9,14 @@ class ModelRetailcrmOrder extends Model { if(empty($orders)) return false; - if(empty($settings['retailcrm_url']) || empty($settings['retailcrm_apikey'])) + if(empty($this->settings['retailcrm_url']) || empty($this->settings['retailcrm_apikey'])) return false; require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; $this->retailcrmApi = new RetailcrmProxy( - $settings['retailcrm_url'], - $settings['retailcrm_apikey'], + $this->settings['retailcrm_url'], + $this->settings['retailcrm_apikey'], $this->serLogs() ); @@ -130,9 +130,9 @@ class ModelRetailcrmOrder extends Model { private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/admin/model/retailcrm/references.php b/admin/model/retailcrm/references.php index 118ef6d..8c4a553 100644 --- a/admin/model/retailcrm/references.php +++ b/admin/model/retailcrm/references.php @@ -5,9 +5,20 @@ require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; class ModelRetailcrmReferences extends Model { protected $retailcrm; + private $opencartApiClient; + + public function getOpercartDeliveryTypes() + { + $this->load->model('user/api'); + $this->opencartApiClient = new OpencartApiClient($this->registry); + + return $this->opencartApiClient->getDeliveryTypes(); + } public function getDeliveryTypes() { + $this->load->model('setting/store'); + return array( 'opencart' => $this->getOpercartDeliveryTypes(), 'retailcrm' => $this->getApiDeliveryTypes() @@ -30,28 +41,6 @@ class ModelRetailcrmReferences extends Model ); } - public function getOpercartDeliveryTypes() - { - $deliveryMethods = array(); - $files = glob(DIR_APPLICATION . 'controller/shipping/*.php'); - - if ($files) { - foreach ($files as $file) { - $extension = basename($file, '.php'); - - $this->load->language('shipping/' . $extension); - - if ($this->config->get($extension . '_status')) { - $deliveryMethods[$extension.'.'.$extension] = strip_tags( - $this->language->get('heading_title') - ); - } - } - } - - return $deliveryMethods; - } - public function getOpercartOrderStatuses() { $this->load->model('localisation/order_status'); @@ -139,9 +128,9 @@ class ModelRetailcrmReferences extends Model private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/admin/view/template/module/retailcrm.tpl b/admin/view/template/module/retailcrm.tpl index 144f68b..cce16f4 100644 --- a/admin/view/template/module/retailcrm.tpl +++ b/admin/view/template/module/retailcrm.tpl @@ -45,7 +45,19 @@
- +

+
+
+ +
+ +
+ +
+
@@ -56,17 +68,21 @@

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

diff --git a/catalog/controller/api/retailcrm.php b/catalog/controller/api/retailcrm.php new file mode 100644 index 0000000..13d8ba9 --- /dev/null +++ b/catalog/controller/api/retailcrm.php @@ -0,0 +1,81 @@ +load->model('localisation/country'); + $this->load->model('setting/setting'); + $countries = $this->model_setting_setting->getSetting('retailcrm')['retailcrm_country']; + $deliveryTypes = array(); + + foreach ($countries as $country) { + $deliveryTypes = array_merge($deliveryTypes, $this->getDeliveryTypesByZones($country)); + } + + if (isset($this->request->server['HTTP_ORIGIN'])) { + $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']); + $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); + $this->response->addHeader('Access-Control-Max-Age: 1000'); + $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($deliveryTypes)); + } + + protected function getDeliveryTypesByZones($country_id) + { + $this->load->model('extension/extension'); + $this->load->model('localisation/zone'); + $this->load->model('localisation/country'); + + $shippingModules = $this->model_extension_extension->getExtensions('shipping'); + $zones = $this->model_localisation_zone->getZonesByCountryId($country_id); + $country = $this->model_localisation_country->getCountry($country_id); + $quote_data = array(); + + foreach ($zones as $zone) { + $address = array( + 'country_id' => $country_id, + 'zone_id' => $zone['zone_id'], + 'iso_code_2' => $country['iso_code_2'], + 'iso_code_3' => $country['iso_code_3'], + 'zone_code' => $zone['code'], + 'postcode' => '', + 'city' => '' + ); + + foreach ($shippingModules as $shippingModule) { + $this->load->model('shipping/' . $shippingModule['code']); + + if ($this->config->get($shippingModule['code'] . '_status')) { + if ($shippingModule['code'] == 'free') { + $free_total = $this->config->get('free_total'); + + if ($free_total > 0) { + $this->config->set('free_total', 0); + } + } + + if($this->{'model_shipping_' . $shippingModule['code']}->getQuote($address)) { + $quote_data[] = $this->{'model_shipping_' . $shippingModule['code']}->getQuote($address); + } + } + } + } + + $deliveryTypes = array(); + + foreach ($quote_data as $shipping) { + + foreach ($shipping['quote'] as $shippingMethod) { + $deliveryTypes[$shipping['code']]['title'] = $shipping['title']; + $deliveryTypes[$shipping['code']][$shippingMethod['code']] = $shippingMethod; + } + + } + + return $deliveryTypes; + } +} diff --git a/catalog/model/retailcrm/customer.php b/catalog/model/retailcrm/customer.php index fc2d061..80f9f7b 100644 --- a/catalog/model/retailcrm/customer.php +++ b/catalog/model/retailcrm/customer.php @@ -43,9 +43,9 @@ class ModelRetailcrmCustomer extends Model { private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index f6ba894..8a5d1b0 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -261,9 +261,9 @@ class ModelRetailcrmOrder extends Model { private function setLogs() { if (version_compare(VERSION, '2.0', '>')) { - $logs = DIR_SYSTEM . 'storage/logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'storage/logs/retailcrm.log'; } else { - $logs = DIR_SYSTEM . 'logs/ecomlogic.log'; + $logs = DIR_SYSTEM . 'logs/retailcrm.log'; } return $logs; diff --git a/system/library/retailcrm/OpencartApiClient.php b/system/library/retailcrm/OpencartApiClient.php index 9d1ed37..e21b1da 100644 --- a/system/library/retailcrm/OpencartApiClient.php +++ b/system/library/retailcrm/OpencartApiClient.php @@ -288,6 +288,12 @@ class OpencartApiClient { $this->request('order/add', array(), $order); } + public function getDeliveryTypes() { + $this->apiToken = $this->session->data['token']; + + return $this->request('retailcrm/getDeliveryTypes', array(), array()); + } + private function getInnerIpAddr() { $opencartStoreInfo = $this->model_setting_store->getStore($this->opencartStoreId);