• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/requisite/conversion/psrequisiteconverter.php
  • Класс: Bitrix\Crm\Requisite\Conversion\PSRequisiteConverter
  • Вызов: PSRequisiteConverter::saveCompanyRequisite
static function saveCompanyRequisite(&$info)
{
	if (self::$psEntityTreeMap === null)
		self::$psEntityTreeMap = array();

	if (isset($info['ID']) && $info['ID'] > 0 && is_array($info['RQ']) && !empty($info['RQ'])
		&& is_array(self::$convFieldSets) && is_array(self::$convFieldSets['RQ']))
	{
		// prepare hashes of existing requisites
		$rqHashList = array();
		if (isset($info['IS_NEW']) && $info['IS_NEW'] === 'N')
		{
			$requisite = new EntityRequisite();
			$res = $requisite->getList(
				array(
					'order' => array('SORT' => 'ASC', 'ID' => 'ASC'),
					'filter' => array(
						'=ENTITY_TYPE_ID' => \CCrmOwnerType::Company,
						'=ENTITY_ID' => (int)$info['ID']
					),
					'select' => array_merge(
						array('ID', 'PRESET_COUNTRY_ID' => 'PRESET.COUNTRY_ID'),
						$requisite->getRqFields()
					)
				)
			);
			$fieldSets = self::$convFieldSets;
			while ($row = $res->fetch())
			{
				$requisiteId = isset($row['ID']) ? (int)$row['ID'] : 0;
				$presetCountryId = isset($row['PRESET_COUNTRY_ID']) ? (int)$row['PRESET_COUNTRY_ID'] : 0;
				if ($requisiteId > 0 && $presetCountryId > 0)
				{
					$value = isset($row['RQ_COMPANY_NAME']) ? trim(strval($row['RQ_COMPANY_NAME'])) : '';
					$hashData = '|'.$presetCountryId.'|'.$value;
					if (is_array($fieldSets['RQ'][$presetCountryId]))
					{
						foreach (array_keys($fieldSets['RQ'][$presetCountryId]) as $fieldName)
						{
							if ($fieldName === EntityRequisite::ADDRESS)
							{
								$value = '';
								$addrValue = RequisiteAddress::getByOwner(
									RequisiteAddress::Registered,
									\CCrmOwnerType::Requisite,
									$requisiteId
								);
								if (is_array($addrValue) && isset($addrValue['ADDRESS_1']))
									$value = trim(strval($addrValue['ADDRESS_1']));
							}
							else
							{
								$value = isset($row[$fieldName]) ? trim(strval($row[$fieldName])) : '';
							}
							$hashData .= '|'.$value;
						}
					}
					if (!is_array($rqHashList[$presetCountryId]))
						$rqHashList[$presetCountryId] = array();
					$rqHashList[$presetCountryId][md5($hashData)] = $requisiteId;
				}
			}
			unset($res, $row, $hashData, $value, $requisiteId, $presetCountryId);
		}

		// add requisites
		foreach ($info['RQ'] as $presetCountryId => &$rqList)
		{
			foreach ($rqList as $rqHash => &$rqInfo)
			{
				$rqInfo['CO_ID'] = (int)$info['ID'];
				$rqInfo['PRESET_COUNTRY_ID'] = $presetCountryId;
				$requisiteId = isset($rqHashList[$presetCountryId][$rqHash]) ?
					(int)$rqHashList[$presetCountryId][$rqHash] : 0;
				if ($requisiteId > 0)
				{
					$rqInfo['IS_NEW'] = 'N';
				}
				else
				{
					$requisiteId = self::addRequisite($rqInfo);
					$rqInfo['IS_NEW'] = 'Y';
				}
				$rqInfo['ID'] = $requisiteId;
				if ($requisiteId > 0)
				{
					if (is_array($rqInfo['PS']))
					{
						foreach ($rqInfo['PS'] as $psId)
						{
							self::$psEntityTreeMap[$psId] = array(
								'CO' => $rqInfo['CO_ID'],
								'RQ' => $requisiteId,
								'BD' => 0
							);
						}
					}
					self::saveRequisiteBankDetail($rqInfo);
				}
			}
			unset($rqInfo);
		}
		unset($rqList);
	}
}