Provider::onBeforeDocumentSave

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. Provider
  4. onBeforeDocumentSave
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/catalog/contractor/provider.php
  • Класс: Bitrix\Crm\Integration\Catalog\Contractor\Provider
  • Вызов: Provider::onBeforeDocumentSave
static function onBeforeDocumentSave(array $fields): Result
{
	$requestClientData = $fields['CLIENT_DATA'] ?? '';

	$createdEntities = [];
	$updatedEntities = [];

	$clientData = null;
	try
	{
		$clientData = Json::decode(
			Encoding::convertEncoding($requestClientData, LANG_CHARSET, 'UTF-8')
		);
	}
	catch (SystemException $e) {}

	$clientData = is_array($clientData) ? $clientData : [];

	$companyId = 0;
	$companyIds = null;
	if (
		isset($clientData['COMPANY_DATA'])
		&& is_array($clientData['COMPANY_DATA'])
	)
	{
		$companyIds = [];
		$companyData = $clientData['COMPANY_DATA'];
		if (!empty($companyData))
		{
			$companyItem = $companyData[0];
			$companyId = isset($companyItem['id']) ? (int)$companyItem['id'] : 0;

			if ($companyId <= 0)
			{
				$companyId = BaseComponent::createEntity(
					CCrmOwnerType::Company,
					$companyItem,
					[
						'userPermissions' => self::getPermissions(),
						'startWorkFlows' => true
					]
				);

				if ($companyId > 0)
				{
					$createdEntities[CCrmOwnerType::Company] = [$companyId];
				}
			}
			elseif (
				$companyItem['title']
				|| (isset($companyItem['multifields']) && is_array($companyItem['multifields']))
			)
			{
				if (!isset($updatedEntities[CCrmOwnerType::Company]))
				{
					$updatedEntities[CCrmOwnerType::Company] = [];
				}
				$updatedEntities[CCrmOwnerType::Company][$companyId] = $companyItem;
			}
		}

		if ($companyId > 0)
		{
			$companyIds[] = $companyId;

			Entity::addLastRecentlyUsedItems(
				'crm.store.document.details',
				'company',
				[
					[
						'ENTITY_TYPE_ID' => CCrmOwnerType::Company,
						'ENTITY_ID' => $companyId,
						'CATEGORY_ID' => self::getCategoryIdByEntityType(CCrmOwnerType::Company),
					]
				]
			);
		}
	}

	$contactIds = null;
	$contactsToCompanyBindings = null;
	if (
		isset($clientData['CONTACT_DATA'])
		&& is_array($clientData['CONTACT_DATA'])
	)
	{
		$contactIds = [];
		$contactData = $clientData['CONTACT_DATA'];

		foreach ($contactData as $contactItem)
		{
			if (!is_array($contactItem))
			{
				continue;
			}

			$contactId = isset($contactItem['id']) ? (int)$contactItem['id'] : 0;
			if ($contactId <= 0)
			{
				$contactId = BaseComponent::createEntity(
					CCrmOwnerType::Contact,
					$contactItem,
					[
						'userPermissions' => self::getPermissions(),
						'startWorkFlows' => true,
					]
				);

				if ($contactId > 0)
				{
					if (!is_array($contactsToCompanyBindings))
					{
						$contactsToCompanyBindings = [];
					}
					$contactsToCompanyBindings[] = $contactId;

					if (!isset($createdEntities[CCrmOwnerType::Contact]))
					{
						$createdEntities[CCrmOwnerType::Contact] = [];
					}
					$createdEntities[CCrmOwnerType::Contact][] = $contactId;
				}
			}
			elseif (
				$contactItem['title']
				|| (isset($contactItem['multifields']) && is_array($contactItem['multifields']))
				|| (isset($contactItem['requisites']) && is_array($contactItem['requisites']))
			)
			{
				if (!isset($updatedEntities[CCrmOwnerType::Contact]))
				{
					$updatedEntities[CCrmOwnerType::Contact] = [];
				}

				$updatedEntities[CCrmOwnerType::Contact][$contactId] = $contactItem;
			}

			if ($contactId > 0)
			{
				$contactIds[] = $contactId;
			}
		}

		if (!empty($contactIds))
		{
			$contactIds = array_unique($contactIds);
		}

		$fields['CONTACT_IDS'] = $contactIds;
		if (!empty($fields['CONTACT_IDS']))
		{
			$contactBindings = [];
			foreach($fields['CONTACT_IDS'] as $contactId)
			{
				$contactBindings[] = [
					'ENTITY_TYPE_ID' => CCrmOwnerType::Contact,
					'ENTITY_ID' => $contactId,
					'CATEGORY_ID' => self::getCategoryIdByEntityType(CCrmOwnerType::Contact),
				];
			}

			Entity::addLastRecentlyUsedItems(
				'crm.store.document.details',
				'contact',
				$contactBindings
			);

		}
	}

	return (new Result())->setData([
		'COMPANY_ID' => $companyId,
		'DOCUMENT_BINDINGS' => [
			CCrmOwnerType::Contact => $contactIds,
			CCrmOwnerType::Company => $companyIds,
		],
		'CONTACT_COMPANY_BINDINGS' => $contactsToCompanyBindings,
		'CREATED_ENTITIES' => $createdEntities,
		'UPDATE_ENTITIES' => $updatedEntities,
	]);
}

Добавить комментарий