• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/compatible/discountcompatibility.php
  • Класс: BitrixSaleCompatibleDiscountCompatibility
  • Вызов: DiscountCompatibility::calculateSaleDiscount
static function calculateSaleDiscount(&$order, $discount)
{
	if (!self::$init)
		self::init();
	if (!self::isSuccess())
		return false;

	$order['DISCOUNT_RESULT'] = SaleDiscountActions::getActionResult();
	$order['DISCOUNT_DESCR'] = SaleDiscountActions::getActionDescription();
	if (!empty($order['DISCOUNT_RESULT']) && is_array($order['DISCOUNT_RESULT']))
	{
		$stepResult = self::getStepResult($order);
	}
	else
	{
		$stepResult = self::getStepResultOld($order);
		if (!empty($stepResult))
		{
			if (empty($order['DISCOUNT_DESCR']) || !is_array($order['DISCOUNT_DESCR']))
				$order['DISCOUNT_DESCR'] = DiscountResultCompatibleFormat::getDiscountDescription($stepResult);
		}
	}
	SaleDiscountActions::fillCompatibleFields($order);
	$applied = !empty($stepResult);

	$orderDiscountId = 0;
	$orderCouponId = '';

	if ($applied)
	{
		self::correctStepResult($order, $stepResult, $discount);

		$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();
			SaleDiscountCouponsManager::setApply($orderCouponId, $stepResult);
			unset($couponResult);
		}
	}

	unset($order['DISCOUNT_DESCR'], $order['DISCOUNT_RESULT']);

	self::$currentOrderData = $order;
	if ($applied && $orderCouponId != '')
	{
		$couponApply = SaleDiscountCouponsManager::setApply(self::$couponsCache[$orderCouponId]['COUPON'], $stepResult);
		unset($couponApply);
	}

	if ($applied)
	{
		if (!empty($stepResult['BASKET']))
		{
			$publicMode = self::usedByClient();
			foreach ($order['BASKET_ITEMS'] as $basketCode => $basketItem)
			{
				$code = ($publicMode ? $basketItem['ID'] : $basketCode);
				if (empty($stepResult['BASKET'][$code]))
					continue;
				if ($stepResult['BASKET'][$code]['APPLY'] == 'Y')
					$order['BASKET_ITEMS'][$basketCode]['ACTION_APPLIED'] = 'Y';
			}
			unset($code, $basketCode, $basketItem);
			unset($publicMode);
		}

		self::$discountResult['ORDER'][] = array(
			'DISCOUNT_ID' => $orderDiscountId,
			'COUPON_ID' => $orderCouponId,
			'RESULT' => $stepResult
		);
		return true;
	}

	return false;
}