CCrmExternalSaleImport::SaveOrderDataProducts

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

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

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

		$this->catalogId = CCrmCatalog::GetCatalogId($this->arExternalSale["NAME"], $this->externalSaleId, SITE_ID);
		if (!$this->catalogId)
		{
			if (($ex = $GLOBALS["APPLICATION"]->GetException()) !== false)
				$this->AddError($ex->GetID(), $ex->GetString());
			else
				$this->AddError("CCA", "Catalog creation error");

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

	$arProductRows = array();

	foreach ($arOrder["ITEMS"] as $arItem)
	{
		$productId = 0;
		$dbProduct = CCrmProduct::GetList(array(), array("CATALOG_ID" => $this->catalogId, "ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => $arItem["ID"], "CHECK_PERMISSIONS" => "N"), array('ID'), array('nTopCount' => 1));
		if ($arProduct = $dbProduct->Fetch())
			$productId = $arProduct["ID"];

		$arFields = array(
			'NAME' => $arItem["NAME"],
			'ACTIVE' => "Y",
			'CATALOG_ID' => $this->catalogId,
			'PRICE' => $arItem["PRICE"],
			'CURRENCY_ID' => $arOrder["CURRENCY"],
			'ORIGINATOR_ID' => $this->externalSaleId,
			'ORIGIN_ID' => $arItem["ID"],
		);

		if ($productId == 0)
		{
			$res = CCrmProduct::Add($arFields);
			if($res > 0)
			{
				$productId = (int)$res;
			}
		}
		else
		{
			$res = CCrmProduct::Update($productId, $arFields);
		}

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

			continue;
		}

		$arProductRows[] = array(
			'PRODUCT_ID' => $productId,
			'PRICE' => $arItem["PRICE"],
			'QUANTITY' => $arItem["QUANTITY"],
		);
	}

	if (is_array($arOrder["TAXES"]))
	{
		foreach ($arOrder["TAXES"] as $arItem)
		{
			if (intval($arItem["IN_PRICE"]) > 0)
				continue;

			$productId = 0;
			$dbProduct = CCrmProduct::GetList(array(), array("CATALOG_ID" => $this->catalogId, "ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => "tax_".$arItem["NAME"], "CHECK_PERMISSIONS" => "N"), array('ID'), array('nTopCount' => 1));
			if ($arProduct = $dbProduct->Fetch())
				$productId = $arProduct["ID"];

			$arFields = array(
				'NAME' => $arItem["NAME"],
				'ACTIVE' => "Y",
				'CATALOG_ID' => $this->catalogId,
				'PRICE' => $arItem["PRICE"],
				'CURRENCY_ID' => $arOrder["CURRENCY"],
				'ORIGINATOR_ID' => $this->externalSaleId,
				'ORIGIN_ID' => "tax_".$arItem["NAME"],
			);

			if ($productId == 0)
			{
				$res = CCrmProduct::Add($arFields);
				$productId = intval($res);
			}
			else
			{
				$res = CCrmProduct::Update($productId, $arFields);
			}

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

				continue;
			}

			$arProductRows[] = array(
				'PRODUCT_ID' => $productId,
				'PRICE' => $arItem["PRICE"],
				'QUANTITY' => 1,
			);
		}
	}

	if (is_array($arOrder["DISCOUNTS"]))
	{
		foreach ($arOrder["DISCOUNTS"] as $arItem)
		{
			if (intval($arItem["IN_PRICE"]) > 0)
				continue;

			$productId = 0;
			$dbProduct = CCrmProduct::GetList(array(), array("CATALOG_ID" => $this->catalogId, "ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => "discount_".$arItem["NAME"], "CHECK_PERMISSIONS" => "N"), array('ID'), array('nTopCount' => 1));
			if ($arProduct = $dbProduct->Fetch())
				$productId = $arProduct["ID"];

			$arFields = array(
				'NAME' => $arItem["NAME"],
				'ACTIVE' => "Y",
				'CATALOG_ID' => $this->catalogId,
				'PRICE' => $arItem["PRICE"],
				'CURRENCY_ID' => $arOrder["CURRENCY"],
				'ORIGINATOR_ID' => $this->externalSaleId,
				'ORIGIN_ID' => "discount_".$arItem["NAME"],
			);

			if ($productId == 0)
			{
				$res = CCrmProduct::Add($arFields);
				$productId = intval($res);
			}
			else
			{
				$res = CCrmProduct::Update($productId, $arFields);
			}

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

				continue;
			}

			$arProductRows[] = array(
				'PRODUCT_ID' => $productId,
				'PRICE' => -$arItem["PRICE"],
				'QUANTITY' => 1,
			);
		}
	}

	self::AddTrace(array("SAVE DEAL PRODUCTS: {$dealId}" => $arProductRows));
	CCrmProductRow::SaveRows("D", $dealId, $arProductRows, null, false, false);
	self::AddTrace("SAVE_ORDER_PRODUCT_DATA:FINISHED");
	return true;
}

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