From eff355041f8e79900a542c5b26d7c3869c5e9d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A7=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2?= <45812598+Chazovs@users.noreply.github.com> Date: Tue, 25 Aug 2020 11:14:47 +0300 Subject: [PATCH] Add history filter (#122) --- .../general/history/RetailCrmHistory_v5.php | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index 25d95613..35ddbcf7 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -1256,6 +1256,7 @@ class RetailCrmHistory public static function assemblyCustomer($customerHistory) { + $customerHistory = self::filterHistory($customerHistory, 'customer'); $server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot(); $fields = array(); if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) { @@ -1324,6 +1325,7 @@ class RetailCrmHistory public static function assemblyOrder($orderHistory) { + $orderHistory = self::filterHistory($orderHistory, 'order'); $server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot(); if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) { @@ -1469,7 +1471,76 @@ class RetailCrmHistory return $orders; } - + + /** + * Filters out history by these terms: + * - Changes from current API key will be added only if CMS changes are more actual than history. + * - All other changes will be merged as usual. + * It fixes these problems: + * - Changes from current API key are merged when it's not needed. + * - Changes from CRM can overwrite more actual changes from CMS due to ignoring current API key changes. + * + * @param array $historyEntries Raw history from CRM + * @param string $recordType Entity field name, e.g. `customer` or `order`. + * + * @return array + */ + private static function filterHistory($historyEntries, $recordType) + { + $history = []; + $organizedHistory = []; + $notOurChanges = []; + + foreach ($historyEntries as $entry) { + if (!isset($entry[$recordType]['externalId'])) { + if ($entry['source'] == 'api' + && isset($change['apiKey']['current']) + && $entry['apiKey']['current'] == true + && $entry['field'] != 'externalId' + ) { + continue; + } + + $history[] = $entry; + + continue; + } + + $externalId = $entry[$recordType]['externalId']; + $field = $entry['field']; + + if (!isset($organizedHistory[$externalId])) { + $organizedHistory[$externalId] = []; + } + + if (!isset($notOurChanges[$externalId])) { + $notOurChanges[$externalId] = []; + } + + if ($entry['source'] == 'api' + && isset($entry['apiKey']['current']) + && $entry['apiKey']['current'] == true + ) { + if (isset($notOurChanges[$externalId][$field]) || $entry['field'] == 'externalId') { + $organizedHistory[$externalId][] = $entry; + } else { + continue; + } + } else { + $organizedHistory[$externalId][] = $entry; + $notOurChanges[$externalId][$field] = true; + } + } + + unset($notOurChanges); + + foreach ($organizedHistory as $historyChunk) { + $history = array_merge($history, $historyChunk); + } + + return $history; + } + /** * Update shipment in order *