Beispiel #1
0
 public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors)
 {
     global $DB;
     $arIDS = array();
     $rsDiscountIDs = CSaleDiscount::GetDiscountGroupList(array(), array('GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID'])), false, false, array('DISCOUNT_ID'));
     while ($arDiscountID = $rsDiscountIDs->Fetch()) {
         $arDiscountID['DISCOUNT_ID'] = (int) $arDiscountID['DISCOUNT_ID'];
         if (0 < $arDiscountID['DISCOUNT_ID']) {
             $arIDS[$arDiscountID['DISCOUNT_ID']] = true;
         }
     }
     if (!empty($arIDS)) {
         if (isset($arOrder['BASKET_ITEMS']) && !empty($arOrder['BASKET_ITEMS']) && is_array($arOrder['BASKET_ITEMS'])) {
             $arExtend = array('catalog' => array('fields' => true, 'props' => true));
             foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) {
                 ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend));
             }
             foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) {
                 if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC'])) {
                     if (isset($arOneItem['DISCOUNT_PRICE'])) {
                         $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE'];
                         $arOneItem['DISCOUNT_PRICE'] = 0;
                     }
                 }
             }
             if (isset($arOneItem)) {
                 unset($arOneItem);
             }
         }
         $arIDS = array_keys($arIDS);
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS);
         } else {
             $needDiscountHandlers = array();
             foreach ($arIDS as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         $strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
         CTimeZone::Disable();
         $rsDiscounts = CSaleDiscount::GetList(array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC'), array('ID' => $arIDS, 'LID' => $arOrder['SITE_ID'], 'ACTIVE' => 'Y', '!>ACTIVE_FROM' => $strDate, '!<ACTIVE_TO' => $strDate), false, false, array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION'));
         CTimeZone::Enable();
         $discountApply = array();
         $resultDiscountList = array();
         $resultDiscountKeys = array();
         $resultDiscountIndex = 0;
         while ($arDiscount = $rsDiscounts->Fetch()) {
             if (!isset($discountApply[$arDiscount['ID']])) {
                 $discountApply[$arDiscount['ID']] = true;
                 $applyFlag = true;
                 if (isset(self::$cacheDiscountHandlers[$arDiscount['ID']])) {
                     $moduleList = self::$cacheDiscountHandlers[$arDiscount['ID']]['MODULES'];
                     if (!empty($moduleList)) {
                         foreach ($moduleList as &$moduleID) {
                             if (!isset(self::$usedModules[$moduleID])) {
                                 self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                             }
                             if (!self::$usedModules[$moduleID]) {
                                 $applyFlag = false;
                                 break;
                             }
                         }
                         unset($moduleID);
                     }
                     unset($moduleList);
                 }
                 if ($applyFlag && self::__Unpack($arOrder, $arDiscount['UNPACK'])) {
                     $oldOrder = $arOrder;
                     self::__ApplyActions($arOrder, $arDiscount['APPLICATION']);
                     $discountResult = self::getDiscountResult($oldOrder, $arOrder, false);
                     if (!empty($discountResult)) {
                         $resultDiscountList[$resultDiscountIndex] = array('ID' => $arDiscount['ID'], 'PRIORITY' => $arDiscount['PRIORITY'], 'SORT' => $arDiscount['SORT'], 'LAST_DISCOUNT' => $arDiscount['LAST_DISCOUNT'], 'UNPACK' => $arDiscount['UNPACK'], 'APPLICATION' => $arDiscount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$arDiscount['ID']]);
                         $resultDiscountKeys[$arDiscount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($arDiscount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     unset($discountResult);
                 }
             }
         }
         unset($arDiscount, $rsDiscounts);
         if ($resultDiscountIndex > 0) {
             $rsDiscounts = CSaleDiscount::GetList(array(), array('ID' => array_keys($resultDiscountKeys)), false, false, array('ID', 'NAME', 'CONDITIONS', 'ACTIONS'));
             while ($arDiscount = $rsDiscounts->Fetch()) {
                 $arDiscount['ID'] = (int) $arDiscount['ID'];
                 if (isset($resultDiscountKeys[$arDiscount['ID']])) {
                     $key = $resultDiscountKeys[$arDiscount['ID']];
                     $resultDiscountList[$key]['NAME'] = $arDiscount['NAME'];
                     $resultDiscountList[$key]['CONDITIONS'] = $arDiscount['CONDITIONS'];
                     $resultDiscountList[$key]['ACTIONS'] = $arDiscount['ACTIONS'];
                 }
             }
         }
         $arOrder["ORDER_PRICE"] = 0;
         $arOrder["ORDER_WEIGHT"] = 0;
         $arOrder["USE_VAT"] = false;
         $arOrder["VAT_RATE"] = 0;
         $arOrder["VAT_SUM"] = 0;
         $arOrder["DISCOUNT_PRICE"] = 0.0;
         $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"];
         $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"];
         foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) {
             if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) {
                 $arOrder["ORDER_PRICE"] += doubleval($arShoppingCartItem["PRICE"]) * doubleval($arShoppingCartItem["QUANTITY"]);
                 $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"];
                 $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
                 if ($arShoppingCartItem["VAT_RATE"] > 0) {
                     $arOrder["USE_VAT"] = true;
                     if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) {
                         $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"];
                     }
                     $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"];
                 }
             }
         }
         if (isset($arShoppingCartItem)) {
             unset($arShoppingCartItem);
         }
     }
 }
