• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/discountbase.php
  • Класс: BitrixSaleDiscountBase
  • Вызов: DiscountBase::calculateFullBasketDiscount
protected function calculateFullBasketDiscount()
{
	$result = new Result;

	if ((string)MainConfigOption::get('sale', 'use_sale_discount_only') == 'Y')
		return $result;
	if (empty($this->basketDiscountList))
		return $result;

	/** @var DiscountCouponsManager $couponClassName */
	$couponClassName = $this->getDiscountCouponClassName();

	$applyExist = $this->isBasketApplyResultExist();

	$applyBlock = &$this->discountResult['APPLY_BLOCKS'][$this->discountResultCounter]['BASKET'];

	foreach ($this->getBasketCodes(true) as $basketCode)
	{
		if ($this->isOrderNew() && array_key_exists($basketCode, $applyBlock))
			unset($applyBlock[$basketCode]);
		if (empty($this->basketDiscountList[$basketCode]))
			continue;

		$itemData = array(
			'MODULE_ID' => $this->orderData['BASKET_ITEMS'][$basketCode]['MODULE'],
			'PRODUCT_ID' => $this->orderData['BASKET_ITEMS'][$basketCode]['PRODUCT_ID'],
			'BASKET_ID' => $basketCode
		);
		foreach ($this->basketDiscountList[$basketCode] as $index => $discount)
		{
			$discountResult = $this->convertDiscount($discount);
			if (!$discountResult->isSuccess())
			{
				$result->addErrors($discountResult->getErrors());
				unset($discountResult);
				return $result;
			}
			$orderDiscountId = $discountResult->getId();
			$discountData = $discountResult->getData();
			$orderCouponId = '';
			$this->basketDiscountList[$basketCode][$index]['ORDER_DISCOUNT_ID'] = $orderDiscountId;
			if ($discountData['USE_COUPONS'] == 'Y')
			{
				if (empty($discount['COUPON']))
				{
					$result->addError(new MainEntityEntityError(
						Loc::getMessage('BX_SALE_DISCOUNT_ERR_DISCOUNT_WITHOUT_COUPON'),
						self::ERROR_ID
					));
					return $result;
				}
				$couponResult = $this->convertCoupon($discount['COUPON'], $orderDiscountId);
				if (!$couponResult->isSuccess())
				{
					$result->addErrors($couponResult->getErrors());
					unset($couponResult);
					return $result;
				}
				$orderCouponId = $couponResult->getId();

				$couponClassName::setApplyByProduct($itemData, array($orderCouponId));
				unset($couponResult);
			}
			unset($discountData, $discountResult);
			if (!isset($applyBlock[$basketCode]))
				$applyBlock[$basketCode] = array();
			$applyBlock[$basketCode][$index] = array(
				'DISCOUNT_ID' => $orderDiscountId,
				'COUPON_ID' => $orderCouponId,
				'RESULT' => array(
					'APPLY' => 'Y',
					'DESCR' => false,
					'DESCR_DATA' => false
				)
			);

			$currentProduct = $this->orderData['BASKET_ITEMS'][$basketCode];
			$orderApplication = (
				!empty($this->discountsCache[$orderDiscountId]['APPLICATION'])
				? $this->discountsCache[$orderDiscountId]['APPLICATION']
				: null
			);
			if (!empty($orderApplication))
			{
				$this->orderData['BASKET_ITEMS'][$basketCode]['DISCOUNT_RESULT'] = (
					!empty($this->discountsCache[$orderDiscountId]['ACTIONS_DESCR_DATA'])
					? $this->discountsCache[$orderDiscountId]['ACTIONS_DESCR_DATA']
					: false
				);

				$applyProduct = null;
				eval('$applyProduct='.$orderApplication.';');
				if (is_callable($applyProduct))
					$applyProduct($this->orderData['BASKET_ITEMS'][$basketCode]);
				unset($applyProduct);

				if (!empty($this->orderData['BASKET_ITEMS'][$basketCode]['DISCOUNT_RESULT']))
				{
					$applyBlock[$basketCode][$index]['RESULT']['DESCR_DATA'] = $this->orderData['BASKET_ITEMS'][$basketCode]['DISCOUNT_RESULT']['BASKET'];
					$applyBlock[$basketCode][$index]['RESULT']['DESCR'] = $this->formatDescription($this->orderData['BASKET_ITEMS'][$basketCode]['DISCOUNT_RESULT']);
				}
				unset($this->orderData['BASKET_ITEMS'][$basketCode]['DISCOUNT_RESULT']);
			}
			unset($orderApplication);

			if ($applyExist && !$this->getStatusApplyBasketDiscount($basketCode, $orderDiscountId, $orderCouponId))
			{
				$this->orderData['BASKET_ITEMS'][$basketCode] = $currentProduct;
				$applyBlock[$basketCode][$index]['RESULT']['APPLY'] = 'N';
			}
			unset($disable, $currentProduct);
			if ($applyBlock[$basketCode][$index]['RESULT']['APPLY'] == 'Y')
				$this->orderData['BASKET_ITEMS'][$basketCode]['ACTION_APPLIED'] = 'Y';
		}
		unset($discount, $index);
	}
	unset($basketCode);

	unset($applyBlock);

	return $result;
}