CCrmExternalSaleImport::SaveOrderDataDeal

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmExternalSaleImport
  4. SaveOrderDataDeal
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_external_sale_import.php
  • Класс: \CCrmExternalSaleImport
  • Вызов: CCrmExternalSaleImport::SaveOrderDataDeal
private function SaveOrderDataDeal($arOrder, $contactId = null, $companyId = null)
{
	self::AddTrace('SAVE_ORDER_DEAL_DATA:START');

	$dealId = 0;
	$dealTitle = "";
	$dealCategoryId = 0;

	$dbDeal = CCrmDeal::GetListEx(
		array(),
		array(
			"ORIGINATOR_ID" => $this->externalSaleId,
			"ORIGIN_ID" => $arOrder["ID"],
			"CHECK_PERMISSIONS" => "N"
		)
	);
	if ($arDeal = $dbDeal->Fetch())
	{
		$dealId = (int)$arDeal["ID"];
		if(isset($arDeal["TITLE"]))
		{
			$dealTitle = $arDeal["TITLE"];
		}
		if(isset($arDeal["CATEGORY_ID"]))
		{
			$dealCategoryId = (int)$arDeal["CATEGORY_ID"];
		}
	}

	$newDeal = ($dealId == 0);

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

	$arFields = array(
		'ORIGINATOR_ID' => $this->externalSaleId,
		'ORIGIN_ID' => $arOrder["ID"],
		'BEGINDATE' => $arOrder["DATE_INSERT"],
		'CURRENCY_ID' => $arOrder["CURRENCY"],
		'EXCH_RATE' => $arOrder["CURRENCY_RATE"],
		'OPPORTUNITY' => $arOrder["PRICE"]
	);

	// Prevent reset comment if order comment is empty
	if(isset($arOrder["COMMENT"]) && $arOrder["COMMENT"] !== "")
	{
		$arFields["COMMENTS"] = $arOrder["COMMENT"];
	}

	if ($contactId != null && intval($contactId) > 0)
		$arFields["CONTACT_ID"] = $contactId;
	if ($companyId != null && intval($companyId) > 0)
		$arFields["COMPANY_ID"] = $companyId;

	// Prevent reset stage for existed deals
	if ($newDeal)
	{
		$arStageList = Bitrix\Crm\Category\DealCategory::getStageList($dealCategoryId);
		if(!empty($arStageList))
		{
			$arFields["STAGE_ID"] = current(array_keys($arStageList));
		}
	}

	$arAdditionalInfo = array();
	if ($contactId != null && intval($contactId) > 0)
	{
		if (isset($arOrder["CONTRACTOR"]["FULL_NAME"]) && $arOrder["CONTRACTOR"]["FULL_NAME"] != "")
			$arAdditionalInfo['CONTACT_FULL_NAME'] = $arOrder["CONTRACTOR"]["FULL_NAME"];
		elseif (isset($arOrder["CONTRACTOR"]["NAME"]) && $arOrder["CONTRACTOR"]["NAME"] != "")
			$arAdditionalInfo['CONTACT_FULL_NAME'] = $arOrder["CONTRACTOR"]["NAME"];
	}
	if ($companyId != null && intval($companyId) > 0)
	{
		if (isset($arOrder["CONTRACTOR"]["OFFICIAL_NAME"]) && $arOrder["CONTRACTOR"]["OFFICIAL_NAME"] != "")
			$arAdditionalInfo['COMPANY_FULL_NAME'] = $arOrder["CONTRACTOR"]["OFFICIAL_NAME"];
		elseif (isset($arOrder["CONTRACTOR"]["NAME"]) && $arOrder["CONTRACTOR"]["NAME"] != "")
			$arAdditionalInfo['COMPANY_FULL_NAME'] = $arOrder["CONTRACTOR"]["NAME"];
	}


	if (is_array($arOrder["PROPERTIES"]))
	{
		foreach ($arOrder["PROPERTIES"] as $arProp)
		{
			if (!empty($arProp["VALUE"]))
			{
				$arAdditionalInfo[mb_strtoupper($arProp["NAME"])] = $arProp["VALUE"];
				if ($arAdditionalInfo[mb_strtoupper($arProp["NAME"])] == "true")
					$arAdditionalInfo[mb_strtoupper($arProp["NAME"])] = true;
				elseif ($arAdditionalInfo[mb_strtoupper($arProp["NAME"])] == "false")
					$arAdditionalInfo[mb_strtoupper($arProp["NAME"])] = false;
			}

			switch(mb_strtoupper($arProp["NAME"]))
			{
				case 'CANCELED':
					if($arProp["VALUE"] == 'true')
					{
						$arFields["STAGE_ID"] = \Bitrix\Crm\Category\DealCategory::prepareStageID(
							$dealCategoryId,
							"LOSE"
						);
						$arFields["PROBABILITY"] = 0;
					}
					break;
				case 'ORDERPAID':
					if($arProp["VALUE"] == 'true')
					{
						$arFields["STAGE_ID"] = \Bitrix\Crm\Category\DealCategory::prepareStageID(
							$dealCategoryId,
							"WON"
						);
						$arFields["PROBABILITY"] = 100;
					}
					break;
			}
		}
	}

	$arFields["ADDITIONAL_INFO"] = serialize($arAdditionalInfo);

	$accountNumber = isset($arOrder["ACCOUNT_NUMBER"]) && $arOrder["ACCOUNT_NUMBER"] !== ''
		? $arOrder["ACCOUNT_NUMBER"] : $arOrder["ID"];

	$assignedById = isset($this->arExternalSale["RESPONSIBLE"]) ? (int)$this->arExternalSale["RESPONSIBLE"] : 0;
	if ($assignedById <= 0)
	{
		$assignedById = 1;
	}

	$obj = new CCrmDeal(false);
	if ($dealId == 0)
	{
		$arFields['TITLE'] = sprintf("%s #%s", $this->arExternalSale["PREFIX"], $accountNumber);
		$arFields['OPENED'] = $this->arExternalSale["PUBLIC"];
		$arFields["TYPE_ID"] = 'SALE';
		$arFields["CLOSEDATE"] = ConvertTimeStamp(time() + CTimeZone::GetOffset() + 86400, "FULL");
		if (!isset($arFields["PROBABILITY"]))
		{
			$arFields["PROBABILITY"] = $this->arExternalSale["PROBABILITY"];
		}
		$arFields["ASSIGNED_BY_ID"] = $assignedById;

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

		$res = $obj->Add(
			$arFields,
			true,
			array(
				'DISABLE_USER_FIELD_CHECK' => true,
				'CURRENT_USER' => $assignedById
			)
		);
		if($res > 0)
		{
			$dealId = (int)$res;
			$this->arImportResult->numberOfCreatedDeals++;
		}
	}
	else
	{
		$defaultDealTitle = isset($this->params['DEFAULT_DEAL_TITLE'])
			? $this->params['DEFAULT_DEAL_TITLE'] : 'Deal';

		if ($dealTitle === '' || $dealTitle === $defaultDealTitle)
		{
			$arFields['TITLE'] = sprintf("%s #%s", $this->arExternalSale["PREFIX"], $accountNumber);
		}

		self::AddTrace(array("UPDATE DEAL: {$dealId}" => $arFields));

		$res = $obj->Update(
			$dealId,
			$arFields,
			true,
			true,
			array(
				'DISABLE_USER_FIELD_CHECK' => true,
				'CURRENT_USER' => $assignedById
			)
		);
		if($res)
		{
			$this->arImportResult->numberOfUpdatedDeals++;
		}
	}

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

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

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

	self::AddTrace("SAVE_ORDER_DEAL_DATA:FINISHED:{$dealId}");
	return array($dealId, $newDeal);
}

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