• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/fuser.php
  • Класс: BitrixSaleFuser
  • Вызов: Fuser::update
static function update(int $id, array $options = []): Result
{
	$result = new Result();

	if ($id <= 0)
	{
		return $result;
	}

	$options['update'] ??= true;
	if (!is_bool($options['update']))
	{
		$options['update'] = true;
	}
	$options['save'] ??= false;
	if (!is_bool($options['save']))
	{
		$options['save'] = false;
	}

	$fuser = InternalsFuserTable::getRow([
		'select' => [
			'ID',
			'USER_ID',
			'CODE'
		],
		'filter' => [
			'=ID' => $id,
		]
	]);

	$databaseUpdate = $options['update'] && $fuser !== null;
	$encodeCookie = static::isEncodeCookie();

	$userCode = trim((string)($fuser['CODE'] ?? null));
	$currentUserId = self::getCurrentUserId();

	if ($databaseUpdate)
	{
		$fields = [
			'DATE_UPDATE' => new MainTypeDateTime(),
		];
		if ($currentUserId !== null)
		{
			$userId = (int)$fuser['USER_ID'];
			if ($userId === 0 || $userId === $currentUserId)
			{
				$fields['USER_ID'] = $currentUserId;
			}
		}
		if ($encodeCookie && $userCode === '')
		{
			$userCode = static::generateCode();
			$fields['CODE'] = $userCode;
		}

		/** @var ORMDataUpdateResult $internalResult */
		$internalResult = static::save($id, $fields);
		if (!$internalResult->isSuccess())
		{
			$result->addErrors($internalResult->getErrors());

			return $result;
		}
	}
	else
	{
		if ($encodeCookie && $userCode === '')
		{
			$userCode = static::generateCode();
		}
	}

	if ($options['save'] && $currentUserId !== null)
	{
		$cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
		static::setIdToCookie($cookieValue);
	}
	static::setIdToSession($id);

	return $result;
}