function ImportElementPrices($arXMLElement, &$counter) { /** @global CMain $APPLICATION */ global $APPLICATION; static $catalogs = array(); $arElement = array( "ID" => 0, "XML_ID" => $arXMLElement[GetMessage("IBLOCK_XML2_ID")], ); $hashPosition = strrpos($arElement["XML_ID"], "#"); if ( $this->use_offers && $hashPosition === false && isset($this->PROPERTY_MAP["CML2_LINK"]) && isset($this->arProperties[$this->PROPERTY_MAP["CML2_LINK"]]) ) { $IBLOCK_ID = $this->arProperties[$this->PROPERTY_MAP["CML2_LINK"]]["LINK_IBLOCK_ID"]; if (!isset($catalogs[$IBLOCK_ID])) { $catalogs[$IBLOCK_ID] = true; $rs = CCatalog::GetList(array(),array("IBLOCK_ID" => $IBLOCK_ID)); if (!$rs->Fetch()) { $obCatalog = new CCatalog(); $boolFlag = $obCatalog->Add(array( "IBLOCK_ID" => $IBLOCK_ID, "YANDEX_EXPORT" => "N", "SUBSCRIPTION" => "N", )); if (!$boolFlag) { if ($ex = $APPLICATION->GetException()) $this->LAST_ERROR = $ex->GetString(); return 0; } } } } else { $IBLOCK_ID = $this->next_step["IBLOCK_ID"]; } $obElement = new CIBlockElement; $rsElement = $obElement->GetList( Array("ID"=>"asc"), Array("=XML_ID" => $arElement["XML_ID"], "IBLOCK_ID" => $IBLOCK_ID), false, false, Array("ID", "TMP_ID", "ACTIVE") ); if($arDBElement = $rsElement->Fetch()) { $arElement["ID"] = $arDBElement["ID"]; if(isset($arXMLElement[GetMessage("IBLOCK_XML2_PRICES")])) {//Collect price information for future use $arElement["PRICES"] = array(); foreach($arXMLElement[GetMessage("IBLOCK_XML2_PRICES")] as $price) { if( isset($price[GetMessage("IBLOCK_XML2_PRICE_TYPE_ID")]) && array_key_exists($price[GetMessage("IBLOCK_XML2_PRICE_TYPE_ID")], $this->PRICES_MAP) ) { $price["PRICE"] = $this->PRICES_MAP[$price[GetMessage("IBLOCK_XML2_PRICE_TYPE_ID")]]; $arElement["PRICES"][] = $price; } } $arElement["DISCOUNTS"] = array(); if(isset($arXMLElement[GetMessage("IBLOCK_XML2_DISCOUNTS")])) { foreach($arXMLElement[GetMessage("IBLOCK_XML2_DISCOUNTS")] as $discount) { if( isset($discount[GetMessage("IBLOCK_XML2_DISCOUNT_CONDITION")]) && $discount[GetMessage("IBLOCK_XML2_DISCOUNT_CONDITION")] === GetMessage("IBLOCK_XML2_DISCOUNT_COND_VOLUME") ) { $discount_value = $this->ToInt($discount[GetMessage("IBLOCK_XML2_DISCOUNT_COND_VALUE")]); $discount_percent = $this->ToFloat($discount[GetMessage("IBLOCK_XML2_DISCOUNT_COND_PERCENT")]); if($discount_value > 0 && $discount_percent > 0) $arElement["DISCOUNTS"][$discount_value] = $discount_percent; } } } } if(isset($arXMLElement[GetMessage("IBLOCK_XML2_AMOUNT")])) { $arElementTmp = array(); $arElement["QUANTITY_RESERVED"] = 0; if($arElement["ID"]) $arElementTmp = CCatalogProduct::GetById($arElement["ID"]); if(is_array($arElementTmp) && !empty($arElementTmp) && isset($arElementTmp["QUANTITY_RESERVED"])) $arElement["QUANTITY_RESERVED"] = $arElementTmp["QUANTITY_RESERVED"]; $arElement["QUANTITY"] = $this->ToFloat($arXMLElement[GetMessage("IBLOCK_XML2_AMOUNT")]) - doubleval($arElement["QUANTITY_RESERVED"]); } else $arElement["QUANTITY"] = 0; if(isset($arElement["PRICES"]) && $this->bCatalog) { $arProduct = array( "ID" => $arElement["ID"], ); if(isset($arElement["QUANTITY"])) $arProduct["QUANTITY"] = $arElement["QUANTITY"]; //Get weight from element traits $rsWeight = CIBlockElement::GetProperty($IBLOCK_ID, $arElement["ID"], array(), array("CODE" => "CML2_TRAITS")); while($arWeight = $rsWeight->Fetch()) { if($arWeight["DESCRIPTION"] == GetMessage("IBLOCK_XML2_WEIGHT")) $arProduct["WEIGHT"] = $this->ToFloat($arWeight["VALUE"])*1000; } //Here start VAT handling //Check if all the taxes exists in BSM catalog $arTaxMap = array(); $rsTaxProperty = CIBlockElement::GetProperty($IBLOCK_ID, $arElement["ID"], array("sort" => "asc"), array("CODE" => "CML2_TAXES")); while($arTaxProperty = $rsTaxProperty->Fetch()) { if( strlen($arTaxProperty["VALUE"]) > 0 && strlen($arTaxProperty["DESCRIPTION"]) > 0 && !array_key_exists($arTaxProperty["DESCRIPTION"], $arTaxMap) ) { $arTaxMap[$arTaxProperty["DESCRIPTION"]] = array( "RATE" => $this->ToFloat($arTaxProperty["VALUE"]), "ID" => $this->CheckTax($arTaxProperty["DESCRIPTION"], $this->ToFloat($arTaxProperty["VALUE"])), ); } } //First find out if all the prices have TAX_IN_SUM true $TAX_IN_SUM = "Y"; foreach($arElement["PRICES"] as $price) { if($price["PRICE"]["TAX_IN_SUM"] !== "true") { $TAX_IN_SUM = "N"; break; } } //If there was found not included tax we'll make sure //that all prices has the same flag if($TAX_IN_SUM === "N") { foreach($arElement["PRICES"] as $price) { if($price["PRICE"]["TAX_IN_SUM"] !== "false") { $TAX_IN_SUM = "Y"; break; } } //Check if there is a mix of tax in sum //and correct it by recalculating all the prices if($TAX_IN_SUM === "Y") { foreach($arElement["PRICES"] as $key=>$price) { if($price["PRICE"]["TAX_IN_SUM"] !== "true") { $TAX_NAME = $price["PRICE"]["TAX_NAME"]; if(array_key_exists($TAX_NAME, $arTaxMap)) { $PRICE_WO_TAX = $this->ToFloat($price[GetMessage("IBLOCK_XML2_PRICE_FOR_ONE")]); $PRICE = $PRICE_WO_TAX + ($PRICE_WO_TAX / 100.0 * $arTaxMap[$TAX_NAME]["RATE"]); $arElement["PRICES"][$key][GetMessage("IBLOCK_XML2_PRICE_FOR_ONE")] = $PRICE; } } } } } foreach($arElement["PRICES"] as $price) { $TAX_NAME = $price["PRICE"]["TAX_NAME"]; if(array_key_exists($TAX_NAME, $arTaxMap)) { $arProduct["VAT_ID"] = $arTaxMap[$TAX_NAME]["ID"]; break; } } $arProduct["VAT_INCLUDED"] = $TAX_IN_SUM; CCatalogProduct::Add($arProduct); $this->SetProductPrice($arElement["ID"], $arElement["PRICES"], $arElement["DISCOUNTS"]); $counter["UPD"]++; } } if(isset($arXMLElement[GetMessage("IBLOCK_XML2_STORE_AMOUNT_LIST")])) { $arElement["STORE_AMOUNT"]=array(); foreach($arXMLElement[GetMessage("IBLOCK_XML2_STORE_AMOUNT_LIST")] as $storeAmount) { if(isset($storeAmount[GetMessage("IBLOCK_XML2_STORE_ID")])) { $storeXMLID = $storeAmount[GetMessage("IBLOCK_XML2_STORE_ID")]; $amount = $storeAmount[GetMessage("IBLOCK_XML2_AMOUNT")]; $arElement["STORE_AMOUNT"][]=array($storeXMLID => $amount); } } } if(isset($arElement["STORE_AMOUNT"])) $this->ImportStoresAmount($arElement["STORE_AMOUNT"], $arElement["ID"], $counter); return $arElement["ID"]; }
function ImportElementPrices($arXMLElement, &$counter, $arParent = false) { /** @global CMain $APPLICATION */ global $APPLICATION; static $catalogs = array(); $arElement = array("ID" => 0, "XML_ID" => $arXMLElement[$this->mess["IBLOCK_XML2_ID"]]); $hashPosition = strrpos($arElement["XML_ID"], "#"); if ($this->use_offers && $hashPosition === false && !$this->force_offers && isset($this->PROPERTY_MAP["CML2_LINK"]) && isset($this->arProperties[$this->PROPERTY_MAP["CML2_LINK"]])) { $IBLOCK_ID = $this->arProperties[$this->PROPERTY_MAP["CML2_LINK"]]["LINK_IBLOCK_ID"]; if (!isset($catalogs[$IBLOCK_ID])) { $catalogs[$IBLOCK_ID] = true; $rs = CCatalog::GetList(array(), array("IBLOCK_ID" => $IBLOCK_ID)); if (!$rs->Fetch()) { $obCatalog = new CCatalog(); $boolFlag = $obCatalog->Add(array("IBLOCK_ID" => $IBLOCK_ID, "YANDEX_EXPORT" => "N", "SUBSCRIPTION" => "N")); if (!$boolFlag) { if ($ex = $APPLICATION->GetException()) { $this->LAST_ERROR = $ex->GetString(); } return 0; } } } } else { $IBLOCK_ID = $this->next_step["IBLOCK_ID"]; } $obElement = new CIBlockElement(); $rsElement = $obElement->GetList(array("ID" => "asc"), array("=XML_ID" => $arElement["XML_ID"], "IBLOCK_ID" => $IBLOCK_ID), false, false, array("ID", "TMP_ID", "ACTIVE")); $arDBElement = $rsElement->Fetch(); if ($arDBElement) { $arElement["ID"] = $arDBElement["ID"]; } if (isset($arXMLElement[$this->mess["IBLOCK_XML2_STORE_AMOUNT_LIST"]])) { $arElement["STORE_AMOUNT"] = array(); foreach ($arXMLElement[$this->mess["IBLOCK_XML2_STORE_AMOUNT_LIST"]] as $storeAmount) { if (isset($storeAmount[$this->mess["IBLOCK_XML2_STORE_ID"]])) { $storeXMLID = $storeAmount[$this->mess["IBLOCK_XML2_STORE_ID"]]; $amount = $this->ToFloat($storeAmount[$this->mess["IBLOCK_XML2_AMOUNT"]]); $arElement["STORE_AMOUNT"][$storeXMLID] = $amount; } } } elseif (isset($arXMLElement[$this->mess["IBLOCK_XML2_RESTS"]])) { $arElement["STORE_AMOUNT"] = array(); foreach ($arXMLElement[$this->mess["IBLOCK_XML2_RESTS"]] as $xmlRest) { foreach ($xmlRest as $storeAmount) { if (is_array($storeAmount)) { if (isset($storeAmount[$this->mess["IBLOCK_XML2_ID"]])) { $storeXMLID = $storeAmount[$this->mess["IBLOCK_XML2_ID"]]; $amount = $this->ToFloat($storeAmount[$this->mess["IBLOCK_XML2_AMOUNT"]]); $arElement["STORE_AMOUNT"][$storeXMLID] = $amount; } } else { if (strlen($storeAmount) > 0) { $amount = $this->ToFloat($storeAmount); $arElement["QUANTITY"] = $amount; } } } } } elseif ($arParent && (array_key_exists($this->mess["IBLOCK_XML2_STORES"], $arXMLElement) || array_key_exists($this->mess["IBLOCK_XML2_STORE"], $arXMLElement))) { $arElement["STORE_AMOUNT"] = array(); $rsStores = $this->_xml_file->GetList(array("ID" => "asc"), array("><LEFT_MARGIN" => array($arParent["LEFT_MARGIN"], $arParent["RIGHT_MARGIN"]), "NAME" => $this->mess["IBLOCK_XML2_STORE"]), array("ID", "ATTRIBUTES")); while ($arStore = $rsStores->Fetch()) { if (strlen($arStore["ATTRIBUTES"]) > 0) { $info = unserialize($arStore["ATTRIBUTES"]); if (is_array($info) && array_key_exists($this->mess["IBLOCK_XML2_STORE_ID"], $info) && array_key_exists($this->mess["IBLOCK_XML2_STORE_AMOUNT"], $info)) { $arElement["STORE_AMOUNT"][$info[$this->mess["IBLOCK_XML2_STORE_ID"]]] = $this->ToFloat($info[$this->mess["IBLOCK_XML2_STORE_AMOUNT"]]); } } } } if (isset($arElement["STORE_AMOUNT"])) { $this->ImportStoresAmount($arElement["STORE_AMOUNT"], $arElement["ID"], $counter); } if ($arDBElement) { $arProduct = array("ID" => $arElement["ID"]); if (isset($arXMLElement[$this->mess["IBLOCK_XML2_PRICES"]])) { $arElement["PRICES"] = array(); foreach ($arXMLElement[$this->mess["IBLOCK_XML2_PRICES"]] as $price) { if (isset($price[$this->mess["IBLOCK_XML2_PRICE_TYPE_ID"]]) && array_key_exists($price[$this->mess["IBLOCK_XML2_PRICE_TYPE_ID"]], $this->PRICES_MAP)) { $price["PRICE"] = $this->PRICES_MAP[$price[$this->mess["IBLOCK_XML2_PRICE_TYPE_ID"]]]; $arElement["PRICES"][] = $price; if (array_key_exists($this->mess["IBLOCK_XML2_MEASURE"], $price) && !isset($arProduct["MEASURE"])) { $tmp = $this->convertBaseUnitFromXmlToPropertyValue($price[$this->mess["IBLOCK_XML2_MEASURE"]]); if ($tmp["DESCRIPTION"] > 0) { $arProduct["MEASURE"] = $tmp["DESCRIPTION"]; } } } } $arElement["DISCOUNTS"] = array(); if (isset($arXMLElement[$this->mess["IBLOCK_XML2_DISCOUNTS"]])) { foreach ($arXMLElement[$this->mess["IBLOCK_XML2_DISCOUNTS"]] as $discount) { if (isset($discount[$this->mess["IBLOCK_XML2_DISCOUNT_CONDITION"]]) && $discount[$this->mess["IBLOCK_XML2_DISCOUNT_CONDITION"]] === $this->mess["IBLOCK_XML2_DISCOUNT_COND_VOLUME"]) { $discount_value = $this->ToInt($discount[$this->mess["IBLOCK_XML2_DISCOUNT_COND_VALUE"]]); $discount_percent = $this->ToFloat($discount[$this->mess["IBLOCK_XML2_DISCOUNT_COND_PERCENT"]]); if ($discount_value > 0 && $discount_percent > 0) { $arElement["DISCOUNTS"][$discount_value] = $discount_percent; } } } } } if (array_key_exists($this->mess["IBLOCK_XML2_AMOUNT"], $arXMLElement)) { $arElementTmp = array(); $arElement["QUANTITY_RESERVED"] = 0; if ($arElement["ID"]) { $arElementTmp = CCatalogProduct::GetById($arElement["ID"]); } if (is_array($arElementTmp) && !empty($arElementTmp) && isset($arElementTmp["QUANTITY_RESERVED"])) { $arElement["QUANTITY_RESERVED"] = $arElementTmp["QUANTITY_RESERVED"]; } $arElement["QUANTITY"] = $this->ToFloat($arXMLElement[$this->mess["IBLOCK_XML2_AMOUNT"]]) - doubleval($arElement["QUANTITY_RESERVED"]); } if (isset($arElement["PRICES"]) && $this->bCatalog) { if (isset($arElement["QUANTITY"])) { $arProduct["QUANTITY"] = $arElement["QUANTITY"]; } elseif (isset($arElement["STORE_AMOUNT"]) && !empty($arElement["STORE_AMOUNT"])) { $arProduct["QUANTITY"] = array_sum($arElement["STORE_AMOUNT"]); } $rsWeight = CIBlockElement::GetProperty($IBLOCK_ID, $arElement["ID"], array(), array("CODE" => "CML2_TRAITS")); while ($arWeight = $rsWeight->Fetch()) { if ($arWeight["DESCRIPTION"] == $this->mess["IBLOCK_XML2_WEIGHT"]) { $arProduct["WEIGHT"] = $this->ToFloat($arWeight["VALUE"]) * 1000; } } $rsUnit = CIBlockElement::GetProperty($IBLOCK_ID, $arElement["ID"], array(), array("CODE" => "CML2_BASE_UNIT")); while ($arUnit = $rsUnit->Fetch()) { if ($arUnit["DESCRIPTION"] > 0) { $arProduct["MEASURE"] = $arUnit["DESCRIPTION"]; } } //Here start VAT handling //Check if all the taxes exists in BSM catalog $arTaxMap = array(); $rsTaxProperty = CIBlockElement::GetProperty($IBLOCK_ID, $arElement["ID"], array("sort" => "asc"), array("CODE" => "CML2_TAXES")); while ($arTaxProperty = $rsTaxProperty->Fetch()) { if (strlen($arTaxProperty["VALUE"]) > 0 && strlen($arTaxProperty["DESCRIPTION"]) > 0 && !array_key_exists($arTaxProperty["DESCRIPTION"], $arTaxMap)) { $arTaxMap[$arTaxProperty["DESCRIPTION"]] = array("RATE" => $this->ToFloat($arTaxProperty["VALUE"]), "ID" => $this->CheckTax($arTaxProperty["DESCRIPTION"], $this->ToFloat($arTaxProperty["VALUE"]))); } } //First find out if all the prices have TAX_IN_SUM true $TAX_IN_SUM = "Y"; foreach ($arElement["PRICES"] as $price) { if ($price["PRICE"]["TAX_IN_SUM"] !== "true") { $TAX_IN_SUM = "N"; break; } } //If there was found not included tax we'll make sure //that all prices has the same flag if ($TAX_IN_SUM === "N") { foreach ($arElement["PRICES"] as $price) { if ($price["PRICE"]["TAX_IN_SUM"] !== "false") { $TAX_IN_SUM = "Y"; break; } } //Check if there is a mix of tax in sum //and correct it by recalculating all the prices if ($TAX_IN_SUM === "Y") { foreach ($arElement["PRICES"] as $key => $price) { if ($price["PRICE"]["TAX_IN_SUM"] !== "true") { $TAX_NAME = $price["PRICE"]["TAX_NAME"]; if (array_key_exists($TAX_NAME, $arTaxMap)) { $PRICE_WO_TAX = $this->ToFloat($price[$this->mess["IBLOCK_XML2_PRICE_FOR_ONE"]]); $PRICE = $PRICE_WO_TAX + $PRICE_WO_TAX / 100.0 * $arTaxMap[$TAX_NAME]["RATE"]; $arElement["PRICES"][$key][$this->mess["IBLOCK_XML2_PRICE_FOR_ONE"]] = $PRICE; } } } } } if ($TAX_IN_SUM == "Y" && $arTaxMap) { $vat = current($arTaxMap); $arProduct["VAT_ID"] = $vat["ID"]; } else { foreach ($arElement["PRICES"] as $price) { $TAX_NAME = $price["PRICE"]["TAX_NAME"]; if (array_key_exists($TAX_NAME, $arTaxMap)) { $arProduct["VAT_ID"] = $arTaxMap[$TAX_NAME]["ID"]; break; } } } $arProduct["VAT_INCLUDED"] = $TAX_IN_SUM; CCatalogProduct::Add($arProduct); $this->SetProductPrice($arElement["ID"], $arElement["PRICES"], $arElement["DISCOUNTS"]); } elseif ($this->bCatalog && isset($arElement["STORE_AMOUNT"]) && !empty($arElement["STORE_AMOUNT"]) && CCatalogProduct::GetById($arElement["ID"])) { CCatalogProduct::Update($arElement["ID"], array("QUANTITY" => array_sum($arElement["STORE_AMOUNT"]))); } elseif ($this->bCatalog && isset($arElement["QUANTITY"]) && CCatalogProduct::GetById($arElement["ID"])) { CCatalogProduct::Update($arElement["ID"], array("QUANTITY" => $arElement["QUANTITY"])); } } $counter["UPD"]++; return $arElement["ID"]; }
foreach ($_POST["PRODUCT"] as $product) { ?> var arBarCodes = new Array(); <?php if (is_array($product["BAR_CODE"])) { foreach ($product["BAR_CODE"] as $barCode) { ?> arBarCodes.push(<?php echo $barCode; ?> ); <?php } $product["BAR_CODE"] = ''; } $reserved = CCatalogProduct::GetById($product["PRODUCT_ID"]); $reserved = $reserved["QUANTITY_RESERVED"]; ?> FillProductFields(1, { 'id': "<?php echo $product["PRODUCT_ID"]; ?> ", 'module': "<?php echo $product["MODULE"]; ?> ", 'name': "<?php echo $product["NAME"]; ?> ",