CCrmExternalSaleImport::SaveOrderDataContact

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmExternalSaleImport
  4. SaveOrderDataContact
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_external_sale_import.php
  • Класс: \CCrmExternalSaleImport
  • Вызов: CCrmExternalSaleImport::SaveOrderDataContact
private function SaveOrderDataContact($arOrder)
{
	if (!isset($arOrder["CONTRACTOR"]) || !is_array($arOrder["CONTRACTOR"]))
		return false;

	self::AddTrace('SAVE_ORDER_CONTACT_DATA:START');

	$contactId = 0;

	$contactXmlId = $arOrder["CONTRACTOR"]["ID"];
	if (isset($arOrder["CONTRACTOR"]["FIRST_NAME"]) && $arOrder["CONTRACTOR"]["FIRST_NAME"] != "")
		$contactXmlId .= "|".$arOrder["CONTRACTOR"]["FIRST_NAME"];
	if (isset($arOrder["CONTRACTOR"]["LAST_NAME"]) && $arOrder["CONTRACTOR"]["LAST_NAME"] != "")
		$contactXmlId .= "|".$arOrder["CONTRACTOR"]["LAST_NAME"];

	$dbContact = CCrmContact::GetListEx(
		array(),
		array("ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => $contactXmlId, "CHECK_PERMISSIONS" => "N")
	);

	if ($arContact = $dbContact->Fetch())
		$contactId = $arContact["ID"];

	$arFields = array(
		'ORIGINATOR_ID' => $this->externalSaleId,
		'ORIGIN_ID' => $contactXmlId,
		'TYPE_ID' => 'CLIENT',
		'OPENED' => 'Y'
	);

	$sources = CCrmStatus::GetStatusList('SOURCE');
	if(isset($sources['WEB']))
	{
		$arFields['SOURCE_ID'] = 'WEB';
	}

	if (isset($arOrder["CONTRACTOR"]["FIRST_NAME"]) && $arOrder["CONTRACTOR"]["FIRST_NAME"] != "")
		$arFields['NAME'] = $arOrder["CONTRACTOR"]["FIRST_NAME"];
	if (isset($arOrder["CONTRACTOR"]["LAST_NAME"]) && $arOrder["CONTRACTOR"]["LAST_NAME"] != "")
		$arFields['LAST_NAME'] = $arOrder["CONTRACTOR"]["LAST_NAME"];
	if (isset($arOrder["CONTRACTOR"]["SECOND_NAME"]) && $arOrder["CONTRACTOR"]["SECOND_NAME"] != "")
		$arFields['SECOND_NAME'] = $arOrder["CONTRACTOR"]["SECOND_NAME"];
	if (isset($arOrder["CONTRACTOR"]["BIRTHDAY"]) && $arOrder["CONTRACTOR"]["BIRTHDAY"] != "")
		$arFields['BIRTHDATE'] = $arOrder["CONTRACTOR"]["BIRTHDAY"];

	if (isset($arOrder["CONTRACTOR"]["FULL_NAME"]) && $arOrder["CONTRACTOR"]["FULL_NAME"] != "")
		$arFields['FULL_NAME'] = $arOrder["CONTRACTOR"]["FULL_NAME"];
	elseif (isset($arOrder["CONTRACTOR"]["NAME"]) && $arOrder["CONTRACTOR"]["NAME"] != "")
		$arFields['FULL_NAME'] = $arOrder["CONTRACTOR"]["NAME"];

	if (is_array($arOrder["CONTRACTOR"]["ADDRESS"]))
	{
		if (isset($arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"]) && $arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"] != '')
		{
			if (!empty($arFields["ADDRESS"]))
				$arFields["ADDRESS"] .= "\n";
			$arFields["ADDRESS"] .= $arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"];
		}
		else
		{
			foreach ($arOrder["CONTRACTOR"]["ADDRESS"] as $key => $val)
			{
				if ($key == "VIEW")
				{
					continue;
				}
				if (!empty($arFields["ADDRESS"]))
				{
					$arFields["ADDRESS"] .= ", ";
				}
				$arFields["ADDRESS"] .= $val;
			}
		}
	}
	if (is_array($arOrder["CONTRACTOR"]["CONTACTS"]))
	{
		$arFields["FM"] = array();
		if ($contactId > 0)
		{
			$dbCrmFieldMulti = CCrmFieldMulti::GetList(array(), array('ENTITY_ID' => 'CONTACT', 'ELEMENT_ID' => $contactId, "CHECK_PERMISSIONS" => "N"));
			while ($arCrmFieldMulti = $dbCrmFieldMulti->Fetch())
				$arFields["FM"][$arCrmFieldMulti["TYPE_ID"]][$arCrmFieldMulti["ID"]] = array("VALUE_TYPE" => $arCrmFieldMulti["VALUE_TYPE"], "VALUE" => $arCrmFieldMulti["VALUE"]);
		}

		$arMapTmp = array(
			"MAIL" => "EMAIL", "E-MAIL" => "EMAIL", "WORKPHONE" => "PHONE"
		);
		$arInc = array();
		foreach ($arOrder["CONTRACTOR"]["CONTACTS"] as $val)
		{
			$t = mb_strtoupper(preg_replace("/\s/", "", $val["TYPE"]));
			if (!isset($arMapTmp[$t]))
			{
				continue;
			}

			$bFound = false;
			$tNew = $arMapTmp[$t];
			if (isset($arFields["FM"][$tNew]) && is_array($arFields["FM"][$tNew]))
			{
				if(count($arFields["FM"][$tNew]) >= 50)
				{
					//Disable adding new communication after threshold is exceeded
					$bFound = true;
				}
				else
				{
					foreach ($arFields["FM"][$tNew] as $k => $v)
					{
						if ($v["VALUE"] == $val["VALUE"])
						{
							$bFound = true;
							break;
						}
					}
				}
			}
			if (!$bFound)
			{
				$arInc[$tNew]++;
				$arFields["FM"][$tNew]["n".$arInc[$tNew]] = array("VALUE_TYPE" => "WORK", "VALUE" => $val["VALUE"]);
			}
		}
	}

	$newContact = ($contactId == 0);

	if ($this->arExternalSale == null)
		$this->arExternalSale = CCrmExternalSale::GetDefaultSettings($this->externalSaleId);

	$obj = new CCrmContact(false);
	if ($contactId == 0)
	{
		if (
			(!isset($arFields['NAME']) || ($arFields['NAME'] == ''))
			&& (!isset($arFields['LAST_NAME']) || ($arFields['LAST_NAME'] == ''))
		)
			$arFields['LAST_NAME'] = $contactXmlId;

		$assignedById = $this->arExternalSale["RESPONSIBLE"];
		if ($assignedById > 0)
			$arFields["ASSIGNED_BY_ID"] = $assignedById;

		self::AddTrace(array('ADD CONTACT' => $arFields));

		$res = $obj->Add($arFields, true, array('DISABLE_USER_FIELD_CHECK' => true));
		if($res > 0)
		{
			$contactId = (int)$res;
			$this->arImportResult->numberOfCreatedContacts++;
		}
	}
	else
	{
		self::AddTrace(array("UPDATE CONTACT: {$contactId}" => $arFields));

		$res = $obj->Update($contactId, $arFields, true, true, array('DISABLE_USER_FIELD_CHECK' => true));
		if($res)
		{
			$this->arImportResult->numberOfUpdatedContacts++;
		}
	}

	if (!$res)
	{
		if (($ex = $GLOBALS["APPLICATION"]->GetException()) !== false)
			$this->AddError($ex->GetID(), $ex->GetString());
		else
			$this->AddError("CCA", "Contact creation error");

		if (!empty($obj->LAST_ERROR))
			$this->AddError("CCA", $obj->LAST_ERROR);

		self::AddTrace('SAVE_ORDER_CONTACT_DATA:FAILED');
		return false;
	}

	self::AddTrace("SAVE_ORDER_CONTACT_DATA:FINISHED:{$contactId}");
	return array($contactId, $newContact);
}

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