• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/Relation/RelationManager.php
  • Класс: Bitrix\Crm\Relation\RelationManager
  • Вызов: RelationManager::getPredefinedRelations
protected function getPredefinedRelations(): Relation\Collection
{
	if ($this->predefinedRelations !== null)
	{
		return $this->predefinedRelations;
	}

	$quoteFactory = Container::getInstance()->getFactory(\CCrmOwnerType::Quote);

	$bindingSettings =
		(new Settings())
			->setIsPredefined(true)
			->setIsChildrenListEnabled(true)
			->setRelationType(RelationType::BINDING)
	;

	$conversionSettings =
		(clone $bindingSettings)
			->setRelationType(RelationType::CONVERSION)
	;

	$predefinedRelations = [
		//region Contact child predefined relations
		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::Invoice),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmInvoice::class, 'UF_CONTACT_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::Quote),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\ContactToFactory($quoteFactory)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::Deal),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\EntityBinding(
				[DealContactTable::class, 'getDealContactIDs'],
				[DealContactTable::class, 'getContactDealIDs'],
				[DealContactTable::class, 'bindContactIDs'],
				[DealContactTable::class, 'unbindContactIDs'],
				static function($fromId, $toId) {
					DealContactTable::rebindAllDeals($fromId, $toId);
					\CCrmDeal::Rebind(\CCrmOwnerType::Contact, $fromId, $toId);
				}
			)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::Lead),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\EntityBinding(
				[LeadContactTable::class, 'getLeadContactIDs'],
				[LeadContactTable::class, 'getContactLeadIDs'],
				[LeadContactTable::class, 'bindContactIDs'],
				[LeadContactTable::class, 'unbindContactIDs'],
				static function($fromId, $toId) {
					LeadContactTable::rebindAllLeads($fromId, $toId);
					\CCrmLead::Rebind(\CCrmOwnerType::Contact, $fromId, $toId);
				}
			)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::Order),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\ContactToOrder()),
		//endregion

		//region Company child predefined relations
		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Lead),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmLead::class, 'COMPANY_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Deal),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmDeal::class, 'COMPANY_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Quote),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Factory($quoteFactory, Item::FIELD_NAME_COMPANY_ID)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Invoice),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmInvoice::class, 'UF_COMPANY_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Contact),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\EntityBinding(
				[ContactCompanyTable::class, 'getContactCompanyIDs'],
				[ContactCompanyTable::class, 'getCompanyContactIDs'],
				[ContactCompanyTable::class, 'bindCompanyIDs'],
				[ContactCompanyTable::class, 'unbindCompanyIDs'],
				[ContactCompanyTable::class, 'rebindAllContacts']
			)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::Order),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\CompanyToOrder()),
		//endregion

		//region Deal child predefined relations
		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Deal, \CCrmOwnerType::Quote),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Factory($quoteFactory, Item\Quote::FIELD_NAME_DEAL_ID)),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Deal, \CCrmOwnerType::Invoice),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmInvoice::class, 'UF_DEAL_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Deal, \CCrmOwnerType::Order),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\EntityToOrder()),
		//endregion

		//region Lead child predefined relations
		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Lead, \CCrmOwnerType::Company),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmCompany::class, 'LEAD_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmContact::class, 'LEAD_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmDeal::class, 'LEAD_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Lead, \CCrmOwnerType::Quote),
			clone $bindingSettings
		))
			->setStorageStrategy(new StorageStrategy\Factory($quoteFactory, Item\Quote::FIELD_NAME_LEAD_ID)),
		//endregion

		//region Quote child predefined relations
		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Quote, \CCrmOwnerType::Deal),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmDeal::class, 'QUOTE_ID')),

		(new Relation(
			new RelationIdentifier(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice),
			clone $conversionSettings
		))
			->setStorageStrategy(new StorageStrategy\Compatible(\CCrmInvoice::class, 'UF_QUOTE_ID')),
		//endregion
	];

	//region SmartInvoice predefined relations
	$factory = Container::getInstance()->getFactory(\CCrmOwnerType::SmartInvoice);
	if ($factory)
	{

		$predefinedRelations[] =
			(new Relation(
				new RelationIdentifier(\CCrmOwnerType::SmartInvoice, \CCrmOwnerType::Order),
				clone $bindingSettings
			))
			->setStorageStrategy(new StorageStrategy\EntityToOrder())
		;

		$predefinedRelations[] =
			(new Relation(
				new RelationIdentifier(\CCrmOwnerType::Contact, \CCrmOwnerType::SmartInvoice),
				clone $bindingSettings
			))
			->setStorageStrategy(new StorageStrategy\ContactToFactory($factory))
		;

		$predefinedRelations[] =
			(new Relation(
				new RelationIdentifier(\CCrmOwnerType::Company, \CCrmOwnerType::SmartInvoice),
				clone $bindingSettings
			))
			->setStorageStrategy(new StorageStrategy\Factory($factory, Item::FIELD_NAME_COMPANY_ID))
		;
	}
	//endregion

	//@TODO collect predefined relations across modules via event
	if (Loader::includeModule('catalog'))
	{
		if (Manager::isActiveProviderByModule(Manager::PROVIDER_STORE_DOCUMENT, 'crm'))
		{
			$predefinedRelations[] =
				(new Relation(
					new RelationIdentifier(
						\CCrmOwnerType::Company,
						\CCrmOwnerType::StoreDocument
					),
					clone $bindingSettings
				))
					->setStorageStrategy(new StoreDocumentRelationStorageStrategy(\CCrmOwnerType::Company));

			$predefinedRelations[] =
				(new Relation(
					new RelationIdentifier(
						\CCrmOwnerType::Contact,
						\CCrmOwnerType::StoreDocument
					),
					clone $bindingSettings
				))
					->setStorageStrategy(new StoreDocumentRelationStorageStrategy(\CCrmOwnerType::Contact));
		}

		if (Manager::isActiveProviderByModule(Manager::PROVIDER_AGENT_CONTRACT, 'crm'))
		{
			$predefinedRelations[] =
				(new Relation(
					new RelationIdentifier(
						\CCrmOwnerType::Company,
						\CCrmOwnerType::AgentContractDocument
					),
					clone $bindingSettings
				))
					->setStorageStrategy(new AgentContractRelationStorageStrategy(\CCrmOwnerType::Company));

			$predefinedRelations[] =
				(new Relation(
					new RelationIdentifier(
						\CCrmOwnerType::Contact,
						\CCrmOwnerType::AgentContractDocument
					),
					clone $bindingSettings
				))
					->setStorageStrategy(new AgentContractRelationStorageStrategy(\CCrmOwnerType::Contact));
		}
	}

	$this->mixinPredefinedRelationsForDynamic($predefinedRelations);

	$this->predefinedRelations = new Relation\Collection($predefinedRelations);

	return $this->predefinedRelations;
}