From ed5de5186e66e0aaced73ffb8cb98ffcb5fea925 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 18 Jan 2019 09:52:08 +0300 Subject: [PATCH] v3.2.1 --- .travis.yml | 2 +- CHANGELOG.md | 3 ++ VERSION | 2 +- .../extension/retailcrm/history/v4_5.php | 5 +++ .../admin/model/extension/retailcrm/order.php | 35 ++++++++++++++- .../controller/extension/module/retailcrm.php | 28 ++++++------ .../model/extension/retailcrm/order.php | 44 ++++++++++++++++--- tests/admin/ModelRetailcrmOrderAdminTest.php | 3 +- .../ModelRetailcrmOrderCatalogTest.php | 8 ++-- 9 files changed, 101 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index c98c779..32815d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ before_script: - composer install - composer setup - bin/robo project:deploy - - (php -S localhost:8000 -t www &) 2> /dev/null > /dev/null + - (php -S localhost:8000 -t www &) 2> /dev/null > /dev/null(php -S localhost:8000 -t www &) 2> /dev/null > /dev/null - sleep 2 script: diff --git a/CHANGELOG.md b/CHANGELOG.md index 48c8ff6..adb15c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v.3.2.1 +* Изменена логика передачи данных по заказам и клиентам. Контактные данные по доставке передаются в карточку заказа, контактные данные плательщика в карточку клиента. + ## v.3.2.0 * Добавлена выгрузка картинок для категорий в ICML * Добавлена настройка для выбора валюты, в которой будет выгружаться цена в ICML diff --git a/VERSION b/VERSION index 944880f..e4604e3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.2.0 +3.2.1 diff --git a/src/upload/admin/model/extension/retailcrm/history/v4_5.php b/src/upload/admin/model/extension/retailcrm/history/v4_5.php index 8d82b3f..39b72f6 100644 --- a/src/upload/admin/model/extension/retailcrm/history/v4_5.php +++ b/src/upload/admin/model/extension/retailcrm/history/v4_5.php @@ -777,6 +777,11 @@ class ModelExtensionRetailcrmHistoryV45 extends ModelExtensionRetailcrmHistory foreach ($customers as $customer) { $customer_id = $customer['externalId']; $customerData = $this->model_customer_customer->getCustomer($customer_id); + + if (!$customerData) { + continue; + } + $customerData['password'] = false; $customerData['firstname'] = $customer['firstName']; diff --git a/src/upload/admin/model/extension/retailcrm/order.php b/src/upload/admin/model/extension/retailcrm/order.php index b870b97..4a3cd83 100644 --- a/src/upload/admin/model/extension/retailcrm/order.php +++ b/src/upload/admin/model/extension/retailcrm/order.php @@ -90,6 +90,37 @@ class ModelExtensionRetailcrmOrder extends Model { unset($customers); } + if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) { + $new_customer = array( + 'externalId' => uniqid(), + 'firstName' => $order_data['firstname'], + 'lastName' => $order_data['lastname'], + 'email' => $order_data['email'], + 'createdAt' => $order_data['date_added'], + 'address' => array( + 'countryIso' => $order_data['payment_iso_code_2'], + 'index' => $order_data['payment_postcode'], + 'city' => $order_data['payment_city'], + 'region' => $order_data['payment_zone'], + 'text' => $order_data['payment_address_1'] . ' ' . $order_data['payment_address_2'] + ) + ); + + if (!empty($order_data['telephone'])) { + $new_customer['phones'] = array( + array( + 'number' => $order_data['telephone'] + ) + ); + } + + $res = $retailcrmApiClient->customersCreate($new_customer); + + if ($res->isSuccessful() && isset($res['id'])) { + $order['customer']['id'] = $res['id']; + } + } + self::$lastRepsonse = $retailcrmApiClient->ordersCreate($order); return $order; @@ -152,8 +183,8 @@ class ModelExtensionRetailcrmOrder extends Model { } $order['externalId'] = $order_data['order_id']; - $order['firstName'] = $order_data['firstname']; - $order['lastName'] = $order_data['lastname']; + $order['firstName'] = $order_data['shipping_firstname']; + $order['lastName'] = $order_data['shipping_lastname']; $order['phone'] = $order_data['telephone']; $order['customerComment'] = $order_data['comment']; diff --git a/src/upload/catalog/controller/extension/module/retailcrm.php b/src/upload/catalog/controller/extension/module/retailcrm.php index faf73ae..5f86be9 100644 --- a/src/upload/catalog/controller/extension/module/retailcrm.php +++ b/src/upload/catalog/controller/extension/module/retailcrm.php @@ -48,23 +48,21 @@ class ControllerExtensionModuleRetailcrm extends Controller { } } - if (!isset($data['fromApi'])) { - $this->load->model('setting/setting'); - $status = $this->model_setting_setting->getSetting($moduleTitle); + $this->load->model('setting/setting'); + $status = $this->model_setting_setting->getSetting($moduleTitle); - if (isset($data['order_status_id']) && $data['order_status_id'] > 0) { - $data['order_status'] = $status[$moduleTitle . '_status'][$data['order_status_id']]; - } + if (isset($data['order_status_id']) && $data['order_status_id'] > 0) { + $data['order_status'] = $status[$moduleTitle . '_status'][$data['order_status_id']]; + } - if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/order.php')) { - $this->load->model('extension/retailcrm/custom/order'); - $order = $this->model_extension_retailcrm_custom_order->processOrder($data); - $this->model_extension_retailcrm_custom_order->sendToCrm($order, $this->retailcrmApiClient); - } else { - $this->load->model('extension/retailcrm/order'); - $order = $this->model_extension_retailcrm_order->processOrder($data); - $this->model_extension_retailcrm_order->sendToCrm($order, $this->retailcrmApiClient); - } + if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/order.php')) { + $this->load->model('extension/retailcrm/custom/order'); + $order = $this->model_extension_retailcrm_custom_order->processOrder($data); + $this->model_extension_retailcrm_custom_order->sendToCrm($order, $data, $this->retailcrmApiClient); + } else { + $this->load->model('extension/retailcrm/order'); + $order = $this->model_extension_retailcrm_order->processOrder($data); + $this->model_extension_retailcrm_order->sendToCrm($order, $data, $this->retailcrmApiClient); } } diff --git a/src/upload/catalog/model/extension/retailcrm/order.php b/src/upload/catalog/model/extension/retailcrm/order.php index bbffb03..fd79533 100644 --- a/src/upload/catalog/model/extension/retailcrm/order.php +++ b/src/upload/catalog/model/extension/retailcrm/order.php @@ -18,12 +18,13 @@ class ModelExtensionRetailcrmOrder extends Model { * * @param array $order * @param \RetailcrmProxy $retailcrmApiClient + * @param array $data * @param bool $create (default = true) * * @return mixed */ - public function sendToCrm($order, $retailcrmApiClient, $create = true) { - if (isset($this->request->post['fromApi']) || $retailcrmApiClient === false) { + public function sendToCrm($order, $retailcrmApiClient, $data, $create = true) { + if ($retailcrmApiClient === false) { return false; } @@ -35,6 +36,37 @@ class ModelExtensionRetailcrmOrder extends Model { } } + if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) { + $new_customer = array( + 'externalId' => uniqid(), + 'firstName' => $data['firstname'], + 'lastName' => $data['lastname'], + 'email' => $data['email'], + 'createdAt' => $data['date_added'], + 'address' => array( + 'countryIso' => $data['payment_iso_code_2'], + 'index' => $data['payment_postcode'], + 'city' => $data['payment_city'], + 'region' => $data['payment_zone'], + 'text' => $data['payment_address_1'] . ' ' . $data['payment_address_2'] + ) + ); + + if (!empty($data['telephone'])) { + $new_customer['phones'] = array( + array( + 'number' => $data['telephone'] + ) + ); + } + + $res = $retailcrmApiClient->customersCreate($new_customer); + + if ($res->isSuccessful() && isset($res['id'])) { + $order['customer']['id'] = $res['id']; + } + } + if ($create) { $retailcrmApiClient->ordersCreate($order); } else { @@ -98,8 +130,8 @@ class ModelExtensionRetailcrmOrder extends Model { } } - if (!empty($order_data['payment_iso_code_2'])) { - $order['countryIso'] = $order_data['payment_iso_code_2']; + if (!empty($order_data['shipping_iso_code_2'])) { + $order['countryIso'] = $order_data['shipping_iso_code_2']; } if (isset($this->settings[$this->moduleTitle . '_order_number']) @@ -109,8 +141,8 @@ class ModelExtensionRetailcrmOrder extends Model { } $order['externalId'] = $order_id; - $order['firstName'] = $order_data['firstname']; - $order['lastName'] = $order_data['lastname']; + $order['firstName'] = $order_data['shipping_firstname']; + $order['lastName'] = $order_data['shipping_lastname']; $order['phone'] = $order_data['telephone']; $order['customerComment'] = $order_data['comment']; diff --git a/tests/admin/ModelRetailcrmOrderAdminTest.php b/tests/admin/ModelRetailcrmOrderAdminTest.php index 3ed8d70..e789866 100644 --- a/tests/admin/ModelRetailcrmOrderAdminTest.php +++ b/tests/admin/ModelRetailcrmOrderAdminTest.php @@ -166,6 +166,7 @@ class ModelRetailcrmOrderAdminTest extends OpenCartTest ); $this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn($response); + $this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($response); $orderSend = $this->orderModel->uploadOrder($order, $this->apiClientMock); $this->assertArrayHasKey('status', $orderSend); @@ -181,7 +182,7 @@ class ModelRetailcrmOrderAdminTest extends OpenCartTest $this->assertContains('#', $orderSend['items'][0]['offer']['externalId']); $this->assertArrayHasKey('payments', $orderSend); $this->assertArrayHasKey('customerComment', $orderSend); - $this->assertArrayNotHasKey('customer', $orderSend); + $this->assertArrayHasKey('customer', $orderSend); $this->assertNotEmpty($orderSend['payments']); } } diff --git a/tests/catalog/ModelRetailcrmOrderCatalogTest.php b/tests/catalog/ModelRetailcrmOrderCatalogTest.php index 88ee084..c9786fc 100644 --- a/tests/catalog/ModelRetailcrmOrderCatalogTest.php +++ b/tests/catalog/ModelRetailcrmOrderCatalogTest.php @@ -23,7 +23,8 @@ class ModelRetailcrmOrderCatalogTest extends OpenCartTest 'ordersEdit', 'ordersGet', 'ordersPaymentEdit', - 'customersList' + 'customersList', + 'customersCreate' )) ->getMock(); @@ -65,7 +66,7 @@ class ModelRetailcrmOrderCatalogTest extends OpenCartTest } $orderProcess = $this->orderModel->processOrder($order); - $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock); + $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order); $this->assertArrayHasKey('status', $orderSend); $this->assertEquals('new', $orderSend['status']); @@ -135,8 +136,9 @@ class ModelRetailcrmOrderCatalogTest extends OpenCartTest $this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse); $this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse); + $this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($orderEditResponse); $orderProcess = $this->orderModel->processOrder($order); - $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, false); + $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order, false); $this->assertArrayHasKey('status', $orderSend); $this->assertEquals('new', $orderSend['status']);