• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/controller/price.php
  • Класс: BitrixCatalogControllerPrice
  • Вызов: Price::modifyValidate
private function modifyValidate(array $items): Result
{
	$r = new Result();
	$items = array_values($items);
	$basePriceType = GroupTable::getBasePriceType();
	$basePriceTypeId = $basePriceType['ID'];
	$groupTypes = GroupTable::getTypeList();
	$sortedByType = [];
	$extendPrices = false;
	foreach ($items as $fields)
	{
		$groupId = (int)$fields['CATALOG_GROUP_ID'];
		if (!isset($groupTypes[$groupId]))
		{
			$r->addError(new Error('Validate price error. Catalog price group is wrong'));

			return $r;
		}

		if (!$extendPrices)
		{
			$extendPrices = (isset($fields['QUANTITY_FROM']) || isset($fields['QUANTITY_TO']));
		}
		$sortedByType[$groupId][] = $fields;
	}

	$allowEmptyRange = Option::get('catalog', 'save_product_with_empty_price_range') === 'Y';
	$enableQuantityRanges = Feature::isPriceQuantityRangesEnabled();

	if (!$extendPrices)
	{
		if (count($items) > count($sortedByType))
		{
			$r->addError(new Error('Validate price error. Catalog product is allowed has only single price without ranges in price group.'));
		}

		return $r;
	}

	if ($enableQuantityRanges === false)
	{
		$r->addError(new Error('Validate price error. Price quantity ranges disabled'));

		return $r;
	}

	$basePrices = $sortedByType[$basePriceTypeId];
	if (!$basePrices)
	{
		$r->addError(new Error('Validate price error. Ranges of base price are not equal to another price group range'));

		return $r;
	}

	$basePrices = $this->sortPriceRanges($basePrices);

	foreach ($sortedByType as $typeId => $prices)
	{
		$count = count($prices);
		$prices = $this->sortPriceRanges($prices);

		foreach ($prices as $i => $item)
		{
			$quantityFrom = (float)$item['QUANTITY_FROM'];
			$quantityTo = (float)$item['QUANTITY_TO'];

			if (
				$typeId !== $basePriceTypeId
				&& (
					!isset($basePrices[$i])
					|| $quantityFrom !== (float)$basePrices[$i]['QUANTITY_FROM']
					|| $quantityTo !== (float)$basePrices[$i]['QUANTITY_TO']
				)
			)
			{
				$r->addError(
					new Error(
						'Validate price error. Ranges of base price are not equal to another price group range'
					)
				);

				return $r;
			}

			if (
				($i !== 0 && $quantityFrom <= 0)
				|| ($i === 0 && $quantityFrom < 0)
			)
			{
				$r->addError(
					new Error(
						"Quantity bounds error: lower bound {$quantityFrom} must be above zero (for the first range)"
					)
				);
			}

			if (
				($i !== $count-1 && $quantityTo <= 0)
				|| ($i === $count-1 && $quantityTo < 0)
			)
			{
				$r->addError(
					new Error(
						"Quantity bounds error: higher bound {$quantityTo} must be above zero (for the last range)"
					)
				);

			}

			if ($quantityFrom > $quantityTo	&& ($i !== $count-1 || $quantityTo > 0))
			{
				$r->addError(
					new Error(
						"Quantity bounds error: range {$quantityFrom}-{$quantityTo} is incorrect"
					)
				);
			}

			$nextQuantityFrom = (float)$prices[$i + 1]["QUANTITY_FROM"];
			$nextQuantityTo = (float)$prices[$i + 1]["QUANTITY_TO"];
			if ($i < $count-1 && $quantityTo >= $nextQuantityFrom)
			{
				$r->addError(
					new Error(
						"Quantity bounds error: ranges {$quantityFrom}-{$quantityTo} and {$nextQuantityFrom}-{$nextQuantityTo} overlap"
					)
				);
			}

			if (
				$i < $count-1
				&& $nextQuantityFrom - $quantityTo > 1
				&& !$allowEmptyRange
			)
			{
				$validRangeFrom = $quantityTo + 1;
				$validRangeTo = $nextQuantityFrom - 1;

				$r->addError(
					new Error(
						"Invalid quantity range entry: no price is specified for range {$validRangeFrom}-{$validRangeTo})"
					)
				);
			}

			if ($i >= $count-1 && $quantityTo > 0)
			{
				$r->addError(
					new Error(
						"Invalid quantity range entry: no price is specified for quantity over {$quantityTo}"
					)
				);
			}
		}
	}

	return $r;
}