diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b10c675..ebd9a345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2026-06-30 v6.8.3 +- Исправлено присвоение PERSON_TYPE_ID для $newOrder + ## 2026-06-18 v6.8.2 - Исправлены замечания по безопасности модуля. Уточнены защитные проверки и обработка данных после повторной проверки приложения на Маркетплейсе. diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index 3e734aa0..dc6ee968 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -20,6 +20,7 @@ use Bitrix\Sale\Delivery\Services\Manager; use Bitrix\Sale\Fuser; use Bitrix\Sale\Internals\PaymentTable; +use Bitrix\Sale\Internals\OrderPropsTable; use Bitrix\Sale\Location\Search\Finder; use Bitrix\Sale\Order; use Bitrix\Sale\OrderUserProperties; @@ -681,44 +682,30 @@ public static function orderHistory(): bool } } - $propsRemove = false; - $personType = $newOrder->getField('PERSON_TYPE_ID'); - - if (RetailCrmOrder::isOrderCorporate($order) - || (!empty($order['contragentType']) && in_array($order['contragentType'], ['legal-entity', 'enterpreneur'])) - ) { - $personType = $contragentTypes['legal-entity']; - $newOrder->setField('PERSON_TYPE_ID', $personType); - - $propsRemove = true; - } else { - if (isset($order['orderType']) && $order['orderType']) { - $nType = []; - $tList = RCrmActions::OrderTypesList([['LID' => $site]]); + $currentPersonType = (int) $newOrder->getField('PERSON_TYPE_ID'); + $personType = self::resolvePersonType( + $order, + $site, + $contragentTypes, + $optionsOrderTypes, + $currentPersonType + ); - foreach ($tList as $type) { - if (isset($optionsOrderTypes[$type['ID']])) { - $nType[$optionsOrderTypes[$type['ID']]] = $type['ID']; - } - } + $personTypeChanged = $personType > 0 && $personType !== $currentPersonType; - $newOptionsOrderTypes = $nType; + $propsRemove = $personType > 0 && ( + RetailCrmOrder::isOrderCorporate($order) + || (!empty($order['contragentType']) + && in_array($order['contragentType'], ['legal-entity', 'enterpreneur'], true)) + || $personTypeChanged + ); - if ($newOptionsOrderTypes[$order['orderType']]) { - if ($personType != $newOptionsOrderTypes[$order['orderType']] && $personType != 0) { - $propsRemove = true; - } + if ($personTypeChanged) { + $newOrder->setField('PERSON_TYPE_ID', $personType); + } - $personType = $newOptionsOrderTypes[$order['orderType']]; - $newOrder->setField('PERSON_TYPE_ID', $personType); - } elseif ($personType == 0) { - RCrmActions::eventLog( - 'RetailCrmHistory::orderHistory', - 'orderType not found', - 'PERSON_TYPE_ID = 0' - ); - } - } + if ($propsRemove) { + self::deleteOrderPropsExceptPersonType($newOrder, $personType); } //status @@ -767,14 +754,7 @@ public static function orderHistory(): bool $orderDump = []; $propertyCollectionArr['properties'] = $nProps; - if ($propsRemove) {//delete props - foreach ($propertyCollectionArr['properties'] as $orderProp) { - if ($orderProp['PROPS_GROUP_ID'] == 0) { - $somePropValue = $propertyCollection->getItemByOrderPropertyId($orderProp['ID']); - self::setProp($somePropValue); - } - } - + if ($propsRemove) { $orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']); $orderDump = $order; @@ -1472,6 +1452,75 @@ public static function orderHistory(): bool return false; } + private static function resolvePersonType( + array $order, + string $site, + array $contragentTypes, + array $optionsOrderTypes, + int $currentPersonType + ): int { + if ( + RetailCrmOrder::isOrderCorporate($order) + || (!empty($order['contragentType']) + && in_array($order['contragentType'], ['legal-entity', 'enterpreneur'], true)) + ) { + return (int) ($contragentTypes['legal-entity'] ?? 0); + } + + if (empty($order['orderType'])) { + return $currentPersonType; + } + + $newOptionsOrderTypes = []; + + foreach (RCrmActions::OrderTypesList([['LID' => $site]]) as $type) { + if (isset($optionsOrderTypes[$type['ID']])) { + $newOptionsOrderTypes[$optionsOrderTypes[$type['ID']]] = (int) $type['ID']; + } + } + + if (isset($newOptionsOrderTypes[$order['orderType']])) { + return $newOptionsOrderTypes[$order['orderType']]; + } + + if ($currentPersonType === 0) { + RCrmActions::eventLog( + 'RetailCrmHistory::orderHistory', + 'orderType not found', + 'PERSON_TYPE_ID = 0' + ); + } + + return $currentPersonType; + } + + private static function deleteOrderPropsExceptPersonType(Order $order, int $personType): void + { + if ($personType <= 0 || !$order->getId()) { + return; + } + + $allowedPropertyIds = []; + $properties = OrderPropsTable::getList([ + 'select' => ['ID'], + 'filter' => ['=PERSON_TYPE_ID' => $personType], + ]); + + while ($property = $properties->fetch()) { + $allowedPropertyIds[] = (int) $property['ID']; + } + + $propertyCollection = $order->getPropertyCollection(); + + foreach ($propertyCollection as $propertyValue) { + $orderPropertyId = (int) $propertyValue->getField('ORDER_PROPS_ID'); + + if (empty($allowedPropertyIds) || !in_array($orderPropertyId, $allowedPropertyIds, true)) { + $propertyValue->delete(); + } + } + } + /** * @param string $shopCode * diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 3b3ab263..b5f2d6f7 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1 @@ -- Исправлены замечания по безопасности модуля. Уточнены защитные проверки и обработка данных после повторной проверки приложения на Маркетплейсе. +- Исправлено присвоение PERSON_TYPE_ID для $newOrder diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index 995e1087..f1a5af79 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,6 +1,6 @@ '6.8.2', - 'VERSION_DATE' => '2026-06-18 12:00:00' + 'VERSION' => '6.8.3', + 'VERSION_DATE' => '2026-06-30 12:00:00' ]; diff --git a/intaro.retailcrm/lib/component/constants.php b/intaro.retailcrm/lib/component/constants.php index 806fd461..1476a436 100644 --- a/intaro.retailcrm/lib/component/constants.php +++ b/intaro.retailcrm/lib/component/constants.php @@ -18,7 +18,7 @@ */ class Constants { - public const MODULE_VERSION = '6.8.2'; + public const MODULE_VERSION = '6.8.3'; public const CRM_PURCHASE_PRICE_NULL = 'purchasePrice_null'; public const BITRIX_USER_ID_PREFIX = 'bitrixUserId-'; public const CRM_USERS_MAP = 'crm_users_map';