Esempio n. 1
0
 public function checkQuantity($arBasketItem, $desiredQuantity)
 {
     global $USER;
     $arResult = array();
     /** @var $productProvider IBXSaleProductProvider */
     if ($productProvider = CSaleBasket::GetProductProvider($arBasketItem)) {
         $arFieldsTmp = $productProvider::GetProductData(array("PRODUCT_ID" => $arBasketItem["PRODUCT_ID"], "QUANTITY" => $desiredQuantity, "RENEWAL" => "N", "USER_ID" => $USER->GetID(), "SITE_ID" => SITE_ID, "BASKET_ID" => $arBasketItem["ID"], "CHECK_QUANTITY" => "Y", "CHECK_PRICE" => "N", "NOTES" => $arBasketItem["NOTES"]));
     } elseif (isset($arBasketItem["CALLBACK_FUNC"]) && !empty($arBasketItem["CALLBACK_FUNC"])) {
         $arFieldsTmp = CSaleBasket::ExecuteCallbackFunction($arBasketItem["CALLBACK_FUNC"], $arBasketItem["MODULE"], $arBasketItem["PRODUCT_ID"], $desiredQuantity, "N", $USER->GetID(), SITE_ID);
     } else {
         return $arResult;
     }
     if (empty($arFieldsTmp) || !isset($arFieldsTmp["QUANTITY"])) {
         $arResult["ERROR"] = Loc::getMessage("SBB_PRODUCT_NOT_AVAILABLE", array("#PRODUCT#" => $arBasketItem["NAME"]));
     } elseif ($desiredQuantity > doubleval($arFieldsTmp["QUANTITY"])) {
         $arResult["ERROR"] = Loc::getMessage("SBB_PRODUCT_NOT_ENOUGH_QUANTITY", array("#PRODUCT#" => $arBasketItem["NAME"], "#NUMBER#" => $desiredQuantity));
     }
     return $arResult;
 }
Esempio n. 2
0
                 if (strlen($arFields["ORDER_CALLBACK_FUNC"]) > 0) {
                     $arFields = CSaleBasket::ExecuteCallbackFunction($arFields["ORDER_CALLBACK_FUNC"], $arFields["MODULE"], $arFields["PRODUCT_ID"], $arFields["QUANTITY"]);
                 }
             }
             if (!$res) {
                 if ($ex = $APPLICATION->GetException()) {
                     $errorMessage .= $ex->GetString();
                 } else {
                     $errorMessage .= str_replace("#ID#", $arBasketList[$i][$j]["PRODUCT_ID"], GetMessage("SOE_ERROR_SAVE_ITEM")) . ". ";
                 }
             }
         }
     }
     foreach ($arOldBasketList as $key => $value) {
         if (strlen($value["CANCEL_CALLBACK_FUNC"]) > 0) {
             $arFields = CSaleBasket::ExecuteCallbackFunction($value["CANCEL_CALLBACK_FUNC"], $value["MODULE"], $value["PRODUCT_ID"], $value["QUANTITY"], true);
         }
         CSaleBasket::Delete($key);
     }
 }
 if (strlen($errorMessage) <= 0) {
     $arOldTaxList = array();
     $dbTax = CSaleOrderTax::GetList(array("APPLY_ORDER" => "ASC"), array("ORDER_ID" => $ID), false, false, array("*"));
     while ($arTax = $dbTax->Fetch()) {
         $arOldTaxList[IntVal($arTax["ID"])] = "Y";
     }
     for ($i = 0; $i < count($arIDs); $i++) {
         for ($j = 0; $j < count($arTaxList); $j++) {
             $arFields = array("ORDER_ID" => $arIDs[$i], "TAX_NAME" => $arTaxList[$j]["TAX_NAME"], "VALUE" => $arTaxList[$j]["VALUE"], "VALUE_MONEY" => $arOrderTaxList[$i][$j]["VALUE_MONEY"], "APPLY_ORDER" => $arTaxList[$j]["APPLY_ORDER"], "IS_PERCENT" => "Y", "IS_IN_PRICE" => $arTaxList[$j]["IS_IN_PRICE"], "CODE" => $arTaxList[$j]["CODE"]);
             $res = False;
             if ($arTaxList[$j]["ID"] > 0) {
Esempio n. 3
0
 function UpdateBasketPrices($fuserID, $siteID)
 {
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         return false;
     }
     $siteID = (string) $siteID;
     if ($siteID == '') {
         $siteID = SITE_ID;
     }
     $dbBasketItems = CSaleBasket::GetList(array("ALL_PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $siteID, "ORDER_ID" => "NULL", "SUBSCRIBE" => "N"), false, false, array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CALLBACK_FUNC", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbBasketItems->Fetch()) {
         $arFields = false;
         $arItem['CALLBACK_FUNC'] = (string) $arItem['CALLBACK_FUNC'];
         $arItem['PRODUCT_PROVIDER_CLASS'] = (string) $arItem['PRODUCT_PROVIDER_CLASS'];
         if ('' != $arItem['PRODUCT_PROVIDER_CLASS'] || '' != $arItem['CALLBACK_FUNC']) {
             $arItem['MODULE'] = (string) $arItem['MODULE'];
             $arItem['PRODUCT_ID'] = (int) $arItem['PRODUCT_ID'];
             $arItem['QUANTITY'] = (double) $arItem['QUANTITY'];
             if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                 $arFields = $productProvider::GetProductData(array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "RENEWAL" => "N", "CHECK_COUPONS" => 'Y' == $arItem['CAN_BUY'] && 'N' == $arItem['DELAY'] ? 'Y' : 'N', "BASKET_ID" => $arItem["ID"], "NOTES" => $arItem["NOTES"]));
             } else {
                 $arFields = CSaleBasket::ExecuteCallbackFunction($arItem["CALLBACK_FUNC"], $arItem["MODULE"], $arItem["PRODUCT_ID"], $arItem["QUANTITY"], "N");
             }
             if (!empty($arFields) && is_array($arFields)) {
                 $arFields['CAN_BUY'] = 'Y';
                 $arFields['TYPE'] = (int) $arItem['TYPE'];
                 $arFields['SET_PARENT_ID'] = (int) $arItem['SET_PARENT_ID'];
             } else {
                 $arFields = array('CAN_BUY' => 'N');
             }
             CSaleBasket::Update($arItem['ID'], $arFields);
         }
     }
     return true;
 }
Esempio n. 4
0
 function UpdateBasketPrices($fuserID, $siteID)
 {
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         return false;
     }
     $siteID = (string) $siteID;
     if ($siteID == '') {
         $siteID = SITE_ID;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $dbBasketItems = CSaleBasket::GetList(array("ALL_PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $siteID, "ORDER_ID" => "NULL", "SUBSCRIBE" => "N"), false, false, array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CALLBACK_FUNC", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbBasketItems->Fetch()) {
         if ($arItem['CAN_BUY'] != 'Y') {
             continue;
         }
         $arFields = false;
         $arItem['CALLBACK_FUNC'] = (string) $arItem['CALLBACK_FUNC'];
         $arItem['PRODUCT_PROVIDER_CLASS'] = (string) $arItem['PRODUCT_PROVIDER_CLASS'];
         if ('' != $arItem['PRODUCT_PROVIDER_CLASS'] || '' != $arItem['CALLBACK_FUNC']) {
             $arItem['MODULE'] = (string) $arItem['MODULE'];
             $arItem['PRODUCT_ID'] = (int) $arItem['PRODUCT_ID'];
             $arItem['QUANTITY'] = (double) $arItem['QUANTITY'];
             if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                 $arFields = $productProvider::GetProductData(array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "RENEWAL" => "N", "CHECK_COUPONS" => 'Y' == $arItem['CAN_BUY'] && 'N' == $arItem['DELAY'] ? 'Y' : 'N', "CHECK_DISCOUNT" => CSaleBasketHelper::isSetItem($arItem) ? 'N' : 'Y', "BASKET_ID" => $arItem["ID"], "NOTES" => $arItem["NOTES"]));
             } else {
                 $arFields = CSaleBasket::ExecuteCallbackFunction($arItem["CALLBACK_FUNC"], $arItem["MODULE"], $arItem["PRODUCT_ID"], $arItem["QUANTITY"], "N");
             }
             if (!empty($arFields) && is_array($arFields)) {
                 if ($isOrderConverted == 'Y' && $arItem['DELAY'] == 'N') {
                     if (!Sale\Compatible\DiscountCompatibility::isInited()) {
                         Sale\Compatible\DiscountCompatibility::init();
                     }
                     if (Sale\Compatible\DiscountCompatibility::usedByClient()) {
                         if (isset($arFields['BASE_PRICE']) && isset($arFields['CURRENCY'])) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemBasePrice($arItem['ID'], $arFields['BASE_PRICE'], $arFields['CURRENCY']);
                         }
                         if (!empty($arFields['DISCOUNT_LIST'])) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemDiscounts($arItem['ID'], $arFields['DISCOUNT_LIST']);
                         }
                     }
                 }
                 $arFields['CAN_BUY'] = 'Y';
                 $arFields['TYPE'] = (int) $arItem['TYPE'];
                 $arFields['SET_PARENT_ID'] = (int) $arItem['SET_PARENT_ID'];
             } else {
                 $arFields = array('CAN_BUY' => 'N');
             }
             CSaleBasket::Update($arItem['ID'], $arFields);
         }
     }
     return true;
 }
Esempio n. 5
0
/**
 * @param int $PRODUCT_ID
 * @param float|int $QUANTITY
 * @param array $arRewriteFields
 * @param bool|array $arProductParams
 * @return bool|int
 */
