• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/cleaning/cleaningmanager.php
  • Класс: Bitrix\Crm\Cleaning\CleaningManager
  • Вызов: CleaningManager::customizeCleaner
static function customizeCleaner(Cleaner $cleaner): void
{
	$entityTypeId = $cleaner->getOptions()->getEntityTypeId();

	if (
		$entityTypeId === \CCrmOwnerType::Lead
		|| $entityTypeId === \CCrmOwnerType::Contact
		|| $entityTypeId === \CCrmOwnerType::Company
	)
	{
		$cleaner->addJob(
			new class extends Cleaner\Job {
				public function run(Options $options): Result
				{
					EntityAddress::unregister(
						$options->getEntityTypeId(),
						$options->getEntityId(),
						EntityAddressType::Primary,
					);

					return new Result();
				}
			}
		);
	}

	if ($entityTypeId === \CCrmOwnerType::Company)
	{
		$cleaner->addJob(
			new class extends Cleaner\Job {
				public function run(Options $options): Result
				{
					EntityAddress::unregister(
						$options->getEntityTypeId(),
						$options->getEntityId(),
						EntityAddressType::Registered,
					);

					return new Result();
				}
			}
		);
	}

	if ($entityTypeId === \CCrmOwnerType::Contact)
	{
		$cleaner
			->addJob(
				new class extends Cleaner\Job {
					public function run(Options $options): Result
					{
						\Bitrix\Crm\Binding\LeadContactTable::unbindAllLeads($options->getEntityId());
						\Bitrix\Crm\Binding\DealContactTable::unbindAllDeals($options->getEntityId());
						\Bitrix\Crm\Binding\ContactCompanyTable::unbindAllCompanies($options->getEntityId());
						\Bitrix\Crm\Binding\QuoteContactTable::unbindAllQuotes($options->getEntityId());
						\Bitrix\Crm\Binding\EntityContactTable::deleteByContact($options->getEntityId());

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Options $options): Result
					{
						$identifier = new ItemIdentifier($options->getEntityTypeId(), $options->getEntityId());

						\CCrmLiveFeed::deleteUserCrmConnection(
							\Bitrix\Crm\UserField\Types\ElementType::getValueByIdentifier($identifier),
						);

						return new Result();
					}
				}
			)
		;
	}

	if ($entityTypeId === \CCrmOwnerType::Company || $entityTypeId === \CCrmOwnerType::Contact)
	{
		$cleaner
			->addJob(
				new class extends Cleaner\Job {
					public function run(Options $options): Result
					{
						if (Main\Loader::includeModule('sale'))
						{
							$binding = new \Bitrix\Crm\Order\ContactCompanyBinding($options->getEntityTypeId());
							$binding->unbind($options->getEntityId());
						}

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Options $options): Result
					{
						(new Contractor\StoreDocumentContactCompanyBinding($options->getEntityTypeId()))
							->unbind($options->getEntityId());
						(new Contractor\AgentContractContactCompanyBinding($options->getEntityTypeId()))
							->unbind($options->getEntityId());

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Options $options): Result
					{
						$requisite = new \Bitrix\Crm\EntityRequisite();

						return $requisite->deleteByEntity($options->getEntityTypeId(), $options->getEntityId());
					}
				}
			)
		;
	}

	if ($entityTypeId === \CCrmOwnerType::Deal)
	{
		$cleaner->addJob(
			new class extends Cleaner\Job {
				public function run(Cleaner\Options $options): Result
				{
					\Bitrix\Crm\Binding\DealContactTable::unbindAllContacts($options->getEntityId());

					return new Result();
				}
			}
		);
	}

	if ($entityTypeId === \CCrmOwnerType::Quote)
	{
		$cleaner
			->addJob(
				new class extends Cleaner\Job {
					public function run(Cleaner\Options $options): Result
					{
						\Bitrix\Crm\Binding\QuoteContactTable::unbindAllContacts($options->getEntityId());

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Cleaner\Options $options): Result
					{
						return \Bitrix\Crm\QuoteElementTable::deleteByQuoteId($options->getEntityId());
					}
				}
			)
		;
	}

	if (\CCrmOwnerType::isUseDynamicTypeBasedApproach($entityTypeId))
	{
		$cleaner
			->addJob(
				new class extends Cleaner\Job {
					public function run(Cleaner\Options $options): Result
					{
						\Bitrix\Crm\Binding\EntityContactTable::deleteByItem(
							$options->getEntityTypeId(),
							$options->getEntityId(),
						);

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Cleaner\Options $options): Result
					{
						\Bitrix\Crm\Model\AssignedTable::deleteByItem(
							$options->getEntityTypeId(),
							$options->getEntityId(),
						);

						return new Result();
					}
				}
			)
			->addJob(
				new class extends Cleaner\Job {
					public function run(Cleaner\Options $options): Result
					{
						\Bitrix\Crm\Binding\OrderEntityTable::deleteByOwner(
							$options->getEntityTypeId(),
							$options->getEntityId(),
						);

						return new Result();
					}
				}
			)
		;
	}
}