CCrmExternalSaleImport::SaveOrderDataCompany

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

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

	$companyId = 0;

	$companyXmlId = $arOrder["CONTRACTOR"]["ID"];
	if (isset($arOrder["CONTRACTOR"]["INN"]) && $arOrder["CONTRACTOR"]["INN"] != "")
		$companyXmlId .= "|".$arOrder["CONTRACTOR"]["INN"];

	$dbCompany = CCrmCompany::GetList(array(), array("ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => $companyXmlId, "CHECK_PERMISSIONS" => "N"));
	if ($arCompany = $dbCompany->Fetch())
		$companyId = $arCompany["ID"];

	$arFields = array(
		'ORIGINATOR_ID' => $this->externalSaleId,
		'ORIGIN_ID' => $companyXmlId,
		'COMPANY_TYPE' => 'CUSTOMER',
	);

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

	if (is_array($arOrder["CONTRACTOR"]["ADDRESS"]))
	{
		foreach ($arOrder["CONTRACTOR"]["ADDRESS"] as $key => $val)
		{
			if ($key == "VIEW")
				continue;
			if (!empty($arFields["ADDRESS"]))
				$arFields["ADDRESS"] .= ", ";
			$arFields["ADDRESS"] .= $val;
		}
		if (isset($arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"]))
		{
			if (!empty($arFields["ADDRESS"]))
				$arFields["ADDRESS"] .= "\n";
			$arFields["ADDRESS"] .= $arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"];
		}
	}
	if (is_array($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"]))
	{
		foreach ($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"] as $key => $val)
		{
			if ($key == "VIEW")
				continue;
			if (!empty($arFields["ADDRESS_LEGAL"]))
				$arFields["ADDRESS_LEGAL"] .= ", ";
			$arFields["ADDRESS_LEGAL"] .= $val;
		}
		if (isset($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"]["VIEW"]))
		{
			if (!empty($arFields["ADDRESS_LEGAL"]))
				$arFields["ADDRESS_LEGAL"] .= "\n";
			$arFields["ADDRESS_LEGAL"] .= $arOrder["CONTRACTOR"]["LEGAL_ADDRESS"]["VIEW"];
		}
	}
	if (is_array($arOrder["CONTRACTOR"]["CONTACTS"]))
	{
		$arFields["FM"] = array();
		if ($companyId > 0)
		{
			$dbCrmFieldMulti = CCrmFieldMulti::GetList(array(), array('ENTITY_ID' => 'COMPANY', 'ELEMENT_ID' => $companyId, "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"]);
			}
		}
	}

	$arMapTmp = array("INN", "KPP", "EGRPO", "OKVED", "OKDP", "OKOPF", "OKFC", "OKPO");
	foreach ($arMapTmp as $m)
	{
		if (isset($arOrder["CONTRACTOR"][$m]))
			$arFields["BANKING_DETAILS"] .= $m.": ".$arOrder["CONTRACTOR"][$m]."\n";
	}
	if (is_array($arOrder["CONTRACTOR"]["BANK_ADDRESS"]))
	{
		foreach ($arOrder["CONTRACTOR"]["BANK_ADDRESS"] as $key => $val)
		{
			if (!empty($arFields["BANKING_DETAILS"]))
				$arFields["BANKING_DETAILS"] .= ", ";
			$arFields["BANKING_DETAILS"] .= $val;
		}
	}

	$newCompany = ($companyId == 0);

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

	$obj = new CCrmCompany(false);
	if ($companyId == 0)
	{
		if (!isset($arFields['TITLE']) || ($arFields['TITLE'] == ''))
			$arFields['TITLE'] = $companyXmlId;

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

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

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

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

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

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

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

	self::AddTrace("SAVE_ORDER_COMPANY_DATA:FINISHED:{$companyId}");
	return array($companyId, $newCompany);
}

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