private function SaveOrderDataProducts($arOrder, $dealId)
 {
     if (!isset($arOrder["ITEMS"]) || !is_array($arOrder["ITEMS"])) {
         return false;
     }
     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");
             }
             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);
         }
     }
     CCrmProductRow::SaveRows("D", $dealId, $arProductRows, null, false, false);
     return true;
 }