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

	$basketDiscountResult = $this->calculateApplyBasketDiscount();
	if (!$basketDiscountResult->isSuccess())
	{
		$result->addErrors($basketDiscountResult->getErrors());
		unset($basketDiscountResult);

		return $result;
	}
	unset($basketDiscountResult);

	$roundApply = false;
	if ($this->isRoundMode(self::ROUND_MODE_BASKET_DISCOUNT))
	{
		$roundApply = true;
		$this->roundApplyBasketPrices();
	}

	if ($this->isRoundMode(self::ROUND_MODE_SALE_DISCOUNT) && !$roundApply)
	{
		$this->roundApplyBasketPricesByIndex(array(
			'DISCOUNT_INDEX' => -1,
			'DISCOUNT_ID' => 0
		));
	}
	if (!empty($this->discountResult['APPLY_BLOCKS'][$this->discountResultCounter]['ORDER']))
	{
		$index = -1;
		foreach ($this->discountResult['APPLY_BLOCKS'][$this->discountResultCounter]['ORDER'] as $indexDiscount => $discount)
		{
			$index++;
			$orderDiscountId = $discount['DISCOUNT_ID'];
			if (!isset($this->discountsCache[$orderDiscountId]))
			{
				$result->addError(new MainEntityEntityError(
					Loc::getMessage('BX_SALE_DISCOUNT_ERR_APPLY_WITHOUT_SALE_DISCOUNT'),
					self::ERROR_ID
				));
				return $result;
			}
			DiscountActions::clearAction();
			if (!empty($discount['RESULT']['BASKET']))
			{
				if ($discount['ACTION_BLOCK_LIST'])
				{
					$applyResultMode = DiscountActions::APPLY_RESULT_MODE_COUNTER;
					$blockList = array();
					foreach ($discount['RESULT']['BASKET'] as $basketCode => $basketItem)
						$blockList[$basketCode] = $basketItem['ACTION_BLOCK_LIST'];
					unset($basketCode, $basketItem);
				}
				else
				{
					if ($this->discountsCache[$orderDiscountId]['SIMPLE_ACTION'])
					{
						$applyResultMode = DiscountActions::APPLY_RESULT_MODE_SIMPLE;
						$blockList = array_fill_keys(array_keys($discount['RESULT']['BASKET']), true);
					}
					else
					{
						$applyResultMode = DiscountActions::APPLY_RESULT_MODE_DESCR;
						$blockList = array();
						foreach ($discount['RESULT']['BASKET'] as $basketCode => $basketItem)
							$blockList[$basketCode] = $basketItem['DESCR_DATA'];
						unset($basketCode, $basketItem);
					}
				}
				DiscountActions::setApplyResultMode($applyResultMode);
				DiscountActions::setApplyResult(array('BASKET' => $blockList));
				unset($blockList, $applyResultMode);
			}
			if ($this->isRoundMode(self::ROUND_MODE_SALE_DISCOUNT) && !$roundApply)
			{
				$this->roundApplyBasketPricesByIndex(array(
					'DISCOUNT_INDEX' => $index,
					'DISCOUNT_ID' => $orderDiscountId
				));
			}
			$this->fillCurrentStep(array(
				'discountIndex' => $indexDiscount,
				'discountId' => $orderDiscountId,
			));
			$actionsResult = $this->applySaleDiscount();
			if (!$actionsResult->isSuccess())
			{
				$result->addErrors($actionsResult->getErrors());
				unset($actionsResult);
				return $result;
			}
			unset($orderDiscountId);
		}
		if ($this->isRoundMode(self::ROUND_MODE_SALE_DISCOUNT) && !$roundApply)
		{
			$index++;
			$this->roundApplyBasketPricesByIndex(array(
				'DISCOUNT_INDEX' => $index,
				'DISCOUNT_ID' => 0
			));
			$roundConfig = $this->getRoundIndex('BASKET_ROUND');
			if (is_array($roundConfig))
			{
				if ($roundConfig['DISCOUNT_INDEX'] > $index && $roundConfig['DISCOUNT_ID'] == 0)
				{
					$this->roundApplyBasketPricesByIndex(array(
						'DISCOUNT_INDEX' => $roundConfig['DISCOUNT_INDEX'],
						'DISCOUNT_ID' => 0
					));
				}
			}
			unset($roundConfig);
		}
		unset($discount, $indexDiscount, $currentList);
	}

	if ($this->isRoundMode(self::ROUND_MODE_FINAL_PRICE))
		$this->roundApplyBasketPrices();

	$this->fillEmptyCurrentStep();

	return $result;
}