function Add2BasketByProductID($PRODUCT_ID, $QUANTITY = 1, $arRewriteFields = array(), $arProductParams = false)
{
    global $APPLICATION;
    /* for old use */
    if ($arProductParams === false) {
        $arProductParams = $arRewriteFields;
        $arRewriteFields = array();
    }
    $boolRewrite = !empty($arRewriteFields) && is_array($arRewriteFields);
    if ($boolRewrite && isset($arRewriteFields['SUBSCRIBE']) && $arRewriteFields['SUBSCRIBE'] == 'Y') {
        return SubscribeProduct($PRODUCT_ID, $arRewriteFields, $arProductParams);
    }
    $PRODUCT_ID = (int) $PRODUCT_ID;
    if ($PRODUCT_ID <= 0) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_EMPTY_PRODUCT_ID'), "EMPTY_PRODUCT_ID");
        return false;
    }
    $QUANTITY = (double) $QUANTITY;
    if ($QUANTITY <= 0) {
        $QUANTITY = 1;
    }
    if (!Loader::includeModule("sale")) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_NO_SALE_MODULE'), "NO_SALE_MODULE");
        return false;
    }
    if (Loader::includeModule("statistic") && isset($_SESSION['SESS_SEARCHER_ID']) && (int) $_SESSION["SESS_SEARCHER_ID"] > 0) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_SESS_SEARCHER'), "SESS_SEARCHER");
        return false;
    }
    $rsProducts = CCatalogProduct::GetList(array(), array('ID' => $PRODUCT_ID), false, false, array('ID', 'CAN_BUY_ZERO', 'QUANTITY_TRACE', 'QUANTITY', 'WEIGHT', 'WIDTH', 'HEIGHT', 'LENGTH', 'TYPE', 'MEASURE'));
    if (!($arCatalogProduct = $rsProducts->Fetch())) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_NO_PRODUCT'), "NO_PRODUCT");
        return false;
    }
    $arCatalogProduct['MEASURE'] = (int) $arCatalogProduct['MEASURE'];
    $arCatalogProduct['MEASURE_NAME'] = '';
    $arCatalogProduct['MEASURE_CODE'] = 0;
    if ($arCatalogProduct['MEASURE'] <= 0) {
        $arMeasure = CCatalogMeasure::getDefaultMeasure(true, true);
        $arCatalogProduct['MEASURE_NAME'] = $arMeasure['~SYMBOL_RUS'];
        $arCatalogProduct['MEASURE_CODE'] = $arMeasure['CODE'];
    } else {
        $rsMeasures = CCatalogMeasure::getList(array(), array('ID' => $arCatalogProduct['MEASURE']), false, false, array('ID', 'SYMBOL_RUS', 'CODE'));
        if ($arMeasure = $rsMeasures->GetNext()) {
            $arCatalogProduct['MEASURE_NAME'] = $arMeasure['~SYMBOL_RUS'];
            $arCatalogProduct['MEASURE_CODE'] = $arMeasure['CODE'];
        }
    }
    $dblQuantity = (double) $arCatalogProduct["QUANTITY"];
    $intQuantity = (int) $arCatalogProduct["QUANTITY"];
    $boolQuantity = $arCatalogProduct["CAN_BUY_ZERO"] != 'Y' && $arCatalogProduct["QUANTITY_TRACE"] == 'Y';
    if ($boolQuantity && $dblQuantity <= 0) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_PRODUCT_RUN_OUT'), "PRODUCT_RUN_OUT");
        return false;
    }
    $rsItems = CIBlockElement::GetList(array(), array("ID" => $PRODUCT_ID, "ACTIVE" => "Y", "ACTIVE_DATE" => "Y", "CHECK_PERMISSIONS" => "Y", "MIN_PERMISSION" => "R"), false, false, array("ID", "IBLOCK_ID", "XML_ID", "NAME", "DETAIL_PAGE_URL"));
    if (!($arProduct = $rsItems->GetNext())) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_ERR_NO_IBLOCK_ELEMENT'), "NO_IBLOCK_ELEMENT");
        return false;
    }
    $strCallbackFunc = "";
    $strProductProviderClass = "CCatalogProductProvider";
    if ($boolRewrite) {
        if (isset($arRewriteFields['CALLBACK_FUNC'])) {
            $strCallbackFunc = $arRewriteFields['CALLBACK_FUNC'];
        }
        if (isset($arRewriteFields['PRODUCT_PROVIDER_CLASS'])) {
            $strProductProviderClass = $arRewriteFields['PRODUCT_PROVIDER_CLASS'];
        }
    }
    $arCallbackPrice = false;
    if (!empty($strProductProviderClass)) {
        if ($productProvider = CSaleBasket::GetProductProvider(array('MODULE' => 'catalog', 'PRODUCT_PROVIDER_CLASS' => $strProductProviderClass))) {
            $providerParams = array('PRODUCT_ID' => $PRODUCT_ID, 'QUANTITY' => $QUANTITY, 'RENEWAL' => 'N');
            $arCallbackPrice = $productProvider::GetProductData($providerParams);
            unset($providerParams);
        }
    } elseif (!empty($strCallbackFunc)) {
        $arCallbackPrice = CSaleBasket::ExecuteCallbackFunction($strCallbackFunc, 'catalog', $PRODUCT_ID, $QUANTITY, 'N');
    }
    if (empty($arCallbackPrice) || !is_array($arCallbackPrice)) {
        $APPLICATION->ThrowException(Loc::getMessage('CATALOG_PRODUCT_PRICE_NOT_FOUND'), "NO_PRODUCT_PRICE");
        return false;
    }
    $arProps = array();
    $strIBlockXmlID = (string) CIBlock::GetArrayByID($arProduct['IBLOCK_ID'], 'XML_ID');
    if ($strIBlockXmlID !== '') {
        $arProps[] = array('NAME' => 'Catalog XML_ID', 'CODE' => 'CATALOG.XML_ID', 'VALUE' => $strIBlockXmlID);
    }
    // add sku props
    $arParentSku = CCatalogSku::GetProductInfo($PRODUCT_ID, $arProduct['IBLOCK_ID']);
    if (!empty($arParentSku)) {
        if (strpos($arProduct["~XML_ID"], '#') === false) {
            $parentIterator = Iblock\ElementTable::getList(array('select' => array('ID', 'XML_ID'), 'filter' => array('ID' => $arParentSku['ID'])));
            if ($parent = $parentIterator->fetch()) {
                $arProduct["~XML_ID"] = $parent['XML_ID'] . '#' . $arProduct["~XML_ID"];
            }
            unset($parent, $parentIterator);
        }
    }
    if (!empty($arProductParams) && is_array($arProductParams)) {
        foreach ($arProductParams as &$arOneProductParams) {
            $arProps[] = array("NAME" => $arOneProductParams["NAME"], "CODE" => $arOneProductParams["CODE"], "VALUE" => $arOneProductParams["VALUE"], "SORT" => $arOneProductParams["SORT"]);
        }
        unset($arOneProductParams);
    }
    $arProps[] = array("NAME" => "Product XML_ID", "CODE" => "PRODUCT.XML_ID", "VALUE" => $arProduct["~XML_ID"]);
    $arFields = array("PRODUCT_ID" => $PRODUCT_ID, "PRODUCT_PRICE_ID" => $arCallbackPrice["PRODUCT_PRICE_ID"], "PRICE" => $arCallbackPrice["PRICE"], "CURRENCY" => $arCallbackPrice["CURRENCY"], "DISCOUNT_PRICE" => $arCallbackPrice["DISCOUNT_PRICE"], "WEIGHT" => $arCatalogProduct["WEIGHT"], "DIMENSIONS" => serialize(array("WIDTH" => $arCatalogProduct["WIDTH"], "HEIGHT" => $arCatalogProduct["HEIGHT"], "LENGTH" => $arCatalogProduct["LENGTH"])), "QUANTITY" => $boolQuantity && $dblQuantity < $QUANTITY ? $dblQuantity : $QUANTITY, "LID" => SITE_ID, "DELAY" => "N", "CAN_BUY" => "Y", "NAME" => $arProduct["~NAME"], "MODULE" => "catalog", "PRODUCT_PROVIDER_CLASS" => "CCatalogProductProvider", "NOTES" => $arCallbackPrice["NOTES"], "DETAIL_PAGE_URL" => $arProduct["~DETAIL_PAGE_URL"], "CATALOG_XML_ID" => $strIBlockXmlID, "PRODUCT_XML_ID" => $arProduct["~XML_ID"], "VAT_RATE" => $arCallbackPrice['VAT_RATE'], "PROPS" => $arProps, "TYPE" => $arCatalogProduct["TYPE"] == CCatalogProduct::TYPE_SET ? CCatalogProductSet::TYPE_SET : NULL, "MEASURE_NAME" => $arCatalogProduct['MEASURE_NAME'], "MEASURE_CODE" => $arCatalogProduct['MEASURE_CODE']);
    if ($boolRewrite) {
        $arFields = array_merge($arFields, $arRewriteFields);
    }
    $result = CSaleBasket::Add($arFields);
    if ($result) {
        if (Loader::includeModule("statistic")) {
            CStatistic::Set_Event("sale2basket", "catalog", $arFields["DETAIL_PAGE_URL"]);
        }
    }
    return $result;
}
Esempio n. 6
0
 /**
  * The function add viewed product
  *
  * @param array $arFields - params for add
  * @return true false
  */
 public static function Add($arFields)
 {
     global $DB;
     global $USER;
     global $APPLICATION;
     foreach (GetModuleEvents("sale", "OnBeforeViewedAdd", true) as $arEvent) {
         if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) {
             return false;
         }
     }
     if (isset($arFields["ID"])) {
         unset($arFields["ID"]);
     }
     $arFields["PRODUCT_ID"] = IntVal($arFields["PRODUCT_ID"]);
     $arFields["USER_ID"] = IntVal($arFields["USER_ID"]);
     $arFields["FUSER_ID"] = IntVal($arFields["FUSER_ID"]);
     $arFields["IBLOCK_ID"] = IntVal($arFields["IBLOCK_ID"]);
     if (strlen($arFields["CALLBACK_FUNC"]) <= 0) {
         $arFields["CALLBACK_FUNC"] = "CatalogViewedProductCallback";
     }
     if (strlen($arFields["MODULE"]) <= 0) {
         $arFields["MODULE"] = "catalog";
     }
     if (strlen($arFields["PRODUCT_PROVIDER_CLASS"]) <= 0 && $arFields["MODULE"] == "catalog") {
         $arFields["PRODUCT_PROVIDER_CLASS"] = "CCatalogProductProvider";
     }
     if ($arFields["PRODUCT_ID"] <= 0) {
         return false;
     }
     if (strlen($arFields["LID"]) <= 0) {
         return false;
     }
     if (\Bitrix\Main\Loader::includeModule("catalog")) {
         if (\Bitrix\Main\Config\Option::get("sale", "viewed_capability", "") == "Y") {
             return \Bitrix\Catalog\CatalogViewedProductTable::refresh($arFields["PRODUCT_ID"], CSaleBasket::GetBasketUserID(), $arFields["LID"]);
         }
     }
     $arFilter = array();
     $arFilter["PRODUCT_ID"] = $arFields["PRODUCT_ID"];
     if ($arFields["USER_ID"] > 0) {
         $arFuserItems = CSaleUser::GetList(array("USER_ID" => $arFields["USER_ID"]));
         $FUSER_ID = $arFuserItems["ID"];
     } elseif (IntVal($arFields["FUSER_ID"]) > 0) {
         $FUSER_ID = $arFields["FUSER_ID"];
     } else {
         $FUSER_ID = CSaleBasket::GetBasketUserID();
     }
     $arFilter["FUSER_ID"] = $FUSER_ID;
     $arFields["FUSER_ID"] = $FUSER_ID;
     $db_res = CSaleViewedProduct::GetList(array(), $arFilter, false, false, array('ID'));
     if (!($arItems = $db_res->Fetch())) {
         if (CModule::IncludeModule('catalog')) {
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider($arFields)) {
                 $arResultTmp = $productProvider::ViewProduct(array("PRODUCT_ID" => $arFields["PRODUCT_ID"], "USER_ID" => $arFields["USER_ID"], "SITE_ID" => $arFields["LID"]));
             } else {
                 $arResultTmp = CSaleBasket::ExecuteCallbackFunction($arFields["CALLBACK_FUNC"], $arFields["MODULE"], $arFields["PRODUCT_ID"], $arFields["USER_ID"], $arFields["LID"]);
             }
             if ($arResultTmp && count($arResultTmp) > 0) {
                 $arFields = array_merge($arFields, $arResultTmp);
             }
             if (strlen($arFields["NAME"]) <= 0) {
                 return false;
             }
             $arInsert = $DB->PrepareInsert("b_sale_viewed_product", $arFields);
             //chance deleted
             $rnd = mt_rand(0, 1000);
             if ($rnd < 100) {
                 $db_res = CSaleViewedProduct::GetList(array(), array("FUSER_ID" => $FUSER_ID), array("COUNT" => "ID"), false);
                 $arCount = $db_res->Fetch();
                 $viewedCount = COption::GetOptionString("sale", "viewed_count", "100");
                 if ($arCount["ID"] > IntVal($viewedCount)) {
                     $limit = $arCount["ID"] - $viewedCount + $viewedCount * 0.2;
                     CSaleViewedProduct::DeleteForUser($FUSER_ID, $limit);
                 }
             }
             $strSql = "INSERT INTO b_sale_viewed_product (" . $arInsert[0] . ", DATE_VISIT) VALUES(" . $arInsert[1] . ", " . $DB->GetNowFunction() . ")";
             $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             $ID = IntVal($DB->LastID());
         }
     } else {
         $ID = IntVal($arItems["ID"]);
         $arFields["DATE_VISIT"] = $DB->GetNowFunction();
         $arInsert = $DB->PrepareInsert("b_sale_viewed_product", $arFields);
         CSaleViewedProduct::Update($ID, $arFields);
     }
     foreach (GetModuleEvents("sale", "OnViewedAdd", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($arFields));
     }
     return $ID;
 }
