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

	$orderId = $this->getOrder()->getId();

	$applyBlock = &$this->discountResult['APPLY_BLOCKS'][$this->discountResultCounter];
	/** @var OrderDiscount $storageClassName */
	$storageClassName = $this->getOrderDiscountClassName();

	$rulesList = array();
	if (!empty($applyBlock['BASKET']))
	{
		foreach ($applyBlock['BASKET'] as $basketCode => $discountList)
		{
			$commonFields = $this->getEntitySaveIdentifier([
				'ENTITY_TYPE' => self::ENTITY_BASKET_ITEM,
				'ENTITY_CODE' => $basketCode
			]);
			if ($commonFields === null)
				continue;
			foreach ($discountList as $discount)
			{
				$rulesList[] = $commonFields + [
					'ORDER_DISCOUNT_ID' => $discount['DISCOUNT_ID'],
					'COUPON_ID' => $discount['COUPON_ID'],
					'APPLY' => $discount['RESULT']['APPLY'],
					'DESCR' => $discount['RESULT']['DESCR_DATA']
				];
			}
			unset($discount);
		}
		unset($discount, $commonFields, $discountList, $basketCode);
	}
	if (!empty($applyBlock['ORDER']))
	{
		foreach ($applyBlock['ORDER'] as $discount)
		{
			if (!empty($discount['RESULT']['BASKET']))
			{
				foreach ($discount['RESULT']['BASKET'] as $basketCode => $applyData)
				{
					$commonFields = $this->getEntitySaveIdentifier([
						'ENTITY_TYPE' => self::ENTITY_BASKET_ITEM,
						'ENTITY_CODE' => $basketCode
					]);
					if ($commonFields === null)
						continue;
					$rulesList[] = $commonFields + [
						'ORDER_DISCOUNT_ID' => $discount['DISCOUNT_ID'],
						'COUPON_ID' => $discount['COUPON_ID'],
						'APPLY' => $applyData['APPLY'],
						'ACTION_BLOCK_LIST' => $applyData['ACTION_BLOCK_LIST'],
						'DESCR' => $applyData['DESCR_DATA']
					];
				}
				unset($commonFields, $basketCode, $applyData);
			}
			if (!empty($discount['RESULT']['DELIVERY']))
			{
				$commonFields = $this->getEntitySaveIdentifier([
					'ENTITY_TYPE' => self::ENTITY_DELIVERY,
					'ENTITY_CODE' => $discount['RESULT']['DELIVERY']['DELIVERY_ID']
				]);
				if ($commonFields === null)
					continue;
				$rulesList[] = $commonFields + [
					'ORDER_DISCOUNT_ID' => $discount['DISCOUNT_ID'],
					'COUPON_ID' => $discount['COUPON_ID'],
					'APPLY' => $discount['RESULT']['DELIVERY']['APPLY'],
					'DESCR' => $discount['RESULT']['DELIVERY']['DESCR_DATA']
				];
				unset($commonFields);
			}
		}
		unset($discount);
	}

	if (!empty($rulesList))
	{
		$this->normalizeNewResultRows($rulesList);
		$blockResult = $storageClassName::addResultBlock($orderId, $rulesList);
		if (!$blockResult->isSuccess())
		{
			$result->addError(new MainEntityEntityError(
				Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'),
				self::ERROR_ID
			));
		}
		unset($blockResult);
	}
	unset($rulesList);

	if (!empty($applyBlock['BASKET_ROUND']))
	{
		$roundList = array();
		foreach ($applyBlock['BASKET_ROUND'] as $basketCode => $roundData)
		{
			$commonFields = $this->getEntitySaveIdentifier([
				'ENTITY_TYPE' => self::ENTITY_BASKET_ITEM,
				'ENTITY_CODE' => $basketCode
			]);
			if ($commonFields === null)
				continue;
			$roundList[] = $commonFields + [
				'APPLY' => $roundData['APPLY'],
				'ROUND_RULE' => $roundData['ROUND_RULE']
			];
		}
		unset($commonFields, $roundData, $basketCode);
		if (!empty($roundList))
		{
			$this->normalizeNewResultRows($roundList);
			$blockResult = $storageClassName::addRoundBlock($orderId, $roundList);
			if (!$blockResult->isSuccess())
			{
				$result->addError(new MainEntityEntityError(
					Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'),
					self::ERROR_ID
				));
			}
			unset($blockResult);
		}
		unset($roundList);
	}

	unset($applyBlock);

	return $result;
}