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

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

	$this->orderData['DISCOUNT_RESULT'] = DiscountActions::getActionResult();
	$this->orderData['DISCOUNT_DESCR'] = DiscountActions::getActionDescription();
	if (!empty($this->orderData['DISCOUNT_RESULT']) && is_array($this->orderData['DISCOUNT_RESULT']))
	{
		$stepResult = $this->getStepResult($this->orderData);
	}
	else
	{
		$stepResult = DiscountResultCompatibleFormat::getStepResult(
			$this->orderData,
			$this->currentStep['oldData']
		);
		if (!empty($stepResult))
		{
			if (empty($this->orderData['DISCOUNT_DESCR']) || !is_array($this->orderData['DISCOUNT_DESCR']))
				$this->orderData['DISCOUNT_DESCR'] = DiscountResultCompatibleFormat::getDiscountDescription($stepResult);
		}
	}

	DiscountActions::fillCompatibleFields($this->orderData);
	$applied = !empty($stepResult);

	$orderDiscountId = 0;
	$orderCouponId = '';

	if ($applied)
	{
		$this->correctStepResult($stepResult, $this->currentStep['discount']);

		$this->currentStep['discount']['ACTIONS_DESCR'] = $this->orderData['DISCOUNT_DESCR'];
		$discountResult = $this->convertDiscount($this->currentStep['discount']);
		if (!$discountResult->isSuccess())
		{
			$result->addErrors($discountResult->getErrors());
			return $result;
		}
		$orderDiscountId = $discountResult->getId();
		$discountData = $discountResult->getData();

		$this->currentStep['discount']['ORDER_DISCOUNT_ID'] = $orderDiscountId;

		if ($discountData['USE_COUPONS'] == 'Y')
		{
			if (empty($this->currentStep['discount']['COUPON']))
			{
				$result->addError(new MainEntityEntityError(
					Loc::getMessage('BX_SALE_DISCOUNT_ERR_DISCOUNT_WITHOUT_COUPON'),
					self::ERROR_ID
				));

				return $result;
			}
			$couponResult = $this->convertCoupon($this->currentStep['discount']['COUPON']['COUPON'], $orderDiscountId);
			if (!$couponResult->isSuccess())
			{
				$result->addErrors($couponResult->getErrors());
				unset($couponResult);

				return $result;
			}
			$orderCouponId = $couponResult->getId();
			$couponClassName::setApply($orderCouponId, $stepResult);
			unset($couponResult);
		}
		$this->setDiscountStoredActionData($orderDiscountId, DiscountActions::getStoredData());
	}
	unset($this->orderData['DISCOUNT_DESCR'], $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->applyResult['BASKET']) && is_array($this->applyResult['BASKET']))
			{
				foreach ($this->applyResult['BASKET'] as $basketCode => $discountList)
				{
					if (
						is_array($discountList) && !empty($discountList[$orderDiscountId]) && $discountList[$orderDiscountId] == 'N'
					)
					{
						if (empty($stepResult['BASKET'][$basketCode]))
							continue;
						$stepResult['BASKET'][$basketCode]['APPLY'] = 'N';
						$this->orderData['BASKET_ITEMS'][$basketCode] = $this->currentStep['oldData']['BASKET_ITEMS'][$basketCode];
					}
				}
				unset($basketCode, $discountList);
			}
			if (!empty($this->applyResult['DELIVERY']))
			{
				if (
					is_array($this->applyResult['DELIVERY']) && !empty($this->applyResult['DELIVERY'][$orderDiscountId]) && $this->applyResult['DELIVERY'][$orderDiscountId] == 'N'
				)
				{
					$this->orderData['PRICE_DELIVERY'] = $this->currentStep['oldData']['PRICE_DELIVERY'];
					$this->orderData['PRICE_DELIVERY_DIFF'] = $this->currentStep['oldData']['PRICE_DELIVERY_DIFF'];
					$stepResult['DELIVERY']['APPLY'] = 'N';
				}
			}
		}
	}

	if ($applied && $orderCouponId != '')
	{
		$couponApply = $couponClassName::setApply($this->couponsCache[$orderCouponId]['COUPON'], $stepResult);
		unset($couponApply);
	}

	if ($applied)
	{
		$this->tryToRevertApplyStatusInBlocks($stepResult);

		if (!empty($stepResult['BASKET']))
		{
			foreach ($stepResult['BASKET'] as $basketCode => $itemResult)
			{
				if ($itemResult['APPLY'] == 'Y')
					$this->orderData['BASKET_ITEMS'][$basketCode]['ACTION_APPLIED'] = 'Y';
			}
			unset($basketCode, $itemResult);
		}

		$this->discountResult['APPLY_BLOCKS'][$this->discountResultCounter]['ORDER'][] = array(
			'DISCOUNT_ID' => $orderDiscountId,
			'COUPON_ID' => $orderCouponId,
			'RESULT' => $stepResult
		);
		if ($this->currentStep['discount']['LAST_DISCOUNT'] == 'Y')
			$this->currentStep['stop'] = true;

		if ($this->currentStep['discount']['LAST_LEVEL_DISCOUNT'] == 'Y')
			$this->currentStep['stopLevel'] = true;
	}

	return $result;
}