Beispiel #2
0
 public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors)
 {
     if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) {
         return;
     }
     $arIDS = array();
     $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y')));
     while ($groupDiscount = $groupDiscountIterator->fetch()) {
         $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID'];
         if ($groupDiscount['DISCOUNT_ID'] > 0) {
             $arIDS[$groupDiscount['DISCOUNT_ID']] = true;
         }
     }
     if (!empty($arIDS)) {
         $arIDS = array_keys($arIDS);
         $couponList = DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true);
         $arExtend = array('catalog' => array('fields' => true, 'props' => true));
         foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend));
         }
         foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) {
             if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) {
                 if (isset($arOneItem['DISCOUNT_PRICE'])) {
                     $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE'];
                     $arOneItem['DISCOUNT_PRICE'] = 0;
                 }
             }
         }
         if (isset($arOneItem)) {
             unset($arOneItem);
         }
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS);
         } else {
             $needDiscountHandlers = array();
             foreach ($arIDS as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         CTimeZone::Disable();
         $currentDatetime = new Type\DateTime();
         $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS');
         $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC');
         $discountFilter = array('@ID' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime));
         if (empty($couponList)) {
             $discountFilter['=USE_COUPONS'] = 'N';
         } else {
             $discountFilter[] = array('LOGIC' => 'OR', '=USE_COUPONS' => 'N', array('=USE_COUPONS' => 'Y', '=COUPON.COUPON' => array_keys($couponList)));
             $discountSelect['DISCOUNT_COUPON'] = 'COUPON.COUPON';
         }
         $discountIterator = Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder));
         $discountApply = array();
         $resultDiscountList = array();
         $resultDiscountKeys = array();
         $resultDiscountIndex = 0;
         while ($discount = $discountIterator->fetch()) {
             $discount['ID'] = (int) $discount['ID'];
             if (isset($discountApply[$discount['ID']])) {
                 continue;
             }
             $discount['MODULE'] = 'sale';
             $discountApply[$discount['ID']] = true;
             $applyFlag = true;
             if (isset(self::$cacheDiscountHandlers[$discount['ID']])) {
                 $moduleList = self::$cacheDiscountHandlers[$discount['ID']]['MODULES'];
                 if (!empty($moduleList)) {
                     foreach ($moduleList as &$moduleID) {
                         if (!isset(self::$usedModules[$moduleID])) {
                             self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                         }
                         if (!self::$usedModules[$moduleID]) {
                             $applyFlag = false;
                             break;
                         }
                     }
                     unset($moduleID);
                 }
                 unset($moduleList);
             }
             if ($applyFlag && self::__Unpack($arOrder, $discount['UNPACK'])) {
                 $oldOrder = $arOrder;
                 self::__ApplyActions($arOrder, $discount['APPLICATION']);
                 $discountResult = self::getDiscountResult($oldOrder, $arOrder, false);
                 if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) {
                     if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) {
                         if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Internals\DiscountCouponTable::TYPE_BASKET_ROW) {
                             self::changeDiscountResult($oldOrder, $arOrder, $discountResult);
                         }
                         $couponApply = DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult);
                     }
                     $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'UNPACK' => $discount['UNPACK'], 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                     $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                     $resultDiscountIndex++;
                     if ($discount['LAST_DISCOUNT'] == 'Y') {
                         break;
                     }
                 }
                 unset($discountResult);
             }
         }
         unset($discount, $discountIterator);
         CTimeZone::Enable();
         if ($resultDiscountIndex > 0) {
             $discountIterator = Internals\DiscountTable::getList(array('select' => array('ID', 'NAME', 'CONDITIONS', 'ACTIONS'), 'filter' => array('@ID' => array_keys($resultDiscountKeys))));
             while ($discount = $discountIterator->fetch()) {
                 $discount['ID'] = (int) $discount['ID'];
                 if (isset($resultDiscountKeys[$discount['ID']])) {
                     $key = $resultDiscountKeys[$discount['ID']];
                     $resultDiscountList[$key]['NAME'] = $discount['NAME'];
                     $resultDiscountList[$key]['CONDITIONS'] = $discount['CONDITIONS'];
                     $resultDiscountList[$key]['ACTIONS'] = $discount['ACTIONS'];
                 }
             }
             unset($discount, $discountIterator);
         }
         $arOrder["ORDER_PRICE"] = 0;
         $arOrder["ORDER_WEIGHT"] = 0;
         $arOrder["USE_VAT"] = false;
         $arOrder["VAT_RATE"] = 0;
         $arOrder["VAT_SUM"] = 0;
         $arOrder["DISCOUNT_PRICE"] = 0.0;
         $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"];
         $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"];
         foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) {
             if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) {
                 $arOrder["ORDER_PRICE"] += doubleval($arShoppingCartItem["PRICE"]) * doubleval($arShoppingCartItem["QUANTITY"]);
                 $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"];
                 $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0;
                 if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) {
                     $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
                 }
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
                 if ($arShoppingCartItem["VAT_RATE"] > 0) {
                     $arOrder["USE_VAT"] = true;
                     if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) {
                         $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"];
                     }
                     $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"];
                 }
             }
         }
         if (isset($arShoppingCartItem)) {
             unset($arShoppingCartItem);
         }
         $arOrder['DISCOUNT_LIST'] = $resultDiscountList;
     }
 }
