From f5f5a1d60065df3b25a0f5a88e2fac6f0b591436 Mon Sep 17 00:00:00 2001 From: acevodo Date: Mon, 22 Jan 2024 09:46:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B5=D1=80=D1=81=D0=B8=D1=8F=200.8.8=20?= =?UTF-8?q?(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Версия 0.8.8 - Исправление ошибки при отсутствии code. - Для каждой из СД добавлена возможность выбора нужна ли отгрузка от двери и нужна ли отгрузка от ПВЗ. - Добавлено кеширование. - Добавлен флаг показывать заглушку в корзине об ошибке или нет. --- 2.1/install.xml | 7 +- .../admin/controller/shipping/apiship.php | 10 +- .../language/english/shipping/apiship.php | 2 + .../language/russian/shipping/apiship.php | 2 + .../admin/view/template/shipping/apiship.tpl | 38 +- 2.1/upload/catalog/model/shipping/apiship.php | 168 +++---- 2.1/upload/system/library/apiship/apiship.php | 99 ++++- 2.3/install.xml | 7 +- .../controller/extension/shipping/apiship.php | 10 +- .../en-gb/extension/shipping/apiship.php | 2 + .../ru-ru/extension/shipping/apiship.php | 2 + .../template/extension/shipping/apiship.tpl | 38 +- .../model/extension/shipping/apiship.php | 375 ++++++++-------- 2.3/upload/system/library/apiship/apiship.php | 99 ++++- 3.x/install.xml | 7 +- .../controller/extension/shipping/apiship.php | 10 +- .../en-gb/extension/shipping/apiship.php | 2 + .../ru-ru/extension/shipping/apiship.php | 2 + .../template/extension/shipping/apiship.twig | 41 +- .../model/extension/shipping/apiship.php | 417 +++++++++--------- 3.x/upload/system/library/apiship/apiship.php | 91 +++- CHANGELOG.md | 6 + 22 files changed, 926 insertions(+), 509 deletions(-) diff --git a/2.1/install.xml b/2.1/install.xml index 6a9a205..58b4bcd 100644 --- a/2.1/install.xml +++ b/2.1/install.xml @@ -2,7 +2,7 @@ ApiShip ApiShip - 0.8.7 (OpenCart 2.0 - 2.2) + 0.8.8 (OpenCart 2.0 - 2.2) ApiShip https://apiship.ru @@ -731,7 +731,10 @@ $this->_templateData['shipping_apiship_providers_keys'] = json_encode(array_keys($providers)); $this->_templateData['shipping_apiship_group_points'] = $this->config->get('shipping_apiship_group_points') ? 'true' : 'false'; - $code = $this->_templateData['shipping_method']['code']; + $code = ""; + if (is_array($this->_templateData['shipping_method']) && isset($this->_templateData['shipping_method']['code'])) { + $code = $this->_templateData['shipping_method']['code']; + } if ((strpos($code, 'apiship')!==false) && (strpos($code, 'error')!==false)) { diff --git a/2.1/upload/admin/controller/shipping/apiship.php b/2.1/upload/admin/controller/shipping/apiship.php index 4467868..c8fc4e4 100644 --- a/2.1/upload/admin/controller/shipping/apiship.php +++ b/2.1/upload/admin/controller/shipping/apiship.php @@ -48,7 +48,7 @@ public function index() { $data['text_none'] = $this->language->get('text_none'); $data['text_shipping_apiship_cron_url_copy'] = $this->language->get('text_shipping_apiship_cron_url_copy'); - $data['shipping_apiship_version'] = '0.8.7 (OpenCart 2.0 - 2.2)'; + $data['shipping_apiship_version'] = '0.8.8 (OpenCart 2.0 - 2.2)'; $data['shipping_apiship_version_js_mod'] = rand(); $data['button_save'] = $this->language->get('button_save'); @@ -90,6 +90,7 @@ public function index() { $data['entry_shipping_apiship_tax_class'] = $this->language->get('entry_shipping_apiship_tax_class'); $data['entry_shipping_apiship_geo_zone'] = $this->language->get('entry_shipping_apiship_geo_zone'); $data['entry_shipping_apiship_icon_show'] = $this->language->get('entry_shipping_apiship_icon_show'); + $data['entry_shipping_apiship_error_stub_show'] = $this->language->get('entry_shipping_apiship_error_stub_show'); $data['entry_shipping_apiship_group_points'] = $this->language->get('entry_shipping_apiship_group_points'); $data['entry_shipping_apiship_prefix'] = $this->language->get('entry_shipping_apiship_prefix'); $data['entry_shipping_apiship_export_status'] = $this->language->get('entry_shipping_apiship_export_status'); @@ -110,6 +111,7 @@ public function index() { $data['entry_extra_settings'] = $this->language->get('entry_extra_settings'); $data['entry_providers_points'] = $this->language->get('entry_providers_points'); $data['entry_providers_point'] = $this->language->get('entry_providers_point'); + $data['entry_providers_courier'] = $this->language->get('entry_providers_courier'); $data['entry_events_mapping'] = $this->language->get('entry_events_mapping'); $data['entry_events_mapping_notify'] = $this->language->get('entry_events_mapping_notify'); @@ -414,6 +416,12 @@ public function index() { $data['shipping_apiship_prefix'] = $this->config->get('shipping_apiship_prefix'); } + if (isset($this->request->post['shipping_apiship_error_stub_show'])) { + $data['shipping_apiship_error_stub_show'] = $this->request->post['shipping_apiship_error_stub_show']; + } else { + $data['shipping_apiship_error_stub_show'] = $this->config->get('shipping_apiship_error_stub_show'); + } + if (isset($this->request->post['shipping_apiship_mode'])) { $data['shipping_apiship_mode'] = $this->request->post['shipping_apiship_mode']; } else { diff --git a/2.1/upload/admin/language/english/shipping/apiship.php b/2.1/upload/admin/language/english/shipping/apiship.php index 129db0a..f77066b 100644 --- a/2.1/upload/admin/language/english/shipping/apiship.php +++ b/2.1/upload/admin/language/english/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Show icon'; $_['entry_shipping_apiship_prefix'] = 'Order prefix'; +$_['entry_shipping_apiship_error_stub_show'] = 'Show a stub in the cart about an error'; $_['entry_shipping_apiship_mode'] = 'Working mode'; $_['entry_shipping_apiship_mode_normal'] = 'Normal'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Extra settings'; $_['entry_providers_points'] = 'DC points'; $_['entry_providers_point'] = 'DC point'; +$_['entry_providers_courier'] = 'Picked up by courier'; $_['entry_events_mapping'] = 'Status mapping table'; $_['entry_events_mapping_notify'] = 'Notify'; diff --git a/2.1/upload/admin/language/russian/shipping/apiship.php b/2.1/upload/admin/language/russian/shipping/apiship.php index 3bf9917..60ff5dc 100644 --- a/2.1/upload/admin/language/russian/shipping/apiship.php +++ b/2.1/upload/admin/language/russian/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Показывать иконку'; $_['entry_shipping_apiship_prefix'] = 'Префикс номера заказа'; +$_['entry_shipping_apiship_error_stub_show'] = 'Показывать заглушку в корзине об ошибке'; $_['entry_shipping_apiship_mode'] = 'Режим работы'; $_['entry_shipping_apiship_mode_normal'] = 'Нормальный'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Дополнительные параметры'; $_['entry_providers_points'] = 'Параметры отгрузки в службы доставки'; $_['entry_providers_point'] = 'Привоз на склад'; +$_['entry_providers_courier'] = 'Забирает курьер'; $_['entry_events_mapping'] = 'Параметры сопоставления статусов'; $_['entry_events_mapping_notify'] = 'Оповестить'; diff --git a/2.1/upload/admin/view/template/shipping/apiship.tpl b/2.1/upload/admin/view/template/shipping/apiship.tpl index 32774ee..0285c2b 100644 --- a/2.1/upload/admin/view/template/shipping/apiship.tpl +++ b/2.1/upload/admin/view/template/shipping/apiship.tpl @@ -384,9 +384,9 @@
@@ -397,6 +397,21 @@ + +
+ + +
+ +
+
+ @@ -519,6 +534,21 @@ +
+ +
+
+ +
+
+
+
@@ -704,12 +734,12 @@ function set_providers_checkbox(checked, provider) { } -$(".shipping_apiship_provider_checkbox").on("click", function () { +$(".shipping_apiship_provider_point").on("click", function () { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); -jQuery('.shipping_apiship_provider_checkbox').each(function() { +jQuery('.shipping_apiship_provider_point').each(function() { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); diff --git a/2.1/upload/catalog/model/shipping/apiship.php b/2.1/upload/catalog/model/shipping/apiship.php index e54c016..903d33d 100644 --- a/2.1/upload/catalog/model/shipping/apiship.php +++ b/2.1/upload/catalog/model/shipping/apiship.php @@ -57,6 +57,7 @@ public function __construct($params) { 'shipping_apiship_mode' => $this->config->get('shipping_apiship_mode'), 'shipping_apiship_prefix' => $this->config->get('shipping_apiship_prefix'), + 'shipping_apiship_error_stub_show' => $this->config->get('shipping_apiship_error_stub_show') ? true : false, 'shipping_apiship_title' => $this->config->get('shipping_apiship_title'), 'shipping_apiship_template' => html_entity_decode($this->config->get('shipping_apiship_template')), @@ -90,27 +91,6 @@ public function __construct($params) { } - private function setData($key, $value) { - - if(!isset($value)) return; - - if(session_id() == '') { - session_start(); - } - $_SESSION['shipping_apiship'][$key] = $value; - - } - - private function getData($key) { - - if(session_id() == '') { - session_start(); - } - - if (isset($_SESSION['shipping_apiship'][$key])) return $_SESSION['shipping_apiship'][$key]; - return null; - } - private function apiship_point($id) { $data = $this->apiship->apiship_point_by_params(['id=' . $id]); if (isset($data[0])) return $data[0]; else return []; @@ -144,7 +124,7 @@ private function get_provider_name($provider_key) { } public function get_last_tracing_id() { - return $this->getData('shipping_apiship_last_tracing_id'); + return $this->apiship->getData('shipping_apiship_last_tracing_id'); } @@ -219,10 +199,7 @@ public function get_quote_list($address, $full_list = false) { $quote_data = []; $start_points = []; - $select_points = []; - - $select_point = []; - if (isset($this->session->data['shipping_apiship'])) $select_point = $this->session->data['shipping_apiship']; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); $region = (isset($address['zone']))?$address['zone']:''; $city = (isset($address['city']))?trim($address['city']):''; @@ -234,12 +211,12 @@ public function get_quote_list($address, $full_list = false) { $apiship_calculator_data = $this->apiship->apiship_calculator($country,$region,$city,$postcode,$ext_address,[],$this->cart->getProducts(),$this->getCartTotal(),$cash_on_delivery); $data = $apiship_calculator_data['body']; - $this->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); - $this->setData('shipping_apiship_region',$region); - $this->setData('shipping_apiship_city',$city); - $this->setData('shipping_apiship_postcode',$postcode); - $this->setData('shipping_apiship_ext_address',$ext_address); - $this->setData('shipping_apiship_country',$country); + $this->apiship->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); + $this->apiship->setData('shipping_apiship_region',$region); + $this->apiship->setData('shipping_apiship_city',$city); + $this->apiship->setData('shipping_apiship_postcode',$postcode); + $this->apiship->setData('shipping_apiship_ext_address',$ext_address); + $this->apiship->setData('shipping_apiship_country',$country); if ($full_list == false) { @@ -251,7 +228,10 @@ public function get_quote_list($address, $full_list = false) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; + foreach(array(1,2) as $pickup_type) { + + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; @@ -279,7 +259,7 @@ public function get_quote_list($address, $full_list = false) { if ($tariff['daysMax'] > $daysMaxAllPoints) $daysMaxAllPoints = $tariff['daysMax']; } - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error'; + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error' . '_' . $pickup_type; if (empty($start_points[$provider['providerKey']])) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; } elseif ($tariff['deliveryCost'] < $start_points[$provider['providerKey']]['deliveryCost']) { @@ -287,27 +267,28 @@ public function get_quote_list($address, $full_list = false) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; } - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if (isset($select_point[$provider['providerKey']])) - if ('apiship.' . $key == $select_point[$provider['providerKey']]['code']) { + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if (isset($select_points[$provider['providerKey']])) + if ('apiship.' . $key == $select_points[$provider['providerKey']]['code']) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; } - } - } - - } + } + } + + } +/* $this->apiship->toLog('get_quote_list debug', [ 'address' => $address, 'start_points' => $start_points, 'session' => $this->session->data, - 'select_point' => $select_point, + 'select_points' => $select_points, 'weight' => $this->cart->getWeight() ]); - +*/ if ($this->apiship_params['shipping_apiship_group_points']) { // все ПВЗ на одной карте @@ -317,7 +298,7 @@ public function get_quote_list($address, $full_list = false) { foreach($start_points as $provider_key => $element) { - $shipping_apiship_last_select_code = $this->getData('shipping_apiship_last_select_code'); + $shipping_apiship_last_select_code = $this->apiship->getData('shipping_apiship_last_select_code'); if (isset($shipping_apiship_last_select_code)) if ('apiship.' . $element['key'] == $shipping_apiship_last_select_code) { @@ -473,11 +454,14 @@ public function get_quote_list($address, $full_list = false) { foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - - if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + foreach(array(1,2) as $pickup_type) { - $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId']; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + + $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $pickup_type; $quote_data[$key] = [ 'code' => 'apiship.' . $key, 'title' => $this->get_title([ @@ -493,19 +477,20 @@ public function get_quote_list($address, $full_list = false) { 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], 'text' => $this->currency->format($this->tax->calculate($tariff['deliveryCost'], $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) ]; + } } } - /* + if ($this->apiship_params['shipping_apiship_error_stub_show']) { if(empty($quote_data)) { // нет данных, потому что таймаут $title = $this->apiship_params['shipping_apiship_error_timeout']; // нет данных, потому что ошибка if (isset($data['message'])) { - //$title = $this->apiship_params['shipping_apiship_error_calculator']; + $title = $data['message']; if (isset($data['errors'])) $errors = $data['errors']; else $errors = []; foreach($errors as $error) { @@ -525,7 +510,8 @@ public function get_quote_list($address, $full_list = false) { ); } - */ + } + if(!empty($quote_data)) $method_data = array( @@ -555,7 +541,9 @@ private function get_title($params) { if (isset($params['daysMax'])) $daysMax = $params['daysMax']; else $daysMax = ''; if (isset($params['tariffDescription'])) $tariffDescription = $params['tariffDescription']; else $tariffDescription = ''; - $template = '%type %company %time'; + $template = '%type %company'; + if (strpos($this->apiship_params['shipping_apiship_template'],'%time')!==false) $template = $template . ' %time'; + if ($params['template'] == 'shipping_apiship_template') $template = $this->apiship_params['shipping_apiship_template']; $type_name = ''; @@ -625,12 +613,17 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; + foreach(array(1,2) as $pickup_type) { + + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + foreach($tariff['pointIds'] as $point_id) { if (!in_array($point_id,$points_ids)) $points_ids[] = $point_id; } } } + } $points = $this->apiship->apiship_points($points_ids); foreach($points as $point) @@ -665,9 +658,12 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; + foreach(array(1,2) as $pickup_type) { - $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; if (!isset($data_points[$point_id])) continue; $point = $data_points[$point_id]; $cost = $tariff['deliveryCost']; @@ -708,6 +704,7 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr } } } + } usort($all_points, function($a, $b) { @@ -769,11 +766,11 @@ public function get_points() { if (isset($this->request->post['code'])) $code = $this->request->post['code']; else $code = ''; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); $this->apiship->toLog('get_points', [ 'country' => $country, @@ -789,9 +786,14 @@ public function get_points() { $provider = [$parce_code['provider']]; if ($this->apiship_params['shipping_apiship_group_points']) $provider = []; - $points = $this->get_points_array($country, $region, $city, $postcode, $ext_address, $provider); + $data = $this->get_points_array($country, $region, $city, $postcode, $ext_address); + + $points = []; + foreach($data['points'] as $point) { + if ($point['provider_key']==$parce_code['provider']) $points[] = $point; + } - echo json_encode($points); + echo json_encode(['error' => $data['error'],'points' => $points]); } @@ -806,14 +808,13 @@ public function set_point() { $delivery_type = $parce_code['delivery_type']; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); $cost = -1; - $postcode = ''; $address1 = ''; $payment_method_code = (isset($this->session->data['payment_method']['code']))?$this->session->data['payment_method']['code']:''; $cash_on_delivery = in_array($payment_method_code, $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); @@ -826,8 +827,12 @@ public function set_point() { foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; + foreach(array(1,2) as $pickup_type) { + + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; if ('apiship.' . $key == $code) { $cost = $tariff['deliveryCost']; $point = $this->apiship_point($point_id); @@ -850,6 +855,7 @@ public function set_point() { } } } + } if ($cost == -1) { echo json_encode(['error' => $this->apiship_params['shipping_apiship_error_calculator']]); @@ -864,9 +870,12 @@ public function set_point() { 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) ]; - $this->session->data['shipping_apiship'][$parce_code['provider']] = $shipping_apiship; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); + $select_points[$parce_code['provider']] = $shipping_apiship; + $this->apiship->setData('shipping_apiship_select_points', $select_points); + $this->session->data['shipping_methods']['apiship']['quote'][$parce_code['short_code']] = $shipping_apiship; - $this->setData('shipping_apiship_last_select_code', $code); + $this->apiship->setData('shipping_apiship_last_select_code', $code); $shipping_apiship['postcode'] = $postcode; $shipping_apiship['address1'] = $address1; @@ -1043,7 +1052,6 @@ public function export_order() { $order_params['costAssessedCost'] = $this->apiship->format_cost($order['total'] - $order_totals['shipping']); - //$cash_on_delivery = in_array($order['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $paid_orders = in_array($order['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); $order_params['costCodCost'] = ($paid_orders==false) ? $this->apiship->format_cost($order['total']) : 0; @@ -1287,11 +1295,14 @@ private function set_apiship_order_status($apiship_order_id, $status_id) { $this->db->query("UPDATE `" . DB_PREFIX . "apiship_order` SET status = " . $status_id . " WHERE apiship_order_id = '" . (int)$apiship_order_id . "'"); } - private function get_pickup_type($provider) { - if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) return 2; - return 1; + private function get_pickup_types($provider) { + $pickup_types = []; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) $pickup_types[] = 2; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['courier_type'])) $pickup_types[] = 1; + return $pickup_types; } + private function get_status($apiship_order_status) { $key = $apiship_order_status['status']['key']; @@ -1358,13 +1369,14 @@ public function get_order_params() { $this->load->model('checkout/order'); $order_info = $this->model_checkout_order->getOrder($order_id); $apiship_paid = in_array($order_info['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); - //$cash_on_delivery = in_array($order_info['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $apiship_export = $this->is_order_export($order_id); $parce_code = $this->apiship->parce_code($order_info['shipping_code']); if (isset($parce_code['provider'])) $provider = $parce_code['provider']; else $provider = ''; - $apiship_pickup_type = $this->get_pickup_type($provider); + if (in_array(2, $this->get_pickup_types($provider))) $apiship_pickup_type = 2; else $apiship_pickup_type = 1; + + if (!empty($parce_code['pickup_type'])) $apiship_pickup_type = $parce_code['pickup_type']; $order_products = $this->getOrderProducts($order_id); diff --git a/2.1/upload/system/library/apiship/apiship.php b/2.1/upload/system/library/apiship/apiship.php index e414410..1d09bb4 100644 --- a/2.1/upload/system/library/apiship/apiship.php +++ b/2.1/upload/system/library/apiship/apiship.php @@ -97,6 +97,18 @@ function($curl, $header) use (&$headers) private function apiship_data($cmd, $limit, $filter_list = '') { + $data_key = 'apiship_' . $cmd . md5($limit . print_r($filter_list,1)); + $data_hash = md5($cmd . $limit . print_r($filter_list,1)); + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['rows']; + } + } + + $offset = 0; $rows = []; $x_tracing_ids = []; @@ -131,9 +143,38 @@ private function apiship_data($cmd, $limit, $filter_list = '') { $this->toLog('shipping_apiship_data '.$cmd, ['url' => $url, '$x_tracing_id' => $x_tracing_ids, 'output' => $rows]); + $this->setData($data_key, ['rows' => $rows, 'data_hash' => $data_hash]); return $rows; } + public function setData($key, $value, $expired_timeout_minuts = 10) { + + if(!isset($value)) return; + + if(session_id() == '') { + session_start(); + } + + if ($expired_timeout_minuts == 0) + $_SESSION['shipping_apiship'][$key] = ['value' => $value]; + else + $_SESSION['shipping_apiship'][$key] = ['value' => $value, 'time' => strtotime('now') + 60*$expired_timeout_minuts]; + + } + + public function getData($key) { + + if(session_id() == '') { + session_start(); + } + + if (!isset($_SESSION['shipping_apiship'][$key])) return null; + if (!isset($_SESSION['shipping_apiship'][$key]['time'])) $_SESSION['shipping_apiship'][$key]['value']; + if ((strtotime('now') > $_SESSION['shipping_apiship'][$key]['time'])) return null; + + return $_SESSION['shipping_apiship'][$key]['value']; + } + public function apiship_providers() { return $this->apiship_data('lists/providers', 10000); } @@ -196,6 +237,18 @@ public function apiship_waybills($order_id) { } public function apiship_points($points) { + + $data_hash = md5(print_r($points,1)); + $data_key = 'apiship_points'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['all_points']; + } + } + $all_points = []; $limit = 1000; $offset = 0; @@ -223,6 +276,7 @@ public function apiship_points($points) { $offset++; } + $this->setData($data_key, ['all_points' => $all_points, 'data_hash' => $data_hash]); return $all_points; } @@ -270,6 +324,8 @@ public function apiship_oc_order_status($order_id) { public function apiship_calculator($country, $region, $city, $postcode, $ext_address, $providers, $products, $total, $cash_on_delivery) { + $postcode = ''; + if (trim($city) == '') { $output['message'] = $this->apiship_params['shipping_apiship_error_select_city']; @@ -279,6 +335,17 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add ]; } + $data_hash = md5($country.$region.$city.$postcode.$ext_address.print_r($providers,1).print_r($products,1).$total.$cash_on_delivery); + $data_key = 'apiship_calculator'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data; + } + } + $calculate_data = $this->calculate_places($products, $total); $items = $calculate_data['items']; @@ -352,20 +419,22 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add $data = [ 'body' => json_decode($output['body'], true), - 'x-tracing-id' => $x_tracing_id + 'x-tracing-id' => $x_tracing_id, + 'data_hash' => $data_hash ]; $this->toLog('shipping_apiship_calculator', ['url' => $url, 'params' => $params, 'output' => $data], isset($data['body']['errors'])); if (isset($data['body']['errors'])) { - $output['message'] = $this->apiship_params['shipping_apiship_error_calculator']; + $output['message'] = sprintf($this->apiship_params['shipping_apiship_no_shipping'], $city .', '.$region); return [ 'body' => $output, - 'x-tracing-id' => '' + 'x-tracing-id' => $x_tracing_id ]; } + $this->setData($data_key, $data); return $data; } @@ -584,6 +653,7 @@ public function apiship_order_info($apiship_order_id) { public function get_address($params, $show_post_index = false) { + if (!isset($params['regionType'])) return ''; if ($params['regionType'] == 'г') { $address = $params['region']; @@ -675,21 +745,30 @@ public function get_integrator_statuses() { } public function parce_code($code) { - $code_parts = explode('.',$code); - if (isset($code_parts[1])) $tariff_parts = explode('_',$code_parts[1]); + $short_code = ""; + if (isset($code_parts[1])) { + $tariff_parts = explode('_',$code_parts[1]); + $short_code = $code_parts[1]; + } if (isset($tariff_parts[0])) $delivery_type = $tariff_parts[0]; else $delivery_type = ''; if (isset($tariff_parts[1])) $provider = $tariff_parts[1]; else $provider = ''; if (isset($tariff_parts[2])) $tariff_id = $tariff_parts[2]; else $tariff_id = ''; if (isset($tariff_parts[3])) $point_id = $tariff_parts[3]; else $point_id = ''; + if (isset($tariff_parts[4])) $pickup_type = $tariff_parts[4]; else $pickup_type = ''; + if ($delivery_type == 'door') { + $pickup_type = $point_id; + $point_id = ''; + } return [ 'delivery_type' => $delivery_type, 'provider' => $provider, 'tariff_id' => $tariff_id, 'point_id' => $point_id, - 'short_code' => $code_parts[1] + 'short_code' => $short_code, + 'pickup_type' => $pickup_type ]; } @@ -771,10 +850,10 @@ public function calculate_places($products, $total_sum) { if ($total_cost != 0) { if ($items[count($items)-1]['quantity'] > 1) { - $items[] = end($items); - $items[count($items)-2]['quantity'] = $items[count($items)-2]['quantity'] - 1; - $items[count($items)-1]['quantity'] = 1; - $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; + $items[] = end($items); + $items[count($items)-2]['quantity'] = $items[count($items)-2]['quantity'] - 1; + $items[count($items)-1]['quantity'] = 1; + $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; } else { $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; } diff --git a/2.3/install.xml b/2.3/install.xml index 789f3e6..adb9caf 100644 --- a/2.3/install.xml +++ b/2.3/install.xml @@ -2,7 +2,7 @@ ApiShip ApiShip - 0.8.7 (OpenCart 2.3) + 0.8.8 (OpenCart 2.3) ApiShip https://apiship.ru @@ -732,7 +732,10 @@ $this->_templateData['shipping_apiship_providers_keys'] = json_encode(array_keys($providers)); $this->_templateData['shipping_apiship_group_points'] = $this->config->get('shipping_apiship_group_points') ? 'true' : 'false'; - $code = $this->_templateData['shipping_method']['code']; + $code = ""; + if (is_array($this->_templateData['shipping_method']) && isset($this->_templateData['shipping_method']['code'])) { + $code = $this->_templateData['shipping_method']['code']; + } if ((strpos($code, 'apiship')!==false) && (strpos($code, 'error')!==false)) { diff --git a/2.3/upload/admin/controller/extension/shipping/apiship.php b/2.3/upload/admin/controller/extension/shipping/apiship.php index d785dca..a15c038 100644 --- a/2.3/upload/admin/controller/extension/shipping/apiship.php +++ b/2.3/upload/admin/controller/extension/shipping/apiship.php @@ -47,7 +47,7 @@ public function index() { $data['text_none'] = $this->language->get('text_none'); $data['text_shipping_apiship_cron_url_copy'] = $this->language->get('text_shipping_apiship_cron_url_copy'); - $data['shipping_apiship_version'] = '0.8.7 (OpenCart 2.3)'; + $data['shipping_apiship_version'] = '0.8.8 (OpenCart 2.3)'; $data['shipping_apiship_version_js_mod'] = rand(); $data['button_save'] = $this->language->get('button_save'); @@ -89,6 +89,7 @@ public function index() { $data['entry_shipping_apiship_tax_class'] = $this->language->get('entry_shipping_apiship_tax_class'); $data['entry_shipping_apiship_geo_zone'] = $this->language->get('entry_shipping_apiship_geo_zone'); $data['entry_shipping_apiship_icon_show'] = $this->language->get('entry_shipping_apiship_icon_show'); + $data['entry_shipping_apiship_error_stub_show'] = $this->language->get('entry_shipping_apiship_error_stub_show'); $data['entry_shipping_apiship_group_points'] = $this->language->get('entry_shipping_apiship_group_points'); $data['entry_shipping_apiship_prefix'] = $this->language->get('entry_shipping_apiship_prefix'); $data['entry_shipping_apiship_export_status'] = $this->language->get('entry_shipping_apiship_export_status'); @@ -109,6 +110,7 @@ public function index() { $data['entry_extra_settings'] = $this->language->get('entry_extra_settings'); $data['entry_providers_points'] = $this->language->get('entry_providers_points'); $data['entry_providers_point'] = $this->language->get('entry_providers_point'); + $data['entry_providers_courier'] = $this->language->get('entry_providers_courier'); $data['entry_events_mapping'] = $this->language->get('entry_events_mapping'); $data['entry_events_mapping_notify'] = $this->language->get('entry_events_mapping_notify'); @@ -413,6 +415,12 @@ public function index() { $data['shipping_apiship_prefix'] = $this->config->get('shipping_apiship_prefix'); } + if (isset($this->request->post['shipping_apiship_error_stub_show'])) { + $data['shipping_apiship_error_stub_show'] = $this->request->post['shipping_apiship_error_stub_show']; + } else { + $data['shipping_apiship_error_stub_show'] = $this->config->get('shipping_apiship_error_stub_show'); + } + if (isset($this->request->post['shipping_apiship_mode'])) { $data['shipping_apiship_mode'] = $this->request->post['shipping_apiship_mode']; } else { diff --git a/2.3/upload/admin/language/en-gb/extension/shipping/apiship.php b/2.3/upload/admin/language/en-gb/extension/shipping/apiship.php index 129db0a..f77066b 100644 --- a/2.3/upload/admin/language/en-gb/extension/shipping/apiship.php +++ b/2.3/upload/admin/language/en-gb/extension/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Show icon'; $_['entry_shipping_apiship_prefix'] = 'Order prefix'; +$_['entry_shipping_apiship_error_stub_show'] = 'Show a stub in the cart about an error'; $_['entry_shipping_apiship_mode'] = 'Working mode'; $_['entry_shipping_apiship_mode_normal'] = 'Normal'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Extra settings'; $_['entry_providers_points'] = 'DC points'; $_['entry_providers_point'] = 'DC point'; +$_['entry_providers_courier'] = 'Picked up by courier'; $_['entry_events_mapping'] = 'Status mapping table'; $_['entry_events_mapping_notify'] = 'Notify'; diff --git a/2.3/upload/admin/language/ru-ru/extension/shipping/apiship.php b/2.3/upload/admin/language/ru-ru/extension/shipping/apiship.php index 3bf9917..60ff5dc 100644 --- a/2.3/upload/admin/language/ru-ru/extension/shipping/apiship.php +++ b/2.3/upload/admin/language/ru-ru/extension/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Показывать иконку'; $_['entry_shipping_apiship_prefix'] = 'Префикс номера заказа'; +$_['entry_shipping_apiship_error_stub_show'] = 'Показывать заглушку в корзине об ошибке'; $_['entry_shipping_apiship_mode'] = 'Режим работы'; $_['entry_shipping_apiship_mode_normal'] = 'Нормальный'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Дополнительные параметры'; $_['entry_providers_points'] = 'Параметры отгрузки в службы доставки'; $_['entry_providers_point'] = 'Привоз на склад'; +$_['entry_providers_courier'] = 'Забирает курьер'; $_['entry_events_mapping'] = 'Параметры сопоставления статусов'; $_['entry_events_mapping_notify'] = 'Оповестить'; diff --git a/2.3/upload/admin/view/template/extension/shipping/apiship.tpl b/2.3/upload/admin/view/template/extension/shipping/apiship.tpl index 32774ee..0285c2b 100644 --- a/2.3/upload/admin/view/template/extension/shipping/apiship.tpl +++ b/2.3/upload/admin/view/template/extension/shipping/apiship.tpl @@ -384,9 +384,9 @@
@@ -397,6 +397,21 @@
+ +
+ + +
+ +
+
+ @@ -519,6 +534,21 @@ +
+ +
+
+ +
+
+
+
@@ -704,12 +734,12 @@ function set_providers_checkbox(checked, provider) { } -$(".shipping_apiship_provider_checkbox").on("click", function () { +$(".shipping_apiship_provider_point").on("click", function () { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); -jQuery('.shipping_apiship_provider_checkbox').each(function() { +jQuery('.shipping_apiship_provider_point').each(function() { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); diff --git a/2.3/upload/catalog/model/extension/shipping/apiship.php b/2.3/upload/catalog/model/extension/shipping/apiship.php index 8ce007a..40187e7 100644 --- a/2.3/upload/catalog/model/extension/shipping/apiship.php +++ b/2.3/upload/catalog/model/extension/shipping/apiship.php @@ -57,6 +57,7 @@ public function __construct($params) { 'shipping_apiship_mode' => $this->config->get('shipping_apiship_mode'), 'shipping_apiship_prefix' => $this->config->get('shipping_apiship_prefix'), + 'shipping_apiship_error_stub_show' => $this->config->get('shipping_apiship_error_stub_show') ? true : false, 'shipping_apiship_title' => $this->config->get('shipping_apiship_title'), 'shipping_apiship_template' => html_entity_decode($this->config->get('shipping_apiship_template')), @@ -90,27 +91,6 @@ public function __construct($params) { } - private function setData($key, $value) { - - if(!isset($value)) return; - - if(session_id() == '') { - session_start(); - } - $_SESSION['shipping_apiship'][$key] = $value; - - } - - private function getData($key) { - - if(session_id() == '') { - session_start(); - } - - if (isset($_SESSION['shipping_apiship'][$key])) return $_SESSION['shipping_apiship'][$key]; - return null; - } - private function apiship_point($id) { $data = $this->apiship->apiship_point_by_params(['id=' . $id]); if (isset($data[0])) return $data[0]; else return []; @@ -144,7 +124,7 @@ private function get_provider_name($provider_key) { } public function get_last_tracing_id() { - return $this->getData('shipping_apiship_last_tracing_id'); + return $this->apiship->getData('shipping_apiship_last_tracing_id'); } @@ -228,10 +208,7 @@ public function get_quote_list($address, $full_list = false) { $quote_data = []; $start_points = []; - $select_points = []; - - $select_point = []; - if (isset($this->session->data['shipping_apiship'])) $select_point = $this->session->data['shipping_apiship']; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); $region = (isset($address['zone']))?$address['zone']:''; $city = (isset($address['city']))?trim($address['city']):''; @@ -240,15 +217,16 @@ public function get_quote_list($address, $full_list = false) { $country = (isset($address['iso_code_2']))?trim($address['iso_code_2']):''; $payment_method_code = (isset($this->session->data['payment_method']['code']))?$this->session->data['payment_method']['code']:''; $cash_on_delivery = in_array($payment_method_code, $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); + $apiship_calculator_data = $this->apiship->apiship_calculator($country,$region,$city,$postcode,$ext_address,[],$this->cart->getProducts(),$this->getCartTotal(),$cash_on_delivery); $data = $apiship_calculator_data['body']; - $this->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); - $this->setData('shipping_apiship_region',$region); - $this->setData('shipping_apiship_city',$city); - $this->setData('shipping_apiship_postcode',$postcode); - $this->setData('shipping_apiship_ext_address',$ext_address); - $this->setData('shipping_apiship_country',$country); + $this->apiship->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); + $this->apiship->setData('shipping_apiship_region',$region); + $this->apiship->setData('shipping_apiship_city',$city); + $this->apiship->setData('shipping_apiship_postcode',$postcode); + $this->apiship->setData('shipping_apiship_ext_address',$ext_address); + $this->apiship->setData('shipping_apiship_country',$country); if ($full_list == false) { @@ -260,63 +238,67 @@ public function get_quote_list($address, $full_list = false) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - - if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + foreach(array(1,2) as $pickup_type) { - if (empty($daysMin[$provider['providerKey']])) { - $daysMin[$provider['providerKey']] = $tariff['daysMin']; - } else { - if ($tariff['daysMin'] < $daysMin[$provider['providerKey']]) $daysMin[$provider['providerKey']] = $tariff['daysMin']; - } + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; - if (empty($daysMax[$provider['providerKey']])) { - $daysMax[$provider['providerKey']] = $tariff['daysMax']; - } else { - if ($tariff['daysMax'] > $daysMax[$provider['providerKey']]) $daysMax[$provider['providerKey']] = $tariff['daysMax']; - } - - if (empty($daysMinAllPoints)) { - $daysMinAllPoints = $tariff['daysMin']; - } else { - if ($tariff['daysMin'] < $daysMinAllPoints) $daysMinAllPoints = $tariff['daysMin']; - } - - if (empty($daysMaxAllPoints)) { - $daysMaxAllPoints = $tariff['daysMax']; - } else { - if ($tariff['daysMax'] > $daysMaxAllPoints) $daysMaxAllPoints = $tariff['daysMax']; - } + if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error'; - if (empty($start_points[$provider['providerKey']])) { - $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; - } elseif ($tariff['deliveryCost'] < $start_points[$provider['providerKey']]['deliveryCost']) { - if (strpos($start_points[$provider['providerKey']]['key'],'error')!==false) + if (empty($daysMin[$provider['providerKey']])) { + $daysMin[$provider['providerKey']] = $tariff['daysMin']; + } else { + if ($tariff['daysMin'] < $daysMin[$provider['providerKey']]) $daysMin[$provider['providerKey']] = $tariff['daysMin']; + } + + if (empty($daysMax[$provider['providerKey']])) { + $daysMax[$provider['providerKey']] = $tariff['daysMax']; + } else { + if ($tariff['daysMax'] > $daysMax[$provider['providerKey']]) $daysMax[$provider['providerKey']] = $tariff['daysMax']; + } + + if (empty($daysMinAllPoints)) { + $daysMinAllPoints = $tariff['daysMin']; + } else { + if ($tariff['daysMin'] < $daysMinAllPoints) $daysMinAllPoints = $tariff['daysMin']; + } + + if (empty($daysMaxAllPoints)) { + $daysMaxAllPoints = $tariff['daysMax']; + } else { + if ($tariff['daysMax'] > $daysMaxAllPoints) $daysMaxAllPoints = $tariff['daysMax']; + } + + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error' . '_' . $pickup_type; + if (empty($start_points[$provider['providerKey']])) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; - } - - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if (isset($select_point[$provider['providerKey']])) - if ('apiship.' . $key == $select_point[$provider['providerKey']]['code']) { + } elseif ($tariff['deliveryCost'] < $start_points[$provider['providerKey']]['deliveryCost']) { + if (strpos($start_points[$provider['providerKey']]['key'],'error')!==false) + $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; + } + + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if (isset($select_points[$provider['providerKey']])) + if ('apiship.' . $key == $select_points[$provider['providerKey']]['code']) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; } - } - } - - } + } + } + + } +/* $this->apiship->toLog('get_quote_list debug', [ 'address' => $address, 'start_points' => $start_points, 'session' => $this->session->data, - 'select_point' => $select_point, + 'select_points' => $select_points, 'weight' => $this->cart->getWeight() ]); - +*/ if ($this->apiship_params['shipping_apiship_group_points']) { // все ПВЗ на одной карте @@ -326,7 +308,7 @@ public function get_quote_list($address, $full_list = false) { foreach($start_points as $provider_key => $element) { - $shipping_apiship_last_select_code = $this->getData('shipping_apiship_last_select_code'); + $shipping_apiship_last_select_code = $this->apiship->getData('shipping_apiship_last_select_code'); if (isset($shipping_apiship_last_select_code)) if ('apiship.' . $element['key'] == $shipping_apiship_last_select_code) { @@ -482,39 +464,43 @@ public function get_quote_list($address, $full_list = false) { foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; + foreach(array(1,2) as $pickup_type) { - if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; - - $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId']; - $quote_data[$key] = [ - 'code' => 'apiship.' . $key, - 'title' => $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'door', - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]), - 'cost' => $tariff['deliveryCost'], - 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], - 'text' => $this->currency->format($this->tax->calculate($tariff['deliveryCost'], $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) - ]; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + + $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $pickup_type; + $quote_data[$key] = [ + 'code' => 'apiship.' . $key, + 'title' => $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'door', + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]), + 'cost' => $tariff['deliveryCost'], + 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], + 'text' => $this->currency->format($this->tax->calculate($tariff['deliveryCost'], $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) + ]; + } } } - /* + if ($this->apiship_params['shipping_apiship_error_stub_show']) { if(empty($quote_data)) { // нет данных, потому что таймаут $title = $this->apiship_params['shipping_apiship_error_timeout']; // нет данных, потому что ошибка if (isset($data['message'])) { - //$title = $this->apiship_params['shipping_apiship_error_calculator']; + $title = $data['message']; if (isset($data['errors'])) $errors = $data['errors']; else $errors = []; foreach($errors as $error) { @@ -533,8 +519,9 @@ public function get_quote_list($address, $full_list = false) { 'text' => $this->currency->format(0, $this->apiship_params['shipping_apiship_rub_select']) ); + } } - */ + if(!empty($quote_data)) $method_data = array( @@ -564,7 +551,9 @@ private function get_title($params) { if (isset($params['daysMax'])) $daysMax = $params['daysMax']; else $daysMax = ''; if (isset($params['tariffDescription'])) $tariffDescription = $params['tariffDescription']; else $tariffDescription = ''; - $template = '%type %company %time'; + $template = '%type %company'; + if (strpos($this->apiship_params['shipping_apiship_template'],'%time')!==false) $template = $template . ' %time'; + if ($params['template'] == 'shipping_apiship_template') $template = $this->apiship_params['shipping_apiship_template']; $type_name = ''; @@ -634,12 +623,17 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - foreach($tariff['pointIds'] as $point_id) { - if (!in_array($point_id,$points_ids)) $points_ids[] = $point_id; - } + foreach(array(1,2) as $pickup_type) { + + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + foreach($tariff['pointIds'] as $point_id) { + if (!in_array($point_id,$points_ids)) $points_ids[] = $point_id; + } } } + } $points = $this->apiship->apiship_points($points_ids); foreach($points as $point) @@ -674,46 +668,50 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - - $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if (!isset($data_points[$point_id])) continue; - $point = $data_points[$point_id]; - $cost = $tariff['deliveryCost']; - - $all_points[] = [ - 'lon' => $point['lon'], - 'lat' => $point['lat'], - 'code' => 'apiship.' . $code, - - 'tariff' => $tariff['tariffName'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], + foreach(array(1,2) as $pickup_type) { - 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']), - 'cost' => round($cost), - - 'title' => $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'point', - 'sub_type' => $point['type'], - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'pointName' => $point['name'], - 'pointAddress' => $point['address'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]), - - 'type' => $apiship_point_types[$point['type']-1], - 'provider' => $apiship_providers[$provider['providerKey']], - 'provider_key' => $provider['providerKey'], - - //'phones' => $point['phone'], - //'workTime' => $point['workTime'], - - ]; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if (!isset($data_points[$point_id])) continue; + $point = $data_points[$point_id]; + $cost = $tariff['deliveryCost']; + + $all_points[] = [ + 'lon' => $point['lon'], + 'lat' => $point['lat'], + 'code' => 'apiship.' . $code, + + 'tariff' => $tariff['tariffName'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + + 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']), + 'cost' => round($cost), + + 'title' => $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'point', + 'sub_type' => $point['type'], + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'pointName' => $point['name'], + 'pointAddress' => $point['address'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]), + + 'type' => $apiship_point_types[$point['type']-1], + 'provider' => $apiship_providers[$provider['providerKey']], + 'provider_key' => $provider['providerKey'], + + //'phones' => $point['phone'], + //'workTime' => $point['workTime'], + + ]; + } } } } @@ -778,11 +776,11 @@ public function get_points() { if (isset($this->request->post['code'])) $code = $this->request->post['code']; else $code = ''; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); $this->apiship->toLog('get_points', [ 'country' => $country, @@ -798,9 +796,14 @@ public function get_points() { $provider = [$parce_code['provider']]; if ($this->apiship_params['shipping_apiship_group_points']) $provider = []; - $points = $this->get_points_array($country, $region, $city, $postcode, $ext_address, $provider); + $data = $this->get_points_array($country, $region, $city, $postcode, $ext_address); + + $points = []; + foreach($data['points'] as $point) { + if ($point['provider_key']==$parce_code['provider']) $points[] = $point; + } - echo json_encode($points); + echo json_encode(['error' => $data['error'],'points' => $points]); } @@ -815,14 +818,13 @@ public function set_point() { $delivery_type = $parce_code['delivery_type']; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); $cost = -1; - $postcode = ''; $address1 = ''; $payment_method_code = (isset($this->session->data['payment_method']['code']))?$this->session->data['payment_method']['code']:''; $cash_on_delivery = in_array($payment_method_code, $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); @@ -835,29 +837,34 @@ public function set_point() { foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if ('apiship.' . $key == $code) { - $cost = $tariff['deliveryCost']; - $point = $this->apiship_point($point_id); + foreach(array(1,2) as $pickup_type) { - $postcode = $point['postIndex']; - $address1 = $this->apiship->get_address($point); - $title = $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'point', - 'sub_type' => $point['type'], - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'pointName' => $point['name'], - 'pointAddress' => $point['address'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]); + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if ('apiship.' . $key == $code) { + $cost = $tariff['deliveryCost']; + $point = $this->apiship_point($point_id); + + $postcode = $point['postIndex']; + $address1 = $this->apiship->get_address($point); + $title = $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'point', + 'sub_type' => $point['type'], + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'pointName' => $point['name'], + 'pointAddress' => $point['address'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]); + } } } - } + } } if ($cost == -1) { @@ -873,9 +880,12 @@ public function set_point() { 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) ]; - $this->session->data['shipping_apiship'][$parce_code['provider']] = $shipping_apiship; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); + $select_points[$parce_code['provider']] = $shipping_apiship; + $this->apiship->setData('shipping_apiship_select_points', $select_points); + $this->session->data['shipping_methods']['apiship']['quote'][$parce_code['short_code']] = $shipping_apiship; - $this->setData('shipping_apiship_last_select_code', $code); + $this->apiship->setData('shipping_apiship_last_select_code', $code); $shipping_apiship['postcode'] = $postcode; $shipping_apiship['address1'] = $address1; @@ -1052,7 +1062,6 @@ public function export_order() { $order_params['costAssessedCost'] = $this->apiship->format_cost($order['total'] - $order_totals['shipping']); - //$cash_on_delivery = in_array($order['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $paid_orders = in_array($order['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); $order_params['costCodCost'] = ($paid_orders==false) ? $this->apiship->format_cost($order['total']) : 0; @@ -1296,11 +1305,14 @@ private function set_apiship_order_status($apiship_order_id, $status_id) { $this->db->query("UPDATE `" . DB_PREFIX . "apiship_order` SET status = " . $status_id . " WHERE apiship_order_id = '" . (int)$apiship_order_id . "'"); } - private function get_pickup_type($provider) { - if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) return 2; - return 1; + private function get_pickup_types($provider) { + $pickup_types = []; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) $pickup_types[] = 2; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['courier_type'])) $pickup_types[] = 1; + return $pickup_types; } + private function get_status($apiship_order_status) { $key = $apiship_order_status['status']['key']; @@ -1367,13 +1379,14 @@ public function get_order_params() { $this->load->model('checkout/order'); $order_info = $this->model_checkout_order->getOrder($order_id); $apiship_paid = in_array($order_info['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); - //$cash_on_delivery = in_array($order_info['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $apiship_export = $this->is_order_export($order_id); $parce_code = $this->apiship->parce_code($order_info['shipping_code']); if (isset($parce_code['provider'])) $provider = $parce_code['provider']; else $provider = ''; - $apiship_pickup_type = $this->get_pickup_type($provider); + if (in_array(2, $this->get_pickup_types($provider))) $apiship_pickup_type = 2; else $apiship_pickup_type = 1; + + if (!empty($parce_code['pickup_type'])) $apiship_pickup_type = $parce_code['pickup_type']; $order_products = $this->getOrderProducts($order_id); diff --git a/2.3/upload/system/library/apiship/apiship.php b/2.3/upload/system/library/apiship/apiship.php index 44cda5e..c33f76f 100644 --- a/2.3/upload/system/library/apiship/apiship.php +++ b/2.3/upload/system/library/apiship/apiship.php @@ -97,6 +97,18 @@ function($curl, $header) use (&$headers) private function apiship_data($cmd, $limit, $filter_list = '') { + $data_key = 'apiship_' . $cmd . md5($limit . print_r($filter_list,1)); + $data_hash = md5($cmd . $limit . print_r($filter_list,1)); + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['rows']; + } + } + + $offset = 0; $rows = []; $x_tracing_ids = []; @@ -131,9 +143,38 @@ private function apiship_data($cmd, $limit, $filter_list = '') { $this->toLog('shipping_apiship_data '.$cmd, ['url' => $url, '$x_tracing_id' => $x_tracing_ids, 'output' => $rows]); + $this->setData($data_key, ['rows' => $rows, 'data_hash' => $data_hash]); return $rows; } + public function setData($key, $value, $expired_timeout_minuts = 10) { + + if(!isset($value)) return; + + if(session_id() == '') { + session_start(); + } + + if ($expired_timeout_minuts == 0) + $_SESSION['shipping_apiship'][$key] = ['value' => $value]; + else + $_SESSION['shipping_apiship'][$key] = ['value' => $value, 'time' => strtotime('now') + 60*$expired_timeout_minuts]; + + } + + public function getData($key) { + + if(session_id() == '') { + session_start(); + } + + if (!isset($_SESSION['shipping_apiship'][$key])) return null; + if (!isset($_SESSION['shipping_apiship'][$key]['time'])) $_SESSION['shipping_apiship'][$key]['value']; + if ((strtotime('now') > $_SESSION['shipping_apiship'][$key]['time'])) return null; + + return $_SESSION['shipping_apiship'][$key]['value']; + } + public function apiship_providers() { return $this->apiship_data('lists/providers', 10000); } @@ -196,6 +237,18 @@ public function apiship_waybills($order_id) { } public function apiship_points($points) { + + $data_hash = md5(print_r($points,1)); + $data_key = 'apiship_points'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['all_points']; + } + } + $all_points = []; $limit = 1000; $offset = 0; @@ -223,6 +276,7 @@ public function apiship_points($points) { $offset++; } + $this->setData($data_key, ['all_points' => $all_points, 'data_hash' => $data_hash]); return $all_points; } @@ -269,6 +323,8 @@ public function apiship_oc_order_status($order_id) { } public function apiship_calculator($country, $region, $city, $postcode, $ext_address, $providers, $products, $total, $cash_on_delivery) { + + $postcode = ''; if (trim($city) == '') { $output['message'] = $this->apiship_params['shipping_apiship_error_select_city']; @@ -279,6 +335,17 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add ]; } + $data_hash = md5($country.$region.$city.$postcode.$ext_address.print_r($providers,1).print_r($products,1).$total.$cash_on_delivery); + $data_key = 'apiship_calculator'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data; + } + } + $calculate_data = $this->calculate_places($products, $total); $items = $calculate_data['items']; @@ -352,20 +419,22 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add $data = [ 'body' => json_decode($output['body'], true), - 'x-tracing-id' => $x_tracing_id + 'x-tracing-id' => $x_tracing_id, + 'data_hash' => $data_hash ]; $this->toLog('shipping_apiship_calculator', ['url' => $url, 'params' => $params, 'output' => $data], isset($data['body']['errors'])); if (isset($data['body']['errors'])) { - $output['message'] = $this->apiship_params['shipping_apiship_error_calculator']; + $output['message'] = sprintf($this->apiship_params['shipping_apiship_no_shipping'], $city .', '.$region); return [ 'body' => $output, - 'x-tracing-id' => '' + 'x-tracing-id' => $x_tracing_id ]; } + $this->setData($data_key, $data); return $data; } @@ -584,6 +653,7 @@ public function apiship_order_info($apiship_order_id) { public function get_address($params, $show_post_index = false) { + if (!isset($params['regionType'])) return ''; if ($params['regionType'] == 'г') { $address = $params['region']; @@ -675,21 +745,30 @@ public function get_integrator_statuses() { } public function parce_code($code) { - $code_parts = explode('.',$code); - if (isset($code_parts[1])) $tariff_parts = explode('_',$code_parts[1]); + $short_code = ""; + if (isset($code_parts[1])) { + $tariff_parts = explode('_',$code_parts[1]); + $short_code = $code_parts[1]; + } if (isset($tariff_parts[0])) $delivery_type = $tariff_parts[0]; else $delivery_type = ''; if (isset($tariff_parts[1])) $provider = $tariff_parts[1]; else $provider = ''; if (isset($tariff_parts[2])) $tariff_id = $tariff_parts[2]; else $tariff_id = ''; if (isset($tariff_parts[3])) $point_id = $tariff_parts[3]; else $point_id = ''; + if (isset($tariff_parts[4])) $pickup_type = $tariff_parts[4]; else $pickup_type = ''; + if ($delivery_type == 'door') { + $pickup_type = $point_id; + $point_id = ''; + } return [ 'delivery_type' => $delivery_type, 'provider' => $provider, 'tariff_id' => $tariff_id, 'point_id' => $point_id, - 'short_code' => $code_parts[1] + 'short_code' => $short_code, + 'pickup_type' => $pickup_type ]; } @@ -771,10 +850,10 @@ public function calculate_places($products, $total_sum) { if ($total_cost != 0) { if ($items[count($items)-1]['quantity'] > 1) { - $items[] = end($items); - $items[count($items)-2]['quantity'] = $items[count($items)-2]['quantity'] - 1; - $items[count($items)-1]['quantity'] = 1; - $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; + $items[] = end($items); + $items[count($items)-2]['quantity'] = $items[count($items)-2]['quantity'] - 1; + $items[count($items)-1]['quantity'] = 1; + $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; } else { $items[count($items)-1]['cost'] = $items[count($items)-1]['cost'] + $total_cost; } diff --git a/3.x/install.xml b/3.x/install.xml index 5ecc717..ac2e99a 100644 --- a/3.x/install.xml +++ b/3.x/install.xml @@ -2,7 +2,7 @@ ApiShip ApiShip - 0.8.7 (OpenCart 3.x) + 0.8.8 (OpenCart 3.x) ApiShip https://apiship.ru @@ -731,7 +731,10 @@ $this->_templateData['shipping_apiship_providers_keys'] = json_encode(array_keys($providers)); $this->_templateData['shipping_apiship_group_points'] = $this->config->get('shipping_apiship_group_points') ? 'true' : 'false'; - $code = $this->_templateData['shipping_method']['code']; + $code = ""; + if (is_array($this->_templateData['shipping_method']) && isset($this->_templateData['shipping_method']['code'])) { + $code = $this->_templateData['shipping_method']['code']; + } if ((strpos($code, 'apiship')!==false) && (strpos($code, 'error')!==false)) { diff --git a/3.x/upload/admin/controller/extension/shipping/apiship.php b/3.x/upload/admin/controller/extension/shipping/apiship.php index c525721..3cdf8ca 100644 --- a/3.x/upload/admin/controller/extension/shipping/apiship.php +++ b/3.x/upload/admin/controller/extension/shipping/apiship.php @@ -46,7 +46,7 @@ public function index() { $data['text_none'] = $this->language->get('text_none'); $data['text_shipping_apiship_cron_url_copy'] = $this->language->get('text_shipping_apiship_cron_url_copy'); - $data['shipping_apiship_version'] = '0.8.7 (OpenCart 3.x)'; + $data['shipping_apiship_version'] = '0.8.8 (OpenCart 3.x)'; $data['shipping_apiship_version_js_mod'] = rand(); $data['button_save'] = $this->language->get('button_save'); @@ -88,6 +88,7 @@ public function index() { $data['entry_shipping_apiship_tax_class'] = $this->language->get('entry_shipping_apiship_tax_class'); $data['entry_shipping_apiship_geo_zone'] = $this->language->get('entry_shipping_apiship_geo_zone'); $data['entry_shipping_apiship_icon_show'] = $this->language->get('entry_shipping_apiship_icon_show'); + $data['entry_shipping_apiship_error_stub_show'] = $this->language->get('entry_shipping_apiship_error_stub_show'); $data['entry_shipping_apiship_group_points'] = $this->language->get('entry_shipping_apiship_group_points'); $data['entry_shipping_apiship_prefix'] = $this->language->get('entry_shipping_apiship_prefix'); $data['entry_shipping_apiship_export_status'] = $this->language->get('entry_shipping_apiship_export_status'); @@ -108,6 +109,7 @@ public function index() { $data['entry_extra_settings'] = $this->language->get('entry_extra_settings'); $data['entry_providers_points'] = $this->language->get('entry_providers_points'); $data['entry_providers_point'] = $this->language->get('entry_providers_point'); + $data['entry_providers_courier'] = $this->language->get('entry_providers_courier'); $data['entry_events_mapping'] = $this->language->get('entry_events_mapping'); $data['entry_events_mapping_notify'] = $this->language->get('entry_events_mapping_notify'); @@ -411,6 +413,12 @@ public function index() { } else { $data['shipping_apiship_prefix'] = $this->config->get('shipping_apiship_prefix'); } + + if (isset($this->request->post['shipping_apiship_error_stub_show'])) { + $data['shipping_apiship_error_stub_show'] = $this->request->post['shipping_apiship_error_stub_show']; + } else { + $data['shipping_apiship_error_stub_show'] = $this->config->get('shipping_apiship_error_stub_show'); + } if (isset($this->request->post['shipping_apiship_mode'])) { $data['shipping_apiship_mode'] = $this->request->post['shipping_apiship_mode']; diff --git a/3.x/upload/admin/language/en-gb/extension/shipping/apiship.php b/3.x/upload/admin/language/en-gb/extension/shipping/apiship.php index 129db0a..f77066b 100644 --- a/3.x/upload/admin/language/en-gb/extension/shipping/apiship.php +++ b/3.x/upload/admin/language/en-gb/extension/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Show icon'; $_['entry_shipping_apiship_prefix'] = 'Order prefix'; +$_['entry_shipping_apiship_error_stub_show'] = 'Show a stub in the cart about an error'; $_['entry_shipping_apiship_mode'] = 'Working mode'; $_['entry_shipping_apiship_mode_normal'] = 'Normal'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Extra settings'; $_['entry_providers_points'] = 'DC points'; $_['entry_providers_point'] = 'DC point'; +$_['entry_providers_courier'] = 'Picked up by courier'; $_['entry_events_mapping'] = 'Status mapping table'; $_['entry_events_mapping_notify'] = 'Notify'; diff --git a/3.x/upload/admin/language/ru-ru/extension/shipping/apiship.php b/3.x/upload/admin/language/ru-ru/extension/shipping/apiship.php index 3bf9917..60ff5dc 100644 --- a/3.x/upload/admin/language/ru-ru/extension/shipping/apiship.php +++ b/3.x/upload/admin/language/ru-ru/extension/shipping/apiship.php @@ -86,6 +86,7 @@ $_['entry_shipping_apiship_icon_show'] = 'Показывать иконку'; $_['entry_shipping_apiship_prefix'] = 'Префикс номера заказа'; +$_['entry_shipping_apiship_error_stub_show'] = 'Показывать заглушку в корзине об ошибке'; $_['entry_shipping_apiship_mode'] = 'Режим работы'; $_['entry_shipping_apiship_mode_normal'] = 'Нормальный'; @@ -100,6 +101,7 @@ $_['entry_extra_settings'] = 'Дополнительные параметры'; $_['entry_providers_points'] = 'Параметры отгрузки в службы доставки'; $_['entry_providers_point'] = 'Привоз на склад'; +$_['entry_providers_courier'] = 'Забирает курьер'; $_['entry_events_mapping'] = 'Параметры сопоставления статусов'; $_['entry_events_mapping_notify'] = 'Оповестить'; diff --git a/3.x/upload/admin/view/template/extension/shipping/apiship.twig b/3.x/upload/admin/view/template/extension/shipping/apiship.twig index 4aa9ea6..84177b8 100644 --- a/3.x/upload/admin/view/template/extension/shipping/apiship.twig +++ b/3.x/upload/admin/view/template/extension/shipping/apiship.twig @@ -384,19 +384,35 @@
-
+ +
+ + +
+ + +
+ +
+
{% endfor %} @@ -519,6 +535,21 @@ +
+ +
+
+ +
+
+
+
@@ -704,12 +735,12 @@ function set_providers_checkbox(checked, provider) { } -$(".shipping_apiship_provider_checkbox").on("click", function () { +$(".shipping_apiship_provider_point").on("click", function () { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); -jQuery('.shipping_apiship_provider_checkbox').each(function() { +jQuery('.shipping_apiship_provider_point').each(function() { provider = $(this).attr('data-id') set_providers_checkbox(this.checked, provider) }); diff --git a/3.x/upload/catalog/model/extension/shipping/apiship.php b/3.x/upload/catalog/model/extension/shipping/apiship.php index 5879e1d..7efcd50 100644 --- a/3.x/upload/catalog/model/extension/shipping/apiship.php +++ b/3.x/upload/catalog/model/extension/shipping/apiship.php @@ -57,6 +57,7 @@ public function __construct($params) { 'shipping_apiship_mode' => $this->config->get('shipping_apiship_mode'), 'shipping_apiship_prefix' => $this->config->get('shipping_apiship_prefix'), + 'shipping_apiship_error_stub_show' => $this->config->get('shipping_apiship_error_stub_show') ? true : false, 'shipping_apiship_title' => $this->config->get('shipping_apiship_title'), 'shipping_apiship_template' => html_entity_decode($this->config->get('shipping_apiship_template')), @@ -90,27 +91,6 @@ public function __construct($params) { } - private function setData($key, $value) { - - if(!isset($value)) return; - - if(session_id() == '') { - session_start(); - } - $_SESSION['shipping_apiship'][$key] = $value; - - } - - private function getData($key) { - - if(session_id() == '') { - session_start(); - } - - if (isset($_SESSION['shipping_apiship'][$key])) return $_SESSION['shipping_apiship'][$key]; - return null; - } - private function apiship_point($id) { $data = $this->apiship->apiship_point_by_params(['id=' . $id]); if (isset($data[0])) return $data[0]; else return []; @@ -144,7 +124,7 @@ private function get_provider_name($provider_key) { } public function get_last_tracing_id() { - return $this->getData('shipping_apiship_last_tracing_id'); + return $this->apiship->getData('shipping_apiship_last_tracing_id'); } @@ -229,10 +209,7 @@ public function get_quote_list($address, $full_list = false) { $quote_data = []; $start_points = []; - $select_points = []; - - $select_point = []; - if (isset($this->session->data['shipping_apiship'])) $select_point = $this->session->data['shipping_apiship']; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); $region = (isset($address['zone']))?$address['zone']:''; $city = (isset($address['city']))?trim($address['city']):''; @@ -241,15 +218,16 @@ public function get_quote_list($address, $full_list = false) { $country = (isset($address['iso_code_2']))?trim($address['iso_code_2']):''; $payment_method_code = (isset($this->session->data['payment_method']['code']))?$this->session->data['payment_method']['code']:''; $cash_on_delivery = in_array($payment_method_code, $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); + $apiship_calculator_data = $this->apiship->apiship_calculator($country,$region,$city,$postcode,$ext_address,[],$this->cart->getProducts(),$this->getCartTotal(),$cash_on_delivery); $data = $apiship_calculator_data['body']; - $this->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); - $this->setData('shipping_apiship_region',$region); - $this->setData('shipping_apiship_city',$city); - $this->setData('shipping_apiship_postcode',$postcode); - $this->setData('shipping_apiship_ext_address',$ext_address); - $this->setData('shipping_apiship_country',$country); + $this->apiship->setData('shipping_apiship_last_tracing_id',$apiship_calculator_data['x-tracing-id']); + $this->apiship->setData('shipping_apiship_region',$region); + $this->apiship->setData('shipping_apiship_city',$city); + $this->apiship->setData('shipping_apiship_postcode',$postcode); + $this->apiship->setData('shipping_apiship_ext_address',$ext_address); + $this->apiship->setData('shipping_apiship_country',$country); if ($full_list == false) { @@ -261,46 +239,50 @@ public function get_quote_list($address, $full_list = false) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - - if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + foreach(array(1,2) as $pickup_type) { - if (empty($daysMin[$provider['providerKey']])) { - $daysMin[$provider['providerKey']] = $tariff['daysMin']; - } else { - if ($tariff['daysMin'] < $daysMin[$provider['providerKey']]) $daysMin[$provider['providerKey']] = $tariff['daysMin']; - } - - if (empty($daysMax[$provider['providerKey']])) { - $daysMax[$provider['providerKey']] = $tariff['daysMax']; - } else { - if ($tariff['daysMax'] > $daysMax[$provider['providerKey']]) $daysMax[$provider['providerKey']] = $tariff['daysMax']; - } - - if (empty($daysMinAllPoints)) { - $daysMinAllPoints = $tariff['daysMin']; - } else { - if ($tariff['daysMin'] < $daysMinAllPoints) $daysMinAllPoints = $tariff['daysMin']; - } - - if (empty($daysMaxAllPoints)) { - $daysMaxAllPoints = $tariff['daysMax']; - } else { - if ($tariff['daysMax'] > $daysMaxAllPoints) $daysMaxAllPoints = $tariff['daysMax']; - } + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error'; - if (empty($start_points[$provider['providerKey']])) { - $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; - } elseif ($tariff['deliveryCost'] < $start_points[$provider['providerKey']]['deliveryCost']) { - if (strpos($start_points[$provider['providerKey']]['key'],'error')!==false) + if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + + if (empty($daysMin[$provider['providerKey']])) { + $daysMin[$provider['providerKey']] = $tariff['daysMin']; + } else { + if ($tariff['daysMin'] < $daysMin[$provider['providerKey']]) $daysMin[$provider['providerKey']] = $tariff['daysMin']; + } + + if (empty($daysMax[$provider['providerKey']])) { + $daysMax[$provider['providerKey']] = $tariff['daysMax']; + } else { + if ($tariff['daysMax'] > $daysMax[$provider['providerKey']]) $daysMax[$provider['providerKey']] = $tariff['daysMax']; + } + + if (empty($daysMinAllPoints)) { + $daysMinAllPoints = $tariff['daysMin']; + } else { + if ($tariff['daysMin'] < $daysMinAllPoints) $daysMinAllPoints = $tariff['daysMin']; + } + + if (empty($daysMaxAllPoints)) { + $daysMaxAllPoints = $tariff['daysMax']; + } else { + if ($tariff['daysMax'] > $daysMaxAllPoints) $daysMaxAllPoints = $tariff['daysMax']; + } + + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . 'error' . '_' . $pickup_type; + if (empty($start_points[$provider['providerKey']])) { $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; - } - - $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if (isset($select_point[$provider['providerKey']])) - if ('apiship.' . $key == $select_point[$provider['providerKey']]['code']) { - $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; + } elseif ($tariff['deliveryCost'] < $start_points[$provider['providerKey']]['deliveryCost']) { + if (strpos($start_points[$provider['providerKey']]['key'],'error')!==false) + $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; + } + + $key = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if (isset($select_points[$provider['providerKey']])) + if ('apiship.' . $key == $select_points[$provider['providerKey']]['code']) { + $start_points[$provider['providerKey']] = ['key' => $key, 'tariffName' => $tariff['tariffName'], 'daysMin' => $tariff['daysMin'], 'daysMax' => $tariff['daysMax'], 'deliveryCost' => $tariff['deliveryCost'], 'tariffDescription' => $tariff['tariffDescription']]; + } } } @@ -309,15 +291,15 @@ public function get_quote_list($address, $full_list = false) { } - +/* $this->apiship->toLog('get_quote_list debug', [ 'address' => $address, 'start_points' => $start_points, 'session' => $this->session->data, - 'select_point' => $select_point, + 'select_points' => $select_points, 'weight' => $this->cart->getWeight() ]); - +*/ if ($this->apiship_params['shipping_apiship_group_points']) { // все ПВЗ на одной карте @@ -327,7 +309,7 @@ public function get_quote_list($address, $full_list = false) { foreach($start_points as $provider_key => $element) { - $shipping_apiship_last_select_code = $this->getData('shipping_apiship_last_select_code'); + $shipping_apiship_last_select_code = $this->apiship->getData('shipping_apiship_last_select_code'); if (isset($shipping_apiship_last_select_code)) if ('apiship.' . $element['key'] == $shipping_apiship_last_select_code) { @@ -483,59 +465,64 @@ public function get_quote_list($address, $full_list = false) { foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; + foreach(array(1,2) as $pickup_type) { - if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; - - $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId']; - $quote_data[$key] = [ - 'code' => 'apiship.' . $key, - 'title' => $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'door', - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]), - 'cost' => $tariff['deliveryCost'], - 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], - 'text' => $this->currency->format($this->tax->calculate($tariff['deliveryCost'], $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) - ]; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; + + $key = 'door_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $pickup_type; + $quote_data[$key] = [ + 'code' => 'apiship.' . $key, + 'title' => $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'door', + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]), + 'cost' => $tariff['deliveryCost'], + 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], + 'text' => $this->currency->format($this->tax->calculate($tariff['deliveryCost'], $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) + ]; + } } } - /* - if(empty($quote_data)) { - // нет данных, потому что таймаут - $title = $this->apiship_params['shipping_apiship_error_timeout']; - - // нет данных, потому что ошибка - if (isset($data['message'])) { - //$title = $this->apiship_params['shipping_apiship_error_calculator']; - $title = $data['message']; - if (isset($data['errors'])) $errors = $data['errors']; else $errors = []; - foreach($errors as $error) { - $title = $title . ", " . $error['message']; + if ($this->apiship_params['shipping_apiship_error_stub_show']) { + if(empty($quote_data)) { + // нет данных, потому что таймаут + $title = $this->apiship_params['shipping_apiship_error_timeout']; + + // нет данных, потому что ошибка + if (isset($data['message'])) { + + $title = $data['message']; + if (isset($data['errors'])) $errors = $data['errors']; else $errors = []; + foreach($errors as $error) { + $title = $title . ", " . $error['message']; + } } + + // нет данных, потому что поиск не нашел + if (isset($data['deliveryToPoint']) || isset($data['deliveryToDoor'])) $title = sprintf($this->apiship_params['shipping_apiship_no_shipping'], $city .', '.$region); + + $quote_data['error'] = array( + 'code' => 'apiship.error', + 'title' => $title, + 'cost' => 0, + 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], + 'text' => $this->currency->format(0, $this->apiship_params['shipping_apiship_rub_select']) + ); + } - - // нет данных, потому что поиск не нашел - if (isset($data['deliveryToPoint']) || isset($data['deliveryToDoor'])) $title = sprintf($this->apiship_params['shipping_apiship_no_shipping'], $city .', '.$region); - - $quote_data['error'] = array( - 'code' => 'apiship.error', - 'title' => $title, - 'cost' => 0, - 'tax_class_id' => $this->apiship_params['shipping_apiship_tax_class_id'], - 'text' => $this->currency->format(0, $this->apiship_params['shipping_apiship_rub_select']) - ); - } - */ + if(!empty($quote_data)) $method_data = array( @@ -565,7 +552,9 @@ private function get_title($params) { if (isset($params['daysMax'])) $daysMax = $params['daysMax']; else $daysMax = ''; if (isset($params['tariffDescription'])) $tariffDescription = $params['tariffDescription']; else $tariffDescription = ''; - $template = '%type %company %time'; + $template = '%type %company'; + if (strpos($this->apiship_params['shipping_apiship_template'],'%time')!==false) $template = $template . ' %time'; + if ($params['template'] == 'shipping_apiship_template') $template = $this->apiship_params['shipping_apiship_template']; $type_name = ''; @@ -635,9 +624,14 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($providers as $provider) { if (isset($provider['tariffs'])) $tariffs = $provider['tariffs']; else $tariffs = []; foreach($tariffs as $tariff) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - foreach($tariff['pointIds'] as $point_id) { - if (!in_array($point_id,$points_ids)) $points_ids[] = $point_id; + foreach(array(1,2) as $pickup_type) { + + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + foreach($tariff['pointIds'] as $point_id) { + if (!in_array($point_id,$points_ids)) $points_ids[] = $point_id; + } } } } @@ -675,46 +669,50 @@ private function get_points_array($country, $region, $city, $postcode, $ext_addr foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - - $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if (!isset($data_points[$point_id])) continue; - $point = $data_points[$point_id]; - $cost = $tariff['deliveryCost']; - - $all_points[] = [ - 'lon' => $point['lon'], - 'lat' => $point['lat'], - 'code' => 'apiship.' . $code, - - 'tariff' => $tariff['tariffName'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - - 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']), - 'cost' => round($cost), - - 'title' => $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'point', - 'sub_type' => $point['type'], - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'pointName' => $point['name'], - 'pointAddress' => $point['address'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]), - - 'type' => $apiship_point_types[$point['type']-1], - 'provider' => $apiship_providers[$provider['providerKey']], - 'provider_key' => $provider['providerKey'], + foreach(array(1,2) as $pickup_type) { - //'phones' => $point['phone'], - //'workTime' => $point['workTime'], - - ]; + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $code = 'point_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if (!isset($data_points[$point_id])) continue; + $point = $data_points[$point_id]; + $cost = $tariff['deliveryCost']; + + $all_points[] = [ + 'lon' => $point['lon'], + 'lat' => $point['lat'], + 'code' => 'apiship.' . $code, + + 'tariff' => $tariff['tariffName'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + + 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']), + 'cost' => round($cost), + + 'title' => $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'point', + 'sub_type' => $point['type'], + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'pointName' => $point['name'], + 'pointAddress' => $point['address'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]), + + 'type' => $apiship_point_types[$point['type']-1], + 'provider' => $apiship_providers[$provider['providerKey']], + 'provider_key' => $provider['providerKey'], + + //'phones' => $point['phone'], + //'workTime' => $point['workTime'], + + ]; + } } } } @@ -779,11 +777,11 @@ public function get_points() { if (isset($this->request->post['code'])) $code = $this->request->post['code']; else $code = ''; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); $this->apiship->toLog('get_points', [ 'country' => $country, @@ -799,9 +797,14 @@ public function get_points() { $provider = [$parce_code['provider']]; if ($this->apiship_params['shipping_apiship_group_points']) $provider = []; - $points = $this->get_points_array($country, $region, $city, $postcode, $ext_address, $provider); + $data = $this->get_points_array($country, $region, $city, $postcode, $ext_address); + + $points = []; + foreach($data['points'] as $point) { + if ($point['provider_key']==$parce_code['provider']) $points[] = $point; + } - echo json_encode($points); + echo json_encode(['error' => $data['error'],'points' => $points]); } @@ -816,14 +819,13 @@ public function set_point() { $delivery_type = $parce_code['delivery_type']; - $region = $this->getData('shipping_apiship_region'); - $city = $this->getData('shipping_apiship_city'); - $postcode = $this->getData('shipping_apiship_postcode'); - $ext_address = $this->getData('shipping_apiship_ext_address'); - $country = $this->getData('shipping_apiship_country'); - - $cost = -1; - $postcode = ''; + $region = $this->apiship->getData('shipping_apiship_region'); + $city = $this->apiship->getData('shipping_apiship_city'); + $postcode = $this->apiship->getData('shipping_apiship_postcode'); + $ext_address = $this->apiship->getData('shipping_apiship_ext_address'); + $country = $this->apiship->getData('shipping_apiship_country'); + + $cost = -1; $address1 = ''; $payment_method_code = (isset($this->session->data['payment_method']['code']))?$this->session->data['payment_method']['code']:''; $cash_on_delivery = in_array($payment_method_code, $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); @@ -836,26 +838,31 @@ public function set_point() { foreach($tariffs as $tariff) { if (!isset($tariff['tariffDescription'])) $tariff['tariffDescription'] = ''; foreach($tariff['pointIds'] as $point_id) { - if (!in_array($this->get_pickup_type($provider['providerKey']), $tariff['pickupTypes'])) continue; - $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id; - if ('apiship.' . $key == $code) { - $cost = $tariff['deliveryCost']; - $point = $this->apiship_point($point_id); + foreach(array(1,2) as $pickup_type) { - $postcode = $point['postIndex']; - $address1 = $this->apiship->get_address($point); - $title = $this->get_title([ - 'template' => 'shipping_apiship_template', - 'type' => 'point', - 'sub_type' => $point['type'], - 'providerKey' => $provider['providerKey'], - 'tariffName' => $tariff['tariffName'], - 'pointName' => $point['name'], - 'pointAddress' => $point['address'], - 'daysMin' => $tariff['daysMin'], - 'daysMax' => $tariff['daysMax'], - 'tariffDescription' => $tariff['tariffDescription'] - ]); + if (!in_array($pickup_type, $tariff['pickupTypes'])) continue; + if (!in_array($pickup_type, $this->get_pickup_types($provider['providerKey']))) continue; + + $key = $delivery_type. '_' . $provider['providerKey'] . '_' . $tariff['tariffId'] . '_' . $point_id . '_' . $pickup_type; + if ('apiship.' . $key == $code) { + $cost = $tariff['deliveryCost']; + $point = $this->apiship_point($point_id); + + $postcode = $point['postIndex']; + $address1 = $this->apiship->get_address($point); + $title = $this->get_title([ + 'template' => 'shipping_apiship_template', + 'type' => 'point', + 'sub_type' => $point['type'], + 'providerKey' => $provider['providerKey'], + 'tariffName' => $tariff['tariffName'], + 'pointName' => $point['name'], + 'pointAddress' => $point['address'], + 'daysMin' => $tariff['daysMin'], + 'daysMax' => $tariff['daysMax'], + 'tariffDescription' => $tariff['tariffDescription'] + ]); + } } } } @@ -874,9 +881,12 @@ public function set_point() { 'text' => $this->currency->format($this->tax->calculate($cost, $this->apiship_params['shipping_apiship_tax_class_id'], $this->config->get('config_tax')), $this->apiship_params['shipping_apiship_rub_select']) ]; - $this->session->data['shipping_apiship'][$parce_code['provider']] = $shipping_apiship; + $select_points = $this->apiship->getData('shipping_apiship_select_points'); + $select_points[$parce_code['provider']] = $shipping_apiship; + $this->apiship->setData('shipping_apiship_select_points', $select_points); + $this->session->data['shipping_methods']['apiship']['quote'][$parce_code['short_code']] = $shipping_apiship; - $this->setData('shipping_apiship_last_select_code', $code); + $this->apiship->setData('shipping_apiship_last_select_code', $code); $shipping_apiship['postcode'] = $postcode; $shipping_apiship['address1'] = $address1; @@ -1053,7 +1063,6 @@ public function export_order() { $order_params['costAssessedCost'] = $this->apiship->format_cost($order['total'] - $order_totals['shipping']); - //$cash_on_delivery = in_array($order['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $paid_orders = in_array($order['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); $order_params['costCodCost'] = ($paid_orders==false) ? $this->apiship->format_cost($order['total']) : 0; @@ -1297,11 +1306,14 @@ private function set_apiship_order_status($apiship_order_id, $status_id) { $this->db->query("UPDATE `" . DB_PREFIX . "apiship_order` SET status = " . $status_id . " WHERE apiship_order_id = '" . (int)$apiship_order_id . "'"); } - private function get_pickup_type($provider) { - if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) return 2; - return 1; + private function get_pickup_types($provider) { + $pickup_types = []; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['pickup_type'])) $pickup_types[] = 2; + if (isset($this->apiship_params['shipping_apiship_provider'][$provider]['courier_type'])) $pickup_types[] = 1; + return $pickup_types; } + private function get_status($apiship_order_status) { $key = $apiship_order_status['status']['key']; @@ -1368,13 +1380,14 @@ public function get_order_params() { $this->load->model('checkout/order'); $order_info = $this->model_checkout_order->getOrder($order_id); $apiship_paid = in_array($order_info['order_status_id'], $this->apiship_params['shipping_apiship_paid_orders']); - //$cash_on_delivery = in_array($order_info['payment_code'], $this->apiship_params['shipping_apiship_cash_on_delivery_payment_methods']); $apiship_export = $this->is_order_export($order_id); $parce_code = $this->apiship->parce_code($order_info['shipping_code']); if (isset($parce_code['provider'])) $provider = $parce_code['provider']; else $provider = ''; - $apiship_pickup_type = $this->get_pickup_type($provider); + if (in_array(2, $this->get_pickup_types($provider))) $apiship_pickup_type = 2; else $apiship_pickup_type = 1; + + if (!empty($parce_code['pickup_type'])) $apiship_pickup_type = $parce_code['pickup_type']; $order_products = $this->getOrderProducts($order_id); diff --git a/3.x/upload/system/library/apiship/apiship.php b/3.x/upload/system/library/apiship/apiship.php index f6c189d..dcd456c 100644 --- a/3.x/upload/system/library/apiship/apiship.php +++ b/3.x/upload/system/library/apiship/apiship.php @@ -97,6 +97,18 @@ function($curl, $header) use (&$headers) private function apiship_data($cmd, $limit, $filter_list = '') { + $data_key = 'apiship_' . $cmd . md5($limit . print_r($filter_list,1)); + $data_hash = md5($cmd . $limit . print_r($filter_list,1)); + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['rows']; + } + } + + $offset = 0; $rows = []; $x_tracing_ids = []; @@ -131,9 +143,38 @@ private function apiship_data($cmd, $limit, $filter_list = '') { $this->toLog('shipping_apiship_data '.$cmd, ['url' => $url, '$x_tracing_id' => $x_tracing_ids, 'output' => $rows]); + $this->setData($data_key, ['rows' => $rows, 'data_hash' => $data_hash]); return $rows; } + public function setData($key, $value, $expired_timeout_minuts = 10) { + + if(!isset($value)) return; + + if(session_id() == '') { + session_start(); + } + + if ($expired_timeout_minuts == 0) + $_SESSION['shipping_apiship'][$key] = ['value' => $value]; + else + $_SESSION['shipping_apiship'][$key] = ['value' => $value, 'time' => strtotime('now') + 60*$expired_timeout_minuts]; + + } + + public function getData($key) { + + if(session_id() == '') { + session_start(); + } + + if (!isset($_SESSION['shipping_apiship'][$key])) return null; + if (!isset($_SESSION['shipping_apiship'][$key]['time'])) $_SESSION['shipping_apiship'][$key]['value']; + if ((strtotime('now') > $_SESSION['shipping_apiship'][$key]['time'])) return null; + + return $_SESSION['shipping_apiship'][$key]['value']; + } + public function apiship_providers() { return $this->apiship_data('lists/providers', 10000); } @@ -196,6 +237,18 @@ public function apiship_waybills($order_id) { } public function apiship_points($points) { + + $data_hash = md5(print_r($points,1)); + $data_key = 'apiship_points'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data['all_points']; + } + } + $all_points = []; $limit = 1000; $offset = 0; @@ -223,6 +276,7 @@ public function apiship_points($points) { $offset++; } + $this->setData($data_key, ['all_points' => $all_points, 'data_hash' => $data_hash]); return $all_points; } @@ -270,6 +324,8 @@ public function apiship_oc_order_status($order_id) { public function apiship_calculator($country, $region, $city, $postcode, $ext_address, $providers, $products, $total, $cash_on_delivery) { + $postcode = ''; + if (trim($city) == '') { $output['message'] = $this->apiship_params['shipping_apiship_error_select_city']; @@ -279,6 +335,17 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add ]; } + $data_hash = md5($country.$region.$city.$postcode.$ext_address.print_r($providers,1).print_r($products,1).$total.$cash_on_delivery); + $data_key = 'apiship_calculator'; + + $data = $this->getData($data_key); + if ($data!==null) { + if ($data['data_hash']==$data_hash) { + $this->toLog($data_key . ' cached'); + return $data; + } + } + $calculate_data = $this->calculate_places($products, $total); $items = $calculate_data['items']; @@ -352,20 +419,22 @@ public function apiship_calculator($country, $region, $city, $postcode, $ext_add $data = [ 'body' => json_decode($output['body'], true), - 'x-tracing-id' => $x_tracing_id + 'x-tracing-id' => $x_tracing_id, + 'data_hash' => $data_hash ]; $this->toLog('shipping_apiship_calculator', ['url' => $url, 'params' => $params, 'output' => $data], isset($data['body']['errors'])); if (isset($data['body']['errors'])) { - $output['message'] = $this->apiship_params['shipping_apiship_error_calculator']; + $output['message'] = sprintf($this->apiship_params['shipping_apiship_no_shipping'], $city .', '.$region); return [ 'body' => $output, - 'x-tracing-id' => '' + 'x-tracing-id' => $x_tracing_id ]; } + $this->setData($data_key, $data); return $data; } @@ -584,6 +653,7 @@ public function apiship_order_info($apiship_order_id) { public function get_address($params, $show_post_index = false) { + if (!isset($params['regionType'])) return ''; if ($params['regionType'] == 'г') { $address = $params['region']; @@ -675,21 +745,30 @@ public function get_integrator_statuses() { } public function parce_code($code) { - $code_parts = explode('.',$code); - if (isset($code_parts[1])) $tariff_parts = explode('_',$code_parts[1]); + $short_code = ""; + if (isset($code_parts[1])) { + $tariff_parts = explode('_',$code_parts[1]); + $short_code = $code_parts[1]; + } if (isset($tariff_parts[0])) $delivery_type = $tariff_parts[0]; else $delivery_type = ''; if (isset($tariff_parts[1])) $provider = $tariff_parts[1]; else $provider = ''; if (isset($tariff_parts[2])) $tariff_id = $tariff_parts[2]; else $tariff_id = ''; if (isset($tariff_parts[3])) $point_id = $tariff_parts[3]; else $point_id = ''; + if (isset($tariff_parts[4])) $pickup_type = $tariff_parts[4]; else $pickup_type = ''; + if ($delivery_type == 'door') { + $pickup_type = $point_id; + $point_id = ''; + } return [ 'delivery_type' => $delivery_type, 'provider' => $provider, 'tariff_id' => $tariff_id, 'point_id' => $point_id, - 'short_code' => $code_parts[1] + 'short_code' => $short_code, + 'pickup_type' => $pickup_type ]; } diff --git a/CHANGELOG.md b/CHANGELOG.md index 3480252..59727af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.8.8 - 2024-01-20 +* Исправление ошибки при отсутствии code. +* Для каждой из СД добавлена возможность выбора нужна ли отгрузка от двери и нужна ли отгрузка от ПВЗ. +* Добавлено кеширование. +* Добавлен флаг показывать заглушку в корзине об ошибке или нет. + ## 0.8.7 - 2023-12-06 * Исправлена ошибка Значение поля 'Оценочная стоимость / Стоимость страховки' должен быть числом с максимум 2-мя знаками в дробной части. * В админке при выборе ПВЗ отгузки поиск сделан только через код ПВЗ.