• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/orderdiscountbase.php
  • Класс: BitrixSaleOrderDiscountBase
  • Вызов: OrderDiscountBase::saveStoredDataBlock
static function saveStoredDataBlock($order, $storageType, array $block, array $options = array())
{
	$result = new Result;

	$order = (int)$order;
	if ($order <= 0)
	{
		$result->addError(new MainError(
			Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_BAD_ORDER_ID'),
			self::ERROR_ID
		));
		return $result;
	}

	$storageType = static::getStorageTypeInternal($storageType);
	if ($storageType === null)
	{
		$result->addError(new MainError(
			Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_BAD_STORAGE_TYPE'),
			self::ERROR_ID
		));
		return $result;
	}

	$allowUpdate = (isset($options['ALLOW_UPDATE']) && $options['ALLOW_UPDATE'] === 'Y');
	$deleteMissing = (isset($options['DELETE_MISSING']) && $options['DELETE_MISSING'] === 'Y');

	$collection = [];
	$deleteList = [];
	$iterator = static::getStoredDataIterator([
		'select' => ['*'],
		'filter' => [
			'=ORDER_ID' => $order,
			'=ENTITY_TYPE' => $storageType,
		]
	]);
	while ($row = $iterator->fetch())
	{
		$index = static::getEntityIndex($row);
		$collection[$index] = $row;
		$deleteList[$index] = $row['ID'];
	}
	unset($row, $iterator);

	$existError = false;
	foreach ($block as $index => $row)
	{
		if (isset($deleteList[$index]))
			unset($deleteList[$index]);
		if (!empty($collection[$index]) && !$allowUpdate)
		{
			$existError = true;
			continue;
		}
		if (empty($collection[$index]))
		{
			$row['ORDER_ID'] = $order;
			$row['ENTITY_TYPE'] = $storageType;
			$resultInternal = static::addStoredDataInternal($row);
		}
		else
		{
			$resultInternal = static::updateStoredDataInternal(
				$collection[$index]['ID'],
				['ENTITY_DATA' => $row['ENTITY_DATA']]
			);
		}
		if (!$resultInternal->isSuccess())
			$result->addErrors($resultInternal->getErrors());
	}
	unset($resultInternal, $index, $row);

	if ($existError)
	{
		$result->addError(new MainError(
			Loc::getMessage('SALE_ORDER_DISCOUNT_ERR_ORDER_STORED_DATA_ALREADY_EXISTS'),
			self::ERROR_ID
		));
	}
	unset($existError);

	if ($result->isSuccess())
	{
		if ($deleteMissing && !empty($deleteList))
			static::deleteRowsByIndex(static::getStoredDataTableInternal(), 'ID', $deleteList);
	}
	unset($deleteList, $deleteMissing);

	return $result;
}