Optimization of the history processing algorithm (#254)

This commit is contained in:
Uryvskiy Dima 2023-04-28 11:44:38 +03:00 committed by GitHub
parent feaeca458f
commit 090a5783c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 139 deletions

View File

@ -1,3 +1,6 @@
## v4.1.9
* Optimization of the history processing algorithm
## v4.1.8 ## v4.1.8
* Fixed customer externalId when creating a customer and sending it to RetailCRM * Fixed customer externalId when creating a customer and sending it to RetailCRM

View File

@ -1 +1 @@
4.1.8 4.1.9

View File

@ -186,42 +186,21 @@ class ControllerExtensionModuleRetailcrm extends Controller
$this->request->post $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( $api = $this->retailcrm->getApiClient(
$this->request->post[$this->moduleTitle . '_url'], $this->request->post[$this->moduleTitle . '_url'],
$this->request->post[$this->moduleTitle . '_apikey'] $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( $this->model_setting_setting->editSetting(
'retailcrm_history', 'retailcrm_history',
array( [
'retailcrm_history_orders' => isset($sinceIdOrders) ? $sinceIdOrders : 1, 'retailcrm_history_orders' => $this->getHistorySinceId($api, 'ordersHistory'),
'retailcrm_history_customers' => isset($sinceIdCustomers) ? $sinceIdCustomers : 1 'retailcrm_history_customers' => $this->getHistorySinceId($api, 'customersHistory'),
) ]
); );
} }
@ -934,4 +913,33 @@ class ControllerExtensionModuleRetailcrm extends Controller
return false; 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;
}
} }

View File

