• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/requisite/entitylink.php
  • Класс: Bitrix\Crm\Requisite\EntityLink
  • Вызов: EntityLink::checkConsistence
static function checkConsistence($entityTypeId, $entityId,
	$requisiteId, $bankDetailId, $mcRequisiteId, $mcBankDetailId, $options = null)
{
	$enableEntityCheck = (!is_array($options) || !isset($options['ENTITY_CHECK'])
		|| $options['ENTITY_CHECK'] === 'Y' || $options['ENTITY_CHECK'] === true);
	$skipIsMyCompanyCheck = is_array($options) && isset($options['IS_MY_COMPANY_CHECK'])
		&& $options['IS_MY_COMPANY_CHECK'] !== 'Y' && $options['IS_MY_COMPANY_CHECK'] !== true;

	if (!$enableEntityCheck
		&& (!is_array($options)
			|| !is_array($options['CLIENT_SELLER_INFO'])
			|| !isset($options['CLIENT_SELLER_INFO']['CLIENT_ENTITY_TYPE_ID'])
			|| !isset($options['CLIENT_SELLER_INFO']['CLIENT_ENTITY_ID'])
			|| !(\CCrmOwnerType::IsDefined($options['CLIENT_SELLER_INFO']['CLIENT_ENTITY_TYPE_ID'])
				|| $options['CLIENT_SELLER_INFO']['CLIENT_ENTITY_TYPE_ID'] === \CCrmOwnerType::Undefined)
			|| ($options['CLIENT_ENTITY_ID'] ?? null) < 0))
	{
		throw new Main\SystemException(
			'The entity check is disabled, but client type and id are not specified.',
			self::ERR_ENTITY_CHECK_N_BUT_CLIENT_NOT_SPECIFIED
		);
	}

	$availableEntityTypeIds = static::getAvailableEntityTypeIds();

	if ($enableEntityCheck && !isset($availableEntityTypeIds[$entityTypeId]))
	{
		throw new Main\SystemException(
			'Entity type is not defined or invalid.',
			self::ERR_INVALID_ENTITY_TYPE
		);
	}

	if($enableEntityCheck && !(is_int($entityId) && $entityId > 0))
	{
		throw new Main\SystemException(
			'Entity identifier is not defined or invalid.',
			self::ERR_INVALID_ENTITY_ID
		);
	}

	if(!is_int($requisiteId) || $requisiteId < 0)
	{
		throw new Main\SystemException(
			'Requisite identifier is not defined or invalid.',
			self::ERR_INVALID_REQUSIITE_ID
		);
	}

	if(!is_int($bankDetailId) || $bankDetailId < 0)
	{
		throw new Main\SystemException(
			'BankDetail identifier is not defined or invalid.',
			self::ERR_INVALID_BANK_DETAIL_ID
		);
	}

	if(!is_int($mcRequisiteId) || $mcRequisiteId < 0)
	{
		throw new Main\SystemException(
			'Requisite identifier of your company is not defined or invalid.',
			self::ERR_INVALID_MC_REQUSIITE_ID
		);
	}

	if(!is_int($mcBankDetailId) || $mcBankDetailId < 0)
	{
		throw new Main\SystemException(
			'BankDetail identifier of your company is not defined or invalid.',
			self::ERR_INVALID_MC_BANK_DETAIL_ID
		);
	}

	if (is_array($options) && is_array($options['CLIENT_SELLER_INFO']))
	{
		$clientSellerInfo = $options['CLIENT_SELLER_INFO'];
	}
	else
	{
		$clientSellerInfo = self::getEntityClientSellerInfo($entityTypeId, $entityId);
	}

	$requisite = new EntityRequisite();
	$bankDetail = new EntityBankDetail();
	$entityTypeName = null;

	if ($requisiteId > 0)
	{
		if ($clientSellerInfo['CLIENT_ENTITY_TYPE_ID'] === \CCrmOwnerType::Undefined
			|| $clientSellerInfo['CLIENT_ENTITY_ID'] <= 0)
		{
			if ($entityTypeName === null)
			{
				$entityTypeName = ucfirst(mb_strtolower(\CCrmOwnerType::ResolveName($entityTypeId)));
			}
			throw new Main\SystemException(
				"Requisite with ID '$requisiteId' can not be tied to the $entityTypeName ".
					"in which the client is not selected.",
				self::ERR_REQUISITE_TIED_TO_ENTITY_WITHOUT_CLIENT
			);
		}

		$res = $requisite->getList(
			array(
				'filter' => array('=ID' => $requisiteId),
				'select' => array('ID', 'ENTITY_TYPE_ID', 'ENTITY_ID')
			)
		);
		$row = $res->fetch();
		unset($res);
		if (!is_array($row))
		{
			throw new Main\SystemException(
				"The Requisite with ID '$requisiteId' is not found.",
				self::ERR_REQUISITE_NOT_FOUND
			);
		}
		$rqEntityTypeId = isset($row['ENTITY_TYPE_ID']) ? (int)$row['ENTITY_TYPE_ID'] : 0;
		$rqEntityId = isset($row['ENTITY_ID']) ? (int)$row['ENTITY_ID'] : 0;
		$clientEntityTypeId = (int)$clientSellerInfo['CLIENT_ENTITY_TYPE_ID'];
		$clientEntityId = (int)$clientSellerInfo['CLIENT_ENTITY_ID'];
		if ($clientEntityTypeId !== $rqEntityTypeId || $clientEntityId !== $rqEntityId)
		{
			$clientEntityTypeName = ucfirst(mb_strtolower(\CCrmOwnerType::ResolveName($clientEntityTypeId)));
			throw new Main\SystemException(
				"The Requisite with ID '$requisiteId' is not assigned to $clientEntityTypeName ".
					"with ID '$clientEntityId'.",
				self::ERR_REQUISITE_NOT_ASSIGNED
			);
		}
	}

	if ($bankDetailId > 0)
	{
		if ($requisiteId <= 0)
		{
			throw new Main\SystemException(
				"The BankDetail can not be assigned without Requisite.",
				self::ERR_BANK_DETAIL_NOT_ASSIGNED_WO_REQUISITE
			);
		}

		$res = $bankDetail->getList(
			array(
				'filter' => array('=ID' => $bankDetailId),
				'select' => array('ID', 'ENTITY_TYPE_ID', 'ENTITY_ID')
			)
		);
		$row = $res->fetch();
		unset($res);
		if (!is_array($row))
		{
			throw new Main\SystemException(
				"The BankDetail with ID '$bankDetailId' is not found.",
				self::ERR_BANK_DETAIL_NOT_FOUND
			);
		}
		$bdEntityTypeId = isset($row['ENTITY_TYPE_ID']) ? (int)$row['ENTITY_TYPE_ID'] : 0;
		$bdEntityId = isset($row['ENTITY_ID']) ? (int)$row['ENTITY_ID'] : 0;
		if ($bdEntityTypeId !== \CCrmOwnerType::Requisite || $bdEntityId !== $requisiteId)
		{
			throw new Main\SystemException(
				"The BankDetail with ID '$bankDetailId' is not assigned to Requisite with ID '$requisiteId'.",
				self::ERR_BANK_DETAIL_NOT_ASSIGNED
			);
		}
	}

	if ($mcRequisiteId > 0)
	{
		if ($clientSellerInfo['MYCOMPANY_ID'] <= 0
			|| (!$skipIsMyCompanyCheck && $clientSellerInfo['MYCOMPANY_ID'] > 0
				&& !self::isMyCompany($clientSellerInfo['MYCOMPANY_ID'])))
		{
			if ($entityTypeName === null)
			{
				$entityTypeName = ucfirst(mb_strtolower(\CCrmOwnerType::ResolveName($entityTypeId)));
			}
			throw new Main\SystemException(
				"Requisite of your company with ID '$requisiteId' can not be tied to the $entityTypeName ".
				"in which your company is not selected.",
				self::ERR_MC_REQUISITE_TIED_TO_ENTITY_WITHOUT_MYCOMPANY
			);
		}

		$myCompanyId = (int)$clientSellerInfo['MYCOMPANY_ID'];
		$res = $requisite->getList(
			array(
				'filter' => array('=ID' => $mcRequisiteId),
				'select' => array('ID', 'ENTITY_TYPE_ID', 'ENTITY_ID')
			)
		);
		$row = $res->fetch();
		unset($res);
		if (!is_array($row))
		{
			throw new Main\SystemException(
				"The Requisite of your company with ID '$mcRequisiteId' is not found.",
				self::ERR_MC_REQUISITE_NOT_FOUND
			);
		}
		$rqEntityTypeId = isset($row['ENTITY_TYPE_ID']) ? (int)$row['ENTITY_TYPE_ID'] : 0;
		$rqEntityId = isset($row['ENTITY_ID']) ? (int)$row['ENTITY_ID'] : 0;
		if ($rqEntityTypeId !== \CCrmOwnerType::Company || $rqEntityId !== $myCompanyId)
		{
			throw new Main\SystemException(
				"The Requisite with ID '$mcRequisiteId' is not assigned to your company with ID '$myCompanyId'.",
				self::ERR_MC_REQUISITE_NOT_ASSIGNED
			);
		}
	}

	if ($mcBankDetailId > 0)
	{
		if ($mcRequisiteId <= 0)
		{
			throw new Main\SystemException(
				"The BankDetail of your company can not be assigned without Requisite of your company.",
				self::ERR_MC_BANK_DETAIL_NOT_ASSIGNED_WO_MC_REQUISITE
			);
		}

		$res = $bankDetail->getList(
			array(
				'filter' => array('=ID' => $mcBankDetailId),
				'select' => array('ID', 'ENTITY_TYPE_ID', 'ENTITY_ID')
			)
		);
		$row = $res->fetch();
		unset($res);
		if (!is_array($row))
		{
			throw new Main\SystemException(
				"The BankDetail of your company with ID '$mcBankDetailId' is not found.",
				self::ERR_MC_BANK_DETAIL_NOT_FOUND
			);
		}
		$bdEntityTypeId = isset($row['ENTITY_TYPE_ID']) ? (int)$row['ENTITY_TYPE_ID'] : 0;
		$bdEntityId = isset($row['ENTITY_ID']) ? (int)$row['ENTITY_ID'] : 0;
		if ($bdEntityTypeId !== \CCrmOwnerType::Requisite || $bdEntityId !== $mcRequisiteId)
		{
			throw new Main\SystemException(
				"The BankDetail of your company with ID '$mcBankDetailId' is not assigned to ".
					"Requisite of your company with ID '$mcRequisiteId'.",
				self::ERR_MC_BANK_DETAIL_NOT_ASSIGNED
			);
		}
	}
}