Esempio n. 7
0
	/**
	 * <p>Метод осуществляет продление подписки с кодом ID.</p> <p><b>Примечание</b>. Метод использует внутреннюю транзакцию. Если у вас используется <b>MySQL</b> и <b>InnoDB</b>, и ранее была открыта транзакция, то ее необходимо закрыть до подключения метода.</p>
	 *
	 *
	 *
	 *
	 * @param int $ID  Код записи с информацией о продлении.
	 *
	 *
	 *
	 * @return bool <p>Метод возвращает <i>true</i> в случае успешного продления или <i>false</i>
	 * в случае ошибки.</p><br><br>
	 *
	 * @static
	 * @link http://dev.1c-bitrix.ru/api_help/sale/classes/csalerecurring/csalerecurring.nextpayment.php
	 * @author Bitrix
	 */
	public static function NextPayment($ID)
	{
		global $DB;
		global $USER;

		$ID = IntVal($ID);
		if ($ID <= 0)
		{
			$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGR_NO_RECID"), "NO_RECORD_ID");
			return False;
		}

		$arRecur = CSaleRecurring::GetByID($ID);
		if (!$arRecur)
		{
			$GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $ID, GetMessage("SKGR_NO_RECID1")), "NO_RECORD");
			return False;
		}

		$arOrder = CSaleOrder::GetByID($arRecur["ORDER_ID"]);
		if (!$arOrder)
		{
			$GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $arRecur["ORDER_ID"], GetMessage("SKGR_NO_ORDER1")), "NO_ORDER");
			return False;
		}


		$bSuccess = True;
		$newOrderID = IntVal($arRecur["ORDER_ID"]);

		/** @var $productProvider IBXSaleProductProvider */
		if ($productProvider = CSaleBasket::GetProductProvider($arRecur))
		{
			$arProduct = $productProvider::RecurringOrderProduct(array(
				"PRODUCT_ID" => $arRecur["PRODUCT_ID"],
				"USER_ID"    => $arOrder["USER_ID"]
			));
		}
		else
		{
			$arProduct = CSaleRecurring::ExecuteCallbackFunction(
					$arRecur["CALLBACK_FUNC"],
					$arRecur["MODULE"],
					$arRecur["PRODUCT_ID"],
					$arOrder["USER_ID"]
				);
		}

		if (!$arProduct || !is_array($arProduct) || count($arProduct) <= 0)
		{
			CSaleRecurring::CancelRecurring($arRecur["ID"], "Y", "Product is not found");

			/*
			$arFields = array(
					"CANCELED" => "Y",
					"DATE_CANCELED" => Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG))),
					"CANCELED_REASON" => "Product is not found"
				);
			CSaleRecurring::Update($arRecur["ID"], $arFields);
			*/
			//CSaleRecurring::Delete($arRecur["ID"]);

			return True;
		}

		if ($arProduct["WITHOUT_ORDER"] == "Y" || $arRecur["SUCCESS_PAYMENT"] == "Y")
		{
			$baseSiteCurrency = CSaleLang::GetLangCurrency($arOrder["LID"]);
			$productPrice = roundEx(CCurrencyRates::ConvertCurrency($arProduct["PRICE"], $arProduct["CURRENCY"], $baseSiteCurrency), SALE_VALUE_PRECISION);

			// Delivery
			$deliveryPrice = 0;
			$deliveryID = 0;
			$arOrder["DELIVERY_ID"] = IntVal($arOrder["DELIVERY_ID"]);
			if ($arOrder["DELIVERY_ID"] > 0)
			{
				$deliveryLocation = 0;
				$dbOrderPropValues = CSaleOrderPropsValue::GetList(
						array(),
						array(
								"ORDER_ID" => $arRecur["ORDER_ID"],
								"PROP_IS_LOCATION" => "Y"
							),
						false,
						false,
						array("VALUE")
					);
				if ($arOrderPropValues = $dbOrderPropValues->Fetch())
					$deliveryLocation = IntVal($arOrderPropValues["VALUE"]);

				$dbDelivery = CSaleDelivery::GetList(
						array("SORT" => "ASC", "NAME" => "ASC"),
						array(
								"LID" => $arOrder["LID"],
								"WEIGHT" => DoubleVal($arProduct["WEIGHT"]) * DoubleVal($arProduct["QUANTITY"]), // Changed by Sigurd, 2007-08-16
								"ORDER_PRICE" => $productPrice * DoubleVal($arProduct["QUANTITY"]),
								"ACTIVE" => "Y",
								"LOCATION" => $deliveryLocation
							)
					);
				while ($arDelivery = $dbDelivery->Fetch())
				{
					$deliveryPriceTmp = roundEx(CCurrencyRates::ConvertCurrency($arDelivery["PRICE"], $arDelivery["CURRENCY"], $baseSiteCurrency), SALE_VALUE_PRECISION);
					if (IntVal($arDelivery["ID"]) == $arOrder["DELIVERY_ID"])
					{
						$deliveryID = IntVal($arDelivery["ID"]);
						$deliveryPrice = $deliveryPriceTmp;
						break;
					}
					if ($deliveryPriceTmp < $deliveryPrice || $deliveryID <= 0)
					{
						$deliveryID = IntVal($arDelivery["ID"]);
						$deliveryPrice = $deliveryPriceTmp;
					}
				}

				if ($deliveryID <= 0)
				{
					$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGR_NO_DELIVERY"), "NO_DELIVERY");
					return False;
				}
			}

			// Sale discounts
			$discount = 0;
			$discountPrice = $productPrice;
			$discountProduct = 0;
			$dbDiscount = CSaleDiscount::GetList(
					array("SORT" => "ASC"),
					array(
							"LID" => $arOrder["LID"],
							"ACTIVE" => "Y",
							"!>ACTIVE_FROM" => Date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL"))),
							"!<ACTIVE_TO" => Date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL"))),
							"<=PRICE_FROM" => $productPrice,
							">=PRICE_TO" => $productPrice,
							"USER_GROUPS" => $USER->GetUserGroup($arOrder['USER_ID']),
						)
				);
			if ($arDiscount = $dbDiscount->Fetch())
			{
				if ($arDiscount["DISCOUNT_TYPE"] == "P")
				{
					$discountProduct = roundEx($productPrice * $arDiscount["DISCOUNT_VALUE"] / 100, SALE_VALUE_PRECISION);
					$discount = roundEx($discountProduct * DoubleVal($arProduct["QUANTITY"]), SALE_VALUE_PRECISION); // Changed by Sigurd, 2007-08-16
					$discountPrice = $productPrice - $discountProduct;
				}
				else
				{
					$discountValue = CCurrencyRates::ConvertCurrency($arDiscount["DISCOUNT_VALUE"], $arDiscount["CURRENCY"], $baseSiteCurrency);
					$discountValue = roundEx($discountValue, SALE_VALUE_PRECISION);

					$discountProduct = roundEx(1.0 * $discountValue / DoubleVal($arProduct["QUANTITY"]), SALE_VALUE_PRECISION);// Changed by Sigurd, 2007-08-16
					$discount = roundEx($curDiscount * DoubleVal($arProduct["QUANTITY"]), SALE_VALUE_PRECISION);
					$discountPrice = $productPrice - $discountProduct;
				}
			}

			$bUseVat = false;
			$vatRate = 0;
			if(DoubleVal($arProduct["VAT_RATE"]) > 0)
			{
				$bUseVat = true;
				$vatRate = $arProduct["VAT_RATE"];
			}

			// Tax
			$arTaxExempt = array();
			$dbUserGroups = CUser::GetUserGroupEx($arOrder["USER_ID"]);
			while ($arUserGroups = $dbUserGroups->Fetch())
			{
				$dbTaxExemptTmp = CSaleTax::GetExemptList(array("GROUP_ID" => $arUserGroups["GROUP_ID"]));
				while ($arTaxExemptTmp = $dbTaxExemptTmp->Fetch())
				{
					$arTaxExemptTmp["TAX_ID"] = IntVal($arTaxExemptTmp["TAX_ID"]);
					if (!in_array($arTaxExemptTmp["TAX_ID"], $arTaxExempt))
						$arTaxExempt[] = $arTaxExemptTmp["TAX_ID"];
				}
			}

			$taxPrice = 0;
			$taxVatPrice = 0;
			if(!$bUseVat)
			{
				$taxLocation = 0;
				$dbOrderPropValues = CSaleOrderPropsValue::GetList(
						array(),
						array(
								"ORDER_ID" => $arRecur["ORDER_ID"],
								"PROP_IS_LOCATION4TAX" => "Y"
							),
						false,
						false,
						array("VALUE")
					);
				if ($arOrderPropValues = $dbOrderPropValues->Fetch())
					$taxLocation = IntVal($arOrderPropValues["VALUE"]);

				$arTaxList = array();
				$dbTaxRateTmp = CSaleTaxRate::GetList(
						array("APPLY_ORDER" => "ASC"),
						array(
								"LID" => $arOrder["LID"],
								"PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"],
								"ACTIVE" => "Y",
								"LOCATION" => $taxLocation
							)
					);
				while ($arTaxRateTmp = $dbTaxRateTmp->Fetch())
				{
					if (!in_array(IntVal($arTaxRateTmp["TAX_ID"]), $arTaxExempt))
					{
						$arTaxList[] = $arTaxRateTmp;
					}
				}
			}
			else
			{
				$arTaxList[] = Array(
							"ID" => 0,
							"TAX_NAME" => GetMessage("SKGR_VAT"),
							"IS_PERCENT" => "Y",
							"VALUE" => $vatRate*100,
							"VALUE_MONEY" => 0,
							"APPLY_ORDER" => 100,
							"IS_IN_PRICE" => "Y",
							"CODE" => "VAT"
				);
			}


			$arTaxSums = array();

			if (!empty($arTaxList))
			{
				if(!$bUseVat)
				{
					$taxPriceTmp = CSaleOrderTax::CountTaxes(
							$discountPrice * DoubleVal($arProduct["QUANTITY"]),
							$arTaxList,
							$baseSiteCurrency
						);

					for ($di = 0, $intCount = count($arTaxList); $di < $intCount; $di++)
					{
						$arTaxList[$di]["VALUE_MONEY"] += $arTaxList[$di]["TAX_VAL"];
					}

					for ($di = 0, $intCount = count($arTaxList); $di < $intCount; $di++)
					{
						$arTaxSums[$arTaxList[$di]["TAX_ID"]]["VALUE"] = $arTaxList[$di]["VALUE_MONEY"];
						$arTaxSums[$arTaxList[$di]["TAX_ID"]]["NAME"] = $arTaxList[$di]["NAME"];
						if ($arTaxList[$di]["IS_IN_PRICE"] != "Y")
						{
							$taxPrice += $arTaxList[$di]["VALUE_MONEY"];
						}
					}
				}
				else
				{
					$arTaxList[0]["VALUE_MONEY"] = (($discountPrice / ($vatRate +1)) * $vatRate) * DoubleVal($arProduct["QUANTITY"]);
					$taxVatPrice = $arTaxList[0]["VALUE_MONEY"];
				}
			}


			// Changed by Sigurd, 2007-08-16
			$totalOrderPrice = $discountPrice * DoubleVal($arProduct["QUANTITY"]) + $deliveryPrice + $taxPrice;

			$arProduct["WITHOUT_ORDER"] = (($arProduct["WITHOUT_ORDER"]=="Y") ? "Y" : "N");
			if ($arProduct["WITHOUT_ORDER"] == "N")
			{
				$DB->StartTransaction();

				// Saving
				$arSaleUser = CSaleUser::GetList(Array(), Array("USER_ID" => $arOrder["USER_ID"]));
				if(!empty($arSaleUser))
				{
					$currentFUser = $arSaleUser["ID"];
				}
				else
				{
					$currentFUser = CSaleUser::_Add(
							array(
									"=DATE_INSERT" => $DB->GetNowFunction(),
									"=DATE_UPDATE" => $DB->GetNowFunction(),
									"USER_ID" => $arOrder["USER_ID"]
								)
						);
				}

				$arFields = array(
						"FUSER_ID" => $currentFUser,
						"PRODUCT_ID" => $arProduct["PRODUCT_ID"],
						"PRODUCT_NAME" => $arProduct["PRODUCT_NAME"],
						"PRODUCT_URL" => $arProduct["PRODUCT_URL"],
						"PRODUCT_PRICE_ID" => $arProduct["PRODUCT_PRICE_ID"],
						"PRICE" => $arProduct["PRICE"],
						"CURRENCY" => $arProduct["CURRENCY"],
						"WEIGHT" => $arProduct["WEIGHT"],
						"QUANTITY" => $arProduct["QUANTITY"],
						"LID" => $arOrder["LID"],
						"DELAY" => "N",
						"CAN_BUY" => "Y",
						"NAME" => $arProduct["NAME"],
						"CALLBACK_FUNC" => $arProduct["CALLBACK_FUNC"],
						"ORDER_CALLBACK_FUNC" => $arProduct["ORDER_CALLBACK_FUNC"],
						"CANCEL_CALLBACK_FUNC" => $arProduct["CANCEL_CALLBACK_FUNC"],
						"PAY_CALLBACK_FUNC" => $arProduct["PAY_CALLBACK_FUNC"],
						"PRODUCT_PROVIDER_CLASS" => $arProduct["PRODUCT_PROVIDER_CLASS"],
						"MODULE" => $arRecur["MODULE"],
						"NOTES" => $arProduct["CATALOG_GROUP_NAME"],
						"DETAIL_PAGE_URL" => $arProduct["DETAIL_PAGE_URL"],
						"VATE_RATE" => $arProduct["VATE_RATE"],
						"PRODUCT_XML_ID" => $arProduct["PRODUCT_XML_ID"],
						"RENEWAL" => "Y"
					);

				$basketID = CSaleBasket::Add($arFields);

				$basketID = IntVal($basketID);
				if ($basketID <= 0)
					$bSuccess = False;

				if ($bSuccess)
				{
					if (CModule::IncludeModule("statistic"))
						CStatistic::Set_Event("eStore", "add2basket", $arFields["PRODUCT_ID"]);

					$arFields = array(
							"LID" => $arOrder["LID"],
							"PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"],
							"PAYED" => "N",
							"CANCELED" => "N",
							"STATUS_ID" => "N",
							"PRICE_DELIVERY" => $deliveryPrice,
							"ALLOW_DELIVERY" => "N",
							"PRICE" => $totalOrderPrice,
							"CURRENCY" => $baseSiteCurrency,
							"DISCOUNT_VALUE" => $discount,
							"USER_ID" => $arOrder["USER_ID"],
							"PAY_SYSTEM_ID" => $arOrder["PAY_SYSTEM_ID"],
							"DELIVERY_ID" => $deliveryID,
							"USER_DESCRIPTION" => $arOrder["USER_DESCRIPTION"],
							"TAX_VALUE" => (($bUseVat)? $taxVatPrice : $taxPrice),
							"STAT_GID" => $arOrder["STAT_GID"],
							"RECURRING_ID" => $arRecur["ID"]
						);

					$newOrderID = CSaleOrder::Add($arFields);

					$newOrderID = IntVal($newOrderID);
					if ($newOrderID <= 0)
						$bSuccess = False;
				}

				if ($bSuccess)
				{
					$arDiscounts = array();
					$arDiscounts[$basketID] = $discountProduct;
					CSaleBasket::OrderBasket($newOrderID, $currentFUser, $arOrder["LID"], $arDiscounts);
				}

				if ($bSuccess)
				{
					for ($it = 0, $intCount = count($arTaxList); $it < $intCount; $it++)
					{
						$arFields = array(
							"ORDER_ID" => $newOrderID,
							"TAX_NAME" => $arTaxList[$it]["TAX_NAME"],
							"IS_PERCENT" => $arTaxList[$it]["IS_PERCENT"],
							"VALUE" => ($arTaxList[$it]["IS_PERCENT"]=="Y") ? $arTaxList[$it]["VALUE"] : RoundEx(CCurrencyRates::ConvertCurrency($arTaxList[$it]["VALUE"], $arTaxList[$it]["CURRENCY"], $baseSiteCurrency), 2),
							"VALUE_MONEY" => $arTaxList[$it]["VALUE_MONEY"],
							"APPLY_ORDER" => $arTaxList[$it]["APPLY_ORDER"],
							"IS_IN_PRICE" => $arTaxList[$it]["IS_IN_PRICE"],
							"CODE" => $arTaxList[$it]["CODE"]
							);
						CSaleOrderTax::Add($arFields);
					}

					$dbOrderPropValues = CSaleOrderPropsValue::GetList(
							array(),
							array("ORDER_ID" => $arRecur["ORDER_ID"]),
							false,
							false,
							array("ORDER_PROPS_ID", "NAME", "CODE", "VALUE", "PROP_IS_PAYER", "PROP_IS_EMAIL")
						);
					while ($arOrderPropValues = $dbOrderPropValues->Fetch())
					{
						$arFields = array(
							"ORDER_ID" => $newOrderID,
							"ORDER_PROPS_ID" => $arOrderPropValues["ORDER_PROPS_ID"],
							"NAME" => $arOrderPropValues["NAME"],
							"CODE" => $arOrderPropValues["CODE"],
							"VALUE" => $arOrderPropValues["VALUE"]
							);
						CSaleOrderPropsValue::Add($arFields);

						if ($arOrderPropValues["PROP_IS_PAYER"] == "Y")
							$payerName = $arOrderPropValues["VALUE"];
						if ($arOrderPropValues["PROP_IS_EMAIL"] == "Y")
							$payerEMail = $arOrderPropValues["VALUE"];
					}
				}

				if ($bSuccess)
				{
					if (CModule::IncludeModule("statistic"))
						CStatistic::Set_Event("eStore", "order_create", $newOrderID);
				}

				if ($bSuccess)
				{
					$strOrderList = "";
					$dbBasketTmp = CSaleBasket::GetList(
						array("NAME" => "ASC"),
						array("ORDER_ID" => $newOrderID)
					);
					while ($arBasketTmp = $dbBasketTmp->Fetch())
					{
						$strOrderList .= $arBasketTmp["NAME"]." - ".$arBasketTmp["QUANTITY"]." ".GetMessage("SALE_QUANTITY_UNIT");
						$strOrderList .= "\n";
					}

					if (strlen($payerName) <= 0 || strlen($payerEMail) <= 0)
					{
						$dbUser = CUser::GetByID($arOrder["USER_ID"]);
						if ($arUser = $dbUser->Fetch())
						{
							if (strlen($payerName) <= 0)
								$payerName = $arUser["NAME"].((strlen($arUser["NAME"])<=0 || strlen($arUser["LAST_NAME"])<=0) ? "" : " ").$arUser["LAST_NAME"];
							if (strlen($payerEMail) <= 0)
								$payerEMail = $arUser["EMAIL"];
						}
					}

					$arFields = Array(
						"ORDER_ID" => $newOrderID,
						"ORDER_DATE" => Date($DB->DateFormatToPHP(CLang::GetDateFormat("SHORT", $arOrder["LID"]))),
						"ORDER_USER" => $payerName,
						"PRICE" => SaleFormatCurrency($totalOrderPrice, $baseSiteCurrency),
						"BCC" => COption::GetOptionString("sale", "order_email", "order@".$SERVER_NAME),
						"EMAIL" => $payerEMail,
						"ORDER_LIST" => $strOrderList,
						"SALE_EMAIL" => COption::GetOptionString("sale", "order_email", "order@".$SERVER_NAME)
					);
					$eventName = "SALE_NEW_ORDER_RECURRING";

					$bSend = true;
					foreach(GetModuleEvents("sale", "OnOrderRecurringSendEmail", true) as $arEvent)
					{
						if (ExecuteModuleEventEx($arEvent, Array($newOrderID, &$eventName, &$arFields))===false)
							$bSend = false;
					}

					if($bSend)
					{
						$event = new CEvent;
						$event->Send($eventName, $arOrder["LID"], $arFields, "N");
					}
				}

				if ($bSuccess)
					$DB->Commit();
				else
					$DB->Rollback();
			}
		}
		else
		{
			$totalOrderPrice = $arOrder["PRICE"];
			$baseSiteCurrency = $arOrder["CURRENCY"];
		}

		$res = False;
		if ($bSuccess)
		{
			$res = CSaleUserAccount::Pay($arOrder["USER_ID"], $totalOrderPrice, $baseSiteCurrency, $newOrderID, True);
			if ($res)
			{
				if ($arProduct["WITHOUT_ORDER"] == "N")
				{
					CSaleOrder::PayOrder($newOrderID, "Y", False, False, $arRecur["ID"]);
					CSaleOrder::DeliverOrder($newOrderID, "Y", $arRecur["ID"]);
					CSaleOrder::DeductOrder($newOrderID, "Y", "", true, array(), $arRecur["ID"]);
				}
				else
				{
					/** @var $productProvider IBXSaleProductProvider */
					if ($productProvider = CSaleBasket::GetProductProvider($arProduct))
					{
						$r = $productProvider::DeliverProduct(array(
							"PRODUCT_ID" => $arProduct["PRODUCT_ID"],
							"USER_ID"    => $arOrder["USER_ID"],
							"PAID"       => true
						));
					}
					else
					{
						$r = CSaleBasket::ExecuteCallbackFunction(
							$arProduct["PAY_CALLBACK_FUNC"],
							$arRecur["MODULE"],
							$arProduct["PRODUCT_ID"],
							$arOrder["USER_ID"],
							true
						);
					}
				}

				$arFields = array(
					"ORDER_ID" => $newOrderID,
					"PRODUCT_NAME" => $arProduct["PRODUCT_NAME"],
					"PRODUCT_URL" => $arProduct["PRODUCT_URL"],
					"PRICE_TYPE" => $arProduct["PRICE_TYPE"],
					"RECUR_SCHEME_TYPE" => $arProduct["RECUR_SCHEME_TYPE"],
					"RECUR_SCHEME_LENGTH" => $arProduct["RECUR_SCHEME_LENGTH"],
					"WITHOUT_ORDER" => $arProduct["WITHOUT_ORDER"],
					"PRIOR_DATE" => Date($GLOBALS["DB"]->DateFormatToPHP(CLang::GetDateFormat("FULL", SITE_ID))),
					"NEXT_DATE" => $arProduct["NEXT_DATE"],
					"REMAINING_ATTEMPTS" => (Defined("SALE_PROC_REC_ATTEMPTS") ? SALE_PROC_REC_ATTEMPTS : 3),
					"SUCCESS_PAYMENT" => "Y"
				);

				CSaleRecurring::Update($arRecur["ID"], $arFields);
			}
			else
			{
				$arFields = array(
						"ORDER_ID" => $newOrderID,
						"PRODUCT_NAME" => $arProduct["PRODUCT_NAME"],
						"PRODUCT_URL" => $arProduct["PRODUCT_URL"],
						"PRICE_TYPE" => $arProduct["PRICE_TYPE"],
						"RECUR_SCHEME_LENGTH" => $arProduct["RECUR_SCHEME_LENGTH"],
						"RECUR_SCHEME_TYPE" => $arProduct["RECUR_SCHEME_TYPE"],
						"WITHOUT_ORDER" => $arProduct["WITHOUT_ORDER"],
						"NEXT_DATE" => Date($GLOBALS["DB"]->DateFormatToPHP(CLang::GetDateFormat("FULL", SITE_ID)), time() + SALE_PROC_REC_TIME + CTimeZone::GetOffset()),
						"REMAINING_ATTEMPTS" => (IntVal($arRecur["REMAINING_ATTEMPTS"]) - 1),
						"SUCCESS_PAYMENT" => "N"
					);
				CSaleRecurring::Update($arRecur["ID"], $arFields);

				if ((IntVal($arRecur["REMAINING_ATTEMPTS"]) - 1) <= 0)
				{
					CSaleRecurring::CancelRecurring($arRecur["ID"], "Y", "Can't pay order");
					/*
					$arFields["CANCELED"] = "Y";
					$arFields["DATE_CANCELED"] = Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG)));
					$arFields["CANCELED_REASON"] = "Can't pay order";
					*/
				}
			}
		}

		return $res;
	}