@ -72,15 +72,10 @@ class ModelExtensionRetailcrmHistory extends Model {
return false; return false;
} }
$sinceIdOrders = $history['retailcrm_history_orders'] ? $history['retailcrm_history_orders'] : null; $sinceIdOrders = $history['retailcrm_history_orders'] ?? 0;
$sinceIdCustomers = $history['retailcrm_history_customers'] ? $history['retailcrm_history_customers'] : null; $sinceIdCustomers = $history['retailcrm_history_customers'] ?? 0;
$packsOrders = $retailcrmApiClient->ordersHistory(['sinceId' => $sinceIdOrders]);
$packsOrders = $retailcrmApiClient->ordersHistory(array( $packsCustomers = $retailcrmApiClient->customersHistory(['sinceId' => $sinceIdCustomers]);
'sinceId' => $sinceIdOrders ? $sinceIdOrders : 0
), 1, 100);
$packsCustomers = $retailcrmApiClient->customersHistory(array(
'sinceId' => $sinceIdCustomers ? $sinceIdCustomers : 0
), 1, 100);
if (!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0 if (!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0
&& !$packsCustomers->isSuccessful() && count($packsCustomers->history) <= 0 && !$packsCustomers->isSuccessful() && count($packsCustomers->history) <= 0
@ -88,30 +83,28 @@ class ModelExtensionRetailcrmHistory extends Model {
return false; return false;
} }
$orders = RetailcrmHistoryHelper::assemblyOrder($packsOrders->history);
$customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history);
$ordersHistory = $packsOrders->history; $ordersHistory = $packsOrders->history;
$customersHistory = $packsCustomers->history; $customersHistory = $packsCustomers->history;
$lastChangeOrders = $ordersHistory ? end($ordersHistory) : null; $lastChangeOrders = $ordersHistory ? end($ordersHistory) : null;
$lastChangeCustomers = $customersHistory ? end($customersHistory) : null; $lastChangeCustomers = $customersHistory ? end($customersHistory) : null;
if ($lastChangeOrders !== null) { if ($lastChangeOrders !== null && $lastChangeCustomers !== null) {
$sinceIdOrders = $lastChangeOrders['id']; $this->model_setting_setting->editSetting(
} 'retailcrm_history',
[
if ($lastChangeCustomers !== null) { 'retailcrm_history_orders' => $lastChangeOrders['id'],
$sinceIdCustomers = $lastChangeCustomers['id']; 'retailcrm_history_customers' => $lastChangeCustomers['id']
]
);
} }
$orders = RetailcrmHistoryHelper::assemblyOrder($ordersHistory);
$customers = RetailcrmHistoryHelper::assemblyCustomer($customersHistory);
$newOrders = [];
$updatedOrders = [];
$this->settings = $settings; $this->settings = $settings;
$this->status = array_flip($settings[$this->moduleTitle . '_status']); $this->status = array_flip($settings[$this->moduleTitle . '_status']);
$updatedOrders = array();
$newOrders = array();
foreach ($orders as $order) { foreach ($orders as $order) {
if (isset($order['deleted'])) { if (isset($order['deleted'])) {
continue; continue;
@ -126,7 +119,7 @@ class ModelExtensionRetailcrmHistory extends Model {
unset($orders); unset($orders);
$updateCustomers = array(); $updateCustomers = [];
foreach ($customers as $customer) { foreach ($customers as $customer) {
if (isset($customer['deleted'])) { if (isset($customer['deleted'])) {
@ -141,34 +134,27 @@ class ModelExtensionRetailcrmHistory extends Model {
unset($customers); unset($customers);
if (!empty($updateCustomers)) { if (!empty($updateCustomers)) {
$customers = $retailcrmApiClient->customersList(array('ids' => $updateCustomers)); $customers = $retailcrmApiClient->customersList(['ids' => $updateCustomers]);
if ($customers) { if ($customers) {
$this->updateCustomers($customers['customers']); $this->updateCustomers($customers['customers']);
} }
} }
if (!empty($newOrders)) { if (!empty($newOrders)) {
$orders = $retailcrmApiClient->ordersList(array('ids' => $newOrders)); $orders = $retailcrmApiClient->ordersList(['ids' => $newOrders]);
if ($orders) { if ($orders) {
$this->createResult = $this->createOrders($orders['orders'], $retailcrmApiClient); $this->createResult = $this->createOrders($orders['orders'], $retailcrmApiClient);
} }
} }
if (!empty($updatedOrders)) { if (!empty($updatedOrders)) {
$orders = $retailcrmApiClient->ordersList(array('ids' => $updatedOrders)); $orders = $retailcrmApiClient->ordersList(['ids' => $updatedOrders]);
if ($orders) { if ($orders) {
$this->updateOrders($orders['orders'], $retailcrmApiClient); $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'])) { if (!empty($this->createResult['customers'])) {
$retailcrmApiClient->customersFixExternalIds($this->createResult['customers']); $retailcrmApiClient->customersFixExternalIds($this->createResult['customers']);
} }

View File

@ -584,30 +584,18 @@ class RetailcrmApiClient5
/** /**
* Get orders history * Get orders history
*
* @param array $filter * @param array $filter
* @param null $page * @param int|null $limit
* @param null $limit
* *
* @return ApiResponse * @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( return $this->client->makeRequest(
'/orders/history', '/orders/history',
RetailcrmHttpClient::METHOD_GET, RetailcrmHttpClient::METHOD_GET,
$parameters ['filter' => $filter, 'limit' => $limit]
); );
} }
@ -908,30 +896,18 @@ class RetailcrmApiClient5
/** /**
* Get customers history * Get customers history
*
* @param array $filter * @param array $filter
* @param null $page * @param int|null $limit
* @param null $limit
* *
* @return ApiResponse * @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( return $this->client->makeRequest(
'/customers/history', '/customers/history',
RetailcrmHttpClient::METHOD_GET, RetailcrmHttpClient::METHOD_GET,
$parameters ['filter' => $filter, 'limit' => $limit]
); );
} }
@ -1108,34 +1084,17 @@ class RetailcrmApiClient5
/** /**
* Get orders assembly history * Get orders assembly history
* *
* @param array $filter (default: array()) * @param array $filter
* @param int $page (default: null) * @param int|null $limit
* @param int $limit (default: null)
*
* @throws \InvalidArgumentException
* @throws \RetailCrm\Exception\CurlException
* @throws \RetailCrm\Exception\InvalidJsonException
* *
* @return ApiResponse * @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( return $this->client->makeRequest(
'/orders/packs/history', '/orders/packs/history',
RetailcrmHttpClient::METHOD_GET, RetailcrmHttpClient::METHOD_GET,
$parameters ['filter' => $filter, 'limit' => $limit]
); );
} }
@ -2452,30 +2411,18 @@ class RetailcrmApiClient5
/** /**
* Get corporate customers history * 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( return $this->client->makeRequest(
'/customers-corporate/history', '/customers-corporate/history',
RetailcrmHttpClient::METHOD_GET, RetailcrmHttpClient::METHOD_GET,
$parameters ['filter' => $filter, 'limit' => $limit]
); );
} }

View File

@ -6,7 +6,7 @@
* @method ordersCreate($order, $site = null) * @method ordersCreate($order, $site = null)
* @method ordersEdit($order, $by = 'externalId', $site = null) * @method ordersEdit($order, $by = 'externalId', $site = null)
* @method ordersGet($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 customersCreate($customer, $site = null)
* @method customersEdit($customer, $by = 'externalId', $site = null) * @method customersEdit($customer, $by = 'externalId', $site = null)
* @method customersList(array $filter = [], $page = null, $limit = null) * @method customersList(array $filter = [], $page = null, $limit = null)
@ -27,16 +27,16 @@ class RetailcrmProxy {
public function __construct($url, $key) { public function __construct($url, $key) {
$this->api = new RetailcrmApiClient5($url, $key); $this->api = new RetailcrmApiClient5($url, $key);
$this->log = new \Log('retailcrm.log'); $this->log = new \Log('retailcrm.log');
} }
public function __call($method, $arguments) { public function __call($method, $arguments) {
try { try {
$response = call_user_func_array(array($this->api, $method), $arguments); $response = call_user_func_array([$this->api, $method], $arguments);
if (!$response->isSuccessful()) { if (!$response->isSuccessful()) {
$this->log->write(sprintf("[%s] %s", $method, $response->getErrorMsg())); $this->log->write(sprintf("[%s] %s", $method, $response->getErrorMsg()));
if (isset($response['errors'])) { if (isset($response['errors'])) {
$error = implode("\n", $response['errors']); $error = implode("\n", $response['errors']);
$this->log->write($error . "\n"); $this->log->write($error . "\n");