CCrmDealRecurringRestProxy::innerAdd

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmDealRecurringRestProxy
  4. innerAdd
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmDealRecurringRestProxy
  • Вызов: CCrmDealRecurringRestProxy::innerAdd
protected function innerAdd(&$fields, &$errors, array $params = null)
{
	$ID = null;
	$dealId = (int)$fields['DEAL_ID'];
	if ($dealId <= 0)
	{
		$errors[] = 'Deal ID is empty.';
		return false;
	}
	if(
		!CCrmDeal::CheckUpdatePermission($dealId, CCrmPerms::GetCurrentUserPermissions())
		|| !CCrmDeal::CheckCreatePermission(CCrmPerms::GetCurrentUserPermissions())
	)
	{
		$errors[] = 'Access denied.';
		return false;
	}

	try
	{
		if (
			$fields['IS_LIMIT'] !== \Bitrix\Crm\Recurring\Entity\Deal::LIMITED_BY_TIMES
			&& $fields['IS_LIMIT'] !== \Bitrix\Crm\Recurring\Entity\Deal::LIMITED_BY_DATE
		)
		{
			$fields['IS_LIMIT'] = \Bitrix\Crm\Recurring\Entity\Deal::NO_LIMITED;
		}
		if (!empty($fields['PARAMS']) && is_array($fields['PARAMS']))
		{
			$fields['PARAMS'] = $this->prepareParams($fields);
		}
		else
		{
			$fields['PARAMS'] = [];
		}
		if(!empty($fields['START_DATE']))
		{
			$fields['START_DATE'] = new \Bitrix\Main\Type\Date($fields['START_DATE']);
		}
		if(!empty($fields['LIMIT_DATE']))
		{
			$fields['LIMIT_DATE'] = new \Bitrix\Main\Type\Date($fields['LIMIT_DATE']);
		}
		$newRecurringFields = $fields;

		$dealRecurringInstance = \Bitrix\Crm\Recurring\Entity\Deal::getInstance();
		$dealFields = \CCrmDeal::GetByID($dealId, false);
		if ($dealFields['IS_RECURRING'] === 'Y')
		{
			$recurringRawSearch = $dealRecurringInstance->getList([
				'filter' => ['DEAL_ID' => $dealId],
				'limit' => 1
			]);
			if ($recurringRawSearch->fetch())
			{
				$errors[] = 'Deal already have had recurring settings.';
				return false;
			}

			$result = $dealRecurringInstance->add($newRecurringFields);
			if ($result->isSuccess())
			{
				$ID = $result->getId();
			}
		}
		else
		{
			unset($newRecurringFields['DEAL_ID']);
			$dealUserType = new \CCrmUserType($GLOBALS['USER_FIELD_MANAGER'], \CCrmDeal::GetUserFieldEntityID());
			$userFields = $dealUserType->GetEntityFields($dealId);
			foreach ($userFields as $key => $ufField)
			{
				$dealFields[$key] = $ufField['VALUE'];
			}
			$result = $dealRecurringInstance->createEntity($dealFields, $newRecurringFields);
			if ($result->isSuccess())
			{
				$data = $result->getData();
				$ID = $data['ID'];
			}
		}

		if (!$result->isSuccess())
		{
			$errors = $result->getErrorMessages();
			return false;
		}

		return $ID;
	}
	catch(Main\SystemException $ex)
	{
		$errors[] = $ex->getMessage();
		return false;
	}
}

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