...Человеческий поиск в разработке...
- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/entitypreset.php
- Класс: Bitrix\Crm\EntityPreset
- Вызов: EntityPreset::changeCurrentCountry
public function changeCurrentCountry(int $countryId): Main\Result { $result = new Main\Result(); if ($countryId <= 0 || !in_array($countryId, EntityRequisite::getAllowedRqFieldCountries(), true)) { $result->addError(new Main\Error("Incorrect country for change (ID: $countryId)")); } if ($result->isSuccess()) { if (!static::checkChangeCurrentCountryPermission()) { $result->addError(new Main\Error('Access denied!')); } } if ($result->isSuccess()) { $currentCountryId = static::getCurrentCountryId(); if ($currentCountryId > 0) { //region Delete all default presets for which there are no requisites. $fixedPresetList = EntityRequisite::getFixedPresetList(); $defPresetsXmlIds = []; foreach ($fixedPresetList as $presetInfo) { $defPresetsXmlIds[] = $presetInfo['XML_ID']; } unset($presetInfo); $existsDefPresetMap = []; $existsDefPresetsByXmlId = []; $res = $this->getList( [ 'filter' => [ '=ENTITY_TYPE_ID' => \CCrmOwnerType::Requisite, [ 'LOGIC' => 'OR', '=CREATED_BY_ID' => 0, '@XML_ID' => $defPresetsXmlIds, ] ], 'select' => ['ID', 'COUNTRY_ID', 'XML_ID'] ] ); unset($defPresetsXmlIds); if (is_object($res)) { $requisite = EntityRequisite::getSingleInstance(); $emptyXmlIdIndex = 1; while ($row = $res->fetch()) { $id = (int)$row['ID']; $presetCountryId = (int)$row['COUNTRY_ID']; $deleted = 'N'; if ($presetCountryId !== $countryId) { $resRq = $requisite->getList( [ 'filter' => ['=PRESET_ID' => $id], 'select' => ['ID'], 'limit' => 1, ] ); if (is_object($resRq)) { $rowRq = $resRq->fetch(); if (!$rowRq) { // Remove directly through the table because you need to avoid // checks and setting default presets. $delResult = PresetTable::delete($id); if ($delResult->isSuccess()) { $deleted = 'Y'; } } } } $xmlId = ''; if (isset($row['XML_ID']) && is_string($row['XML_ID']) && $row['XML_ID'] !== '') { $xmlId = $row['XML_ID']; $index = $row['XML_ID']; } else { $index = ""; $emptyXmlIdIndex++; } $emptyXmlIdIndex++; $existsDefPresetMap[$id] = [ 'COUNTRY_ID' => $presetCountryId, 'XML_ID' => $xmlId, 'deleted' => $deleted ]; $existsDefPresetsByXmlId[$index] = [ 'ID' => $id, 'COUNTRY_ID' => $presetCountryId, 'deleted' => $deleted ]; } } unset( $id, $deleted, $row, $res, $rowRq, $resRq, $presetCountryId, $requisite, $resDel, $defPresetsOptionValue, $emptyXmlIdIndex, $xmlId, $index ); //endregion Delete all default presets for which there are no requisites. //region Create new default presets. $sort = 500; $datetimeEntity = new Main\DB\SqlExpression( Main\Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction() ); foreach ($fixedPresetList as $presetData) { if ($countryId === (int)$presetData['COUNTRY_ID']) { $sort += 10; if ( !isset($existsDefPresetsByXmlId[$presetData['XML_ID']]) || ( isset($existsDefPresetsByXmlId[$presetData['XML_ID']]['deleted']) && $existsDefPresetsByXmlId[$presetData['XML_ID']]['deleted'] === 'Y' ) ) { $presetFields = [ 'ENTITY_TYPE_ID' => EntityPreset::Requisite, 'COUNTRY_ID' => $countryId, 'DATE_CREATE' => $datetimeEntity, 'CREATED_BY_ID' => 0, 'NAME' => $presetData['NAME'], 'ACTIVE' => $presetData['ACTIVE'], 'SORT' => $sort, 'XML_ID' => $presetData['XML_ID'], 'SETTINGS' => $presetData['SETTINGS'] ]; //region Rename existing presets if their names match the new one. $res = $this->getList( [ 'filter' => [ '=ENTITY_TYPE_ID' => \CCrmOwnerType::Requisite, '=NAME' => $presetFields['NAME'], ], 'select' => ['ID', 'NAME', 'COUNTRY_ID'], ] ); if (is_object($res)) { while ($row = $res->fetch()) { $countryCode = EntityPreset::getCountryCodeById($row['COUNTRY_ID']); if ($countryCode !== '') { PresetTable::update( (int)$row['ID'], ['NAME' => $row['NAME'] . " ($countryCode)"] ); } } } unset($res, $row, $countryCode); //endregion Rename existing presets if their names match the new one. PresetTable::add($presetFields); } } } unset( $existsDefPresetsByXmlId, $fixedPresetList, $presetData, $sort, $presetFields ); //endregion Create new default presets. // Set new identifier of the current country. Option::set('crm', 'crm_requisite_preset_country_id', $countryId); $entityTypeNames = ['COMPANY', 'CONTACT']; //region Get current default presets identifiers $defPresetMap = []; foreach ($entityTypeNames as $entityTypeName) { $defPresetMap[$entityTypeName] = [ 'ID' => 0, 'COUNTRY_ID' => 0, 'NAME' => '', 'SETTINGS' => [] ]; } unset($entityTypeName); $optionValue = Option::get('crm', 'requisite_default_presets'); $optionModified = false; if ($optionValue !== '') { $optionValue = unserialize($optionValue, ['allowed_classes' => false]); } if (!is_array($optionValue)) { $optionValue = []; } foreach ($entityTypeNames as $entityTypeName) { if (isset($optionValue[$entityTypeName])) { $defPresetMap[$entityTypeName]['ID'] = (int)$optionValue[$entityTypeName]; if ($defPresetMap[$entityTypeName]['ID'] < 0) { $defPresetMap[$entityTypeName]['ID'] = 0; $optionModified = true; } } } unset($entityTypeName); //region Check existing of default presets $existsDefPreset = []; foreach ($entityTypeNames as $entityTypeName) { $existsDefPreset[$entityTypeName] = false; } unset($entityTypeName); if ($defPresetMap['COMPANY']['ID'] > 0 || $defPresetMap['CONTACT']['ID'] > 0) { $ids = []; foreach ($entityTypeNames as $entityTypeName) { if ( $defPresetMap[$entityTypeName]['ID'] > 0 && !in_array($defPresetMap[$entityTypeName]['ID'], $ids, true) ) { $ids[] = $defPresetMap[$entityTypeName]['ID']; } } unset($entityTypeName); if (!empty($ids)) { $res = $this->getList( [ 'filter' => [ '=ENTITY_TYPE_ID' => \CCrmOwnerType::Requisite, '@ID' => $ids ], 'select' => ['ID', 'COUNTRY_ID', 'NAME', 'SETTINGS'], ] ); if (is_object($res)) { while ($row = $res->fetch()) { $presetId = is_array($row) && isset($row['ID']) ? (int)$row['ID'] : 0; if ($presetId > 0) { foreach ($entityTypeNames as $entityTypeName) { if ($presetId === $defPresetMap[$entityTypeName]['ID']) { $existsDefPreset[$entityTypeName] = true; $defPresetMap[$entityTypeName]['COUNTRY_ID'] = (int)$row['COUNTRY_ID']; if ($defPresetMap[$entityTypeName]['COUNTRY_ID'] < 0) { $defPresetMap[$entityTypeName]['COUNTRY_ID'] = 0; } if (isset($row['NAME']) && is_string($row['NAME']) && $row['NAME'] !== '') { $defPresetMap[$entityTypeName]['NAME'] = $row['NAME']; } if (is_array($row['SETTINGS'])) { $defPresetMap[$entityTypeName]['SETTINGS'] = $row['SETTINGS']; } } } } } } unset($ids, $res, $row, $presetId, $entityTypeName); } } foreach ($entityTypeNames as $entityTypeName) { if (!$existsDefPreset[$entityTypeName]) { $defPresetMap[$entityTypeName]['ID'] = 0; $optionModified = true; } } unset($entityTypeName, $existsDefPreset); //endregion Check existing of default presets $optionValue = []; foreach ($entityTypeNames as $entityTypeName) { $optionValue[$entityTypeName] = $defPresetMap[$entityTypeName]['ID']; } unset($entityTypeNames, $entityTypeName); //endregion Get current default presets identifiers //region Reset default presets to option $countryCode = static::getCountryCodeById($countryId); $personTypeMap = [ 'COMPANY' => $countryCode === 'RU' ? 'COMPANY' : 'LEGALENTITY', 'CONTACT' => 'PERSON', ]; foreach ($personTypeMap as $optionParamName => $personType) { $xmlId = str_replace( ['%COUNTRY%', '%PERSON%'], [$countryCode, $personType], '#CRM_REQUISITE_PRESET_DEF_%COUNTRY%_%PERSON%#' ); $res = $this->getList( [ 'order' => ['SORT' => 'ASC', 'ID' => 'ASC'], 'filter' => [ '=ENTITY_TYPE_ID' => \CCrmOwnerType::Requisite, '=XML_ID' => $xmlId ], 'select' => ['ID', 'COUNTRY_ID', 'NAME', 'SETTINGS'], 'limit' => 1 ] ); if (is_object($res)) { $row = $res->fetch(); if (is_array($row)) { $presetId = isset($row['ID']) ? (int)$row['ID'] : 0; $presetCountryId = (isset($row['COUNTRY_ID']) && $row['COUNTRY_ID'] > 0) ? (int)$row['COUNTRY_ID'] : 0 ; $presetName = (isset($row['NAME']) && is_string($row['NAME'])) ? $row['NAME'] : ''; $settings = is_array($row['SETTINGS']) ? $row['SETTINGS'] : []; if ($presetId > 0) { $optionValue[$optionParamName] = $presetId; $defPresetMap[$optionParamName]['ID'] = $presetId; $defPresetMap[$optionParamName]['COUNTRY_ID'] = $presetCountryId; $defPresetMap[$optionParamName]['NAME'] = $presetName; $defPresetMap[$optionParamName]['SETTINGS'] = $settings; $optionModified = true; } } } } unset( $personTypeMap, $optionParamName, $personType, $xmlId, $res, $row, $presetId, $presetCountryId, $presetName, $settings ); if ($optionModified) { Option::set('crm', 'requisite_default_presets', serialize($optionValue)); } unset($optionModified, $optionValue); //endregion Reset default presets to option //region Convert synchronization settings for order properties with requisite fields $this->convertOrderPropsSyncSettings( $defPresetMap, $existsDefPresetMap ); //endregion Convert synchronization settings for order properties with requisite fields unset($defPresetMap, $existsDefPresetMap); } } return $result; }