From 090a5783c57ee3eb6723a188b34673793a8e57c6 Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Fri, 28 Apr 2023 11:44:38 +0300 Subject: [PATCH] Optimization of the history processing algorithm (#254) --- CHANGELOG.md | 3 + VERSION | 2 +- .../controller/extension/module/retailcrm.php | 66 ++++++++------ .../model/extension/retailcrm/history.php | 56 +++++------- .../retailcrm/lib/api/RetailcrmApiClient5.php | 89 ++++--------------- .../retailcrm/lib/api/RetailcrmProxy.php | 6 +- 6 files changed, 83 insertions(+), 139 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb8217..88c6c8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v4.1.9 +* Optimization of the history processing algorithm + ## v4.1.8 * Fixed customer externalId when creating a customer and sending it to RetailCRM diff --git a/VERSION b/VERSION index a7c00da..18837e7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.8 +4.1.9 diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index e8e62f7..63bb76d 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -186,42 +186,21 @@ class ControllerExtensionModuleRetailcrm extends Controller $this->request->post ); - if (!isset($history_setting['retailcrm_history_orders']) && !isset($history_setting['retailcrm_history_customers'])) { + if ( + !isset($history_setting['retailcrm_history_orders']) + && !isset($history_setting['retailcrm_history_customers']) + ) { $api = $this->retailcrm->getApiClient( $this->request->post[$this->moduleTitle . '_url'], $this->request->post[$this->moduleTitle . '_apikey'] ); - $ordersHistory = $api->ordersHistory(); - - if ($ordersHistory && $ordersHistory->isSuccessful() && !empty($ordersHistory['history'])) { - $ordersHistory = $api->ordersHistory(array(), $ordersHistory['pagination']['totalPageCount']); - - if ($ordersHistory && $ordersHistory->isSuccessful()) { - $ordersHistoryArr = $ordersHistory['history']; - $lastChangeOrders = end($ordersHistoryArr); - $sinceIdOrders = $lastChangeOrders['id']; - } - } - - $customersHistory = $api->customersHistory(); - - if ($customersHistory && $customersHistory->isSuccessful() && !empty($customersHistory['history'])) { - $customersHistory = $api->customersHistory(array(), $customersHistory['pagination']['totalPageCount']); - - if ($customersHistory && $customersHistory->isSuccessful()) { - $customersHistoryArr = $customersHistory['history']; - $lastChangeCustomers = end($customersHistoryArr); - $sinceIdCustomers = $lastChangeCustomers['id']; - } - } - $this->model_setting_setting->editSetting( 'retailcrm_history', - array( - 'retailcrm_history_orders' => isset($sinceIdOrders) ? $sinceIdOrders : 1, - 'retailcrm_history_customers' => isset($sinceIdCustomers) ? $sinceIdCustomers : 1 - ) + [ + 'retailcrm_history_orders' => $this->getHistorySinceId($api, 'ordersHistory'), + 'retailcrm_history_customers' => $this->getHistorySinceId($api, 'customersHistory'), + ] ); } @@ -934,4 +913,33 @@ class ControllerExtensionModuleRetailcrm extends Controller return false; } + + private function getHistorySinceId($api, $method) + { + $lastSinceId = 0; + $startDate = new DateTime('-1 days'); + $historyResponse = $api->$method(['startDate' => $startDate->format('Y-m-d H:i:s')]); + + if ( + !$historyResponse instanceof ApiResponse + || !$historyResponse->isSuccessful() + || empty($historyResponse['history']) + || empty($historyResponse['pagination']) + ) { + return $lastSinceId; + } + + $startPage = $historyResponse['pagination']['currentPage']; + $lastPage = $historyResponse['pagination']['totalPageCount']; + + for ($startPage; $startPage <= $lastPage; ++$startPage) { + if ($historyResponse instanceof ApiResponse && !empty($historyResponse['history'])) { + $history = $historyResponse['history']; + $lastSinceId = end($history)['id']; + $historyResponse = $api->$method(['sinceId' => $lastSinceId]); + } + } + + return $lastSinceId; + } } diff --git a/src/upload/admin/model/extension/retailcrm/history.php b/src/upload/admin/model/extension/retailcrm/history.php index f1a3083..ab57013 100644 --- a/src/upload/admin/model/extension/retailcrm/history.php +++ b/src/upload/admin/model/extension/retailcrm/history.php @@ -72,15 +72,10 @@ class ModelExtensionRetailcrmHistory extends Model { return false; } - $sinceIdOrders = $history['retailcrm_history_orders'] ? $history['retailcrm_history_orders'] : null; - $sinceIdCustomers = $history['retailcrm_history_customers'] ? $history['retailcrm_history_customers'] : null; - - $packsOrders = $retailcrmApiClient->ordersHistory(array( - 'sinceId' => $sinceIdOrders ? $sinceIdOrders : 0 - ), 1, 100); - $packsCustomers = $retailcrmApiClient->customersHistory(array( - 'sinceId' => $sinceIdCustomers ? $sinceIdCustomers : 0 - ), 1, 100); + $sinceIdOrders = $history['retailcrm_history_orders'] ?? 0; + $sinceIdCustomers = $history['retailcrm_history_customers'] ?? 0; + $packsOrders = $retailcrmApiClient->ordersHistory(['sinceId' => $sinceIdOrders]); + $packsCustomers = $retailcrmApiClient->customersHistory(['sinceId' => $sinceIdCustomers]); if (!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0 && !$packsCustomers->isSuccessful() && count($packsCustomers->history) <= 0 @@ -88,30 +83,28 @@ class ModelExtensionRetailcrmHistory extends Model { return false; } - $orders = RetailcrmHistoryHelper::assemblyOrder($packsOrders->history); - $customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history); - $ordersHistory = $packsOrders->history; $customersHistory = $packsCustomers->history; - $lastChangeOrders = $ordersHistory ? end($ordersHistory) : null; $lastChangeCustomers = $customersHistory ? end($customersHistory) : null; - if ($lastChangeOrders !== null) { - $sinceIdOrders = $lastChangeOrders['id']; - } - - if ($lastChangeCustomers !== null) { - $sinceIdCustomers = $lastChangeCustomers['id']; + if ($lastChangeOrders !== null && $lastChangeCustomers !== null) { + $this->model_setting_setting->editSetting( + 'retailcrm_history', + [ + 'retailcrm_history_orders' => $lastChangeOrders['id'], + 'retailcrm_history_customers' => $lastChangeCustomers['id'] + ] + ); } + $orders = RetailcrmHistoryHelper::assemblyOrder($ordersHistory); + $customers = RetailcrmHistoryHelper::assemblyCustomer($customersHistory); + $newOrders = []; + $updatedOrders = []; $this->settings = $settings; - $this->status = array_flip($settings[$this->moduleTitle . '_status']); - $updatedOrders = array(); - $newOrders = array(); - foreach ($orders as $order) { if (isset($order['deleted'])) { continue; @@ -126,7 +119,7 @@ class ModelExtensionRetailcrmHistory extends Model { unset($orders); - $updateCustomers = array(); + $updateCustomers = []; foreach ($customers as $customer) { if (isset($customer['deleted'])) { @@ -141,34 +134,27 @@ class ModelExtensionRetailcrmHistory extends Model { unset($customers); if (!empty($updateCustomers)) { - $customers = $retailcrmApiClient->customersList(array('ids' => $updateCustomers)); + $customers = $retailcrmApiClient->customersList(['ids' => $updateCustomers]); if ($customers) { $this->updateCustomers($customers['customers']); } } if (!empty($newOrders)) { - $orders = $retailcrmApiClient->ordersList(array('ids' => $newOrders)); + $orders = $retailcrmApiClient->ordersList(['ids' => $newOrders]); if ($orders) { $this->createResult = $this->createOrders($orders['orders'], $retailcrmApiClient); } } if (!empty($updatedOrders)) { - $orders = $retailcrmApiClient->ordersList(array('ids' => $updatedOrders)); + $orders = $retailcrmApiClient->ordersList(['ids' => $updatedOrders]); + if ($orders) { $this->updateOrders($orders['orders'], $retailcrmApiClient); } } - $this->model_setting_setting->editSetting( - 'retailcrm_history', - array( - 'retailcrm_history_orders' => $sinceIdOrders, - 'retailcrm_history_customers' => $sinceIdCustomers - ) - ); - if (!empty($this->createResult['customers'])) { $retailcrmApiClient->customersFixExternalIds($this->createResult['customers']); } diff --git a/src/upload/system/library/retailcrm/lib/api/RetailcrmApiClient5.php b/src/upload/system/library/retailcrm/lib/api/RetailcrmApiClient5.php index 3d83f37..1ac298c 100644 --- a/src/upload/system/library/retailcrm/lib/api/RetailcrmApiClient5.php +++ b/src/upload/system/library/retailcrm/lib/api/RetailcrmApiClient5.php @@ -584,30 +584,18 @@ class RetailcrmApiClient5 /** * Get orders history + * * @param array $filter - * @param null $page - * @param null $limit + * @param int|null $limit * * @return ApiResponse */ - public function ordersHistory(array $filter = array(), $page = null, $limit = null) + public function ordersHistory(array $filter = [], ?int $limit = 100) { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - return $this->client->makeRequest( '/orders/history', RetailcrmHttpClient::METHOD_GET, - $parameters + ['filter' => $filter, 'limit' => $limit] ); } @@ -908,30 +896,18 @@ class RetailcrmApiClient5 /** * Get customers history + * * @param array $filter - * @param null $page - * @param null $limit + * @param int|null $limit * * @return ApiResponse */ - public function customersHistory(array $filter = array(), $page = null, $limit = null) + public function customersHistory(array $filter = [], ?int $limit = 100) { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - return $this->client->makeRequest( '/customers/history', RetailcrmHttpClient::METHOD_GET, - $parameters + ['filter' => $filter, 'limit' => $limit] ); } @@ -1108,34 +1084,17 @@ class RetailcrmApiClient5 /** * Get orders assembly history * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws \RetailCrm\Exception\CurlException - * @throws \RetailCrm\Exception\InvalidJsonException + * @param array $filter + * @param int|null $limit * * @return ApiResponse */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + public function ordersPacksHistory(array $filter = [], ?int $limit = 100) { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - return $this->client->makeRequest( '/orders/packs/history', RetailcrmHttpClient::METHOD_GET, - $parameters + ['filter' => $filter, 'limit' => $limit] ); } @@ -2452,30 +2411,18 @@ class RetailcrmApiClient5 /** * Get corporate customers history - * @param array $filter - * @param null $page - * @param null $limit * - * @return \ApiResponse + * @param array $filter + * @param int|null $limit + * + * @return ApiResponse */ - public function customersCorporateHistory(array $filter = [], $page = null, $limit = null) + public function customersCorporateHistory(array $filter = [], ?int $limit = 100) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - return $this->client->makeRequest( '/customers-corporate/history', RetailcrmHttpClient::METHOD_GET, - $parameters + ['filter' => $filter, 'limit' => $limit] ); } diff --git a/src/upload/system/library/retailcrm/lib/api/RetailcrmProxy.php b/src/upload/system/library/retailcrm/lib/api/RetailcrmProxy.php index f619f9f..8f39603 100644 --- a/src/upload/system/library/retailcrm/lib/api/RetailcrmProxy.php +++ b/src/upload/system/library/retailcrm/lib/api/RetailcrmProxy.php @@ -6,7 +6,7 @@ * @method ordersCreate($order, $site = null) * @method ordersEdit($order, $by = 'externalId', $site = null) * @method ordersGet($order, $by = 'externalId', $site = null) - * @method ordersList($filter, $page, $limit) + * @method ordersList(array $filter = [], $page = null, $limit = null) * @method customersCreate($customer, $site = null) * @method customersEdit($customer, $by = 'externalId', $site = null) * @method customersList(array $filter = [], $page = null, $limit = null) @@ -27,16 +27,16 @@ class RetailcrmProxy { public function __construct($url, $key) { $this->api = new RetailcrmApiClient5($url, $key); - $this->log = new \Log('retailcrm.log'); } public function __call($method, $arguments) { try { - $response = call_user_func_array(array($this->api, $method), $arguments); + $response = call_user_func_array([$this->api, $method], $arguments); if (!$response->isSuccessful()) { $this->log->write(sprintf("[%s] %s", $method, $response->getErrorMsg())); + if (isset($response['errors'])) { $error = implode("\n", $response['errors']); $this->log->write($error . "\n");