Esempio n. 8
0
         $propertyIterator = CIBlockElement::GetProperty($element['IBLOCK_ID'], $element['ID'], array(), array('ID' => $sku['SKU_PROPERTY_ID']));
         if ($property = $propertyIterator->Fetch()) {
             $parentId = (int) $property['VALUE'];
         }
         unset($property, $propertyIterator);
     }
 }
 if (!empty($element) && $parentId > 0) {
     $bBasketUpdate = false;
     $arPropsValues["CML2_LINK"] = $parentId;
     $newProductId = getProductByProps($element['IBLOCK_ID'], $arPropsValues, true);
     if (!empty($newProductId)) {
         if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
             $arFieldsTmp = $productProvider::GetProductData(array("PRODUCT_ID" => $newProductId['ID'], "QUANTITY" => $arItem['QUANTITY'], "RENEWAL" => "N", "USER_ID" => $USER->GetID(), "SITE_ID" => SITE_ID, "BASKET_ID" => $arItem['ID'], "CHECK_QUANTITY" => "Y", "CHECK_PRICE" => "Y", "NOTES" => $arItem["NOTES"]));
         } elseif (isset($arItem["CALLBACK_FUNC"]) && !empty($arItem["CALLBACK_FUNC"])) {
             $arFieldsTmp = CSaleBasket::ExecuteCallbackFunction($arItem["CALLBACK_FUNC"], $arItem["MODULE"], $newProductId['ID'], $arItem['QUANTITY'], "N", $USER->GetID(), SITE_ID);
         }
         if (!empty($arFieldsTmp) && is_array($arFieldsTmp)) {
             $arFields = array('PRODUCT_ID' => $newProductId['ID'], 'PRODUCT_PRICE_ID' => $arFieldsTmp["PRODUCT_PRICE_ID"], 'PRICE' => $arFieldsTmp["PRICE"], 'CURRENCY' => $arFieldsTmp["CURRENCY"], 'QUANTITY' => $arFieldsTmp['QUANTITY'], 'WEIGHT' => $arFieldsTmp['WEIGHT']);
             $arProps = array();
             if (strpos($newProductId['XML_ID'], '#') === false) {
                 $parentIterator = \Bitrix\Iblock\ElementTable::getList(array('select' => array('ID', 'XML_ID'), 'filter' => array('ID' => $parentId)));
                 if ($parentProduct = $parentIterator->fetch()) {
                     $newProductId['XML_ID'] = $parentProduct['XML_ID'] . '#' . $newProductId['XML_ID'];
                 }
                 unset($parentProduct, $parentIterator);
             }
             $arFields["PRODUCT_XML_ID"] = $newProductId['XML_ID'];
             $propertyIterator = \Bitrix\Iblock\PropertyTable::getList(array('select' => array('ID', 'CODE'), 'filter' => array('IBLOCK_ID' => $newProductId['IBLOCK_ID'], '!ID' => $sku['SKU_PROPERTY_ID'])));
             while ($property = $propertyIterator->fetch()) {
                 $property['CODE'] = (string) $property['CODE'];
Esempio n. 9
0
	function UpdateBasketPrices($fuserID, $siteID)
	{
		$fuserID = intval($fuserID);
		if (0 >= $fuserID)
			return false;
		if(strlen($siteID) <= 0)
			$siteID = SITE_ID;

		$dbBasketItems = CSaleBasket::GetList(
			array("ALL_PRICE" => "DESC"),
			array(
				"FUSER_ID" => $fuserID,
				"LID" => $siteID,
				"ORDER_ID" => "NULL",
				"SUBSCRIBE" => "N"
			),
			false,
			false,
			array("ID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES")
		);
		while ($arItem = $dbBasketItems->Fetch())
		{
			$arFields = false;
			$arItem['CALLBACK_FUNC'] = strval($arItem['CALLBACK_FUNC']);
			$arItem['PRODUCT_PROVIDER_CLASS'] = strval($arItem['PRODUCT_PROVIDER_CLASS']);
			if ('' != $arItem['PRODUCT_PROVIDER_CLASS'] || '' != $arItem['CALLBACK_FUNC'])
			{
				$arItem["MODULE"] = strval($arItem["MODULE"]);
				$arItem['PRODUCT_ID'] = intval($arItem['PRODUCT_ID']);
				$arItem['QUANTITY'] = floatval($arItem['QUANTITY']);

				if ($productProvider = CSaleBasket::GetProductProvider($arItem))
				{
					$arFields = $productProvider::GetProductData(array(
						"PRODUCT_ID" => $arItem["PRODUCT_ID"],
						"QUANTITY"   => $arItem["QUANTITY"],
						"RENEWAL"    => "N",
						"CHECK_COUPONS" => ('Y' == $arItem['CAN_BUY'] && 'N' == $arItem['DELAY'] ? 'Y' : 'N'),
						"BASKET_ID" => $arItem["ID"],
						"NOTES" => $arItem["NOTES"]
					));
				}
				else
				{
					$arFields = CSaleBasket::ExecuteCallbackFunction(
						$arItem["CALLBACK_FUNC"],
						$arItem["MODULE"],
						$arItem["PRODUCT_ID"],
						$arItem["QUANTITY"],
						"N"
					);
				}

				if (!empty($arFields) && is_array($arFields))
				{
					$arFields["CAN_BUY"] = "Y";
				}
				else
				{
					$arFields = array('CAN_BUY' => 'N');
				}

				CSaleBasket::Update($arItem['ID'], $arFields);
			}
		}
	}
Esempio n. 10
0
 /**
  * @param Shipment $shipment
  * @return Result
  * @throws NotSupportedException
  */
 public static function deliverShipment(Shipment $shipment)
 {
     global $APPLICATION;
     $result = new Result();
     $needDeliver = $shipment->needDeliver();
     if ($needDeliver === null || $needDeliver === false && $shipment->getId() <= 0) {
         return $result;
     }
     $resultList = array();
     $shipmentItemCollection = $shipment->getShipmentItemCollection();
     $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
     $basketProviderMap = static::createProviderBasketMap($basketList, array('ORDER_ID', 'USER_ID', 'QUANTITY', 'PAID'));
     $basketProviderList = static::redistributeToProviders($basketProviderMap);
     if (!empty($basketProviderList)) {
         foreach ($basketProviderList as $provider => $providerBasketItemList) {
             if ($provider instanceof Provider) {
                 throw new NotSupportedException('provider not supported');
             } elseif ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider))) {
                 foreach ($providerBasketItemList as $providerBasketItem) {
                     if ($providerBasketItem['BASKET_ITEM']->isBundleParent()) {
                         continue;
                     }
                     if (!$providerBasketItem['BASKET_ITEM']->isEmptyItem()) {
                         $data = array("PRODUCT_ID" => $providerBasketItem["PRODUCT_ID"], "USER_ID" => $providerBasketItem["USER_ID"], "PAID" => $providerBasketItem["PAID"], "ORDER_ID" => $providerBasketItem["ORDER_ID"], "BASKET_ID" => $providerBasketItem['BASKET_ID']);
                         $APPLICATION->ResetException();
                         $resultProductData = $provider::DeliverProduct($data);
                         if ($ex = $APPLICATION->GetException()) {
                             $result->addError(new ResultError($ex->GetString(), $ex->GetID()));
                         }
                         if (!empty($resultProductData) && is_array($resultProductData)) {
                             $resultProductData['ORDER_ID'] = $providerBasketItem['ORDER_ID'];
                         }
                     } else {
                         $resultProductData = true;
                     }
                     $resultList[$providerBasketItem['BASKET_CODE']] = $resultProductData;
                 }
             } else {
                 foreach ($providerBasketItemList as $providerBasketItem) {
                     $resultProductData = \CSaleBasket::ExecuteCallbackFunction($providerBasketItem['CALLBACK_FUNC'], $providerBasketItem['MODULE'], $providerBasketItem['PRODUCT_ID'], $providerBasketItem['USER_ID'], $providerBasketItem["PAID"], $providerBasketItem['ORDER_ID'], $providerBasketItem["QUANTITY"]);
                     $basketCode = $providerBasketItem['BASKET_ITEM']->getBasketCode();
                     if (!empty($resultProductData) && is_array($resultProductData)) {
                         $resultProductData['ORDER_ID'] = $providerBasketItem['ORDER_ID'];
                     }
                     $resultList[$basketCode] = $resultProductData;
                 }
             }
         }
         if (!empty($resultList) && is_array($resultList)) {
             foreach ($resultList as $basketCode => $resultData) {
                 if ($resultData && !empty($resultData) && is_array($resultData)) {
                     $resultData["REMAINING_ATTEMPTS"] = defined("SALE_PROC_REC_ATTEMPTS") ? SALE_PROC_REC_ATTEMPTS : 3;
                     $resultData["SUCCESS_PAYMENT"] = "Y";
                     // TODO: recurringID
                     /*
                     if ($recurringID > 0)
                     	\CSaleRecurring::Update($recurringID, $resultData);
                     else
                     	\CSaleRecurring::Add($resultData);
                     */
                 }
                 /*
                 elseif ($recurringID > 0)
                 {
                 	\CSaleRecurring::Delete($recurringID);
                 }
                 */
             }
         }
     }
     if (!empty($resultList)) {
         $result->setData($resultList);
     }
     return $result;
 }
