Esempio n. 1
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;
     }
 }
 /**
  * Save sale discount.
  *
  * @param array &$order				Current order data.
  * @param array $discount			Discount data.
  * @return bool
  */
 public static function calculateSaleDiscount(&$order, $discount)
 {
     if (!self::$init) {
         self::init();
     }
     if (!self::isSuccess()) {
         return false;
     }
     if (!empty($order['DISCOUNT_RESULT']) && is_array($order['DISCOUNT_RESULT'])) {
         $stepResult = self::getStepResult($order);
     } else {
         $stepResult = self::getStepResultOld($order);
     }
     $applied = !empty($stepResult);
     $orderDiscountId = 0;
     $orderCouponId = '';
     if ($applied) {
         self::correctStepResult($order, $stepResult, $discount);
         if (!empty($order['DISCOUNT_DESCR']) && is_array($order['DISCOUNT_DESCR'])) {
             $discount['ACTIONS_DESCR'] = $order['DISCOUNT_DESCR'];
         }
         $discountResult = self::convertDiscount($discount);
         if (!$discountResult->isSuccess()) {
             return false;
         }
         $orderDiscountId = $discountResult->getId();
         $discountData = $discountResult->getData();
         $discount['ORDER_DISCOUNT_ID'] = $orderDiscountId;
         if ($discountData['USE_COUPONS'] == 'Y') {
             if (empty($discount['COUPON'])) {
                 return false;
             }
             $couponResult = self::convertCoupon($discount['COUPON']['COUPON'], $orderDiscountId);
             if (!$couponResult->isSuccess()) {
                 return false;
             }
             $orderCouponId = $couponResult->getId();
             Sale\DiscountCouponsManager::setApply($orderCouponId, $stepResult);
             unset($couponResult);
         }
     }
     if (array_key_exists('DISCOUNT_DESCR', $order)) {
         unset($order['DISCOUNT_DESCR']);
     }
     if (array_key_exists('DISCOUNT_RESULT', $order)) {
         unset($order['DISCOUNT_RESULT']);
     }
     self::$currentOrderData = $order;
     if ($applied && $orderCouponId != '') {
         $couponApply = Sale\DiscountCouponsManager::setApply(self::$couponsCache[$orderCouponId]['COUPON'], $stepResult);
         unset($couponApply);
     }
     if ($applied) {
         self::$discountResult['ORDER'][] = array('DISCOUNT_ID' => $orderDiscountId, 'COUPON_ID' => $orderCouponId, 'RESULT' => $stepResult);
         return true;
     }
     return false;
 }
Esempio n. 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);
 }
