CAllCrmQuote::rewriteClientFieldsFromRequisite

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmQuote
  4. rewriteClientFieldsFromRequisite
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_quote.php
  • Класс: \CAllCrmQuote
  • Вызов: CAllCrmQuote::rewriteClientFieldsFromRequisite
static function rewriteClientFieldsFromRequisite(&$fields, $requisiteId, $dualFields = true)
{
	$companyId = isset($fields['COMPANY_ID']) ? intval($fields['COMPANY_ID']) : 0;
	$contactId = isset($fields['CONTACT_ID']) ? intval($fields['CONTACT_ID']) : 0;

	$personTypeContact = 1;
	$personTypeCompany = 2;

	$personTypeId = 0;
	if ($companyId > 0)
		$personTypeId = $personTypeCompany;
	else if ($contactId > 0)
		$personTypeId = $personTypeContact;

	$requisiteId = (int)$requisiteId;

	if ($requisiteId > 0 && ($personTypeId == $personTypeContact || $personTypeId == $personTypeCompany))
	{
		// requisite values
		$requisiteValues = array();
		$requisite = new \Bitrix\Crm\EntityRequisite();
		$preset = new \Bitrix\Crm\EntityPreset();
		$row = $requisite->getById($requisiteId);
		if (is_array($row))
		{
			if (isset($row['PRESET_ID']) && $row['PRESET_ID'] > 0)
			{
				$presetFields = array();
				$res = $preset->getList(array(
					'order' => array('SORT' => 'ASC', 'ID' => 'ASC'),
					'filter' => array(
						'=ENTITY_TYPE_ID' => \Bitrix\Crm\EntityPreset::Requisite,
						'=ID' => (int)$row['PRESET_ID']
					),
					'select' => array('ID', 'COUNTRY_ID', 'SETTINGS'),
					'limit' => 1
				));
				if ($presetData = $res->fetch())
				{
					if (is_array($presetData['SETTINGS']))
					{
						$presetFieldsInfo = $preset->settingsGetFields($presetData['SETTINGS']);
						foreach ($presetFieldsInfo as $fieldInfo)
						{
							if (isset($fieldInfo['FIELD_NAME']))
								$presetFields[$fieldInfo['FIELD_NAME']] = true;
						}
						unset($presetFieldsInfo, $fieldInfo);
					}
				}
				unset($res, $presetData);

				foreach ($row as $fieldName => $fieldValue)
				{
					if (isset($presetFields[$fieldName]))
					{
						$requisiteValues[$fieldName] = $fieldValue;
					}
				}
				unset($fieldName, $fieldValue, $valueKey);

				// addresses
				foreach ($requisite->getAddresses($requisiteId) as $addrTypeId => $addrFields)
				{
					$valueKey = Bitrix\Crm\EntityRequisite::ADDRESS.'_'.$addrTypeId;
					$requisiteValues[$valueKey] =
						AddressFormatter::getSingleInstance()->formatTextComma($addrFields);
				}
			}
		}

		// full name
		$fullName = isset($requisiteValues['RQ_NAME']) ?
			trim(strval($requisiteValues['RQ_NAME'])) : '';
		if (empty($fullName))
		{
			$firstName = isset($requisiteValues['RQ_FIRST_NAME'])
				? trim(strval($requisiteValues['RQ_FIRST_NAME'])) : '';
			$lastName = isset($requisiteValues['RQ_LAST_NAME']) ?
				trim(strval($requisiteValues['RQ_LAST_NAME'])) : '';
			$secondName = isset($requisiteValues['RQ_SECOND_NAME']) ?
				trim(strval($requisiteValues['RQ_SECOND_NAME'])) : '';
			if (!empty($firstName) || !empty($lastName) || !empty($secondName))
			{
				$fullName = CUser::FormatName(
					\Bitrix\Crm\Format\PersonNameFormatter::getFormat(),
					array(
						'LOGIN' => '[]',
						'NAME' => $firstName,
						'LAST_NAME' => $lastName,
						'SECOND_NAME' => $secondName
					),
					true, false
				);
				if (!empty($fullName) && $fullName !== '[]')
				{
					$requisiteValues['RQ_NAME'] = $fullName;
				}
			}
		}

		$requisiteToClientFieldsMap = array(
			$personTypeCompany => array(
				'RQ_COMPANY_NAME' => 'CLIENT_TITLE',
				'RQ_ADDR_'.EntityAddressType::Registered => 'CLIENT_ADDR',
				'RQ_INN' => 'CLIENT_TP_ID',
				'RQ_KPP' => 'CLIENT_TPA_ID',
				'RQ_CONTACT' => 'CLIENT_CONTACT',
				'RQ_EMAIL' => 'CLIENT_EMAIL',
				'RQ_PHONE' => 'CLIENT_PHONE'
			),
			$personTypeContact => array(
				'RQ_NAME' => 'CLIENT_TITLE',
				'RQ_EMAIL' => 'CLIENT_EMAIL',
				'RQ_PHONE' => 'CLIENT_PHONE',
				'RQ_ADDR_'.EntityAddressType::Primary => 'CLIENT_ADDR'
			),
		);

		if (is_array($requisiteValues) && !empty($requisiteValues))
		{
			foreach ($requisiteToClientFieldsMap[$personTypeId] as $rqIndex => $clientFields)
			{
				if (isset($requisiteValues[$rqIndex])
					&& !empty($requisiteValues[$rqIndex])
				)
				{
					if (!is_array($clientFields))
						$clientFields = array($clientFields);
					foreach ($clientFields as $clientFieldName)
					{
						if ($dualFields)
						{
							$fields['~'.$clientFieldName] = $requisiteValues[$rqIndex];
							$fields[$clientFieldName] = htmlspecialcharsbx($requisiteValues[$rqIndex]);
						}
						else
						{
							$fields[$clientFieldName] = $requisiteValues[$rqIndex];
						}
					}
				}
			}
		}
	}
}

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