Esempio n. 11
0
}
$viewedMessage = '';
$basketMessage = '';
$viewedError = '';
$basketError = '';
//ACTIONS
//viewed
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'viewed_apply' && IntVal($_REQUEST["viewed_id"]) > 0 && strlen($_REQUEST["viewed_lid"]) > 0 && $saleModulePermissions >= "W") {
    $PRODUCT_ID = IntVal($_REQUEST["viewed_id"]);
    $LID = trim($_REQUEST["viewed_lid"]);
    if (CModule::IncludeModule("catalog")) {
        /** @var $productProvider IBXSaleProductProvider */
        if ($productProvider = CSaleBasket::GetProductProvider(array("MODULE" => "catalog", "PRODUCT_PROVIDER_CLASS" => "CCatalogProductProvider"))) {
            $arViews = $productProvider::GetProductData(array("PRODUCT_ID" => $PRODUCT_ID, "QUANTITY" => 1, "RENEWAL" => "N", "SITE_ID" => $LID));
        } else {
            $arViews = CSaleBasket::ExecuteCallbackFunction('CatalogBasketCallback', 'catalog', $PRODUCT_ID, 1, $LID);
        }
        if (count($arViews) <= 0) {
            $viewedError = GetMessage('BUYER_VIEWED_ERROR_PRODUCT_FAIL');
            $viewedMessage = "";
        } else {
            $dbProduct = CIBlockElement::GetList(array(), array("ID" => $PRODUCT_ID), false, false, array('ID', 'NAME', "XML_ID", "IBLOCK_EXTERNAL_ID"));
            $arProduct = $dbProduct->Fetch();
            $arViews['NAME'] = $arProduct["NAME"];
            $arViews['PRODUCT_ID'] = $arProduct["ID"];
            $arViews['MODULE'] = "catalog";
            $arViews["LID"] = $LID;
        }
    } else {
        $dbViewsList = CSaleViewedProduct::GetList(array(), array("FUSER_ID" => $arFuserItems["ID"], "PRODUCT_ID" => $PRODUCT_ID), false, false);
        $arViews = $dbViewsList->Fetch();
Esempio n. 12
0
 /**
  * <p>Метод добавляет товар в корзину, если его ещё нет, и обновляет параметры товара с увеличением количества, если он уже находится в корзине. В массиве <b>arFields</b> перечисляются все параметры товара, которые нужны для работы модуля Интернет-магазина (т.е. этот модуль не зависит от других модулей и работает полностью самостоятельно). Метод динамичный.</p> <p>Интернет-магазин не зависит от других модулей, поэтому товары в корзину модуля продаж могут добавляться из любого места (например, из торгового каталога или со статической страницы). Для некоторых модулей существуют функции - оболочки, облегчающие добавление товара в корзину (например, для модуля <b>catalog</b> существуют функции <b>Add2Basket</b> и <b>Add2BasketByProductID</b>). </p>
  *
  *
  * @param array $arFields  Ассоциативный массив параметров элемента корзины, содержащий
  * следующие ключи: <ul> <li> <b>PRODUCT_ID</b> - уникальный в рамках модуля код
  * товара (обязательное поле);</li> <li> <b>PRODUCT_PRICE_ID</b> - ID (идентификатор)
  * конкретного ценового предложения товара, пришедшего в корзину.
  * Может быть использован в классе <a
  * href="http://dev.1c-bitrix.ru/api_help/catalog/classes/cprice/index.php">CPrice</a> модуля Catalog для
  * получения детальной информации о цене.</li> <li> <b> PRICE</b> - стоимость
  * единицы товара (обязательное поле);</li> <li> <b>CURRENCY</b> - валюта
  * стоимости единицы товара (обязательное поле), если валюта
  * отличается от базовой валюты для данного сайта, то стоимость
  * будет автоматически сконвертирована по текущему курсу;</li> <li>
  * <b>WEIGHT</b> - вес единицы товара;</li> <li> <b>QUANTITY</b> - количество единиц
  * товара;</li> <li> <b>LID</b> - сайт, на котором сделана покупка
  * (обязательное поле);</li> <li> <b>DELAY</b> - флаг "товар отложен" (Y/N);</li> <li>
  * <b>CAN_BUY</b> - флаг "товар можно купить" (Y/N) - может устанавливаться
  * автоматически про наличии функции обратного вызова для
  * поддержки актуальности корзины;</li> <li> <b>NAME</b> - название товара
  * (обязательное поле);</li> <li> <b>PRODUCT_XML_ID</b> - внешний код товара
  * (необходим при обмене заказами с 1С);</li> <li> <b>CATALOG_XML_ID</b> - внешний
  * код каталога (необходим при обмене заказами с 1С);</li> <li> <b>ORDER_ID</b> -
  * идентификатор заказа. Ключ будет пустым, если товар еще не
  * добавлен в заказ;</li> <li> <b>CALLBACK_FUNC<font color="#FF0000">*</font></b> - название
  * функции обратного вызова для поддержки актуальности корзины
  * (описание ниже);</li> <li> <b>MODULE</b> - модуль, добавляющий товар в
  * корзину;</li> <li> <b>NOTES</b> - особые заметки, например, тип цены;</li> <li>
  * <b>ORDER_CALLBACK_FUNC<font color="#FF0000">*</font></b> - название функции обратного
  * вызова для оформления заказа (описание ниже);</li> <li> <b>DETAIL_PAGE_URL</b> -
  * ссылка на страницу детального просмотра товара;</li> <li>
  * <b>CANCEL_CALLBACK_FUNC<font color="#FF0000">*</font></b> - название функции обратного
  * вызова для отмены заказа (описание ниже);</li> <li> <b>PAY_CALLBACK_FUNC<font
  * color="#FF0000">*</font></b> - название функции обратного вызова, которая
  * вызывается при установке флага "Доставка разрешена" заказа;</li> <li>
  * <b>FUSER_ID</b> - идентификатор пользователя интернет-магазина,
  * необязательный параметр, по умолчанию CSaleBasket::GetBasketUserID() (текущий
  * пользователь);</li> <li> <b>DISCOUNT_PRICE</b> - величина скидки;</li> <li>
  * <b>DISCOUNT_NAME</b> - название скидки;</li> <li> <b>DISCOUNT_VALUE</b> - размер скидки (в
  * процентах);</li> <li> <b>DISCOUNT_COUPON</b> - купон скидки;</li> <li> <b>PROPS</b> - массив
  * свойств товара, который сохраняется в корзине. Каждый элемент
  * этого массива является массивом следующего формата: <pre
  * class="syntax"><code>array("NAME" =&gt; "Название свойства", "CODE" =&gt; "Код свойства",
  * "VALUE" =&gt; "Значение свойства", "SORT" =&gt; "Индекс сортировки")</code></pre> </li>
  * <li> <b>PRODUCT_PROVIDER_CLASS<font color="#0000FF">**</font></b> - содержит имя класса,
  * реализующего интерфейс <b> IBXSaleProductProvider</b>. Торговый каталог
  * записывает в это поле имя класса <b>CCatalogProductProvider</b>. Если поле
  * пусто, то возникает попытка использовать старые поля:<b>CALLBACK_FUNC</b>,
  * <b>ORDER_CALLBACK_FUNC</b>, <b>CANCEL_CALLBACK_FUNC</b> и <b>PAY_CALLBACK_FUNC</b>.</li> </ul>
  *
  * @return int <p>Метод возвращает код элемента корзины, в который попал данный
  * товар.</p> <a name="examples"></a>
  *
  * <h4>Example</h4> 
  * <pre>
  * &lt;?<br>if (CModule::IncludeModule("sale"))<br>{<br>  $arFields = array(<br>    "PRODUCT_ID" =&gt; 51,<br>    "PRODUCT_PRICE_ID" =&gt; 0,<br>    "PRICE" =&gt; 138.54,<br>    "CURRENCY" =&gt; "RUB",<br>    "WEIGHT" =&gt; 530,<br>    "QUANTITY" =&gt; 1,<br>    "LID" =&gt; LANG,<br>    "DELAY" =&gt; "N",<br>    "CAN_BUY" =&gt; "Y",<br>    "NAME" =&gt; "Чемодан кожаный",<br>    "CALLBACK_FUNC" =&gt; "MyBasketCallback",<br>    "MODULE" =&gt; "my_module",<br>    "NOTES" =&gt; "",<br>    "ORDER_CALLBACK_FUNC" =&gt; "MyBasketOrderCallback",<br>    "DETAIL_PAGE_URL" =&gt; "/".LANG."/detail.php?ID=51"<br>  );<br><br>  $arProps = array();<br><br>  $arProps[] = array(<br>    "NAME" =&gt; "Цвет",<br>    "CODE" =&gt; "color",<br>    "VALUE" =&gt; "черный"<br>  );<br><br>  $arProps[] = array(<br>    "NAME" =&gt; "Размер",<br>    "VALUE" =&gt; "1.5 x 2.5"<br>  );<br><br>  $arFields["PROPS"] = $arProps;<br><br>  CSaleBasket::Add($arFields);<br>}<br>?&gt;<br>
  * 
  * 
  * 
  * //пример на проверку создания покупателя
  * 
  * $userId=XXX;//id пользователя
  * //получаем FUSER_ID, если покупатель для данного пользователя существует
  * $FUSER_ID=CSaleUser::GetList(array('USER_ID' =&gt; $userId));
  * //если покупателя нет - создаем его
  * if(!$FUSER_ID['ID'])                  
  *        $FUSER_ID['ID']=CSaleUser::_Add(array("USER_ID" =&gt; $userId)); //обратите внимание на нижнее подчеркивание перед Add
  * //если не получается создать покупателя - то тут уж ничего не поделаешь
  * if(!$FUSER_ID['ID']){         
  *  echo "Error while creating SaleUser";      
  *  die();      
  * }
  * $FUSER_ID=$FUSER_ID['ID'];
  * //теперь переменную $FUSER_ID можно использовать для добавления товаров в корзину пользователя с $userId.
  * 
  * 
  * Смотрите также:
  * 
  * <li><a href="/api_help/catalog/interface.php">Взаимодействие торгового каталога и магазина</a></li>
  * </pre>
  *
  *
  * @static
  * @link http://dev.1c-bitrix.ru/api_help/sale/classes/csalebasket/csalebasket__add.php
  * @author Bitrix
  */
 public static function Add($arFields)
 {
     global $DB, $APPLICATION;
     if (isset($arFields["ID"])) {
         unset($arFields["ID"]);
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     CSaleBasket::Init();
     if (!CSaleBasket::CheckFields("ADD", $arFields)) {
         return false;
     }
     if (!array_key_exists('IGNORE_CALLBACK_FUNC', $arFields) || 'Y' != $arFields['IGNORE_CALLBACK_FUNC']) {
         if (array_key_exists("CALLBACK_FUNC", $arFields) && !empty($arFields["CALLBACK_FUNC"]) || array_key_exists("PRODUCT_PROVIDER_CLASS", $arFields) && !empty($arFields["PRODUCT_PROVIDER_CLASS"])) {
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider(array("MODULE" => $arFields["MODULE"], "PRODUCT_PROVIDER_CLASS" => $arFields["PRODUCT_PROVIDER_CLASS"]))) {
                 $providerParams = array("PRODUCT_ID" => $arFields["PRODUCT_ID"], "QUANTITY" => $arFields["QUANTITY"], "RENEWAL" => $arFields["RENEWAL"], "USER_ID" => isset($arFields["USER_ID"]) ? $arFields["USER_ID"] : 0, "SITE_ID" => isset($arFields["LID"]) ? $arFields["LID"] : false);
                 if (isset($arFields['NOTES'])) {
                     $providerParams['NOTES'] = $arFields['NOTES'];
                 }
                 if (!$productProvider::GetProductData($providerParams)) {
                     return false;
                 }
             } else {
                 if (!CSaleBasket::ExecuteCallbackFunction($arFields["CALLBACK_FUNC"], $arFields["MODULE"], $arFields["PRODUCT_ID"], $arFields["QUANTITY"], $arFields["RENEWAL"], $arFields["USER_ID"], $arFields["LID"])) {
                     return false;
                 }
             }
         }
     }
     if ($isOrderConverted != "Y") {
         foreach (GetModuleEvents("sale", "OnBeforeBasketAdd", true) as $arEvent) {
             if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) {
                 return false;
             }
         }
     }
     $bFound = false;
     $bEqAr = false;
     //TODO: is order converted?
     if ($isOrderConverted == "Y") {
         /** @var \Bitrix\Sale\BasketItem $basketItem */
         if (!($basketItem = \Bitrix\Sale\Compatible\BasketCompatibility::add($arFields))) {
             $APPLICATION->ThrowException(Localization\Loc::getMessage('BT_MOD_SALE_BASKET_ERR_ID_ABSENT'), "BASKET_ITEM");
             return false;
         }
         $ID = $basketItem->getId();
         $arFields['QUANTITY'] = $basketItem->getQuantity();
     } else {
         $boolProps = !empty($arFields["PROPS"]) && is_array($arFields["PROPS"]);
         // check if this item is already in the basket
         $arDuplicateFilter = array("FUSER_ID" => $arFields["FUSER_ID"], "PRODUCT_ID" => $arFields["PRODUCT_ID"], "LID" => $arFields["LID"], "ORDER_ID" => "NULL");
         if (!(isset($arFields["TYPE"]) && $arFields["TYPE"] == CSaleBasket::TYPE_SET)) {
             if (isset($arFields["SET_PARENT_ID"])) {
                 $arDuplicateFilter["SET_PARENT_ID"] = $arFields["SET_PARENT_ID"];
             } else {
                 $arDuplicateFilter["SET_PARENT_ID"] = "NULL";
             }
         }
         $db_res = CSaleBasket::GetList(array(), $arDuplicateFilter, false, false, array("ID", "QUANTITY"));
         while ($res = $db_res->Fetch()) {
             if (!$bEqAr) {
                 $arPropsCur = array();
                 $arPropsOld = array();
                 if ($boolProps) {
                     foreach ($arFields["PROPS"] as &$arProp) {
                         if (array_key_exists('VALUE', $arProp) && '' != $arProp["VALUE"]) {
                             $propID = '';
                             if (array_key_exists('CODE', $arProp) && '' != $arProp["CODE"]) {
                                 $propID = $arProp["CODE"];
                             } elseif (array_key_exists('NAME', $arProp) && '' != $arProp["NAME"]) {
                                 $propID = $arProp["NAME"];
                             }
                             if ('' == $propID) {
                                 continue;
                             }
                             $arPropsCur[$propID] = $arProp["VALUE"];
                         }
                     }
                     if (isset($arProp)) {
                         unset($arProp);
                     }
                 }
                 $dbProp = CSaleBasket::GetPropsList(array(), array("BASKET_ID" => $res["ID"]), false, false, array('NAME', 'VALUE', 'CODE'));
                 while ($arProp = $dbProp->Fetch()) {
                     if ('' != $arProp["VALUE"]) {
                         $propID = '';
                         if ('' != $arProp["CODE"]) {
                             $propID = $arProp["CODE"];
                         } elseif ('' != $arProp["NAME"]) {
                             $propID = $arProp["NAME"];
                         }
                         if ('' == $propID) {
                             continue;
                         }
                         $arPropsOld[$propID] = $arProp["VALUE"];
                     }
                 }
                 $bEqAr = false;
                 if (count($arPropsCur) == count($arPropsOld)) {
                     $bEqAr = true;
                     foreach ($arPropsCur as $key => $val) {
                         if (!array_key_exists($key, $arPropsOld) || $arPropsOld[$key] != $val) {
                             $bEqAr = false;
                             break;
                         }
                     }
                 }
                 if ($bEqAr) {
                     $ID = $res["ID"];
                     $arFields["QUANTITY"] += $res["QUANTITY"];
                     CSaleBasket::Update($ID, $arFields);
                     $bFound = true;
                     continue;
                 }
             }
         }
     }
     if (!$bFound) {
         //TODO: is order converted?
         if ($isOrderConverted != "Y") {
             $arInsert = $DB->PrepareInsert("b_sale_basket", $arFields);
             $strSql = "INSERT INTO b_sale_basket(" . $arInsert[0] . ", DATE_INSERT, DATE_UPDATE) VALUES(" . $arInsert[1] . ", " . $DB->GetNowFunction() . ", " . $DB->GetNowFunction() . ")";
             $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             $ID = intval($DB->LastID());
             $boolOrder = false;
             if (isset($arFields['ORDER_ID'])) {
                 $boolOrder = 0 < (int) $arFields['ORDER_ID'];
             }
             if (!$boolOrder && !CSaleBasketHelper::isSetItem($arFields)) {
                 $siteID = isset($arFields["LID"]) ? $arFields["LID"] : SITE_ID;
                 $_SESSION["SALE_BASKET_NUM_PRODUCTS"][$siteID]++;
             }
             if ($boolProps) {
                 foreach ($arFields["PROPS"] as &$prop) {
                     if ('' != $prop["NAME"]) {
                         $arInsert = $DB->PrepareInsert("b_sale_basket_props", $prop);
                         $strSql = "INSERT INTO b_sale_basket_props(BASKET_ID, " . $arInsert[0] . ") VALUES(" . $ID . ", " . $arInsert[1] . ")";
                         $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
                     }
                 }
                 if (isset($prop)) {
                     unset($prop);
                 }
             }
             // if item is set parent
             if (isset($arFields["TYPE"]) && $arFields["TYPE"] == CSaleBasket::TYPE_SET) {
                 CSaleBasket::Update($ID, array("SET_PARENT_ID" => $ID));
                 if (!isset($arFields["MANUAL_SET_ITEMS_INSERTION"])) {
                     /** @var $productProvider IBXSaleProductProvider */
                     if ($productProvider = CSaleBasket::GetProductProvider($arFields)) {
                         if (method_exists($productProvider, "GetSetItems")) {
                             $arSets = $productProvider::GetSetItems($arFields["PRODUCT_ID"], CSaleBasket::TYPE_SET, array('BASKET_ID' => $ID));
                             if (is_array($arSets)) {
                                 foreach ($arSets as $arSetData) {
                                     foreach ($arSetData["ITEMS"] as $setItem) {
                                         $setItem["SET_PARENT_ID"] = $ID;
                                         $setItem["LID"] = $arFields["LID"];
                                         $setItem["QUANTITY"] = $setItem["QUANTITY"] * $arFields["QUANTITY"];
                                         $setItem['FUSER_ID'] = $arFields['FUSER_ID'];
                                         CSaleBasket::Add($setItem);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if ($boolOrder) {
             CSaleOrderChange::AddRecord($arFields["ORDER_ID"], "BASKET_ADDED", array("PRODUCT_ID" => $arFields["PRODUCT_ID"], "NAME" => $arFields["NAME"], "QUANTITY" => $arFields["QUANTITY"]));
         }
     }
     if ($isOrderConverted != "Y") {
         foreach (GetModuleEvents("sale", "OnBasketAdd", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($ID, $arFields));
         }
     }
     return $ID;
 }
Esempio n. 13
0
 function UpdateBasketPrices($fuserID, $siteID)
 {
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         return false;
     }
     $siteID = (string) $siteID;
     if ($siteID == '') {
         $siteID = SITE_ID;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     DiscountCouponsManager::clearApply(false);
     $basketItems = array();
     /*
     if (!empty($arBasketItems) && is_array($arBasketItems))
     			{
     				foreach ($arBasketItems as &$basketItemData)
     				{
     					if (array_key_exists('MEASURE_RATIO', $basketItemData))
     					{
     						$basketItemQuantity = floatval($basketItemData['QUANTITY']);
     						$basketItemRatio = floatval($basketItemData['MEASURE_RATIO']);
     
     						$mod = roundEx(($basketItemQuantity / $basketItemRatio - intval($basketItemQuantity / $basketItemRatio)), 6);
     
     						if ($mod !== 0)
     						{
     							$basketItemData['QUANTITY'] = ceil(ceil($basketItemQuantity) / $basketItemRatio)* $basketItemRatio;
     							CSaleBasket::Update($basketItemData['ID'], array('QUANTITY' => $basketItemData['QUANTITY']));
     						}
     					}
     				}
     				unset($basketItemData);
     			}
     */
     $dbBasketItems = CSaleBasket::GetList(array("ALL_PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $siteID, "ORDER_ID" => "NULL", "SUBSCRIBE" => "N"), false, false, array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CALLBACK_FUNC", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbBasketItems->Fetch()) {
         $basketItems[] = $arItem;
     }
     if (!empty($basketItems) && is_array($basketItems)) {
         $basketItems = getRatio($basketItems);
         foreach ($basketItems as $basketItem) {
             $fields = array();
             $basketItem['CALLBACK_FUNC'] = (string) $basketItem['CALLBACK_FUNC'];
             $basketItem['PRODUCT_PROVIDER_CLASS'] = (string) $basketItem['PRODUCT_PROVIDER_CLASS'];
             if (strval(trim($basketItem['PRODUCT_PROVIDER_CLASS'])) !== '' || strval(trim($basketItem['CALLBACK_FUNC'])) !== '') {
                 $basketItem['MODULE'] = (string) $basketItem['MODULE'];
                 $basketItem['PRODUCT_ID'] = (int) $basketItem['PRODUCT_ID'];
                 $basketItem['QUANTITY'] = (double) $basketItem['QUANTITY'];
                 if ($productProvider = CSaleBasket::GetProductProvider($basketItem)) {
                     $fields = $productProvider::GetProductData(array("PRODUCT_ID" => $basketItem["PRODUCT_ID"], "QUANTITY" => $basketItem["QUANTITY"], "RENEWAL" => "N", "CHECK_COUPONS" => 'Y' == $basketItem['CAN_BUY'] && 'N' == $basketItem['DELAY'] ? 'Y' : 'N', "CHECK_DISCOUNT" => CSaleBasketHelper::isSetItem($basketItem) ? 'N' : 'Y', "BASKET_ID" => $basketItem["ID"], "NOTES" => $basketItem["NOTES"]));
                 } else {
                     $fields = CSaleBasket::ExecuteCallbackFunction($basketItem["CALLBACK_FUNC"], $basketItem["MODULE"], $basketItem["PRODUCT_ID"], $basketItem["QUANTITY"], "N");
                 }
                 if (!empty($fields) && is_array($fields)) {
                     if ($isOrderConverted == 'Y' && $basketItem['DELAY'] == 'N') {
                         if (!Sale\Compatible\DiscountCompatibility::isInited()) {
                             Sale\Compatible\DiscountCompatibility::init();
                         }
                         if (Sale\Compatible\DiscountCompatibility::usedByClient()) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemData($basketItem['ID'], $fields);
                         }
                     }
                     $fields['CAN_BUY'] = 'Y';
                     $fields['TYPE'] = (int) $basketItem['TYPE'];
                     $fields['SET_PARENT_ID'] = (int) $basketItem['SET_PARENT_ID'];
                 } else {
                     $fields = array('CAN_BUY' => 'N');
                 }
             }
             if (array_key_exists('MEASURE_RATIO', $basketItem)) {
                 $basketItemQuantity = floatval($basketItem['QUANTITY']);
                 $basketItemRatio = floatval($basketItem['MEASURE_RATIO']);
                 $mod = roundEx($basketItemQuantity / $basketItemRatio - intval($basketItemQuantity / $basketItemRatio), 6);
                 if ($mod != 0) {
                     $fields['QUANTITY'] = floor(ceil($basketItemQuantity) / $basketItemRatio) * $basketItemRatio;
                 }
             }
             if (!empty($fields) && is_array($fields)) {
                 CSaleBasket::Update($basketItem['ID'], $fields);
             }
         }
     }
     return true;
 }
Esempio n. 14
0
 function OrderCanceled($orderID, $bCancel)
 {
     global $DB;
     $orderID = IntVal($orderID);
     if ($orderID <= 0) {
         return False;
     }
     $bCancel = $bCancel ? True : False;
     $arOrder = CSaleOrder::GetByID($orderID);
     if ($arOrder) {
         $dbBasketList = CSaleBasket::GetList(array("NAME" => "ASC"), array("ORDER_ID" => $orderID));
         while ($arBasket = $dbBasketList->Fetch()) {
             if (strlen($arBasket["CANCEL_CALLBACK_FUNC"]) > 0 && strlen($arBasket["PRODUCT_PROVIDER_CLASS"]) <= 0) {
                 $arFields = CSaleBasket::ExecuteCallbackFunction($arBasket["CANCEL_CALLBACK_FUNC"], $arBasket["MODULE"], $arBasket["PRODUCT_ID"], $arBasket["QUANTITY"], $bCancel);
             }
         }
     }
 }