Beispiel #3
0
 public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors)
 {
     if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) {
         return;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $oldDelivery = '';
     $checkIds = true;
     $arIDS = array();
     if ($isOrderConverted == 'Y') {
         if (isset($arOrder['DELIVERY_ID']) && $arOrder['DELIVERY_ID'] != '') {
             $oldDelivery = $arOrder['DELIVERY_ID'];
             $arOrder['DELIVERY_ID'] = \CSaleDelivery::getIdByCode($arOrder['DELIVERY_ID']);
         }
         $adminSection = defined('ADMIN_SECTION') && ADMIN_SECTION === true;
         if (!$adminSection) {
             $mode = Sale\Compatible\DiscountCompatibility::MODE_MANAGER;
             $modeParams = array();
             if (isset($arOrder['CURRENCY'])) {
                 $modeParams['CURRENCY'] = $arOrder['CURRENCY'];
             }
             if (isset($arOrder['SITE_ID'])) {
                 $modeParams['SITE_ID'] = $arOrder['SITE_ID'];
                 if (!isset($modeParams['CURRENCY'])) {
                     $modeParams['CURRENCY'] = Sale\Internals\SiteCurrencyTable::getSiteCurrency($modeParams['SITE_ID']);
                 }
             }
         } else {
             $mode = Sale\Compatible\DiscountCompatibility::MODE_CLIENT;
             $modeParams = array('SITE_ID' => SITE_ID, 'CURRENCY' => Sale\Internals\SiteCurrencyTable::getSiteCurrency(SITE_ID));
         }
         unset($adminSection);
         if (!empty($modeParams)) {
             Sale\Discount\Actions::setUseMode(Sale\Discount\Actions::MODE_CALCULATE, array('USE_BASE_PRICE' => \Bitrix\Main\Config\Option::get('sale', 'get_discount_percent_from_base_price'), 'SITE_ID' => $modeParams['SITE_ID'], 'CURRENCY' => $modeParams['CURRENCY']));
         }
         if (!Sale\Compatible\DiscountCompatibility::isInited()) {
             if (!empty($modeParams)) {
                 Sale\Compatible\DiscountCompatibility::init($mode, $modeParams);
             }
         }
         unset($modeParams, $mode);
         Sale\Compatible\DiscountCompatibility::clearDiscountResult();
         Sale\Compatible\DiscountCompatibility::fillBasketData($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::calculateBasketDiscounts($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::setApplyMode($arOrder['BASKET_ITEMS']);
         $applyMode = Sale\Discount::getApplyMode();
         if ($applyMode == Sale\Discount::APPLY_MODE_FULL_LAST || $applyMode == Sale\Discount::APPLY_MODE_FULL_DISABLE) {
             foreach ($arOrder['BASKET_ITEMS'] as &$basketItem) {
                 if (isset($basketItem['LAST_DISCOUNT']) && $basketItem['LAST_DISCOUNT'] == 'Y') {
                     $checkIds = false;
                     break;
                 }
             }
             unset($basketItem);
         }
     }
     if ($checkIds) {
         $groupDiscountIterator = Sale\Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y')));
         while ($groupDiscount = $groupDiscountIterator->fetch()) {
             $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID'];
             if ($groupDiscount['DISCOUNT_ID'] > 0) {
                 $arIDS[$groupDiscount['DISCOUNT_ID']] = true;
             }
         }
     }
     if (!empty($arIDS)) {
         $arIDS = array_keys($arIDS);
         $couponList = Sale\DiscountCouponsManager::getForApply(array('MODULE_ID' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true);
         //TODO: fix this condition
         $useProps = true;
         $iblockPropList = array();
         $entityList = Sale\Internals\DiscountEntitiesTable::getByDiscount($arIDS, array('=MODULE_ID' => 'catalog', '=ENTITY' => 'ELEMENT_PROPERTY'));
         if (empty($entityList)) {
             $useProps = false;
         } else {
             if (empty($entityList['catalog']['ELEMENT_PROPERTY'])) {
                 $useProps = false;
             } else {
                 foreach ($entityList['catalog']['ELEMENT_PROPERTY'] as $entity) {
                     $entityField = explode(':', $entity['FIELD_TABLE']);
                     if (isset($entityField[1])) {
                         $propId = (int) $entityField[1];
                         if ($propId > 0) {
                             $iblockPropList[$propId] = $propId;
                         }
                         unset($propId);
                     }
                     unset($entityField);
                 }
                 unset($entity);
                 if (empty($iblockPropList)) {
                     $useProps = false;
                 }
             }
         }
         $arExtend = array('catalog' => array('fields' => true, 'props' => $useProps));
         if ($useProps) {
             $arExtend['iblock']['props'] = $iblockPropList;
         }
         unset($iblockPropList, $useProps);
         foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend));
         }
         foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) {
             if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) {
                 if (isset($arOneItem['DISCOUNT_PRICE'])) {
                     $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE'];
                     $arOneItem['DISCOUNT_PRICE'] = 0;
                     $arOneItem['BASE_PRICE'] = $arOneItem['PRICE'];
                 }
             }
         }
         if (isset($arOneItem)) {
             unset($arOneItem);
         }
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS);
         } else {
             $needDiscountHandlers = array();
             foreach ($arIDS as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         $currentDatetime = new Main\Type\DateTime();
         $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS', 'EXECUTE_MODULE', 'NAME', 'CONDITIONS_LIST', 'ACTIONS_LIST');
         $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC');
         $discountFilter = array('@ID' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime));
         if (empty($couponList)) {
             $discountFilter['=USE_COUPONS'] = 'N';
         } else {
             $discountFilter[] = array('LOGIC' => 'OR', '=USE_COUPONS' => 'N', array('=USE_COUPONS' => 'Y', '=COUPON.COUPON' => array_keys($couponList)));
             $discountSelect['DISCOUNT_COUPON'] = 'COUPON.COUPON';
         }
         $discountIterator = Sale\Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder));
         $discountApply = array();
         $resultDiscountFullList = array();
         $resultDiscountList = array();
         $resultDiscountKeys = array();
         $resultDiscountIndex = 0;
         while ($discount = $discountIterator->fetch()) {
             $discount['ID'] = (int) $discount['ID'];
             if (isset($discountApply[$discount['ID']])) {
                 continue;
             }
             $discount['MODULE'] = 'sale';
             $discount['MODULE_ID'] = 'sale';
             if ($discount['USE_COUPONS'] == 'Y') {
                 $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']];
             }
             $discountApply[$discount['ID']] = true;
             $applyFlag = true;
             if (isset(self::$cacheDiscountHandlers[$discount['ID']])) {
                 $moduleList = self::$cacheDiscountHandlers[$discount['ID']]['MODULES'];
                 if (!empty($moduleList)) {
                     foreach ($moduleList as &$moduleID) {
                         if (!isset(self::$usedModules[$moduleID])) {
                             self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                         }
                         if (!self::$usedModules[$moduleID]) {
                             $applyFlag = false;
                             break;
                         }
                     }
                     unset($moduleID);
                     if ($applyFlag) {
                         $discount['MODULES'] = $moduleList;
                     }
                 }
                 unset($moduleList);
             }
             if ($isOrderConverted == 'Y') {
                 Sale\Compatible\DiscountCompatibility::setOrderData($arOrder);
             }
             if ($applyFlag && self::__Unpack($arOrder, $discount['UNPACK'])) {
                 $oldOrder = $arOrder;
                 if ($isOrderConverted == 'Y') {
                     Sale\Discount\Actions::clearAction();
                 }
                 self::__ApplyActions($arOrder, $discount['APPLICATION']);
                 if ($isOrderConverted == 'Y') {
                     $resultDiscountFullList[] = $discount;
                     if (Sale\Compatible\DiscountCompatibility::calculateSaleDiscount($arOrder, $discount)) {
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => self::getDiscountResult($oldOrder, $arOrder, false), 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     Sale\Discount\Actions::clearAction();
                 } else {
                     $discountResult = self::getDiscountResult($oldOrder, $arOrder, false);
                     if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) {
                         if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) {
                             if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Sale\Internals\DiscountCouponTable::TYPE_BASKET_ROW) {
                                 self::changeDiscountResult($oldOrder, $arOrder, $discountResult);
                             }
                             $couponApply = Sale\DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult);
                             unset($couponApply);
                         }
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     unset($discountResult);
                 }
             }
         }
         unset($discount, $discountIterator);
         $arOrder["ORDER_PRICE"] = 0;
         $arOrder["ORDER_WEIGHT"] = 0;
         $arOrder["USE_VAT"] = false;
         $arOrder["VAT_RATE"] = 0;
         $arOrder["VAT_SUM"] = 0;
         $arOrder["DISCOUNT_PRICE"] = 0.0;
         $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"];
         $arOrder["PRICE_DELIVERY"] = roundEx($arOrder["PRICE_DELIVERY"], SALE_VALUE_PRECISION);
         $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"];
         foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) {
             if (isset($arShoppingCartItem['CATALOG'])) {
                 unset($arShoppingCartItem['CATALOG']);
             }
             if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) {
                 $customPrice = isset($arShoppingCartItem['CUSTOM_PRICE']) && ($arShoppingCartItem['CUSTOM_PRICE'] = 'Y');
                 if (!$customPrice) {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = roundEx($arShoppingCartItem['DISCOUNT_PRICE'], SALE_VALUE_PRECISION);
                     if ($arShoppingCartItem['DISCOUNT_PRICE'] > 0) {
                         $arShoppingCartItem['PRICE'] = $arShoppingCartItem['BASE_PRICE'] - $arShoppingCartItem['DISCOUNT_PRICE'];
                     } else {
                         $arShoppingCartItem['PRICE'] = roundEx($arShoppingCartItem['PRICE'], SALE_VALUE_PRECISION);
                     }
                 } else {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = 0;
                 }
                 if (isset($arShoppingCartItem['VAT_RATE'])) {
                     $vatRate = (double) $arShoppingCartItem['VAT_RATE'];
                     if ($vatRate > 0) {
                         $arShoppingCartItem['VAT_VALUE'] = $arShoppingCartItem['PRICE'] / ($vatRate + 1) * $vatRate;
                     }
                     unset($vatRate);
                 }
                 $arOrder["ORDER_PRICE"] += $arShoppingCartItem["PRICE"] * $arShoppingCartItem["QUANTITY"];
                 $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"];
                 $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0;
                 if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) {
                     $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
                 }
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
                 if ($arShoppingCartItem["VAT_RATE"] > 0) {
                     $arOrder["USE_VAT"] = true;
                     if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) {
                         $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"];
                     }
                     $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"];
                 }
             }
         }
         unset($arShoppingCartItem);
         $arOrder['DISCOUNT_LIST'] = $resultDiscountList;
         $arOrder['FULL_DISCOUNT_LIST'] = $resultDiscountFullList;
         if ($isOrderConverted == 'Y') {
             Sale\Compatible\DiscountCompatibility::setOldDiscountResult($resultDiscountList);
         }
     }
     if ($isOrderConverted == 'Y' && $oldDelivery != '') {
         $arOrder['DELIVERY_ID'] = $oldDelivery;
     }
     $arOrder["ORDER_PRICE"] = roundEx($arOrder["ORDER_PRICE"], SALE_VALUE_PRECISION);
 }