Esempio n. 4
0
 /**
  * Calculate step discount result by exist order.
  *
  * @return Result
  */
 protected function calculateApplySaleDiscountResult()
 {
     $result = new Result();
     if (!empty($this->orderData['DISCOUNT_RESULT']) && is_array($this->orderData['DISCOUNT_RESULT'])) {
         $stepResult = self::getStepResult($this->orderData);
     } else {
         $stepResult = self::getStepResultOld($this->orderData, $this->currentStep['oldData']);
     }
     $applied = !empty($stepResult);
     $orderDiscountId = 0;
     $orderCouponId = '';
     if ($applied) {
         $this->correctStepResult($stepResult, $this->discountsCache[$this->currentStep['discountId']]);
         $orderDiscountId = $this->discountResult['ORDER'][$this->currentStep['discountIndex']]['DISCOUNT_ID'];
         $orderCouponId = $this->discountResult['ORDER'][$this->currentStep['discountIndex']]['COUPON_ID'];
     }
     if (array_key_exists('DISCOUNT_DESCR', $this->orderData)) {
         unset($this->orderData['DISCOUNT_DESCR']);
     }
     if (array_key_exists('DISCOUNT_RESULT', $this->orderData)) {
         unset($this->orderData['DISCOUNT_RESULT']);
     }
     if ($applied) {
         if (!empty($this->applyResult['DISCOUNT_LIST'][$orderDiscountId]) && $this->applyResult['DISCOUNT_LIST'][$orderDiscountId] == 'N' || $orderCouponId != '' && !empty($this->applyResult['COUPON_LIST'][$orderCouponId]) && $this->applyResult['COUPON_LIST'][$orderCouponId] == 'N') {
             $this->orderData = $this->currentStep['oldData'];
             if (!empty($stepResult['BASKET'])) {
                 foreach ($stepResult['BASKET'] as &$basketItem) {
                     $basketItem['APPLY'] = 'N';
                 }
                 unset($basketItem);
             }
             if (!empty($stepResult['DELIVERY'])) {
                 $stepResult['DELIVERY']['APPLY'] = 'N';
             }
         } else {
             if (!empty($this->discountResult['ORDER'][$this->currentStep['discountIndex']]['RESULT'])) {
                 $existDiscountResult = $this->discountResult['ORDER'][$this->currentStep['discountIndex']]['RESULT'];
                 if (!empty($existDiscountResult['BASKET'])) {
                     $basketCodeList = $this->getBasketCodes(false);
                     if (!empty($basketCodeList)) {
                         foreach ($basketCodeList as &$basketCode) {
                             if ($this->isCustomPriceByCode($basketCode)) {
                                 continue;
                             }
                             if (isset($existDiscountResult['BASKET'][$basketCode])) {
                                 $disable = $existDiscountResult['BASKET'][$basketCode]['APPLY'] == 'N';
                                 if (isset($this->applyResult['BASKET'][$basketCode][$orderDiscountId])) {
                                     $applyDisable = $this->applyResult['BASKET'][$basketCode][$orderDiscountId] == 'N';
                                     if ($disable != $applyDisable) {
                                         $disable = $applyDisable;
                                     }
                                     unset($applyDisable);
                                 }
                                 if ($disable) {
                                     $stepResult['BASKET'][$basketCode]['APPLY'] = 'N';
                                     $this->orderData['BASKET_ITEMS'][$basketCode] = $this->currentStep['oldData']['BASKET_ITEMS'][$basketCode];
                                 } else {
                                     $stepResult['BASKET'][$basketCode]['APPLY'] = 'Y';
                                 }
                             }
                         }
                         unset($disable, $basketCode);
                     }
                 }
                 if (!empty($existDiscountResult['DELIVERY'])) {
                     $disable = $existDiscountResult['DELIVERY']['APPLY'] == 'N';
                     if (!empty($this->applyResult['DELIVERY'][$orderDiscountId])) {
                         $applyDisable = $this->applyResult['DELIVERY'][$orderDiscountId] == 'N';
                         if ($disable != $applyDisable) {
                             $disable = $applyDisable;
                         }
                         unset($applyDisable);
                     }
                     if ($disable) {
                         $this->orderData['PRICE_DELIVERY'] = $this->currentStep['oldData']['PRICE_DELIVERY'];
                         $this->orderData['PRICE_DELIVERY_DIFF'] = $this->currentStep['oldData']['PRICE_DELIVERY_DIFF'];
                         $stepResult['DELIVERY']['APPLY'] = 'N';
                     } else {
                         $stepResult['DELIVERY']['APPLY'] = 'Y';
                     }
                     unset($disable);
                 }
             }
         }
     }
     if ($applied && $orderCouponId != '') {
         $couponApply = DiscountCouponsManager::setApply($this->couponsCache[$orderCouponId]['COUPON'], $stepResult);
         unset($couponApply);
     }
     if ($applied) {
         $this->mergeDiscountActionResult($this->currentStep['discountIndex'], $stepResult);
     } else {
         if (!empty($this->discountResult['ORDER'][$this->currentStep['discountIndex']])) {
             $this->discountResult['ORDER'][$this->currentStep['discountIndex']]['RESULT'] = array();
         }
     }
     return $result;
 }