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; }
public static function OnAccountCurrencyChange() { $accountCurrencyID = CCrmCurrency::GetAccountCurrencyID(); if (!isset($accountCurrencyID[0])) { return; } $rs = self::GetList(array('ID' => 'ASC'), array(), array('ID', 'CURRENCY_ID', 'OPPORTUNITY', 'TAX_VALUE', 'EXCH_RATE')); $entity = new CCrmDeal(false); while ($arParams = $rs->Fetch()) { $ID = intval($arParams['ID']); $entity->Update($ID, $arParams, false, false); $arRows = CCrmProductRow::LoadRows('D', $ID); $context = array(); if (isset($arParams['CURRENCY_ID'])) { $context['CURRENCY_ID'] = $arParams['CURRENCY_ID']; } if (isset($arParams['EXCH_RATE'])) { $context['EXCH_RATE'] = $arParams['EXCH_RATE']; } if (count($arRows) > 0) { CCrmProductRow::SaveRows('D', $ID, $arRows, $context); } } }
public static function SaveProductRows($ID, $arRows, $checkPerms = true, $regEvent = true, $syncOwner = true) { $context = array(); $arParams = self::GetByID($ID); if (is_array($arParams)) { if (isset($arParams['CURRENCY_ID'])) { $context['CURRENCY_ID'] = $arParams['CURRENCY_ID']; } if (isset($arParams['EXCH_RATE'])) { $context['EXCH_RATE'] = $arParams['EXCH_RATE']; } } return CCrmProductRow::SaveRows(self::OWNER_TYPE, $ID, $arRows, $context, $checkPerms, $regEvent, $syncOwner); }
} if (isset($arProduct['TAX_RATE'])) { $fields['TAX_RATE'] = round(doubleval($arProduct['TAX_RATE']), 2); } if (isset($arProduct['TAX_INCLUDED'])) { $fields['TAX_INCLUDED'] = strtoupper($arProduct['TAX_INCLUDED']) === 'Y' ? 'Y' : 'N'; } if (isset($arProduct['CUSTOMIZED'])) { $fields['CUSTOMIZED'] = strtoupper($arProduct['CUSTOMIZED']) === 'Y' ? 'Y' : 'N'; } if (isset($arProduct['SORT'])) { $fields['SORT'] = intval($arProduct['SORT']); } $arProductRows[] = $fields; } $bSuccess = CCrmProductRow::SaveRows($ownerType, $ownerID, $arProductRows); if (!$bSuccess) { echo CUtil::PhpToJSObject(array('ERROR' => 'PRODUCT_ROWS_SAVING_ERROR')); } else { echo CUtil::PhpToJSObject(array('SAVE_PRODUCT_ROWS' => 'OK')); } } elseif ($mode === 'REMOVE_PRODUCT') { if ($perms->HavePerm($ownerName, BX_CRM_PERM_NONE, 'WRITE')) { echo CUtil::PhpToJSObject(array('ERROR' => 'PERMISSION_DENIED')); die; } $ID = isset($_POST['ID']) ? intval($_POST['ID']) : 0; if ($ID <= 0) { echo CUtil::PhpToJSObject(array('ERROR' => 'ID_NOT_FOUND')); die; }