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; } }
/** * Load from database discount id for user groups. * * @return void * @throws Main\ArgumentException */ protected function loadDiscountByUserGroups() { if (!array_key_exists('USER_ID', $this->orderData)) { return; } $userGroups = \CUser::getUserGroup($this->orderData['USER_ID']); Main\Type\Collection::normalizeArrayValuesByInt($userGroups); $cacheKey = md5('U' . implode('_', $userGroups)); if (!isset($this->discountByUserCache[$cacheKey])) { $discountCache = array(); $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => $userGroups, '=ACTIVE' => 'Y'), 'order' => array('DISCOUNT_ID' => 'ASC'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $discountCache[$groupDiscount['DISCOUNT_ID']] = $groupDiscount['DISCOUNT_ID']; } } unset($groupDiscount, $groupDiscountIterator); if (!empty($discountCache)) { $this->discountByUserCache[$cacheKey] = $discountCache; } unset($discountCache); } $this->discountIds = $this->discountByUserCache[$cacheKey]; }
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); }
/** * Calculate discount by new order. * * @return Result */ protected function calculateFull() { $result = new Result(); $this->discountIds = array(); if (!$this->isMixedBasket()) { $this->fillEmptyDiscountResult(); } else { $this->discountResult['ORDER'] = array(); } $order = $this->getOrder(); $basket = $order->getBasket(); /** @var BasketItem $basketItem */ foreach ($basket as $basketItem) { $code = $basketItem->getBasketCode(); if ($this->isCustomPriceByCode($code)) { if (array_key_exists($code, $this->basketDiscountList)) { unset($this->basketDiscountList[$code]); } } else { if (!isset($this->basketDiscountList[$code])) { $this->basketDiscountList[$code] = $basketItem->getField('DISCOUNT_LIST'); if ($this->basketDiscountList[$code] === null) { unset($this->basketDiscountList[$code]); } } } } unset($code, $basketItem, $basket); $this->resetBasketPrices(); if ($this->isMixedBasket()) { DiscountCouponsManager::clearApply(false); $basketDiscountResult = $this->calculateMixedBasketDiscount(); } else { DiscountCouponsManager::clearApply(); $basketDiscountResult = $this->calculateFullBasketDiscount(); } if (!$basketDiscountResult->isSuccess()) { $result->addErrors($basketDiscountResult->getErrors()); unset($basketDiscountResult); return $result; } unset($basketDiscountResult); $this->roundBasketPrices(); $codeList = array_keys($this->orderData['BASKET_ITEMS']); switch (self::getApplyMode()) { case self::APPLY_MODE_DISABLE: foreach ($codeList as &$code) { if (isset($this->basketDiscountList[$code]) && !empty($this->basketDiscountList[$code])) { $this->orderData['BASKET_ITEMS'][$code]['LAST_DISCOUNT'] = 'Y'; } } unset($code); break; case self::APPLY_MODE_LAST: foreach ($codeList as &$code) { if (!isset($this->basketDiscountList[$code]) || empty($this->basketDiscountList[$code])) { continue; } $lastDiscount = end($this->basketDiscountList[$code]); if (!empty($lastDiscount['LAST_DISCOUNT']) && $lastDiscount['LAST_DISCOUNT'] == 'Y') { $this->orderData['BASKET_ITEMS'][$code]['LAST_DISCOUNT'] = 'Y'; } } unset($code); break; case self::APPLY_MODE_ADD: break; } unset($codeList); $userGroups = \CUser::getUserGroup($this->orderData['USER_ID']); Main\Type\Collection::normalizeArrayValuesByInt($userGroups); $cacheKey = md5('U' . implode('_', $userGroups)); if (!isset($this->discountByUserCache[$cacheKey])) { $discountCache = array(); $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => $userGroups, '=ACTIVE' => 'Y'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $discountCache[$groupDiscount['DISCOUNT_ID']] = $groupDiscount['DISCOUNT_ID']; } } unset($groupDiscount, $groupDiscountIterator); if (!empty($discountCache)) { Main\Type\Collection::normalizeArrayValuesByInt($discountCache); $this->discountByUserCache[$cacheKey] = $discountCache; } unset($discountCache); } if (!empty($this->discountByUserCache[$cacheKey])) { $this->discountIds = $this->discountByUserCache[$cacheKey]; } if (empty($this->discountIds)) { $this->discountIds = null; } else { $this->getDiscountModules(); $couponList = DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $this->discountIds), array(), true); $cacheKey = md5('D' . implode('_', $this->discountIds)); if (!empty($couponList)) { $cacheKey .= '-C' . implode('_', array_keys($couponList)); } $currentList = array(); if (!isset($this->saleDiscountCache[$cacheKey])) { $discountApply = array(); $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' => $this->discountIds, '=LID' => $this->orderData['SITE_ID'], '@EXECUTE_MODULE' => array('sale', 'all'), array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime)); unset($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)); while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; if (isset($discountApply[$discount['ID']])) { continue; } $discountApply[$discount['ID']] = true; if (!$this->loadDiscountModules('sale' . $discount['ID'])) { continue; } if ($discount['USE_COUPONS'] == 'Y') { $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']]; } $discount['CONDITIONS'] = $discount['CONDITIONS_LIST']; $discount['ACTIONS'] = $discount['ACTIONS_LIST']; $discount['MODULE_ID'] = 'sale'; if (isset($this->cacheDiscountModules['sale' . $discount['ID']])) { $discount['MODULES'] = $this->cacheDiscountModules['sale' . $discount['ID']]; } unset($discount['ACTIONS_LIST'], $discount['CONDITIONS_LIST']); $currentList[] = $discount; } unset($discount, $discountIterator, $discountApply); $this->saleDiscountCache[$cacheKey] = $currentList; } else { $currentList = $this->saleDiscountCache[$cacheKey]; } unset($couponList); $this->discountIds = array(); if (!empty($currentList)) { foreach ($currentList as &$discount) { $this->discountIds[] = $discount['ID']; } unset($discount); $this->extendOrderData(); foreach ($currentList as $indexDiscount => $discount) { $this->fillCurrentStep(array('discount' => $discount)); if (!self::checkDiscountConditions()) { continue; } $actionsResult = $this->applySaleDiscount(); if (!$actionsResult->isSuccess()) { $result->addErrors($actionsResult->getErrors()); unset($actionsResult); return $result; } if ($this->currentStep['stop']) { break; } } unset($discount, $indexDiscount, $currentList); } $this->fillEmptyCurrentStep(); } return $result; }