• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/model/price.php
  • Класс: BitrixCatalogModelPrice
  • Вызов: Price::prepareForAdd
static function prepareForAdd(ORMDataAddResult $result, $id, array &$data): void
{
	$fields = $data['fields'];
	parent::prepareForAdd($result, $id, $fields);
	if (!$result->isSuccess())
		return;

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

	static $defaultValues = null,
		$blackList = null;

	if ($defaultValues === null)
	{
		$defaultValues = [
			'PRODUCT_ID' => 0,
			'CATALOG_GROUP_ID' => 0,
			'EXTRA_ID' => null,
			'PRICE' => null,
			'CURRENCY' => null,
			'QUANTITY_FROM' => null,
			'QUANTITY_TO' => null,
			'TMP_ID' => null
		];

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

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

	$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')
		));
		return;
	}
	$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')
		));
		return;
	}
	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 ($fields['TMP_ID'] !== null)
		$fields['TMP_ID'] = mb_substr($fields['TMP_ID'], 0, 40);

	static::checkQuantityRange($result, $fields);

	if ($fields['EXTRA_ID'] !== null)
	{
		$fields['EXTRA_ID'] = (int)$fields['EXTRA_ID'];
		if (!isset(self::$extraList[$fields['EXTRA_ID']]))
		{
			unset($fields['EXTRA_ID']);
		}
		else
		{
			if (
				(!isset($fields['PRICE']) && !isset($fields['CURRENCY']))
				|| (isset($data['actions']['OLD_RECOUNT']) && $data['actions']['OLD_RECOUNT'] === true)
			)
				self::calculatePriceFromBase(null, $fields);
		}
	}

	if ($fields['PRICE'] === null)
	{
		$result->addError(new ORMEntityError(
			Loc::getMessage('BX_CATALOG_MODEL_PRICE_ERR_WRONG_PRICE')
		));
	}
	else
	{
		$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')
			));
		}
	}
	$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())
	{
		$fields['TIMESTAMP_X'] = new MainTypeDateTime();
		if (isset($fields['PRICE_SCALE']))
			$fields['PRICE_SCALE'] = self::checkPriceValue($fields['PRICE_SCALE']);
		// self::checkPriceValue return float or null
		if (!isset($fields['PRICE_SCALE']))
		{
			//TODO: replace CCurrency::GetByID to d7 cached method
			$currency = CCurrency::GetByID($fields['CURRENCY']);
			$fields['PRICE_SCALE'] = $fields['PRICE']*$currency['CURRENT_BASE_RATE'];
			unset($currency);
		}

		if (isset($data['actions']['PARENT_PRICE']))
			unset($data['actions']['PARENT_PRICE']);
		if (!self::$separateSkuMode)
		{
			$product = Product::getCacheItem($fields['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 ($fields['CATALOG_GROUP_ID'] != self::$basePriceType)
				unset($data['actions']['RECOUNT_PRICES']);
			else
				$data['actions']['RECOUNT_PRICES'] = true;
		}

		$data['fields'] = $fields;
	}

	unset($fields);
}