• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/model/price.php
  • Класс: BitrixCatalogModelPrice
  • Вызов: Price::prepareForUpdate
static function prepareForUpdate(ORMDataUpdateResult $result, $id, array &$data): void
{
	$id = (int)$id;
	if ($id <= 0)
	{
		$result->addError(new ORMEntityError(
			Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_PRICE_ID')
		));
		return;
	}

	if (self::$separateSkuMode === null)
	{
		self::loadSettings();
	}

	$fields = $data['fields'];
	parent::prepareForUpdate($result, $id, $fields);
	if (!$result->isSuccess())
		return;

	$blackList = [
		'ID' => true
	];

	$fields = array_diff_key($fields, $blackList);

	if (array_key_exists('PRODUCT_ID', $fields))
	{
		$fields['PRODUCT_ID'] = (int)$fields['PRODUCT_ID'];
		if ($fields['PRODUCT_ID'] <= 0)
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_PRODUCT_ID')
			));
	}

	if (array_key_exists('CATALOG_GROUP_ID', $fields))
	{
		$fields['CATALOG_GROUP_ID'] = (int)$fields['CATALOG_GROUP_ID'];
		if (!isset(self::$priceTypes[$fields['CATALOG_GROUP_ID']]))
		{
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_CATALOG_GROUP_ID')
			));
		}
		else
		{
			if ($fields['CATALOG_GROUP_ID'] == self::$basePriceType)
			{
				$fields['EXTRA_ID'] = null;
				if (isset($data['actions']['OLD_RECOUNT']))
				{
					if ($data['actions']['OLD_RECOUNT'] === true)
						$data['actions']['PARENT_PRICE'] = true;
				}
			}
		}
	}

	if (isset($fields['TMP_ID']))
		$fields['TMP_ID'] = mb_substr($fields['TMP_ID'], 0, 40);

	$existQuantityFrom = array_key_exists('QUANTITY_FROM', $fields);
	$existQuantityTo = array_key_exists('QUANTITY_TO', $fields);
	if ($existQuantityFrom != $existQuantityTo)
	{
		if ($existQuantityFrom)
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_QUANTITY_RANGE_LEFT_BORDER_ONLY')
			));
		else
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_QUANTITY_RANGE_RIGHT_BORDER_ONLY')
			));
	}
	else
	{
		if ($existQuantityFrom)
			static::checkQuantityRange($result, $fields);
	}
	unset($existQuantityTo, $existQuantityFrom);

	if (isset($fields['EXTRA_ID']))
	{
		$fields['EXTRA_ID'] = (int)$fields['EXTRA_ID'];
		if (!isset(self::$extraList[$fields['EXTRA_ID']]))
		{
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_EXTRA_ID')
			));
		}
		else
		{
			if (
				(!isset($fields['PRICE']) && !isset($fields['CURRENCY']))
				|| (isset($data['actions']['OLD_RECOUNT']) && $data['actions']['OLD_RECOUNT'] === true)
			)
				self::calculatePriceFromBase($id, $fields);
		}
	}

	if (array_key_exists('PRICE', $fields))
	{
		$fields['PRICE'] = self::checkPriceValue($fields['PRICE']);
		if ($fields['PRICE'] === null || $fields['PRICE'] < 0)
		{
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_PRICE')
			));
		}
	}

	if (array_key_exists('CURRENCY', $fields))
	{
		$fields['CURRENCY'] = (string)$fields['CURRENCY'];
		if (!CurrencyCurrencyManager::isCurrencyExist($fields['CURRENCY']))
		{
			$result->addError(new ORMEntityError(
				Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_CURRENCY')
			));
		}
	}

	if ($result->isSuccess())
	{
		if (isset($data['actions']['PARENT_PRICE']))
			unset($data['actions']['PARENT_PRICE']);

		$priceScale = !isset($fields['PRICE_SCALE']) && (isset($fields['PRICE']) || isset($fields['CURRENCY']));
		$needCalculatePrice = (
			!self::$separateSkuMode
			&& (
				isset($fields['PRODUCT_ID'])
				|| isset($fields['CATALOG_GROUP_ID'])
				|| isset($fields['PRICE'])
				|| isset($fields['CURRENCY'])
			)
		);
		$recountPrices = isset($data['actions']['RECOUNT_PRICES']);

		$copyFields = [];
		if ($priceScale || $needCalculatePrice || $recountPrices)
			$copyFields = array_merge(static::getCacheItem($id, true), $fields);

		if (isset($fields['PRICE_SCALE']))
		{
			$fields['PRICE_SCALE'] = self::checkPriceValue($fields['PRICE_SCALE']);
			if ($fields['PRICE_SCALE'] === null || $fields['PRICE_SCALE'] < 0)
				unset($fields['PRICE_SCALE']);
		}
		if (!isset($fields['PRICE_SCALE']))
		{
			if (isset($fields['PRICE']) || isset($fields['CURRENCY']))
			{
				//TODO: replace CCurrency::GetByID to d7 cached method
				$currency = CCurrency::GetByID($copyFields['CURRENCY']);
				$fields['PRICE_SCALE'] = $copyFields['PRICE']*$currency['CURRENT_BASE_RATE'];
				unset($currency);
			}
		}
		if ($needCalculatePrice)
		{
			$product = Product::getCacheItem($copyFields['PRODUCT_ID'], true);
			if (!empty($product) && $product['TYPE'] == CatalogProductTable::TYPE_OFFER)
				$data['actions']['PARENT_PRICE'] = true;
			unset($product);
		}
		if (isset($data['actions']['RECOUNT_PRICES']))
		{
			if ($copyFields['CATALOG_GROUP_ID'] != self::$basePriceType)
				unset($data['actions']['RECOUNT_PRICES']);
			else
				$data['actions']['RECOUNT_PRICES'] = true;
		}

		unset($copyFields);

		$data['fields'] = $fields;
	}
	unset($fields);
}